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

main
ts 123 lines 4.72 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 spanner from "@distilled.cloud/gcp/spanner_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 // Partitions need an ENTERPRISE_PLUS instance plus a partition at 1000 PU each
15 // (~$10/h) and more node quota than the default testing project has
16 // (`Project 457525637530 cannot add 1 nodes in region us-central1.`). Set
17 // GCP_TEST_SPANNER_PARTITIONS=1 to opt in.
18 const runLifecycle = !!process.env.GCP_TEST_SPANNER_PARTITIONS && !process.env.FAST;
19
20 const waitUntilGone = (name: string) =>
21 spanner.getProjectsInstancesInstancePartitions({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31 test.provider(
32 "getProjectsInstancesInstancePartitions on a missing partition fails with NotFound",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 spanner.getProjectsInstancesInstancePartitions({
40 name: `projects/${project}/instances/alchemy-spanner-missing/instancePartitions/alchemy-missing`,
41 }),
42 );
43 expect(error._tag).toBe("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 90_000 },
48 );
49
50 test.provider.skipIf(!runLifecycle)(
51 "create, update, and delete a spanner instance partition",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 const instance = yield* GCP.Spanner.Instance("App", {
59 config: "regional-us-central1",
60 processingUnits: 1000,
61 displayName: "alchemy-pt-spnr",
62 edition: "ENTERPRISE_PLUS",
63 defaultBackupScheduleType: "NONE",
64 });
65 const partition = yield* GCP.Spanner.InstancesInstancePartition("West", {
66 instance: instance.instanceId,
67 config: "regional-us-west1",
68 displayName: "alchemy-west-pt",
69 processingUnits: 1000,
70 });
71 return { instance, partition };
72 }),
73 );
74
75 expect(created.partition.name).toContain("/instancePartitions/");
76 expect(created.partition.instancePartitionId).toEqual(expect.any(String));
77 expect(created.partition.instanceId).toEqual(created.instance.instanceId);
78 expect(created.partition.config).toContain("regional-us-west1");
79 expect(created.partition.displayName).toEqual("alchemy-west-pt");
80 expect(created.partition.state).toEqual("READY");
81
82 const fetched = yield* spanner.getProjectsInstancesInstancePartitions({
83 name: created.partition.name,
84 });
85 expect(fetched.name).toEqual(created.partition.name);
86 expect(fetched.displayName).toEqual("alchemy-west-pt");
87
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 const instance = yield* GCP.Spanner.Instance("App", {
91 instanceId: created.instance.instanceId,
92 config: "regional-us-central1",
93 processingUnits: 1000,
94 displayName: "alchemy-pt-spnr",
95 edition: "ENTERPRISE_PLUS",
96 defaultBackupScheduleType: "NONE",
97 });
98 const partition = yield* GCP.Spanner.InstancesInstancePartition("West", {
99 instance: instance.instanceId,
100 instancePartitionId: created.partition.instancePartitionId,
101 config: "regional-us-west1",
102 displayName: "alchemy-prod-pt",
103 processingUnits: 1000,
104 });
105 return { instance, partition };
106 }),
107 );
108
109 expect(updated.partition.name).toEqual(created.partition.name);
110 expect(updated.partition.displayName).toEqual("alchemy-prod-pt");
111
112 const refetched = yield* spanner.getProjectsInstancesInstancePartitions({
113 name: created.partition.name,
114 });
115 expect(refetched.displayName).toEqual("alchemy-prod-pt");
116
117 yield* stack.destroy();
118
119 const gone = yield* waitUntilGone(created.partition.name);
120 expect(gone).toEqual("gone");
121 }).pipe(logLevel),
122 { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 180_000 },
123 );