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

main
ts 333 lines 11.4 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1";
7 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { callProbe, expectProbe } from "../bindingHost.ts";
11 import AIPlatformBindingsHost, {
12 Agent,
13 Browser,
14 Code,
15 Paused,
16 Resumed,
17 runSandboxes,
18 runSandboxPause,
19 Train,
20 } from "./fixtures/bindings-host.ts";
21
22 const testOptions = { providers: GCP.providers() };
23 const { test, beforeAll, afterAll } = Test.make(testOptions);
24 const sharedStack = Core.scratchStack(testOptions, "AIPlatformBindings");
25
26 let baseUrl: string;
27 let hostAccount: string;
28 let names: {
29 engine: string;
30 pipeline: string;
31 sandbox?: string;
32 template?: string;
33 paused?: string;
34 resumed?: string;
35 };
36
37 /** Every project-level role (and its IAM Condition) `account` holds. */
38 const projectGrantsOf = (account: string) =>
39 Effect.gen(function* () {
40 const { project } = yield* GcpEnvironment.current;
41 const policy = yield* resourcemanager.getIamPolicyProjects({
42 resource: `projects/${project}`,
43 body: { options: { requestedPolicyVersion: 3 } },
44 });
45 return (policy.bindings ?? [])
46 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${account}`))
47 .map((binding) => ({
48 role: binding.role,
49 condition: binding.condition?.expression,
50 }))
51 .sort((left, right) => (left.role ?? "").localeCompare(right.role ?? ""));
52 });
53
54 /**
55 * Vertex AI resources have no resource-level IAM policy, so every AI
56 * Platform binding grants on the project: aiplatform.viewer for reads,
57 * aiplatform.user for predict / cancel / pause / resume.
58 */
59 const PROJECT_GRANTS = [
60 { role: "roles/aiplatform.user", condition: undefined },
61 { role: "roles/aiplatform.viewer", condition: undefined },
62 ];
63
64 const expectProjectGrants = Effect.gen(function* () {
65 expect(yield* projectGrantsOf(hostAccount)).toEqual(PROJECT_GRANTS);
66 });
67
68 describe(
69 "AIPlatform Bindings",
70 {
71 tags: ["provider:gcp", "provider:gcp:aiplatform", "provider:gcp:run", "live"],
72 },
73 () => {
74 beforeAll(
75 Effect.gen(function* () {
76 yield* sharedStack.destroy();
77 const out = yield* sharedStack.deploy(
78 Effect.gen(function* () {
79 const host = yield* AIPlatformBindingsHost;
80 const engine = yield* Agent;
81 const pipeline = yield* Train;
82 const sandboxes = runSandboxes
83 ? {
84 sandbox: (yield* Code).name,
85 template: (yield* Browser).name,
86 }
87 : {};
88 const paused = runSandboxPause
89 ? {
90 paused: (yield* Paused).name,
91 resumed: (yield* Resumed).name,
92 }
93 : {};
94 return {
95 uri: host.uri,
96 serviceAccount: host.serviceAccount,
97 names: {
98 engine: engine.name,
99 pipeline: pipeline.name,
100 ...sandboxes,
101 ...paused,
102 },
103 };
104 }),
105 );
106 baseUrl = out.uri!;
107 hostAccount = out.serviceAccount!;
108 names = out.names;
109 }),
110 { timeout: 900_000 },
111 );
112
113 afterAll(sharedStack.destroy(), { timeout: 600_000 });
114
115 describe("GenerateContent", () => {
116 test.provider(
117 "calls Gemini (global and regional) as the host's service account",
118 (_stack) =>
119 Effect.gen(function* () {
120 const out = yield* expectProbe<{
121 text: string;
122 regional: string;
123 modelVersion?: string;
124 }>(baseUrl, "generateContent");
125 expect(out.text.toLowerCase()).toContain("pong");
126 expect(out.regional.toLowerCase()).toContain("ping");
127 expect(out.modelVersion).toContain("gemini-2.5-flash");
128 yield* expectProjectGrants;
129 }),
130 {
131 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
132 timeout: 600_000,
133 },
134 );
135 });
136
137 describe("GetReasoningEngine", () => {
138 test.provider(
139 "reads the reasoning engine as the host's service account",
140 (_stack) =>
141 Effect.gen(function* () {
142 const live = yield* expectProbe<{
143 name?: string;
144 displayName?: string;
145 }>(baseUrl, "getReasoningEngine");
146 const expected = yield* aiplatform.getReasoningEngines({
147 name: names.engine,
148 });
149 expect(live.name).toEqual(names.engine);
150 expect(live.displayName).toEqual(expected.displayName);
151 yield* expectProjectGrants;
152 }),
153 {
154 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
155 timeout: 600_000,
156 },
157 );
158 });
159
160 describe("QueryReasoningEngine", () => {
161 test.provider(
162 "queries the reasoning engine as the host's service account",
163 (_stack) =>
164 Effect.gen(function* () {
165 // The engine has no deployed package, so the query is refused
166 // after authorization — the same answer the deployer gets.
167 const outcome = yield* callProbe(baseUrl, "queryReasoningEngine");
168 expect(outcome.ok ? "ok" : outcome.error._tag).toEqual("ReasoningEngineNotRunning");
169 const expected = yield* aiplatform
170 .queryReasoningEngines({
171 name: names.engine,
172 body: { input: { input: "hello" } },
173 })
174 .pipe(
175 Effect.map(() => "ok"),
176 Effect.catchTag("ReasoningEngineNotRunning", (error) => Effect.succeed(error._tag)),
177 );
178 expect(expected).toEqual("ReasoningEngineNotRunning");
179 yield* expectProjectGrants;
180 }),
181 {
182 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
183 timeout: 600_000,
184 },
185 );
186 });
187
188 describe("GetTrainingPipeline", () => {
189 test.provider(
190 "reads the training pipeline as the host's service account",
191 (_stack) =>
192 Effect.gen(function* () {
193 const live = yield* expectProbe<{
194 name?: string;
195 displayName?: string;
196 }>(baseUrl, "getTrainingPipeline");
197 expect(live.name).toEqual(names.pipeline);
198 expect(live.displayName).toEqual("alchemy-binding-pipeline");
199 yield* expectProjectGrants;
200 }),
201 {
202 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
203 timeout: 600_000,
204 },
205 );
206 });
207
208 describe("CancelTrainingPipeline", () => {
209 test.provider(
210 "cancels the training pipeline as the host's service account",
211 (_stack) =>
212 Effect.gen(function* () {
213 yield* expectProbe(baseUrl, "cancelTrainingPipeline");
214 const pipeline = yield* aiplatform
215 .getProjectsLocationsTrainingPipelines({ name: names.pipeline })
216 .pipe(
217 Effect.repeat({
218 schedule: Schedule.spaced("5 seconds"),
219 // Wait for CANCELLED (not just CANCELLING): only a
220 // settled pipeline can be deleted on teardown.
221 until: (live) => live.state === "PIPELINE_STATE_CANCELLED",
222 times: 36,
223 }),
224 );
225 expect(pipeline.state).toEqual("PIPELINE_STATE_CANCELLED");
226 yield* expectProjectGrants;
227 }),
228 {
229 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
230 timeout: 600_000,
231 },
232 );
233 });
234
235 describe.skipIf(!runSandboxes)("GetSandboxEnvironment", () => {
236 test.provider(
237 "reads the sandbox environment as the host's service account",
238 (_stack) =>
239 Effect.gen(function* () {
240 const live = yield* expectProbe<{
241 name?: string;
242 displayName?: string;
243 }>(baseUrl, "getSandboxEnvironment");
244 const expected = yield* aiplatform.getReasoningEnginesSandboxEnvironments({
245 name: names.sandbox!,
246 });
247 expect(live.name).toEqual(names.sandbox);
248 expect(live.displayName).toEqual(expected.displayName);
249 expect(live.displayName).toContain("code");
250 yield* expectProjectGrants;
251 }),
252 {
253 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
254 timeout: 600_000,
255 },
256 );
257 });
258
259 describe.skipIf(!runSandboxes)("GetSandboxEnvironmentTemplate", () => {
260 test.provider(
261 "reads the sandbox environment template as the host's service account",
262 (_stack) =>
263 Effect.gen(function* () {
264 const live = yield* expectProbe<{
265 name?: string;
266 displayName?: string;
267 }>(baseUrl, "getSandboxEnvironmentTemplate");
268 const expected = yield* aiplatform.getReasoningEnginesSandboxEnvironmentTemplates({
269 name: names.template!,
270 });
271 expect(live.name).toEqual(names.template);
272 expect(live.displayName).toEqual(expected.displayName);
273 expect(live.displayName).toContain("browser");
274 yield* expectProjectGrants;
275 }),
276 {
277 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
278 timeout: 600_000,
279 },
280 );
281 });
282
283 describe.skipIf(!runSandboxPause)("PauseSandboxEnvironment", () => {
284 test.provider(
285 "pauses the sandbox environment as the host's service account",
286 (_stack) =>
287 Effect.gen(function* () {
288 yield* expectProbe(baseUrl, "pauseSandboxEnvironment");
289 const live = yield* aiplatform
290 .getReasoningEnginesSandboxEnvironments({ name: names.paused! })
291 .pipe(
292 Effect.repeat({
293 schedule: Schedule.spaced("5 seconds"),
294 until: (sandbox) => sandbox.state === "STATE_PAUSED",
295 times: 18,
296 }),
297 );
298 expect(live.state).toEqual("STATE_PAUSED");
299 yield* expectProjectGrants;
300 }),
301 {
302 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
303 timeout: 600_000,
304 },
305 );
306 });
307
308 describe.skipIf(!runSandboxPause)("ResumeSandboxEnvironment", () => {
309 test.provider(
310 "resumes a paused sandbox environment as the host's service account",
311 (_stack) =>
312 Effect.gen(function* () {
313 yield* expectProbe(baseUrl, "resumeSandboxEnvironment");
314 const live = yield* aiplatform
315 .getReasoningEnginesSandboxEnvironments({ name: names.resumed! })
316 .pipe(
317 Effect.repeat({
318 schedule: Schedule.spaced("5 seconds"),
319 until: (sandbox) => sandbox.state === "STATE_RUNNING",
320 times: 18,
321 }),
322 );
323 expect(live.state).toEqual("STATE_RUNNING");
324 yield* expectProjectGrants;
325 }),
326 {
327 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
328 timeout: 600_000,
329 },
330 );
331 });
332 },
333 );