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

main
ts 167 lines 5.84 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.getProjectsLocationsTlsRoutes({ 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 "getProjectsLocationsTlsRoutes 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.getProjectsLocationsTlsRoutes({
34 name: `projects/${project}/locations/global/tlsRoutes/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 tls 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: "tls mesh",
58 labels: { env: "test" },
59 });
60 const backend = yield* GCP.Compute.BackendService("Backend", {
61 loadBalancingScheme: "INTERNAL_SELF_MANAGED",
62 protocol: "HTTP",
63 description: "tls backend",
64 });
65 const route = yield* GCP.NetworkServices.TlsRoute("Secure", {
66 location: "global",
67 description: "tls route a",
68 labels: { env: "test" },
69 meshes: [mesh.name],
70 rules: [
71 {
72 matches: [{ sniHost: ["api.example.com"], alpn: ["h2"] }],
73 action: {
74 destinations: [
75 {
76 serviceName: backend.selfLink ?? backend.name,
77 weight: 1,
78 },
79 ],
80 },
81 },
82 ],
83 });
84 return { mesh, backend, route };
85 }),
86 );
87
88 expect(created.route.name).toContain("/tlsRoutes/");
89 expect(created.route.tlsRouteId).toEqual(expect.any(String));
90 expect(created.route.location).toEqual("global");
91 expect(created.route.description).toEqual("tls route a");
92 expect(created.route.meshes).toContain(created.mesh.name);
93 expect(created.route.rules[0]?.matches?.[0]?.sniHost).toContain("api.example.com");
94 expect(created.route.labels).toMatchObject({ env: "test" });
95 expect(created.route.createTime).toEqual(expect.any(String));
96
97 const fetched = yield* networkservices.getProjectsLocationsTlsRoutes({
98 name: created.route.name,
99 });
100 expect(fetched.name).toEqual(created.route.name);
101 expect(fetched.description).toEqual("tls route a");
102 expect(fetched.labels?.env).toEqual("test");
103 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
104 true,
105 );
106
107 const updated = yield* stack.deploy(
108 Effect.gen(function* () {
109 const mesh = yield* GCP.NetworkServices.Mesh("Sidecar", {
110 meshId: created.mesh.meshId,
111 location: "global",
112 description: "tls mesh",
113 labels: { env: "test" },
114 });
115 const backend = yield* GCP.Compute.BackendService("Backend", {
116 name: created.backend.name,
117 loadBalancingScheme: "INTERNAL_SELF_MANAGED",
118 protocol: "HTTP",
119 description: "tls backend",
120 });
121 const route = yield* GCP.NetworkServices.TlsRoute("Secure", {
122 tlsRouteId: created.route.tlsRouteId,
123 location: "global",
124 description: "tls route b",
125 labels: { env: "prod", role: "tls" },
126 meshes: [mesh.name],
127 rules: [
128 {
129 matches: [{ sniHost: ["secure.example.com"], alpn: ["h2"] }],
130 action: {
131 destinations: [
132 {
133 serviceName: backend.selfLink ?? backend.name,
134 weight: 1,
135 },
136 ],
137 idleTimeout: "60s",
138 },
139 },
140 ],
141 });
142 return { mesh, backend, route };
143 }),
144 );
145
146 expect(updated.route.name).toEqual(created.route.name);
147 expect(updated.route.description).toEqual("tls route b");
148 expect(updated.route.labels).toMatchObject({ env: "prod", role: "tls" });
149 expect(updated.route.rules[0]?.matches?.[0]?.sniHost).toContain("secure.example.com");
150
151 const refetched = yield* networkservices.getProjectsLocationsTlsRoutes({
152 name: created.route.name,
153 });
154 expect(refetched.description).toEqual("tls route b");
155 expect(refetched.labels?.env).toEqual("prod");
156 expect(refetched.labels?.role).toEqual("tls");
157
158 yield* stack.destroy();
159
160 const gone = yield* waitUntilGone(created.route.name);
161 expect(gone).toEqual("gone");
162 }).pipe(logLevel),
163 {
164 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
165 timeout: 180_000,
166 },
167 );