teqo-backup/tests/int/bahiaGeometries.int.spec.ts
Francisco Ignácio Fontoura Solla b8547ff292 Deliver B3 Leaflet choropleth maps and register post-simplify follow-ups.
Add BahiaMap surfaces on nuclei list, detail, and geral dashboard with server-side choropleth aggregation, lazy geometry loading (B5 F1), and roadmap plans for A7 F4, B5 F3, and B6 scale debts.
2026-07-19 17:13:27 -03:00

96 lines
3.8 KiB
TypeScript

// @vitest-environment node
import { statSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import {
loadMunicipalityGeometryModule,
loadTerritoryGeometryModule,
} from '@/lib/bahiaGeometries'
import { bahiaMunicipalityCodes } from '@/lib/bahiaMunicipalityCodes'
import { bahiaIdentityTerritoryRecords } from '@/lib/bahiaTerritories'
/** Soft ceiling against accidental unsimplified meshes (plan target ≤ ~600 KB). */
const MAX_TOPO_BYTES = 600 * 1024
const repoRoot = join(dirname(fileURLToPath(import.meta.url)), '../..')
const hasNonEmptyGeometry = (
geometry: { type: string; coordinates?: unknown } | null | undefined,
): boolean => {
if (!geometry || geometry.type === 'GeometryCollection') return false
if (!Array.isArray(geometry.coordinates) || geometry.coordinates.length === 0) return false
return true
}
describe('Bahia static geometries', () => {
it('keeps committed TopoJSON under the size budget', () => {
const municipalityBytes = statSync(
join(repoRoot, 'src/lib/geometries/bahia-municipalities.topo.json'),
).size
const territoryBytes = statSync(
join(repoRoot, 'src/lib/geometries/bahia-identity-territories.topo.json'),
).size
expect(municipalityBytes).toBeGreaterThan(10_000)
expect(territoryBytes).toBeGreaterThan(1_000)
expect(municipalityBytes).toBeLessThanOrEqual(MAX_TOPO_BYTES)
expect(territoryBytes).toBeLessThanOrEqual(MAX_TOPO_BYTES)
})
it('exposes 417 municipality features whose codarea set matches the code table', async () => {
const { topology, features, getMunicipalityFeature } = await loadMunicipalityGeometryModule()
expect(topology.objects.municipalities.geometries).toHaveLength(417)
expect(features).toHaveLength(417)
const featureCodes = new Set(features.map((entry) => entry.properties.codarea))
const tableCodes = new Set(Object.values(bahiaMunicipalityCodes))
expect(featureCodes).toEqual(tableCodes)
for (const entry of features) {
expect(entry.properties.codarea).toMatch(/^29\d{5}$/)
expect(bahiaMunicipalityCodes[entry.properties.name]).toBe(entry.properties.codarea)
expect(hasNonEmptyGeometry(entry.geometry)).toBe(true)
}
expect(getMunicipalityFeature('2927408')?.properties.name).toBe('Salvador')
expect(getMunicipalityFeature('0000000')).toBeUndefined()
})
it('exposes 27 territory features whose codes match bahiaIdentityTerritoryRecords', async () => {
const { topology, features, getTerritoryFeature } = await loadTerritoryGeometryModule()
expect(topology.objects.territories.geometries).toHaveLength(27)
expect(features).toHaveLength(27)
const featureCodes = features.map((entry) => entry.properties.code).sort()
const recordCodes = bahiaIdentityTerritoryRecords.map((entry) => entry.code).sort()
expect(featureCodes).toEqual(recordCodes)
for (const record of bahiaIdentityTerritoryRecords) {
const featureEntry = getTerritoryFeature(record.code)
expect(featureEntry, record.code).toBeDefined()
expect(featureEntry?.properties.name).toBe(record.name)
expect(hasNonEmptyGeometry(featureEntry?.geometry)).toBe(true)
}
expect(getTerritoryFeature('01')?.properties.name).toBe('Irecê')
expect(getTerritoryFeature('99')).toBeUndefined()
})
it('exposes TopoJSON object names expected by consumers', async () => {
const [municipalityModule, territoryModule] = await Promise.all([
loadMunicipalityGeometryModule(),
loadTerritoryGeometryModule(),
])
expect(municipalityModule.topology.type).toBe('Topology')
expect(territoryModule.topology.type).toBe('Topology')
expect(Object.keys(municipalityModule.topology.objects)).toEqual(['municipalities'])
expect(Object.keys(territoryModule.topology.objects)).toEqual(['territories'])
})
})