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

main
ts 148 lines 5.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 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 import { withNetworkSlot } 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 // Multicast needs a multicast domain activation in the project; without one
16 // create fails with BadRequest "The request was invalid: missing
17 // multicast_domain_activation". Set GCP_TEST_MULTICAST=1 on a project with
18 // multicast activated.
19 const runLifecycle = !process.env.FAST && !!process.env.GCP_TEST_MULTICAST;
20
21 const waitUntilGone = (name: string) =>
22 networkservices.getProjectsLocationsMulticastGroupConsumerActivations({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32 test.provider(
33 "getProjectsLocationsMulticastGroupConsumerActivations on a missing activation 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.getProjectsLocationsMulticastGroupConsumerActivations({
41 name: `projects/${project}/locations/us-central1-a/multicastGroupConsumerActivations/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.skipIf(!runLifecycle)(
55 "create, update, and delete a multicast group consumer activation",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const vpc = yield* GCP.Compute.Network("Vpc", {
63 autoCreateSubnetworks: false,
64 description: "multicast group vpc",
65 });
66 const association = yield* GCP.NetworkServices.MulticastConsumerAssociation("Consumers", {
67 location: "us-central1-a",
68 network: vpc.selfLink.as<string>(),
69 description: "mcast assoc",
70 labels: { env: "test" },
71 });
72 const activation = yield* GCP.NetworkServices.MulticastGroupConsumerActivation("Join", {
73 location: "us-central1-a",
74 multicastConsumerAssociation: association.name,
75 multicastGroupRangeActivation: process.env.GCP_TEST_MULTICAST_RANGE_ACTIVATION ?? "",
76 description: "mcast gca a",
77 labels: { env: "test" },
78 logConfig: { enabled: true },
79 });
80 return { vpc, association, activation };
81 }),
82 );
83
84 expect(created.activation.name).toContain("/multicastGroupConsumerActivations/");
85 expect(created.activation.multicastGroupConsumerActivationId).toEqual(expect.any(String));
86 expect(created.activation.location).toEqual("us-central1-a");
87 expect(created.activation.description).toEqual("mcast gca a");
88 expect(created.activation.labels).toMatchObject({ env: "test" });
89 expect(created.activation.logConfig?.enabled).toEqual(true);
90
91 const fetched = yield* networkservices.getProjectsLocationsMulticastGroupConsumerActivations({
92 name: created.activation.name,
93 });
94 expect(fetched.name).toEqual(created.activation.name);
95 expect(fetched.description).toEqual("mcast gca a");
96 expect(fetched.labels?.env).toEqual("test");
97 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
98 true,
99 );
100
101 const updated = yield* stack.deploy(
102 Effect.gen(function* () {
103 const vpc = yield* GCP.Compute.Network("Vpc", {
104 networkName: created.vpc.networkName,
105 autoCreateSubnetworks: false,
106 description: "multicast group vpc",
107 });
108 const association = yield* GCP.NetworkServices.MulticastConsumerAssociation("Consumers", {
109 multicastConsumerAssociationId: created.association.multicastConsumerAssociationId,
110 location: "us-central1-a",
111 network: vpc.selfLink.as<string>(),
112 description: "mcast assoc",
113 labels: { env: "test" },
114 });
115 const activation = yield* GCP.NetworkServices.MulticastGroupConsumerActivation("Join", {
116 multicastGroupConsumerActivationId:
117 created.activation.multicastGroupConsumerActivationId,
118 location: "us-central1-a",
119 multicastConsumerAssociation: association.name,
120 multicastGroupRangeActivation:
121 created.activation.multicastGroupRangeActivation ??
122 process.env.GCP_TEST_MULTICAST_RANGE_ACTIVATION ??
123 "",
124 description: "mcast gca b",
125 labels: { env: "prod", role: "mcast" },
126 logConfig: { enabled: false },
127 });
128 return { vpc, association, activation };
129 }),
130 );
131
132 expect(updated.activation.name).toEqual(created.activation.name);
133 expect(updated.activation.description).toEqual("mcast gca b");
134 expect(updated.activation.labels).toMatchObject({
135 env: "prod",
136 role: "mcast",
137 });
138
139 yield* stack.destroy();
140
141 const gone = yield* waitUntilGone(created.activation.name);
142 expect(gone).toEqual("gone");
143 }).pipe(logLevel, withNetworkSlot),
144 {
145 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
146 timeout: 90_000,
147 },
148 );