The best way to control Claude Code, Codex and more from any computer or phone

main
ts 155 lines 5.49 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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as networksecurity from "@distilled.cloud/gcp/networksecurity_v1";
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 waitUntilGone = (name: string) =>
16 networksecurity.getOrganizationsLocationsAddressGroups({ name }).pipe(
17 Effect.as("found" as const),
18 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
19 Effect.repeat({
20 schedule: Schedule.spaced("1 second"),
21 until: (status) => status === "gone",
22 times: 10,
23 }),
24 );
25
26 test.provider(
27 "getOrganizationsLocationsAddressGroups on a missing group fails with NotFound (Forbidden without org IAM)",
28 (stack) =>
29 Effect.gen(function* () {
30 const { project } = yield* GcpEnvironment.current;
31 yield* stack.destroy();
32
33 const resource = yield* resourcemanager.getProjects({
34 name: `projects/${project}`,
35 });
36 const parent = resource.parent ?? "organizations/0";
37 const organization = parent.startsWith("organizations/")
38 ? parent.slice("organizations/".length)
39 : "0";
40 const error = yield* Effect.flip(
41 networksecurity.getOrganizationsLocationsAddressGroups({
42 name: `organizations/${organization}/locations/global/addressGroups/alchemy-missing-address-group`,
43 }),
44 );
45 expect(error._tag).toEqual(
46 process.env.GCP_TEST_ORG_NETWORKSECURITY ? "NotFound" : "Forbidden",
47 );
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
53 timeout: 90_000,
54 },
55 );
56
57 // The testing credentials lack org-level Network Security IAM (Forbidden:
58 // "Permission 'networksecurity.….get' denied on 'organizations/…'"). Set
59 // GCP_TEST_ORG_NETWORKSECURITY=1 when they administer the organization.
60 test.provider.skipIf(!!process.env.GCP_TEST_ORG_NETWORKSECURITY)(
61 "createOrganizationsLocationsAddressGroups without org IAM fails with Forbidden",
62 (stack) =>
63 Effect.gen(function* () {
64 const { project } = yield* GcpEnvironment.current;
65 yield* stack.destroy();
66
67 const resource = yield* resourcemanager.getProjects({
68 name: `projects/${project}`,
69 });
70 const parent = resource.parent ?? "organizations/0";
71 const organization = parent.startsWith("organizations/")
72 ? parent.slice("organizations/".length)
73 : parent.startsWith("folders/")
74 ? "0"
75 : parent;
76 const error = yield* Effect.flip(
77 networksecurity.createOrganizationsLocationsAddressGroups({
78 parent: `organizations/${organization}/locations/global`,
79 addressGroupId: "alchemy-org-address-group-probe",
80 body: {
81 type: "IPV4",
82 capacity: 100,
83 items: ["10.0.0.1"],
84 },
85 }),
86 );
87 expect(error._tag).toBe("Forbidden");
88
89 yield* stack.destroy();
90 }).pipe(logLevel),
91 {
92 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
93 timeout: 90_000,
94 },
95 );
96
97 test.provider.skipIf(!!process.env.FAST || !process.env.GCP_TEST_ORG_NETWORKSECURITY)(
98 "create, update, and delete an organization address group",
99 (stack) =>
100 Effect.gen(function* () {
101 yield* stack.destroy();
102
103 const created = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GCP.NetworkSecurity.OrganizationsAddressGroup("OrgAllowlist", {
106 type: "IPV4",
107 capacity: 100,
108 items: ["10.0.0.1"],
109 description: "org address group a",
110 labels: { env: "test" },
111 });
112 }),
113 );
114
115 expect(created.name).toContain("/addressGroups/");
116 expect(created.name).toContain("organizations/");
117 expect(created.addressGroupId).toEqual(expect.any(String));
118 expect(created.location).toEqual("global");
119 expect(created.items).toEqual(["10.0.0.1"]);
120 expect(created.labels).toMatchObject({ env: "test" });
121
122 const fetched = yield* networksecurity.getOrganizationsLocationsAddressGroups({
123 name: created.name,
124 });
125 expect(fetched.name).toEqual(created.name);
126 expect(fetched.labels?.env).toEqual("test");
127
128 const updated = yield* stack.deploy(
129 Effect.gen(function* () {
130 return yield* GCP.NetworkSecurity.OrganizationsAddressGroup("OrgAllowlist", {
131 addressGroupId: created.addressGroupId,
132 organization: created.organization,
133 type: "IPV4",
134 capacity: 100,
135 items: ["10.0.0.1", "10.1.0.0/24"],
136 description: "org address group b",
137 labels: { env: "prod", role: "allowlist" },
138 });
139 }),
140 );
141
142 expect(updated.name).toEqual(created.name);
143 expect(updated.description).toEqual("org address group b");
144 expect(updated.labels).toMatchObject({ env: "prod", role: "allowlist" });
145
146 yield* stack.destroy();
147
148 const gone = yield* waitUntilGone(created.name);
149 expect(gone).toEqual("gone");
150 }).pipe(logLevel),
151 {
152 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
153 timeout: 90_000,
154 },
155 );