@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 93 lines 3.42 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 storage from "@distilled.cloud/gcp/storage_v1";
6 import * as Effect from "effect/Effect";
7 import * as Redacted from "effect/Redacted";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 // A just-created HMAC key can read back as 404 "Access ID not found" briefly.
14 const getKey = (projectId: string, accessId: string) =>
15 storage.getProjectsHmacKeys({ projectId, accessId }).pipe(
16 Effect.retry({
17 while: (error) => error._tag === "NotFound",
18 schedule: Schedule.spaced("1 second"),
19 times: 10,
20 }),
21 );
22
23 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
24
25 // The testing profile's own service account, derived from the configured project.
26 const serviceAccountEmailOf = GcpEnvironment.current.pipe(
27 Effect.map(({ project }) => `alchemy-testing@${project}.iam.gserviceaccount.com`),
28 );
29
30 const waitUntilGone = (projectId: string, accessId: string) =>
31 storage.getProjectsHmacKeys({ projectId, accessId }).pipe(
32 Effect.map((metadata) =>
33 metadata.state?.toUpperCase() === "DELETED" ? ("gone" as const) : ("found" as const),
34 ),
35 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
36 Effect.repeat({
37 schedule: Schedule.spaced("1 second"),
38 until: (status) => status === "gone",
39 times: 10,
40 }),
41 );
42
43 test.provider(
44 "create, update, and delete an HMAC key",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48 const serviceAccountEmail = yield* serviceAccountEmailOf;
49
50 const created = yield* stack.deploy(
51 Effect.gen(function* () {
52 return yield* GCP.Storage.HmacKey("Interop", {
53 serviceAccountEmail,
54 });
55 }),
56 );
57
58 expect(created.accessId).toEqual(expect.any(String));
59 expect(created.accessId.length).toBeGreaterThan(0);
60 expect(created.serviceAccountEmail).toEqual(serviceAccountEmail);
61 expect(created.state).toEqual("ACTIVE");
62 expect(created.projectId).toEqual(expect.any(String));
63 expect(Redacted.isRedacted(created.secret)).toEqual(true);
64
65 const fetched = yield* getKey(created.projectId, created.accessId);
66 expect(fetched.accessId).toEqual(created.accessId);
67 expect(fetched.state).toEqual("ACTIVE");
68 expect(fetched.serviceAccountEmail).toEqual(serviceAccountEmail);
69
70 const updated = yield* stack.deploy(
71 Effect.gen(function* () {
72 return yield* GCP.Storage.HmacKey("Interop", {
73 serviceAccountEmail,
74 state: "INACTIVE",
75 });
76 }),
77 );
78
79 expect(updated.accessId).toEqual(created.accessId);
80 expect(updated.state).toEqual("INACTIVE");
81 expect(Redacted.isRedacted(updated.secret)).toEqual(true);
82
83 const refetched = yield* getKey(updated.projectId, updated.accessId);
84 expect(refetched.accessId).toEqual(created.accessId);
85 expect(refetched.state).toEqual("INACTIVE");
86
87 yield* stack.destroy();
88
89 const gone = yield* waitUntilGone(created.projectId, created.accessId);
90 expect(gone).toEqual("gone");
91 }).pipe(logLevel),
92 { tags: ["provider:gcp", "provider:gcp:storage", "live"], timeout: 90_000 },
93 );