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

main
ts 125 lines 4.31 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.getProjectsLocationsEndpointPolicies({ 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 const matcher = (value: string) => ({
26 metadataLabelMatcher: {
27 metadataLabelMatchCriteria: "MATCH_ANY" as const,
28 metadataLabels: [{ labelName: "app", labelValue: value }],
29 },
30 });
31
32 test.provider(
33 "getProjectsLocationsEndpointPolicies on a missing policy fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 yield* stack.destroy();
37
38 const { project } = yield* GcpEnvironment.current;
39 const error = yield* Effect.flip(
40 networkservices.getProjectsLocationsEndpointPolicies({
41 name: `projects/${project}/locations/global/endpointPolicies/alchemy-missing`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel),
48 {
49 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
50 timeout: 90_000,
51 },
52 );
53
54 test.provider(
55 "create, update, and delete an endpoint policy",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 return yield* GCP.NetworkServices.EndpointPolicy("Sidecar", {
63 location: "global",
64 type: "SIDECAR_PROXY",
65 description: "endpoint policy a",
66 labels: { env: "test" },
67 endpointMatcher: matcher("web"),
68 trafficPortSelector: { ports: ["8080"] },
69 });
70 }),
71 );
72
73 expect(created.name).toContain("/endpointPolicies/");
74 expect(created.endpointPolicyId).toEqual(expect.any(String));
75 expect(created.location).toEqual("global");
76 expect(created.type).toEqual("SIDECAR_PROXY");
77 expect(created.description).toEqual("endpoint policy a");
78 expect(created.labels).toMatchObject({ env: "test" });
79 expect(created.createTime).toEqual(expect.any(String));
80
81 const fetched = yield* networkservices.getProjectsLocationsEndpointPolicies({
82 name: created.name,
83 });
84 expect(fetched.name).toEqual(created.name);
85 expect(fetched.description).toEqual("endpoint policy a");
86 expect(fetched.labels?.env).toEqual("test");
87 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
88 true,
89 );
90
91 const updated = yield* stack.deploy(
92 Effect.gen(function* () {
93 return yield* GCP.NetworkServices.EndpointPolicy("Sidecar", {
94 endpointPolicyId: created.endpointPolicyId,
95 location: "global",
96 type: "SIDECAR_PROXY",
97 description: "endpoint policy b",
98 labels: { env: "prod", role: "sidecar" },
99 endpointMatcher: matcher("api"),
100 trafficPortSelector: { ports: ["8081"] },
101 });
102 }),
103 );
104
105 expect(updated.name).toEqual(created.name);
106 expect(updated.description).toEqual("endpoint policy b");
107 expect(updated.labels).toMatchObject({ env: "prod", role: "sidecar" });
108
109 const refetched = yield* networkservices.getProjectsLocationsEndpointPolicies({
110 name: created.name,
111 });
112 expect(refetched.description).toEqual("endpoint policy b");
113 expect(refetched.labels?.env).toEqual("prod");
114 expect(refetched.labels?.role).toEqual("sidecar");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(created.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 {
122 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
123 timeout: 90_000,
124 },
125 );