@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 253 lines 8.52 KB
Raw
1 import { describe, expect } from "bun:test";
2 import { createHash } from "node:crypto";
3 import * as GCP from "@alchemy.run/gcp";
4 import * as Test from "@alchemy.run/test/Bun";
5 import * as firestore from "@distilled.cloud/gcp/firestore_v1";
6 import * as run from "@distilled.cloud/gcp/run_v2";
7 import * as storage from "@distilled.cloud/gcp/storage_v1";
8 import * as Alchemy from "alchemy";
9 import * as Effect from "effect/Effect";
10 import * as FetchHttpClient from "effect/http/FetchHttpClient";
11 import * as HttpClient from "effect/http/HttpClient";
12 import * as HttpClientRequest from "effect/http/HttpClientRequest";
13 import * as Layer from "effect/Layer";
14 import * as Schedule from "effect/Schedule";
15 import Stack from "../alchemy.run.ts";
16
17 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
18 providers: GCP.providers(),
19 state: Alchemy.localState(),
20 });
21
22 const { getWhenReady } = Test;
23
24 // Out-of-band calls to the Google APIs resolve the same stored credentials
25 // the deploy uses, so the test runs against the configured profile.
26 const GcpHttp = Layer.mergeAll(GCP.GcpAuth, GCP.fromAuthProvider(), FetchHttpClient.layer);
27
28 // The project comes from the same credential the deploy uses.
29 const currentProject = GCP.GcpEnvironment.current.pipe(
30 Effect.map((env) => env.project),
31 Effect.provide(GCP.fromCredentials().pipe(Layer.provide(GcpHttp))),
32 );
33
34 // Kept for the post-destroy checks in `afterAll`.
35 let deployed:
36 | {
37 url: string | undefined;
38 indexerUrl: string | undefined;
39 location: string;
40 bucketName: string;
41 databaseName: string;
42 }
43 | undefined;
44
45 // Deploy, tests and destroy all sit behind the same Docker guard.
46 describe("gcp-storage-uploads", () => {
47 const stack = beforeAll(
48 deploy(Stack).pipe(
49 Effect.tap((outputs) =>
50 Effect.sync(() => {
51 deployed = outputs;
52 }),
53 ),
54 ),
55 { timeout: 900_000 },
56 );
57
58 const gone = <A, E extends { _tag: string }, R>(get: Effect.Effect<A, E, R>) =>
59 get.pipe(
60 Effect.as("found" as const),
61 Effect.catchIf(
62 (error) => error._tag === "NotFound",
63 () => Effect.succeed("gone" as const),
64 ),
65 );
66
67 afterAll.skipIf(!!process.env.NO_DESTROY)(
68 Effect.gen(function* () {
69 yield* destroy(Stack);
70 if (deployed === undefined) return;
71 const { bucketName, databaseName, url, indexerUrl, location } = deployed;
72
73 // Destroy leaves nothing behind: bucket (and its notification),
74 // database, and both services.
75 expect(yield* gone(storage.getBuckets({ bucket: bucketName }))).toEqual("gone");
76 // A deleted database may linger briefly with `deleteTime` set.
77 const database = yield* firestore.getProjectsDatabases({ name: databaseName }).pipe(
78 Effect.map((database) => (database.deleteTime === undefined ? "found" : "gone")),
79 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
80 );
81 expect(database).toEqual("gone");
82 const project = yield* currentProject;
83 const services = yield* run.listProjectsLocationsServices({
84 parent: `projects/${project}/locations/${location}`,
85 });
86 const uris = (services.services ?? []).flatMap((service) => [
87 service.uri,
88 ...(service.urls ?? []),
89 ]);
90 expect(uris).not.toContain(url);
91 expect(uris).not.toContain(indexerUrl);
92 }).pipe(Effect.provide(GcpHttp)),
93 { timeout: 600_000 },
94 );
95
96 const baseUrlOf = (url: string | undefined) => {
97 if (url === undefined) throw new Error("the service has no URL");
98 return url.replace(/\/+$/, "");
99 };
100
101 const sha256 = (bytes: Uint8Array) => createHash("sha256").update(bytes).digest("hex");
102
103 const put = (baseUrl: string, name: string, body: Uint8Array, type: string) =>
104 HttpClient.execute(
105 HttpClientRequest.put(`${baseUrl}/files/${name}`).pipe(
106 HttpClientRequest.bodyUint8Array(body, type),
107 ),
108 );
109
110 interface FileRecord {
111 name: string;
112 object: string;
113 generation: string;
114 size: number;
115 contentType: string;
116 sha256: string;
117 indexedAt: string;
118 }
119
120 const listFiles = (baseUrl: string) =>
121 Effect.gen(function* () {
122 const res = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/files`));
123 if (res.status !== 200) return [] as FileRecord[];
124 return ((yield* res.json) as unknown as { files: FileRecord[] }).files;
125 });
126
127 test(
128 "serves the health route and rejects bad names",
129 Effect.gen(function* () {
130 const { url } = yield* stack;
131 expect(url).toMatch(/^https:\/\//);
132 const baseUrl = baseUrlOf(url);
133 const health = yield* getWhenReady(`${baseUrl}/`);
134 expect(health.status).toBe(200);
135
136 const bad = yield* put(
137 baseUrl,
138 encodeURIComponent("../escape"),
139 new TextEncoder().encode("nope"),
140 "text/plain",
141 );
142 expect(bad.status).toBe(400);
143
144 const missing = yield* HttpClient.execute(
145 HttpClientRequest.get(`${baseUrl}/files/never-uploaded.txt`),
146 );
147 expect(missing.status).toBe(404);
148 }),
149 { timeout: 120_000 },
150 );
151
152 test(
153 "uploads are stored, indexed with their sha256, and deleted",
154 Effect.gen(function* () {
155 const { url, bucketName, databaseName } = yield* stack;
156 const baseUrl = baseUrlOf(url);
157 yield* getWhenReady(`${baseUrl}/`);
158
159 const files = [
160 {
161 name: "hello.txt",
162 body: new TextEncoder().encode("Hello, uploads!\n"),
163 type: "text/plain",
164 },
165 {
166 name: "random.bin",
167 body: crypto.getRandomValues(new Uint8Array(64 * 1024)),
168 type: "application/octet-stream",
169 },
170 ];
171
172 for (const file of files) {
173 const res = yield* put(baseUrl, file.name, file.body, file.type);
174 expect(res.status).toBe(202);
175 expect(yield* res.json).toMatchObject({
176 name: file.name,
177 object: `uploads/${file.name}`,
178 size: file.body.byteLength,
179 });
180
181 // The bytes read back through the API are the bytes uploaded.
182 const back = yield* HttpClient.execute(
183 HttpClientRequest.get(`${baseUrl}/files/${file.name}`),
184 );
185 expect(back.status).toBe(200);
186 expect(back.headers["content-type"]).toContain(file.type);
187 expect(new Uint8Array(yield* back.arrayBuffer)).toEqual(file.body);
188 }
189
190 // The indexer runs off the bucket notification. A fresh deploy's
191 // project-level Firestore grants can take minutes to propagate; until
192 // then the push fails and Pub/Sub redelivers.
193 const indexed = yield* listFiles(baseUrl).pipe(
194 Effect.repeat({
195 schedule: Schedule.spaced("10 seconds"),
196 until: (records) =>
197 files.every((file) => records.some((record) => record.name === file.name)),
198 times: 42,
199 }),
200 );
201
202 for (const file of files) {
203 const record = indexed.find((record) => record.name === file.name)!;
204 expect(record).toMatchObject({
205 object: `uploads/${file.name}`,
206 size: file.body.byteLength,
207 sha256: sha256(file.body),
208 });
209 expect(record.contentType).toContain(file.type);
210 expect(record.generation).toMatch(/^\d+$/);
211
212 // The record is a real Firestore document.
213 const document = yield* firestore
214 .getProjectsDatabasesDocuments({
215 name: `${databaseName}/documents/files/${file.name}`,
216 })
217 .pipe(Effect.provide(GcpHttp));
218 expect(document.fields?.sha256?.stringValue).toEqual(sha256(file.body));
219
220 // And the generation it indexed is the live object's.
221 const object = yield* storage
222 .getObjects({ bucket: bucketName, object: `uploads/${file.name}` })
223 .pipe(Effect.provide(GcpHttp));
224 expect(String(object.generation)).toEqual(record.generation);
225 }
226
227 const [deleted] = files;
228 const del = yield* HttpClient.execute(
229 HttpClientRequest.delete(`${baseUrl}/files/${deleted!.name}`),
230 );
231 expect(del.status).toBe(204);
232
233 const after = yield* HttpClient.execute(
234 HttpClientRequest.get(`${baseUrl}/files/${deleted!.name}`),
235 );
236 expect(after.status).toBe(404);
237 const remaining = yield* listFiles(baseUrl);
238 expect(remaining.map((record) => record.name)).toEqual(["random.bin"]);
239
240 expect(
241 yield* gone(
242 storage
243 .getObjects({
244 bucket: bucketName,
245 object: `uploads/${deleted!.name}`,
246 })
247 .pipe(Effect.provide(GcpHttp)),
248 ),
249 ).toEqual("gone");
250 }),
251 { timeout: 600_000 },
252 );
253 });