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

main
ts 122 lines 4.47 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.getProjectsLocationsServiceLbPolicies({ 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 "getProjectsLocationsServiceLbPolicies on a missing policy 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.getProjectsLocationsServiceLbPolicies({
34 name: `projects/${project}/locations/global/serviceLbPolicies/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 service lb policy",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const created = yield* stack.deploy(
54 Effect.gen(function* () {
55 return yield* GCP.NetworkServices.ServiceLbPolicy("Spread", {
56 location: "global",
57 description: "lb policy a",
58 labels: { env: "test" },
59 loadBalancingAlgorithm: "SPRAY_TO_REGION",
60 autoCapacityDrain: { enable: true },
61 });
62 }),
63 );
64
65 expect(created.name).toContain("/serviceLbPolicies/");
66 expect(created.serviceLbPolicyId).toEqual(expect.any(String));
67 expect(created.location).toEqual("global");
68 expect(created.description).toEqual("lb policy a");
69 expect(created.loadBalancingAlgorithm).toEqual("SPRAY_TO_REGION");
70 expect(created.autoCapacityDrain?.enable).toEqual(true);
71 expect(created.labels).toMatchObject({ env: "test" });
72 expect(created.createTime).toEqual(expect.any(String));
73
74 const fetched = yield* networkservices.getProjectsLocationsServiceLbPolicies({
75 name: created.name,
76 });
77 expect(fetched.name).toEqual(created.name);
78 expect(fetched.description).toEqual("lb policy a");
79 expect(fetched.loadBalancingAlgorithm).toEqual("SPRAY_TO_REGION");
80 expect(fetched.labels?.env).toEqual("test");
81 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
82 true,
83 );
84
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.NetworkServices.ServiceLbPolicy("Spread", {
88 serviceLbPolicyId: created.serviceLbPolicyId,
89 location: "global",
90 description: "lb policy b",
91 labels: { env: "prod", role: "lb" },
92 loadBalancingAlgorithm: "WATERFALL_BY_REGION",
93 autoCapacityDrain: { enable: false },
94 failoverConfig: { failoverHealthThreshold: 70 },
95 });
96 }),
97 );
98
99 expect(updated.name).toEqual(created.name);
100 expect(updated.description).toEqual("lb policy b");
101 expect(updated.loadBalancingAlgorithm).toEqual("WATERFALL_BY_REGION");
102 expect(updated.failoverConfig?.failoverHealthThreshold).toEqual(70);
103 expect(updated.labels).toMatchObject({ env: "prod", role: "lb" });
104
105 const refetched = yield* networkservices.getProjectsLocationsServiceLbPolicies({
106 name: created.name,
107 });
108 expect(refetched.description).toEqual("lb policy b");
109 expect(refetched.loadBalancingAlgorithm).toEqual("WATERFALL_BY_REGION");
110 expect(refetched.labels?.env).toEqual("prod");
111 expect(refetched.labels?.role).toEqual("lb");
112
113 yield* stack.destroy();
114
115 const gone = yield* waitUntilGone(created.name);
116 expect(gone).toEqual("gone");
117 }).pipe(logLevel),
118 {
119 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
120 timeout: 90_000,
121 },
122 );