1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as essentialcontacts from "@distilled.cloud/gcp/essentialcontacts_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 essentialcontacts.getOrganizationsContacts({ 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
27// Organization contacts need org-level Essential Contacts permissions the
28// testing service account does not have (calls fail with Forbidden). Set
29// GOOGLE_ORGANIZATION_ID on an identity that can manage org contacts.
30const organizationId = process.env.GOOGLE_ORGANIZATION_ID;
31const runLifecycle = !!organizationId;
32
33const organizationOf = () =>
34 Effect.succeed(
35 organizationId === undefined
36 ? ""
37 : organizationId.startsWith("organizations/")
38 ? organizationId
39 : `organizations/${organizationId}`,
40 );
41
42test.provider.skipIf(!runLifecycle)(
43 "getOrganizationsContacts on a missing contact fails with a typed tag",
44 (stack) =>
45 Effect.gen(function* () {
46 yield* stack.destroy();
47
48 const organization = yield* organizationOf();
49 const error = yield* Effect.flip(
50 essentialcontacts.getOrganizationsContacts({
51 name: `${organization}/contacts/0`,
52 }),
53 );
54 expect(error._tag).toEqual("NotFound");
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 {
59 tags: ["provider:gcp", "provider:gcp:essentialcontacts", "live"],
60 timeout: 90_000,
61 },
62);
63
64test.provider.skipIf(!runLifecycle)(
65 "create, update, replace, and delete an organization essential contact",
66 (stack) =>
67 Effect.gen(function* () {
68 yield* stack.destroy();
69
70 const organization = yield* organizationOf();
71
72 const created = yield* stack.deploy(
73 Effect.gen(function* () {
74 return yield* GCP.EssentialContacts.OrganizationContact("Ops", {
75 organization,
76 email: "org-ops@example.com",
77 languageTag: "en-US",
78 notificationCategorySubscriptions: ["ALL"],
79 });
80 }),
81 );
82
83 expect(created.contactId.length).toBeGreaterThan(0);
84 expect(created.organization).toEqual(organization);
85 expect(created.email).toEqual("org-ops@example.com");
86 expect(created.languageTag).toEqual("en-US");
87
88 const fetched = yield* essentialcontacts.getOrganizationsContacts({
89 name: created.name,
90 });
91 expect(fetched.name).toEqual(created.name);
92 expect(fetched.email).toContain("+alc.");
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.EssentialContacts.OrganizationContact("Ops", {
97 organization,
98 email: "org-ops@example.com",
99 languageTag: "en-GB",
100 notificationCategorySubscriptions: ["LEGAL"],
101 });
102 }),
103 );
104
105 expect(updated.name).toEqual(created.name);
106 expect(updated.languageTag).toEqual("en-GB");
107 expect(updated.notificationCategorySubscriptions).toEqual(["LEGAL"]);
108
109 const replaced = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* GCP.EssentialContacts.OrganizationContact("Ops", {
112 organization,
113 email: "org-sec@example.com",
114 languageTag: "en-GB",
115 notificationCategorySubscriptions: ["SECURITY"],
116 });
117 }),
118 );
119
120 expect(replaced.email).toEqual("org-sec@example.com");
121
122 yield* stack.destroy();
123
124 const gone = yield* waitUntilGone(replaced.name);
125 expect(gone).toEqual("gone");
126 }).pipe(logLevel),
127 {
128 tags: ["provider:gcp", "provider:gcp:essentialcontacts", "live"],
129 timeout: 90_000,
130 },
131);