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("ZonehavenAccount", 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.zonehavenAccount; await mock.beforeEach(); }); afterEach(async function() { await mongoose.disconnect(); }); it("is saved", async function() { assert.isObject(testItem); const item = new models.ZonehavenAccount(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, "default"); assert.equal(sut.url, testItem.url); assert.equal(sut.username, testItem.username); assert.isObject(sut.password); assert.equal(sut.password?.iv, testItem.password?.iv); assert.equal(sut.password?.encryptedData, testItem.password?.encryptedData); assert.equal(sut.token, testItem.token); assert.isTrue(isDate(sut.expireAt)); assert.equal(sut.expireAt?.toISOString(), testItem.expireAt?.toISOString()); assert.equal(sut.referer, testItem.referer); }); it("enforces a unique name", async function() { const item = new models.ZonehavenAccount(testItem); await item.save(); const duplicate = new models.ZonehavenAccount({ ...testItem, _id: new mongoose.Types.ObjectId() }); try { await duplicate.save(); assert.fail("Should have thrown duplicate key error"); } catch (error) { assert.include(String((error as Error).message), "duplicate key"); } }); it("does not allow changing name after creation", async function() { const item = new models.ZonehavenAccount(testItem); const sut = await item.save(); sut.name = "demo"; const saved = await sut.save(); assert.equal(saved.name, "default"); }); });