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

main
ts 106 lines 4.09 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 import { DEFAULT_NETWORK, defaultNetworkSelfLink } from "../networkQuota.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Creating a regional endpoint fails on the testing project with an LRO
16 // error `{ code: 13, message: "an internal error has occurred" }` for every
17 // target (storage.us-central1.p.rep / storage.us-central1.rep) and access
18 // type (REGIONAL / GLOBAL), reproduced with gcloud and the REST API as well.
19 // Set GCP_TEST_REGIONAL_ENDPOINTS=1 on a project where creation works.
20 const runLifecycle = !process.env.FAST && process.env.GCP_TEST_REGIONAL_ENDPOINTS === "1";
21
22 const waitUntilGone = (name: string) =>
23 networkconnectivity.getProjectsLocationsRegionalEndpoints({ name }).pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("2 seconds"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33 test.provider(
34 "getProjectsLocationsRegionalEndpoints on a missing endpoint fails with a typed tag",
35 (stack) =>
36 Effect.gen(function* () {
37 const { project } = yield* GcpEnvironment.current;
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 networkconnectivity.getProjectsLocationsRegionalEndpoints({
42 name: `projects/${project}/locations/us-central1/regionalEndpoints/alchemy-missing`,
43 }),
44 );
45 expect(error._tag).toEqual("NotFound");
46
47 yield* stack.destroy();
48 }).pipe(logLevel),
49 {
50 tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"],
51 timeout: 90_000,
52 },
53 );
54
55 test.provider.skipIf(!runLifecycle)(
56 "create and delete a regional endpoint",
57 (stack) =>
58 Effect.gen(function* () {
59 const { project } = yield* GcpEnvironment.current;
60 yield* stack.destroy();
61
62 const created = yield* stack.deploy(
63 Effect.gen(function* () {
64 const subnet = yield* GCP.Compute.Subnetwork("RepSubnet", {
65 network: DEFAULT_NETWORK,
66 ipCidrRange: "172.20.11.0/24",
67 privateIpGoogleAccess: true,
68 });
69 const endpoint = yield* GCP.NetworkConnectivity.RegionalEndpoint("Storage", {
70 targetGoogleApi: "storage.us-central1.p.rep.googleapis.com",
71 accessType: "REGIONAL",
72 network: defaultNetworkSelfLink(project),
73 subnetwork: subnet.selfLink.as<string>(),
74 description: "rep a",
75 labels: { env: "test" },
76 });
77 return { subnet, endpoint };
78 }),
79 );
80
81 expect(created.endpoint.name).toContain("/regionalEndpoints/");
82 expect(created.endpoint.location).toEqual("us-central1");
83 expect(created.endpoint.targetGoogleApi).toEqual("storage.us-central1.p.rep.googleapis.com");
84 expect(created.endpoint.accessType).toEqual("REGIONAL");
85 expect(created.endpoint.labels).toMatchObject({ env: "test" });
86
87 const fetched = yield* networkconnectivity.getProjectsLocationsRegionalEndpoints({
88 name: created.endpoint.name,
89 });
90 expect(fetched.name).toEqual(created.endpoint.name);
91 expect(fetched.targetGoogleApi).toEqual("storage.us-central1.p.rep.googleapis.com");
92 expect(fetched.labels?.env).toEqual("test");
93 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
94 true,
95 );
96
97 yield* stack.destroy();
98
99 const gone = yield* waitUntilGone(created.endpoint.name);
100 expect(gone).toEqual("gone");
101 }).pipe(logLevel),
102 {
103 tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"],
104 timeout: 600_000,
105 },
106 );