← back to Homesonspec
packages/shared/src/geo.test.ts
46 lines
import { describe, expect, it } from "vitest";
import { bboxAround, haversineMiles, latLonInState, stateForZip } from "./geo";
describe("bboxAround", () => {
it("produces a symmetric box around the center", () => {
const box = bboxAround(30.3, -97.7, 10);
expect((box.minLat + box.maxLat) / 2).toBeCloseTo(30.3, 6);
expect((box.minLon + box.maxLon) / 2).toBeCloseTo(-97.7, 6);
expect(box.maxLat - box.minLat).toBeCloseTo(20 / 69, 3);
});
});
describe("haversineMiles", () => {
it("Austin to Dallas is ~182 miles", () => {
const d = haversineMiles(30.2672, -97.7431, 32.7767, -96.797);
expect(d).toBeGreaterThan(170);
expect(d).toBeLessThan(195);
});
it("zero distance for identical points", () => {
expect(haversineMiles(30, -97, 30, -97)).toBe(0);
});
});
describe("latLonInState", () => {
it("Austin is in TX", () => {
expect(latLonInState(30.2672, -97.7431, "TX")).toBe(true);
});
it("Phoenix is not in TX", () => {
expect(latLonInState(33.4484, -112.074, "TX")).toBe(false);
});
it("unknown state returns null (cannot verify, not failure)", () => {
expect(latLonInState(30, -97, "XX")).toBeNull();
});
});
describe("stateForZip", () => {
it("787xx is TX, 850xx is AZ, 275xx is NC", () => {
expect(stateForZip("78701")).toBe("TX");
expect(stateForZip("85001")).toBe("AZ");
expect(stateForZip("27513")).toBe("NC");
});
it("unknown prefix returns null", () => {
expect(stateForZip("99999")).toBeNull();
});
});