Files
Enes Yakıştır 33b0940012
Build and Push Docker Images / build (push) Canceled after 0s
CI / Typecheck and unit tests (push) Canceled after 0s
feat: SnapCAD Studio'yu YakHub deposuna taşı
- DWG/DXF içe aktarma ve .skf yeniden açma düzeltmeleri

- YakNet Bulut eşitlemede boş/eski kopya koruması ve yerel-only proje bildirimi

- Panelden içe aktarma duyuruları ve erişilebilir Türkçe hata mesajları

- AGPL-3.0 lisansı, README ve YakHub bağlantıları
2026-10-25 18:20:21 +03:00

762 lines
32 KiB
TypeScript

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, describe, expect, it, vi } from "vitest";
import { strFromU8, strToU8, unzipSync, zipSync } from "fflate";
import { importedShapeFromDxf } from "@/lib/dxfImport";
import { editorHistoryEntry } from "@/lib/editorHistory";
import { placementWorkplaneFromSurface } from "@/lib/placementWorkplane";
import { attachProjectAsset, projectAssetFromBytes } from "@/lib/projectAssets";
import * as projectAssets from "@/lib/projectAssets";
import { canonicalizeShape } from "@/lib/workplaneShapes";
import {
exportSkfProject,
importSkfProject,
inspectSkfProjectPackage,
SKF_FORMAT_VERSION,
SKF_LIMITS,
SKF_SCHEMA_ID,
type SkfProjectDocumentV1,
type SkfProjectExportInput,
} from "@/lib/skfProject";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE } from "@/lib/workplaneSettings";
import type { ShapeKind, WorkplaneShape } from "@/types/sketchforge";
// DWG tarayıcı çekirdeği testte gerçek WASM çalıştırmaz; kaynak varlık yolunun
// doğru içe aktarıcıya yönlendirildiğini doğrulamak için sabit bir mesh döner.
vi.mock("@/lib/dwgKernel", () => ({
importedShapeFromDwg: vi.fn(async () => ({
importedMesh: {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 4321,
baseDepth: 21,
baseHeight: 3,
triangleCount: 1,
sourceFormat: "dwg",
},
})),
}));
function shape(kind: ShapeKind, id = `${kind}-1`, overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
return {
id,
name: kind,
kind,
color: "#12a4cc",
x: 1,
z: 2,
elevation: 3,
size: 20,
width: 20,
depth: 18,
height: 16,
rotation: 15,
rotationX: 5,
rotationZ: 10,
locked: false,
hidden: false,
...overrides,
};
}
function input(shapes: WorkplaneShape[], overrides: Partial<SkfProjectExportInput> = {}): SkfProjectExportInput {
const history = [editorHistoryEntry(shapes, [])];
return {
projectId: "project-original",
projectName: "Round trip",
createdAt: 1_700_000_000_000,
modifiedAt: 1_700_000_100_000,
shapes,
history,
historyIndex: 0,
assets: [],
workspace: DEFAULT_WORKPLANE_WORKSPACE,
snapGrid: DEFAULT_SNAP_GRID,
placementElevation: 12.5,
...overrides,
};
}
function packageDocument(bytes: Uint8Array) {
const files = unzipSync(bytes);
return { files, document: JSON.parse(strFromU8(files["project.json"])) as SkfProjectDocumentV1 };
}
function mutateProject(bytes: Uint8Array, mutate: (document: SkfProjectDocumentV1) => void) {
const files = unzipSync(bytes);
const document = JSON.parse(strFromU8(files["project.json"])) as SkfProjectDocumentV1;
mutate(document);
files["project.json"] = strToU8(JSON.stringify(document));
return zipSync(files);
}
describe("SketchForge .skf project packages", () => {
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
});
it("encodes shared geometry once across a long transform history and subsequent saves", async () => {
const points = Array.from({ length: 30_000 }, (_, index) => Math.sin(index));
const resource = shape("mesh", "shared-geometry", {
cadDisplayEdges: [{ points }],
cadDisplayEdgesVersion: 2,
cadBrep: "Shared regression BREP ".repeat(10_000),
importedMesh: { positions: points, baseWidth: 2, baseDepth: 2, baseHeight: 2, triangleCount: points.length / 9, sourceFormat: "json" },
});
const history = Array.from({ length: 40 }, (_, x) => editorHistoryEntry([{ ...resource, x }], [resource.id]));
const saveInput = input(history[20].shapes, { history, historyIndex: 20 });
const hash = vi.spyOn(projectAssets, "sha256Hex");
const bytes = await exportSkfProject(saveInput);
expect(hash).toHaveBeenCalledTimes(3);
hash.mockClear();
await exportSkfProject(saveInput);
expect(hash).not.toHaveBeenCalled();
const { files, document } = packageDocument(bytes);
expect(document.assets.map((asset) => asset.kind).sort()).toEqual(["brep", "derived-mesh", "display-edges"]);
expect(files["project.json"].length).toBeLessThan(100_000);
expect(document.states.every((state) => state.nodes.every((node) => node.definition.cadDisplayEdges === undefined))).toBe(true);
const restored = await importSkfProject(bytes);
expect(restored.historyIndex).toBe(20);
expect(restored.history.map((entry) => entry.shapes[0].x)).toEqual(history.map((entry) => entry.shapes[0].x));
for (const entry of restored.history) {
expect(entry.shapes[0].cadDisplayEdges).toBe(restored.shapes[0].cadDisplayEdges);
expect(entry.shapes[0].importedMesh).toBe(restored.shapes[0].importedMesh);
}
expect(restored.shapes[0].cadDisplayEdges).toEqual(resource.cadDisplayEdges);
expect(restored.shapes[0].cadBrep).toBe(resource.cadBrep);
hash.mockClear();
await exportSkfProject(input(restored.shapes, { history: restored.history, historyIndex: restored.historyIndex }));
expect(hash).toHaveBeenCalledTimes(2); // One mesh and one edge resource, independent of state count.
});
it("reads V1 inline display edges and interns them before re-saving", async () => {
const original = shape("box", "legacy-edges", { cadDisplayEdges: [{ points: [0, 0, 0, 1, 2, 3] }], cadDisplayEdgesVersion: 2 });
const moved = { ...original, x: 42 };
const history = [editorHistoryEntry([original], []), editorHistoryEntry([moved], [moved.id])];
const packaged = await exportSkfProject(input([moved], { history, historyIndex: 1 }));
const legacy = mutateProject(packaged, (document) => {
document.formatVersion = 1;
document.minimumReaderVersion = 1;
document.assets = document.assets.filter((asset) => asset.kind !== "display-edges");
for (const state of document.states) for (const node of state.nodes) {
node.cadDisplayEdgesAssetId = undefined;
node.definition.cadDisplayEdges = original.cadDisplayEdges;
}
});
const restored = await importSkfProject(legacy);
expect(restored.history).toHaveLength(2);
expect(restored.history[0].shapes[0].cadDisplayEdges).toBe(restored.shapes[0].cadDisplayEdges);
const saved = packageDocument(await exportSkfProject(input(restored.shapes, { history: restored.history, historyIndex: 1 })));
expect(saved.document.assets.filter((asset) => asset.kind === "display-edges")).toHaveLength(1);
});
it("keeps replaced edge and mesh arrays distinct even when their lengths match", async () => {
const original = shape("mesh", "changed-resources", {
cadDisplayEdges: [{ points: [0, 0, 0, 1, 1, 1] }],
importedMesh: { positions: [0, 0, 0, 1, 0, 0, 0, 1, 0], baseWidth: 1, baseDepth: 1, baseHeight: 1, triangleCount: 1, sourceFormat: "json" },
});
await exportSkfProject(input([original]));
const changed = { ...original, cadDisplayEdges: [{ points: [0, 0, 0, 2, 2, 2] }], importedMesh: { ...original.importedMesh!, normals: original.importedMesh!.positions } };
const history = [editorHistoryEntry([original], []), editorHistoryEntry([changed], [])];
const restored = await importSkfProject(await exportSkfProject(input([changed], { history, historyIndex: 1 })));
expect(restored.history[0].shapes[0].cadDisplayEdges).toEqual(original.cadDisplayEdges);
expect(restored.shapes[0].cadDisplayEdges).toEqual(changed.cadDisplayEdges);
expect(restored.history[0].shapes[0].importedMesh?.normals).toBeUndefined();
expect(restored.shapes[0].importedMesh?.normals).toEqual(changed.importedMesh.normals);
});
it("rejects missing and corrupt display-edge resources", async () => {
const bytes = await exportSkfProject(input([shape("box", "bad-edges", { cadDisplayEdges: [{ points: [0, 0, 0] }] })]));
await expect(importSkfProject(mutateProject(bytes, (document) => {
document.states[0].nodes[0].cadDisplayEdgesAssetId = "missing";
}))).rejects.toThrow(/display-edge asset/);
const { files, document } = packageDocument(bytes);
const edge = document.assets.find((asset) => asset.kind === "display-edges")!;
files[edge.path] = strToU8('[{"points":["invalid",0,0]}]');
edge.byteLength = files[edge.path].length;
edge.sha256 = await projectAssets.sha256Hex(files[edge.path]);
files["project.json"] = strToU8(JSON.stringify(document));
await expect(importSkfProject(zipSync(files))).rejects.toThrow(/display-edge coordinates/);
});
it("round-trips every supported native shape kind and editable properties", async () => {
const nativeKinds: ShapeKind[] = [
"box", "cylinder", "sphere", "sketch", "scribble", "cone", "pyramid", "roof", "text", "roundRoof",
"halfSphere", "torus", "tube", "gear", "ring", "wedge", "polygon", "icosahedron",
];
const shapes = nativeKinds.map((kind, index) => shape(kind, `${kind}-${index}`, {
hole: index === 2,
locked: index === 3,
hidden: index === 4,
mirrorX: index === 5,
sides: index + 3,
teeth: kind === "gear" ? 18 : undefined,
toothSize: kind === "gear" ? 3.25 : undefined,
toothWidth: kind === "gear" ? 2.75 : undefined,
centerHoleSize: kind === "gear" ? 7.5 : undefined,
gearType: kind === "gear" ? "helical" : undefined,
helixAngle: kind === "gear" ? -30 : undefined,
helixQuality: kind === "gear" ? 24 : undefined,
text: kind === "text" ? "Editable" : undefined,
sketchProfile: kind === "sketch" ? {
points: [
{ id: "p1", x: 0, z: 0 },
{ id: "p2", x: 10, z: 0 },
{ id: "p3", x: 10, z: 10 },
],
segments: [
{ id: "s1", startId: "p1", endId: "p2", kind: "line" },
{ id: "s2", startId: "p2", endId: "p3", kind: "bezier" },
{ id: "s3", startId: "p3", endId: "p1", kind: "line" },
],
} : undefined,
}));
const customWorkspace = { ...DEFAULT_WORKPLANE_WORKSPACE, gridColor: "#a34fd1" };
const exported = await exportSkfProject(input(shapes, { workspace: customWorkspace }));
const { document } = packageDocument(exported);
const summary = await inspectSkfProjectPackage(exported);
const restored = await importSkfProject(exported);
expect(restored.projectName).toBe("Round trip");
expect(restored.sourceProjectId).toBe("project-original");
expect(JSON.stringify(restored.shapes)).toBe(JSON.stringify(shapes.map(canonicalizeShape)));
expect(restored.workspace).toEqual(customWorkspace);
expect(restored.snapGrid).toBe(DEFAULT_SNAP_GRID);
expect(restored.placementElevation).toBe(12.5);
expect(summary).toEqual({
projectName: "Round trip",
createdAt: 1_700_000_000_000,
modifiedAt: 1_700_000_100_000,
formatVersion: SKF_FORMAT_VERSION,
});
expect(document.assets.filter((entry) => entry.kind === "derived-mesh")).toHaveLength(0);
});
it("preserves an oriented placement workplane", async () => {
const placementWorkplane = placementWorkplaneFromSurface(
{ x: 12, y: 8, z: -3 },
{ x: 1, y: 0, z: 0 },
{ x: 0, y: 1, z: 0 },
);
const sketchPlacementWorkplane = placementWorkplaneFromSurface(
{ x: -4, y: 6, z: 9 },
{ x: 0, y: 0, z: -1 },
{ x: 1, y: 0, z: 0 },
);
const exported = await exportSkfProject(input([shape("box")], {
placementElevation: 0,
placementWorkplane,
sketchPlacementWorkplane,
}));
const restored = await importSkfProject(exported);
expect(restored.placementWorkplane).toEqual(placementWorkplane);
expect(restored.sketchPlacementWorkplane).toEqual(sketchPlacementWorkplane);
});
it("preserves editable revolve sketch settings and generated geometry", async () => {
const revolve = shape("mesh", "revolve-sketch", {
name: "Sketch revolve",
sketchOperation: "revolve",
sketchRevolve: { startAngle: 25, sweepAngle: -220, sides: 48, quality: 8 },
sketchProfile: {
points: [{ id: "p1", x: -4, z: 0 }, { id: "p2", x: -8, z: 0 }, { id: "p3", x: -8, z: 16 }, { id: "p4", x: -4, z: 16 }],
segments: [
{ id: "s1", startId: "p1", endId: "p2", kind: "line" },
{ id: "s2", startId: "p2", endId: "p3", kind: "line" },
{ id: "s3", startId: "p3", endId: "p4", kind: "line" },
{ id: "s4", startId: "p4", endId: "p1", kind: "line" },
],
},
importedMesh: {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 16,
baseDepth: 16,
baseHeight: 16,
triangleCount: 1,
sourceFormat: "json",
},
});
const restored = await importSkfProject(await exportSkfProject(input([revolve])));
expect(restored.shapes[0].sketchOperation).toBe("revolve");
expect(restored.shapes[0].sketchRevolve).toEqual(revolve.sketchRevolve);
expect(restored.shapes[0].sketchProfile).toEqual(revolve.sketchProfile);
expect(restored.shapes[0].importedMesh?.positions).toEqual(revolve.importedMesh?.positions);
});
it("preserves nested groups, holes, intersection metadata, edge history, B-Rep, and undo/redo", async () => {
const cadDisplayEdges = [{ points: [0, 0, 0, 1, 2, 3] }];
const solid = shape("box", "solid", { x: 0, cadDisplayEdges });
const hole = shape("cylinder", "hole", { hole: true, color: "#b8c2cc", x: 4 });
const group = shape("mesh", "group", {
name: "Intersection",
groupOperation: "intersection",
groupedBaseWidth: 30,
groupedBaseDepth: 30,
groupedBaseHeight: 20,
groupedShapes: [solid, hole],
importedMesh: {
positions: [0, 0, 0, 2, 0, 0, 0, 2, 0],
normals: [0, 0, 1, 0, 0, 1, 0, 0, 1],
baseWidth: 2,
baseDepth: 1,
baseHeight: 2,
triangleCount: 1,
sourceFormat: "json",
},
cadBrep: "BREP exact payload",
cadDisplayEdges,
edgeTreatments: [{ kind: "fillet", amount: 1.25, edgeCount: 3 }],
edgeTreatmentHistory: [{
id: "edge-history-1",
createdAt: 1_700_000_050_000,
feature: { kind: "fillet", amount: 1.25, edgeCount: 3 },
before: shape("box", "group", { width: 30, depth: 30, height: 20, cadDisplayEdges }),
}],
});
const before = editorHistoryEntry([solid, hole], ["solid", "hole"]);
const after = editorHistoryEntry([group], ["group"]);
const exported = await exportSkfProject(input([group], { history: [before, after], historyIndex: 1 }));
const { document } = packageDocument(exported);
expect(document.groups[0].operation).toBe("boolean-intersection");
expect(document.features.some((feature) => feature.type === "fillet")).toBe(true);
expect(document.exactCad).toHaveLength(1);
expect(document.assets.filter((asset) => asset.kind === "display-edges")).toHaveLength(1);
const restored = await importSkfProject(exported);
expect(restored.history).toHaveLength(2);
expect(restored.historyIndex).toBe(1);
expect(restored.shapes[0].groupedShapes).toEqual([solid, hole]);
expect(restored.shapes[0].groupOperation).toBe("intersection");
expect(restored.shapes[0].cadBrep).toBe("BREP exact payload");
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 };
const moved = { ...resized, x: 18 };
const history = [
editorHistoryEntry([created], ["history-box"]),
editorHistoryEntry([resized], ["history-box"]),
editorHistoryEntry([moved], ["history-box"]),
];
const restored = await importSkfProject(await exportSkfProject(input([resized], {
history,
historyIndex: 1,
compressionLevel: 1,
})));
expect(restored.history).toHaveLength(3);
expect(restored.historyIndex).toBe(1);
expect(restored.history[0].shapes[0]).toMatchObject({ width: 20, x: 0 });
expect(restored.history[1].shapes[0]).toMatchObject({ width: 30, x: 0 });
expect(restored.history[2].shapes[0]).toMatchObject({ width: 30, x: 18 });
expect(restored.shapes[0]).toMatchObject({ width: 30, x: 0 });
});
it("stores one original source asset for repeated imported instances and regenerates it once", async () => {
const sourceBytes = strToU8("solid source");
const asset = await projectAssetFromBytes("shared.stl", "stl", sourceBytes);
const importedMesh: NonNullable<WorkplaneShape["importedMesh"]> = {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 1,
baseDepth: 1,
baseHeight: 1,
triangleCount: 1,
sourceFormat: "stl",
assetId: asset.id,
};
const instances = [
shape("mesh", "instance-a", { importedMesh, x: 0 }),
shape("mesh", "instance-b", { importedMesh, x: 30 }),
];
const exported = await exportSkfProject(input(instances, { assets: [asset] }));
const { document } = packageDocument(exported);
let importCalls = 0;
const restored = await importSkfProject(exported, {
sourceImporter: async () => {
importCalls += 1;
return { ...importedMesh, assetId: undefined };
},
});
expect(document.assets.filter((entry) => entry.kind === "source")).toHaveLength(1);
expect(document.assets.filter((entry) => entry.kind === "derived-mesh")).toHaveLength(0);
expect(restored.assets).toHaveLength(1);
expect(restored.shapes[0].importedMesh?.assetId).toBe(restored.shapes[1].importedMesh?.assetId);
expect(importCalls).toBe(1);
});
it("exports and imports source-backed projects without Web Crypto", async () => {
const sourceBytes = strToU8("solid http source");
const asset = await projectAssetFromBytes("http.stl", "stl", sourceBytes);
const importedMesh: NonNullable<WorkplaneShape["importedMesh"]> = {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 1,
baseDepth: 1,
baseHeight: 1,
triangleCount: 1,
sourceFormat: "stl",
assetId: asset.id,
};
vi.stubGlobal("crypto", {});
const exported = await exportSkfProject(input([shape("mesh", "http-object", { importedMesh })], { assets: [asset] }));
const restored = await importSkfProject(exported, {
sourceImporter: async () => ({ ...importedMesh, assetId: undefined }),
});
expect(packageDocument(exported).document.assets[0].sha256).toBe(asset.sha256);
expect(restored.assets[0].sha256).toBe(asset.sha256);
expect(restored.shapes[0].importedMesh?.assetId).toBe(restored.assets[0].id);
});
it("shares decoded derived-mesh arrays across restored history states", async () => {
const importedMesh: NonNullable<WorkplaneShape["importedMesh"]> = {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
normals: [0, 0, 1, 0, 0, 1, 0, 0, 1],
baseWidth: 1,
baseDepth: 1,
baseHeight: 1,
triangleCount: 1,
sourceFormat: "json",
};
const before = shape("mesh", "derived-history", { importedMesh, x: 0 });
const after = { ...before, x: 20 };
const history = [editorHistoryEntry([before], []), editorHistoryEntry([after], [after.id])];
const restored = await importSkfProject(await exportSkfProject(input([after], { history, historyIndex: 1 })));
const firstPositions = restored.history[0].shapes[0].importedMesh?.positions;
const secondPositions = restored.history[1].shapes[0].importedMesh?.positions;
expect(firstPositions).toBe(secondPositions);
expect(restored.shapes[0].importedMesh?.positions).toBe(secondPositions);
});
it.each(["svg", "step"] as const)("stores and restores original %s sources", async (sourceFormat) => {
const asset = await projectAssetFromBytes(`source.${sourceFormat}`, sourceFormat, strToU8(`${sourceFormat} source`));
const importedMesh: NonNullable<WorkplaneShape["importedMesh"]> = {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 1,
baseDepth: 1,
baseHeight: 1,
triangleCount: 1,
sourceFormat,
assetId: asset.id,
...(sourceFormat === "step" ? { brepStep: "exact STEP" } : {}),
};
const exported = await exportSkfProject(input([shape("mesh", `${sourceFormat}-object`, { importedMesh })], { assets: [asset] }));
const { document } = packageDocument(exported);
const restored = await importSkfProject(exported, { sourceImporter: async () => importedMesh });
expect(document.assets.filter((entry) => entry.kind === "source")).toHaveLength(1);
expect(document.assets.filter((entry) => entry.kind === "derived-mesh")).toHaveLength(0);
expect(restored.assets[0].sourceFormat).toBe(sourceFormat);
expect(restored.shapes[0].importedMesh?.sourceFormat).toBe(sourceFormat);
if (sourceFormat === "step") expect(document.exactCad[0].importedStepAssetId).toBeTruthy();
});
it("deduplicates and restores reference images stored in sketches and image plates", async () => {
const dataUrl = "data:image/png;base64,AAECAwQ=";
const imageShape = shape("sketch", "sketch-images", {
imagePlate: { dataUrl, mimeType: "image/png", pixelWidth: 2, pixelHeight: 2 },
sketchProfile: {
points: [
{ id: "p1", x: 0, z: 0 },
{ id: "p2", x: 10, z: 0 },
{ id: "p3", x: 0, z: 10 },
],
segments: [
{ id: "s1", startId: "p1", endId: "p2" },
{ id: "s2", startId: "p2", endId: "p3" },
{ id: "s3", startId: "p3", endId: "p1" },
],
images: [{
id: "image-1",
name: "Reference",
dataUrl,
mimeType: "image/png",
pixelWidth: 2,
pixelHeight: 2,
x: 0,
z: 0,
width: 10,
depth: 10,
lockAspect: true,
locked: true,
}],
},
});
const exported = await exportSkfProject(input([imageShape]));
const { document } = packageDocument(exported);
const restored = await importSkfProject(exported);
expect(document.assets.filter((entry) => entry.kind === "image")).toHaveLength(1);
expect(restored.shapes[0].imagePlate?.dataUrl).toBe(dataUrl);
expect(restored.shapes[0].sketchProfile?.images?.[0].dataUrl).toBe(dataUrl);
expect(restored.shapes[0].sketchProfile?.images?.[0].locked).toBe(true);
});
it("deduplicates derived geometry when legacy imported objects share one mesh", async () => {
const mesh = {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 1,
baseDepth: 1,
baseHeight: 1,
triangleCount: 1,
sourceFormat: "json" as const,
};
const exported = await exportSkfProject(input([
shape("mesh", "legacy-a", { importedMesh: mesh }),
shape("mesh", "legacy-b", { importedMesh: mesh }),
]));
const { document } = packageDocument(exported);
const restored = await importSkfProject(exported);
expect(document.assets.filter((entry) => entry.kind === "derived-mesh")).toHaveLength(1);
expect(restored.shapes[0].importedMesh?.positions).toEqual(mesh.positions);
expect(restored.shapes[1].importedMesh?.positions).toEqual(mesh.positions);
});
it("exports in a far-western timezone without underflowing the ZIP 1980 date floor", async () => {
// ZIP stores entry mtimes as DOS dates, and fflate encodes them with local-time
// getters, rejecting years outside 1980-2099 with "date not in range 1980-2099".
// A UTC-pinned epoch rolls back to 1979 west of UTC, so exercise the fix under the
// most extreme western offset. Node re-reads process.env.TZ per Date operation.
const originalTz = process.env.TZ;
process.env.TZ = "Etc/GMT+12";
try {
const exported = await exportSkfProject(input([shape("box")]));
const restored = await importSkfProject(exported);
expect(restored.shapes[0].kind).toBe("box");
} finally {
if (originalTz === undefined) delete process.env.TZ;
else process.env.TZ = originalTz;
}
});
it("writes compact project data that stays compatible with the reader limit", async () => {
const exported = await exportSkfProject(input([shape("box")]));
const { files, document } = packageDocument(exported);
const projectJson = strFromU8(files["project.json"]);
expect(projectJson).toBe(JSON.stringify(document));
await expect(importSkfProject(exported)).resolves.toMatchObject({ projectName: "Round trip" });
});
it("refuses to create an .skf package that its reader cannot reopen", async () => {
const oversizedName = "x".repeat(SKF_LIMITS.projectJsonBytes);
await expect(exportSkfProject(input([shape("box")], { projectName: oversizedName })))
.rejects.toThrow("Project data exceeds the 64 MB .skf limit");
});
it("migrates the documented v0 JSON project without changing IDs", async () => {
const box = shape("box", "legacy-box");
const legacy = strToU8(JSON.stringify({
schema: SKF_SCHEMA_ID,
formatVersion: 0,
project: { id: "legacy-project", name: "Legacy", createdAt: 100, modifiedAt: 200 },
shapes: [box],
workspace: DEFAULT_WORKPLANE_WORKSPACE,
snapGrid: "0.5 mm",
}));
const restored = await importSkfProject(legacy);
expect(restored.migratedFromVersion).toBe(0);
expect(restored.sourceProjectId).toBe("legacy-project");
expect(restored.shapes[0].id).toBe("legacy-box");
expect(restored.snapGrid).toBe("0.5 mm");
});
it("rejects unsupported future versions before restoring any state", async () => {
const exported = await exportSkfProject(input([shape("box")]));
const future = mutateProject(exported, (document) => {
(document as { formatVersion: number }).formatVersion = SKF_FORMAT_VERSION + 1;
});
await expect(importSkfProject(future)).rejects.toThrow("requires a newer SketchForge version");
});
it("rejects missing references, duplicate IDs, cyclic groups, unknown operations, and corrupt assets", async () => {
const twoShapes = await exportSkfProject(input([shape("box", "one"), shape("box", "two")]));
const missing = mutateProject(twoShapes, (document) => {
document.states[0].rootNodeIds[0] = "missing-node";
});
await expect(importSkfProject(missing)).rejects.toThrow("missing node");
const duplicate = mutateProject(twoShapes, (document) => {
const [first, second] = document.states[0].nodes;
second.objectId = first.objectId;
second.definition.id = first.objectId;
});
await expect(importSkfProject(duplicate)).rejects.toThrow("duplicate object ID");
const grouped = await exportSkfProject(input([shape("mesh", "group-cycle", {
groupedShapes: [shape("box", "child-a"), shape("box", "child-b")],
})]));
const cyclic = mutateProject(grouped, (document) => {
const root = document.states[0].nodes.find((node) => node.objectId === "group-cycle")!;
root.groupedShapeNodeIds = [root.nodeId];
});
await expect(importSkfProject(cyclic)).rejects.toThrow("Cyclic group");
const unknownOperation = mutateProject(grouped, (document) => {
document.features[0].type = "unsupported-operation";
});
await expect(importSkfProject(unknownOperation)).rejects.toThrow("Unknown operation type");
const legacyMesh = await exportSkfProject(input([shape("mesh", "mesh", {
importedMesh: {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 1,
baseDepth: 1,
baseHeight: 1,
triangleCount: 1,
sourceFormat: "json",
},
})]));
const { files, document } = packageDocument(legacyMesh);
const asset = document.assets.find((entry) => entry.kind === "derived-mesh")!;
files[asset.path][20] ^= 0xff;
await expect(importSkfProject(zipSync(files))).rejects.toThrow("integrity check");
});
});
function rectangleDxf(width: number, height: number) {
return `0
SECTION
2
ENTITIES
0
LWPOLYLINE
8
0
90
4
70
1
10
0.0
20
0.0
10
${width}
20
0.0
10
${width}
20
${height}
10
0.0
20
${height}
0
ENDSEC
0
EOF`;
}
describe("Imported CAD source assets survive .skf round trips", () => {
it("reconstructs a DXF source asset when a project is reopened", async () => {
const dxf = rectangleDxf(40, 20);
const original = importedShapeFromDxf("plan.dxf", dxf);
const asset = await projectAssetFromBytes("plan.dxf", "dxf", strToU8(dxf));
const packaged = await exportSkfProject(input([attachProjectAsset(original, asset.id)], { assets: [asset] }));
// DXF kaynak dosyası yeniden üretilebildiği için paket yalnızca kaynağı taşır.
const { document } = packageDocument(packaged);
expect(document.assets.filter((entry) => entry.kind === "source").map((entry) => entry.sourceFormat)).toEqual(["dxf"]);
expect(document.assets.some((entry) => entry.kind === "derived-mesh")).toBe(false);
const restored = await importSkfProject(packaged);
expect(restored.shapes).toHaveLength(1);
expect(restored.shapes[0].kind).toBe("mesh");
expect(restored.shapes[0].importedMesh?.sourceFormat).toBe("dxf");
expect(restored.shapes[0].importedMesh?.triangleCount).toBe(original.importedMesh?.triangleCount);
expect(restored.shapes[0].width).toBeCloseTo(40, 0);
expect(restored.shapes[0].depth).toBeCloseTo(20, 0);
});
it("accepts a DWG source asset and reuses the stored derived mesh", async () => {
// Gerçek bir DWG içe aktarımı, kaynak varlığını "dwg" biçiminde saklar ama
// şeklin mesh'i DXF'ten üretildiği için sourceFormat "dxf" olarak kalır.
// Bu birleşim daha önce "unknown source format" hatasıyla projeyi boş açıyordu.
const importedMesh = {
positions: [0, 0, 0, 10, 0, 0, 0, 10, 0],
baseWidth: 270,
baseDepth: 280,
baseHeight: 4,
triangleCount: 1,
sourceFormat: "dxf" as const,
};
const dwgShape = shape("mesh", "dwg-mesh", { importedMesh });
const dwgBytes = strToU8("AC1032");
const asset = await projectAssetFromBytes("plan.dwg", "dwg", dwgBytes);
const packaged = await exportSkfProject(input([attachProjectAsset(dwgShape, asset.id)], { assets: [asset] }));
const { document } = packageDocument(packaged);
expect(document.assets.map((entry) => [entry.kind, entry.sourceFormat])).toContainEqual(["source", "dwg"]);
expect(document.assets.some((entry) => entry.kind === "derived-mesh")).toBe(true);
// Kaynak DWG dönüştürülmeden, paketteki hazır mesh ile açılmalı.
const restored = await importSkfProject(packaged);
expect(restored.shapes).toHaveLength(1);
expect(restored.shapes[0].importedMesh?.triangleCount).toBe(1);
expect(restored.shapes[0].importedMesh?.baseWidth).toBe(270);
});
it("routes a DWG source asset to the DWG importer when no derived mesh is stored", async () => {
const dwgShape = shape("mesh", "dwg-source-only", {
importedMesh: {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 1,
baseDepth: 1,
baseHeight: 1,
triangleCount: 1,
sourceFormat: "dwg",
},
});
const asset = await projectAssetFromBytes("source-only.dwg", "dwg", strToU8("AC1032"));
const packaged = await exportSkfProject(input([attachProjectAsset(dwgShape, asset.id)], { assets: [asset] }));
const { document } = packageDocument(packaged);
expect(document.assets.some((entry) => entry.kind === "derived-mesh")).toBe(false);
const restored = await importSkfProject(packaged);
expect(restored.shapes).toHaveLength(1);
expect(restored.shapes[0].importedMesh?.baseWidth).toBe(4321);
expect(restored.shapes[0].importedMesh?.sourceFormat).toBe("dwg");
});
});