mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-24 00:53:05 +00:00
Card "Onde estou" no dashboard staff: point-in-polygon contra a malha municipal que o painel do mapa ja carrega, sem artefato de centroides, sem migration, sem collection, sem server action e sem Consent -- a posicao e lida por navigator.geolocation e casada no browser, nunca sai do aparelho. - src/lib/municipalityProximity.ts: haversine, ray casting, centroide ponderado por area e uniao discriminada (inScope | zoneCity | outOfScope | outsideBahia) - src/utilities/campaignGeolocation.ts: flag de sessao, leitura guardada de permissao e requestCurrentPosition com falha tipada - Salvador nao ganha ZE chutada: link para a lista filtrada, com href serializado no servidor (importar o serializador canonico no cliente custava 21 kB de First Load JS) - fallback fora da carteira ordenado por centroide, teto de 150 km - fix grosseiro (>10 km) mostra o resultado com ressalva de precisao /campanha First Load JS: 265 -> 268 kB.
180 lines
7.3 KiB
TypeScript
180 lines
7.3 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'
|
|
import { municipalityCatalog } from '@/lib/municipalityCatalog'
|
|
import {
|
|
featureCentroid,
|
|
findContainingMunicipality,
|
|
resolveNearbyMunicipality,
|
|
} from '@/lib/municipalityProximity'
|
|
|
|
/** 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()
|
|
})
|
|
|
|
/**
|
|
* B14 resolves "onde estou" against this same mesh, so the containment used in
|
|
* the field is pinned here with real coordinates — the synthetic squares in
|
|
* tests/unit/municipalityProximity cover the algorithm, not the data.
|
|
*/
|
|
describe('municipality containment (B14)', () => {
|
|
const cityCentres: ReadonlyArray<{ city: string; lat: number; lng: number }> = [
|
|
{ city: 'Salvador', lat: -12.973, lng: -38.5121 },
|
|
{ city: 'Feira de Santana', lat: -12.2664, lng: -38.9663 },
|
|
{ city: 'Vitória da Conquista', lat: -14.8615, lng: -40.8442 },
|
|
{ city: 'Seabra', lat: -12.4172, lng: -41.7702 },
|
|
]
|
|
|
|
it('places known city centres in their own município', async () => {
|
|
const { features } = await loadMunicipalityGeometryModule()
|
|
|
|
for (const { city, lat, lng } of cityCentres) {
|
|
const containing = findContainingMunicipality(features, { lat, lng })
|
|
expect(containing?.properties.name, city).toBe(city)
|
|
}
|
|
})
|
|
|
|
it('resolves nothing outside the state', async () => {
|
|
const { features } = await loadMunicipalityGeometryModule()
|
|
|
|
// Atlantic off Salvador and downtown São Paulo.
|
|
expect(findContainingMunicipality(features, { lat: -13, lng: -37.5 })).toBeUndefined()
|
|
expect(findContainingMunicipality(features, { lat: -23.55, lng: -46.63 })).toBeUndefined()
|
|
})
|
|
|
|
it('keeps every centroid inside its own bounding box', async () => {
|
|
const { features } = await loadMunicipalityGeometryModule()
|
|
|
|
for (const entry of features) {
|
|
const centroid = featureCentroid(entry)
|
|
const rings =
|
|
entry.geometry.type === 'Polygon'
|
|
? [entry.geometry.coordinates]
|
|
: entry.geometry.coordinates
|
|
const positions = rings.flat(2)
|
|
const longitudes = positions.map(([lng]) => lng)
|
|
const latitudes = positions.map(([, lat]) => lat)
|
|
|
|
expect(centroid.lng, entry.properties.name).toBeGreaterThanOrEqual(Math.min(...longitudes))
|
|
expect(centroid.lng, entry.properties.name).toBeLessThanOrEqual(Math.max(...longitudes))
|
|
expect(centroid.lat, entry.properties.name).toBeGreaterThanOrEqual(Math.min(...latitudes))
|
|
expect(centroid.lat, entry.properties.name).toBeLessThanOrEqual(Math.max(...latitudes))
|
|
}
|
|
})
|
|
|
|
it('sends a Salvador position to the zone list and an out-of-portfolio one to the nearest', async () => {
|
|
const geometry = await loadMunicipalityGeometryModule()
|
|
const salvadorZones = municipalityCatalog
|
|
.filter((entry) => entry.city === 'Salvador')
|
|
.map(({ slug, name, ibgeCode }) => ({ slug, name, ibgeCode }))
|
|
const seabra = municipalityCatalog.find((entry) => entry.slug === 'seabra')!
|
|
const salvadorPoint = { lat: -12.973, lng: -38.5121 }
|
|
|
|
expect(
|
|
resolveNearbyMunicipality({
|
|
point: salvadorPoint,
|
|
geometry,
|
|
accessible: salvadorZones,
|
|
}),
|
|
).toEqual({
|
|
kind: 'zoneCity',
|
|
city: 'Salvador',
|
|
ibgeCode: '2927408',
|
|
zoneCount: salvadorZones.length,
|
|
})
|
|
|
|
expect(
|
|
resolveNearbyMunicipality({
|
|
point: salvadorPoint,
|
|
geometry,
|
|
accessible: [{ slug: seabra.slug, name: seabra.name, ibgeCode: seabra.ibgeCode }],
|
|
}),
|
|
).toEqual({ kind: 'outOfScope', city: 'Salvador', nearestInScope: null })
|
|
})
|
|
})
|
|
|
|
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'])
|
|
})
|
|
})
|