teqo/tests/unit/campaignConsent.unit.spec.ts
Francisco Ignácio Fontoura Solla b079daa9bb Add C2 nominal supporter registry for /campanha.
Ship the supporter join collection, consent-keyed create/import/remove actions, and apoiadores UI so staff can build a phone-required nominal base before mass field ops.
2026-07-18 20:32:54 -03:00

69 lines
2.4 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it, vi } from 'vitest'
import type { Payload } from 'payload'
import {
getConsentByKey,
getLeadershipConsent,
requireConsentByKey,
requireLeadershipConsent,
SUPPORTER_REGISTRATION_CONSENT_KEY,
} from '@/utilities/campaignConsent'
import { CAMPAIGN_INVITE_CONSENT_KEY } from '@/utilities/campaignInvite'
describe('campaign consent descriptor', () => {
it('returns the stable descriptor and propagates the transaction request', async () => {
const req = { transactionID: 17 }
const find = vi.fn().mockResolvedValue({
docs: [{ id: 9, text: { root: { children: [] } } }],
})
const payload = { find } as unknown as Payload
const consent = await getLeadershipConsent(payload, req)
expect(consent).toEqual({
id: 9,
text: { root: { children: [] } },
contentHash: 'd800986181e1730e945e028853848395d8c34ccb95b54abbbaa50cb9539a845b',
})
expect(find).toHaveBeenCalledWith(
expect.objectContaining({
req,
where: { key: { equals: CAMPAIGN_INVITE_CONSENT_KEY } },
}),
)
})
it('resolves arbitrary consent keys and fails closed when missing', async () => {
const find = vi
.fn()
.mockResolvedValueOnce({
docs: [{ id: 3, text: { root: { children: [] } }, key: SUPPORTER_REGISTRATION_CONSENT_KEY }],
})
.mockResolvedValue({ docs: [] })
const payload = { find } as unknown as Payload
await expect(getConsentByKey(payload, SUPPORTER_REGISTRATION_CONSENT_KEY)).resolves.toEqual({
id: 3,
text: { root: { children: [] } },
contentHash: 'd800986181e1730e945e028853848395d8c34ccb95b54abbbaa50cb9539a845b',
key: SUPPORTER_REGISTRATION_CONSENT_KEY,
})
await expect(getConsentByKey(payload, 'missing-key')).resolves.toBeNull()
await expect(
requireConsentByKey(payload, 'missing-key', undefined, 'Consentimento ausente.'),
).rejects.toThrow('Consentimento ausente.')
})
it('keeps nullable and required missing-consent semantics distinct', async () => {
const payload = {
find: vi.fn().mockResolvedValue({ docs: [] }),
} as unknown as Payload
await expect(getLeadershipConsent(payload)).resolves.toBeNull()
await expect(requireLeadershipConsent(payload, undefined, 'Consentimento ausente.')).rejects.toThrow(
'Consentimento ausente.',
)
})
})