| 1 | import * as AWS from "@/AWS"; |
| 2 | import { LinkedWhatsAppBusinessAccount } from "@/AWS/SocialMessaging"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as socialmessaging from "@distilled.cloud/aws/socialmessaging"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | |
| 8 | const { test } = Test.make({ providers: AWS.providers() }); |
| 9 | |
| 10 | // WhatsApp Business onboarding requires the Meta embedded-signup OAuth flow |
| 11 | // in the AWS console (cannot be automated), so the full lifecycle is gated |
| 12 | // behind AWS_TEST_SOCIALMESSAGING=1 + AWS_TEST_SOCIALMESSAGING_WABA_ID. |
| 13 | // The ungated probes below prove the distilled error union carries the typed |
| 14 | // tags the provider's read/reconcile/delete paths depend on, in every CI run. |
| 15 | |
| 16 | test.provider( |
| 17 | "listLinkedWhatsAppBusinessAccounts succeeds (no onboarding required)", |
| 18 | () => |
| 19 | Effect.gen(function* () { |
| 20 | const response = |
| 21 | yield* socialmessaging.listLinkedWhatsAppBusinessAccounts({}); |
| 22 | expect(Array.isArray(response.linkedAccounts ?? [])).toBe(true); |
| 23 | }), |
| 24 | { tags: ["provider:aws", "provider:aws:socialmessaging", "live"] }, |
| 25 | ); |
| 26 | |
| 27 | test.provider( |
| 28 | "getLinkedWhatsAppBusinessAccount on a nonexistent id fails with ResourceNotFoundException", |
| 29 | () => |
| 30 | Effect.gen(function* () { |
| 31 | const error = yield* Effect.flip( |
| 32 | socialmessaging.getLinkedWhatsAppBusinessAccount({ |
| 33 | // well-formed but nonexistent linked-WABA id |
| 34 | id: "waba-0123456789abcdef0123456789abcdef", |
| 35 | }), |
| 36 | ); |
| 37 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 38 | }), |
| 39 | { tags: ["provider:aws", "provider:aws:socialmessaging", "live"] }, |
| 40 | ); |
| 41 | |
| 42 | test.provider( |
| 43 | "disassociateWhatsAppBusinessAccount on a nonexistent id fails with ResourceNotFoundException", |
| 44 | () => |
| 45 | Effect.gen(function* () { |
| 46 | const error = yield* Effect.flip( |
| 47 | socialmessaging.disassociateWhatsAppBusinessAccount({ |
| 48 | id: "waba-0123456789abcdef0123456789abcdef", |
| 49 | }), |
| 50 | ); |
| 51 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 52 | }), |
| 53 | { tags: ["provider:aws", "provider:aws:socialmessaging", "live"] }, |
| 54 | ); |
| 55 | |
| 56 | // Full lifecycle — requires a WhatsApp Business Account already linked via |
| 57 | // the AWS End User Messaging Social console (Meta embedded signup). Set |
| 58 | // AWS_TEST_SOCIALMESSAGING=1 and AWS_TEST_SOCIALMESSAGING_WABA_ID=waba-... |
| 59 | // to run. NOTE: the final stack.destroy() disassociates the WABA from the |
| 60 | // AWS account; re-running afterwards requires redoing the console signup. |
| 61 | test.provider.skipIf( |
| 62 | !process.env.AWS_TEST_SOCIALMESSAGING || |
| 63 | !process.env.AWS_TEST_SOCIALMESSAGING_WABA_ID, |
| 64 | )( |
| 65 | "adopt linked WABA, sync tags + event destinations, destroy", |
| 66 | (stack) => |
| 67 | Effect.gen(function* () { |
| 68 | const wabaId = process.env.AWS_TEST_SOCIALMESSAGING_WABA_ID!; |
| 69 | yield* stack.destroy(); |
| 70 | |
| 71 | const { account } = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | const account = yield* LinkedWhatsAppBusinessAccount("Business", { |
| 74 | accountId: wabaId, |
| 75 | tags: { fixture: "socialmessaging" }, |
| 76 | }); |
| 77 | return { account }; |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | expect(account.id).toBe(wabaId); |
| 82 | expect(account.arn).toContain(":waba/"); |
| 83 | expect(account.wabaId).toBeDefined(); |
| 84 | expect(account.wabaName).toBeDefined(); |
| 85 | expect(account.tags.fixture).toBe("socialmessaging"); |
| 86 | |
| 87 | // Out-of-band verification via distilled. |
| 88 | const observed = yield* socialmessaging.getLinkedWhatsAppBusinessAccount({ |
| 89 | id: wabaId, |
| 90 | }); |
| 91 | expect(observed.account?.id).toBe(wabaId); |
| 92 | const observedTags = yield* socialmessaging.listTagsForResource({ |
| 93 | resourceArn: account.arn, |
| 94 | }); |
| 95 | const tagMap = Object.fromEntries( |
| 96 | (observedTags.tags ?? []).map((tag) => [tag.key, tag.value]), |
| 97 | ); |
| 98 | expect(tagMap.fixture).toBe("socialmessaging"); |
| 99 | expect(tagMap["alchemy::id"]).toBe("Business"); |
| 100 | |
| 101 | // Update — change tags only. |
| 102 | const updated = yield* stack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | const account = yield* LinkedWhatsAppBusinessAccount("Business", { |
| 105 | accountId: wabaId, |
| 106 | tags: { fixture: "socialmessaging", updated: "true" }, |
| 107 | }); |
| 108 | return { account }; |
| 109 | }), |
| 110 | ); |
| 111 | expect(updated.account.tags.updated).toBe("true"); |
| 112 | |
| 113 | // Destroy — disassociates the WABA; verify it is gone. |
| 114 | yield* stack.destroy(); |
| 115 | const gone = yield* Effect.flip( |
| 116 | socialmessaging.getLinkedWhatsAppBusinessAccount({ id: wabaId }), |
| 117 | ); |
| 118 | expect(gone._tag).toBe("ResourceNotFoundException"); |
| 119 | }), |
| 120 | { |
| 121 | tags: ["provider:aws", "provider:aws:socialmessaging", "live"], |
| 122 | timeout: 120_000, |
| 123 | }, |
| 124 | ); |