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

main
ts 106 lines 3.74 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 compute from "@distilled.cloud/gcp/compute_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const waitUntilGone = (projectId: string, templateName: string) =>
15 compute
16 .getInstanceTemplates({
17 project: projectId,
18 instanceTemplate: templateName,
19 })
20 .pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("1 second"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30 test.provider(
31 "create, replace, and delete an instance template",
32 (stack) =>
33 Effect.gen(function* () {
34 const { project } = yield* GcpEnvironment.current;
35 yield* stack.destroy();
36
37 const created = yield* stack.deploy(
38 Effect.gen(function* () {
39 return yield* GCP.Compute.InstanceTemplate("Web", {
40 machineType: "e2-micro",
41 labels: { env: "test" },
42 disks: [
43 {
44 boot: true,
45 autoDelete: true,
46 sourceImage: "projects/debian-cloud/global/images/family/debian-12",
47 diskSizeGb: 10,
48 },
49 ],
50 networkInterfaces: [{ network: "global/networks/default" }],
51 });
52 }),
53 );
54
55 expect(created.templateName).toEqual(expect.any(String));
56 expect(created.machineType).toEqual("e2-micro");
57 expect(created.labels).toMatchObject({ env: "test" });
58 expect(created.project).toEqual(project);
59
60 const fetched = yield* compute.getInstanceTemplates({
61 project,
62 instanceTemplate: created.templateName,
63 });
64 expect(fetched.name).toEqual(created.templateName);
65 expect(fetched.properties?.machineType).toEqual("e2-micro");
66 expect(fetched.properties?.labels?.env).toEqual("test");
67 expect(fetched.properties?.labels?.["alchemy-id"]).toEqual(expect.any(String));
68
69 const updated = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* GCP.Compute.InstanceTemplate("Web", {
72 templateName: created.templateName,
73 machineType: "e2-small",
74 labels: { env: "prod", role: "web" },
75 disks: [
76 {
77 boot: true,
78 autoDelete: true,
79 sourceImage: "projects/debian-cloud/global/images/family/debian-12",
80 diskSizeGb: 10,
81 },
82 ],
83 networkInterfaces: [{ network: "global/networks/default" }],
84 });
85 }),
86 );
87
88 expect(updated.templateName).toEqual(created.templateName);
89 expect(updated.machineType).toEqual("e2-small");
90 expect(updated.labels).toMatchObject({ env: "prod", role: "web" });
91
92 const refetched = yield* compute.getInstanceTemplates({
93 project,
94 instanceTemplate: updated.templateName,
95 });
96 expect(refetched.properties?.machineType).toEqual("e2-small");
97 expect(refetched.properties?.labels?.env).toEqual("prod");
98 expect(refetched.properties?.labels?.role).toEqual("web");
99
100 yield* stack.destroy();
101
102 const gone = yield* waitUntilGone(created.project, created.templateName);
103 expect(gone).toEqual("gone");
104 }).pipe(logLevel),
105 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
106 );