1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as gmailpostmastertools from "@distilled.cloud/gcp/gmailpostmastertools_v2";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Schedule from "effect/Schedule";
7import { logLevel, runLifecycle } from "./common.ts";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const waitUntilGone = (name: string) =>
12 gmailpostmastertools.getDomainsUsers({ name }).pipe(
13 Effect.as("found" as const),
14 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
15 Effect.repeat({
16 schedule: Schedule.spaced("1 second"),
17 until: (status) => status === "gone",
18 times: 10,
19 }),
20 );
21
22test.provider.skipIf(runLifecycle)(
23 "getDomainsUsers on a missing user fails with a typed tag",
24 (stack) =>
25 Effect.gen(function* () {
26 yield* stack.destroy();
27
28 const error = yield* Effect.flip(
29 gmailpostmastertools.getDomainsUsers({
30 name: "domains/alchemy-missing.example.com/users/missing@example.com",
31 }),
32 );
33 expect(error._tag).toEqual("InsufficientScopes");
34
35 yield* stack.destroy();
36 }).pipe(logLevel),
37 {
38 tags: ["provider:gcp", "provider:gcp:gmailpostmastertools", "live"],
39 timeout: 90_000,
40 },
41);
42
43test.provider.skipIf(runLifecycle)(
44 "createDomainsUsers without Postmaster Tools access fails with a typed tag",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48
49 const error = yield* Effect.flip(
50 gmailpostmastertools.createDomainsUsers({
51 parent: "domains/alchemy-missing.example.com",
52 body: {
53 userId: "alchemy-probe@example.com",
54 permission: "READER",
55 },
56 }),
57 );
58 expect(error._tag).toEqual("InsufficientScopes");
59
60 yield* stack.destroy();
61 }).pipe(logLevel),
62 {
63 tags: ["provider:gcp", "provider:gcp:gmailpostmastertools", "live"],
64 timeout: 90_000,
65 },
66);
67
68test.provider.skipIf(!runLifecycle)(
69 "create, update, and delete a domain user",
70 (stack) =>
71 Effect.gen(function* () {
72 yield* stack.destroy();
73
74 const created = yield* stack.deploy(
75 Effect.gen(function* () {
76 const domain = yield* GCP.GmailPostmasterTools.Domain("Mail", {});
77 const user = yield* GCP.GmailPostmasterTools.DomainsUser("Ada", {
78 parent: domain.name,
79 permission: "READER",
80 });
81 return { domain, user };
82 }),
83 );
84
85 expect(created.user.parent).toEqual(created.domain.name);
86 expect(created.user.userId.startsWith("alchemy-")).toEqual(true);
87 expect(created.user.permission).toEqual("READER");
88
89 const fetched = yield* gmailpostmastertools.getDomainsUsers({
90 name: created.user.name,
91 });
92 expect(fetched.permission).toEqual("READER");
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 const domain = yield* GCP.GmailPostmasterTools.Domain("Mail", {
97 domainId: created.domain.domainId,
98 });
99 const user = yield* GCP.GmailPostmasterTools.DomainsUser("Ada", {
100 parent: domain.name,
101 userId: created.user.userId,
102 permission: "ADMIN",
103 });
104 return { domain, user };
105 }),
106 );
107
108 expect(updated.user.name).toEqual(created.user.name);
109 expect(updated.user.permission).toEqual("ADMIN");
110
111 yield* stack.destroy();
112
113 const gone = yield* waitUntilGone(created.user.name);
114 expect(gone).toEqual("gone");
115 }).pipe(logLevel),
116 {
117 tags: ["provider:gcp", "provider:gcp:gmailpostmastertools", "live"],
118 timeout: 90_000,
119 },
120);