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

main
ts 110 lines 3.91 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 networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_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 // A service connection map publishes a producer service class, which must be
15 // registered for the project first (BadRequest "serviceClass gcp-cloud-sql
16 // does not exist: invalid argument"). Set GCP_TEST_SERVICE_CONNECTION_MAP=1 on
17 // a producer project with the class registered.
18 const runLifecycle = !!process.env.GCP_TEST_SERVICE_CONNECTION_MAP;
19
20 const waitUntilGone = (name: string) =>
21 networkconnectivity.getProjectsLocationsServiceConnectionMaps({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31 test.provider(
32 "getProjectsLocationsServiceConnectionMaps on a missing map fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 networkconnectivity.getProjectsLocationsServiceConnectionMaps({
40 name: `projects/${project}/locations/us-central1/serviceConnectionMaps/alchemy-missing`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 {
48 tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"],
49 timeout: 90_000,
50 },
51 );
52
53 test.provider.skipIf(!runLifecycle)(
54 "create, update, and delete a service connection map",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* GCP.NetworkConnectivity.ServiceConnectionMap("Sql", {
62 serviceClass: "gcp-cloud-sql",
63 description: "map a",
64 labels: { env: "test" },
65 });
66 }),
67 );
68
69 expect(created.name).toContain("/serviceConnectionMaps/");
70 expect(created.location).toEqual("us-central1");
71 expect(created.serviceClass).toEqual("gcp-cloud-sql");
72 expect(created.description).toEqual("map a");
73 expect(created.labels).toMatchObject({ env: "test" });
74
75 const fetched = yield* networkconnectivity.getProjectsLocationsServiceConnectionMaps({
76 name: created.name,
77 });
78 expect(fetched.name).toEqual(created.name);
79 expect(fetched.serviceClass).toEqual("gcp-cloud-sql");
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.NetworkConnectivity.ServiceConnectionMap("Sql", {
88 serviceConnectionMapId: created.serviceConnectionMapId,
89 location: created.location,
90 serviceClass: "gcp-cloud-sql",
91 description: "map b",
92 labels: { env: "prod", role: "psc" },
93 });
94 }),
95 );
96
97 expect(updated.name).toEqual(created.name);
98 expect(updated.description).toEqual("map b");
99 expect(updated.labels).toMatchObject({ env: "prod", role: "psc" });
100
101 yield* stack.destroy();
102
103 const gone = yield* waitUntilGone(created.name);
104 expect(gone).toEqual("gone");
105 }).pipe(logLevel),
106 {
107 tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"],
108 timeout: 180_000,
109 },
110 );