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

main
ts 122 lines 4.59 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as compute from "@distilled.cloud/gcp/compute_v1";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const waitUntilGone = (project: string, firewall: string) =>
14 compute.getFirewalls({ project, firewall }).pipe(
15 Effect.as("found" as const),
16 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
17 Effect.repeat({
18 schedule: Schedule.spaced("1 second"),
19 until: (status) => status === "gone",
20 times: 10,
21 }),
22 );
23
24 test.provider.skipIf(!!process.env.FAST)(
25 "create, update, replace, and delete a firewall",
26 (stack) =>
27 Effect.gen(function* () {
28 yield* stack.destroy();
29
30 const created = yield* stack.deploy(
31 Effect.gen(function* () {
32 return yield* GCP.Compute.Firewall("AllowHttp", {
33 description: "test http",
34 allowed: [{ protocol: "tcp", ports: ["80"] }],
35 sourceRanges: ["10.0.0.0/8"],
36 targetTags: ["alchemy-fw"],
37 });
38 }),
39 );
40
41 expect(created.firewallName).toEqual(expect.any(String));
42 expect(created.direction).toEqual("INGRESS");
43 expect(created.disabled).toEqual(false);
44 expect(created.priority).toEqual(1000);
45 expect(created.description).toEqual("test http");
46 expect(created.allowed).toEqual(
47 expect.arrayContaining([expect.objectContaining({ protocol: "tcp", ports: ["80"] })]),
48 );
49 expect(created.sourceRanges).toEqual(["10.0.0.0/8"]);
50 expect(created.targetTags).toEqual(["alchemy-fw"]);
51 expect(created.network).toEqual(expect.stringContaining("networks/"));
52
53 const fetched = yield* compute.getFirewalls({
54 project: created.project,
55 firewall: created.firewallName,
56 });
57 expect(fetched.name).toEqual(created.firewallName);
58 expect(fetched.description).toContain("alchemy-id=");
59 expect(fetched.description).toContain("test http");
60 expect(fetched.allowed?.[0]?.IPProtocol).toEqual("tcp");
61 expect(fetched.sourceRanges).toEqual(["10.0.0.0/8"]);
62
63 const updated = yield* stack.deploy(
64 Effect.gen(function* () {
65 return yield* GCP.Compute.Firewall("AllowHttp", {
66 firewallName: created.firewallName,
67 description: "updated http",
68 allowed: [{ protocol: "tcp", ports: ["80", "443"] }],
69 sourceRanges: ["10.0.0.0/8"],
70 targetTags: ["alchemy-fw", "web"],
71 priority: 800,
72 disabled: true,
73 });
74 }),
75 );
76
77 expect(updated.firewallName).toEqual(created.firewallName);
78 expect(updated.id).toEqual(created.id);
79 expect(updated.description).toEqual("updated http");
80 expect(updated.priority).toEqual(800);
81 expect(updated.disabled).toEqual(true);
82 expect(updated.targetTags.sort()).toEqual(["alchemy-fw", "web"].sort());
83 expect(updated.allowed[0]?.ports?.sort()).toEqual(["443", "80"]);
84
85 const fetchedUpdate = yield* compute.getFirewalls({
86 project: updated.project,
87 firewall: updated.firewallName,
88 });
89 expect(fetchedUpdate.priority).toEqual(800);
90 expect(fetchedUpdate.disabled).toEqual(true);
91
92 const replaced = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.Compute.Firewall("AllowHttp", {
95 firewallName: created.firewallName,
96 description: "egress https",
97 direction: "EGRESS",
98 allowed: [{ protocol: "tcp", ports: ["443"] }],
99 destinationRanges: ["10.0.0.0/8"],
100 targetTags: ["alchemy-fw"],
101 });
102 }),
103 );
104
105 expect(replaced.firewallName).toEqual(created.firewallName);
106 expect(replaced.direction).toEqual("EGRESS");
107 expect(replaced.destinationRanges).toEqual(["10.0.0.0/8"]);
108 expect(replaced.id).not.toEqual(created.id);
109
110 const fetchedReplace = yield* compute.getFirewalls({
111 project: replaced.project,
112 firewall: replaced.firewallName,
113 });
114 expect((fetchedReplace.direction ?? "").toUpperCase()).toEqual("EGRESS");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(created.project, created.firewallName);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 },
122 );