| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as gmailpostmastertools from "@distilled.cloud/gcp/gmailpostmastertools_v2"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import { logLevel, runLifecycle } from "./common.ts"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const waitUntilGone = (name: string) => |
| 12 | gmailpostmastertools.getDomains({ 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 | |
| 22 | test.provider.skipIf(runLifecycle)( |
| 23 | "getDomains on a missing domain fails with a typed tag", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* stack.destroy(); |
| 27 | |
| 28 | const error = yield* Effect.flip( |
| 29 | gmailpostmastertools.getDomains({ |
| 30 | name: "domains/alchemy-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 | |
| 43 | test.provider.skipIf(runLifecycle)( |
| 44 | "createDomains 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.createDomains({ |
| 51 | body: { domainId: "alchemy-probe.example.com" }, |
| 52 | }), |
| 53 | ); |
| 54 | expect(error._tag).toEqual("InsufficientScopes"); |
| 55 | |
| 56 | yield* stack.destroy(); |
| 57 | }).pipe(logLevel), |
| 58 | { |
| 59 | tags: ["provider:gcp", "provider:gcp:gmailpostmastertools", "live"], |
| 60 | timeout: 90_000, |
| 61 | }, |
| 62 | ); |
| 63 | |
| 64 | test.provider.skipIf(!runLifecycle)( |
| 65 | "create, update, and delete a domain", |
| 66 | (stack) => |
| 67 | Effect.gen(function* () { |
| 68 | yield* stack.destroy(); |
| 69 | |
| 70 | const created = yield* stack.deploy( |
| 71 | Effect.gen(function* () { |
| 72 | return yield* GCP.GmailPostmasterTools.Domain("Mail", {}); |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(created.name.startsWith("domains/")).toEqual(true); |
| 77 | expect(created.domainId.length).toBeGreaterThan(0); |
| 78 | expect(created.domainId.startsWith("alchemy-")).toEqual(true); |
| 79 | |
| 80 | const fetched = yield* gmailpostmastertools.getDomains({ |
| 81 | name: created.name, |
| 82 | }); |
| 83 | expect(fetched.name).toEqual(created.name); |
| 84 | |
| 85 | const updated = yield* stack.deploy( |
| 86 | Effect.gen(function* () { |
| 87 | return yield* GCP.GmailPostmasterTools.Domain("Mail", { |
| 88 | domainId: created.domainId, |
| 89 | }); |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | expect(updated.name).toEqual(created.name); |
| 94 | expect(updated.domainId).toEqual(created.domainId); |
| 95 | |
| 96 | yield* stack.destroy(); |
| 97 | |
| 98 | const gone = yield* waitUntilGone(created.name); |
| 99 | expect(gone).toEqual("gone"); |
| 100 | }).pipe(logLevel), |
| 101 | { |
| 102 | tags: ["provider:gcp", "provider:gcp:gmailpostmastertools", "live"], |
| 103 | timeout: 90_000, |
| 104 | }, |
| 105 | ); |