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

main
ts 163 lines 5.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
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const region = "us-central1";
15
16 const waitUntilGone = (project: string, targetTcpProxyName: string) =>
17 compute
18 .getRegionTargetTcpProxies({
19 project,
20 region,
21 targetTcpProxy: targetTcpProxyName,
22 })
23 .pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("1 second"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33 const resourceTail = (value: string | undefined): string => {
34 if (value === undefined || value.length === 0) return "";
35 const parts = value.split("/").filter((part) => part.length > 0);
36 return parts[parts.length - 1] ?? "";
37 };
38
39 test.provider(
40 "getRegionTargetTcpProxies on a missing proxy fails with NotFound",
41 (stack) =>
42 Effect.gen(function* () {
43 yield* stack.destroy();
44 const { project } = yield* GcpEnvironment.current;
45 const error = yield* Effect.flip(
46 compute.getRegionTargetTcpProxies({
47 project,
48 region,
49 targetTcpProxy: "alchemy-missing-rtcp",
50 }),
51 );
52 expect(error._tag).toBe("NotFound");
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
56 );
57
58 test.provider(
59 "create, update, and delete a regional target tcp proxy",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const check = yield* GCP.Compute.RegionHealthCheck("Probe", {
67 region,
68 description: "tcp probe",
69 type: "TCP",
70 tcpHealthCheck: { port: 80 },
71 });
72 const backend = yield* GCP.Compute.RegionBackendService("TcpA", {
73 region,
74 protocol: "TCP",
75 loadBalancingScheme: "INTERNAL_MANAGED",
76 description: "tcp backend a",
77 healthChecks: [check.selfLink.as<string>()],
78 });
79 const proxy = yield* GCP.Compute.RegionTargetTcpProxy("Proxy", {
80 region,
81 description: "tcp frontend",
82 service: backend.name,
83 proxyHeader: "NONE",
84 });
85 return { check, backend, proxy };
86 }),
87 );
88
89 expect(created.proxy.targetTcpProxyName).toEqual(expect.any(String));
90 expect(created.proxy.region).toEqual(region);
91 expect(created.proxy.description).toEqual("tcp frontend");
92 expect(
93 created.proxy.proxyHeader === "NONE" || created.proxy.proxyHeader === undefined,
94 ).toEqual(true);
95 expect(resourceTail(created.proxy.service)).toEqual(created.backend.name);
96
97 const fetched = yield* compute.getRegionTargetTcpProxies({
98 project: created.proxy.project,
99 region,
100 targetTcpProxy: created.proxy.targetTcpProxyName,
101 });
102 expect(fetched.name).toEqual(created.proxy.targetTcpProxyName);
103 expect(resourceTail(fetched.service)).toEqual(resourceTail(created.proxy.service));
104 expect(fetched.proxyHeader ?? "NONE").toEqual("NONE");
105 expect(fetched.description).toContain("[alchemy ");
106 expect(fetched.description).toContain("tcp frontend");
107
108 const updated = yield* stack.deploy(
109 Effect.gen(function* () {
110 const check = yield* GCP.Compute.RegionHealthCheck("Probe", {
111 healthCheckName: created.check.healthCheckName,
112 region,
113 description: "tcp probe",
114 type: "TCP",
115 tcpHealthCheck: { port: 80 },
116 });
117 yield* GCP.Compute.RegionBackendService("TcpA", {
118 name: created.backend.name,
119 region,
120 protocol: "TCP",
121 loadBalancingScheme: "INTERNAL_MANAGED",
122 description: "tcp backend a",
123 healthChecks: [check.selfLink.as<string>()],
124 });
125 const other = yield* GCP.Compute.RegionBackendService("TcpB", {
126 region,
127 protocol: "TCP",
128 loadBalancingScheme: "INTERNAL_MANAGED",
129 description: "tcp backend b",
130 healthChecks: [check.selfLink.as<string>()],
131 });
132 const proxy = yield* GCP.Compute.RegionTargetTcpProxy("Proxy", {
133 targetTcpProxyName: created.proxy.targetTcpProxyName,
134 region,
135 description: "tcp frontend",
136 service: other.name,
137 proxyHeader: "PROXY_V1",
138 });
139 return { other, proxy };
140 }),
141 );
142
143 expect(updated.proxy.targetTcpProxyName).toEqual(created.proxy.targetTcpProxyName);
144 expect(updated.proxy.description).toEqual("tcp frontend");
145 expect(updated.proxy.proxyHeader).toEqual("PROXY_V1");
146 expect(resourceTail(updated.proxy.service)).toEqual(updated.other.name);
147
148 const refetched = yield* compute.getRegionTargetTcpProxies({
149 project: updated.proxy.project,
150 region,
151 targetTcpProxy: updated.proxy.targetTcpProxyName,
152 });
153 expect(refetched.proxyHeader).toEqual("PROXY_V1");
154 expect(resourceTail(refetched.service)).toEqual(resourceTail(updated.proxy.service));
155 expect(resourceTail(refetched.service)).not.toEqual(resourceTail(created.proxy.service));
156
157 yield* stack.destroy();
158
159 const gone = yield* waitUntilGone(created.proxy.project, created.proxy.targetTcpProxyName);
160 expect(gone).toEqual("gone");
161 }).pipe(logLevel),
162 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 },
163 );