import { assert } from "chai"; import { describe, it, beforeEach, afterEach } from "node:test"; import { rejects as assertRejects } from "node:assert/strict"; import * as m from "../index"; import * as config from "./config"; import mockModule from "./mock"; describe("Esri", 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.esri; await mock.beforeEach(); }); afterEach(async function() { await mongoose.disconnect(); }); it("is saved", async function() { assert.isObject(testItem); const item = new models.Esri(testItem); const sut = await item.save(); assert.isNotNull(sut._id); assert.isNotNull(sut.id); assert.equal(sut._id.toString(), testItem._id?.toString()); assert.equal(sut.departmentId, testItem.departmentId); assert.equal(sut.auth.username, testItem.auth?.username); assert.equal(sut.auth.encrypted.iv, testItem.auth?.encrypted.iv); assert.equal(sut.auth.encrypted.encryptedData, testItem.auth?.encrypted.encryptedData); assert.equal(sut.maps.length, 1); const sutm = sut.maps[0]; let testItemM; if (testItem.maps?.length) { testItemM = testItem.maps[0]; } assert.equal(sutm?.itemId, testItemM?.itemId); assert.equal(sutm?.title, testItemM?.title); assert.equal(sutm?.url, testItemM?.url); assert.equal(sutm?.access, testItemM?.access); assert.equal(sutm?.owner, testItemM?.owner); assert.equal(sutm?.type, testItemM?.type); assert.equal(sutm?.mapLayers.length, 3); assert.equal(sutm?.mapLayers.length, 3); assert.equal(sutm?.baseMap.baseMapLayers.length, 1); assert.equal(sutm?.baseMap.title, testItemM?.baseMap.title); const itemId = "01cebf7aede94ed3a9f4c260e18a7d7e"; // compare saved items assert.equal(sut.mapsProperties.length, 1, "all items"); const mapPropsFound = sut.mapsProperties.filter(p => { return p.itemId === itemId; }); assert.equal(mapPropsFound.length, 1, "found items"); const props = mapPropsFound[0]; assert.equal(props?.download, true); }); it("enforces unique arcGISAuth.username constraint", async function() { await new models.Esri({ departmentId: new mongoose.Types.ObjectId(), arcGISAuth: { username: "tc_dup_user" }, }).save(); // A second department claiming the same non-empty username must be rejected. // assertRejects fails if the save does NOT reject, so there is no false-positive. await assertRejects( new models.Esri({ departmentId: new mongoose.Types.ObjectId(), arcGISAuth: { username: "tc_dup_user" }, }).save(), /duplicate key/ ); }); it("normalizes an empty arcGISAuth.username to null", async function() { // EsriAuth.username defaults to "" (schema shared with auth); an arcGISAuth // present without a real username collapses to null so it is never indexed. const saved = await new models.Esri({ departmentId: new mongoose.Types.ObjectId(), arcGISAuth: { username: "" }, }).save(); assert.isNull(saved.arcGISAuth); }); it("allows multiple missing/empty arcGISAuth.username and distinct values", async function() { // missing arcGISAuth entirely (defaults to null) — not indexed await new models.Esri({ departmentId: new mongoose.Types.ObjectId() }).save(); await new models.Esri({ departmentId: new mongoose.Types.ObjectId() }).save(); // empty-string username — normalized to null by the pre-save hook, so not indexed. // Both must coexist: without normalization the second would collide on "" under $exists. const empty1 = await new models.Esri({ departmentId: new mongoose.Types.ObjectId(), arcGISAuth: { username: "" } }).save(); const empty2 = await new models.Esri({ departmentId: new mongoose.Types.ObjectId(), arcGISAuth: { username: "" } }).save(); assert.isNull(empty1.arcGISAuth); assert.isNull(empty2.arcGISAuth); // distinct non-empty usernames — allowed await new models.Esri({ departmentId: new mongoose.Types.ObjectId(), arcGISAuth: { username: "tc_user_a" } }).save(); await new models.Esri({ departmentId: new mongoose.Types.ObjectId(), arcGISAuth: { username: "tc_user_b" } }).save(); const count = await models.Esri.countDocuments({}); assert.equal(count, 6); }); });