1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as servicedirectory from "@distilled.cloud/gcp/servicedirectory_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const waitUntilGone = (name: string) =>
17 servicedirectory.getProjectsLocationsNamespacesServices({ name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27test.provider(
28 "create, update, resolve, and delete a service",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 const ns = yield* GCP.ServiceDirectory.Namespace("Services", {
36 location: "us-central1",
37 labels: { env: "test" },
38 });
39 const api = yield* GCP.ServiceDirectory.Service("Api", {
40 namespace: ns.name,
41 annotations: { protocol: "http" },
42 });
43 return { ns, api };
44 }),
45 );
46
47 expect(created.api.name).toContain("/services/");
48 expect(created.api.serviceId).toEqual(expect.any(String));
49 expect(created.api.namespace).toEqual(created.ns.name);
50 expect(created.api.namespaceId).toEqual(created.ns.namespaceId);
51 expect(created.api.location).toEqual("us-central1");
52 expect(created.api.annotations).toMatchObject({ protocol: "http" });
53 expect(created.api.uid).toEqual(expect.any(String));
54
55 const fetched =
56 yield* servicedirectory.getProjectsLocationsNamespacesServices({
57 name: created.api.name,
58 });
59 expect(fetched.name).toEqual(created.api.name);
60 expect(fetched.annotations?.protocol).toEqual("http");
61 expect(fetched.annotations?.["alchemy-id"]).toEqual(expect.any(String));
62 expect(fetched.uid).toEqual(created.api.uid);
63
64 const resolved =
65 yield* servicedirectory.resolveProjectsLocationsNamespacesServices({
66 name: created.api.name,
67 body: {},
68 });
69 expect(resolved.service?.name).toEqual(created.api.name);
70
71 const updated = yield* stack.deploy(
72 Effect.gen(function* () {
73 const ns = yield* GCP.ServiceDirectory.Namespace("Services", {
74 namespaceId: created.ns.namespaceId,
75 location: "us-central1",
76 labels: { env: "test" },
77 });
78 const api = yield* GCP.ServiceDirectory.Service("Api", {
79 namespace: ns.name,
80 serviceId: created.api.serviceId,
81 annotations: { protocol: "grpc", role: "api" },
82 });
83 return { ns, api };
84 }),
85 );
86
87 expect(updated.api.name).toEqual(created.api.name);
88 expect(updated.api.uid).toEqual(created.api.uid);
89 expect(updated.api.annotations).toMatchObject({
90 protocol: "grpc",
91 role: "api",
92 });
93
94 const refetched =
95 yield* servicedirectory.getProjectsLocationsNamespacesServices({
96 name: created.api.name,
97 });
98 expect(refetched.annotations?.protocol).toEqual("grpc");
99 expect(refetched.annotations?.role).toEqual("api");
100
101 yield* stack.destroy();
102
103 const gone = yield* waitUntilGone(created.api.name);
104 expect(gone).toEqual("gone");
105 }).pipe(logLevel),
106 {
107 tags: ["provider:gcp", "provider:gcp:servicedirectory", "live"],
108 timeout: 90_000,
109 },
110);