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"; describe("TrackerResponder", function() { let models: m.BackendModels, mongoose: m.MongooseModule; beforeEach(async function() { const c = await m.connect(config.url); models = c.models; mongoose = c.mongoose; const mock = mockModule({ mongoose }); await mock.beforeEach(); await models.TrackerResponder.deleteMany({}); }); afterEach(async function() { await mongoose.disconnect(); }); it("is saved with defaults", async function() { const item = new models.TrackerResponder({ departmentId: "d1", mac: "aa:bb:cc:dd:ee:01", }); const sut = await item.save(); assert.isNotNull(sut._id); assert.equal(sut.departmentId, "d1"); assert.equal(sut.mac, "AA:BB:CC:DD:EE:01"); assert.isTrue(sut.uuid !== ""); assert.equal(sut.trackerPersonnelId, ""); assert.equal(sut.personnelName, ""); assert.equal(sut.friendlyName, ""); assert.equal(sut.displayName, ""); assert.equal(sut.seatPosition, ""); assert.equal(sut.personnelUUID, ""); assert.equal(sut.assignedBy, ""); assert.isNull(sut.assignedAt); assert.isTrue(sut.active); assert.equal(sut.source, ""); assert.instanceOf(sut.createdAt, Date); assert.instanceOf(sut.modified, Date); }); it("persists vendor and user-owned fields", async function() { const item = new models.TrackerResponder({ departmentId: "d1", mac: "AA:BB:CC:DD:EE:02", trackerPersonnelId: "12345", personnelName: "Fire Hose 1", friendlyName: "FH-1", displayName: "Engine 1 Nozzle", seatPosition: "Fire Hose 1", personnelUUID: "pk-uuid-1", assignedBy: "user-1", assignedAt: new Date("2026-07-23T00:00:00.000Z"), source: "TXRX", }); const sut = await item.save(); assert.equal(sut.trackerPersonnelId, "12345"); assert.equal(sut.personnelName, "Fire Hose 1"); assert.equal(sut.friendlyName, "FH-1"); assert.equal(sut.displayName, "Engine 1 Nozzle"); assert.equal(sut.seatPosition, "Fire Hose 1"); assert.equal(sut.personnelUUID, "pk-uuid-1"); assert.equal(sut.assignedBy, "user-1"); assert.equal(sut.assignedAt?.toISOString(), "2026-07-23T00:00:00.000Z"); assert.equal(sut.source, "TXRX"); }); it("enforces unique (departmentId, mac)", async function() { await models.TrackerResponder.createIndexes(); await new models.TrackerResponder({ departmentId: "d1", mac: "AA:BB:CC:DD:EE:03", }).save(); // Same MAC in another department is allowed const other = await new models.TrackerResponder({ departmentId: "d2", mac: "AA:BB:CC:DD:EE:03", }).save(); assert.isNotNull(other._id); try { await new models.TrackerResponder({ departmentId: "d1", mac: "aa:bb:cc:dd:ee:03", }).save(); assert.isFalse(true, "Expecting duplicate (departmentId, mac) to fail."); } catch (error) { assert.match((error as Error).message, /E11000/); } }); it("enforces unique (departmentId, uuid)", async function() { await models.TrackerResponder.createIndexes(); const first = await new models.TrackerResponder({ departmentId: "d1", mac: "AA:BB:CC:DD:EE:04", }).save(); // Same uuid in another department is allowed const other = await new models.TrackerResponder({ departmentId: "d2", mac: "AA:BB:CC:DD:EE:04", uuid: first.uuid, }).save(); assert.isNotNull(other._id); try { await new models.TrackerResponder({ departmentId: "d1", mac: "AA:BB:CC:DD:EE:05", uuid: first.uuid, }).save(); assert.isFalse(true, "Expecting duplicate (departmentId, uuid) to fail."); } catch (error) { assert.match((error as Error).message, /E11000/); } }); it("rejects an empty or null uuid", async function() { for (const uuid of ["", null]) { try { await new models.TrackerResponder({ departmentId: "d1", mac: "AA:BB:CC:DD:EE:06", uuid, }).save(); assert.isFalse(true, `Expecting uuid ${JSON.stringify(uuid)} to fail validation.`); } catch (error) { assert.match((error as Error).message, /uuid.*required/i); } } }); });