Add project device update history
This commit is contained in:
@@ -42,6 +42,10 @@ import {
|
||||
createProjectDeviceRowSyncProjectCommand,
|
||||
type ProjectDeviceSyncRowSnapshot,
|
||||
} from "../src/domain/models/project-device-row-sync-project-command.model.js";
|
||||
import {
|
||||
assertProjectDeviceUpdateProjectCommand,
|
||||
createProjectDeviceUpdateProjectCommand,
|
||||
} from "../src/domain/models/project-device-project-command.model.js";
|
||||
|
||||
describe("serialized project commands", () => {
|
||||
it("round-trips a versioned command envelope", () => {
|
||||
@@ -293,6 +297,62 @@ describe("project-device row sync project commands", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("project-device update project commands", () => {
|
||||
it("creates typed canonical field changes with null clearing", () => {
|
||||
const command = createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-1",
|
||||
{
|
||||
displayName: "Flurbeleuchtung",
|
||||
cosPhi: null,
|
||||
voltageV: 230,
|
||||
}
|
||||
);
|
||||
|
||||
assert.doesNotThrow(() =>
|
||||
assertProjectDeviceUpdateProjectCommand(command)
|
||||
);
|
||||
assert.deepEqual(command.payload.changes, [
|
||||
{ field: "displayName", value: "Flurbeleuchtung" },
|
||||
{ field: "cosPhi", value: null },
|
||||
{ field: "voltageV", value: 230 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("rejects empty, duplicate and invalid canonical values", () => {
|
||||
assert.throws(
|
||||
() =>
|
||||
createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-1",
|
||||
{}
|
||||
),
|
||||
/at least one change/
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
assertProjectDeviceUpdateProjectCommand({
|
||||
schemaVersion: 1,
|
||||
type: "project-device.update",
|
||||
payload: {
|
||||
projectDeviceId: "project-device-1",
|
||||
changes: [
|
||||
{ field: "quantity", value: 1 },
|
||||
{ field: "quantity", value: 2 },
|
||||
],
|
||||
},
|
||||
}),
|
||||
/duplicate field/
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-1",
|
||||
{ simultaneityFactor: 1.1 }
|
||||
),
|
||||
/outside its allowed range/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("circuit device-row structure project commands", () => {
|
||||
const row = {
|
||||
id: "row-1",
|
||||
|
||||
@@ -16,6 +16,7 @@ import { CircuitSectionRenumberProjectCommandRepository } from "../src/db/reposi
|
||||
import { CircuitStructureProjectCommandRepository } from "../src/db/repositories/circuit-structure-project-command.repository.js";
|
||||
import { DistributionBoardRepository } from "../src/db/repositories/distribution-board.repository.js";
|
||||
import { ProjectHistoryRepository } from "../src/db/repositories/project-history.repository.js";
|
||||
import { ProjectDeviceProjectCommandRepository } from "../src/db/repositories/project-device-project-command.repository.js";
|
||||
import { ProjectDeviceRowSyncProjectCommandRepository } from "../src/db/repositories/project-device-row-sync-project-command.repository.js";
|
||||
import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
|
||||
import { circuitSections } from "../src/db/schema/circuit-sections.js";
|
||||
@@ -37,6 +38,7 @@ import { createCircuitSectionReorderProjectCommand } from "../src/domain/models/
|
||||
import { createCircuitSectionRenumberProjectCommand } from "../src/domain/models/circuit-section-renumber-project-command.model.js";
|
||||
import { createCircuitInsertProjectCommand } from "../src/domain/models/circuit-structure-project-command.model.js";
|
||||
import { createProjectDeviceRowSyncProjectCommand } from "../src/domain/models/project-device-row-sync-project-command.model.js";
|
||||
import { createProjectDeviceUpdateProjectCommand } from "../src/domain/models/project-device-project-command.model.js";
|
||||
import { ProjectCommandService } from "../src/domain/services/project-command.service.js";
|
||||
|
||||
function createTestDatabase(): DatabaseContext {
|
||||
@@ -93,6 +95,7 @@ function createService(context: DatabaseContext) {
|
||||
new CircuitStructureProjectCommandRepository(context.db),
|
||||
new CircuitSectionReorderProjectCommandRepository(context.db),
|
||||
new CircuitSectionRenumberProjectCommandRepository(context.db),
|
||||
new ProjectDeviceProjectCommandRepository(context.db),
|
||||
new ProjectDeviceRowSyncProjectCommandRepository(context.db),
|
||||
new ProjectHistoryRepository(context.db)
|
||||
);
|
||||
@@ -266,6 +269,62 @@ describe("project command service", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("dispatches project-device updates and their persisted inverse", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
context.db
|
||||
.insert(projectDevices)
|
||||
.values({
|
||||
id: "project-device-1",
|
||||
projectId: "project-1",
|
||||
name: "Luminaire",
|
||||
displayName: "Office lighting",
|
||||
phaseType: "single_phase",
|
||||
quantity: 4,
|
||||
powerPerUnit: 0.04,
|
||||
simultaneityFactor: 0.8,
|
||||
})
|
||||
.run();
|
||||
const service = createService(context);
|
||||
const updated = service.executeUser({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
command: createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-1",
|
||||
{
|
||||
displayName: "Flurbeleuchtung",
|
||||
powerPerUnit: 0.05,
|
||||
}
|
||||
),
|
||||
});
|
||||
assert.equal(updated.history.undoDepth, 1);
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(projectDevices)
|
||||
.where(eq(projectDevices.id, "project-device-1"))
|
||||
.get()?.displayName,
|
||||
"Flurbeleuchtung"
|
||||
);
|
||||
|
||||
const undone = createService(context).undo({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
});
|
||||
assert.equal(undone.history.redoDepth, 1);
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(projectDevices)
|
||||
.where(eq(projectDevices.id, "project-device-1"))
|
||||
.get()?.displayName,
|
||||
"Office lighting"
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects stale undo and preserves the domain value and stack", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,292 @@
|
||||
import path from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import {
|
||||
createDatabaseContext,
|
||||
type DatabaseContext,
|
||||
} from "../src/db/database-context.js";
|
||||
import { ProjectDeviceProjectCommandRepository } from "../src/db/repositories/project-device-project-command.repository.js";
|
||||
import { ProjectHistoryRepository } from "../src/db/repositories/project-history.repository.js";
|
||||
import { projectDevices } from "../src/db/schema/project-devices.js";
|
||||
import { projectRevisions } from "../src/db/schema/project-revisions.js";
|
||||
import { projects } from "../src/db/schema/projects.js";
|
||||
import { ProjectRevisionConflictError } from "../src/domain/errors/project-revision-conflict.error.js";
|
||||
import { createProjectDeviceUpdateProjectCommand } from "../src/domain/models/project-device-project-command.model.js";
|
||||
|
||||
function createTestDatabase(): DatabaseContext {
|
||||
const context = createDatabaseContext(":memory:");
|
||||
migrate(context.db, {
|
||||
migrationsFolder: path.resolve("src", "db", "migrations"),
|
||||
});
|
||||
context.db
|
||||
.insert(projects)
|
||||
.values([
|
||||
{ id: "project-1", name: "Test project" },
|
||||
{ id: "project-2", name: "Other project" },
|
||||
])
|
||||
.run();
|
||||
context.db
|
||||
.insert(projectDevices)
|
||||
.values([
|
||||
{
|
||||
id: "project-device-1",
|
||||
projectId: "project-1",
|
||||
name: "Luminaire",
|
||||
displayName: "Office lighting",
|
||||
phaseType: "single_phase",
|
||||
connectionKind: "fixed",
|
||||
costGroup: "440",
|
||||
category: "lighting",
|
||||
quantity: 4,
|
||||
powerPerUnit: 0.04,
|
||||
simultaneityFactor: 0.8,
|
||||
cosPhi: 0.95,
|
||||
remark: "DALI",
|
||||
voltageV: 230,
|
||||
},
|
||||
{
|
||||
id: "project-device-foreign",
|
||||
projectId: "project-2",
|
||||
name: "Foreign device",
|
||||
displayName: "Foreign device",
|
||||
phaseType: "three_phase",
|
||||
quantity: 1,
|
||||
powerPerUnit: 1,
|
||||
simultaneityFactor: 1,
|
||||
},
|
||||
])
|
||||
.run();
|
||||
return context;
|
||||
}
|
||||
|
||||
function getProjectDevice(
|
||||
context: DatabaseContext,
|
||||
projectDeviceId = "project-device-1"
|
||||
) {
|
||||
const device = context.db
|
||||
.select()
|
||||
.from(projectDevices)
|
||||
.where(eq(projectDevices.id, projectDeviceId))
|
||||
.get();
|
||||
assert.ok(device);
|
||||
return device;
|
||||
}
|
||||
|
||||
describe("project-device project-command repository", () => {
|
||||
it("updates canonical fields and supports persisted undo and redo", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const store = new ProjectDeviceProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
const command = createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-1",
|
||||
{
|
||||
displayName: "Flurbeleuchtung",
|
||||
phaseType: "three_phase",
|
||||
connectionKind: null,
|
||||
powerPerUnit: 0.05,
|
||||
cosPhi: null,
|
||||
voltageV: 400,
|
||||
}
|
||||
);
|
||||
const updated = store.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command,
|
||||
});
|
||||
assert.deepEqual(
|
||||
{
|
||||
displayName: getProjectDevice(context).displayName,
|
||||
phaseType: getProjectDevice(context).phaseType,
|
||||
connectionKind: getProjectDevice(context).connectionKind,
|
||||
powerPerUnit: getProjectDevice(context).powerPerUnit,
|
||||
cosPhi: getProjectDevice(context).cosPhi,
|
||||
voltageV: getProjectDevice(context).voltageV,
|
||||
},
|
||||
{
|
||||
displayName: "Flurbeleuchtung",
|
||||
phaseType: "three_phase",
|
||||
connectionKind: null,
|
||||
powerPerUnit: 0.05,
|
||||
cosPhi: null,
|
||||
voltageV: 400,
|
||||
}
|
||||
);
|
||||
|
||||
store.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
source: "undo",
|
||||
historyTargetChangeSetId: updated.revision.changeSetId,
|
||||
command: updated.inverse,
|
||||
});
|
||||
assert.deepEqual(
|
||||
{
|
||||
displayName: getProjectDevice(context).displayName,
|
||||
phaseType: getProjectDevice(context).phaseType,
|
||||
connectionKind: getProjectDevice(context).connectionKind,
|
||||
powerPerUnit: getProjectDevice(context).powerPerUnit,
|
||||
cosPhi: getProjectDevice(context).cosPhi,
|
||||
voltageV: getProjectDevice(context).voltageV,
|
||||
},
|
||||
{
|
||||
displayName: "Office lighting",
|
||||
phaseType: "single_phase",
|
||||
connectionKind: "fixed",
|
||||
powerPerUnit: 0.04,
|
||||
cosPhi: 0.95,
|
||||
voltageV: 230,
|
||||
}
|
||||
);
|
||||
|
||||
store.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 2,
|
||||
source: "redo",
|
||||
historyTargetChangeSetId: updated.revision.changeSetId,
|
||||
command,
|
||||
});
|
||||
assert.deepEqual(
|
||||
new ProjectHistoryRepository(context.db).getState(
|
||||
"project-1"
|
||||
),
|
||||
{
|
||||
projectId: "project-1",
|
||||
currentRevision: 3,
|
||||
undoDepth: 1,
|
||||
redoDepth: 0,
|
||||
undoChangeSetId: updated.revision.changeSetId,
|
||||
redoChangeSetId: null,
|
||||
}
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects project devices from another project", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const store = new ProjectDeviceProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
store.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-foreign",
|
||||
{ displayName: "Not allowed" }
|
||||
),
|
||||
}),
|
||||
/does not belong to project/
|
||||
);
|
||||
assert.equal(
|
||||
getProjectDevice(
|
||||
context,
|
||||
"project-device-foreign"
|
||||
).displayName,
|
||||
"Foreign device"
|
||||
);
|
||||
assert.equal(
|
||||
context.db.select().from(projectRevisions).all().length,
|
||||
0
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back the update for a stale project revision", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const store = new ProjectDeviceProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
store.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-1",
|
||||
{ displayName: "First change" }
|
||||
),
|
||||
});
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
store.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-1",
|
||||
{ displayName: "Stale change" }
|
||||
),
|
||||
}),
|
||||
(error) =>
|
||||
error instanceof ProjectRevisionConflictError &&
|
||||
error.actualRevision === 1
|
||||
);
|
||||
assert.equal(
|
||||
getProjectDevice(context).displayName,
|
||||
"First change"
|
||||
);
|
||||
assert.equal(
|
||||
context.db.select().from(projectRevisions).all().length,
|
||||
1
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back the update after a late history failure", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
context.sqlite.exec(`
|
||||
CREATE TRIGGER fail_project_device_history
|
||||
BEFORE INSERT ON project_history_stack_entries
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced project-device history failure');
|
||||
END;
|
||||
`);
|
||||
const store = new ProjectDeviceProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
store.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createProjectDeviceUpdateProjectCommand(
|
||||
"project-device-1",
|
||||
{
|
||||
displayName: "Must roll back",
|
||||
quantity: 9,
|
||||
}
|
||||
),
|
||||
}),
|
||||
/forced project-device history failure/
|
||||
);
|
||||
assert.equal(
|
||||
getProjectDevice(context).displayName,
|
||||
"Office lighting"
|
||||
);
|
||||
assert.equal(getProjectDevice(context).quantity, 4);
|
||||
assert.equal(
|
||||
context.db.select().from(projectRevisions).all().length,
|
||||
0
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user