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, 07:15 UTC
| 1 | import { describe, expect } from "bun:test"; |
| 2 | import * as GCP from "@alchemy.run/gcp"; |
| 3 | import * as Test from "@alchemy.run/test/Bun"; |
| 4 | import * as bigquery from "@distilled.cloud/gcp/bigquery_v2"; |
| 5 | import * as scheduler from "@distilled.cloud/gcp/cloudscheduler_v1"; |
| 6 | import { Credentials } from "@distilled.cloud/gcp/Credentials"; |
| 7 | import * as run from "@distilled.cloud/gcp/run_v2"; |
| 8 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 9 | import * as Alchemy from "alchemy"; |
| 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 | import { SUMMARY_PREFIX, type DailySummary } from "../src/resources.ts"; |
| 18 | |
| 19 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 20 | providers: GCP.providers(), |
| 21 | state: Alchemy.localState(), |
| 22 | }); |
| 23 | |
| 24 | const { getWhenReady } = Test; |
| 25 | |
| 26 | // Out-of-band calls to the Google APIs resolve the same stored credentials |
| 27 | // the deploy uses, so the test runs against the configured profile. |
| 28 | const GcpHttp = Layer.mergeAll(GCP.GcpAuth, GCP.fromAuthProvider(), FetchHttpClient.layer); |
| 29 | |
| 30 | // Deploy, tests and destroy all sit behind the same Docker guard. |
| 31 | describe("gcp-scheduled-job", () => { |
| 32 | const stack = beforeAll(deploy(Stack), { timeout: 900_000 }); |
| 33 | |
| 34 | afterAll.skipIf(!!process.env.NO_DESTROY)( |
| 35 | Effect.gen(function* () { |
| 36 | const { jobName, schedulerJobName, bucketName } = yield* stack; |
| 37 | yield* destroy(Stack); |
| 38 | |
| 39 | const found = Effect.as("found" as const); |
| 40 | const gone = <A, E extends { readonly _tag: string }, R>(self: Effect.Effect<A, E, R>) => |
| 41 | self.pipe( |
| 42 | Effect.catchIf( |
| 43 | (error) => error._tag === "NotFound", |
| 44 | () => Effect.succeed("gone" as const), |
| 45 | ), |
| 46 | ); |
| 47 | const [job, schedulerJob, bucket] = yield* Effect.all([ |
| 48 | run.getProjectsLocationsJobs({ name: jobName }).pipe(found, gone), |
| 49 | scheduler.getProjectsLocationsJobs({ name: schedulerJobName }).pipe(found, gone), |
| 50 | storage.getBuckets({ bucket: bucketName }).pipe(found, gone), |
| 51 | ]).pipe(Effect.orDie, Effect.provide(GcpHttp)); |
| 52 | expect({ job, schedulerJob, bucket }).toEqual({ |
| 53 | job: "gone", |
| 54 | schedulerJob: "gone", |
| 55 | bucket: "gone", |
| 56 | }); |
| 57 | }), |
| 58 | { timeout: 600_000 }, |
| 59 | ); |
| 60 | |
| 61 | const baseUrlOf = (url: string | undefined) => { |
| 62 | if (url === undefined) throw new Error("the service has no URL"); |
| 63 | return url.replace(/\/+$/, ""); |
| 64 | }; |
| 65 | |
| 66 | /** Every execution the Cloud Run job has, newest first. */ |
| 67 | const executionsOf = (jobName: string) => |
| 68 | run.listProjectsLocationsJobsExecutions({ parent: jobName, pageSize: 100 }).pipe( |
| 69 | Effect.map((page) => page.executions ?? []), |
| 70 | Effect.orDie, |
| 71 | Effect.provide(GcpHttp), |
| 72 | ); |
| 73 | |
| 74 | /** Download an object's content with the test's own credentials. */ |
| 75 | const download = (bucket: string, object: string) => |
| 76 | Effect.gen(function* () { |
| 77 | const { accessToken } = yield* yield* Credentials; |
| 78 | const res = yield* HttpClient.execute( |
| 79 | HttpClientRequest.get( |
| 80 | `https://storage.googleapis.com/storage/v1/b/${bucket}/o/${encodeURIComponent(object)}`, |
| 81 | ).pipe( |
| 82 | HttpClientRequest.setUrlParams({ alt: "media" }), |
| 83 | HttpClientRequest.bearerToken(accessToken), |
| 84 | ), |
| 85 | ); |
| 86 | expect(res.status).toBe(200); |
| 87 | return yield* res.text; |
| 88 | }).pipe(Effect.orDie, Effect.provide(GcpHttp)); |
| 89 | |
| 90 | /** Every summary currently in the bucket. */ |
| 91 | const summariesIn = (bucket: string) => |
| 92 | Effect.gen(function* () { |
| 93 | const { items = [] } = yield* storage |
| 94 | .listObjects({ bucket, prefix: SUMMARY_PREFIX }) |
| 95 | .pipe(Effect.orDie, Effect.provide(GcpHttp)); |
| 96 | const summaries: DailySummary[] = []; |
| 97 | for (const item of items) { |
| 98 | if (item.name === undefined) continue; |
| 99 | summaries.push(JSON.parse(yield* download(bucket, item.name)) as DailySummary); |
| 100 | } |
| 101 | return summaries; |
| 102 | }); |
| 103 | |
| 104 | test( |
| 105 | "POST /run summarizes seeded orders into the bucket", |
| 106 | Effect.gen(function* () { |
| 107 | const { url, project, datasetId, tableId, bucketName } = yield* stack; |
| 108 | const baseUrl = baseUrlOf(url); |
| 109 | |
| 110 | // Unique region names keep the check independent of other rows. |
| 111 | const tag = crypto.randomUUID().slice(0, 8); |
| 112 | const east = `east-${tag}`; |
| 113 | const west = `west-${tag}`; |
| 114 | const occurredAt = new Date(Date.now() - 5 * 60_000).toISOString(); |
| 115 | const seed = [ |
| 116 | { region: east, amountCents: 1000 }, |
| 117 | { region: east, amountCents: 2500 }, |
| 118 | { region: east, amountCents: 499 }, |
| 119 | { region: west, amountCents: 1200 }, |
| 120 | { region: west, amountCents: 800 }, |
| 121 | ].map((row, i) => ({ id: `${tag}-${i}`, occurredAt, ...row })); |
| 122 | |
| 123 | // A just-created table can briefly 404 on the streaming path. |
| 124 | const inserted = yield* bigquery |
| 125 | .insertAllTabledata({ |
| 126 | projectId: project, |
| 127 | datasetId, |
| 128 | tableId, |
| 129 | body: { rows: seed.map((json) => ({ insertId: json.id, json })) }, |
| 130 | }) |
| 131 | .pipe( |
| 132 | Effect.retry({ |
| 133 | while: (error) => error._tag === "NotFound", |
| 134 | schedule: Schedule.spaced("5 seconds"), |
| 135 | times: 12, |
| 136 | }), |
| 137 | Effect.orDie, |
| 138 | Effect.provide(GcpHttp), |
| 139 | ); |
| 140 | expect(inserted.insertErrors ?? []).toEqual([]); |
| 141 | |
| 142 | yield* getWhenReady(`${baseUrl}/`); |
| 143 | const started = yield* HttpClient.execute(HttpClientRequest.post(`${baseUrl}/run`)); |
| 144 | expect(started.status).toBe(202); |
| 145 | const { operation } = (yield* started.json) as { |
| 146 | operation: string | null; |
| 147 | }; |
| 148 | expect(operation).toContain("/operations/"); |
| 149 | |
| 150 | // An execution can take ~2 minutes to start, then runs for seconds. |
| 151 | const hasOurs = (summary: DailySummary) => |
| 152 | summary.regions.some((r) => r.region === east) && |
| 153 | summary.regions.some((r) => r.region === west); |
| 154 | const summary = yield* summariesIn(bucketName).pipe( |
| 155 | Effect.map((summaries) => summaries.find(hasOurs)), |
| 156 | Effect.repeat({ |
| 157 | schedule: Schedule.spaced("10 seconds"), |
| 158 | until: (found) => found !== undefined, |
| 159 | times: 48, |
| 160 | }), |
| 161 | ); |
| 162 | expect(summary).toBeDefined(); |
| 163 | |
| 164 | const regionOf = (name: string) => summary!.regions.find((r) => r.region === name); |
| 165 | expect(regionOf(east)).toEqual({ |
| 166 | region: east, |
| 167 | orders: 3, |
| 168 | revenueCents: 3999, |
| 169 | }); |
| 170 | expect(regionOf(west)).toEqual({ |
| 171 | region: west, |
| 172 | orders: 2, |
| 173 | revenueCents: 2000, |
| 174 | }); |
| 175 | expect(summary!.orders).toBeGreaterThanOrEqual(5); |
| 176 | expect(summary!.revenueCents).toBeGreaterThanOrEqual(5999); |
| 177 | expect(summary!.execution).toBeTruthy(); |
| 178 | expect(new Date(summary!.to).getTime() - new Date(summary!.from).getTime()).toBe( |
| 179 | 24 * 60 * 60 * 1000, |
| 180 | ); |
| 181 | }), |
| 182 | { timeout: 720_000 }, |
| 183 | ); |
| 184 | |
| 185 | test( |
| 186 | "the Cloud Scheduler job starts an execution through the Admin API", |
| 187 | Effect.gen(function* () { |
| 188 | const { jobName, schedulerJobName } = yield* stack; |
| 189 | |
| 190 | const schedulerJob = yield* scheduler |
| 191 | .getProjectsLocationsJobs({ name: schedulerJobName }) |
| 192 | .pipe(Effect.orDie, Effect.provide(GcpHttp)); |
| 193 | expect(schedulerJob.schedule).toEqual("0 2 * * *"); |
| 194 | expect(schedulerJob.httpTarget?.uri).toEqual(`https://run.googleapis.com/v2/${jobName}:run`); |
| 195 | const job = yield* run |
| 196 | .getProjectsLocationsJobs({ name: jobName }) |
| 197 | .pipe(Effect.orDie, Effect.provide(GcpHttp)); |
| 198 | expect(schedulerJob.httpTarget?.oauthToken?.serviceAccountEmail).toEqual( |
| 199 | job.template?.template?.serviceAccount, |
| 200 | ); |
| 201 | |
| 202 | const before = (yield* executionsOf(jobName)).map((e) => e.name); |
| 203 | |
| 204 | // Don't wait for 02:00: force a run now. |
| 205 | yield* scheduler |
| 206 | .runProjectsLocationsJobs({ name: schedulerJobName, body: {} }) |
| 207 | .pipe(Effect.orDie, Effect.provide(GcpHttp)); |
| 208 | |
| 209 | const fresh = yield* executionsOf(jobName).pipe( |
| 210 | Effect.map((executions) => executions.filter((e) => !before.includes(e.name))), |
| 211 | Effect.repeat({ |
| 212 | schedule: Schedule.spaced("10 seconds"), |
| 213 | until: (executions) => executions.length > 0, |
| 214 | times: 30, |
| 215 | }), |
| 216 | ); |
| 217 | expect(fresh.length).toBeGreaterThanOrEqual(1); |
| 218 | |
| 219 | // Cloud Scheduler records the Admin API's answer; code 0 (or unset) |
| 220 | // means `jobs:run` accepted the OAuth token. |
| 221 | const attempted = yield* scheduler.getProjectsLocationsJobs({ name: schedulerJobName }).pipe( |
| 222 | Effect.orDie, |
| 223 | Effect.provide(GcpHttp), |
| 224 | Effect.repeat({ |
| 225 | schedule: Schedule.spaced("5 seconds"), |
| 226 | until: (j) => j.lastAttemptTime !== undefined, |
| 227 | times: 24, |
| 228 | }), |
| 229 | ); |
| 230 | expect(attempted.status?.code ?? 0).toBe(0); |
| 231 | }), |
| 232 | { timeout: 600_000 }, |
| 233 | ); |
| 234 | }); |