@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 236 lines 7.97 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 workstations from "@distilled.cloud/gcp/workstations_v1";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { expectProbe } from "../bindingHost.ts";
10 import WorkstationsBindingsHost, { Dev } 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, "WorkstationsBindings");
15
16 // Workstation clusters take ~20 minutes to create and delete.
17 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
18
19 let baseUrl: string;
20 let hostAccount: string;
21 let project: string;
22 let clusterName: string;
23 let configName: string;
24 let workstationName: string;
25
26 const member = () => `serviceAccount:${hostAccount}`;
27
28 const rolesIn = (bindings: ReadonlyArray<{ role?: string; members?: ReadonlyArray<string> }>) =>
29 bindings
30 .filter((binding) => (binding.members ?? []).includes(member()))
31 .map((binding) => binding.role)
32 .sort();
33
34 /** Project-level roles (with their IAM Condition) held by the host. */
35 const projectRoles = () =>
36 resourcemanager
37 .getIamPolicyProjects({
38 resource: `projects/${project}`,
39 body: { options: { requestedPolicyVersion: 3 } },
40 })
41 .pipe(
42 Effect.map((policy) =>
43 (policy.bindings ?? [])
44 .filter((binding) => (binding.members ?? []).includes(member()))
45 .map((binding) => ({
46 role: binding.role,
47 condition: binding.condition?.expression,
48 })),
49 ),
50 );
51
52 const configRoles = () =>
53 workstations
54 .getIamPolicyProjectsLocationsWorkstationClustersWorkstationConfigs({
55 resource: configName,
56 })
57 .pipe(Effect.map((policy) => rolesIn(policy.bindings ?? [])));
58
59 const workstationRoles = () =>
60 workstations
61 .getIamPolicyProjectsLocationsWorkstationClustersWorkstationConfigsWorkstations({
62 resource: workstationName,
63 })
64 .pipe(Effect.map((policy) => rolesIn(policy.bindings ?? [])));
65
66 const waitForState = (state: string) =>
67 workstations
68 .getProjectsLocationsWorkstationClustersWorkstationConfigsWorkstations({
69 name: workstationName,
70 })
71 .pipe(
72 Effect.repeat({
73 schedule: Schedule.spaced("10 seconds"),
74 until: (live) => live.state === state,
75 times: 48,
76 }),
77 );
78
79 describe.skipIf(!runLifecycle)(
80 "Workstations Bindings",
81 {
82 tags: ["provider:gcp", "provider:gcp:workstations", "provider:gcp:run", "live"],
83 },
84 () => {
85 beforeAll(
86 Effect.gen(function* () {
87 yield* sharedStack.destroy();
88 const out = yield* sharedStack.deploy(
89 Effect.gen(function* () {
90 const host = yield* WorkstationsBindingsHost;
91 const { cluster, config, workstation } = yield* Dev;
92 return {
93 uri: host.uri,
94 serviceAccount: host.serviceAccount,
95 project: host.project,
96 cluster: cluster.name,
97 config: config.name,
98 workstation: workstation.name,
99 };
100 }),
101 );
102 baseUrl = out.uri!;
103 hostAccount = out.serviceAccount!;
104 project = out.project;
105 clusterName = out.cluster;
106 configName = out.config;
107 workstationName = out.workstation;
108 }),
109 { timeout: 3_000_000 },
110 );
111
112 afterAll(sharedStack.destroy(), { timeout: 3_000_000 });
113
114 describe("GetWorkstationCluster", () => {
115 test.provider(
116 "reads the bound cluster as the host, with workstations.viewer on the project",
117 (_stack) =>
118 Effect.gen(function* () {
119 const out = yield* expectProbe<{ name: string; network: string }>(
120 baseUrl,
121 "getWorkstationCluster",
122 );
123 expect(out.name).toEqual(clusterName);
124 expect(out.network).toContain("/networks/default");
125 // Clusters have no IAM policy of their own.
126 expect(yield* projectRoles()).toEqual([
127 { role: "roles/workstations.viewer", condition: undefined },
128 ]);
129 }),
130 {
131 tags: ["provider:gcp", "provider:gcp:workstations", "live"],
132 timeout: 600_000,
133 },
134 );
135 });
136
137 describe("GetWorkstationConfig", () => {
138 test.provider(
139 "reads the bound config as the host, with workstations.viewer on the config only",
140 (_stack) =>
141 Effect.gen(function* () {
142 const out = yield* expectProbe<{
143 name: string;
144 machineType: string;
145 }>(baseUrl, "getWorkstationConfig");
146 expect(out.name).toEqual(configName);
147 expect(out.machineType).toEqual("e2-standard-2");
148 expect(yield* configRoles()).toEqual(["roles/workstations.viewer"]);
149 }),
150 {
151 tags: ["provider:gcp", "provider:gcp:workstations", "live"],
152 timeout: 600_000,
153 },
154 );
155 });
156
157 describe("GetWorkstation", () => {
158 test.provider(
159 "reads the bound workstation as the host, with workstations.viewer on it",
160 (_stack) =>
161 Effect.gen(function* () {
162 const out = yield* expectProbe<{ name: string; state: string }>(
163 baseUrl,
164 "getWorkstation",
165 );
166 expect(out.name).toEqual(workstationName);
167 expect(out.state).toEqual("STATE_STOPPED");
168 expect(yield* workstationRoles()).toContain("roles/workstations.viewer");
169 }),
170 {
171 tags: ["provider:gcp", "provider:gcp:workstations", "live"],
172 timeout: 600_000,
173 },
174 );
175 });
176
177 describe("StartWorkstation", () => {
178 test.provider(
179 "starts the bound workstation as the host",
180 (_stack) =>
181 Effect.gen(function* () {
182 const out = yield* expectProbe<{ name: string }>(baseUrl, "startWorkstation");
183 expect(out.name).toContain("/operations/");
184 const live = yield* waitForState("STATE_RUNNING");
185 expect(live.state).toEqual("STATE_RUNNING");
186 expect(yield* workstationRoles()).toContain("roles/workstations.user");
187 }),
188 {
189 tags: ["provider:gcp", "provider:gcp:workstations", "live"],
190 timeout: 900_000,
191 },
192 );
193 });
194
195 describe("GenerateAccessToken", () => {
196 test.provider(
197 "mints an access token for the bound workstation as the host",
198 (_stack) =>
199 Effect.gen(function* () {
200 const out = yield* expectProbe<{
201 hasToken: boolean;
202 expireTime: string;
203 }>(baseUrl, "generateAccessToken");
204 expect(out.hasToken).toEqual(true);
205 expect(Date.parse(out.expireTime)).toBeGreaterThan(Date.parse("2026-01-01T00:00:00Z"));
206 expect(yield* workstationRoles()).toContain("roles/workstations.user");
207 }),
208 {
209 tags: ["provider:gcp", "provider:gcp:workstations", "live"],
210 timeout: 600_000,
211 },
212 );
213 });
214
215 describe("StopWorkstation", () => {
216 test.provider(
217 "stops the bound workstation as the host; the host holds only user and viewer on it",
218 (_stack) =>
219 Effect.gen(function* () {
220 const out = yield* expectProbe<{ name: string }>(baseUrl, "stopWorkstation");
221 expect(out.name).toContain("/operations/");
222 const live = yield* waitForState("STATE_STOPPED");
223 expect(live.state).toEqual("STATE_STOPPED");
224 expect(yield* workstationRoles()).toEqual([
225 "roles/workstations.user",
226 "roles/workstations.viewer",
227 ]);
228 }),
229 {
230 tags: ["provider:gcp", "provider:gcp:workstations", "live"],
231 timeout: 900_000,
232 },
233 );
234 });
235 },
236 );