import { assert } from "chai"; import { describe, it, beforeEach, afterEach } from "node:test"; import * as m from "../index"; import * as config from "./config"; import mockModule from "./mock"; describe("AudioStreamAuthentication", 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.audioStreamAuthentication; await mock.beforeEach(); }); afterEach(async function() { await mongoose.disconnect(); }); it("is saved", async function() { assert.isObject(testItem); const item = new models.AudioStreamAuthentication(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, "test-stream"); assert.equal(sut.rotateInterval, 3600); assert.isObject(sut.secret); assert.equal(sut.secret?.iv, testItem.secret?.iv); assert.equal(sut.secret?.encryptedData, testItem.secret?.encryptedData); }); it("enforces unique name constraint", async function() { const item1 = new models.AudioStreamAuthentication(testItem); await item1.save(); const item2 = new models.AudioStreamAuthentication({ name: "test-stream", secret: { iv: "different-iv", encryptedData: "different-data" }, rotateInterval: 7200, }); try { await item2.save(); assert.fail("Should have thrown duplicate key error"); } catch (error) { assert.include(String((error as Error).message), "duplicate key"); } }); it("allows different names", async function() { const item1 = new models.AudioStreamAuthentication(testItem); await item1.save(); const item2 = new models.AudioStreamAuthentication({ name: "different-stream", secret: { iv: "different-iv", encryptedData: "different-data" }, rotateInterval: 7200, }); const sut = await item2.save(); assert.isNotNull(sut._id); assert.equal(sut.name, "different-stream"); assert.equal(sut.rotateInterval, 7200); }); });