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

main
ts 166 lines 6.04 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 file from "@distilled.cloud/gcp/file_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { CAPACITY_ZONE } from "../zones.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Snapshots need a ZONAL/REGIONAL/ENTERPRISE instance, which draws on the
16 // `EnterpriseStorageGibPerRegion` quota. The testing project has none (limit
17 // 0), so the lifecycle is entitlement-gated; the probe below pins the typed
18 // rejection. Instances also take 5–20 minutes to provision.
19 const hasEnterpriseQuota = !!process.env.GCP_TEST_FILESTORE_ENTERPRISE;
20 const runLifecycle = hasEnterpriseQuota && !!process.env.GCP_TEST_SLOW && !process.env.FAST;
21
22 const waitUntilGone = (name: string) =>
23 file.getProjectsLocationsInstancesSnapshots({ name }).pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("2 seconds"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33 test.provider(
34 "getProjectsLocationsInstancesSnapshots on a missing snapshot fails with a typed tag",
35 (stack) =>
36 Effect.gen(function* () {
37 const { project } = yield* GcpEnvironment.current;
38
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 file.getProjectsLocationsInstancesSnapshots({
43 name: `projects/${project}/locations/us-central1-a/instances/alchemy-filestore-missing/snapshots/alchemy-snap-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("NotFound");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:filestore", "live"], timeout: 90_000 },
51 );
52
53 test.provider.skipIf(hasEnterpriseQuota || !!process.env.FAST)(
54 "a snapshot-capable instance without enterprise quota fails with StorageQuotaExceeded",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const error = yield* Effect.flip(
60 stack.deploy(
61 Effect.gen(function* () {
62 return yield* GCP.Filestore.Instance("Nfs", {
63 location: CAPACITY_ZONE,
64 tier: "ZONAL",
65 fileShares: [{ name: "share1", capacityGb: 1024 }],
66 networks: [{ network: "default", modes: ["MODE_IPV4"] }],
67 });
68 }),
69 ),
70 );
71 expect(error._tag).toEqual("StorageQuotaExceeded");
72
73 yield* stack.destroy();
74 }).pipe(logLevel),
75 // If quota is ever granted the deploy creates a real instance; the budget
76 // lets the final destroy remove it instead of abandoning teardown.
77 {
78 tags: ["provider:gcp", "provider:gcp:filestore", "live"],
79 timeout: 2_700_000,
80 retry: 0,
81 },
82 );
83
84 test.provider.skipIf(!runLifecycle)(
85 "create, update, and delete a filestore snapshot",
86 (stack) =>
87 Effect.gen(function* () {
88 yield* stack.destroy();
89
90 const created = yield* stack.deploy(
91 Effect.gen(function* () {
92 const nfs = yield* GCP.Filestore.Instance("Nfs", {
93 location: CAPACITY_ZONE,
94 tier: "ZONAL",
95 fileShares: [{ name: "share1", capacityGb: 1024 }],
96 networks: [{ network: "default", modes: ["MODE_IPV4"] }],
97 labels: { env: "test" },
98 });
99 const snapshot = yield* GCP.Filestore.InstancesSnapshot("Nightly", {
100 instance: nfs.name,
101 description: "alchemy-test-snap",
102 labels: { env: "test" },
103 });
104 return { nfs, snapshot };
105 }),
106 );
107
108 expect(created.snapshot.name).toContain("/snapshots/");
109 expect(created.snapshot.snapshotId).toEqual(expect.any(String));
110 expect(created.snapshot.instance).toEqual(created.nfs.name);
111 expect(created.snapshot.description).toEqual("alchemy-test-snap");
112 expect(created.snapshot.labels).toMatchObject({ env: "test" });
113 expect(created.snapshot.state).toEqual("READY");
114
115 const fetched = yield* file.getProjectsLocationsInstancesSnapshots({
116 name: created.snapshot.name,
117 });
118 expect(fetched.name).toEqual(created.snapshot.name);
119 expect(fetched.description).toEqual("alchemy-test-snap");
120 expect(fetched.labels?.env).toEqual("test");
121
122 const updated = yield* stack.deploy(
123 Effect.gen(function* () {
124 const nfs = yield* GCP.Filestore.Instance("Nfs", {
125 instanceId: created.nfs.instanceId,
126 location: CAPACITY_ZONE,
127 tier: "ZONAL",
128 fileShares: [{ name: "share1", capacityGb: 1024 }],
129 networks: [{ network: "default", modes: ["MODE_IPV4"] }],
130 labels: { env: "test" },
131 });
132 const snapshot = yield* GCP.Filestore.InstancesSnapshot("Nightly", {
133 instance: nfs.name,
134 snapshotId: created.snapshot.snapshotId,
135 description: "alchemy-prod-snap",
136 labels: { env: "prod", role: "backup" },
137 });
138 return { nfs, snapshot };
139 }),
140 );
141
142 expect(updated.snapshot.name).toEqual(created.snapshot.name);
143 expect(updated.snapshot.description).toEqual("alchemy-prod-snap");
144 expect(updated.snapshot.labels).toMatchObject({
145 env: "prod",
146 role: "backup",
147 });
148
149 const refetched = yield* file.getProjectsLocationsInstancesSnapshots({
150 name: created.snapshot.name,
151 });
152 expect(refetched.description).toEqual("alchemy-prod-snap");
153 expect(refetched.labels?.env).toEqual("prod");
154 expect(refetched.labels?.role).toEqual("backup");
155
156 yield* stack.destroy();
157
158 const gone = yield* waitUntilGone(created.snapshot.name);
159 expect(gone).toEqual("gone");
160 }).pipe(logLevel),
161 {
162 tags: ["provider:gcp", "provider:gcp:filestore", "live"],
163 timeout: 2_700_000,
164 retry: 0,
165 },
166 );