teqo/tests/unit/bahiaElectionAggregates.unit.spec.ts
Francisco Ignácio Fontoura Solla a32ba3b4f0 Cache the campaign hot path: committed TSE artifact + request-scoped dedup
- committed artifact src/lib/electionAggregates/bahia-federal-baseline.json
  (Solla nominal + federal T1 valid votes per catalog municipality × TSE year,
  Salvador split by zone) generated by pnpm build:election-aggregates from the
  locally seeded TSE data; typed loader src/lib/bahiaElectionAggregates.ts;
  snapshot spec pins artifact ↔ catalog ↔ baseline ticket
- map bundle: historical years now read the artifact (was 6 statewide
  city×zone scans per dashboard visit); compare mode keeps only the OTHER
  candidate on the DB path (Solla side from the artifact)
- loadMunicipalityScope (React cache()): one municipality find + pledge
  aggregate per request shared by dashboard KPIs, map bundle, and the
  municípios list overview (was 2-3 duplicate loads)
- unstable_cache + election-tse tag on the immutable elections-tab loaders
  (baseline, candidate comparison, candidate options); tag added to the
  /api/revalidate allowlist — runbook: re-seed then bust the tag
- municipalityMapContract.ts: client-safe map vocabulary/bundle types so the
  client panel no longer imports the server data module (build had pulled
  @payload-config into the client graph); municipalityMapData is server-only
  and re-exports the contract; PLAZA_MAP_* renamed to MUNICIPALITY_MAP_*
- petition generateStaticParams enumerates bare ids (getPetitionIds)
2026-07-23 23:06:28 -03:00

58 lines
2.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
FEDERAL_BASELINE_CANDIDATE_NUMBER,
federalBaselineMunicipalitySlugs,
getMunicipalityFederalBaseline,
} from '@/lib/bahiaElectionAggregates'
import { BASELINE_TICKET_2022, HISTORICAL_SERIES_YEARS } from '@/lib/electionResults'
import { municipalityCatalog } from '@/lib/municipalityCatalog'
/**
* Pins the committed election-aggregate artifact to the municipality catalog.
* If either changes shape (new remodel, new TSE scope), regenerate with
* `pnpm build:election-aggregates` — a drift here means the map would render
* zeros silently.
*/
describe('election aggregates artifact', () => {
it('covers exactly the municipality catalog slugs', () => {
const artifactSlugs = new Set(federalBaselineMunicipalitySlugs())
const catalogSlugs = new Set(municipalityCatalog.map((entry) => entry.slug))
expect(artifactSlugs.size).toBe(catalogSlugs.size)
for (const slug of catalogSlugs) {
expect(artifactSlugs.has(slug)).toBe(true)
}
})
it('is generated for the baseline ticket candidate', () => {
expect(FEDERAL_BASELINE_CANDIDATE_NUMBER).toBe(BASELINE_TICKET_2022.candidate.candidateNumber)
})
it('has every historical year populated with plausible statewide totals', () => {
for (const year of HISTORICAL_SERIES_YEARS) {
let candidateTotal = 0
let validTotal = 0
for (const slug of federalBaselineMunicipalitySlugs()) {
const baseline = getMunicipalityFederalBaseline(slug)
const votes = baseline.votesByYear[String(year)]
const valid = baseline.validVotesByYear[String(year)]
expect(votes).toBeTypeOf('number')
expect(valid).toBeTypeOf('number')
expect(votes).toBeGreaterThanOrEqual(0)
expect(valid).toBeGreaterThanOrEqual(votes > 0 ? 1 : 0)
candidateTotal += votes ?? 0
validTotal += valid ?? 0
}
// A statewide federal-deputy campaign always lands in the 5-7 digit range.
expect(candidateTotal).toBeGreaterThan(10_000)
expect(validTotal).toBeGreaterThan(candidateTotal)
}
})
it('returns an empty baseline for unknown slugs', () => {
const baseline = getMunicipalityFederalBaseline('nao-existe')
expect(baseline.votesByYear).toEqual({})
expect(baseline.validVotesByYear).toEqual({})
})
})