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
| 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 pubsub from "@distilled.cloud/gcp/pubsub_v1"; |
| 7 | import * as run from "@distilled.cloud/gcp/run_v2"; |
| 8 | import * as Alchemy from "alchemy"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 12 | import * as HttpClient from "effect/http/HttpClient"; |
| 13 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 14 | import * as Layer from "effect/Layer"; |
| 15 | import * as Schedule from "effect/Schedule"; |
| 16 | import Stack from "../alchemy.run.ts"; |
| 17 | |
| 18 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 19 | providers: GCP.providers(), |
| 20 | state: Alchemy.localState(), |
| 21 | }); |
| 22 | |
| 23 | const { getWhenReady } = Test; |
| 24 | |
| 25 | // Out-of-band calls to the Google APIs resolve the same stored credentials |
| 26 | // the deploy uses, so the test runs against the configured profile. |
| 27 | const GcpHttp = Layer.mergeAll(GCP.GcpAuth, GCP.fromAuthProvider(), FetchHttpClient.layer); |
| 28 | |
| 29 | // Deploy, tests and destroy all sit behind the same Docker guard. |
| 30 | describe("gcp-pubsub-worker", () => { |
| 31 | const stack = beforeAll(deploy(Stack), { timeout: 900_000 }); |
| 32 | |
| 33 | /** Pull subscriptions the worker's event source created on the topic. */ |
| 34 | const subscriptionsOf = (topic: string) => |
| 35 | pubsub.listProjectsTopicsSubscriptions({ topic }).pipe( |
| 36 | Effect.map((page) => page.subscriptions ?? []), |
| 37 | Effect.provide(GcpHttp), |
| 38 | ); |
| 39 | |
| 40 | let subscriptions: string[] = []; |
| 41 | |
| 42 | /** Poll until a deleted resource reads as `NotFound`. */ |
| 43 | const expectGone = <E, R>(what: string, status: Effect.Effect<"found" | "gone", E, R>) => |
| 44 | status.pipe( |
| 45 | // Deletes can take a few seconds to become visible. |
| 46 | Effect.repeat({ |
| 47 | schedule: Schedule.spaced("5 seconds"), |
| 48 | until: (status) => status === "gone", |
| 49 | times: 12, |
| 50 | }), |
| 51 | Effect.tap((status) => Effect.sync(() => expect(`${what}: ${status}`).toBe(`${what}: gone`))), |
| 52 | ); |
| 53 | |
| 54 | const found = Effect.as("found" as const); |
| 55 | const gone = () => Effect.succeed("gone" as const); |
| 56 | |
| 57 | afterAll.skipIf(!!process.env.NO_DESTROY)( |
| 58 | Effect.gen(function* () { |
| 59 | const outputs = yield* stack; |
| 60 | yield* destroy(Stack); |
| 61 | if (outputs === undefined) return; |
| 62 | |
| 63 | yield* expectGone( |
| 64 | "worker pool", |
| 65 | run |
| 66 | .getProjectsLocationsWorkerPools({ name: outputs.workerPoolName }) |
| 67 | .pipe(found, Effect.catchTag("NotFound", gone), Effect.provide(GcpHttp)), |
| 68 | ); |
| 69 | yield* expectGone( |
| 70 | "topic", |
| 71 | pubsub |
| 72 | .getProjectsTopics({ topic: outputs.topicName }) |
| 73 | .pipe(found, Effect.catchTag("NotFound", gone), Effect.provide(GcpHttp)), |
| 74 | ); |
| 75 | for (const subscription of subscriptions) { |
| 76 | yield* expectGone( |
| 77 | subscription, |
| 78 | pubsub |
| 79 | .getProjectsSubscriptions({ subscription }) |
| 80 | .pipe(found, Effect.catchTag("NotFound", gone), Effect.provide(GcpHttp)), |
| 81 | ); |
| 82 | } |
| 83 | yield* expectGone( |
| 84 | "database", |
| 85 | firestore |
| 86 | .getProjectsDatabases({ name: outputs.databaseName }) |
| 87 | .pipe(found, Effect.catchTag("NotFound", gone), Effect.provide(GcpHttp)), |
| 88 | ); |
| 89 | }), |
| 90 | { timeout: 600_000 }, |
| 91 | ); |
| 92 | |
| 93 | const baseUrlOf = (url: string | undefined) => { |
| 94 | if (url === undefined) throw new Error("the service has no URL"); |
| 95 | return url.replace(/\/+$/, ""); |
| 96 | }; |
| 97 | |
| 98 | interface JobStatus { |
| 99 | id: string; |
| 100 | status: "pending" | "done"; |
| 101 | sha256?: string; |
| 102 | words?: number; |
| 103 | chars?: number; |
| 104 | } |
| 105 | |
| 106 | class UnexpectedStatus extends Data.TaggedError("UnexpectedStatus")<{ |
| 107 | status: number; |
| 108 | body: string; |
| 109 | }> {} |
| 110 | |
| 111 | const statusOf = (baseUrl: string, id: string) => |
| 112 | Effect.gen(function* () { |
| 113 | const res = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/jobs/${id}`)); |
| 114 | if (res.status !== 200 && res.status !== 202) { |
| 115 | return yield* new UnexpectedStatus({ |
| 116 | status: res.status, |
| 117 | body: yield* res.text, |
| 118 | }); |
| 119 | } |
| 120 | return (yield* res.json) as unknown as JobStatus; |
| 121 | }); |
| 122 | |
| 123 | const PAYLOADS = [ |
| 124 | "hello world", |
| 125 | "the quick brown fox jumps over the lazy dog", |
| 126 | " alchemy on google cloud ", |
| 127 | "one", |
| 128 | "", |
| 129 | ]; |
| 130 | |
| 131 | test( |
| 132 | "rejects a job without a string payload", |
| 133 | Effect.gen(function* () { |
| 134 | const { url } = yield* stack; |
| 135 | const baseUrl = baseUrlOf(url); |
| 136 | yield* getWhenReady(`${baseUrl}/`); |
| 137 | |
| 138 | const res = yield* HttpClient.execute( |
| 139 | HttpClientRequest.post(`${baseUrl}/jobs`).pipe( |
| 140 | HttpClientRequest.bodyJsonUnsafe({ payload: 42 }), |
| 141 | ), |
| 142 | ); |
| 143 | expect(res.status).toBe(400); |
| 144 | }), |
| 145 | { timeout: 120_000 }, |
| 146 | ); |
| 147 | |
| 148 | test( |
| 149 | "processes submitted jobs through Pub/Sub into Firestore", |
| 150 | Effect.gen(function* () { |
| 151 | const { url, topicName, databaseName } = yield* stack; |
| 152 | const baseUrl = baseUrlOf(url); |
| 153 | yield* getWhenReady(`${baseUrl}/`); |
| 154 | |
| 155 | subscriptions = yield* subscriptionsOf(topicName); |
| 156 | expect(subscriptions.length).toBe(1); |
| 157 | |
| 158 | const ids: string[] = []; |
| 159 | for (const payload of PAYLOADS) { |
| 160 | const res = yield* HttpClient.execute( |
| 161 | HttpClientRequest.post(`${baseUrl}/jobs`).pipe( |
| 162 | HttpClientRequest.bodyJsonUnsafe({ payload }), |
| 163 | ), |
| 164 | ); |
| 165 | expect(res.status).toBe(202); |
| 166 | const body = (yield* res.json) as unknown as JobStatus; |
| 167 | expect(body.status).toBe("pending"); |
| 168 | ids.push(body.id); |
| 169 | } |
| 170 | |
| 171 | // The worker pool's first pull, and project-level Firestore grants on |
| 172 | // a fresh deploy, can each take a few minutes. Until then the API |
| 173 | // answers 202 pending — or 500 while its own read grant propagates. |
| 174 | const results = yield* Effect.forEach(ids, (id) => |
| 175 | statusOf(baseUrl, id).pipe( |
| 176 | Effect.tapError((error) => Effect.logWarning(error)), |
| 177 | Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 24 }), |
| 178 | Effect.repeat({ |
| 179 | schedule: Schedule.spaced("10 seconds"), |
| 180 | until: (status) => status.status === "done", |
| 181 | times: 48, |
| 182 | }), |
| 183 | ), |
| 184 | ); |
| 185 | |
| 186 | for (const [i, payload] of PAYLOADS.entries()) { |
| 187 | const expected = { |
| 188 | id: ids[i], |
| 189 | status: "done", |
| 190 | sha256: createHash("sha256").update(payload, "utf8").digest("hex"), |
| 191 | words: payload.split(/\s+/).filter(Boolean).length, |
| 192 | chars: payload.length, |
| 193 | }; |
| 194 | expect(results[i]).toMatchObject(expected); |
| 195 | |
| 196 | // The result the API served is a real Firestore document. |
| 197 | const document = yield* firestore |
| 198 | .getProjectsDatabasesDocuments({ |
| 199 | name: `${databaseName}/documents/jobs/${ids[i]}`, |
| 200 | }) |
| 201 | .pipe(Effect.provide(GcpHttp)); |
| 202 | expect(document.fields?.status?.stringValue).toBe("done"); |
| 203 | expect(document.fields?.sha256?.stringValue).toBe(expected.sha256); |
| 204 | expect(Number(document.fields?.words?.integerValue)).toBe(expected.words); |
| 205 | } |
| 206 | }), |
| 207 | { timeout: 900_000 }, |
| 208 | ); |
| 209 | }); |