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

main
ts 102 lines 3.43 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 Alchemy from "alchemy";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as HttpClientRequest from "effect/http/HttpClientRequest";
8 import * as Schedule from "effect/Schedule";
9 import Stack from "../alchemy.run.ts";
10
11 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
12 providers: GCP.providers(),
13 state: Alchemy.localState(),
14 });
15
16 const { getWhenReady } = Test;
17
18 // Deploy, tests and destroy all sit behind the same Docker guard.
19 describe("gcp-event-pipeline", () => {
20 const stack = beforeAll(deploy(Stack), { timeout: 900_000 });
21
22 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
23 timeout: 600_000,
24 });
25
26 const baseUrlOf = (url: string | undefined) => {
27 if (url === undefined) throw new Error("the service has no URL");
28 return url.replace(/\/+$/, "");
29 };
30
31 const publish = (baseUrl: string, type: string, payload: unknown) =>
32 HttpClient.execute(
33 HttpClientRequest.post(`${baseUrl}/events`).pipe(
34 HttpClientRequest.bodyJsonUnsafe({ type, payload }),
35 ),
36 );
37
38 const countOf = (baseUrl: string, type: string) =>
39 Effect.gen(function* () {
40 const res = yield* HttpClient.execute(
41 HttpClientRequest.get(`${baseUrl}/events/count?type=${encodeURIComponent(type)}`),
42 );
43 return ((yield* res.json) as { count: number }).count;
44 });
45
46 test(
47 "accepts events without touching BigQuery on the request path",
48 Effect.gen(function* () {
49 const { url } = yield* stack;
50 expect(url).toMatch(/^https:\/\//);
51 const baseUrl = baseUrlOf(url);
52 yield* getWhenReady(`${baseUrl}/`);
53
54 const accepted = yield* publish(baseUrl, "integ.smoke", { n: 1 });
55 expect(accepted.status).toBe(202);
56 const { id } = (yield* accepted.json) as { id: string };
57 expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
58
59 const missingType = yield* HttpClient.execute(
60 HttpClientRequest.post(`${baseUrl}/events`).pipe(
61 HttpClientRequest.bodyJsonUnsafe({ payload: {} }),
62 ),
63 );
64 expect(missingType.status).toBe(400);
65 }),
66 { timeout: 180_000 },
67 );
68
69 test(
70 "the drain job moves published events into BigQuery",
71 Effect.gen(function* () {
72 const { url } = yield* stack;
73 const baseUrl = baseUrlOf(url);
74 yield* getWhenReady(`${baseUrl}/`);
75
76 // A unique type keeps the count independent of other runs against the
77 // same project.
78 const type = `integ.drain.${crypto.randomUUID().slice(0, 8)}`;
79 for (let i = 0; i < 3; i++) {
80 const res = yield* publish(baseUrl, type, { i });
81 expect(res.status).toBe(202);
82 }
83
84 expect(yield* countOf(baseUrl, type)).toBe(0);
85
86 const drain = yield* HttpClient.execute(HttpClientRequest.post(`${baseUrl}/drain`));
87 expect(drain.status).toBe(202);
88
89 // Starting a Cloud Run Job returns immediately; the rows show up once
90 // the execution finishes and BigQuery's streaming buffer is queryable.
91 const landed = yield* countOf(baseUrl, type).pipe(
92 Effect.repeat({
93 schedule: Schedule.spaced("10 seconds"),
94 until: (count) => count >= 3,
95 times: 24,
96 }),
97 );
98 expect(landed).toBeGreaterThanOrEqual(3);
99 }),
100 { timeout: 600_000 },
101 );
102 });