2026-07-14 09:43:37 +03:00
|
|
|
import { describe, expect, it } from "vitest";
|
2026-08-23 13:45:27 +03:00
|
|
|
import { appendEditorHistorySnapshot, boundedEditorHistory, editorHistoryEntry, editorHistoryForExport, hydrateEditorHistoryState, immutableResourceFingerprint, projectShapesFingerprint } from "@/lib/editorHistory";
|
2026-07-14 09:43:37 +03:00
|
|
|
import type { WorkplaneShape } from "@/types/sketchforge";
|
|
|
|
|
|
|
|
|
|
function box(overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
|
|
|
|
|
return {
|
|
|
|
|
id: "box-1",
|
|
|
|
|
name: "Box",
|
|
|
|
|
kind: "box",
|
|
|
|
|
color: "#d41721",
|
|
|
|
|
x: 0,
|
|
|
|
|
z: 0,
|
|
|
|
|
elevation: 0,
|
|
|
|
|
size: 20,
|
|
|
|
|
width: 20,
|
|
|
|
|
depth: 20,
|
|
|
|
|
height: 20,
|
|
|
|
|
rotation: 0,
|
|
|
|
|
locked: false,
|
|
|
|
|
hidden: false,
|
|
|
|
|
...overrides,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("editor history snapshots", () => {
|
|
|
|
|
it("seeds history with the real loaded scene and valid selection", () => {
|
|
|
|
|
const shape = box();
|
|
|
|
|
const entry = editorHistoryEntry([shape], [shape.id, "missing", shape.id]);
|
|
|
|
|
|
|
|
|
|
expect(entry.shapes).toHaveLength(1);
|
|
|
|
|
expect(entry.shapes[0].id).toBe(shape.id);
|
|
|
|
|
expect(entry.selectedIds).toEqual([shape.id]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("detects persistence-relevant fields that the old fingerprint omitted", () => {
|
|
|
|
|
const shape = box({ kind: "cylinder", sides: 32 });
|
|
|
|
|
const baseline = projectShapesFingerprint([shape]);
|
|
|
|
|
|
|
|
|
|
expect(projectShapesFingerprint([{ ...shape, locked: true }])).not.toBe(baseline);
|
|
|
|
|
expect(projectShapesFingerprint([{ ...shape, hidden: true }])).not.toBe(baseline);
|
|
|
|
|
expect(projectShapesFingerprint([{ ...shape, sides: 64 }])).not.toBe(baseline);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("detects mesh coordinate changes even when array lengths are unchanged", () => {
|
|
|
|
|
const shape = box({
|
|
|
|
|
kind: "mesh",
|
|
|
|
|
importedMesh: {
|
|
|
|
|
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
|
|
|
|
|
baseWidth: 1,
|
|
|
|
|
baseDepth: 1,
|
|
|
|
|
baseHeight: 1,
|
|
|
|
|
triangleCount: 1,
|
|
|
|
|
sourceFormat: "json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const changed = {
|
|
|
|
|
...shape,
|
|
|
|
|
importedMesh: { ...shape.importedMesh!, positions: [0, 0, 0, 2, 0, 0, 0, 1, 0] },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
expect(projectShapesFingerprint([changed])).not.toBe(projectShapesFingerprint([shape]));
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-28 14:52:59 +03:00
|
|
|
it("reuses an immutable mesh signature for transform-only history snapshots", () => {
|
|
|
|
|
let coordinateReads = 0;
|
|
|
|
|
const positions = new Proxy([0, 0, 0, 1, 0, 0, 0, 1, 0], {
|
|
|
|
|
get(target, property, receiver) {
|
|
|
|
|
if (typeof property === "string" && /^\d+$/.test(property)) coordinateReads += 1;
|
|
|
|
|
return Reflect.get(target, property, receiver);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const shape = box({
|
|
|
|
|
kind: "mesh",
|
|
|
|
|
importedMesh: {
|
|
|
|
|
positions,
|
|
|
|
|
baseWidth: 1,
|
|
|
|
|
baseDepth: 1,
|
|
|
|
|
baseHeight: 1,
|
|
|
|
|
triangleCount: 1,
|
|
|
|
|
sourceFormat: "stl",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const baseline = projectShapesFingerprint([shape]);
|
|
|
|
|
expect(coordinateReads).toBeGreaterThan(0);
|
|
|
|
|
coordinateReads = 0;
|
|
|
|
|
|
|
|
|
|
expect(projectShapesFingerprint([{ ...shape, x: 25 }])).not.toBe(baseline);
|
|
|
|
|
expect(coordinateReads).toBe(0);
|
|
|
|
|
expect(projectShapesFingerprint([{
|
|
|
|
|
...shape,
|
|
|
|
|
importedMesh: { ...shape.importedMesh!, positions: [...positions] },
|
|
|
|
|
}])).toBe(baseline);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-23 13:45:27 +03:00
|
|
|
it("stream-hashes large mesh arrays and detects changes at the end", () => {
|
|
|
|
|
const positions = Array.from({ length: 10_001 }, (_, index) => Math.fround(Math.sin(index) * 100));
|
|
|
|
|
const resource = {
|
|
|
|
|
positions,
|
|
|
|
|
normals: positions.map((value) => Math.fround(value / 100)),
|
|
|
|
|
baseWidth: 200,
|
|
|
|
|
baseDepth: 150,
|
|
|
|
|
baseHeight: 80,
|
|
|
|
|
triangleCount: Math.floor(positions.length / 9),
|
|
|
|
|
sourceFormat: "stl",
|
|
|
|
|
};
|
|
|
|
|
const changedPositions = [...positions];
|
|
|
|
|
changedPositions[changedPositions.length - 1] += 1;
|
|
|
|
|
|
|
|
|
|
expect(immutableResourceFingerprint({ ...resource, positions: [...positions], normals: [...resource.normals] }))
|
|
|
|
|
.toBe(immutableResourceFingerprint(resource));
|
|
|
|
|
expect(immutableResourceFingerprint({ ...resource, positions: changedPositions }))
|
|
|
|
|
.not.toBe(immutableResourceFingerprint(resource));
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-18 12:12:41 +03:00
|
|
|
it("reuses geometry signatures inside newly restored edge-treatment history wrappers", () => {
|
|
|
|
|
let coordinateReads = 0;
|
|
|
|
|
const points = new Proxy([0, 0, 0, 1, 2, 3], {
|
|
|
|
|
get(target, property, receiver) {
|
|
|
|
|
if (typeof property === "string" && /^\d+$/.test(property)) coordinateReads += 1;
|
|
|
|
|
return Reflect.get(target, property, receiver);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const before = box({ cadDisplayEdges: [{ points }] });
|
|
|
|
|
const entry = { id: "fillet-before", createdAt: 123, feature: { kind: "fillet" as const, amount: 1, edgeCount: 1 }, before };
|
|
|
|
|
const baseline = projectShapesFingerprint([box({ edgeTreatmentHistory: [entry] })]);
|
|
|
|
|
expect(coordinateReads).toBeGreaterThan(0);
|
|
|
|
|
coordinateReads = 0;
|
|
|
|
|
expect(projectShapesFingerprint([box({ edgeTreatmentHistory: [{ ...entry, before: { ...before } }] })])).toBe(baseline);
|
|
|
|
|
expect(coordinateReads).toBe(0);
|
|
|
|
|
expect(projectShapesFingerprint([box({ edgeTreatmentHistory: [{ ...entry, before: { ...before, x: 5 } }] })])).not.toBe(baseline);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves normalized reversible-history references across transform snapshots", () => {
|
|
|
|
|
const original = editorHistoryEntry([box({ edgeTreatmentHistory: [{
|
|
|
|
|
id: "edge-before", createdAt: 123, feature: { kind: "fillet", amount: 1, edgeCount: 1 }, before: box(),
|
|
|
|
|
}] })], []);
|
|
|
|
|
const moved = editorHistoryEntry([{ ...original.shapes[0], x: 5 }], []);
|
|
|
|
|
expect(moved.shapes[0].edgeTreatmentHistory).toBe(original.shapes[0].edgeTreatmentHistory);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-23 13:45:27 +03:00
|
|
|
it("falls back to direct field hashing when serialization exceeds the string limit", () => {
|
|
|
|
|
const resource = {
|
|
|
|
|
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
|
|
|
|
|
toJSON() {
|
|
|
|
|
throw new RangeError("Invalid string length");
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
expect(immutableResourceFingerprint(resource)).toMatch(/^stream-v1:/);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-28 14:52:59 +03:00
|
|
|
it("keeps unlimited history and applies preset or custom action limits", () => {
|
2026-07-14 09:43:37 +03:00
|
|
|
const entries = Array.from({ length: 140 }, (_, index) => ({
|
|
|
|
|
...editorHistoryEntry([box({ id: `box-${index}`, x: index })], []),
|
|
|
|
|
estimatedBytes: 2 * 1024 * 1024,
|
|
|
|
|
}));
|
2026-07-28 14:52:59 +03:00
|
|
|
const unlimited = boundedEditorHistory(entries);
|
|
|
|
|
const lastThirty = boundedEditorHistory(entries, 30);
|
|
|
|
|
const custom = boundedEditorHistory(entries, 7);
|
2026-07-14 09:43:37 +03:00
|
|
|
|
2026-07-28 14:52:59 +03:00
|
|
|
expect(unlimited).toHaveLength(140);
|
|
|
|
|
expect(lastThirty).toHaveLength(31);
|
|
|
|
|
expect(lastThirty[0].shapes[0].id).toBe("box-109");
|
|
|
|
|
expect(custom).toHaveLength(8);
|
|
|
|
|
expect(custom.at(-1)?.shapes[0].id).toBe("box-139");
|
2026-07-14 09:43:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("trims redo only for a real new edit and preserves it for a no-op", () => {
|
|
|
|
|
const entries = [0, 1, 2].map((x) => editorHistoryEntry([box({ x })], []));
|
|
|
|
|
const noOp = appendEditorHistorySnapshot(entries, 1, editorHistoryEntry([box({ x: 1 })], ["box-1"]));
|
|
|
|
|
|
|
|
|
|
expect(noOp.changed).toBe(false);
|
|
|
|
|
expect(noOp.entries).toHaveLength(3);
|
|
|
|
|
expect(noOp.entries[1].selectedIds).toEqual(["box-1"]);
|
|
|
|
|
|
|
|
|
|
const branch = appendEditorHistorySnapshot(noOp.entries, 1, editorHistoryEntry([box({ x: 5 })], []));
|
|
|
|
|
expect(branch.changed).toBe(true);
|
|
|
|
|
expect(branch.entries).toHaveLength(3);
|
|
|
|
|
expect(branch.entries.at(-1)?.shapes[0].x).toBe(5);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("restores a persisted undo and redo stack at its saved index", () => {
|
|
|
|
|
const entries = [0, 5, 10].map((x) => editorHistoryEntry([box({ x })], []));
|
|
|
|
|
const restored = hydrateEditorHistoryState([box({ x: 5 })], entries, 1);
|
|
|
|
|
|
|
|
|
|
expect(restored.index).toBe(1);
|
|
|
|
|
expect(restored.entries).toHaveLength(3);
|
|
|
|
|
expect(restored.entries[restored.index].shapes[0].x).toBe(5);
|
|
|
|
|
expect(restored.entries[0].shapes[0].x).toBe(0);
|
|
|
|
|
expect(restored.entries[2].shapes[0].x).toBe(10);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-28 14:52:59 +03:00
|
|
|
it("restores only the configured number of saved actions without losing the active state", () => {
|
|
|
|
|
const entries = Array.from({ length: 80 }, (_, x) => editorHistoryEntry([box({ x })], []));
|
|
|
|
|
const restored = hydrateEditorHistoryState([box({ x: 60 })], entries, 60, 30);
|
|
|
|
|
|
|
|
|
|
expect(restored.entries).toHaveLength(31);
|
|
|
|
|
expect(restored.index).toBe(30);
|
|
|
|
|
expect(restored.entries[0].shapes[0].x).toBe(30);
|
|
|
|
|
expect(restored.entries[restored.index].shapes[0].x).toBe(60);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-14 09:43:37 +03:00
|
|
|
it("falls back to the loaded scene when persisted history is stale", () => {
|
|
|
|
|
const stale = [editorHistoryEntry([box({ x: 1 })], [])];
|
|
|
|
|
const restored = hydrateEditorHistoryState([box({ x: 9 })], stale, 0);
|
|
|
|
|
|
|
|
|
|
expect(restored.index).toBe(0);
|
|
|
|
|
expect(restored.entries).toHaveLength(1);
|
|
|
|
|
expect(restored.entries[0].shapes[0].x).toBe(9);
|
|
|
|
|
});
|
2026-07-16 12:30:39 +03:00
|
|
|
|
|
|
|
|
it("selects all history or the requested number of recent undo actions for project export", () => {
|
|
|
|
|
const entries = Array.from({ length: 140 }, (_, x) => editorHistoryEntry([box({ x })], []));
|
|
|
|
|
|
|
|
|
|
const unlimited = editorHistoryForExport(entries, 120, "unlimited");
|
|
|
|
|
expect(unlimited.entries).toBe(entries);
|
|
|
|
|
expect(unlimited.index).toBe(120);
|
|
|
|
|
|
|
|
|
|
const lastThirty = editorHistoryForExport(entries, 120, 30);
|
|
|
|
|
expect(lastThirty.entries).toHaveLength(31);
|
|
|
|
|
expect(lastThirty.index).toBe(30);
|
|
|
|
|
expect(lastThirty.entries[0].shapes[0].x).toBe(90);
|
|
|
|
|
expect(lastThirty.entries[30].shapes[0].x).toBe(120);
|
|
|
|
|
});
|
2026-07-14 09:43:37 +03:00
|
|
|
});
|