2026-06-14 22:05:58 +03:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
import type { WorkplaneWorkspaceSettings } from "@/types/sketchforge";
|
2026-07-24 11:55:44 +03:00
|
|
|
import { formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay, normalizeScaleForUnits, parseMeasurementInput, scaleOptionsForUnits } from "@/lib/measurementUnits";
|
2026-08-23 13:28:52 +03:00
|
|
|
import { canBeginShapeDrag, DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint, workspaceHydrationSyncDecision } from "@/lib/workplaneSettings";
|
2026-06-14 22:05:58 +03:00
|
|
|
|
|
|
|
|
describe("workplane settings helpers", () => {
|
|
|
|
|
it("accepts known snap grid values and falls back for unknown values", () => {
|
|
|
|
|
expect(normalizeSnapGrid("0.5 mm")).toBe("0.5 mm");
|
|
|
|
|
expect(normalizeSnapGrid("Huge")).toBe(DEFAULT_SNAP_GRID);
|
|
|
|
|
expect(normalizeSnapGrid(null, "Off")).toBe("Off");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("normalizes workspace settings from partial or invalid data", () => {
|
|
|
|
|
const fallback: WorkplaneWorkspaceSettings = {
|
|
|
|
|
...DEFAULT_WORKPLANE_WORKSPACE,
|
|
|
|
|
width: 300,
|
|
|
|
|
depth: 250,
|
|
|
|
|
background: "#ffffff",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
expect(normalizeWorkspaceSettings(null, fallback)).toEqual(fallback);
|
|
|
|
|
expect(
|
|
|
|
|
normalizeWorkspaceSettings(
|
|
|
|
|
{
|
|
|
|
|
width: 500,
|
|
|
|
|
depth: Number.NaN,
|
|
|
|
|
sizePreset: "",
|
|
|
|
|
gridBlockSize: 2.5,
|
|
|
|
|
gridBlockPreset: "Custom",
|
2026-07-28 14:52:59 +03:00
|
|
|
gridColor: "#a34fd1",
|
2026-06-14 22:05:58 +03:00
|
|
|
background: "#123456",
|
|
|
|
|
showShadows: false,
|
|
|
|
|
showGrid: false,
|
|
|
|
|
cruiseShapes: false,
|
2026-08-23 13:28:52 +03:00
|
|
|
selectBeforeMove: true,
|
2026-06-14 22:05:58 +03:00
|
|
|
zoomSpeed: Infinity,
|
|
|
|
|
units: "Bricks",
|
|
|
|
|
scale: "1:10 (centimeters)",
|
2026-06-19 21:47:13 +03:00
|
|
|
accuracy: 3,
|
2026-06-14 22:05:58 +03:00
|
|
|
},
|
|
|
|
|
fallback,
|
|
|
|
|
),
|
|
|
|
|
).toEqual({
|
|
|
|
|
...fallback,
|
|
|
|
|
width: 500,
|
|
|
|
|
gridBlockSize: 2.5,
|
|
|
|
|
gridBlockPreset: "Custom",
|
2026-07-28 14:52:59 +03:00
|
|
|
gridColor: "#a34fd1",
|
2026-06-14 22:05:58 +03:00
|
|
|
background: "#123456",
|
|
|
|
|
showShadows: false,
|
|
|
|
|
showGrid: false,
|
|
|
|
|
cruiseShapes: false,
|
2026-08-23 13:28:52 +03:00
|
|
|
selectBeforeMove: true,
|
2026-06-14 22:05:58 +03:00
|
|
|
units: "Bricks",
|
2026-07-05 12:28:23 +03:00
|
|
|
scale: "1:1 (studs)",
|
2026-06-19 21:47:13 +03:00
|
|
|
accuracy: 3,
|
2026-06-14 22:05:58 +03:00
|
|
|
});
|
2026-06-19 21:47:13 +03:00
|
|
|
|
|
|
|
|
expect(normalizeWorkspaceSettings({ accuracy: 9 }, fallback).accuracy).toBe(fallback.accuracy);
|
2026-07-28 14:52:59 +03:00
|
|
|
expect(normalizeWorkspaceSettings({ historyLimit: 73 }).historyLimit).toBe(73);
|
|
|
|
|
expect(normalizeWorkspaceSettings({ historyLimit: 9000 }).historyLimit).toBe(5000);
|
2026-08-02 17:38:53 +03:00
|
|
|
expect(normalizeWorkspaceSettings({ historyLimit: "invalid" }).historyLimit).toBe(100);
|
2026-07-28 14:52:59 +03:00
|
|
|
expect(normalizeWorkspaceSettings({ gridColor: "not-a-color" }).gridColor).toBe(DEFAULT_WORKPLANE_WORKSPACE.gridColor);
|
2026-06-14 22:05:58 +03:00
|
|
|
});
|
|
|
|
|
|
2026-08-23 13:28:52 +03:00
|
|
|
it("can require selection before a shape starts moving", () => {
|
|
|
|
|
expect(canBeginShapeDrag(false, false)).toBe(true);
|
|
|
|
|
expect(canBeginShapeDrag(true, false)).toBe(false);
|
|
|
|
|
expect(canBeginShapeDrag(true, true)).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-05 12:28:23 +03:00
|
|
|
it("keeps scale options in the selected unit family", () => {
|
|
|
|
|
expect(scaleOptionsForUnits("Metric (Default)")).toEqual(["1:1 (millimeters)", "1:10 (centimeters)", "1:1000 (meters)"]);
|
|
|
|
|
expect(normalizeScaleForUnits("Metric (Default)", "1:100 (meters)")).toBe("1:1000 (meters)");
|
|
|
|
|
expect(scaleOptionsForUnits("Imperial")).toEqual(["1:1 (inches)", "1:1 (feet)"]);
|
|
|
|
|
expect(normalizeScaleForUnits("Imperial", "1:100 (meters)")).toBe("1:1 (inches)");
|
|
|
|
|
expect(normalizeWorkspaceSettings({ units: "Imperial", scale: "1:100 (meters)" }).scale).toBe("1:1 (inches)");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("changes display units without scaling model values", () => {
|
|
|
|
|
expect(lengthDisplayUnit({ units: "Metric (Default)", scale: "1:1000 (meters)" }).label).toBe("m");
|
|
|
|
|
expect(millimetersToDisplay(20, { units: "Metric (Default)", scale: "1:1 (millimeters)" })).toBe(20);
|
|
|
|
|
expect(millimetersToDisplay(20, { units: "Metric (Default)", scale: "1:1000 (meters)" })).toBe(0.02);
|
|
|
|
|
expect(millimetersToDisplay(20, { units: "Metric (Default)", scale: "1:100 (meters)" })).toBe(0.02);
|
|
|
|
|
expect(Number(millimetersToDisplay(25.4, { units: "Imperial", scale: "1:1 (inches)" }).toFixed(4))).toBe(1);
|
|
|
|
|
expect(Number(millimetersToDisplay(304.8, { units: "Imperial", scale: "1:1 (feet)" }).toFixed(4))).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("uses accuracy as display precision", () => {
|
|
|
|
|
expect(formatMeasurementNumber(20, 1, 0.01)).toBe("20.0");
|
|
|
|
|
expect(formatMeasurementNumber(20, 3, 0.01)).toBe("20.000");
|
|
|
|
|
expect(formatMeasurementNumber(0.0004, 1, 0.001)).toBe("0.0004");
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-24 11:55:44 +03:00
|
|
|
it("accepts dot and comma decimal measurement input", () => {
|
|
|
|
|
expect(parseMeasurementInput("12.5")).toBe(12.5);
|
|
|
|
|
expect(parseMeasurementInput("12,5")).toBe(12.5);
|
|
|
|
|
expect(parseMeasurementInput("1.234,5")).toBe(1234.5);
|
|
|
|
|
expect(parseMeasurementInput("1,234.5")).toBe(1234.5);
|
|
|
|
|
expect(parseMeasurementInput("not a measurement")).toBeNaN();
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-14 22:05:58 +03:00
|
|
|
it("fingerprints workspace and snap settings together", () => {
|
|
|
|
|
const base = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "1.0 mm");
|
2026-07-11 20:34:13 +03:00
|
|
|
const equivalentNewReference = workplaneSettingsFingerprint({ ...DEFAULT_WORKPLANE_WORKSPACE }, "1.0 mm");
|
2026-06-14 22:05:58 +03:00
|
|
|
const changedSnap = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "5.0 mm");
|
|
|
|
|
const changedWorkspace = workplaneSettingsFingerprint({ ...DEFAULT_WORKPLANE_WORKSPACE, width: 300 }, "1.0 mm");
|
|
|
|
|
|
2026-07-11 20:34:13 +03:00
|
|
|
expect(equivalentNewReference).toBe(base);
|
2026-06-14 22:05:58 +03:00
|
|
|
expect(changedSnap).not.toBe(base);
|
|
|
|
|
expect(changedWorkspace).not.toBe(base);
|
|
|
|
|
});
|
2026-07-14 09:43:37 +03:00
|
|
|
|
|
|
|
|
it("blocks the previous project's workspace while a new project hydrates", () => {
|
|
|
|
|
const projectOne = workplaneSettingsFingerprint({ ...DEFAULT_WORKPLANE_WORKSPACE, width: 350, depth: 260 }, "5.0 mm");
|
|
|
|
|
const projectTwo = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "1.0 mm");
|
|
|
|
|
|
|
|
|
|
const staleCommit = workspaceHydrationSyncDecision(projectTwo, projectOne);
|
|
|
|
|
expect(staleCommit).toEqual({ shouldSync: false, pendingFingerprint: projectTwo });
|
|
|
|
|
|
|
|
|
|
const hydratedCommit = workspaceHydrationSyncDecision(staleCommit.pendingFingerprint, projectTwo);
|
|
|
|
|
expect(hydratedCommit).toEqual({ shouldSync: false, pendingFingerprint: null });
|
|
|
|
|
|
|
|
|
|
expect(workspaceHydrationSyncDecision(hydratedCommit.pendingFingerprint, projectTwo).shouldSync).toBe(true);
|
|
|
|
|
});
|
2026-06-14 22:05:58 +03:00
|
|
|
});
|