teqo/tests/unit/bahiaMapStyle.unit.spec.ts
Francisco Ignácio Fontoura Solla 29f45e093a B13: escala relativa no mapa dos Municipios (quantis/LQ/posicao) + artefato TSE v3
O coropleto do Inicio era quase monocromatico porque 0-100% dos validos comprime
uma disputa cujo topo estadual e 4,71%. Tres escalas relativas ao proprio
candidato, todas em classes discretas: quantis (novo default), padrao proprio
(LQ) e posicao no municipio, ao lado de "% dos validos" (B11) e "Total (votos)".

Artefato v3: federalRankByIbgeCode com a colocacao entre todos os candidatos com
votos > 0 na geografia (2014/2018/2022), chaveado por ibgeCode porque e o que o
mapa desenha. Fica no artefato commitado, nao em query de request, que varreria
~100 mil linhas por ano para um dado imutavel. 534 -> 623 KB (orcamento 700 KB).

LQ virou 5 classes nas ancoras do E10, nao diverging: o plano pedia as duas
coisas e se contradizia. Vermelho x azul continua significando "Solla x
adversario" e segue exclusivo do compare.

computeAggregateTerritorialClass classifica um grupo de slugs como um territorio
so, somando insumos e rodando o mesmo classificador (LQ e razao; a media de
razoes nao e a razao das somas) - o E12 herda o helper. TERRITORIAL_CLASS_ANCHORS
mudou para src/lib/territorialClassAnchors.ts para nao arrastar o artefato TSE
ao bundle do cliente.

Quantis sobre o escopo de acesso do usuario, degradando para 3 classes abaixo de
10 municipios com a legenda declarando a contagem efetiva. Rank indisponivel em
2026 (dado TSE); LQ em 2026 reusa o padrao estadual de 2022.

Bolhas opcionais por validos projetados (area proporcional ao valor, cor da
classe E10, interactive: false). 417 marcadores medidos antes de otimizar - nao
derrubaram o frame, entao o corte para top-K nao foi feito.

Sem migration, sem collection, sem server action; bundle client de /campanha
byte a byte igual.
2026-07-26 04:22:56 -03:00

138 lines
4.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
buildLayerStyleContext,
canonicalMapKeysKey,
computeChoroplethMax,
resolvePathStyle,
} from '@/lib/bahiaMapStyle'
describe('bahiaMapStyle', () => {
const baseContext = buildLayerStyleContext({
values: { '2927408': 500, '2905701': 200 },
fillMode: 'sequential',
highlightKey: '',
selectedKey: null,
})
describe('computeChoroplethMax', () => {
it('uses scaleMax when provided', () => {
expect(computeChoroplethMax({ '2927408': 999 }, 'sequential', 1)).toBe(1)
})
it('derives max from positive values in sequential mode', () => {
expect(computeChoroplethMax({ '2927408': 100, '2905701': 300 }, 'sequential')).toBe(300)
})
it('uses absolute max in diverging mode', () => {
expect(computeChoroplethMax({ '2927408': -40, '2905701': 100 }, 'diverging')).toBe(100)
})
})
describe('canonicalMapKeysKey', () => {
it('sorts keys for stable identity', () => {
expect(canonicalMapKeysKey(['2927408', '2905701'])).toBe('2905701,2927408')
expect(canonicalMapKeysKey(['2905701', '2927408'])).toBe('2905701,2927408')
})
it('returns empty string for empty input', () => {
expect(canonicalMapKeysKey([])).toBe('')
})
})
describe('resolvePathStyle', () => {
it('highlights hovered feature over selected', () => {
const context = {
...baseContext,
selectedKey: '2905701',
hoveredKey: '2927408',
}
expect(resolvePathStyle(context, '2927408').weight).toBe(2)
expect(resolvePathStyle(context, '2905701').weight).toBe(1)
})
it('highlights selected feature when not hovering', () => {
const context = {
...baseContext,
selectedKey: '2905701',
hoveredKey: null,
}
expect(resolvePathStyle(context, '2905701').weight).toBe(2)
})
it('reveals selected highlight after hover clears on a different feature', () => {
const context = {
...baseContext,
selectedKey: '2905701',
hoveredKey: null,
}
expect(resolvePathStyle(context, '2927408').weight).toBe(1)
expect(resolvePathStyle(context, '2905701').weight).toBe(2)
})
it('highlights keys in highlightSet', () => {
const context = {
...baseContext,
highlightSet: new Set(['2927408']),
}
expect(resolvePathStyle(context, '2927408').weight).toBe(2)
})
it('keeps fillOpacity when highlighted', () => {
const base = resolvePathStyle(baseContext, '2927408')
const highlighted = resolvePathStyle({ ...baseContext, hoveredKey: '2927408' }, '2927408')
expect(highlighted.fillOpacity).toBe(base.fillOpacity)
expect(highlighted.color).toBe('#c51414')
})
it('uses diverging fill for negative metrics', () => {
const context = buildLayerStyleContext({
values: { '2927408': -100 },
fillMode: 'diverging',
scaleMax: 200,
highlightKey: '',
selectedKey: null,
})
expect(resolvePathStyle(context, '2927408').fillColor).toMatch(/^rgb\(/)
})
it('returns neutral style for missing feature key', () => {
const style = resolvePathStyle(baseContext, '')
expect(style.fillOpacity).toBe(0.35)
expect(style.weight).toBe(1)
})
})
describe('discrete class fills (B13)', () => {
const classedContext = buildLayerStyleContext({
values: { '2927408': 500, '2905701': 200, '2910800': 0 },
fillMode: 'sequential',
fillByKey: { '2927408': '#111111', '2905701': '#eeeeee' },
highlightKey: '',
selectedKey: null,
})
it('paints the class fill instead of the continuous ramp', () => {
expect(resolvePathStyle(classedContext, '2927408').fillColor).toBe('#111111')
expect(resolvePathStyle(classedContext, '2905701').fillColor).toBe('#eeeeee')
})
it('renders a key without a class as no data, even when it has a value', () => {
const style = resolvePathStyle(classedContext, '2910800')
expect(style.fillColor).toBe('#f4f4f5')
expect(style.fillOpacity).toBe(0.35)
})
it('still highlights hover and selection', () => {
const style = resolvePathStyle({ ...classedContext, hoveredKey: '2927408' }, '2927408')
expect(style.weight).toBe(2)
expect(style.fillColor).toBe('#111111')
})
})
})