@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 123 lines 4.26 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/iam_v2";
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 // Needs roles/iam.denyAdmin on the project (GCP_TEST_IAM_DENY_ADMIN=1); Owner
15 // does not include it, so create fails with `Forbidden: Permission
16 // iam.googleapis.com/denypolicies.create denied on resource ...`.
17 const runLifecycle = !!process.env.GCP_TEST_IAM_DENY_ADMIN;
18
19 const attachmentOf = (project: string) =>
20 encodeURIComponent(`cloudresourcemanager.googleapis.com/projects/${project}`);
21 const missingNameOf = (project: string) =>
22 `policies/${attachmentOf(project)}/denypolicies/alchemy-missing`;
23
24 const waitUntilGone = (name: string) =>
25 iam.getPolicies({ name }).pipe(
26 Effect.map((policy) => (policy.deleteTime ? ("gone" as const) : ("found" as const))),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("2 seconds"),
30 until: (status) => status === "gone",
31 times: 8,
32 }),
33 );
34
35 test.provider(
36 "getPolicies on a missing deny policy fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40 const missingName = missingNameOf(project);
41 yield* stack.destroy();
42
43 const error = yield* Effect.flip(iam.getPolicies({ name: missingName }));
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel),
48 { tags: ["provider:gcp", "provider:gcp:iam", "live"], timeout: 90_000 },
49 );
50
51 test.provider.skipIf(runLifecycle)(
52 "createPolicyPolicies without deny-policy access fails with a typed tag",
53 (stack) =>
54 Effect.gen(function* () {
55 const { project } = yield* GcpEnvironment.current;
56 const attachment = attachmentOf(project);
57 yield* stack.destroy();
58
59 const error = yield* Effect.flip(
60 iam.createPolicyPolicies({
61 parent: `policies/${attachment}/denypolicies`,
62 policyId: "alchemy-probe",
63 body: {
64 displayName: "alchemy-probe",
65 rules: [
66 {
67 denyRule: {
68 deniedPermissions: ["iam.googleapis.com/roles.list"],
69 deniedPrincipals: ["principal://goog/subject/alchemy-deny-probe@example.invalid"],
70 },
71 },
72 ],
73 },
74 }),
75 );
76 expect(error._tag).toEqual("Forbidden");
77 expect(error.message).toContain("denypolicies.create");
78
79 yield* stack.destroy();
80 }).pipe(logLevel),
81 { tags: ["provider:gcp", "provider:gcp:iam", "live"], timeout: 90_000 },
82 );
83
84 test.provider.skipIf(!runLifecycle)(
85 "create, update, and delete a deny policy",
86 (stack) =>
87 Effect.gen(function* () {
88 yield* stack.destroy();
89
90 const created = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* GCP.IAM.Policy("Probe", {
93 displayName: "alchemy-probe",
94 });
95 }),
96 );
97
98 expect(created.name).toContain("/denypolicies/");
99 expect(created.policyId).toEqual(expect.any(String));
100 expect(created.displayName).toEqual("alchemy-probe");
101
102 const fetched = yield* iam.getPolicies({ name: created.name });
103 expect(fetched.name).toEqual(created.name);
104 expect(fetched.annotations?.["alchemy-id"]).toEqual(expect.any(String));
105
106 const updated = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* GCP.IAM.Policy("Probe", {
109 policyId: created.policyId,
110 displayName: "alchemy-probe-v2",
111 });
112 }),
113 );
114 expect(updated.name).toEqual(created.name);
115 expect(updated.displayName).toEqual("alchemy-probe-v2");
116
117 yield* stack.destroy();
118
119 const gone = yield* waitUntilGone(created.name);
120 expect(gone).toEqual("gone");
121 }).pipe(logLevel),
122 { tags: ["provider:gcp", "provider:gcp:iam", "live"], timeout: 90_000 },
123 );