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

main
ts 136 lines 4.97 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 } 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
17 const waitUntilGone = (project: string, networkAttachment: string) =>
18 compute
19 .getNetworkAttachments({
20 project,
21 region,
22 networkAttachment,
23 })
24 .pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("2 seconds"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34 test.provider(
35 "getNetworkAttachments on a missing attachment fails with NotFound",
36 (stack) =>
37 Effect.gen(function* () {
38 yield* stack.destroy();
39 const { project } = yield* GcpEnvironment.current;
40 const error = yield* Effect.flip(
41 compute.getNetworkAttachments({
42 project,
43 region,
44 networkAttachment: "alchemy-missing-na",
45 }),
46 );
47 expect(error._tag).toBe("NotFound");
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
51 );
52
53 test.provider(
54 "create, update, and delete a network attachment",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 const subnet = yield* GCP.Compute.Subnetwork("ConsumerSubnet", {
62 network: DEFAULT_NETWORK,
63 region,
64 ipCidrRange: "172.20.0.0/24",
65 });
66 const attachment = yield* GCP.Compute.NetworkAttachment("ConsumerAttachment", {
67 region,
68 subnetworks: [subnet.selfLink.as<string>()],
69 connectionPreference: "ACCEPT_AUTOMATIC",
70 description: "psc consumer",
71 });
72 return { subnet, attachment };
73 }),
74 );
75
76 expect(created.attachment.networkAttachmentName.length).toBeGreaterThan(0);
77 expect(created.attachment.region).toEqual(region);
78 expect(created.attachment.connectionPreference).toEqual("ACCEPT_AUTOMATIC");
79 expect(created.attachment.description).toEqual("psc consumer");
80 expect(created.attachment.subnetworks.length).toBeGreaterThan(0);
81 expect(created.attachment.selfLink).toContain("/networkAttachments/");
82
83 const fetched = yield* compute.getNetworkAttachments({
84 project: created.attachment.project,
85 region,
86 networkAttachment: created.attachment.networkAttachmentName,
87 });
88 expect(fetched.name).toEqual(created.attachment.networkAttachmentName);
89 expect(fetched.connectionPreference).toEqual("ACCEPT_AUTOMATIC");
90 expect(fetched.description).toContain("[alchemy ");
91 expect(fetched.description).toContain("psc consumer");
92
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 const subnet = yield* GCP.Compute.Subnetwork("ConsumerSubnet", {
96 subnetworkName: created.subnet.subnetworkName,
97 network: DEFAULT_NETWORK,
98 region,
99 ipCidrRange: "172.20.0.0/24",
100 });
101 return yield* GCP.Compute.NetworkAttachment("ConsumerAttachment", {
102 networkAttachmentName: created.attachment.networkAttachmentName,
103 region,
104 subnetworks: [subnet.selfLink.as<string>()],
105 connectionPreference: "ACCEPT_MANUAL",
106 producerAcceptLists: [created.attachment.project],
107 description: "psc consumer updated",
108 });
109 }),
110 );
111
112 expect(updated.networkAttachmentName).toEqual(created.attachment.networkAttachmentName);
113 expect(updated.connectionPreference).toEqual("ACCEPT_MANUAL");
114 expect(updated.description).toEqual("psc consumer updated");
115 expect(updated.producerAcceptLists).toEqual(
116 expect.arrayContaining([created.attachment.project]),
117 );
118
119 const fetchedUpdated = yield* compute.getNetworkAttachments({
120 project: updated.project,
121 region,
122 networkAttachment: updated.networkAttachmentName,
123 });
124 expect(fetchedUpdated.connectionPreference).toEqual("ACCEPT_MANUAL");
125 expect(fetchedUpdated.description).toContain("psc consumer updated");
126
127 yield* stack.destroy();
128
129 const gone = yield* waitUntilGone(
130 created.attachment.project,
131 created.attachment.networkAttachmentName,
132 );
133 expect(gone).toEqual("gone");
134 }).pipe(logLevel),
135 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 },
136 );