teqo/tests/unit/federalCandidateTotalsAggregate.unit.spec.ts
Francisco Ignácio Fontoura Solla e2724c10da Remodel the campaign vertical from Praça to Município
Rename the operational unit "Praça"/"Núcleo" to "Município" (code identifier
`municipality`) and land the 2026-07-23 course changes:

- New `candidate` role with unrestricted visibility (`isCampaignStaff` now
  includes it) alongside coordinator/advisor; leader is locked down to the
  supporter contact tool only.
- Demandas become staff-only; dobradinhas modeled via a `stateDeputy` entity.
- Camaçari consolidated to a single município (ZE 170/171 aggregated);
  Salvador keeps its 19 zone municípios.
- Map moves to the staff dashboard; `/campanha/municipios` is list/overview.
- Vote pledges keep the declared vs estimated (scenario) asymmetry; leaders
  never see estimates.

Destructive migration for the campaign vertical (drops nucleus/plaza-era
tables, wipes campaign rows, seeds 435 municípios). Public-site data untouched.

Includes the post-delivery /simplify cleanups: shared reloadStaffActor /
reloadCoordinatorActor helpers, req.context cache for getFreshCampaignUser,
parallelized dashboard loads, candidate dashboard-copy fix, and dead
leader-dashboard / roleKind code removal.
2026-07-23 21:36:20 -03:00

94 lines
2.8 KiB
TypeScript

// @vitest-environment node
import type { Payload } from 'payload'
import { describe, expect, it, vi } from 'vitest'
import type { CampaignUser, User } from '@/payload-types'
import { assertCanReadElectionData } from '@/utilities/campaignAccess'
import * as drizzleBulk from '@/utilities/drizzleBulk'
import { loadFederalCandidateTotalsAggregated } from '@/utilities/federalCandidateTotalsAggregate'
import type { MunicipalityElectionGeography } from '@/utilities/municipalityElectionGeography'
describe('assertCanReadElectionData', () => {
it('allows payload admins and staff campaign users to read election data', () => {
expect(() =>
assertCanReadElectionData({
collection: 'users',
id: 1,
} as User),
).not.toThrow()
expect(() =>
assertCanReadElectionData({
collection: 'campaignUser',
id: 1,
role: 'advisor',
} as unknown as CampaignUser),
).not.toThrow()
expect(() =>
assertCanReadElectionData({
collection: 'campaignUser',
id: 1,
role: 'leader',
} as unknown as CampaignUser),
).toThrow('Leitura de dados eleitorais negada.')
})
it('denies readers outside campaign/admin collections', () => {
expect(() =>
assertCanReadElectionData({
collection: 'contact',
id: 1,
} as never),
).toThrow('Leitura de dados eleitorais negada.')
})
})
describe('loadFederalCandidateTotalsAggregated', () => {
it('maps drizzle aggregate rows and scopes geography in SQL', async () => {
const geography: MunicipalityElectionGeography = {
cityCode: '38490',
zones: [1, 2],
}
const execute = vi.fn().mockResolvedValue({
rows: [
{
candidate_number: 1313,
candidate_name: 'JORGE SOLLA',
party: 'PT',
votes: 2100,
},
{
candidate_number: 1234,
candidate_name: 'OUTRO',
party: 'PL',
votes: 900,
},
],
})
vi.spyOn(drizzleBulk, 'requirePostgresDrizzle').mockReturnValue({ execute })
const payload = { db: { name: 'postgres' } } as Pick<Payload, 'db'>
const totals = await loadFederalCandidateTotalsAggregated(
payload,
{
collection: 'campaignUser',
id: 1,
role: 'coordinator',
} as unknown as CampaignUser,
geography,
)
expect(totals).toEqual([
{ candidateNumber: 1313, name: 'JORGE SOLLA', party: 'PT', votes: 2100 },
{ candidateNumber: 1234, name: 'OUTRO', party: 'PL', votes: 900 },
])
expect(execute).toHaveBeenCalledOnce()
const sqlPayload = JSON.stringify(execute.mock.calls[0]?.[0])
expect(sqlPayload).toContain('city_code')
expect(sqlPayload).not.toContain('city_name')
vi.restoreAllMocks()
})
})