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

main
ts 141 lines 4.93 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 networkservices from "@distilled.cloud/gcp/networkservices_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 = (name: string) =>
15 networkservices.getProjectsLocationsTcpRoutes({ name }).pipe(
16 Effect.as("found" as const),
17 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
18 Effect.repeat({
19 schedule: Schedule.spaced("1 second"),
20 until: (status) => status === "gone",
21 times: 10,
22 }),
23 );
24
25 test.provider(
26 "getProjectsLocationsTcpRoutes on a missing route fails with a typed tag",
27 (stack) =>
28 Effect.gen(function* () {
29 yield* stack.destroy();
30
31 const { project } = yield* GcpEnvironment.current;
32 const error = yield* Effect.flip(
33 networkservices.getProjectsLocationsTcpRoutes({
34 name: `projects/${project}/locations/global/tcpRoutes/alchemy-missing`,
35 }),
36 );
37 expect(error._tag).toEqual("NotFound");
38
39 yield* stack.destroy();
40 }).pipe(logLevel),
41 {
42 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
43 timeout: 90_000,
44 },
45 );
46
47 test.provider(
48 "create, update, and delete a tcp route",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const created = yield* stack.deploy(
54 Effect.gen(function* () {
55 const mesh = yield* GCP.NetworkServices.Mesh("Sidecar", {
56 location: "global",
57 description: "tcp mesh",
58 labels: { env: "test" },
59 });
60 const route = yield* GCP.NetworkServices.TcpRoute("Passthrough", {
61 location: "global",
62 description: "tcp route a",
63 labels: { env: "test" },
64 meshes: [mesh.name],
65 rules: [
66 {
67 matches: [{ address: "0.0.0.0/0", port: "443" }],
68 action: { originalDestination: true },
69 },
70 ],
71 });
72 return { mesh, route };
73 }),
74 );
75
76 expect(created.route.name).toContain("/tcpRoutes/");
77 expect(created.route.tcpRouteId).toEqual(expect.any(String));
78 expect(created.route.location).toEqual("global");
79 expect(created.route.description).toEqual("tcp route a");
80 expect(created.route.meshes).toContain(created.mesh.name);
81 expect(created.route.rules[0]?.action?.originalDestination).toEqual(true);
82 expect(created.route.labels).toMatchObject({ env: "test" });
83 expect(created.route.createTime).toEqual(expect.any(String));
84
85 const fetched = yield* networkservices.getProjectsLocationsTcpRoutes({
86 name: created.route.name,
87 });
88 expect(fetched.name).toEqual(created.route.name);
89 expect(fetched.description).toEqual("tcp route a");
90 expect(fetched.labels?.env).toEqual("test");
91 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
92 true,
93 );
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 const mesh = yield* GCP.NetworkServices.Mesh("Sidecar", {
98 meshId: created.mesh.meshId,
99 location: "global",
100 description: "tcp mesh",
101 labels: { env: "test" },
102 });
103 const route = yield* GCP.NetworkServices.TcpRoute("Passthrough", {
104 tcpRouteId: created.route.tcpRouteId,
105 location: "global",
106 description: "tcp route b",
107 labels: { env: "prod", role: "tcp" },
108 meshes: [mesh.name],
109 rules: [
110 {
111 matches: [{ address: "0.0.0.0/0", port: "80" }],
112 action: { originalDestination: true, idleTimeout: "60s" },
113 },
114 ],
115 });
116 return { mesh, route };
117 }),
118 );
119
120 expect(updated.route.name).toEqual(created.route.name);
121 expect(updated.route.description).toEqual("tcp route b");
122 expect(updated.route.labels).toMatchObject({ env: "prod", role: "tcp" });
123 expect(updated.route.rules[0]?.matches?.[0]?.port).toEqual("80");
124
125 const refetched = yield* networkservices.getProjectsLocationsTcpRoutes({
126 name: created.route.name,
127 });
128 expect(refetched.description).toEqual("tcp route b");
129 expect(refetched.labels?.env).toEqual("prod");
130 expect(refetched.labels?.role).toEqual("tcp");
131
132 yield* stack.destroy();
133
134 const gone = yield* waitUntilGone(created.route.name);
135 expect(gone).toEqual("gone");
136 }).pipe(logLevel),
137 {
138 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
139 timeout: 90_000,
140 },
141 );