1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as networkservices from "@distilled.cloud/gcp/networkservices_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 networkservices.getProjectsLocationsAgentGateways({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("2 seconds"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsAgentGateways on a missing gateway fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 yield* stack.destroy();
33
34 const { project } = yield* GcpEnvironment.current;
35 const error = yield* Effect.flip(
36 networkservices.getProjectsLocationsAgentGateways({
37 name: `projects/${project}/locations/us-central1/agentGateways/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
46 timeout: 90_000,
47 },
48);
49
50// Agent gateway create + update + delete takes ~7 minutes.
51test.provider.skipIf(!process.env.GCP_TEST_SLOW || !!process.env.FAST)(
52 "create, update, and delete an agent gateway",
53 (stack) =>
54 Effect.gen(function* () {
55 yield* stack.destroy();
56
57 const created = yield* stack.deploy(
58 Effect.gen(function* () {
59 return yield* GCP.NetworkServices.AgentGateway("Agents", {
60 location: "us-central1",
61 description: "agent gateway a",
62 labels: { env: "test" },
63 googleManaged: { governedAccessPath: "AGENT_TO_ANYWHERE" },
64 });
65 }),
66 );
67
68 expect(created.name).toContain("/agentGateways/");
69 expect(created.agentGatewayId).toEqual(expect.any(String));
70 expect(created.location).toEqual("us-central1");
71 expect(created.description).toEqual("agent gateway a");
72 expect(created.labels).toMatchObject({ env: "test" });
73 expect(created.googleManaged?.governedAccessPath).toEqual(
74 "AGENT_TO_ANYWHERE",
75 );
76 expect(created.createTime).toEqual(expect.any(String));
77
78 const fetched = yield* networkservices.getProjectsLocationsAgentGateways({
79 name: created.name,
80 });
81 expect(fetched.name).toEqual(created.name);
82 expect(fetched.description).toEqual("agent gateway a");
83 expect(fetched.labels?.env).toEqual("test");
84 expect(
85 Object.keys(fetched.labels ?? {}).some((key) =>
86 key.startsWith("alchemy-"),
87 ),
88 ).toEqual(true);
89
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* GCP.NetworkServices.AgentGateway("Agents", {
93 agentGatewayId: created.agentGatewayId,
94 location: "us-central1",
95 description: "agent gateway b",
96 labels: { env: "prod", role: "agents" },
97 googleManaged: { governedAccessPath: "AGENT_TO_ANYWHERE" },
98 });
99 }),
100 );
101
102 expect(updated.name).toEqual(created.name);
103 expect(updated.description).toEqual("agent gateway b");
104 expect(updated.labels).toMatchObject({ env: "prod", role: "agents" });
105
106 const refetched =
107 yield* networkservices.getProjectsLocationsAgentGateways({
108 name: created.name,
109 });
110 expect(refetched.description).toEqual("agent gateway b");
111 expect(refetched.labels?.env).toEqual("prod");
112 expect(refetched.labels?.role).toEqual("agents");
113
114 yield* stack.destroy();
115
116 const gone = yield* waitUntilGone(created.name);
117 expect(gone).toEqual("gone");
118 }).pipe(logLevel),
119 {
120 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
121 timeout: 900_000,
122 },
123);