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

main
ts 131 lines 5.09 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 waitUntilGone = (targetTcpProxyName: string) =>
15 GcpEnvironment.current.pipe(
16 Effect.flatMap(({ project }) =>
17 compute.getTargetTcpProxies({ project, targetTcpProxy: targetTcpProxyName }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 ),
26 ),
27 );
28
29 const resourceTail = (value: string | undefined): string => {
30 if (value === undefined || value.length === 0) return "";
31 const parts = value.split("/").filter((part) => part.length > 0);
32 return parts[parts.length - 1] ?? "";
33 };
34
35 test.provider(
36 "create, update, and delete a target tcp proxy",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40 yield* stack.destroy();
41
42 const created = yield* stack.deploy(
43 Effect.gen(function* () {
44 const check = yield* GCP.Compute.HealthCheck("Probe", {
45 description: "tcp probe",
46 type: "TCP",
47 tcpHealthCheck: { port: 80 },
48 });
49 const backend = yield* GCP.Compute.BackendService("TcpA", {
50 protocol: "TCP",
51 loadBalancingScheme: "EXTERNAL",
52 description: "tcp backend a",
53 healthChecks: [check.selfLink.as<string>()],
54 });
55 const proxy = yield* GCP.Compute.TargetTcpProxy("Proxy", {
56 description: "tcp frontend",
57 service: backend.name,
58 proxyHeader: "NONE",
59 });
60 return { check, backend, proxy };
61 }),
62 );
63
64 expect(created.proxy.targetTcpProxyName).toEqual(expect.any(String));
65 expect(created.proxy.description).toEqual("tcp frontend");
66 expect(
67 created.proxy.proxyHeader === "NONE" || created.proxy.proxyHeader === undefined,
68 ).toEqual(true);
69 expect(resourceTail(created.proxy.service)).toEqual(created.backend.name);
70
71 const fetched = yield* compute.getTargetTcpProxies({
72 project,
73 targetTcpProxy: created.proxy.targetTcpProxyName,
74 });
75 expect(fetched.name).toEqual(created.proxy.targetTcpProxyName);
76 expect(resourceTail(fetched.service)).toEqual(resourceTail(created.proxy.service));
77 expect(fetched.proxyHeader ?? "NONE").toEqual("NONE");
78 expect(fetched.description).toContain("[alchemy ");
79 expect(fetched.description).toContain("tcp frontend");
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 const check = yield* GCP.Compute.HealthCheck("Probe", {
84 healthCheckName: created.check.healthCheckName,
85 description: "tcp probe",
86 type: "TCP",
87 tcpHealthCheck: { port: 80 },
88 });
89 yield* GCP.Compute.BackendService("TcpA", {
90 name: created.backend.name,
91 protocol: "TCP",
92 loadBalancingScheme: "EXTERNAL",
93 description: "tcp backend a",
94 healthChecks: [check.selfLink.as<string>()],
95 });
96 const other = yield* GCP.Compute.BackendService("TcpB", {
97 protocol: "TCP",
98 loadBalancingScheme: "EXTERNAL",
99 description: "tcp backend b",
100 healthChecks: [check.selfLink.as<string>()],
101 });
102 const proxy = yield* GCP.Compute.TargetTcpProxy("Proxy", {
103 targetTcpProxyName: created.proxy.targetTcpProxyName,
104 description: "tcp frontend",
105 service: other.name,
106 proxyHeader: "PROXY_V1",
107 });
108 return { other, proxy };
109 }),
110 );
111
112 expect(updated.proxy.targetTcpProxyName).toEqual(created.proxy.targetTcpProxyName);
113 expect(updated.proxy.description).toEqual("tcp frontend");
114 expect(updated.proxy.proxyHeader).toEqual("PROXY_V1");
115 expect(resourceTail(updated.proxy.service)).toEqual(updated.other.name);
116
117 const refetched = yield* compute.getTargetTcpProxies({
118 project,
119 targetTcpProxy: updated.proxy.targetTcpProxyName,
120 });
121 expect(refetched.proxyHeader).toEqual("PROXY_V1");
122 expect(resourceTail(refetched.service)).toEqual(resourceTail(updated.proxy.service));
123 expect(resourceTail(refetched.service)).not.toEqual(resourceTail(created.proxy.service));
124
125 yield* stack.destroy();
126
127 const gone = yield* waitUntilGone(created.proxy.targetTcpProxyName);
128 expect(gone).toEqual("gone");
129 }).pipe(logLevel),
130 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 },
131 );