teqo-backup/tests/unit/recentVisits.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

83 lines
2.7 KiB
TypeScript

import { afterEach, describe, expect, it } from 'vitest'
import {
clearRecentVisits,
listRecentVisits,
MAX_ENTRIES,
recordRecentVisit,
STORAGE_KEY,
type RecentVisitEntry,
} from '@/utilities/recentVisits'
const sampleEntry = (overrides: Partial<RecentVisitEntry> = {}): RecentVisitEntry => ({
href: '/campanha/municipios/seabra',
label: 'Seabra',
kind: 'municipality',
visitedAt: 1_700_000_000_000,
...overrides,
})
describe('recentVisits storage', () => {
afterEach(() => {
localStorage.clear()
})
it('returns an empty list when storage is missing or invalid', () => {
expect(listRecentVisits()).toEqual([])
localStorage.setItem(STORAGE_KEY, 'not-json')
expect(listRecentVisits()).toEqual([])
localStorage.setItem(STORAGE_KEY, JSON.stringify({ href: '/campanha/municipios/x' }))
expect(listRecentVisits()).toEqual([])
})
it('drops persisted entries with retired visit kinds', () => {
localStorage.setItem(
STORAGE_KEY,
JSON.stringify([
sampleEntry({ kind: 'municipality' }),
sampleEntry({ href: '/campanha/municipios', kind: 'municipalityList' }),
{ ...sampleEntry({ href: '/campanha/nucleos/antigo' }), kind: 'nucleus' },
]),
)
expect(listRecentVisits()).toEqual([
sampleEntry({ kind: 'municipality' }),
sampleEntry({ href: '/campanha/municipios', kind: 'municipalityList' }),
])
})
it('deduplicates by href and keeps the newest visit at the top', () => {
recordRecentVisit(sampleEntry({ href: '/campanha/municipios/a', visitedAt: 10 }))
recordRecentVisit(sampleEntry({ href: '/campanha/municipios/b', visitedAt: 20 }))
recordRecentVisit(sampleEntry({ href: '/campanha/municipios/a', label: 'Atualizado', visitedAt: 30 }))
expect(listRecentVisits()).toEqual([
sampleEntry({ href: '/campanha/municipios/a', label: 'Atualizado', visitedAt: 30 }),
sampleEntry({ href: '/campanha/municipios/b', visitedAt: 20 }),
])
})
it('truncates the list to MAX_ENTRIES', () => {
for (let index = 0; index < MAX_ENTRIES + 2; index += 1) {
recordRecentVisit(
sampleEntry({
href: `/campanha/municipios/praca-${index}`,
label: `Praça ${index}`,
visitedAt: index,
}),
)
}
const visits = listRecentVisits()
expect(visits).toHaveLength(MAX_ENTRIES)
expect(visits[0]?.href).toBe(`/campanha/municipios/praca-${MAX_ENTRIES + 1}`)
expect(visits.at(-1)?.href).toBe('/campanha/municipios/praca-2')
})
it('clears stored visits', () => {
recordRecentVisit(sampleEntry())
clearRecentVisits()
expect(listRecentVisits()).toEqual([])
expect(localStorage.getItem(STORAGE_KEY)).toBeNull()
})
})