fix: keep grouped object IDs unique

This commit is contained in:
Formsmith
2026-08-03 18:54:15 +03:00
parent 9d2ec76103
commit decf33521a
5 changed files with 130 additions and 14 deletions
+25
View File
@@ -224,6 +224,31 @@ describe("SketchForge .skf project packages", () => {
expect(restored.shapes[0].edgeTreatmentHistory?.[0].before.kind).toBe("box");
});
it("repairs duplicate descendant IDs from legacy shallow-copied groups during export", async () => {
const roof = shape("roundRoof", "round-roof-shared-child");
const box = shape("box", "box-shared-child");
const original = shape("mesh", "group-original", { groupedShapes: [roof, box] });
const duplicate = { ...original, id: "group-copy", x: 30 };
const history = [
editorHistoryEntry([original], [original.id]),
editorHistoryEntry([original, duplicate], [duplicate.id]),
];
const exported = await exportSkfProject(input([original, duplicate], { history, historyIndex: 1 }));
const restored = await importSkfProject(exported);
const collectIds = (entry: WorkplaneShape): string[] => [
entry.id,
...(entry.groupedShapes ?? []).flatMap(collectIds),
];
const restoredIds = restored.shapes.flatMap(collectIds);
expect(original.groupedShapes?.[0].id).toBe(duplicate.groupedShapes?.[0].id);
expect(restored.shapes.map((entry) => entry.id)).toEqual(["group-original", "group-copy"]);
expect(new Set(restoredIds).size).toBe(restoredIds.length);
expect(restored.history).toHaveLength(2);
expect(restored.historyIndex).toBe(1);
});
it("preserves every silently packaged undo and redo action", async () => {
const created = shape("box", "history-box", { width: 20, depth: 20, x: 0 });
const resized = { ...created, width: 30 };
+29
View File
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import type { WorkplaneShape } from "@/types/sketchforge";
import {
canonicalizeShape,
cloneWorkplaneShapeTreeWithFreshIds,
cleanNearZero,
cleanRotationDegrees,
fallbackSolidColor,
@@ -98,6 +99,34 @@ describe("workplane shape helpers", () => {
expect(canonical.groupedShapes?.[0].mirrorZ).toBeUndefined();
});
it("assigns fresh object IDs throughout duplicated group trees", () => {
const original = shape({
id: "outer-group",
kind: "mesh",
groupedShapes: [
shape({ id: "round-roof-child", kind: "roundRoof" }),
shape({
id: "nested-group",
kind: "mesh",
groupedShapes: [shape({ id: "nested-box" })],
}),
],
});
const duplicate = cloneWorkplaneShapeTreeWithFreshIds(original, "copy");
const collectIds = (entry: WorkplaneShape): string[] => [
entry.id,
...(entry.groupedShapes ?? []).flatMap(collectIds),
];
const originalIds = collectIds(original);
const duplicateIds = collectIds(duplicate);
expect(new Set(duplicateIds).size).toBe(duplicateIds.length);
expect(duplicateIds.every((id) => !originalIds.includes(id))).toBe(true);
expect(original.groupedShapes?.[0].id).toBe("round-roof-child");
expect(duplicate.groupedShapes?.[0].kind).toBe("roundRoof");
});
it("keeps shallow equality strict for shape payload references", () => {
const importedMesh = { positions: [0, 0, 0], baseWidth: 1, baseDepth: 1, baseHeight: 1, triangleCount: 0, sourceFormat: "json" as const };
const first = shape({ importedMesh });