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, 04:45 UTC
| 1 | import { expect } from "bun:test"; |
| 2 | import * as GCP from "@alchemy.run/gcp"; |
| 3 | import * as Test from "@alchemy.run/test/Bun"; |
| 4 | import * as cloudfunctions from "@distilled.cloud/gcp/cloudfunctions_v2"; |
| 5 | import * as firestore from "@distilled.cloud/gcp/firestore_v1"; |
| 6 | import * as Alchemy from "alchemy"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import Stack from "../alchemy.run.ts"; |
| 14 | |
| 15 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 16 | providers: GCP.providers(), |
| 17 | state: Alchemy.localState(), |
| 18 | }); |
| 19 | |
| 20 | // Out-of-band calls to the Google APIs resolve the same stored credentials |
| 21 | // the deploy uses, so the test runs against the configured profile. |
| 22 | const GcpHttp = Layer.mergeAll(GCP.GcpAuth, GCP.fromAuthProvider(), FetchHttpClient.layer); |
| 23 | |
| 24 | interface Note { |
| 25 | id: string; |
| 26 | title: string; |
| 27 | body: string; |
| 28 | createdAt: string; |
| 29 | } |
| 30 | |
| 31 | const stack = beforeAll(deploy(Stack), { timeout: 900_000 }); |
| 32 | |
| 33 | afterAll.skipIf(!!process.env.NO_DESTROY)( |
| 34 | Effect.gen(function* () { |
| 35 | const { functionName, databaseName } = yield* stack; |
| 36 | yield* destroy(Stack); |
| 37 | |
| 38 | const fn = yield* cloudfunctions.getProjectsLocationsFunctions({ name: functionName }).pipe( |
| 39 | Effect.as("found" as const), |
| 40 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 41 | Effect.orDie, |
| 42 | Effect.provide(GcpHttp), |
| 43 | ); |
| 44 | expect(fn).toEqual("gone"); |
| 45 | |
| 46 | // Firestore soft-deletes: the database either 404s or reports a |
| 47 | // `deleteTime` until it is purged. |
| 48 | const db = yield* firestore.getProjectsDatabases({ name: databaseName }).pipe( |
| 49 | Effect.map((database) => (database.deleteTime === undefined ? "found" : "gone")), |
| 50 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 51 | Effect.orDie, |
| 52 | Effect.provide(GcpHttp), |
| 53 | ); |
| 54 | expect(db).toEqual("gone"); |
| 55 | }), |
| 56 | { timeout: 600_000 }, |
| 57 | ); |
| 58 | |
| 59 | const baseUrlOf = (url: string | undefined) => { |
| 60 | if (url === undefined) throw new Error("the function has no URL"); |
| 61 | return url.replace(/\/+$/, ""); |
| 62 | }; |
| 63 | |
| 64 | test( |
| 65 | "creates, reads, lists, and deletes notes over public HTTP", |
| 66 | Effect.gen(function* () { |
| 67 | const { url, databaseName } = yield* stack; |
| 68 | expect(url).toMatch(/^https:\/\//); |
| 69 | const baseUrl = baseUrlOf(url); |
| 70 | |
| 71 | // Right after a deploy the public invoker grant (403) and the |
| 72 | // project-level Firestore grant (500) can each take minutes to |
| 73 | // propagate. Poll the list route until both have landed. |
| 74 | const ready = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/notes`)).pipe( |
| 75 | Effect.repeat({ |
| 76 | schedule: Schedule.spaced("10 seconds"), |
| 77 | until: (response) => response.status === 200, |
| 78 | times: 48, |
| 79 | }), |
| 80 | ); |
| 81 | expect(ready.status).toBe(200); |
| 82 | |
| 83 | const unknown = yield* HttpClient.execute( |
| 84 | HttpClientRequest.get(`${baseUrl}/notes/does-not-exist`), |
| 85 | ); |
| 86 | expect(unknown.status).toBe(404); |
| 87 | |
| 88 | const created = yield* HttpClient.execute( |
| 89 | HttpClientRequest.post(`${baseUrl}/notes`).pipe( |
| 90 | HttpClientRequest.bodyJsonUnsafe({ |
| 91 | title: "groceries", |
| 92 | body: "eggs, milk", |
| 93 | }), |
| 94 | ), |
| 95 | ); |
| 96 | expect(created.status).toBe(201); |
| 97 | const note = (yield* created.json) as unknown as Note; |
| 98 | expect(note).toMatchObject({ title: "groceries", body: "eggs, milk" }); |
| 99 | expect(note.id).toMatch(/^[0-9a-f-]{36}$/); |
| 100 | |
| 101 | // The row the function wrote is a real Firestore document. |
| 102 | const document = yield* firestore |
| 103 | .getProjectsDatabasesDocuments({ |
| 104 | name: `${databaseName}/documents/notes/${note.id}`, |
| 105 | }) |
| 106 | .pipe(Effect.orDie, Effect.provide(GcpHttp)); |
| 107 | expect(document.fields?.title?.stringValue).toEqual("groceries"); |
| 108 | expect(document.fields?.body?.stringValue).toEqual("eggs, milk"); |
| 109 | expect(document.fields?.createdAt?.timestampValue).toEqual(expect.any(String)); |
| 110 | |
| 111 | const read = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/notes/${note.id}`)); |
| 112 | expect(read.status).toBe(200); |
| 113 | expect((yield* read.json) as unknown as Note).toEqual(note); |
| 114 | |
| 115 | const listed = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/notes`)); |
| 116 | expect(listed.status).toBe(200); |
| 117 | const { notes } = (yield* listed.json) as unknown as { notes: Note[] }; |
| 118 | expect(notes.map((n) => n.id)).toContain(note.id); |
| 119 | |
| 120 | const invalid = yield* HttpClient.execute( |
| 121 | HttpClientRequest.post(`${baseUrl}/notes`).pipe( |
| 122 | HttpClientRequest.bodyJsonUnsafe({ body: "no title" }), |
| 123 | ), |
| 124 | ); |
| 125 | expect(invalid.status).toBe(400); |
| 126 | |
| 127 | const deleted = yield* HttpClient.execute( |
| 128 | HttpClientRequest.delete(`${baseUrl}/notes/${note.id}`), |
| 129 | ); |
| 130 | expect(deleted.status).toBe(204); |
| 131 | |
| 132 | const gone = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/notes/${note.id}`)); |
| 133 | expect(gone.status).toBe(404); |
| 134 | |
| 135 | const documentGone = yield* firestore |
| 136 | .getProjectsDatabasesDocuments({ |
| 137 | name: `${databaseName}/documents/notes/${note.id}`, |
| 138 | }) |
| 139 | .pipe( |
| 140 | Effect.as("found" as const), |
| 141 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 142 | Effect.orDie, |
| 143 | Effect.provide(GcpHttp), |
| 144 | ); |
| 145 | expect(documentGone).toEqual("gone"); |
| 146 | }), |
| 147 | { timeout: 600_000 }, |
| 148 | ); |