Files
leistungsbilanz-ts/tests/project-device-schema.test.ts
T

91 lines
2.4 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
createProjectDeviceCommandSchema,
createProjectDeviceSchema,
projectDeviceStructureCommandSchema,
updateProjectDeviceCommandSchema,
} from "../src/shared/validation/project-device.schemas.js";
describe("project device circuit-first schema", () => {
it("accepts circuit device fields", () => {
const result = createProjectDeviceSchema.safeParse({
name: "E-Line Pro",
displayName: "Office lighting",
phaseType: "single_phase",
connectionKind: "fixed",
costGroup: "440",
category: "Lighting",
quantity: 6,
powerPerUnit: 0.04,
simultaneityFactor: 0.8,
cosPhi: 0.95,
remark: "DALI",
});
assert.equal(result.success, true);
});
it("requires the circuit-first power and phase fields", () => {
const result = createProjectDeviceSchema.safeParse({
name: "Legacy device",
displayName: "Legacy device",
quantity: 1,
installedPowerPerUnitKw: 0.1,
demandFactor: 1,
phaseCount: 1,
});
assert.equal(result.success, false);
});
it("requires a project revision for create and update commands", () => {
const device = {
name: "E-Line Pro",
displayName: "Bürobeleuchtung",
phaseType: "single_phase" as const,
quantity: 6,
powerPerUnit: 0.04,
simultaneityFactor: 0.8,
};
assert.equal(createProjectDeviceCommandSchema.safeParse(device).success, false);
assert.equal(updateProjectDeviceCommandSchema.safeParse(device).success, false);
assert.equal(
createProjectDeviceCommandSchema.safeParse({
...device,
expectedRevision: 4,
}).success,
true
);
assert.equal(
updateProjectDeviceCommandSchema.safeParse({
...device,
expectedRevision: 4,
}).success,
true
);
});
it("accepts only non-negative integer revisions for structure commands", () => {
assert.equal(
projectDeviceStructureCommandSchema.safeParse({
expectedRevision: 0,
}).success,
true
);
assert.equal(
projectDeviceStructureCommandSchema.safeParse({
expectedRevision: -1,
}).success,
false
);
assert.equal(
projectDeviceStructureCommandSchema.safeParse({
expectedRevision: 1.5,
}).success,
false
);
});
});