|
| 1 | +import { beforeEach, describe, expect, it, mock } from "bun:test"; |
| 2 | + |
| 3 | +const addUserToDefaultAudienceMock = mock( |
| 4 | + (async () => true) as (...args: unknown[]) => Promise<boolean> |
| 5 | +); |
| 6 | + |
| 7 | +mock.module("@cossistant/transactional", () => ({ |
| 8 | + addUserToDefaultAudience: addUserToDefaultAudienceMock, |
| 9 | +})); |
| 10 | + |
| 11 | +const authUserAudienceModulePromise = import("./auth-user-audience"); |
| 12 | + |
| 13 | +function spyConsole() { |
| 14 | + const warnSpy = mock(() => {}); |
| 15 | + const errorSpy = mock(() => {}); |
| 16 | + const originalWarn = console.warn; |
| 17 | + const originalError = console.error; |
| 18 | + |
| 19 | + console.warn = warnSpy as unknown as typeof console.warn; |
| 20 | + console.error = errorSpy as unknown as typeof console.error; |
| 21 | + |
| 22 | + return { |
| 23 | + warnSpy, |
| 24 | + errorSpy, |
| 25 | + restore: () => { |
| 26 | + console.warn = originalWarn; |
| 27 | + console.error = originalError; |
| 28 | + }, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +describe("syncUserToDefaultResendAudience", () => { |
| 33 | + beforeEach(() => { |
| 34 | + addUserToDefaultAudienceMock.mockReset(); |
| 35 | + addUserToDefaultAudienceMock.mockResolvedValue(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it("calls addUserToDefaultAudience when user email exists", async () => { |
| 39 | + const { syncUserToDefaultResendAudience } = |
| 40 | + await authUserAudienceModulePromise; |
| 41 | + |
| 42 | + await syncUserToDefaultResendAudience({ |
| 43 | + id: "user-1", |
| 44 | + email: "person@example.com", |
| 45 | + name: "Person Example", |
| 46 | + }); |
| 47 | + |
| 48 | + expect(addUserToDefaultAudienceMock).toHaveBeenCalledTimes(1); |
| 49 | + expect(addUserToDefaultAudienceMock).toHaveBeenCalledWith( |
| 50 | + "person@example.com", |
| 51 | + "Person Example" |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it("skips and logs a warning when email is missing", async () => { |
| 56 | + const { syncUserToDefaultResendAudience } = |
| 57 | + await authUserAudienceModulePromise; |
| 58 | + const { warnSpy, restore } = spyConsole(); |
| 59 | + |
| 60 | + try { |
| 61 | + await syncUserToDefaultResendAudience({ |
| 62 | + id: "user-2", |
| 63 | + }); |
| 64 | + } finally { |
| 65 | + restore(); |
| 66 | + } |
| 67 | + |
| 68 | + expect(addUserToDefaultAudienceMock).not.toHaveBeenCalled(); |
| 69 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it("does not throw and logs an error when enrollment returns false", async () => { |
| 73 | + const { syncUserToDefaultResendAudience } = |
| 74 | + await authUserAudienceModulePromise; |
| 75 | + const { errorSpy, restore } = spyConsole(); |
| 76 | + addUserToDefaultAudienceMock.mockResolvedValue(false); |
| 77 | + |
| 78 | + try { |
| 79 | + await syncUserToDefaultResendAudience({ |
| 80 | + id: "user-3", |
| 81 | + email: "person@example.com", |
| 82 | + name: "Person Example", |
| 83 | + }); |
| 84 | + } finally { |
| 85 | + restore(); |
| 86 | + } |
| 87 | + |
| 88 | + expect(addUserToDefaultAudienceMock).toHaveBeenCalledTimes(1); |
| 89 | + expect(errorSpy).toHaveBeenCalledTimes(1); |
| 90 | + }); |
| 91 | + |
| 92 | + it("does not throw and logs an error when enrollment throws", async () => { |
| 93 | + const { syncUserToDefaultResendAudience } = |
| 94 | + await authUserAudienceModulePromise; |
| 95 | + const { errorSpy, restore } = spyConsole(); |
| 96 | + addUserToDefaultAudienceMock.mockRejectedValue(new Error("resend failed")); |
| 97 | + |
| 98 | + try { |
| 99 | + await syncUserToDefaultResendAudience({ |
| 100 | + id: "user-4", |
| 101 | + email: "person@example.com", |
| 102 | + name: "Person Example", |
| 103 | + }); |
| 104 | + } finally { |
| 105 | + restore(); |
| 106 | + } |
| 107 | + |
| 108 | + expect(addUserToDefaultAudienceMock).toHaveBeenCalledTimes(1); |
| 109 | + expect(errorSpy).toHaveBeenCalledTimes(1); |
| 110 | + }); |
| 111 | +}); |
0 commit comments