@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 303 lines 10.9 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 compute from "@distilled.cloud/gcp/compute_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { DEFAULT_NETWORK, defaultNetworkSelfLink } from "../networkQuota.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const region = "us-central1";
16 const healthCheckName = "alchemy-sa-probe";
17 const backendName = "alchemy-sa-ilb";
18
19 const waitUntilGone = (serviceAttachmentName: string, projectId: string) =>
20 compute
21 .getServiceAttachments({
22 project: projectId,
23 region,
24 serviceAttachment: serviceAttachmentName,
25 })
26 .pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("2 seconds"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36 const waitRegionOp = (operation: compute.Operation) => {
37 if (operation.status === "DONE") return Effect.succeed(operation);
38 const name = (operation.name ?? "").split("/").pop() ?? "";
39 return GcpEnvironment.current.pipe(
40 Effect.flatMap(({ project }) =>
41 compute.getRegionOperations({ project, region, operation: name }).pipe(
42 Effect.repeat({
43 schedule: Schedule.spaced("2 seconds"),
44 until: (op) => op.status === "DONE",
45 times: 20,
46 }),
47 ),
48 ),
49 );
50 };
51
52 const waitGlobalOp = (operation: compute.Operation) => {
53 if (operation.status === "DONE") return Effect.succeed(operation);
54 const name = (operation.name ?? "").split("/").pop() ?? "";
55 return GcpEnvironment.current.pipe(
56 Effect.flatMap(({ project }) =>
57 compute.getGlobalOperations({ project, operation: name }).pipe(
58 Effect.repeat({
59 schedule: Schedule.spaced("2 seconds"),
60 until: (op) => op.status === "DONE",
61 times: 20,
62 }),
63 ),
64 ),
65 );
66 };
67
68 const ensureProducer = () =>
69 Effect.gen(function* () {
70 const { project } = yield* GcpEnvironment.current;
71 const existingCheck = yield* compute
72 .getHealthChecks({ project, healthCheck: healthCheckName })
73 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
74 const healthCheck =
75 existingCheck ??
76 (yield* compute
77 .insertHealthChecks({
78 project,
79 body: {
80 name: healthCheckName,
81 type: "TCP",
82 tcpHealthCheck: { port: 80 },
83 checkIntervalSec: 5,
84 timeoutSec: 5,
85 },
86 })
87 .pipe(
88 Effect.flatMap(waitGlobalOp),
89 Effect.catchTag("Conflict", () => Effect.void),
90 )
91 .pipe(
92 Effect.flatMap(() => compute.getHealthChecks({ project, healthCheck: healthCheckName })),
93 ));
94
95 const existingBackend = yield* compute
96 .getRegionBackendServices({
97 project,
98 region,
99 backendService: backendName,
100 })
101 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
102 const backend =
103 existingBackend ??
104 (yield* compute
105 .insertRegionBackendServices({
106 project,
107 region,
108 body: {
109 name: backendName,
110 protocol: "TCP",
111 loadBalancingScheme: "INTERNAL",
112 healthChecks: healthCheck.selfLink ? [healthCheck.selfLink] : [],
113 },
114 })
115 .pipe(
116 Effect.flatMap(waitRegionOp),
117 Effect.catchTag("Conflict", () => Effect.void),
118 )
119 .pipe(
120 Effect.flatMap(() =>
121 compute.getRegionBackendServices({
122 project,
123 region,
124 backendService: backendName,
125 }),
126 ),
127 ));
128
129 return { healthCheck, backend };
130 });
131
132 const deleteProducer = () =>
133 GcpEnvironment.current.pipe(
134 Effect.flatMap(({ project }) =>
135 compute
136 .deleteRegionBackendServices({
137 project,
138 region,
139 backendService: backendName,
140 })
141 .pipe(
142 Effect.flatMap(waitRegionOp),
143 Effect.catchTag("NotFound", () => Effect.void),
144 Effect.catchTag("Conflict", () => Effect.void),
145 )
146 .pipe(
147 Effect.flatMap(() =>
148 compute.deleteHealthChecks({ project, healthCheck: healthCheckName }).pipe(
149 Effect.flatMap(waitGlobalOp),
150 Effect.catchTag("NotFound", () => Effect.void),
151 Effect.catchTag("Conflict", () => Effect.void),
152 ),
153 ),
154 ),
155 ),
156 );
157
158 test.provider(
159 "create, update, and delete a service attachment",
160 (stack) =>
161 Effect.gen(function* () {
162 const { project } = yield* GcpEnvironment.current;
163 yield* stack.destroy();
164 const producer = yield* ensureProducer();
165 expect(producer.backend.selfLink).toEqual(expect.any(String));
166
167 const created = yield* stack.deploy(
168 Effect.gen(function* () {
169 const subnet = yield* GCP.Compute.Subnetwork("ProducerSubnet", {
170 network: DEFAULT_NETWORK,
171 region,
172 ipCidrRange: "172.20.2.0/24",
173 });
174 const nat = yield* GCP.Compute.Subnetwork("NatSubnet", {
175 network: DEFAULT_NETWORK,
176 region,
177 ipCidrRange: "172.20.3.0/24",
178 purpose: "PRIVATE_SERVICE_CONNECT",
179 });
180 const rule = yield* GCP.Compute.ForwardingRule("ProducerFr", {
181 region,
182 loadBalancingScheme: "INTERNAL",
183 backendService: producer.backend.selfLink,
184 network: defaultNetworkSelfLink(project),
185 subnetwork: subnet.selfLink,
186 ipProtocol: "TCP",
187 allPorts: true,
188 });
189 const attachment = yield* GCP.Compute.ServiceAttachment("Producer", {
190 region,
191 targetService: rule.selfLink.as<string>(),
192 natSubnets: [nat.selfLink.as<string>()],
193 connectionPreference: "ACCEPT_AUTOMATIC",
194 enableProxyProtocol: false,
195 description: "psc producer",
196 labels: { env: "test" },
197 });
198 return { subnet, nat, rule, attachment };
199 }),
200 );
201
202 expect(created.attachment.serviceAttachmentName).toEqual(expect.any(String));
203 expect(created.attachment.region).toEqual(region);
204 expect(created.attachment.connectionPreference).toEqual("ACCEPT_AUTOMATIC");
205 expect(created.attachment.enableProxyProtocol).toEqual(false);
206 expect(created.attachment.description).toEqual("psc producer");
207 expect(created.attachment.labels).toMatchObject({ env: "test" });
208 expect(created.attachment.selfLink).toEqual(expect.any(String));
209 expect(created.attachment.targetService).toEqual(
210 expect.stringContaining(created.rule.forwardingRuleName),
211 );
212 expect(created.attachment.natSubnets.length).toBeGreaterThan(0);
213
214 const fetched = yield* compute.getServiceAttachments({
215 project: created.attachment.project,
216 region: created.attachment.region,
217 serviceAttachment: created.attachment.serviceAttachmentName,
218 });
219 expect(fetched.name).toEqual(created.attachment.serviceAttachmentName);
220 expect(fetched.connectionPreference).toEqual("ACCEPT_AUTOMATIC");
221 expect(fetched.enableProxyProtocol).toEqual(false);
222 expect(fetched.description).toContain("alchemy-id=");
223 expect(fetched.description).toContain("env=test");
224 expect(fetched.description).toContain("psc producer");
225 expect(fetched.targetService).toEqual(
226 expect.stringContaining(created.rule.forwardingRuleName),
227 );
228
229 const updated = yield* stack.deploy(
230 Effect.gen(function* () {
231 const subnet = yield* GCP.Compute.Subnetwork("ProducerSubnet", {
232 subnetworkName: created.subnet.subnetworkName,
233 network: DEFAULT_NETWORK,
234 region,
235 ipCidrRange: "172.20.2.0/24",
236 });
237 const nat = yield* GCP.Compute.Subnetwork("NatSubnet", {
238 subnetworkName: created.nat.subnetworkName,
239 network: DEFAULT_NETWORK,
240 region,
241 ipCidrRange: "172.20.3.0/24",
242 purpose: "PRIVATE_SERVICE_CONNECT",
243 });
244 const rule = yield* GCP.Compute.ForwardingRule("ProducerFr", {
245 forwardingRuleName: created.rule.forwardingRuleName,
246 region,
247 loadBalancingScheme: "INTERNAL",
248 backendService: producer.backend.selfLink,
249 network: defaultNetworkSelfLink(project),
250 subnetwork: subnet.selfLink,
251 ipProtocol: "TCP",
252 allPorts: true,
253 });
254 return yield* GCP.Compute.ServiceAttachment("Producer", {
255 serviceAttachmentName: created.attachment.serviceAttachmentName,
256 region,
257 targetService: rule.selfLink.as<string>(),
258 natSubnets: [nat.selfLink.as<string>()],
259 connectionPreference: "ACCEPT_MANUAL",
260 enableProxyProtocol: false,
261 consumerAcceptLists: [{ projectIdOrNum: project, connectionLimit: 10 }],
262 reconcileConnections: true,
263 description: "psc producer updated",
264 labels: { env: "prod", role: "psc" },
265 });
266 }),
267 );
268
269 expect(updated.serviceAttachmentName).toEqual(created.attachment.serviceAttachmentName);
270 expect(updated.connectionPreference).toEqual("ACCEPT_MANUAL");
271 expect(updated.description).toEqual("psc producer updated");
272 expect(updated.labels).toMatchObject({ env: "prod", role: "psc" });
273 expect(updated.reconcileConnections).toEqual(true);
274 expect(
275 updated.consumerAcceptLists.some(
276 (item) => item.projectIdOrNum === project && item.connectionLimit === 10,
277 ),
278 ).toEqual(true);
279
280 const fetchedUpdated = yield* compute.getServiceAttachments({
281 project: updated.project,
282 region: updated.region,
283 serviceAttachment: updated.serviceAttachmentName,
284 });
285 expect(fetchedUpdated.connectionPreference).toEqual("ACCEPT_MANUAL");
286 expect(fetchedUpdated.description).toContain("psc producer updated");
287 expect(fetchedUpdated.description).toContain("env=prod");
288 expect(
289 fetchedUpdated.consumerAcceptLists?.some(
290 (item) => item.projectIdOrNum === project && item.connectionLimit === 10,
291 ),
292 ).toEqual(true);
293
294 yield* stack.destroy();
295
296 const gone = yield* waitUntilGone(
297 created.attachment.serviceAttachmentName,
298 created.attachment.project,
299 );
300 expect(gone).toEqual("gone");
301 }).pipe(logLevel, Effect.ensuring(deleteProducer().pipe(Effect.ignore))),
302 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 360_000 },
303 );