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.getProjectsLocationsGateways({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsGateways 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.getProjectsLocationsGateways({
37 name: `projects/${project}/locations/global/gateways/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
50test.provider(
51 "create, update, and delete an open mesh gateway",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 return yield* GCP.NetworkServices.Gateway("Mesh", {
59 location: "global",
60 type: "OPEN_MESH",
61 ports: [443],
62 description: "gateway a",
63 labels: { env: "test" },
64 });
65 }),
66 );
67
68 expect(created.name).toContain("/gateways/");
69 expect(created.gatewayId).toEqual(expect.any(String));
70 expect(created.location).toEqual("global");
71 expect(created.type).toEqual("OPEN_MESH");
72 expect(created.ports).toContain(443);
73 expect(created.description).toEqual("gateway a");
74 expect(created.labels).toMatchObject({ env: "test" });
75 expect(created.createTime).toEqual(expect.any(String));
76
77 const fetched = yield* networkservices.getProjectsLocationsGateways({
78 name: created.name,
79 });
80 expect(fetched.name).toEqual(created.name);
81 expect(fetched.description).toEqual("gateway a");
82 expect(fetched.labels?.env).toEqual("test");
83 expect(
84 Object.keys(fetched.labels ?? {}).some((key) =>
85 key.startsWith("alchemy-"),
86 ),
87 ).toEqual(true);
88
89 const updated = yield* stack.deploy(
90 Effect.gen(function* () {
91 return yield* GCP.NetworkServices.Gateway("Mesh", {
92 gatewayId: created.gatewayId,
93 location: "global",
94 type: "OPEN_MESH",
95 ports: [80, 443],
96 description: "gateway b",
97 labels: { env: "prod", role: "mesh" },
98 });
99 }),
100 );
101
102 expect(updated.name).toEqual(created.name);
103 expect(updated.description).toEqual("gateway b");
104 expect(updated.ports).toEqual(expect.arrayContaining([80, 443]));
105 expect(updated.labels).toMatchObject({ env: "prod", role: "mesh" });
106
107 const refetched = yield* networkservices.getProjectsLocationsGateways({
108 name: created.name,
109 });
110 expect(refetched.description).toEqual("gateway b");
111 expect(refetched.labels?.env).toEqual("prod");
112 expect(refetched.labels?.role).toEqual("mesh");
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: 90_000,
122 },
123);