Files
sd-park-pass-map/app/utils/location.test.ts
Aram Chia Sarafian 369759a3f2 Enforce unique ZIP/library coordinates and improve test output
- Fix duplicate ZIP coordinates (92121/92130)
- Improve test output to show failing/conflicting values
- Tests now ensure all ZIPs and libraries are unique and non-overlapping
2025-07-13 20:27:22 -07:00

69 lines
2.5 KiB
TypeScript

// If using TypeScript, ensure Jest types are available
// This import is only necessary if your environment does not provide 'expect' globally
import { describe, expect, it } from '@jest/globals';
import { getCoordinatesFromZip, SAN_DIEGO_ZIP_COORDINATES } from './location';
import { LIBRARY_LOCATIONS as LIBRARY_LOCATIONS_OBJ } from '../api/availability/route';
// Convert the object to an array of coordinates for testing
const LIBRARY_LOCATIONS = Object.values(LIBRARY_LOCATIONS_OBJ);
const ZIP_COORDS = Object.entries(SAN_DIEGO_ZIP_COORDINATES || {});
describe('San Diego ZIP and Library Coordinates', () => {
it('all ZIP code lat/lngs are unique', () => {
const seen = new Map();
let duplicate = null;
for (const [zip, coord] of ZIP_COORDS) {
const { lat, lng } = coord as { lat: number; lng: number };
const key = `${lat},${lng}`;
if (seen.has(key)) {
duplicate = { zip, other: seen.get(key), lat, lng };
break;
}
seen.set(key, zip);
}
if (duplicate) {
throw new Error(`Duplicate ZIP coordinates: ${duplicate.zip} and ${duplicate.other} share (${duplicate.lat}, ${duplicate.lng})`);
}
expect(duplicate).toBe(null);
});
it('all library lat/lngs are unique', () => {
const seen = new Map();
let duplicate = null;
for (const [i, coord] of LIBRARY_LOCATIONS.entries()) {
const key = `${coord.lat},${coord.lng}`;
if (seen.has(key)) {
duplicate = { idx: i, other: seen.get(key), lat: coord.lat, lng: coord.lng };
break;
}
seen.set(key, i);
}
if (duplicate) {
throw new Error(`Duplicate library coordinates: index ${duplicate.idx} and ${duplicate.other} share (${duplicate.lat}, ${duplicate.lng})`);
}
expect(duplicate).toBe(null);
});
it('no ZIP code lat/lng matches any library lat/lng', () => {
const librarySet = new Set(LIBRARY_LOCATIONS.map(c => `${c.lat},${c.lng}`));
let conflict = null;
for (const [zip, coord] of ZIP_COORDS) {
const { lat, lng } = coord as { lat: number; lng: number };
const key = `${lat},${lng}`;
if (librarySet.has(key)) {
conflict = { zip, lat, lng };
break;
}
}
if (conflict) {
throw new Error(`ZIP code ${conflict.zip} shares coordinates (${conflict.lat}, ${conflict.lng}) with a library`);
}
expect(conflict).toBe(null);
});
it("'92129' has the expected coordinates", () => {
const coord = getCoordinatesFromZip('92129');
expect(coord).toEqual({ lat: 32.9584, lng: -117.1253 });
});
});