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