freshyo/apps/backend/tests/unit/region-address-rebalance.test.ts
2026-10-26 21:57:05 +05:30

85 lines
2.3 KiB
TypeScript

/**
* Unit tests for the pure coordinate -> bound assignment used after a
* superadmin edits a bound. Persisting the result writes only `bound_id`; the
* integration test SREG-18b covers the end-to-end API behaviour, including
* preserving `vague_backfilled_coords`.
*/
import { assignAddressesToBounds } from '@/src/lib/region-geo'
function square(lat: number, lng: number, delta = 0.01) {
return {
type: 'FeatureCollection' as const,
features: [
{
type: 'Feature' as const,
properties: {},
geometry: {
type: 'Polygon' as const,
coordinates: [
[
[lng - delta, lat - delta],
[lng + delta, lat - delta],
[lng + delta, lat + delta],
[lng - delta, lat + delta],
[lng - delta, lat - delta],
],
],
},
},
],
}
}
describe('assignAddressesToBounds', () => {
it('assigns points to the first priority-ordered bound that contains them', () => {
const assignments = assignAddressesToBounds(
[
{ id: 10, geojson: square(16.75, 78.0, 0.05) },
{ id: 20, geojson: square(16.75, 78.0, 0.01) },
],
[
// Inside both: the earlier (lowest-numbered priority) bound wins.
{ id: 1, latitude: 16.75, longitude: 78.0 },
// Inside only the outer bound.
{ id: 2, latitude: 16.79, longitude: 78.0 },
]
)
expect(assignments).toEqual([
{ id: 1, boundId: 10 },
{ id: 2, boundId: 10 },
])
})
it('leaves a point unassigned when it is outside every bound', () => {
expect(
assignAddressesToBounds(
[{ id: 7, geojson: square(16.75, 78.0) }],
[{ id: 99, latitude: 10.0, longitude: 70.0 }]
)
).toEqual([{ id: 99, boundId: null }])
})
it('keeps assignment order and ignores bounds with no usable polygons', () => {
const empty = {
type: 'FeatureCollection' as const,
features: [],
}
const assignments = assignAddressesToBounds(
[
{ id: 1, geojson: empty },
{ id: 2, geojson: square(12.0, 79.0) },
],
[
{ id: 5, latitude: 12.0, longitude: 79.0 },
{ id: 6, latitude: 11.0, longitude: 79.0 },
]
)
expect(assignments).toEqual([
{ id: 5, boundId: 2 },
{ id: 6, boundId: null },
])
})
})