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

main
ts 170 lines 5.98 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as workloadmanager from "@distilled.cloud/gcp/workloadmanager_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { isServiceEnabled } from "../ServiceState.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const parentOf = (project: string) => `projects/${project}/locations/us-central1`;
16
17 // Workload Manager API is entitlement-gated on the default testing project
18 // (`ServiceDisabled`: "Workload Manager API has not been used in project
19 // <project> before or it is disabled."). Set
20 // GCP_TEST_WORKLOADMANAGER=1 on an entitled project to run the lifecycle.
21 const entitled = process.env.GCP_TEST_WORKLOADMANAGER === "1";
22 const runLifecycle = entitled && !process.env.FAST;
23
24 const waitUntilGone = (name: string) =>
25 workloadmanager.getProjectsLocationsEvaluations({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35 const firstRuleName = Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37 const parent = parentOf(project);
38 const page = yield* workloadmanager
39 .listProjectsLocationsRules({
40 parent,
41 evaluationType: "SAP",
42 pageSize: 20,
43 })
44 .pipe(Effect.catchTag("NotFound", () => Effect.succeed({ rules: [] as const })));
45 const named = (page.rules ?? []).find(
46 (rule) => typeof rule.name === "string" && rule.name.length > 0,
47 );
48 return named?.name ?? "sap-hana";
49 });
50
51 test.provider(
52 "getProjectsLocationsEvaluations fails with ServiceDisabled while the API is disabled",
53 (stack) =>
54 Effect.gen(function* () {
55 const { project } = yield* GcpEnvironment.current;
56 const parent = parentOf(project);
57 yield* stack.destroy();
58
59 const error = yield* Effect.flip(
60 workloadmanager.getProjectsLocationsEvaluations({
61 name: `${parent}/evaluations/alchemy-missing-evaluation`,
62 }),
63 );
64 expect(["ServiceDisabled", "NotFound"]).toContain(error._tag);
65
66 yield* stack.destroy();
67 }).pipe(logLevel),
68 {
69 tags: ["provider:gcp", "provider:gcp:workloadmanager", "live"],
70 timeout: 90_000,
71 },
72 );
73
74 test.provider.skipIf(entitled)(
75 "createProjectsLocationsEvaluations is rejected with ServiceDisabled while Workload Manager is disabled",
76 (stack) =>
77 Effect.gen(function* () {
78 // Only meaningful while the API is disabled: enabled, the create would
79 // provision a real resource.
80 if (yield* isServiceEnabled("workloadmanager.googleapis.com")) return;
81 const { project } = yield* GcpEnvironment.current;
82 const parent = parentOf(project);
83 yield* stack.destroy();
84
85 const error = yield* Effect.flip(
86 workloadmanager.createProjectsLocationsEvaluations({
87 parent,
88 evaluationId: "alchemy-evaluation-probe",
89 body: {
90 evaluationType: "SAP",
91 ruleNames: ["sap-hana"],
92 resourceFilter: { scopes: [`projects/${project}`] },
93 description: "alchemy-probe",
94 },
95 }),
96 );
97 expect(error._tag).toEqual("ServiceDisabled");
98
99 yield* stack.destroy();
100 }).pipe(logLevel),
101 {
102 tags: ["provider:gcp", "provider:gcp:workloadmanager", "live"],
103 timeout: 90_000,
104 },
105 );
106
107 test.provider.skipIf(!runLifecycle)(
108 "create, update, and delete an evaluation",
109 (stack) =>
110 Effect.gen(function* () {
111 const { project } = yield* GcpEnvironment.current;
112 yield* stack.destroy();
113
114 const ruleName = yield* firstRuleName;
115
116 const created = yield* stack.deploy(
117 Effect.gen(function* () {
118 return yield* GCP.WorkloadManager.Evaluation("SapBest", {
119 evaluationType: "SAP",
120 ruleNames: [ruleName],
121 resourceFilter: { scopes: [`projects/${project}`] },
122 description: "alchemy-test-evaluation",
123 labels: { env: "test" },
124 });
125 }),
126 );
127
128 expect(created.name).toContain("/evaluations/");
129 expect(created.evaluationId).toEqual(expect.any(String));
130 expect(created.location).toEqual("us-central1");
131 expect(created.project).toEqual(project);
132 expect(created.labels).toMatchObject({ env: "test" });
133 expect(created.description).toEqual("alchemy-test-evaluation");
134 expect(created.ruleNames.length).toBeGreaterThan(0);
135
136 const fetched = yield* workloadmanager.getProjectsLocationsEvaluations({
137 name: created.name,
138 });
139 expect(fetched.name).toEqual(created.name);
140 expect(fetched.labels?.env).toEqual("test");
141 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
142
143 const updated = yield* stack.deploy(
144 Effect.gen(function* () {
145 return yield* GCP.WorkloadManager.Evaluation("SapBest", {
146 evaluationId: created.evaluationId,
147 evaluationType: "SAP",
148 ruleNames: [ruleName],
149 resourceFilter: { scopes: [`projects/${project}`] },
150 description: "alchemy-test-evaluation-v2",
151 labels: { env: "prod", role: "eval" },
152 });
153 }),
154 );
155
156 expect(updated.name).toEqual(created.name);
157 expect(updated.evaluationId).toEqual(created.evaluationId);
158 expect(updated.description).toEqual("alchemy-test-evaluation-v2");
159 expect(updated.labels).toMatchObject({ env: "prod", role: "eval" });
160
161 yield* stack.destroy();
162
163 const gone = yield* waitUntilGone(created.name);
164 expect(gone).toEqual("gone");
165 }).pipe(logLevel),
166 {
167 tags: ["provider:gcp", "provider:gcp:workloadmanager", "live"],
168 timeout: 120_000,
169 },
170 );