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.getProjectsLocationsMeshes({ 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 "getProjectsLocationsMeshes on a missing mesh 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.getProjectsLocationsMeshes({
37 name: `projects/${project}/locations/global/meshes/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 a mesh",
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.Mesh("Sidecar", {
59 location: "global",
60 description: "mesh a",
61 labels: { env: "test" },
62 interceptionPort: 15001,
63 });
64 }),
65 );
66
67 expect(created.name).toContain("/meshes/");
68 expect(created.meshId).toEqual(expect.any(String));
69 expect(created.location).toEqual("global");
70 expect(created.description).toEqual("mesh a");
71 expect(created.interceptionPort).toEqual(15001);
72 expect(created.labels).toMatchObject({ env: "test" });
73 expect(created.createTime).toEqual(expect.any(String));
74
75 const fetched = yield* networkservices.getProjectsLocationsMeshes({
76 name: created.name,
77 });
78 expect(fetched.name).toEqual(created.name);
79 expect(fetched.description).toEqual("mesh a");
80 expect(fetched.interceptionPort).toEqual(15001);
81 expect(fetched.labels?.env).toEqual("test");
82 expect(
83 Object.keys(fetched.labels ?? {}).some((key) =>
84 key.startsWith("alchemy-"),
85 ),
86 ).toEqual(true);
87
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* GCP.NetworkServices.Mesh("Sidecar", {
91 meshId: created.meshId,
92 location: "global",
93 description: "mesh b",
94 labels: { env: "prod", role: "mesh" },
95 interceptionPort: 15006,
96 });
97 }),
98 );
99
100 expect(updated.name).toEqual(created.name);
101 expect(updated.description).toEqual("mesh b");
102 expect(updated.interceptionPort).toEqual(15006);
103 expect(updated.labels).toMatchObject({ env: "prod", role: "mesh" });
104
105 const refetched = yield* networkservices.getProjectsLocationsMeshes({
106 name: created.name,
107 });
108 expect(refetched.description).toEqual("mesh b");
109 expect(refetched.interceptionPort).toEqual(15006);
110 expect(refetched.labels?.env).toEqual("prod");
111 expect(refetched.labels?.role).toEqual("mesh");
112
113 yield* stack.destroy();
114
115 const gone = yield* waitUntilGone(created.name);
116 expect(gone).toEqual("gone");
117 }).pipe(logLevel),
118 {
119 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
120 timeout: 90_000,
121 },
122);