Files
suaveplan/packages/geospatial/logistics/ports/src/distance/distance.test.ts
2026-05-27 23:47:53 +01:00

45 lines
1.6 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import { distanceKm, distanceMetresRaw, distanceNm } from "./distance.js";
const SINGAPORE = { latitude: 1.2644, longitude: 103.8198 };
const ROTTERDAM = { latitude: 51.9, longitude: 4.49 };
describe("distance helpers", () => {
it("distanceNm zero between identical coords", { timeout: 5000 }, () => {
expect(distanceNm(SINGAPORE, SINGAPORE)).toBe(0);
});
it("distanceKm zero between identical coords", { timeout: 5000 }, () => {
expect(distanceKm(SINGAPORE, SINGAPORE)).toBe(0);
});
it("distanceMetresRaw produces unrounded metres", { timeout: 5000 }, () => {
const raw = distanceMetresRaw(SINGAPORE, ROTTERDAM);
expect(raw).toBeGreaterThan(10_000_000);
expect(raw).toBeLessThan(12_000_000);
});
it("Singapore to Rotterdam great-circle is ~5800 nm", {
timeout: 5000,
}, () => {
const nm = distanceNm(SINGAPORE, ROTTERDAM);
expect(nm).toBeGreaterThan(5600);
expect(nm).toBeLessThan(6000);
});
it("Singapore to Rotterdam great-circle is ~10800 km", {
timeout: 5000,
}, () => {
const km = distanceKm(SINGAPORE, ROTTERDAM);
expect(km).toBeGreaterThan(10500);
expect(km).toBeLessThan(11200);
});
it("nm * 1.852 ≈ km within 1% tolerance", { timeout: 5000 }, () => {
const nm = distanceNm(SINGAPORE, ROTTERDAM);
const km = distanceKm(SINGAPORE, ROTTERDAM);
const expected = nm * 1.852;
expect(Math.abs(km - expected) / km).toBeLessThan(0.01);
});
});