@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 112 lines 3.98 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 acm from "@distilled.cloud/gcp/accesscontextmanager_v1";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import { lastSegment, logLevel, projectContext, runLifecycle } from "./common.ts";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const waitUntilGone = (name: string) =>
12 acm.getAccessPoliciesAuthorizedOrgsDescs({ name }).pipe(
13 Effect.as("found" as const),
14 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
15 Effect.repeat({
16 schedule: Schedule.spaced("2 seconds"),
17 until: (status) => status === "gone",
18 times: 10,
19 }),
20 );
21
22 test.provider(
23 "getAccessPoliciesAuthorizedOrgsDescs on an inaccessible descriptor fails with Forbidden",
24 (stack) =>
25 Effect.gen(function* () {
26 yield* stack.destroy();
27
28 const error = yield* Effect.flip(
29 acm.getAccessPoliciesAuthorizedOrgsDescs({
30 name: "accessPolicies/123456789012/authorizedOrgsDescs/alchemy_missing_desc",
31 }),
32 );
33 expect(error._tag).toEqual("Forbidden");
34
35 yield* stack.destroy();
36 }).pipe(logLevel),
37 {
38 tags: ["provider:gcp", "provider:gcp:accesscontextmanager", "live"],
39 timeout: 90_000,
40 },
41 );
42
43 test.provider.skipIf(!runLifecycle)(
44 "create, update, and delete an authorized orgs descriptor",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48
49 const ctx = yield* projectContext();
50 const scopes = ctx.projectNumber.length > 0 ? [`projects/${ctx.projectNumber}`] : undefined;
51 const organization =
52 ctx.organization ??
53 (ctx.parent.startsWith("organizations/") ? ctx.parent : "organizations/123456789012");
54
55 const created = yield* stack.deploy(
56 Effect.gen(function* () {
57 const policy = yield* GCP.AccessContextManager.AccessPolicy("OrgsPolicy", {
58 title: "authorized orgs policy",
59 scopes,
60 });
61 const desc = yield* GCP.AccessContextManager.AccessPoliciesAuthorizedOrgsDesc("Partner", {
62 policy: policy.name,
63 authorizationDirection: "AUTHORIZATION_DIRECTION_FROM",
64 assetType: "ASSET_TYPE_DEVICE",
65 authorizationType: "AUTHORIZATION_TYPE_TRUST",
66 orgs: [organization],
67 });
68 return { policy, desc };
69 }),
70 );
71
72 expect(created.desc.name).toContain("/authorizedOrgsDescs/");
73 expect(created.desc.policy).toEqual(created.policy.name);
74 expect(created.desc.authorizationDirection).toEqual("AUTHORIZATION_DIRECTION_FROM");
75 expect(created.desc.assetType).toEqual("ASSET_TYPE_DEVICE");
76 expect(created.desc.orgs.map(lastSegment)).toContain(lastSegment(organization));
77
78 const fetched = yield* acm.getAccessPoliciesAuthorizedOrgsDescs({
79 name: created.desc.name,
80 });
81 expect(fetched.name).toEqual(created.desc.name);
82
83 const updated = yield* stack.deploy(
84 Effect.gen(function* () {
85 const policy = yield* GCP.AccessContextManager.AccessPolicy("OrgsPolicy", {
86 title: "authorized orgs policy",
87 scopes,
88 });
89 const desc = yield* GCP.AccessContextManager.AccessPoliciesAuthorizedOrgsDesc("Partner", {
90 policy: policy.name,
91 authorizedOrgsDescId: created.desc.authorizedOrgsDescId,
92 authorizationDirection: "AUTHORIZATION_DIRECTION_FROM",
93 assetType: "ASSET_TYPE_DEVICE",
94 authorizationType: "AUTHORIZATION_TYPE_TRUST",
95 orgs: [organization],
96 });
97 return { policy, desc };
98 }),
99 );
100
101 expect(updated.desc.name).toEqual(created.desc.name);
102
103 yield* stack.destroy();
104
105 const gone = yield* waitUntilGone(created.desc.name);
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel),
108 {
109 tags: ["provider:gcp", "provider:gcp:accesscontextmanager", "live"],
110 timeout: 120_000,
111 },
112 );