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

main
ts 135 lines 4.62 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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
5 import * as cloudrun from "@distilled.cloud/gcp/run_v2";
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 // The project comes from the same credential the deploy uses.
27 const currentProject = GCP.GcpEnvironment.current.pipe(
28 Effect.map((env) => env.project),
29 Effect.provide(GCP.fromCredentials().pipe(Layer.provide(GcpHttp))),
30 );
31
32 // Deploy, tests and destroy all sit behind the same Docker guard.
33 describe("gcp-vertex-ai", () => {
34 const stack = beforeAll(deploy(Stack), { timeout: 900_000 });
35
36 const serviceState = (name: string) =>
37 cloudrun.getProjectsLocationsServices({ name }).pipe(
38 Effect.as("found" as const),
39 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
40 Effect.orDie,
41 Effect.provide(GcpHttp),
42 );
43
44 /** Members holding `roles/aiplatform.user` on the project. */
45 const aiplatformUsers = currentProject.pipe(
46 Effect.flatMap((project) =>
47 resourcemanager.getIamPolicyProjects({
48 resource: `projects/${project}`,
49 body: { options: { requestedPolicyVersion: 3 } },
50 }),
51 ),
52 Effect.map((policy) =>
53 (policy.bindings ?? [])
54 .filter((binding) => binding.role === "roles/aiplatform.user")
55 .flatMap((binding) => binding.members ?? []),
56 ),
57 Effect.orDie,
58 Effect.provide(GcpHttp),
59 );
60
61 afterAll.skipIf(!!process.env.NO_DESTROY)(
62 Effect.gen(function* () {
63 const { serviceName, serviceAccount } = yield* stack;
64 yield* destroy(Stack);
65 // Nothing is left behind: the service is gone and its grant revoked.
66 expect(yield* serviceState(serviceName)).toEqual("gone");
67 expect(yield* aiplatformUsers).not.toContain(`serviceAccount:${serviceAccount}`);
68 }),
69 { timeout: 600_000 },
70 );
71
72 const baseUrlOf = (url: string | undefined) => {
73 if (url === undefined) throw new Error("the service has no URL");
74 return url.replace(/\/+$/, "");
75 };
76
77 const chat = (baseUrl: string, body: Record<string, unknown>) =>
78 HttpClient.execute(
79 HttpClientRequest.post(`${baseUrl}/chat`).pipe(HttpClientRequest.bodyJsonUnsafe(body)),
80 );
81
82 test(
83 "grants the runtime service account roles/aiplatform.user",
84 Effect.gen(function* () {
85 const { serviceAccount } = yield* stack;
86 expect(yield* aiplatformUsers).toContain(`serviceAccount:${serviceAccount}`);
87 }),
88 { timeout: 60_000 },
89 );
90
91 test(
92 "POST /chat answers with Gemini",
93 Effect.gen(function* () {
94 const { url } = yield* stack;
95 const baseUrl = baseUrlOf(url);
96 yield* getWhenReady(`${baseUrl}/`);
97
98 // A fresh project-level grant can take a minute or two to reach
99 // Vertex AI; until then the service answers 500 (Forbidden).
100 const res = yield* chat(baseUrl, {
101 prompt: "Reply with exactly: pong",
102 temperature: 0,
103 }).pipe(
104 Effect.repeat({
105 schedule: Schedule.spaced("10 seconds"),
106 until: (response) => response.status !== 500,
107 times: 30,
108 }),
109 );
110 expect(res.status).toBe(200);
111 const body = (yield* res.json) as {
112 text: string;
113 modelVersion: string;
114 usage: { inputTokens: number };
115 };
116 expect(body.text.length).toBeGreaterThan(0);
117 expect(body.text.toLowerCase()).toContain("pong");
118 expect(body.modelVersion).toContain("gemini-2.5-flash");
119 expect(body.usage.inputTokens).toBeGreaterThan(0);
120 }),
121 { timeout: 420_000 },
122 );
123
124 test(
125 "POST /chat rejects a missing prompt",
126 Effect.gen(function* () {
127 const { url } = yield* stack;
128 const baseUrl = baseUrlOf(url);
129 yield* getWhenReady(`${baseUrl}/`);
130 const res = yield* chat(baseUrl, {});
131 expect(res.status).toBe(400);
132 }),
133 { timeout: 120_000 },
134 );
135 });