feat(sensor-placement): add scheme engineering editor
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import {
|
||||
addSensorPoint,
|
||||
createSchemeEditorState,
|
||||
deleteSensorPoint,
|
||||
isSchemeDirty,
|
||||
replaceSensorPoint,
|
||||
resetSchemeEdit,
|
||||
summarizeChanges,
|
||||
undoSchemeEdit,
|
||||
} from "./schemeEditor";
|
||||
import type { SensorPlacementScheme, SensorPoint } from "./types";
|
||||
|
||||
const point = (node_id: string): SensorPoint => ({
|
||||
node_id,
|
||||
project_x: Number(node_id.slice(1)) * 10,
|
||||
project_y: Number(node_id.slice(1)) * 20,
|
||||
map_x: 13500000 + Number(node_id.slice(1)) * 10,
|
||||
map_y: 3600000 + Number(node_id.slice(1)) * 20,
|
||||
longitude: 121,
|
||||
latitude: 31,
|
||||
elevation: 5,
|
||||
});
|
||||
|
||||
const scheme: SensorPlacementScheme = {
|
||||
id: 1,
|
||||
scheme_name: "测试方案",
|
||||
sensor_number: 2,
|
||||
min_diameter: 300,
|
||||
username: "alice",
|
||||
create_time: "2026-07-30T08:00:00+08:00",
|
||||
sensor_location: ["J1", "J2"],
|
||||
sensor_points: [point("J1"), point("J2")],
|
||||
can_edit: true,
|
||||
};
|
||||
|
||||
describe("scheme editor", () => {
|
||||
it("adds unique nodes and can undo", () => {
|
||||
const initial = createSchemeEditorState(scheme);
|
||||
const added = addSensorPoint(initial, point("J3"));
|
||||
|
||||
expect(added.points.map((item) => item.node_id)).toEqual(["J1", "J2", "J3"]);
|
||||
expect(added.statuses.J3).toBe("added");
|
||||
expect(isSchemeDirty(added)).toBe(true);
|
||||
expect(undoSchemeEdit(added).points).toEqual(initial.points);
|
||||
});
|
||||
|
||||
it("replaces a node without changing row order", () => {
|
||||
const initial = createSchemeEditorState(scheme);
|
||||
const replaced = replaceSensorPoint(initial, "J1", point("J3"));
|
||||
|
||||
expect(replaced.points.map((item) => item.node_id)).toEqual(["J3", "J2"]);
|
||||
expect(replaced.statuses.J3).toBe("replaced");
|
||||
expect(summarizeChanges(replaced)).toEqual({
|
||||
added: 0,
|
||||
removed: 0,
|
||||
replaced: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps an added status when replacing a newly added node", () => {
|
||||
const added = addSensorPoint(createSchemeEditorState(scheme), point("J3"));
|
||||
const replaced = replaceSensorPoint(added, "J3", point("J4"));
|
||||
|
||||
expect(replaced.statuses.J3).toBeUndefined();
|
||||
expect(replaced.statuses.J4).toBe("added");
|
||||
});
|
||||
|
||||
it("rejects duplicate replacements and deleting the final row", () => {
|
||||
const initial = createSchemeEditorState(scheme);
|
||||
expect(replaceSensorPoint(initial, "J1", point("J2"))).toBe(initial);
|
||||
|
||||
const oneLeft = deleteSensorPoint(initial, "J1");
|
||||
expect(deleteSensorPoint(oneLeft, "J2")).toBe(oneLeft);
|
||||
});
|
||||
|
||||
it("resets to the loaded baseline", () => {
|
||||
const edited = addSensorPoint(createSchemeEditorState(scheme), point("J3"));
|
||||
const reset = resetSchemeEdit(edited);
|
||||
|
||||
expect(reset.points.map((item) => item.node_id)).toEqual(["J1", "J2"]);
|
||||
expect(isSchemeDirty(reset)).toBe(false);
|
||||
expect(reset.history).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user