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

main
ts 122 lines 4.55 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 and ImportJobs cannot be deleted. Reuse constant
16 // ids so re-runs observe the existing resources instead of leaking.
17 const IMPORT_JOB_ID = "alchemy-test-importjob";
18
19 const waitUntilGone = (name: string) =>
20 kms.getProjectsLocationsKeyRingsImportJobs({ name }).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 "getProjectsLocationsKeyRingsImportJobs on a missing job fails with NotFound",
32 () =>
33 Effect.gen(function* () {
34 const { project } = yield* GcpEnvironment.current;
35 const error = yield* Effect.flip(
36 kms.getProjectsLocationsKeyRingsImportJobs({
37 name: `projects/${project}/locations/us-central1/keyRings/${KEY_RING_ID}/importJobs/alchemy-importjob-does-not-exist`,
38 }),
39 );
40 expect(error._tag).toBe("NotFound");
41 }).pipe(logLevel),
42 { tags: ["provider:gcp", "provider:gcp:kms", "live"] },
43 );
44
45 test.provider(
46 "create and destroy an import job",
47 (stack) =>
48 Effect.gen(function* () {
49 const { project } = yield* GcpEnvironment.current;
50 yield* stack.destroy();
51
52 const created = yield* stack.deploy(
53 Effect.gen(function* () {
54 const ring = yield* GCP.KMS.KeyRing("Keys", {
55 keyRingId: KEY_RING_ID,
56 location: "us-central1",
57 });
58 return yield* GCP.KMS.ImportJob("Wrap", {
59 keyRing: ring.name,
60 importJobId: IMPORT_JOB_ID,
61 importMethod: "RSA_OAEP_3072_SHA256_AES_256",
62 protectionLevel: "SOFTWARE",
63 });
64 }),
65 );
66
67 expect(created.importJobId).toEqual(IMPORT_JOB_ID);
68 expect(created.location).toEqual("us-central1");
69 expect(created.project).toEqual(project);
70 expect(created.keyRing).toContain(`/keyRings/${KEY_RING_ID}`);
71 expect(created.name).toEqual(
72 `projects/${project}/locations/us-central1/keyRings/${KEY_RING_ID}/importJobs/${IMPORT_JOB_ID}`,
73 );
74 expect(created.importMethod).toEqual("RSA_OAEP_3072_SHA256_AES_256");
75 expect(created.protectionLevel).toEqual("SOFTWARE");
76 expect(created.createTime).toEqual(expect.any(String));
77 expect(["ACTIVE", "EXPIRED"]).toContain(created.state);
78 if (created.state === "ACTIVE") {
79 expect(created.publicKeyPem ?? created.publicKeyData).toEqual(expect.any(String));
80 }
81
82 const fetched = yield* kms.getProjectsLocationsKeyRingsImportJobs({
83 name: created.name,
84 });
85 expect(fetched.name).toEqual(created.name);
86 expect(fetched.importMethod).toEqual("RSA_OAEP_3072_SHA256_AES_256");
87 expect(fetched.protectionLevel).toEqual("SOFTWARE");
88
89 const updated = yield* stack.deploy(
90 Effect.gen(function* () {
91 const ring = yield* GCP.KMS.KeyRing("Keys", {
92 keyRingId: KEY_RING_ID,
93 location: "us-central1",
94 });
95 return yield* GCP.KMS.ImportJob("Wrap", {
96 keyRing: ring.name,
97 importJobId: IMPORT_JOB_ID,
98 importMethod: "RSA_OAEP_3072_SHA256_AES_256",
99 protectionLevel: "SOFTWARE",
100 });
101 }),
102 );
103 expect(updated.name).toEqual(created.name);
104 expect(updated.createTime).toEqual(created.createTime);
105
106 yield* stack.destroy();
107
108 const stillThere = yield* kms
109 .getProjectsLocationsKeyRingsImportJobs({ name: created.name })
110 .pipe(
111 Effect.as("found" as const),
112 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
113 );
114 expect(stillThere).toEqual("found");
115
116 const gone = yield* waitUntilGone(
117 `projects/${project}/locations/us-central1/keyRings/${KEY_RING_ID}/importJobs/alchemy-importjob-does-not-exist`,
118 );
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 90_000 },
122 );