import { assert } from "chai"; import { describe, it, beforeEach, afterEach } from "node:test"; import * as m from "../index"; import * as config from "./config"; describe("KindTypeCatalog", function() { let models: m.BackendModels, mongoose: m.MongooseModule; beforeEach(async function() { const c = await m.connect(config.url); models = c.models; mongoose = c.mongoose; await models.KindTypeCatalog.syncIndexes(); }); afterEach(async function() { await models.KindTypeCatalog.deleteMany({}); await mongoose.disconnect(); }); it("is saved with a colorization", async function() { const sut = await new models.KindTypeCatalog({ kind: "engine", type: "1", locationType: "vehicle", colorization: { background: "#0000FF", text: "#FFFFFF" }, }).save(); assert.equal(sut.kind, "engine"); assert.equal(sut.type, "1"); assert.equal(sut.locationType, "vehicle"); assert.equal(sut.colorization?.background, "#0000FF"); assert.isTrue(sut.active); }); it("enforces a unique (kind, type) index", async function() { await new models.KindTypeCatalog({ kind: "engine", type: "1", locationType: "vehicle" }).save(); let err: unknown = null; try { await new models.KindTypeCatalog({ kind: "engine", type: "1", locationType: "vehicle" }).save(); } catch (e) { err = e; } assert.isNotNull(err, "expected duplicate (kind,type) to be rejected"); assert.include(String((err as Error).message), "duplicate key"); }); it("allows the same kind with a different type", async function() { await new models.KindTypeCatalog({ kind: "engine", type: "1", locationType: "vehicle" }).save(); const sut = await new models.KindTypeCatalog({ kind: "engine", type: "2", locationType: "vehicle" }).save(); assert.equal(sut.type, "2"); }); it("requires a non-empty kind", async function() { let err: unknown = null; try { await new models.KindTypeCatalog({ type: "1", locationType: "vehicle" }).save(); } catch (e) { err = e; } assert.isNotNull(err, "expected a missing kind to be rejected"); assert.include(String((err as Error).message), "kind"); }); });