132 lines
3.4 KiB
TypeScript
132 lines
3.4 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { describe, it } from "node:test";
|
|
import {
|
|
createProjectDeviceCommandSchema,
|
|
createProjectDeviceSchema,
|
|
disconnectProjectDeviceRowsSchema,
|
|
projectDeviceStructureCommandSchema,
|
|
synchronizeProjectDeviceRowsSchema,
|
|
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("rejects manually supplied device voltages", () => {
|
|
const result = createProjectDeviceSchema.safeParse({
|
|
name: "Pumpe",
|
|
displayName: "Pumpe",
|
|
phaseType: "three_phase",
|
|
quantity: 1,
|
|
powerPerUnit: 2,
|
|
simultaneityFactor: 1,
|
|
voltageV: 500,
|
|
});
|
|
|
|
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
|
|
);
|
|
});
|
|
|
|
it("requires the current revision for synchronization writes", () => {
|
|
assert.equal(
|
|
synchronizeProjectDeviceRowsSchema.safeParse({
|
|
rowIds: ["row-1"],
|
|
fields: ["quantity"],
|
|
}).success,
|
|
false
|
|
);
|
|
assert.equal(
|
|
synchronizeProjectDeviceRowsSchema.safeParse({
|
|
rowIds: ["row-1"],
|
|
fields: ["quantity"],
|
|
expectedRevision: 7,
|
|
}).success,
|
|
true
|
|
);
|
|
assert.equal(
|
|
disconnectProjectDeviceRowsSchema.safeParse({
|
|
rowIds: ["row-1"],
|
|
expectedRevision: 7,
|
|
}).success,
|
|
true
|
|
);
|
|
});
|
|
});
|