// Run: npx tsx tests/svg.test.ts - SVG → sketch curves. import assert from "node:assert/strict"; import { parseSVG } from "../src/io/svg"; import { arrangement } from "../src/sketch/regions"; const near = (a: number, b: number, tol = 1e-6) => Math.abs(a - b) <= tol * Math.max(1, Math.abs(b)); const checks: [string, () => string][] = [ ["mm units from width + viewBox; rect / circle become those shapes", () => { const { entities, size } = parseSVG(` `); const kinds = entities.map((e) => e.kind).sort().join(","); assert.equal(kinds, "circle,rect"); const r = entities.find((e) => e.kind === "rect") as any; const c = entities.find((e) => e.kind === "circle") as any; // 200 user units = 100 mm → 0.5 mm each. assert.ok(near(r.w, 40) && near(r.h, 20) && near(c.radius, 10), JSON.stringify([r, c])); assert.equal(arrangement(entities).regions.length, 2); return `size ${size.map((v) => v.toFixed(1)).join(" × ")} mm`; }], ["path with lines, a cubic, an arc and close → one closed region", () => { const { entities } = parseSVG(` `); const kinds = entities.map((e) => e.kind); assert.ok(kinds.includes("spline") && kinds.includes("arc") && kinds.includes("line"), kinds.join()); const regions = arrangement(entities).regions; assert.equal(regions.length, 1); return `${entities.length} curves, area ${regions[0].area.toFixed(0)} mm²`; }], ["relative commands, implicit repeats, groups with transforms; y flips up", () => { const { entities } = parseSVG(` `); assert.equal(entities.length, 4); // A 10 px square → 2.646 mm; the drawing is centred on the origin. const xs = entities.flatMap((e: any) => [e.a[0], e.b[0]]); assert.ok(near(Math.max(...xs) - Math.min(...xs), (10 * 25.4) / 96), String(xs)); assert.equal(arrangement(entities).regions.length, 1); return "ok"; }], ["text is skipped with a clear message when nothing else is there", () => { assert.throws(() => parseSVG(`Hi`), /converted to paths/); return "ok"; }], ]; let failed = 0; for (const [name, fn] of checks) { try { console.log("ok ", name, "-", fn()); } catch (e) { failed++; console.log("FAIL", name, "-", e instanceof Error ? e.message : e); } } process.exit(failed ? 1 : 0);