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

main
ts 116 lines 4.04 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 cloudrun from "@distilled.cloud/gcp/run_v2";
5 import * as Alchemy from "alchemy";
6 import * as Effect from "effect/Effect";
7 import * as FetchHttpClient from "effect/http/FetchHttpClient";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Layer from "effect/Layer";
10 import * as Schedule from "effect/Schedule";
11 import Stack from "../alchemy.run.ts";
12
13 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
14 providers: GCP.providers(),
15 state: Alchemy.localState(),
16 });
17
18 const { getWhenReady } = Test;
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 // Deploy, tests and destroy all sit behind the same Docker guard.
25 describe("gcp-service-to-service", () => {
26 const stack = beforeAll(deploy(Stack), { timeout: 900_000 });
27
28 const serviceState = (name: string) =>
29 cloudrun.getProjectsLocationsServices({ name }).pipe(
30 Effect.as("found" as const),
31 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
32 Effect.orDie,
33 Effect.provide(GcpHttp),
34 );
35
36 afterAll.skipIf(!!process.env.NO_DESTROY)(
37 Effect.gen(function* () {
38 const { gatewayName, quotesName } = yield* stack;
39 yield* destroy(Stack);
40 // Nothing is left behind: both Cloud Run services are gone.
41 expect(yield* serviceState(gatewayName)).toEqual("gone");
42 expect(yield* serviceState(quotesName)).toEqual("gone");
43 }),
44 { timeout: 600_000 },
45 );
46
47 const baseUrlOf = (url: string | undefined) => {
48 if (url === undefined) throw new Error("the service has no URL");
49 return url.replace(/\/+$/, "");
50 };
51
52 const status = (url: string) =>
53 HttpClient.get(url).pipe(Effect.map((response) => response.status));
54
55 test(
56 "the gateway is public",
57 Effect.gen(function* () {
58 const { url } = yield* stack;
59 expect(url).toMatch(/^https:\/\//);
60 const res = yield* getWhenReady(`${baseUrlOf(url)}/`);
61 expect(res.status).toBe(200);
62 }),
63 { timeout: 120_000 },
64 );
65
66 test(
67 "the quotes service rejects callers without a Google identity",
68 Effect.gen(function* () {
69 const { quotesUrl } = yield* stack;
70 const code = yield* status(`${baseUrlOf(quotesUrl)}/quote`).pipe(
71 Effect.repeat({
72 schedule: Schedule.spaced("5 seconds"),
73 until: (code) => code === 403,
74 times: 12,
75 }),
76 );
77 expect(code).toBe(403);
78 }),
79 { timeout: 120_000 },
80 );
81
82 test(
83 "GET /quote on the gateway is served by the private quotes service",
84 Effect.gen(function* () {
85 const { url, quotesName, gatewayServiceAccount } = yield* stack;
86 const baseUrl = baseUrlOf(url);
87 yield* getWhenReady(`${baseUrl}/`);
88
89 // run.invoker lands on the quotes service's own policy, for the
90 // gateway's runtime service account only.
91 const policy = yield* cloudrun
92 .getIamPolicyProjectsLocationsServices({ resource: quotesName })
93 .pipe(Effect.orDie, Effect.provide(GcpHttp));
94 const invokers =
95 policy.bindings?.find((binding) => binding.role === "roles/run.invoker")?.members ?? [];
96 expect(invokers).toEqual([`serviceAccount:${gatewayServiceAccount}`]);
97
98 // A fresh run.invoker grant can take a moment to reach Cloud Run's
99 // front end; until then the gateway relays the 403.
100 const response = yield* HttpClient.get(`${baseUrl}/quote?n=1`).pipe(
101 Effect.repeat({
102 schedule: Schedule.spaced("5 seconds"),
103 until: (response) => response.status !== 403,
104 times: 24,
105 }),
106 );
107 expect(response.status).toBe(200);
108 expect(yield* response.json).toEqual({
109 index: 1,
110 quote: "Make it work, make it right, make it fast. — Kent Beck",
111 servedBy: "quotes",
112 });
113 }),
114 { timeout: 180_000 },
115 );
116 });