import { assert } from "chai"; import { describe, it, beforeEach, afterEach } from "node:test"; import * as m from "../index"; import * as config from "./config"; import mockModule from "./mock"; import { decodeLocationVisibility } from "../models/location"; import { LocationVisibility } from "../constants"; describe("Location", function() { let models: m.BackendModels, mongoose: m.MongooseModule; let testItem: Partial; beforeEach(async function() { const c = await m.connect(config.url); models = c.models; mongoose = c.mongoose; const mock = mockModule({ mongoose }); testItem = mock.location; await mock.beforeEach(); }); afterEach(async function() { await mongoose.disconnect(); }); it("is saved", async function() { const item = new models.Location(testItem); const sut = await item.save(); assert.isNotNull(testItem._id); assert.equal(testItem.departmentId, sut.departmentId); assert.equal(testItem.userId, sut.userId); assert.equal(testItem.uuid, sut.uuid); assert.equal(testItem.username, sut.username); assert.equal(testItem.device_type, sut.device_type); assert.equal(testItem.session, sut.session); assert.equal(testItem.active, sut.active); assert.equal(testItem.heading, sut.heading); assert.equal(testItem.locationGeoJSON?.coordinates[0], sut.location?.longitude); assert.equal(testItem.locationGeoJSON?.coordinates[1], sut.location?.latitude); assert.equal(testItem.locationGeoJSON?.type, "Point"); assert.equal(testItem.locationGeoJSON?.coordinates[0], sut.locationGeoJSON.coordinates[0]); assert.equal(testItem.locationGeoJSON?.coordinates[1], sut.locationGeoJSON.coordinates[1]); assert.equal(testItem.opAreaCode, sut.opAreaCode); assert.equal(testItem.opAreaName, sut.opAreaName); assert.equal(testItem.shared, sut.shared); assert.equal(testItem.state, sut.state); assert.equal(testItem.sendToCAD, sut.sendToCAD); assert.equal(testItem.color?.text, sut.color.text); assert.equal(testItem.color?.background, sut.color.background); assert.isTrue(sut.uuid !== ""); assert.equal(testItem.personnelUUID, sut.personnelUUID); const { longitude, latitude } = sut.location ?? { latitude: 0, longitude: 0 }; const maxDistance = 10000; const geoQuery = { locationGeoJSON: { $nearSphere: { $geometry: { type: "Point", coordinates: [longitude, latitude], }, $maxDistance: maxDistance } } }; // For the try/catch test await models.Location.collection.dropIndexes(); try { await models.Location.findOne(geoQuery); assert.isFalse(true, "Expecting above to fail."); } catch (error) { assert.isNotNull(error, "Expecting above to fail"); } // Restore indexes await models.Location.createIndexes(); // In test/dev env this query fails without hint. It seems to work fine in production... const found = await models.Location.findOne(geoQuery, {}, { hint: "shared_1_modified_1_movedAt_1_locationGeoJSON_2dsphere" }); assert.isObject(found); }); it("decodes .visibility", async function() { const item = new models.Location(testItem); const sut = await item.save(); const sutVis = decodeLocationVisibility(sut.visibility); assert.equal(sutVis.length, 4); assert.notEqual(sutVis.indexOf(LocationVisibility.Hidden), -1); assert.notEqual(sutVis.indexOf(LocationVisibility.Visible), -1); assert.notEqual(sutVis.indexOf(LocationVisibility.CAD), -1); assert.notEqual(sutVis.indexOf(LocationVisibility.Shared), -1); }); it("persists kind, type, locationType and kindColor", async function() { const item = new models.Location({ departmentId: "d1", userId: "u1", username: "E1", device_type: "ipad", kind: "engine", type: "1", locationType: "vehicle", kindColor: { background: "#0000FF", text: "#FFFFFF" }, }); const sut = await item.save(); assert.equal(sut.kind, "engine"); assert.equal(sut.type, "1"); assert.equal(sut.locationType, "vehicle"); assert.equal(sut.kindColor?.background, "#0000FF"); assert.equal(sut.kindColor?.text, "#FFFFFF"); }); it("defaults transponderUUID to empty and persists it", async function() { const empty = await new models.Location({ departmentId: "d1", userId: "u1", username: "E3", device_type: "ipad", }).save(); assert.equal(empty.transponderUUID, ""); const tracked = await new models.Location({ departmentId: "d1", userId: "TXRX-tr-uuid-1", username: "Fire Hose 1", device_type: "cad", kindType: "person", locationType: "person", transponderUUID: "tr-uuid-1", }).save(); assert.equal(tracked.transponderUUID, "tr-uuid-1"); assert.equal(tracked.personnelUUID, ""); }); it("does not auto-stamp kindColorChangedAt", async function() { const item = new models.Location({ departmentId: "d1", userId: "u1", username: "E2", device_type: "ipad", kindColor: { background: "#0000FF", text: "#FFFFFF" }, }); const sut = await item.save(); assert.isUndefined(sut.kindColorChangedAt); }); });