@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 179 lines 6.09 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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as compute from "@distilled.cloud/gcp/compute_v1";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { expectProbe } from "../bindingHost.ts";
11 import ComputeBindingsHost, { Vm } from "./fixtures/bindings-host.ts";
12
13 const testOptions = { providers: GCP.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "ComputeBindings");
16
17 // Stop→TERMINATED→RUNNING on e2-micro takes several minutes end to end.
18 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
19
20 let baseUrl: string;
21 let hostAccount: string;
22 let vm: { project: string; zone: string; instanceName: string };
23
24 /** Out-of-band instance status, read with the deployer's credentials. */
25 const liveStatus = Effect.suspend(() =>
26 compute
27 .getInstances({
28 project: vm.project,
29 zone: vm.zone,
30 instance: vm.instanceName,
31 })
32 .pipe(Effect.map((instance) => instance.status)),
33 );
34
35 const waitForStatus = (status: string) =>
36 liveStatus.pipe(
37 Effect.repeat({
38 schedule: Schedule.spaced("5 seconds"),
39 until: (current) => current === status,
40 times: 60,
41 }),
42 );
43
44 /**
45 * The instance-level roles the host's service account holds. GetInstance
46 * grants compute.viewer; Start/Stop share compute.instanceAdmin.v1.
47 */
48 const expectInstanceGrants = Effect.gen(function* () {
49 const policy = yield* compute.getIamPolicyInstances({
50 project: vm.project,
51 zone: vm.zone,
52 resource: vm.instanceName,
53 });
54 const roles = (policy.bindings ?? [])
55 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
56 .map((binding) => binding.role)
57 .sort();
58 expect(roles).toEqual(["roles/compute.instanceAdmin.v1", "roles/compute.viewer"]);
59
60 const { project } = yield* GcpEnvironment.current;
61 const projectPolicy = yield* resourcemanager.getIamPolicyProjects({
62 resource: `projects/${project}`,
63 body: { options: { requestedPolicyVersion: 3 } },
64 });
65 const projectRoles = (projectPolicy.bindings ?? []).filter((binding) =>
66 (binding.members ?? []).includes(`serviceAccount:${hostAccount}`),
67 );
68 expect(projectRoles).toEqual([]);
69 });
70
71 describe.skipIf(!runLifecycle)(
72 "Compute Bindings",
73 {
74 tags: ["provider:gcp", "provider:gcp:compute", "provider:gcp:run", "live"],
75 },
76 () => {
77 beforeAll(
78 Effect.gen(function* () {
79 yield* sharedStack.destroy();
80 const out = yield* sharedStack.deploy(
81 Effect.gen(function* () {
82 const host = yield* ComputeBindingsHost;
83 const instance = yield* Vm;
84 return {
85 uri: host.uri,
86 serviceAccount: host.serviceAccount,
87 vm: {
88 project: instance.project,
89 zone: instance.zone,
90 instanceName: instance.instanceName,
91 },
92 };
93 }),
94 );
95 baseUrl = out.uri!;
96 hostAccount = out.serviceAccount!;
97 vm = out.vm;
98 }),
99 { timeout: 900_000 },
100 );
101
102 afterAll(sharedStack.destroy(), { timeout: 900_000 });
103
104 // Describes run in declaration order: Get, then Stop, then Start.
105 describe("GetInstance", () => {
106 test.provider(
107 "reads the instance as the host's service account",
108 (_stack) =>
109 Effect.gen(function* () {
110 const live = yield* expectProbe<{
111 name?: string;
112 id?: string;
113 status?: string;
114 }>(baseUrl, "getInstance");
115 const expected = yield* compute.getInstances({
116 project: vm.project,
117 zone: vm.zone,
118 instance: vm.instanceName,
119 });
120 expect(live.name).toEqual(vm.instanceName);
121 expect(live.id).toEqual(expected.id);
122 expect(live.status).toEqual(expected.status);
123 yield* expectInstanceGrants;
124 }),
125 {
126 tags: ["provider:gcp", "provider:gcp:compute", "live"],
127 timeout: 600_000,
128 },
129 );
130 });
131
132 describe("StopInstance", () => {
133 test.provider(
134 "stops the instance as the host's service account",
135 (_stack) =>
136 Effect.gen(function* () {
137 yield* waitForStatus("RUNNING");
138 const op = yield* expectProbe<{
139 operationType?: string;
140 targetLink?: string;
141 }>(baseUrl, "stopInstance");
142 expect(op.operationType).toEqual("stop");
143 expect(op.targetLink).toContain(`/instances/${vm.instanceName}`);
144 expect(yield* waitForStatus("TERMINATED")).toEqual("TERMINATED");
145 const seen = yield* expectProbe<{ status?: string }>(baseUrl, "getInstance");
146 expect(seen.status).toEqual("TERMINATED");
147 yield* expectInstanceGrants;
148 }),
149 {
150 tags: ["provider:gcp", "provider:gcp:compute", "live"],
151 timeout: 600_000,
152 },
153 );
154 });
155
156 describe("StartInstance", () => {
157 test.provider(
158 "starts the instance as the host's service account",
159 (_stack) =>
160 Effect.gen(function* () {
161 const op = yield* expectProbe<{
162 operationType?: string;
163 targetLink?: string;
164 }>(baseUrl, "startInstance");
165 expect(op.operationType).toEqual("start");
166 expect(op.targetLink).toContain(`/instances/${vm.instanceName}`);
167 expect(yield* waitForStatus("RUNNING")).toEqual("RUNNING");
168 const seen = yield* expectProbe<{ status?: string }>(baseUrl, "getInstance");
169 expect(seen.status).toEqual("RUNNING");
170 yield* expectInstanceGrants;
171 }),
172 {
173 tags: ["provider:gcp", "provider:gcp:compute", "live"],
174 timeout: 600_000,
175 },
176 );
177 });
178 },
179 );