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

main
ts 106 lines 3.8 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as cloudrun from "@distilled.cloud/gcp/run_v2";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const WORKER_IMAGE = "us-docker.pkg.dev/cloudrun/container/worker-pool";
14
15 const waitUntilGone = (name: string) =>
16 cloudrun.getProjectsLocationsWorkerPools({ name }).pipe(
17 Effect.as("found" as const),
18 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
19 Effect.repeat({
20 schedule: Schedule.spaced("2 seconds"),
21 until: (status) => status === "gone",
22 times: 10,
23 }),
24 );
25
26 test.provider(
27 "create, update, and delete a Cloud Run worker pool",
28 (stack) =>
29 Effect.gen(function* () {
30 yield* stack.destroy();
31
32 const created = yield* stack.deploy(
33 Effect.gen(function* () {
34 return yield* GCP.Run.WorkerPool("Workers", {
35 location: "us-central1",
36 description: "test run worker pool",
37 labels: { env: "test" },
38 scaling: { manualInstanceCount: 0 },
39 template: {
40 containers: [{ image: WORKER_IMAGE }],
41 },
42 });
43 }),
44 );
45
46 expect(created.name).toContain("/workerPools/");
47 expect(created.workerPoolId).toEqual(expect.any(String));
48 expect(created.location).toEqual("us-central1");
49 expect(created.description).toEqual("test run worker pool");
50 expect(created.labels).toMatchObject({ env: "test" });
51 expect(created.image).toEqual(WORKER_IMAGE);
52 expect(created.terminalConditionState).toEqual("CONDITION_SUCCEEDED");
53
54 const fetched = yield* cloudrun.getProjectsLocationsWorkerPools({
55 name: created.name,
56 });
57 expect(fetched.name).toEqual(created.name);
58 expect(fetched.labels?.env).toEqual("test");
59 expect(fetched.description).toEqual("test run worker pool");
60 expect(fetched.template?.containers?.[0]?.image).toEqual(WORKER_IMAGE);
61 expect(fetched.scaling?.manualInstanceCount).toEqual(0);
62
63 const updated = yield* stack.deploy(
64 Effect.gen(function* () {
65 return yield* GCP.Run.WorkerPool("Workers", {
66 workerPoolId: created.workerPoolId,
67 location: "us-central1",
68 description: "prod run worker pool",
69 labels: { env: "prod", role: "worker" },
70 scaling: { manualInstanceCount: 0 },
71 template: {
72 containers: [
73 {
74 image: WORKER_IMAGE,
75 env: [{ name: "ENV", value: "prod" }],
76 },
77 ],
78 },
79 });
80 }),
81 );
82
83 expect(updated.name).toEqual(created.name);
84 expect(updated.description).toEqual("prod run worker pool");
85 expect(updated.labels).toMatchObject({ env: "prod", role: "worker" });
86 expect(updated.image).toEqual(WORKER_IMAGE);
87
88 const refetched = yield* cloudrun.getProjectsLocationsWorkerPools({
89 name: created.name,
90 });
91 expect(refetched.description).toEqual("prod run worker pool");
92 expect(refetched.labels?.env).toEqual("prod");
93 expect(refetched.labels?.role).toEqual("worker");
94 expect(
95 refetched.template?.containers?.[0]?.env?.some(
96 (env) => env.name === "ENV" && env.value === "prod",
97 ),
98 ).toEqual(true);
99
100 yield* stack.destroy();
101
102 const gone = yield* waitUntilGone(created.name);
103 expect(gone).toEqual("gone");
104 }).pipe(logLevel),
105 { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 120_000 },
106 );