- triplo de cenarios: municipalityMapData usa VoteEstimateScenarioFields (2 grafias inline eliminadas) - MAX_LEADERSHIP_MUNICIPALITIES: Leadership.ts importa de lib/schemas/leadership (const local removida) - campaignUserInitials: copia divergente do AvatarStack removida; fonte unica em campaignUserProfile (com fallback defensivo) - tendencia politica: options do select da collection derivam de politicalTrendStatuses + politicalTrendLabels; ProjectionTrendStatus vira alias de PoliticalTrendStatusValue - MunicipalityStrategyViewModel nomeado e exportado (consumidores param de indexar NonNullable<...['strategy']>); nota de por que NAO deriva do zod - DynamicFind documentado como a unica grafia aprovada do cast - stub<T> nos doubles de teste onde a checagem por propriedade e real (campaignUserPhone, campaignAuth, campaignConsent); deep fakes mantem o cast localizado
71 lines
2.4 KiB
TypeScript
71 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'
|
|
|
|
import { stub } from '../helpers/stub'
|
|
|
|
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 = stub<Payload>({ find })
|
|
|
|
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 = stub<Payload>({ find })
|
|
|
|
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 = stub<Payload>({
|
|
find: vi.fn().mockResolvedValue({ docs: [] }),
|
|
})
|
|
|
|
await expect(getLeadershipConsent(payload)).resolves.toBeNull()
|
|
await expect(requireLeadershipConsent(payload, undefined, 'Consentimento ausente.')).rejects.toThrow(
|
|
'Consentimento ausente.',
|
|
)
|
|
})
|
|
})
|