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

main
ts 115 lines 3.92 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as transcoder from "@distilled.cloud/gcp/transcoder_v1";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { expectProbe } from "../bindingHost.ts";
10 import TranscoderBindingsHost from "./fixtures/bindings-host.ts";
11
12 const testOptions = { providers: GCP.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "TranscoderBindings");
15
16 let baseUrl: string;
17 let hostAccount: string;
18 let project: string;
19
20 /** Project-level roles held by the host's service account. */
21 const projectRoles = () =>
22 resourcemanager
23 .getIamPolicyProjects({
24 resource: `projects/${project}`,
25 body: { options: { requestedPolicyVersion: 3 } },
26 })
27 .pipe(
28 Effect.map((policy) =>
29 (policy.bindings ?? [])
30 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
31 .map((binding) => ({
32 role: binding.role,
33 condition: binding.condition?.expression,
34 })),
35 ),
36 );
37
38 const waitUntilJobGone = (name: string) =>
39 transcoder.getProjectsLocationsJobs({ name }).pipe(
40 Effect.as("found" as const),
41 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
42 Effect.repeat({
43 schedule: Schedule.spaced("1 second"),
44 until: (status) => status === "gone",
45 times: 10,
46 }),
47 );
48
49 describe.skipIf(!!process.env.FAST)(
50 "Transcoder Bindings",
51 {
52 tags: ["provider:gcp", "provider:gcp:transcoder", "provider:gcp:run", "live"],
53 },
54 () => {
55 beforeAll(
56 Effect.gen(function* () {
57 yield* sharedStack.destroy();
58 const out = yield* sharedStack.deploy(
59 Effect.gen(function* () {
60 const host = yield* TranscoderBindingsHost;
61 return {
62 uri: host.uri,
63 serviceAccount: host.serviceAccount,
64 project: host.project,
65 };
66 }),
67 );
68 baseUrl = out.uri!;
69 hostAccount = out.serviceAccount!;
70 project = out.project;
71 }),
72 { timeout: 900_000 },
73 );
74
75 afterAll(sharedStack.destroy(), { timeout: 600_000 });
76
77 describe("CreateJob", () => {
78 test.provider(
79 "starts a job from the bound template as the host, with transcoder.editor on the project",
80 (_stack) =>
81 Effect.gen(function* () {
82 const started = yield* expectProbe<{
83 name: string;
84 muxStreams: string[];
85 }>(baseUrl, "createJob");
86 const live = yield* transcoder.getProjectsLocationsJobs({
87 name: started.name,
88 });
89 // The job is not stack-managed: remove it before asserting.
90 yield* transcoder.deleteProjectsLocationsJobs({
91 name: started.name,
92 allowMissing: true,
93 });
94 const gone = yield* waitUntilJobGone(started.name);
95
96 expect(started.name).toMatch(/^projects\/[^/]+\/locations\/us-central1\/jobs\/[^/]+$/);
97 // The job runs the bound template's config.
98 expect(started.muxStreams).toEqual(["sd"]);
99 expect(live.name).toEqual(started.name);
100 expect((live.config?.muxStreams ?? []).map((stream) => stream.key)).toEqual(["sd"]);
101 expect(gone).toEqual("gone");
102
103 // No resource-level IAM and no narrower role with jobs.create.
104 expect(yield* projectRoles()).toEqual([
105 { role: "roles/transcoder.editor", condition: undefined },
106 ]);
107 }),
108 {
109 tags: ["provider:gcp", "provider:gcp:transcoder", "live"],
110 timeout: 600_000,
111 },
112 );
113 });
114 },
115 );