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

main
ts 173 lines 5.87 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 secretmanager from "@distilled.cloud/gcp/secretmanager_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 waitUntilGone = (name: string) =>
15 secretmanager.getProjectsLocationsSecrets({ name }).pipe(
16 Effect.as("found" as const),
17 Effect.catchTag(["NotFound"], () => Effect.succeed("gone" as const)),
18 Effect.repeat({
19 schedule: Schedule.spaced("1 second"),
20 until: (status) => status === "gone",
21 times: 10,
22 }),
23 );
24
25 test.provider(
26 "a missing regional secret reaches the regional endpoint and fails with NotFound",
27 (stack) =>
28 Effect.gen(function* () {
29 const { project } = yield* GcpEnvironment.current;
30 yield* stack.destroy();
31 // Distilled routes locations/{region} Secret Manager requests to
32 // secretmanager.{region}.rep.googleapis.com; the global endpoint would
33 // answer BadRequest instead.
34 const error = yield* Effect.flip(
35 secretmanager.getProjectsLocationsSecrets({
36 name: `projects/${project}/locations/us-central1/secrets/alchemy-missing-regional`,
37 }),
38 );
39 expect(error._tag).toEqual("NotFound");
40 yield* stack.destroy();
41 }).pipe(logLevel),
42 {
43 tags: ["provider:gcp", "provider:gcp:secretmanager", "live"],
44 timeout: 60_000,
45 },
46 );
47
48 test.provider(
49 "create, update, and delete a regional secret",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const created = yield* stack.deploy(
55 Effect.gen(function* () {
56 return yield* GCP.SecretManager.LocationsSecret("ApiKey", {
57 location: "us-central1",
58 labels: { env: "test" },
59 annotations: { owner: "payments" },
60 });
61 }),
62 );
63
64 expect(created.name).toContain("/locations/us-central1/secrets/");
65 expect(created.secretId).toEqual(expect.any(String));
66 expect(created.location).toEqual("us-central1");
67 expect(created.labels).toMatchObject({ env: "test" });
68 expect(created.annotations).toMatchObject({ owner: "payments" });
69
70 const fetched = yield* secretmanager.getProjectsLocationsSecrets({
71 name: created.name,
72 });
73 expect(fetched.name).toEqual(created.name);
74 expect(fetched.labels?.env).toEqual("test");
75 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
76 expect(fetched.annotations?.owner).toEqual("payments");
77
78 const payload = yield* Effect.sync(() =>
79 Buffer.from("alchemy-regional-secret", "utf8").toString("base64"),
80 );
81 const version = yield* secretmanager.addVersionProjectsLocationsSecrets({
82 parent: created.name,
83 body: { payload: { data: payload } },
84 });
85 expect(version.name).toContain("/versions/");
86
87 const accessed = yield* secretmanager.accessProjectsLocationsSecretsVersions({
88 name: `${created.name}/versions/latest`,
89 });
90 expect(accessed.payload?.data).toEqual(payload);
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.SecretManager.LocationsSecret("ApiKey", {
95 secretId: created.secretId,
96 location: "us-central1",
97 labels: { env: "prod", role: "api" },
98 annotations: { owner: "platform" },
99 });
100 }),
101 );
102
103 expect(updated.name).toEqual(created.name);
104 expect(updated.labels).toMatchObject({ env: "prod", role: "api" });
105 expect(updated.annotations).toMatchObject({ owner: "platform" });
106
107 const fetchedUpdate = yield* secretmanager.getProjectsLocationsSecrets({
108 name: created.name,
109 });
110 expect(fetchedUpdate.labels?.env).toEqual("prod");
111 expect(fetchedUpdate.labels?.role).toEqual("api");
112 expect(fetchedUpdate.annotations?.owner).toEqual("platform");
113
114 yield* stack.destroy();
115
116 const gone = yield* waitUntilGone(created.name);
117 expect(gone).toEqual("gone");
118 }).pipe(logLevel),
119 {
120 tags: ["provider:gcp", "provider:gcp:secretmanager", "live"],
121 timeout: 90_000,
122 },
123 );
124
125 test.provider(
126 "replace a regional secret when location changes",
127 (stack) =>
128 Effect.gen(function* () {
129 yield* stack.destroy();
130
131 const created = yield* stack.deploy(
132 Effect.gen(function* () {
133 return yield* GCP.SecretManager.LocationsSecret("RegionalKey", {
134 location: "us-central1",
135 });
136 }),
137 );
138
139 expect(created.location).toEqual("us-central1");
140 expect(created.name).toContain("/locations/us-central1/secrets/");
141
142 const replaced = yield* stack.deploy(
143 Effect.gen(function* () {
144 return yield* GCP.SecretManager.LocationsSecret("RegionalKey", {
145 secretId: created.secretId,
146 location: "us-east1",
147 });
148 }),
149 );
150
151 expect(replaced.secretId).toEqual(created.secretId);
152 expect(replaced.location).toEqual("us-east1");
153 expect(replaced.name).toContain("/locations/us-east1/secrets/");
154 expect(replaced.name).not.toEqual(created.name);
155
156 const fetched = yield* secretmanager.getProjectsLocationsSecrets({
157 name: replaced.name,
158 });
159 expect(fetched.name).toEqual(replaced.name);
160
161 const oldGone = yield* waitUntilGone(created.name);
162 expect(oldGone).toEqual("gone");
163
164 yield* stack.destroy();
165
166 const gone = yield* waitUntilGone(replaced.name);
167 expect(gone).toEqual("gone");
168 }).pipe(logLevel),
169 {
170 tags: ["provider:gcp", "provider:gcp:secretmanager", "live"],
171 timeout: 90_000,
172 },
173 );