import { assert } from "chai"; import { describe, it, beforeEach, afterEach } from "node:test"; import * as m from "../index"; import * as config from "./config"; describe("TCardScheme", 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.TCardScheme.syncIndexes(); }); afterEach(async function() { await models.TCardScheme.deleteMany({}); await mongoose.disconnect(); }); const tan = { name: "Tan", tcardNormalized: "219-8", color: { background: "#aa00ff", text: "#CCFF11" } }; const rose = { name: "Rose", tcardNormalized: "219-3", color: { background: "#ff007f", text: "#000000" } }; const national = { countryCode: "US", tcardSchemeType: m.TCardSchemeKind.National, tcardScheme: [tan, rose], }; async function saveError(item: Record): Promise { try { await new models.TCardScheme(item).save(); } catch (e) { return e as Error; } return null; } it("is saved", async function() { const sut = await new models.TCardScheme(national).save(); assert.equal(sut.countryCode, "US"); assert.equal(sut.departmentId, ""); assert.equal(sut.tcardSchemeType, "national"); assert.instanceOf(sut.modified, Date); const [first, second] = sut.toObject().tcardScheme.map((tcard) => ({ ...tcard, _id: tcard._id.toString() })); assert.match(first?._id ?? "", /^[a-f0-9]{24}$/); assert.notEqual(first?._id, second?._id); assert.deepEqual([first, second], [{ ...tan, _id: first?._id }, { ...rose, _id: second?._id }]); }); it("trims and uppercases fields", async function() { const sut = await new models.TCardScheme({ ...national, countryCode: " ca ", tcardScheme: [{ ...tan, name: " Tan ", tcardNormalized: " 219-8 " }], }).save(); assert.equal(sut.countryCode, "CA"); assert.equal(sut.tcardScheme[0]?.name, "Tan"); assert.equal(sut.tcardScheme[0]?.tcardNormalized, "219-8"); }); it("saves a custom scheme for a department", async function() { const sut = await new models.TCardScheme({ countryCode: "US", departmentId: "5195426cc4e017a988000965", tcardSchemeType: m.TCardSchemeKind.Custom, tcardScheme: [{ ...tan, name: "Trucks" }], }).save(); assert.equal(sut.tcardSchemeType, "custom"); assert.equal(sut.departmentId, "5195426cc4e017a988000965"); }); it("requires a departmentId for a custom scheme", async function() { const err = await saveError({ ...national, tcardSchemeType: m.TCardSchemeKind.Custom }); assert.isNotNull(err, "expected a custom scheme without departmentId to be rejected"); assert.include(String(err?.message), "departmentId"); }); it("advances modified on update and stores no createdAt", async function() { const staleDate = new Date("2020-01-02T03:04:05.000Z"); const saved = await new models.TCardScheme(national).save(); await models.TCardScheme.collection.updateOne({ _id: saved._id }, { $set: { modified: staleDate } }); await models.TCardScheme.findOneAndUpdate({ _id: saved._id }, { $set: { tcardScheme: [tan] } }); const raw = await models.TCardScheme.collection.findOne({ _id: saved._id }); assert.isNotNull(raw); assert.notProperty(raw, "createdAt"); assert.notProperty(raw, "updatedAt"); assert.isAbove((raw?.modified as Date).getTime(), staleDate.getTime()); }); it("rejects an unknown tcardSchemeType", async function() { const err = await saveError({ ...national, tcardSchemeType: "regional" }); assert.isNotNull(err); assert.include(String(err?.message), "tcardSchemeType"); }); it("rejects an invalid countryCode", async function() { const err = await saveError({ ...national, countryCode: "USA" }); assert.isNotNull(err); assert.include(String(err?.message), "countryCode"); }); for (const field of ["name", "tcardNormalized", "color"]) { it(`requires a tcard ${field}`, async function() { const tcard: Record = { ...tan }; delete tcard[field]; const err = await saveError({ ...national, tcardScheme: [tcard] }); assert.isNotNull(err, `expected a tcard without ${field} to be rejected`); assert.include(String(err?.message), `tcardScheme.0.${field}`); }); } it("enforces one national scheme per country", async function() { await new models.TCardScheme(national).save(); const err = await saveError({ ...national, tcardScheme: [] }); assert.isNotNull(err, "expected a second national scheme for US to be rejected"); assert.include(String(err?.message), "countryCode_1_national_unique"); await new models.TCardScheme({ ...national, countryCode: "CA" }).save(); }); it("enforces one custom scheme per department", async function() { const custom = { ...national, tcardSchemeType: m.TCardSchemeKind.Custom, departmentId: "5195426cc4e017a988000965" }; await new models.TCardScheme(national).save(); await new models.TCardScheme(custom).save(); const err = await saveError({ ...custom, countryCode: "CA" }); assert.isNotNull(err, "expected a second custom scheme for the department to be rejected"); assert.include(String(err?.message), "departmentId_1_custom_unique"); await new models.TCardScheme({ ...custom, departmentId: "5195426cc4e017a988000966" }).save(); }); });