@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 91 lines 3.41 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 iam from "@distilled.cloud/gcp/unstable/iam_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 // New accounts read inconsistently for a few seconds after create.
15 const getAccount = (name: string) =>
16 iam.getProjectsServiceAccounts({ name }).pipe(
17 Effect.retry({
18 while: (error) => error._tag === "NotFound",
19 schedule: Schedule.exponential("500 millis"),
20 times: 8,
21 }),
22 );
23
24 const waitUntilGone = (name: string) =>
25 iam.getProjectsServiceAccounts({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35 test.provider(
36 "create, update, and delete a service account",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40 yield* stack.destroy();
41
42 const created = yield* stack.deploy(
43 GCP.IAM.ServiceAccount("Worker", {
44 displayName: "Alchemy worker",
45 description: "test account",
46 }),
47 );
48
49 expect(created.accountId).toMatch(/^[a-z][a-z0-9-]{4,28}[a-z0-9]$/);
50 expect(created.project).toEqual(project);
51 expect(created.email).toEqual(`${created.accountId}@${project}.iam.gserviceaccount.com`);
52 expect(created.name).toEqual(`projects/${project}/serviceAccounts/${created.email}`);
53 expect(created.displayName).toEqual("Alchemy worker");
54 expect(created.description).toEqual("test account");
55 expect(created.uniqueId).toEqual(expect.any(String));
56
57 const fetched = yield* getAccount(created.name);
58 expect(fetched.email).toEqual(created.email);
59 expect(fetched.displayName).toEqual("Alchemy worker");
60 expect(fetched.description).toMatch(/^\[alchemy .*alchemy-id=\S*worker\]\ntest account$/);
61
62 // Pinning the generated id must update in place, not replace.
63 const updated = yield* stack.deploy(
64 GCP.IAM.ServiceAccount("Worker", {
65 accountId: created.accountId,
66 displayName: "Alchemy worker (prod)",
67 description: "updated account",
68 }),
69 );
70
71 expect(updated.name).toEqual(created.name);
72 expect(updated.uniqueId).toEqual(created.uniqueId);
73 expect(updated.displayName).toEqual("Alchemy worker (prod)");
74 expect(updated.description).toEqual("updated account");
75
76 const fetchedUpdate = yield* getAccount(created.name).pipe(
77 Effect.repeat({
78 schedule: Schedule.spaced("1 second"),
79 until: (account) => account.displayName === "Alchemy worker (prod)",
80 times: 10,
81 }),
82 );
83 expect(fetchedUpdate.displayName).toEqual("Alchemy worker (prod)");
84 expect(fetchedUpdate.description).toMatch(/\nupdated account$/);
85
86 yield* stack.destroy();
87
88 expect(yield* waitUntilGone(created.name)).toEqual("gone");
89 }).pipe(logLevel),
90 { tags: ["provider:gcp", "provider:gcp:iam", "live"], timeout: 120_000 },
91 );