test/unit/services/assets.service.spec.tsaddedimport sinon from "sinon";
import * as chai from "chai";
import chaiAsPromised from "chai-as-promised";
import sinonChai from "sinon-chai";
import * as db from "../../../src/db/db";
import * as assetsService from "../../../src/services/assets.service";
const expect = chai.expect;
chai.use(chaiAsPromised).use(sinonChai);
/* eslint-disable @typescript-eslint/no-explicit-any */
describe("Assets Service unit tests (systemId derivation)", () => {
let queryStub: sinon.SinonStub;
const projectId = "443b4964-09df-4f86-9e98-7bec5a9fcdab";
const assetId = "8b141d85-3305-4bdd-b802-e83639c260e8";
const assetTypeId = "cccccccc-dddd-eeee-ffff-aaaaaaaaaaaa";
const systemTypeId = "66666666-7777-8888-9999-000000000000";
const assetRow = {
AssetId: assetId,
Name: "Chiller Unit 1",
AssetTypeId: assetTypeId,
CreatedBy: "user@example.com",
InsertedOn: new Date("2025-01-01T00:00:00Z"),
LastModifiedOn: null,
LastModifiedBy: null,
Id: 42,
};
const mappingRow = {
AssetTypeSystemTypeMappingId: "11111111-2222-3333-4444-555555555555",
AssetTypeId: assetTypeId,
SystemTypeId: systemTypeId,
CreatedBy: "user@example.com",
InsertedOn: new Date("2025-01-01T00:00:00Z"),
LastModifiedOn: null,
LastModifiedBy: null,
};
beforeEach(() => {
queryStub = sinon.stub(db, "query");
});
afterEach(() => {
sinon.restore();
});
describe("getAssetById", () => {
it("should populate systemId from the asset type mapping", async () => {
queryStub.onCall(0).resolves({ rows: [assetRow] });
queryStub.onCall(1).resolves({ rows: [mappingRow] });
const asset = await assetsService.getAssetById(projectId, assetId);
expect(asset.assetId).to.equal(assetId);
expect(asset.systemId).to.equal(systemTypeId);
});
it("should set systemId to null when no mapping exists", async () => {
queryStub.onCall(0).resolves({ rows: [assetRow] });
queryStub.onCall(1).resolves({ rows: [] });
const asset = await assetsService.getAssetById(projectId, assetId);
expect(asset.systemId).to.equal(null);
});
});
describe("listAssets", () => {
it("should populate systemId for each asset from the mapping lookup", async () => {
queryStub.onCall(0).resolves({ rows: [assetRow] });
queryStub.onCall(1).resolves({ rows: [mappingRow] });
const assets = await assetsService.listAssets(projectId, { lastFetchedIndexId: undefined, size: undefined } as any);
expect(assets).to.have.length(1);
expect(assets[0].systemId).to.equal(systemTypeId);
expect(assets[0].indexId).to.equal(42);
});
it("should set systemId to null for assets with no mapping", async () => {
queryStub.onCall(0).resolves({ rows: [assetRow] });
queryStub.onCall(1).resolves({ rows: [] });
const assets = await assetsService.listAssets(projectId, { lastFetchedIndexId: undefined, size: undefined } as any);
expect(assets[0].systemId).to.equal(null);
});
});
});