Files
snapcad-studio/tests/unit/edgeTreatmentHistory.test.ts
T
2026-07-11 20:34:13 +03:00

120 lines
3.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { restoreShapeBeforeEdgeTreatment } from "@/lib/edgeTreatmentHistory";
import type { EdgeTreatmentHistoryEntry, WorkplaneShape } from "@/types/sketchforge";
function box(overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
return {
id: "box",
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,
};
}
function historyEntry(before: WorkplaneShape): EdgeTreatmentHistoryEntry {
return {
id: "history-1",
createdAt: 1,
feature: { kind: "chamfer", amount: 1, edgeCount: 1 },
before,
};
}
describe("edge treatment history restoration", () => {
it("restores a grouped snapshot instead of retaining the treated mesh bounding box", () => {
const before = box({
id: "group-before",
name: "Group",
kind: "mesh",
x: 8,
width: 56,
depth: 24,
height: 60,
size: 56,
groupedBaseWidth: 56,
groupedBaseDepth: 24,
groupedBaseHeight: 60,
groupedShapes: [
box({ id: "normal-child", x: -18 }),
box({ id: "tall-child", x: 14, width: 8, depth: 10, height: 60, size: 10 }),
],
});
const treated = box({
id: "group-current",
name: "Renamed group",
kind: "mesh",
color: "#12abef",
x: 0,
width: 80,
depth: 40,
height: 60,
size: 80,
importedMesh: {
positions: [-40, 0, -20, 40, 0, -20, 40, 60, 20],
baseWidth: 80,
baseDepth: 40,
baseHeight: 60,
triangleCount: 1,
sourceFormat: "json",
},
edgeTreatments: [{ kind: "chamfer", amount: 1, edgeCount: 1 }],
});
const restored = restoreShapeBeforeEdgeTreatment(treated, historyEntry(before));
expect(restored).toMatchObject({
id: "group-current",
name: "Renamed group",
color: "#12abef",
kind: "mesh",
x: 8,
width: 56,
depth: 24,
height: 60,
});
expect(restored.importedMesh).toBeUndefined();
expect(restored.edgeTreatments).toBeUndefined();
expect(restored.groupedShapes?.map((child) => [child.width, child.depth, child.height])).toEqual([
[20, 20, 20],
[8, 10, 60],
]);
});
it("does not resize a restored child primitive to a combined component's bounds", () => {
const before = box({ id: "thin-before", x: 18, width: 8, depth: 6, height: 50, size: 8 });
const combinedComponent = box({
id: "thin-current",
kind: "mesh",
x: 0,
width: 54,
depth: 30,
height: 50,
size: 54,
importedMesh: {
positions: [-27, 0, -15, 27, 0, -15, 27, 50, 15],
baseWidth: 54,
baseDepth: 30,
baseHeight: 50,
triangleCount: 1,
sourceFormat: "json",
},
});
const restored = restoreShapeBeforeEdgeTreatment(combinedComponent, historyEntry(before));
expect(restored).toMatchObject({ kind: "box", x: 18, width: 8, depth: 6, height: 50, size: 8 });
expect(restored.importedMesh).toBeUndefined();
});
});