@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 143 lines 5.44 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 networksecurity from "@distilled.cloud/gcp/networksecurity_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { withNetworkSlot } 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 // Firewall endpoints (Cloud NGFW Enterprise) bill hourly and take well over 5
16 // minutes to provision; set GCP_TEST_NGFW=1 together with GCP_TEST_SLOW to run.
17 const runLifecycle =
18 !!process.env.GCP_TEST_NGFW && !!process.env.GCP_TEST_SLOW && !process.env.FAST;
19
20 const waitUntilGone = (name: string) =>
21 networksecurity.getProjectsLocationsFirewallEndpointAssociations({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31 test.provider(
32 "getProjectsLocationsFirewallEndpointAssociations on a missing association fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 yield* stack.destroy();
36
37 const { project } = yield* GcpEnvironment.current;
38 const error = yield* Effect.flip(
39 networksecurity.getProjectsLocationsFirewallEndpointAssociations({
40 name: `projects/${project}/locations/us-central1-a/firewallEndpointAssociations/alchemy-missing`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 {
48 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
49 timeout: 90_000,
50 },
51 );
52
53 test.provider.skipIf(!runLifecycle)(
54 "create, update, and delete a firewall endpoint association",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 const vpc = yield* GCP.Compute.Network("Vpc", {
62 autoCreateSubnetworks: false,
63 description: "association vpc",
64 });
65 const endpoint = yield* GCP.NetworkSecurity.FirewallEndpoint("Ngfw", {
66 location: "us-central1-a",
67 description: "association endpoint",
68 labels: { env: "test" },
69 });
70 const association = yield* GCP.NetworkSecurity.FirewallEndpointAssociation("Inspect", {
71 location: "us-central1-a",
72 firewallEndpoint: endpoint.name,
73 network: vpc.selfLink.as<string>(),
74 labels: { env: "test" },
75 });
76 return { vpc, endpoint, association };
77 }),
78 );
79
80 expect(created.association.name).toContain("/firewallEndpointAssociations/");
81 expect(created.association.location).toEqual("us-central1-a");
82 expect(created.association.disabled).toEqual(false);
83 expect(created.association.labels).toMatchObject({ env: "test" });
84 expect(created.association.firewallEndpoint).toEqual(created.endpoint.name);
85
86 const fetched = yield* networksecurity.getProjectsLocationsFirewallEndpointAssociations({
87 name: created.association.name,
88 });
89 expect(fetched.name).toEqual(created.association.name);
90 expect(fetched.labels?.env).toEqual("test");
91 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
92 true,
93 );
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 const vpc = yield* GCP.Compute.Network("Vpc", {
98 networkName: created.vpc.networkName,
99 autoCreateSubnetworks: false,
100 description: "association vpc",
101 });
102 const endpoint = yield* GCP.NetworkSecurity.FirewallEndpoint("Ngfw", {
103 firewallEndpointId: created.endpoint.firewallEndpointId,
104 location: "us-central1-a",
105 description: "association endpoint",
106 labels: { env: "test" },
107 });
108 const association = yield* GCP.NetworkSecurity.FirewallEndpointAssociation("Inspect", {
109 firewallEndpointAssociationId: created.association.firewallEndpointAssociationId,
110 location: "us-central1-a",
111 firewallEndpoint: endpoint.name,
112 network: vpc.selfLink.as<string>(),
113 disabled: true,
114 labels: { env: "prod", role: "ngfw" },
115 });
116 return { vpc, endpoint, association };
117 }),
118 );
119
120 expect(updated.association.name).toEqual(created.association.name);
121 expect(updated.association.disabled).toEqual(true);
122 expect(updated.association.labels).toMatchObject({
123 env: "prod",
124 role: "ngfw",
125 });
126
127 const refetched = yield* networksecurity.getProjectsLocationsFirewallEndpointAssociations({
128 name: created.association.name,
129 });
130 expect(refetched.disabled).toEqual(true);
131 expect(refetched.labels?.env).toEqual("prod");
132 expect(refetched.labels?.role).toEqual("ngfw");
133
134 yield* stack.destroy();
135
136 const gone = yield* waitUntilGone(created.association.name);
137 expect(gone).toEqual("gone");
138 }).pipe(logLevel, withNetworkSlot),
139 {
140 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
141 timeout: 90_000,
142 },
143 );