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

main
ts 123 lines 4.2 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 kms from "@distilled.cloud/gcp/cloudkms_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { KEY_RING_ID } from "./common.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Cloud KMS KeyRings cannot be deleted. Reuse a constant id so re-runs
16 // observe the existing ring instead of leaking a new one every pass.
17
18 const waitUntilGone = (name: string) =>
19 kms.getProjectsLocationsKeyRings({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getProjectsLocationsKeyRings on a missing key ring fails with NotFound",
31 () =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34 const error = yield* Effect.flip(
35 kms.getProjectsLocationsKeyRings({
36 name: `projects/${project}/locations/us-central1/keyRings/alchemy-keyring-does-not-exist`,
37 }),
38 );
39 expect(error._tag).toBe("NotFound");
40 }).pipe(logLevel),
41 { tags: ["provider:gcp", "provider:gcp:kms", "live"] },
42 );
43
44 test.provider(
45 "create, replace, and destroy a key ring",
46 (stack) =>
47 Effect.gen(function* () {
48 const { project } = yield* GcpEnvironment.current;
49 yield* stack.destroy();
50
51 const created = yield* stack.deploy(
52 Effect.gen(function* () {
53 return yield* GCP.KMS.KeyRing("Keys", {
54 keyRingId: KEY_RING_ID,
55 location: "us-central1",
56 });
57 }),
58 );
59
60 expect(created.keyRingId).toEqual(KEY_RING_ID);
61 expect(created.location).toEqual("us-central1");
62 expect(created.project).toEqual(project);
63 expect(created.name).toEqual(
64 `projects/${project}/locations/us-central1/keyRings/${KEY_RING_ID}`,
65 );
66 expect(created.createTime).toEqual(expect.any(String));
67
68 const fetched = yield* kms.getProjectsLocationsKeyRings({
69 name: created.name,
70 });
71 expect(fetched.name).toEqual(created.name);
72
73 const updated = yield* stack.deploy(
74 Effect.gen(function* () {
75 return yield* GCP.KMS.KeyRing("Keys", {
76 keyRingId: KEY_RING_ID,
77 location: "us-central1",
78 });
79 }),
80 );
81 expect(updated.name).toEqual(created.name);
82 expect(updated.createTime).toEqual(created.createTime);
83
84 const replaced = yield* stack.deploy(
85 Effect.gen(function* () {
86 return yield* GCP.KMS.KeyRing("Keys", {
87 keyRingId: KEY_RING_ID,
88 location: "us-east1",
89 });
90 }),
91 );
92 expect(replaced.location).toEqual("us-east1");
93 expect(replaced.keyRingId).toEqual(KEY_RING_ID);
94 expect(replaced.name).toEqual(
95 `projects/${project}/locations/us-east1/keyRings/${KEY_RING_ID}`,
96 );
97
98 const fetchedReplacement = yield* kms.getProjectsLocationsKeyRings({
99 name: replaced.name,
100 });
101 expect(fetchedReplacement.name).toEqual(replaced.name);
102
103 // The previous location's ring cannot be deleted and remains.
104 const previous = yield* kms.getProjectsLocationsKeyRings({
105 name: created.name,
106 });
107 expect(previous.name).toEqual(created.name);
108
109 yield* stack.destroy();
110
111 const stillThere = yield* kms.getProjectsLocationsKeyRings({ name: replaced.name }).pipe(
112 Effect.as("found" as const),
113 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
114 );
115 expect(stillThere).toEqual("found");
116
117 const gone = yield* waitUntilGone(
118 `projects/${project}/locations/us-central1/keyRings/alchemy-keyring-does-not-exist`,
119 );
120 expect(gone).toEqual("gone");
121 }).pipe(logLevel),
122 { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 90_000 },
123 );