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

main
ts 196 lines 7.14 KB
Raw
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 * 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 const { getWhenReady } = Test;
21
22 // Out-of-band calls to the Google APIs resolve the same stored credentials
23 // the deploy uses, so the test runs against the configured profile.
24 const GcpHttp = Layer.mergeAll(GCP.GcpAuth, GCP.fromAuthProvider(), FetchHttpClient.layer);
25
26 interface Heartbeat {
27 kind: "heartbeat" | "daily";
28 jobName: string;
29 scheduleTime: string;
30 receivedAt: string;
31 value: number;
32 }
33
34 const baseUrlOf = (url: string | undefined) => {
35 if (url === undefined) throw new Error("the service has no URL");
36 return url.replace(/\/+$/, "");
37 };
38
39 /** The Cloud Scheduler jobs that deliver to this service. */
40 const jobsFor = (project: string, location: string, baseUrl: string) =>
41 scheduler
42 .listProjectsLocationsJobs({
43 parent: `projects/${project}/locations/${location}`,
44 pageSize: 500,
45 })
46 .pipe(
47 Effect.map(({ jobs = [] }) =>
48 jobs.filter((job) => job.httpTarget?.uri?.startsWith(`${baseUrl}/__alchemy/scheduler/`)),
49 ),
50 Effect.orDie,
51 Effect.provide(GcpHttp),
52 );
53
54 // Deploy, tests and destroy all sit behind the same Docker guard.
55 describe("gcp-cron", () => {
56 const stack = beforeAll(deploy(Stack), { timeout: 900_000 });
57
58 afterAll.skipIf(!!process.env.NO_DESTROY)(
59 Effect.gen(function* () {
60 const { url, project, location, tableName } = yield* stack;
61 yield* destroy(Stack);
62
63 expect(yield* jobsFor(project, location, baseUrlOf(url))).toEqual([]);
64
65 // `projects/{project}/datasets/{dataset}/tables/{table}`
66 const [, projectId, , datasetId, , tableId] = tableName.split("/");
67 const table = yield* bigquery
68 .getTables({
69 projectId: projectId!,
70 datasetId: datasetId!,
71 tableId: tableId!,
72 })
73 .pipe(
74 Effect.as("found" as const),
75 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
76 Effect.orDie,
77 Effect.provide(GcpHttp),
78 );
79 expect(table).toEqual("gone");
80 }),
81 { timeout: 600_000 },
82 );
83
84 const heartbeatsOf = (baseUrl: string, kind: Heartbeat["kind"]) =>
85 Effect.gen(function* () {
86 const res = yield* HttpClient.execute(
87 HttpClientRequest.get(`${baseUrl}/heartbeats?kind=${kind}&limit=50`),
88 );
89 expect(res.status).toBe(200);
90 return ((yield* res.json) as unknown as { heartbeats: Heartbeat[] }).heartbeats;
91 });
92
93 test(
94 "creates one Cloud Scheduler job per schedule, both OIDC-authenticated",
95 Effect.gen(function* () {
96 const { url, project, location } = yield* stack;
97 const baseUrl = baseUrlOf(url);
98
99 const jobs = yield* jobsFor(project, location, baseUrl);
100 const byPath = Object.fromEntries(
101 jobs.map((job) => [new URL(job.httpTarget!.uri!).pathname, job]),
102 );
103 expect(Object.keys(byPath).sort()).toEqual([
104 "/__alchemy/scheduler/dailyrollup",
105 "/__alchemy/scheduler/heartbeat",
106 ]);
107 expect(byPath["/__alchemy/scheduler/heartbeat"]!.schedule).toEqual("* * * * *");
108 expect(byPath["/__alchemy/scheduler/dailyrollup"]!.schedule).toEqual("0 0 * * *");
109 for (const job of jobs) {
110 expect(job.httpTarget?.httpMethod).toEqual("POST");
111 expect(job.httpTarget?.oidcToken?.audience).toEqual(job.httpTarget?.uri);
112 }
113
114 // The service is public, but the schedule routes are not: a request
115 // without Cloud Scheduler's token is refused.
116 yield* getWhenReady(`${baseUrl}/`);
117 const forged = yield* HttpClient.execute(
118 HttpClientRequest.post(`${baseUrl}/__alchemy/scheduler/heartbeat`),
119 );
120 expect(forged.status).toBe(401);
121 }),
122 { timeout: 180_000 },
123 );
124
125 test(
126 "a forced run of each schedule records a row in BigQuery",
127 Effect.gen(function* () {
128 const { url, project, location, datasetId, tableId } = yield* stack;
129 const baseUrl = baseUrlOf(url);
130 yield* getWhenReady(`${baseUrl}/`);
131
132 const jobs = yield* jobsFor(project, location, baseUrl);
133 const jobNamed = (path: string) => {
134 const job = jobs.find((candidate) => candidate.httpTarget?.uri?.endsWith(path));
135 if (job?.name === undefined) throw new Error(`no job for ${path}`);
136 return job.name;
137 };
138 const heartbeatJob = jobNamed("/heartbeat");
139 const dailyJob = jobNamed("/dailyrollup");
140
141 // Don't wait for the cron: run both jobs now. Each job's retry policy
142 // redelivers until the fresh service and its grants are ready.
143 for (const name of [heartbeatJob, dailyJob]) {
144 yield* scheduler
145 .runProjectsLocationsJobs({ name, body: {} })
146 .pipe(Effect.orDie, Effect.provide(GcpHttp));
147 }
148
149 const shortName = (name: string) => name.split("/").pop()!;
150
151 const heartbeat = yield* heartbeatsOf(baseUrl, "heartbeat").pipe(
152 Effect.map((rows) => rows.find((row) => row.jobName === shortName(heartbeatJob))),
153 Effect.repeat({
154 schedule: Schedule.spaced("10 seconds"),
155 until: (row) => row !== undefined,
156 times: 30,
157 }),
158 );
159 expect(heartbeat).toBeDefined();
160 expect(heartbeat!.scheduleTime).toMatch(/^\d{4}-\d{2}-\d{2}T/);
161 expect(typeof heartbeat!.value).toBe("number");
162
163 // The daily cron cannot fire during the test, so its row is the forced run.
164 const daily = yield* heartbeatsOf(baseUrl, "daily").pipe(
165 Effect.map((rows) => rows.find((row) => row.jobName === shortName(dailyJob))),
166 Effect.repeat({
167 schedule: Schedule.spaced("10 seconds"),
168 until: (row) => row !== undefined,
169 times: 30,
170 }),
171 );
172 expect(daily).toBeDefined();
173 expect(daily!.value).toBeGreaterThanOrEqual(0);
174
175 // The rows are real BigQuery rows, not just what the service answers.
176 const result = yield* bigquery
177 .queryJobs({
178 projectId: project,
179 body: {
180 query: `SELECT kind, COUNT(*) AS n FROM \`${project}.${datasetId}.${tableId}\` GROUP BY kind`,
181 useLegacySql: false,
182 location,
183 timeoutMs: 30_000,
184 },
185 })
186 .pipe(Effect.orDie, Effect.provide(GcpHttp));
187 expect(result.jobComplete).toBe(true);
188 const counts = Object.fromEntries(
189 (result.rows ?? []).map((row) => [row.f?.[0]?.v as string, Number(row.f?.[1]?.v)]),
190 );
191 expect(counts.heartbeat).toBeGreaterThanOrEqual(1);
192 expect(counts.daily).toBeGreaterThanOrEqual(1);
193 }),
194 { timeout: 600_000 },
195 );
196 });