@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 160 lines 5.67 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 assuredworkloads from "@distilled.cloud/gcp/assuredworkloads_v1";
6 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const defaultLocation = "us-central1";
16
17 // Workloads live under an organization the credentials administer. Set
18 // GOOGLE_ORGANIZATION_ID and GOOGLE_BILLING_ACCOUNT to run the lifecycle.
19 const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim();
20 const organization = organizationId
21 ? organizationId.startsWith("organizations/")
22 ? organizationId
23 : `organizations/${organizationId}`
24 : undefined;
25
26 const waitUntilGone = (name: string) =>
27 assuredworkloads.getOrganizationsLocationsWorkloads({ name }).pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("2 seconds"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37 const billingAccount = process.env.GOOGLE_BILLING_ACCOUNT
38 ? process.env.GOOGLE_BILLING_ACCOUNT.startsWith("billingAccounts/")
39 ? process.env.GOOGLE_BILLING_ACCOUNT
40 : `billingAccounts/${process.env.GOOGLE_BILLING_ACCOUNT}`
41 : undefined;
42
43 const runLifecycle =
44 organization !== undefined && billingAccount !== undefined && !process.env.FAST;
45
46 test.provider.skipIf(organization === undefined)(
47 "getOrganizationsLocationsWorkloads on a missing workload fails with NotFound",
48 (stack) =>
49 Effect.gen(function* () {
50 yield* stack.destroy();
51
52 const error = yield* Effect.flip(
53 assuredworkloads.getOrganizationsLocationsWorkloads({
54 name: `${organization}/locations/${defaultLocation}/workloads/alchemy-missing`,
55 }),
56 );
57 expect(error._tag).toEqual("NotFound");
58
59 yield* stack.destroy();
60 }).pipe(logLevel),
61 {
62 tags: ["provider:gcp", "provider:gcp:assuredworkloads", "live"],
63 timeout: 90_000,
64 },
65 );
66
67 test.provider.skipIf(organization !== undefined)(
68 "getOrganizationsLocationsWorkloads without organization access fails with Forbidden",
69 (stack) =>
70 Effect.gen(function* () {
71 const { project } = yield* GcpEnvironment.current;
72 yield* stack.destroy();
73
74 const parent = yield* resourcemanager.getProjects({
75 name: `projects/${project}`,
76 });
77 // "Permission 'assuredworkloads.workload.get' denied on resource ...
78 // (or it may not exist)": the testing credentials hold no
79 // organization-level Assured Workloads role.
80 const error = yield* Effect.flip(
81 assuredworkloads.getOrganizationsLocationsWorkloads({
82 name: `${parent.parent}/locations/${defaultLocation}/workloads/alchemy-missing`,
83 }),
84 );
85 expect(error._tag).toEqual("Forbidden");
86
87 yield* stack.destroy();
88 }).pipe(logLevel),
89 {
90 tags: ["provider:gcp", "provider:gcp:assuredworkloads", "live"],
91 timeout: 90_000,
92 },
93 );
94
95 test.provider.skipIf(!runLifecycle)(
96 "create, update, and delete a workload",
97 (stack) =>
98 Effect.gen(function* () {
99 yield* stack.destroy();
100
101 const created = yield* stack.deploy(
102 Effect.gen(function* () {
103 return yield* GCP.AssuredWorkloads.Workload("Regulated", {
104 organization: organization!,
105 location: defaultLocation,
106 displayName: "alchemy test",
107 complianceRegime: "US_REGIONAL_ACCESS",
108 billingAccount,
109 labels: { env: "test" },
110 });
111 }),
112 );
113
114 expect(created.workloadId).toEqual(expect.any(String));
115 expect(created.organization).toEqual(organization);
116 expect(created.location).toEqual(defaultLocation);
117 expect(created.name).toEqual(
118 `${organization}/locations/${defaultLocation}/workloads/${created.workloadId}`,
119 );
120 expect(created.displayName).toEqual("alchemy test");
121 expect(created.complianceRegime).toEqual("US_REGIONAL_ACCESS");
122 expect(created.labels).toMatchObject({ env: "test" });
123
124 const fetched = yield* assuredworkloads.getOrganizationsLocationsWorkloads({
125 name: created.name,
126 });
127 expect(fetched.name).toEqual(created.name);
128 expect(fetched.labels?.env).toEqual("test");
129 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
130
131 const updated = yield* stack.deploy(
132 Effect.gen(function* () {
133 return yield* GCP.AssuredWorkloads.Workload("Regulated", {
134 organization: organization!,
135 location: defaultLocation,
136 displayName: "alchemy prod",
137 complianceRegime: "US_REGIONAL_ACCESS",
138 billingAccount,
139 labels: { env: "prod", role: "aw" },
140 violationNotificationsEnabled: false,
141 });
142 }),
143 );
144
145 expect(updated.name).toEqual(created.name);
146 expect(updated.workloadId).toEqual(created.workloadId);
147 expect(updated.displayName).toEqual("alchemy prod");
148 expect(updated.labels).toMatchObject({ env: "prod", role: "aw" });
149 expect(updated.violationNotificationsEnabled).toEqual(false);
150
151 yield* stack.destroy();
152
153 const gone = yield* waitUntilGone(created.name);
154 expect(gone).toEqual("gone");
155 }).pipe(logLevel),
156 {
157 tags: ["provider:gcp", "provider:gcp:assuredworkloads", "live"],
158 timeout: 120_000,
159 },
160 );