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

main
ts 127 lines 4.59 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 composer from "@distilled.cloud/gcp/composer_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { CAPACITY_REGION } from "../zones.ts";
10 import { defaultComputeServiceAccount } from "./serviceAccount.ts";
11
12 const { test } = Test.make({ providers: GCP.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // Composer environments take 20-45 minutes to provision.
17 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
18
19 const waitUntilGone = (name: string) =>
20 composer.getProjectsLocationsEnvironments({ name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("2 seconds"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30 test.provider(
31 "getProjectsLocationsEnvironments on a missing environment fails with NotFound",
32 (stack) =>
33 Effect.gen(function* () {
34 const { project } = yield* GcpEnvironment.current;
35 yield* stack.destroy();
36
37 const error = yield* Effect.flip(
38 composer.getProjectsLocationsEnvironments({
39 name: `projects/${project}/locations/${CAPACITY_REGION}/environments/alchemy-composer-missing`,
40 }),
41 );
42 expect(error._tag).toEqual("NotFound");
43
44 const page = yield* composer.listProjectsLocationsEnvironments({
45 parent: `projects/${project}/locations/${CAPACITY_REGION}`,
46 pageSize: 10,
47 });
48 expect((page.environments ?? []).map((environment) => environment.name)).not.toContain(
49 `projects/${project}/locations/${CAPACITY_REGION}/environments/alchemy-composer-missing`,
50 );
51
52 yield* stack.destroy();
53 }).pipe(logLevel),
54 { tags: ["provider:gcp", "provider:gcp:composer", "live"], timeout: 90_000 },
55 );
56
57 test.provider.skipIf(!runLifecycle)(
58 "create, update, and delete a composer environment",
59 (stack) =>
60 Effect.gen(function* () {
61 const serviceAccount = yield* defaultComputeServiceAccount;
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.Composer.Environment("Airflow", {
67 location: CAPACITY_REGION,
68 labels: { env: "test" },
69 config: {
70 environmentSize: "ENVIRONMENT_SIZE_SMALL",
71 nodeConfig: { serviceAccount },
72 softwareConfig: { imageVersion: "composer-3-airflow-2" },
73 },
74 });
75 }),
76 );
77
78 expect(created.name).toContain("/environments/");
79 expect(created.environmentId).toEqual(expect.any(String));
80 expect(created.location).toEqual(CAPACITY_REGION);
81 expect(created.labels).toMatchObject({ env: "test" });
82 expect(created.state).toEqual("RUNNING");
83
84 const fetched = yield* composer.getProjectsLocationsEnvironments({
85 name: created.name,
86 });
87 expect(fetched.name).toEqual(created.name);
88 expect(fetched.labels?.env).toEqual("test");
89 expect(fetched.state).toEqual("RUNNING");
90 expect(fetched.config?.nodeConfig?.serviceAccount).toEqual(serviceAccount);
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.Composer.Environment("Airflow", {
95 environmentId: created.environmentId,
96 location: CAPACITY_REGION,
97 labels: { env: "prod", role: "airflow" },
98 config: {
99 environmentSize: "ENVIRONMENT_SIZE_SMALL",
100 nodeConfig: { serviceAccount },
101 softwareConfig: { imageVersion: "composer-3-airflow-2" },
102 },
103 });
104 }),
105 );
106
107 expect(updated.name).toEqual(created.name);
108 expect(updated.labels).toMatchObject({ env: "prod", role: "airflow" });
109
110 const refetched = yield* composer.getProjectsLocationsEnvironments({
111 name: created.name,
112 });
113 expect(refetched.labels?.env).toEqual("prod");
114 expect(refetched.labels?.role).toEqual("airflow");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(created.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 // Create (~45 min) + update + delete; a timed-out lifecycle is not retried.
122 {
123 tags: ["provider:gcp", "provider:gcp:composer", "live"],
124 timeout: 5_400_000,
125 retry: 0,
126 },
127 );