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("PersonnelKnown", 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.PersonnelKnown.deleteMany({}); }); afterEach(async function() { await mongoose.disconnect(); }); it("is saved with defaults", async function() { const item = new models.PersonnelKnown({ departmentId: "d1", PersonnelID: "P1", PersonnelName: "John Doe", }); const sut = await item.save(); assert.isNotNull(sut._id); assert.equal(sut.departmentId, "d1"); assert.equal(sut.PersonnelID, "P1"); assert.equal(sut.PersonnelName, "John Doe"); assert.equal(sut.PersonnelRank, ""); assert.equal(sut.PersonnelWorkCode, ""); assert.isFalse(sut.active); assert.isNull(sut.userId); assert.isTrue(sut.uuid !== ""); assert.deepEqual(sut.radios, []); assert.equal(sut.jurisdiction, ""); assert.isNull(sut.agencyId); assert.equal(sut.source, ""); }); it("defines departmentId_1_userId_1_active_partial index in schema", function() { const indexes = models.PersonnelKnown.schema.indexes(); const partialIndex = indexes.find(([fields, options]) => { return ( options?.name === "departmentId_1_userId_1_active_partial" || (fields.departmentId === 1 && fields.userId === 1) ); }); assert.isDefined(partialIndex, "Expected departmentId_1_userId_1_active_partial index to be defined"); const [fields, options] = partialIndex!; assert.deepEqual(fields, { departmentId: 1, userId: 1 }); assert.isTrue(options?.unique); assert.deepEqual(options?.partialFilterExpression, { active: true, userId: { $type: "string" }, }); }); it("enforces partial unique (departmentId, userId) when active and userId is a string", async function() { await models.PersonnelKnown.createIndexes(); await new models.PersonnelKnown({ departmentId: "d1", PersonnelID: "P1", PersonnelName: "User One", userId: "u1", active: true, }).save(); // Same userId in another department is allowed const otherDept = await new models.PersonnelKnown({ departmentId: "d2", PersonnelID: "P1", PersonnelName: "User One", userId: "u1", active: true, }).save(); assert.isNotNull(otherDept._id); // Same userId in same department when active: false is allowed const inactiveSameUser = await new models.PersonnelKnown({ departmentId: "d1", PersonnelID: "P2", PersonnelName: "User One Inactive", userId: "u1", active: false, }).save(); assert.isNotNull(inactiveSameUser._id); // Multiple records with userId: null and active: true are allowed const nullUser1 = await new models.PersonnelKnown({ departmentId: "d1", PersonnelID: "P3", PersonnelName: "User Null 1", userId: null, active: true, }).save(); const nullUser2 = await new models.PersonnelKnown({ departmentId: "d1", PersonnelID: "P4", PersonnelName: "User Null 2", userId: null, active: true, }).save(); assert.isNotNull(nullUser1._id); assert.isNotNull(nullUser2._id); // Duplicate active userId on the same department must fail with E11000 try { await new models.PersonnelKnown({ departmentId: "d1", PersonnelID: "P5", PersonnelName: "User One Duplicate", userId: "u1", active: true, }).save(); assert.isFalse(true, "Expecting duplicate (departmentId, userId) with active: true to fail."); } catch (error) { assert.match((error as Error).message, /E11000/); } }); });