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

main
ts 126 lines 4.58 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
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const INSTANCE_ID = "alchemy-sthsm-does-not-exist";
15 const PROPOSAL_ID = "alchemy-test-sthsm-proposal";
16 const instanceNameOf = (project: string) =>
17 `projects/${project}/locations/us-central1/singleTenantHsmInstances/${INSTANCE_ID}`;
18 const missingProposalNameOf = (project: string) =>
19 `${instanceNameOf(project)}/proposals/alchemy-sthsm-proposal-does-not-exist`;
20
21 // Creating a SingleTenantHsmInstance succeeds as an LRO then lands in
22 // FAILED (`quorumAuth` without 2FA registration). Proposals against a
23 // FAILED instance return BadRequest (`FAILED_PRECONDITION`: "Can not
24 // create proposal for a FAILED SingleTenantHsmInstance."). A missing
25 // parent returns NotFound. Set GCP_TEST_KMS_HSM=1 and
26 // GCP_TEST_KMS_HSM_INSTANCE to a working instance to run the lifecycle.
27 const runLifecycle =
28 !process.env.FAST &&
29 process.env.GCP_TEST_KMS_HSM === "1" &&
30 !!process.env.GCP_TEST_KMS_HSM_INSTANCE;
31 const entitledInstance = process.env.GCP_TEST_KMS_HSM_INSTANCE ?? "";
32
33 const waitUntilGone = (name: string) =>
34 kms.getProjectsLocationsSingleTenantHsmInstancesProposals({ name }).pipe(
35 Effect.as("found" as const),
36 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
37 Effect.repeat({
38 schedule: Schedule.spaced("1 second"),
39 until: (status) => status === "gone",
40 times: 10,
41 }),
42 );
43
44 test.provider(
45 "getProjectsLocationsSingleTenantHsmInstancesProposals on a missing proposal fails with a typed tag",
46 (stack) =>
47 Effect.gen(function* () {
48 const { project } = yield* GcpEnvironment.current;
49 const missingProposalName = missingProposalNameOf(project);
50 yield* stack.destroy();
51
52 const error = yield* Effect.flip(
53 kms.getProjectsLocationsSingleTenantHsmInstancesProposals({
54 name: missingProposalName,
55 }),
56 );
57 expect(error._tag).toBe("NotFound");
58
59 const gone = yield* waitUntilGone(missingProposalName);
60 expect(gone).toEqual("gone");
61
62 yield* stack.destroy();
63 }).pipe(logLevel),
64 { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 90_000 },
65 );
66
67 test.provider.skipIf(runLifecycle)(
68 "create is rejected with NotFound when the parent HSM instance is missing",
69 (stack) =>
70 Effect.gen(function* () {
71 const { project } = yield* GcpEnvironment.current;
72 const instanceName = instanceNameOf(project);
73 yield* stack.destroy();
74
75 const error = yield* Effect.flip(
76 stack.deploy(
77 Effect.gen(function* () {
78 return yield* GCP.KMS.SingleTenantHsmInstanceProposal("Refresh", {
79 singleTenantHsmInstance: instanceName,
80 proposalId: PROPOSAL_ID,
81 refreshSingleTenantHsmInstance: true,
82 });
83 }),
84 ),
85 );
86 expect(error._tag).toBe("NotFound");
87
88 yield* stack.destroy();
89 }).pipe(logLevel),
90 { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 90_000 },
91 );
92
93 test.provider.skipIf(!runLifecycle)(
94 "create and delete a single-tenant HSM instance proposal",
95 (stack) =>
96 Effect.gen(function* () {
97 const { project } = yield* GcpEnvironment.current;
98 yield* stack.destroy();
99
100 const created = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* GCP.KMS.SingleTenantHsmInstanceProposal("Refresh", {
103 singleTenantHsmInstance: entitledInstance,
104 proposalId: PROPOSAL_ID,
105 refreshSingleTenantHsmInstance: true,
106 });
107 }),
108 );
109
110 expect(created.proposalId).toEqual(PROPOSAL_ID);
111 expect(created.name).toContain(`/proposals/${PROPOSAL_ID}`);
112 expect(created.refreshSingleTenantHsmInstance).toEqual(true);
113 expect(created.project).toEqual(project);
114
115 const fetched = yield* kms.getProjectsLocationsSingleTenantHsmInstancesProposals({
116 name: created.name,
117 });
118 expect(fetched.name).toEqual(created.name);
119
120 yield* stack.destroy();
121
122 const gone = yield* waitUntilGone(created.name);
123 expect(gone).toEqual("gone");
124 }).pipe(logLevel),
125 { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 90_000 },
126 );