@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 150 lines 5.02 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as secretmanager from "@distilled.cloud/gcp/secretmanager_v1";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const waitUntilGone = (name: string) =>
14 secretmanager.getProjectsSecrets({ name }).pipe(
15 Effect.as("found" as const),
16 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
17 Effect.repeat({
18 schedule: Schedule.spaced("1 second"),
19 until: (status) => status === "gone",
20 times: 10,
21 }),
22 );
23
24 test.provider(
25 "create, update, and delete a secret",
26 (stack) =>
27 Effect.gen(function* () {
28 yield* stack.destroy();
29
30 const created = yield* stack.deploy(
31 Effect.gen(function* () {
32 return yield* GCP.SecretManager.Secret("ApiKey", {
33 labels: { env: "test" },
34 annotations: { owner: "payments" },
35 });
36 }),
37 );
38
39 expect(created.name).toContain("/secrets/");
40 expect(created.secretId).toEqual(expect.any(String));
41 expect(created.labels).toMatchObject({ env: "test" });
42 expect(created.annotations).toMatchObject({ owner: "payments" });
43 expect(created.replication?.automatic).toBeDefined();
44
45 const fetched = yield* secretmanager.getProjectsSecrets({
46 name: created.name,
47 });
48 expect(fetched.name).toEqual(created.name);
49 expect(fetched.labels?.env).toEqual("test");
50 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
51 expect(fetched.annotations?.owner).toEqual("payments");
52 expect(fetched.replication?.automatic).toBeDefined();
53
54 const payload = yield* Effect.sync(() =>
55 Buffer.from("alchemy-secret-payload", "utf8").toString("base64"),
56 );
57 const version = yield* secretmanager.addVersionProjectsSecrets({
58 parent: created.name,
59 body: { payload: { data: payload } },
60 });
61 expect(version.name).toContain("/versions/");
62
63 const accessed = yield* secretmanager.accessProjectsSecretsVersions({
64 name: `${created.name}/versions/latest`,
65 });
66 expect(accessed.payload?.data).toEqual(payload);
67
68 const updated = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.SecretManager.Secret("ApiKey", {
71 secretId: created.secretId,
72 labels: { env: "prod", role: "api" },
73 annotations: { owner: "platform" },
74 });
75 }),
76 );
77
78 expect(updated.name).toEqual(created.name);
79 expect(updated.labels).toMatchObject({ env: "prod", role: "api" });
80 expect(updated.annotations).toMatchObject({ owner: "platform" });
81
82 const fetchedUpdate = yield* secretmanager.getProjectsSecrets({
83 name: created.name,
84 });
85 expect(fetchedUpdate.labels?.env).toEqual("prod");
86 expect(fetchedUpdate.labels?.role).toEqual("api");
87 expect(fetchedUpdate.annotations?.owner).toEqual("platform");
88
89 yield* stack.destroy();
90
91 const gone = yield* waitUntilGone(created.name);
92 expect(gone).toEqual("gone");
93 }).pipe(logLevel),
94 {
95 tags: ["provider:gcp", "provider:gcp:secretmanager", "live"],
96 timeout: 90_000,
97 },
98 );
99
100 test.provider(
101 "replace a secret when replication changes",
102 (stack) =>
103 Effect.gen(function* () {
104 yield* stack.destroy();
105
106 const created = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* GCP.SecretManager.Secret("RegionalKey", {
109 replication: { automatic: {} },
110 });
111 }),
112 );
113
114 expect(created.replication?.automatic).toBeDefined();
115 expect(created.replication?.userManaged).toBeUndefined();
116
117 const replaced = yield* stack.deploy(
118 Effect.gen(function* () {
119 return yield* GCP.SecretManager.Secret("RegionalKey", {
120 secretId: created.secretId,
121 replication: {
122 userManaged: {
123 replicas: [{ location: "us-central1" }],
124 },
125 },
126 });
127 }),
128 );
129
130 expect(replaced.name).toEqual(created.name);
131 expect(replaced.secretId).toEqual(created.secretId);
132 expect(replaced.replication?.automatic).toBeUndefined();
133 expect(replaced.replication?.userManaged?.replicas?.[0]?.location).toEqual("us-central1");
134
135 const fetched = yield* secretmanager.getProjectsSecrets({
136 name: replaced.name,
137 });
138 expect(fetched.replication?.automatic).toBeUndefined();
139 expect(fetched.replication?.userManaged?.replicas?.[0]?.location).toEqual("us-central1");
140
141 yield* stack.destroy();
142
143 const gone = yield* waitUntilGone(replaced.name);
144 expect(gone).toEqual("gone");
145 }).pipe(logLevel),
146 {
147 tags: ["provider:gcp", "provider:gcp:secretmanager", "live"],
148 timeout: 90_000,
149 },
150 );