import { assert } from "chai"; import { describe, it, beforeEach, afterEach } from "node:test"; import { isDate } from "lodash"; import * as m from "../index"; import * as config from "./config"; import mockModule from "./mock"; describe("GlobalSettings", 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.globalSettings; await mock.beforeEach(); }); afterEach(async function() { await mongoose.disconnect(); }); it("is saved", async function() { assert.isObject(testItem); const item = new models.GlobalSettings(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.name, "global"); // Hardcoded assert.isTrue(isDate(sut.updatedAt)); assert.isObject(sut.arcgisApiKey); assert.isTrue(isDate(sut.arcgisApiKey.expireAt)); assert.equal(sut.arcgisApiKey?.expireAt?.toISOString(), "2025-12-12T01:02:03.000Z"); assert.isObject(sut.arcgisApiKey?.item); assert.equal(sut.arcgisApiKey?.item?.iv, testItem.arcgisApiKey?.item?.iv); assert.equal(sut.arcgisApiKey?.item?.encryptedData, testItem.arcgisApiKey?.item?.encryptedData); assert.isObject(sut.zonehavenToken); assert.isTrue(isDate(sut.zonehavenToken.expireAt)); assert.equal(sut.zonehavenToken?.expireAt?.toISOString(), "2025-12-12T01:02:03.000Z"); assert.equal(sut.zonehavenToken?.token, testItem.zonehavenToken?.token); assert.equal(sut.zonehavenToken?.referer, testItem.zonehavenToken?.referer); assert.isObject(sut.watchDutySync); assert.isTrue(isDate(sut.watchDutySync.lastModifiedAt)); assert.equal(sut.watchDutySync?.lastModifiedAt?.toISOString(), "2025-11-11T04:05:06.000Z"); assert.isObject(sut.watchDutyApiKey?.item); assert.equal(sut.watchDutyApiKey?.item?.iv, testItem.watchDutyApiKey?.item?.iv); assert.equal(sut.watchDutyApiKey?.item?.encryptedData, testItem.watchDutyApiKey?.item?.encryptedData); }); it("leaves watchDutyApiKey unset on a document saved without it", async function() { const legacyItem = { ...testItem }; delete legacyItem.watchDutyApiKey; const item = new models.GlobalSettings(legacyItem); const sut = await item.save(); const stored = await models.GlobalSettings.findById(sut._id).lean(); assert.isObject(stored); assert.isUndefined(stored?.watchDutyApiKey); }); });