import { test } from "node:test"; import assert from "node:assert/strict"; import { hasCoords, resolveAssetLocation } from "./plant-map"; const headquarters = { latitude: -26.10161543177211, longitude: 28.34872485354214 }; const sites = [ { id: "cedar", name: "Cedar Gate", latitude: -25.761766, longitude: 28.33506 }, { id: "hills", name: "The Hills", latitude: -25.8481, longitude: 28.35 }, { id: "west", name: "West Park", latitude: 0, longitude: 0 }, ]; test("assigned vehicles leave the headquarters pin for their saved site", () => { assert.deepEqual(resolveAssetLocation({ ...headquarters, site: "cedar" }, sites), { latitude: -25.761766, longitude: 28.33506, source: "site", siteName: "Cedar Gate", }); }); test("reassigning and editing site coordinates changes the plotted location", () => { const asset = { ...headquarters, site: "hills" }; assert.equal(resolveAssetLocation(asset, sites).latitude, -25.8481); assert.equal(resolveAssetLocation(asset, sites.map(s => s.id === "hills" ? { ...s, latitude: -25.85 } : s)).latitude, -25.85); }); test("an assigned site without a pin does not misrepresent the vehicle at headquarters", () => { for (const site of ["west", "deleted-site"]) { const location = resolveAssetLocation({ ...headquarters, site }, sites); assert.equal(hasCoords(location), false); assert.equal(location.source, "missing-site"); } }); test("removing an assignment restores the vehicle's own saved location", () => { assert.equal(resolveAssetLocation({ ...headquarters, site: "" }, sites).source, "head-office"); assert.deepEqual(resolveAssetLocation({ latitude: -26.2, longitude: 28.1, site: "" }, sites), { latitude: -26.2, longitude: 28.1, source: "manual", siteName: null, }); }); test("cleared coordinates remain unlocated", () => { const location = resolveAssetLocation({ latitude: null, longitude: null, site: "" }, sites); assert.equal(location.source, "unlocated"); assert.equal(hasCoords(location), false); }); test("invalid or incomplete coordinate pairs are never plotted", () => { for (const pair of [ { latitude: null, longitude: 28 }, { latitude: 0, longitude: 0 }, { latitude: NaN, longitude: 28 }, { latitude: -26, longitude: Infinity }, { latitude: -91, longitude: 28 }, { latitude: -26, longitude: 181 }, ]) assert.equal(hasCoords(pair), false); assert.equal(hasCoords({ latitude: 0, longitude: 28 }), true); });