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

main
ts 166 lines 5.68 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 storage from "@distilled.cloud/gcp/storage_v1";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import MarkerJob, { MARKER_OBJECT } from "./fixtures/job.ts";
11
12 const { test } = Test.make({ providers: GCP.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const IMAGE = "us-docker.pkg.dev/cloudrun/container/job:latest";
17
18 class JobRunNotReady extends Data.TaggedError("JobRunNotReady")<{
19 reason: string;
20 }> {}
21
22 const waitUntilGone = (name: string) =>
23 cloudrun.getProjectsLocationsJobs({ name }).pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("1 second"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33 test.provider(
34 "create, update, and delete a job",
35 (stack) =>
36 Effect.gen(function* () {
37 yield* stack.destroy();
38
39 const created = yield* stack.deploy(
40 Effect.gen(function* () {
41 return yield* GCP.Run.Job("Migrate", {
42 location: "us-central1",
43 labels: { env: "test" },
44 containers: [{ image: IMAGE }],
45 });
46 }),
47 );
48
49 expect(created.name).toContain("/jobs/");
50 expect(created.jobId).toEqual(expect.any(String));
51 expect(created.location).toEqual("us-central1");
52 expect(created.labels).toMatchObject({ env: "test" });
53 expect(created.image).toEqual(IMAGE);
54
55 const fetched = yield* cloudrun.getProjectsLocationsJobs({
56 name: created.name,
57 });
58 expect(fetched.name).toEqual(created.name);
59 expect(fetched.labels?.env).toEqual("test");
60 expect(fetched.template?.template?.containers?.[0]?.image).toEqual(IMAGE);
61
62 const updated = yield* stack.deploy(
63 Effect.gen(function* () {
64 return yield* GCP.Run.Job("Migrate", {
65 jobId: created.jobId,
66 location: "us-central1",
67 labels: { env: "prod", role: "migrate" },
68 maxRetries: 1,
69 timeout: "120s",
70 containers: [
71 {
72 image: IMAGE,
73 env: [{ name: "STAGE", value: "prod" }],
74 },
75 ],
76 });
77 }),
78 );
79
80 expect(updated.name).toEqual(created.name);
81 expect(updated.labels).toMatchObject({ env: "prod", role: "migrate" });
82 expect(updated.maxRetries).toEqual(1);
83 expect(updated.timeout).toEqual("120s");
84
85 const refetched = yield* cloudrun.getProjectsLocationsJobs({
86 name: created.name,
87 });
88 expect(refetched.labels?.env).toEqual("prod");
89 expect(refetched.labels?.role).toEqual("migrate");
90 expect(refetched.template?.template?.maxRetries).toEqual(1);
91 expect(refetched.template?.template?.timeout).toEqual("120s");
92 expect(refetched.template?.template?.containers?.[0]?.env).toEqual(
93 expect.arrayContaining([expect.objectContaining({ name: "STAGE", value: "prod" })]),
94 );
95
96 yield* stack.destroy();
97
98 const gone = yield* waitUntilGone(created.name);
99 expect(gone).toEqual("gone");
100 }).pipe(logLevel),
101 { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 120_000 },
102 );
103
104 test.provider(
105 "effect-native Job run writes a Storage object",
106 (stack) =>
107 Effect.gen(function* () {
108 yield* stack.destroy();
109
110 const out = yield* stack.deploy(
111 Effect.gen(function* () {
112 const job = yield* MarkerJob;
113 return { name: job.name, project: job.project };
114 }),
115 );
116
117 yield* cloudrun.runProjectsLocationsJobs({ name: out.name });
118
119 const execution = yield* cloudrun
120 .listProjectsLocationsJobsExecutions({
121 parent: out.name,
122 pageSize: 10,
123 })
124 .pipe(
125 Effect.map((page) => page.executions?.[0]),
126 Effect.filterOrFail(
127 (item): item is cloudrun.GoogleCloudRunV2Execution =>
128 item !== undefined &&
129 (item.completionTime ?? "").length > 0 &&
130 (item.succeededCount ?? 0) >= 1,
131 (item) =>
132 new JobRunNotReady({
133 reason: item?.completionTime ? `failed:${item.failedCount ?? 0}` : "pending",
134 }),
135 ),
136 Effect.retry({
137 while: (error): error is JobRunNotReady =>
138 error._tag === "JobRunNotReady" && error.reason === "pending",
139 // Cloud Run schedules a fresh execution slowly: even Google's
140 // sample job takes ~2 minutes to start in the test project.
141 schedule: Schedule.spaced("10 seconds"),
142 times: 36,
143 }),
144 );
145 expect((execution.succeededCount ?? 0) >= 1).toEqual(true);
146
147 const listed = yield* storage.listBuckets({
148 project: out.project,
149 maxResults: 1000,
150 });
151 const markerBucket = (listed.items ?? []).find((bucket) =>
152 (bucket.labels?.["alchemy-id"] ?? "").includes("jobrunmarker"),
153 );
154 expect(markerBucket?.name).toEqual(expect.any(String));
155 const object = yield* storage.getObjects({
156 bucket: markerBucket!.name!,
157 object: MARKER_OBJECT,
158 });
159 expect(object.name).toEqual(MARKER_OBJECT);
160
161 yield* stack.destroy();
162 const gone = yield* waitUntilGone(out.name);
163 expect(gone).toEqual("gone");
164 }).pipe(logLevel),
165 { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 540_000 },
166 );