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

main
ts 116 lines 4.14 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
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 // Firewall endpoints (Cloud NGFW Enterprise) bill hourly and take well over 5
15 // minutes to provision; set GCP_TEST_NGFW=1 together with GCP_TEST_SLOW to run.
16 const runLifecycle =
17 !!process.env.GCP_TEST_NGFW && !!process.env.GCP_TEST_SLOW && !process.env.FAST;
18
19 const waitUntilGone = (name: string) =>
20 networksecurity.getProjectsLocationsFirewallEndpoints({ name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("2 seconds"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30 test.provider(
31 "getProjectsLocationsFirewallEndpoints on a missing endpoint fails with a typed tag",
32 (stack) =>
33 Effect.gen(function* () {
34 yield* stack.destroy();
35
36 const { project } = yield* GcpEnvironment.current;
37 const error = yield* Effect.flip(
38 networksecurity.getProjectsLocationsFirewallEndpoints({
39 name: `projects/${project}/locations/us-central1-a/firewallEndpoints/alchemy-missing`,
40 }),
41 );
42 expect(error._tag).toEqual("NotFound");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 {
47 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
48 timeout: 90_000,
49 },
50 );
51
52 test.provider.skipIf(!runLifecycle)(
53 "create, update, and delete a firewall endpoint",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 return yield* GCP.NetworkSecurity.FirewallEndpoint("Ngfw", {
61 location: "us-central1-a",
62 description: "firewall endpoint a",
63 labels: { env: "test" },
64 });
65 }),
66 );
67
68 expect(created.name).toContain("/firewallEndpoints/");
69 expect(created.firewallEndpointId).toEqual(expect.any(String));
70 expect(created.location).toEqual("us-central1-a");
71 expect(created.description).toEqual("firewall endpoint a");
72 expect(created.labels).toMatchObject({ env: "test" });
73 expect(created.createTime).toEqual(expect.any(String));
74
75 const fetched = yield* networksecurity.getProjectsLocationsFirewallEndpoints({
76 name: created.name,
77 });
78 expect(fetched.name).toEqual(created.name);
79 expect(fetched.description).toEqual("firewall endpoint a");
80 expect(fetched.labels?.env).toEqual("test");
81 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
82 true,
83 );
84
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.NetworkSecurity.FirewallEndpoint("Ngfw", {
88 firewallEndpointId: created.firewallEndpointId,
89 location: "us-central1-a",
90 description: "firewall endpoint b",
91 labels: { env: "prod", role: "ngfw" },
92 });
93 }),
94 );
95
96 expect(updated.name).toEqual(created.name);
97 expect(updated.description).toEqual("firewall endpoint b");
98 expect(updated.labels).toMatchObject({ env: "prod", role: "ngfw" });
99
100 const refetched = yield* networksecurity.getProjectsLocationsFirewallEndpoints({
101 name: created.name,
102 });
103 expect(refetched.description).toEqual("firewall endpoint b");
104 expect(refetched.labels?.env).toEqual("prod");
105 expect(refetched.labels?.role).toEqual("ngfw");
106
107 yield* stack.destroy();
108
109 const gone = yield* waitUntilGone(created.name);
110 expect(gone).toEqual("gone");
111 }).pipe(logLevel),
112 {
113 tags: ["provider:gcp", "provider:gcp:networksecurity", "live"],
114 timeout: 90_000,
115 },
116 );