1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as cnr from "@distilled.cloud/gcp/cloudnumberregistry_v1alpha";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import {
7 location,
8 logLevel,
9 currentProject,
10 entitled,
11 runLifecycle,
12 waitUntilGone,
13} from "./common.ts";
14
15const { test } = Test.make({ providers: GCP.providers() });
16
17test.provider.skipIf(!entitled)(
18 "getProjectsLocationsRealms on a missing realm fails with a typed tag",
19 (stack) =>
20 Effect.gen(function* () {
21 const project = yield* currentProject;
22
23 yield* stack.destroy();
24
25 const error = yield* Effect.flip(
26 cnr.getProjectsLocationsRealms({
27 name: `projects/${project}/locations/${location}/realms/alchemy-missing-realm`,
28 }),
29 );
30 expect(error._tag).toEqual("NotFound");
31
32 yield* stack.destroy();
33 }).pipe(logLevel),
34 {
35 tags: ["provider:gcp", "provider:gcp:cloudnumberregistry", "live"],
36 timeout: 90_000,
37 },
38);
39
40test.provider.skipIf(entitled)(
41 "getProjectsLocationsRealms fails with ServiceDisabled while the API is disabled",
42 (stack) =>
43 Effect.gen(function* () {
44 const project = yield* currentProject;
45
46 yield* stack.destroy();
47
48 const error = yield* Effect.flip(
49 cnr.getProjectsLocationsRealms({
50 name: `projects/${project}/locations/${location}/realms/alchemy-missing-realm`,
51 }),
52 );
53 expect(error._tag).toEqual("ServiceDisabled");
54
55 yield* stack.destroy();
56 }).pipe(logLevel),
57 {
58 tags: ["provider:gcp", "provider:gcp:cloudnumberregistry", "live"],
59 timeout: 90_000,
60 },
61);
62
63test.provider.skipIf(!runLifecycle)(
64 "create, update, and delete a realm",
65 (stack) =>
66 Effect.gen(function* () {
67 const project = yield* currentProject;
68
69 yield* stack.destroy();
70
71 const created = yield* stack.deploy(
72 Effect.gen(function* () {
73 const book = yield* GCP.CloudNumberRegistry.RegistryBook(
74 "Inventory",
75 {
76 location,
77 labels: { env: "test" },
78 },
79 );
80 const realm = yield* GCP.CloudNumberRegistry.Realm("Private", {
81 location,
82 registryBook: book.name,
83 trafficType: "PRIVATE",
84 managementType: "USER",
85 ipVersion: "IPV4",
86 labels: { env: "test" },
87 });
88 return { book, realm };
89 }),
90 );
91
92 expect(created.realm.realmId).toEqual(expect.any(String));
93 expect(created.realm.name).toEqual(
94 `projects/${project}/locations/${location}/realms/${created.realm.realmId}`,
95 );
96 expect(created.realm.location).toEqual(location);
97 expect(created.realm.trafficType).toEqual("PRIVATE");
98 expect(created.realm.managementType).toEqual("USER");
99 expect(created.realm.labels).toMatchObject({ env: "test" });
100
101 const fetched = yield* cnr.getProjectsLocationsRealms({
102 name: created.realm.name,
103 });
104 expect(fetched.name).toEqual(created.realm.name);
105 expect(fetched.labels?.env).toEqual("test");
106 expect(
107 Object.keys(fetched.labels ?? {}).some((key) =>
108 key.startsWith("alchemy-"),
109 ),
110 ).toEqual(true);
111
112 const updated = yield* stack.deploy(
113 Effect.gen(function* () {
114 const book = yield* GCP.CloudNumberRegistry.RegistryBook(
115 "Inventory",
116 {
117 registryBookId: created.book.registryBookId,
118 location,
119 labels: { env: "test" },
120 },
121 );
122 const realm = yield* GCP.CloudNumberRegistry.Realm("Private", {
123 realmId: created.realm.realmId,
124 location,
125 registryBook: book.name,
126 trafficType: "PRIVATE",
127 managementType: "USER",
128 ipVersion: "IPV4",
129 labels: { env: "prod", role: "realm" },
130 });
131 return { book, realm };
132 }),
133 );
134
135 expect(updated.realm.name).toEqual(created.realm.name);
136 expect(updated.realm.realmId).toEqual(created.realm.realmId);
137 expect(updated.realm.labels).toMatchObject({
138 env: "prod",
139 role: "realm",
140 });
141
142 const refetched = yield* cnr.getProjectsLocationsRealms({
143 name: created.realm.name,
144 });
145 expect(refetched.labels?.env).toEqual("prod");
146 expect(refetched.labels?.role).toEqual("realm");
147
148 yield* stack.destroy();
149 const gone = yield* waitUntilGone(
150 cnr.getProjectsLocationsRealms({ name: created.realm.name }),
151 );
152 expect(gone).toEqual("gone");
153 }).pipe(logLevel),
154 {
155 tags: ["provider:gcp", "provider:gcp:cloudnumberregistry", "live"],
156 timeout: 90_000,
157 },
158);