@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 199 lines 7.84 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as storage from "@distilled.cloud/gcp/storage_v1";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const getObject = (bucket: string, object: string) =>
16 storage
17 .getObjects({ bucket, object })
18 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
19
20 const waitUntilObjectGone = (bucket: string, object: string) =>
21 getObject(bucket, object).pipe(
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (found) => found === undefined,
25 times: 10,
26 }),
27 );
28
29 const waitUntilBucketGone = (bucketName: string) =>
30 storage.getBuckets({ bucket: bucketName }).pipe(
31 Effect.as("found" as const),
32 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
33 Effect.repeat({
34 schedule: Schedule.spaced("1 second"),
35 until: (status) => status === "gone",
36 times: 10,
37 }),
38 );
39
40 test.provider(
41 "inline content: create, update, move key, delete",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 const program = (key: string, content: string, cacheControl?: string) =>
47 Effect.gen(function* () {
48 const bucket = yield* GCP.Storage.Bucket("Objects", {
49 forceDestroy: true,
50 });
51 const object = yield* GCP.Storage.Object("Page", {
52 bucketName: bucket.bucketName,
53 key,
54 content,
55 cacheControl,
56 });
57 return { bucket, object };
58 });
59
60 const created = yield* stack.deploy(program("index.html", "<h1>v1</h1>"));
61 const bucketName = created.bucket.bucketName;
62 expect(created.object.key).toEqual("index.html");
63 expect(created.object.contentType).toEqual("text/html; charset=utf-8");
64 expect(created.object.url).toEqual(`https://storage.googleapis.com/${bucketName}/index.html`);
65
66 const live = yield* getObject(bucketName, "index.html");
67 expect(live?.contentType).toEqual("text/html; charset=utf-8");
68 expect(live?.size).toEqual(String("<h1>v1</h1>".length));
69 expect(live?.metadata?.["alchemy-id"]).toEqual("page");
70
71 // Redeploying identical content leaves the live generation alone.
72 const same = yield* stack.deploy(program("index.html", "<h1>v1</h1>"));
73 expect(same.object.generation).toEqual(created.object.generation);
74
75 // New content and headers converge in one upload.
76 const updated = yield* stack.deploy(program("index.html", "<h1>v2</h1>", "no-cache"));
77 expect(updated.object.generation).not.toEqual(created.object.generation);
78 const liveUpdated = yield* getObject(bucketName, "index.html");
79 expect(liveUpdated?.cacheControl).toEqual("no-cache");
80 expect(liveUpdated?.md5Hash).toEqual(updated.object.md5Hash);
81
82 // Moving the key replaces the object; the old key is deleted.
83 const moved = yield* stack.deploy(program("home.html", "<h1>v2</h1>", "no-cache"));
84 expect(moved.object.key).toEqual("home.html");
85 expect(yield* getObject(bucketName, "home.html")).toBeDefined();
86 yield* waitUntilObjectGone(bucketName, "index.html");
87
88 yield* stack.destroy();
89 yield* waitUntilObjectGone(bucketName, "home.html");
90 yield* waitUntilBucketGone(bucketName);
91 }).pipe(logLevel),
92 { tags: ["provider:gcp", "provider:gcp:storage", "live"], timeout: 180_000 },
93 );
94
95 test.provider(
96 "Files uploads a directory and re-uploads edited files",
97 (stack) =>
98 Effect.gen(function* () {
99 yield* stack.destroy();
100
101 const fs = yield* FileSystem.FileSystem;
102 const path = yield* Path.Path;
103 const dir = yield* fs.makeTempDirectoryScoped({
104 prefix: "alchemy-gcs-files-",
105 });
106 yield* fs.makeDirectory(path.join(dir, "assets"), { recursive: true });
107 yield* fs.writeFileString(path.join(dir, "index.html"), "<h1>one</h1>");
108 yield* fs.writeFileString(path.join(dir, "assets", "app.css"), "body{color:red}");
109 yield* fs.writeFile(
110 path.join(dir, "assets", "logo.png"),
111 new Uint8Array([0x89, 0x50, 0x4e, 0x47]),
112 );
113
114 const program = Effect.gen(function* () {
115 const bucket = yield* GCP.Storage.Bucket("Site", {
116 forceDestroy: true,
117 });
118 const objects = yield* GCP.Storage.Files("SiteFiles", {
119 bucketName: bucket.bucketName,
120 path: dir,
121 prefix: "site",
122 cacheControl: (key) => (key.endsWith(".html") ? "no-cache" : undefined),
123 });
124 return { bucket, objects };
125 });
126
127 const first = yield* stack.deploy(program);
128 const bucketName = first.bucket.bucketName;
129 const byKey = Object.fromEntries(first.objects.map((object) => [object.key, object]));
130 expect(Object.keys(byKey).sort()).toEqual([
131 "site/assets/app.css",
132 "site/assets/logo.png",
133 "site/index.html",
134 ]);
135
136 const html = yield* getObject(bucketName, "site/index.html");
137 expect(html?.contentType).toEqual("text/html; charset=utf-8");
138 expect(html?.cacheControl).toEqual("no-cache");
139 const css = yield* getObject(bucketName, "site/assets/app.css");
140 expect(css?.contentType).toEqual("text/css; charset=utf-8");
141 const png = yield* getObject(bucketName, "site/assets/logo.png");
142 expect(png?.contentType).toEqual("image/png");
143
144 // Editing a file without touching any prop re-uploads only that file.
145 yield* fs.writeFileString(path.join(dir, "index.html"), "<h1>two</h1>");
146 const second = yield* stack.deploy(program);
147 const secondByKey = Object.fromEntries(second.objects.map((object) => [object.key, object]));
148 expect(secondByKey["site/index.html"]!.generation).not.toEqual(
149 byKey["site/index.html"]!.generation,
150 );
151 expect(secondByKey["site/assets/app.css"]!.generation).toEqual(
152 byKey["site/assets/app.css"]!.generation,
153 );
154
155 // Removing a file deletes its object on the next deploy.
156 yield* fs.remove(path.join(dir, "assets", "logo.png"));
157 yield* stack.deploy(program);
158 yield* waitUntilObjectGone(bucketName, "site/assets/logo.png");
159
160 yield* stack.destroy();
161 yield* waitUntilBucketGone(bucketName);
162 }).pipe(Effect.scoped, logLevel),
163 { tags: ["provider:gcp", "provider:gcp:storage", "live"], timeout: 180_000 },
164 );
165
166 test.provider(
167 "bucket website config: set, change, remove",
168 (stack) =>
169 Effect.gen(function* () {
170 yield* stack.destroy();
171
172 const program = (website?: GCP.Storage.BucketWebsite) =>
173 GCP.Storage.Bucket("Web", { forceDestroy: true, website });
174
175 const created = yield* stack.deploy(
176 program({ mainPageSuffix: "index.html", notFoundPage: "404.html" }),
177 );
178 expect(created.website).toEqual({
179 mainPageSuffix: "index.html",
180 notFoundPage: "404.html",
181 });
182
183 const changed = yield* stack.deploy(program({ mainPageSuffix: "home.html" }));
184 const live = yield* storage.getBuckets({ bucket: changed.bucketName });
185 expect(live.website?.mainPageSuffix).toEqual("home.html");
186 expect(live.website?.notFoundPage).toBeUndefined();
187
188 const removed = yield* stack.deploy(program(undefined));
189 expect(removed.website).toBeUndefined();
190 const liveRemoved = yield* storage.getBuckets({
191 bucket: removed.bucketName,
192 });
193 expect(liveRemoved.website?.mainPageSuffix).toBeUndefined();
194
195 yield* stack.destroy();
196 yield* waitUntilBucketGone(created.bucketName);
197 }).pipe(logLevel),
198 { tags: ["provider:gcp", "provider:gcp:storage", "live"], timeout: 180_000 },
199 );