import assert from "node:assert/strict";
import test from "node:test";
import { loadConverter } from "./svgConverterHarness.mjs";
import { readFileSync } from "node:fs";
const convert = loadConverter();
const svg = (body) => ``;
test("attached Mermaid mindmap retains local variable colors, alpha and labels", () => {
const source = readFileSync(new URL("../../tests/fixtures/mermaid-mindmap-css-variables.svg", import.meta.url), "utf8");
const result = convert(source);
assert.equal(result.hasErrors, false, result.errors);
for (const color of ["#4c78a8", "#f58518", "#54a24b", "#e45756"])
assert.ok(result.content.some(e => e.strokeColor === color), color);
assert.ok(result.content.some(e => e.backgroundColor === "#f3f3f3"));
assert.ok(result.content.some(e => e.backgroundColor === "#4c78a838"));
const labels = result.content.filter(e => e.type === "text");
assert.equal(labels.length, 13);
assert.ok(labels.every(e => e.strokeColor === "#1f1f1f"));
});
test("CSS variables inherit computed values and preserve case, specificity and inline precedence", () => {
const result = convert(svg(`
`));
assert.equal(result.hasErrors, false, result.errors);
assert.equal(result.content[0].backgroundColor, "#ffa500");
assert.equal(result.content[0].strokeColor, "#008000");
});
test("CSS var fallbacks support nested functions, inherited color and local overrides", () => {
const result = convert(svg(`
`));
assert.equal(result.hasErrors, false, result.errors);
assert.equal(result.content[0].backgroundColor, "#ff0000");
assert.equal(result.content[0].strokeColor, "#2468ac");
assert.equal(result.content[1].backgroundColor, "#0000ff");
assert.equal(result.content[1].opacity, 50);
});
test("cyclic and missing SVG variables use their call-site fallback or inherited presentation", () => {
const result = convert(svg(`
`));
assert.equal(result.hasErrors, false, result.errors);
assert.deepEqual(Array.from(result.content, e => e.backgroundColor), ["#ffa500", "#008000", "#008000"]);
assert.ok(result.warnings.includes("css"));
});
test("SVG-local variable initial/unset and prototype-like names remain isolated", () => {
const result = convert(svg(`
`));
assert.equal(result.hasErrors, false, result.errors);
assert.equal(result.content[0].backgroundColor, "#0000ff");
assert.equal(result.content[0].strokeColor, "#800080");
assert.equal(result.content[1].backgroundColor, "#ff0000");
});
test("variable dependencies include unused fallbacks and defined empty values do not use fallback", () => {
const result = convert(svg(`
`));
assert.equal(result.hasErrors,false,result.errors);
assert.equal(result.content[0].backgroundColor,"#0000ff");
assert.equal(result.content[1].backgroundColor,"#000000");
});
test("excessive SVG variable chains and expansion fail atomically", () => {
for (const style of [
Array.from({length:40},(_,i)=>`--v${i}:var(--v${i+1})`).join(';')+';fill:var(--v0)',
'--v0:red;'+Array.from({length:16},(_,i)=>`--v${i+1}:var(--v${i})var(--v${i})`).join(';')+';fill:var(--v16)',
]) {
const result = convert(svg(``));
assert.equal(result.hasErrors, true);
assert.equal(result.content.length, 0);
assert.ok(result.errors);
}
});
test("excessive per-element custom property storage fails before descendant expansion", () => {
const style=Array.from({length:1025},(_,i)=>`--v${i}:red`).join(';');
const result=convert(svg(``));
assert.equal(result.hasErrors,true);assert.equal(result.content.length,0);
});
test("SVG alpha uses percent units and preserves zero and near-zero opacity", () => {
for (const opacity of [0, 0.005, 0.5, 1]) {
const result = convert(
svg(``),
);
assert.equal(result.hasErrors, false);
assert.equal(result.content[0].opacity, opacity * 100);
}
});
test("root styles inherit to open primitives without closing their strokes", () => {
const result = convert(
'',
);
assert.equal(result.hasErrors, false);
assert.equal(result.content.length, 2);
for (const element of result.content) {
assert.equal(element.type, "line");
assert.equal(element.polygon, false);
assert.equal(element.backgroundColor, "transparent");
assert.equal(element.strokeColor, "#ff0000");
}
});
test("compound holes never become white patches", () => {
const result = convert(
svg(''),
);
assert.equal(result.hasErrors, false);
assert.ok(result.content.length > 0);
assert.ok(
result.content.every(
(element) => !/white|#fff(?:fff)?$/i.test(element.backgroundColor),
),
);
});
function elements(body, root = "") {
const result = convert(
``,
);
assert.equal(result.hasErrors, false, result.errors);
return result.content;
}
function inside(element, x, y) {
if (element.type !== "line" || element.backgroundColor === "transparent")
return false;
const ring = element.points.map((p) => [p[0] + element.x, p[1] + element.y]);
let hit = false;
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
const a = ring[i],
b = ring[j];
if (
a[1] > y !== b[1] > y &&
x < ((b[0] - a[0]) * (y - a[1])) / (b[1] - a[1]) + a[0]
)
hit = !hit;
}
return hit;
}
test("holes expose the underlying scene; nested islands and disjoint regions retain fill", () => {
const output = elements(
'',
);
for (const [x, y, expected] of [
[2, 2, true],
[7, 7, false],
[20, 20, true],
[55, 5, true],
[45, 5, false],
])
assert.equal(
output.some((e) => inside(e, x, y)),
expected,
`${x},${y}`,
);
});
test("nonzero and evenodd distinguish same-winding nested contours", () => {
for (const [rule, filled] of [
["nonzero", true],
["evenodd", false],
]) {
const output = elements(
``,
);
assert.equal(
output.some((e) => inside(e, 15, 15)),
filled,
);
}
});
test("intersecting contours and self-intersections follow the selected fill rule", () => {
for (const [rule, overlap] of [
["nonzero", true],
["evenodd", false],
]) {
const output = elements(
``,
);
assert.equal(
output.some((e) => inside(e, 5, 10)),
true,
);
assert.equal(
output.some((e) => inside(e, 15, 10)),
overlap,
);
assert.equal(
output.some((e) => inside(e, 25, 10)),
true,
);
}
const bow = elements('');
assert.equal(
bow.some((e) => inside(e, 10, 3)),
true,
);
assert.equal(
bow.some((e) => inside(e, 10, 17)),
true,
);
assert.equal(
bow.some((e) => inside(e, 1, 10)),
false,
);
});
test("overlapping nonzero contours produce no doubly painted translucent area", () => {
const output = elements(
'',
);
assert.equal(output.filter((e) => inside(e, 15, 10)).length, 1);
});
test("opposite overlapping windings cancel only their shared area", () => {
const output = elements('');
assert.equal(
output.some((e) => inside(e, 5, 10)),
true,
);
assert.equal(
output.some((e) => inside(e, 15, 10)),
false,
);
assert.equal(
output.some((e) => inside(e, 25, 10)),
true,
);
});
test("fill closure does not close an open stroke or draw bridges as strokes", () => {
const output = elements('');
assert.equal(output.length, 2);
assert.equal(output[0].strokeColor, "transparent");
assert.equal(output[1].polygon, false);
assert.notDeepEqual([...output[1].points[0]], [...output[1].points.at(-1)]);
const hole = elements(
'',
);
assert.equal(hole.filter((e) => e.strokeColor === "#0000ff").length, 2);
assert.ok(hole.every((e) => e.groupIds[0] === hole[0].groupIds[0]));
});
test("paint precedence and independent alpha do not depend on attribute order", () => {
const variants = [
'fill="red" fill-opacity="0.25" stroke="blue" stroke-opacity="0.5"',
'stroke-opacity="0.5" fill-opacity="0.25" stroke="blue" fill="red"',
];
for (const attrs of variants) {
const [e] = elements(``);
assert.equal(e.backgroundColor, "#ff000040");
assert.equal(e.strokeColor, "#0000ff80");
assert.equal(e.opacity, 100);
}
const [e] = elements(
'',
);
assert.equal(e.backgroundColor, "#008000");
});
test("unsupported SVG colors use the SVG current color without aborting other geometry", () => {
const result = convert(
svg(
'',
),
);
assert.equal(result.hasErrors, false, result.errors);
assert.equal(result.content.length, 2);
assert.equal(result.content[0].backgroundColor, "#2468ac80");
assert.equal(result.content[0].strokeColor, "#2468ac40");
assert.equal(result.content[1].backgroundColor, "#ff0000");
assert.ok(result.warnings.includes("paint"));
});
test("unsupported current colors fall back to black while explicit none and zero alpha stay transparent", () => {
const result = convert(
svg(
'',
),
);
assert.equal(result.hasErrors, false, result.errors);
assert.equal(result.content[0].backgroundColor, "#000000");
assert.equal(result.content[1].backgroundColor, "transparent");
assert.equal(result.content[1].strokeColor, "transparent");
assert.equal(result.content[2].backgroundColor, "transparent");
assert.ok(result.warnings.includes("paint"));
});
test("unsupported paint servers honor an explicit SVG fallback without fetching resources", () => {
const result = convert(
svg(
'',
),
);
assert.equal(result.hasErrors, false, result.errors);
assert.equal(result.content[0].backgroundColor, "#12345680");
assert.equal(result.content[1].backgroundColor, "#008000");
assert.equal(result.content[2].backgroundColor, "transparent");
assert.ok(result.warnings.includes("unsupported"));
});
test("local CSS resolves class, ID, descendant and inline precedence without host CSS", () => {
const [e] = elements(
'',
);
assert.equal(e.backgroundColor, "#800080");
assert.ok(
convert(
svg(
'',
),
).warnings.includes("css"),
);
});
test("CSS specificity does not carry classes into IDs or types into classes", () => {
const repeatedClass = ".c".repeat(11);
for (const rules of [
`#target{fill:red}${repeatedClass}{fill:blue}`,
`${repeatedClass}{fill:blue}#target{fill:red}`,
]) {
const [e] = elements(
``,
);
assert.equal(e.backgroundColor, "#ff0000");
}
const types = `${"g ".repeat(11)}rect`;
const [e] = elements(
`${"".repeat(11)}${"".repeat(11)}`,
);
assert.equal(e.backgroundColor, "#ff0000");
});
test("equal CSS specificity uses source order and universal selectors add no weight", () => {
const [e] = elements(
'',
);
assert.equal(e.backgroundColor, "#0000ff");
const [universal] = elements(
'',
);
assert.equal(universal.backgroundColor, "#008000");
});
test("object/group opacity is not inherited as a presentation value and approximations are explicit", () => {
const result = convert(
svg(
'',
),
);
assert.equal(result.hasErrors, false);
assert.deepEqual(result.content.map((e) => e.opacity).join(","), "50,25");
assert.ok(result.warnings.includes("compositing"));
});
test("use opacity diagnoses compositing for direct and nested referenced children", () => {
for (const [target, opacity] of [["pair", 50], ["nested", 50], ["relay", 25]]) {
const result = convert(svg(
``,
));
assert.equal(result.hasErrors, false);
assert.equal(result.content.length, 2);
assert.ok(result.content.every((e) => e.opacity === opacity));
assert.ok(result.warnings.includes("compositing"), target);
}
});
test("use opacity needs no compositing notice for one emitted piece or opaque reuse", () => {
for (const [body, opacity, count] of [
['', 50, 1],
['', 100, 2],
]) {
const result = convert(svg(body));
assert.equal(result.hasErrors, false);
assert.equal(result.content.length, count);
assert.ok(result.content.every((e) => e.opacity === opacity));
assert.ok(!result.warnings.includes("compositing"));
}
});
test("rendering-tree paint order and all ancestor groups survive native emission", () => {
const output = elements(
'',
);
assert.equal(
output.map((e) => e.backgroundColor).join(","),
"#ff0000,#0000ff,#ffff00,#008000",
);
assert.equal(output[2].groupIds.length, 2);
assert.equal(output[2].groupIds.at(-1), output[1].groupIds.at(-1));
});
test("viewBox origins, CSS lengths and preserveAspectRatio map to the requested viewport", () => {
const [e] = elements(
'',
'viewBox="10 20 20 10" width="2in" height="1in"',
);
assert.equal(e.x, 0);
assert.equal(e.y, 0);
assert.equal(e.width, 192);
assert.equal(e.height, 96);
const [aligned] = elements(
'',
'viewBox="0 0 20 10" width="100" height="100"',
);
assert.equal(aligned.y, 25);
assert.equal(aligned.height, 50);
const [stretched] = elements(
'',
'viewBox="0 0 20 10" width="100" height="100" preserveAspectRatio="none"',
);
assert.equal(stretched.height, 100);
});
test("transforms compose in SVG order and rotate around their explicit center", () => {
const [line] = elements(
'',
'fill="none" stroke="black"',
);
assert.ok(Math.abs(line.x - 15) < 1e-8);
assert.ok(Math.abs(line.y - 17) < 1e-8);
assert.ok(Math.abs(line.points[1][0]) < 1e-8);
assert.ok(Math.abs(line.points[1][1] - 10) < 1e-8);
});
test("rotated, reflected and skewed primitives retain their affine geometry", () => {
const [rect] = elements(
'',
);
assert.ok(Math.abs(rect.angle - Math.PI / 2) < 1e-8);
assert.ok(rect.width > 0 && rect.height > 0);
const [reflected] = elements(
'',
);
assert.ok(reflected.width > 0 && reflected.height > 0);
const [skewed] = elements(
'',
);
assert.equal(skewed.type, "line");
assert.ok(skewed.width > 20);
const [ellipse] = elements(
'',
);
assert.ok(Math.abs(ellipse.angle - Math.PI / 6) < 1e-8);
assert.ok(Math.abs(ellipse.width - 16) < 1e-8);
});
test("local use references retain every child, instance styling and fresh identities", () => {
const output = elements(
'',
);
assert.equal(output.length, 4);
assert.equal(new Set(output.map((e) => e.id)).size, 4);
assert.equal(
output.map((e) => e.backgroundColor).join(","),
"#ff0000,#ff0000,#0000ff,#0000ff",
);
assert.equal(output[2].x - output[0].x, 10);
assert.notEqual(output[0].groupIds.at(-1), output[2].groupIds.at(-1));
});
test("symbol and nested SVG viewports use instance dimensions", () => {
const output = elements(
'',
);
assert.equal(output.length, 2);
assert.equal(output[0].x, 20);
assert.equal(output[0].width, 50);
assert.equal(output[1].x, 90);
assert.equal(output[1].y, 10);
assert.equal(output[1].width, 20);
});
test("curves, relative commands and arcs preserve endpoints with bounded point counts", () => {
const output = elements(
'',
'fill="none" stroke="black"',
);
assert.equal(output.length, 1);
assert.equal(output[0].polygon, false);
assert.ok(output[0].points.length > 8 && output[0].points.length < 300);
assert.ok(Math.abs(output[0].points.at(-1)[0] - 40) < 1e-8);
});
test("round-cap zero-length strokes remain editable dots; zero dimensions remain empty", () => {
const output = elements(
'',
'fill="none" stroke="red" stroke-width="2" stroke-linecap="round"',
);
assert.equal(output.length, 1);
assert.equal(output[0].type, "ellipse");
assert.equal(output[0].width, 2);
assert.equal(output[0].backgroundColor, "#ff0000");
assert.equal(
elements('').length,
0,
);
});
test("hidden containers, definition-only nodes and namespace prefixes are traversed deliberately", () => {
assert.equal(
elements(
'',
).length,
0,
);
assert.equal(
elements(
'',
).length,
1,
);
const result = convert(
'',
);
assert.equal(result.content.length, 1);
});
for (const body of [
'',
'',
'',
'',
'',
'',
'',
'',
])
test(`failed conversion is atomic: ${body.slice(0, 55)}`, () => {
const result = convert(svg(body));
assert.equal(result.hasErrors, true);
assert.equal(result.content.length, 0);
assert.ok(result.errors);
});
test("untrusted scripts/styles/images never execute or fetch and unsupported content is diagnosed", () => {
const result = convert(
svg(
'',
),
);
assert.equal(result.hasErrors, false);
assert.ok(result.warnings.includes("unsupported"));
assert.equal(result.content[0].backgroundColor, "transparent");
assert.equal(
convert(']>').hasErrors,
true,
);
assert.equal(convert("").hasErrors, true);
});
test("document depth and input size are bounded before scene publication", () => {
assert.equal(
convert(
svg(
"".repeat(140) + '' + "".repeat(140),
),
).hasErrors,
true,
);
assert.equal(convert(" ".repeat(2000001)).hasErrors, true);
});
test("repeated imports get new element and group identities", () => {
const input = '';
const a = elements(input),
b = elements(input);
assert.ok(
a.every(
(e) =>
!b.some(
(other) =>
other.id === e.id ||
other.groupIds.some((id) => e.groupIds.includes(id)),
),
),
);
});
test("primitive stroke-before-fill order is preserved without a stroke seam", () => {
const output = elements(
'',
);
assert.equal(output.length, 2);
assert.equal(output[0].strokeColor, "#0000ff");
assert.equal(output[0].backgroundColor, "transparent");
assert.equal(output[1].backgroundColor, "#ff0000");
assert.equal(output[1].strokeColor, "transparent");
});
test("non-scaling strokes remain constant under nonuniform affine transforms", () => {
const result = convert(
svg(
'',
),
);
assert.equal(result.content[0].strokeWidth, 2);
assert.ok(!result.warnings.includes("stroke"));
});
test("bounded fill topology agrees with independent rectangle winding samples", () => {
let random = 291;
const next = () => {
random = (random * 1664525 + 1013904223) >>> 0;
return random;
};
for (let fixture = 0; fixture < 30; fixture++) {
const rectangles = Array.from({ length: 5 }, () => ({
x: next() % 15,
y: next() % 15,
w: 2 + (next() % 8),
h: 2 + (next() % 8),
reverse: next() % 2 === 0,
}));
const path = rectangles
.map((r) =>
r.reverse
? `M${r.x} ${r.y}v${r.h}h${r.w}v${-r.h}Z`
: `M${r.x} ${r.y}h${r.w}v${r.h}h${-r.w}Z`,
)
.join(" ");
for (const rule of ["nonzero", "evenodd"]) {
const output = elements(``);
for (let y = 0.37; y < 24; y += 2.13)
for (let x = 0.23; x < 24; x += 2.17) {
const n = rectangles.reduce(
(sum, r) =>
sum +
(x > r.x && x < r.x + r.w && y > r.y && y < r.y + r.h
? r.reverse
? -1
: 1
: 0),
0,
);
assert.equal(
output.some((e) => inside(e, x, y)),
rule === "evenodd" ? Math.abs(n) % 2 !== 0 : n !== 0,
`${fixture}/${rule}/${x}/${y}`,
);
}
}
}
});
test("outer image clipping is diagnosed without replacing editable output", () => {
const result = convert(
'',
);
assert.equal(result.hasErrors, false);
assert.equal(result.content[0].type, "rectangle");
assert.ok(result.warnings.includes("viewport"));
});
test("use without instance dimensions preserves the referenced SVG's own viewport", () => {
const [e] = elements(
'',
);
assert.equal(e.x, 30);
assert.equal(e.width, 10);
assert.equal(e.height, 20);
});
test("zero-radius and coincident-endpoint arcs follow SVG degeneracy rules", () => {
const [line] = elements(
'',
'fill="none" stroke="black"',
);
assert.equal(line.points.length, 2);
assert.deepEqual([...line.points[1]], [20, 10]);
});
test("paint order accounts for an omitted marker operation", () => {
const output = elements(
'',
);
assert.equal(output[0].backgroundColor, "transparent");
assert.equal(output[1].strokeColor, "transparent");
});
test("unsupported geometric CSS is explicitly diagnosed", () => {
assert.ok(
convert(
svg(''),
).warnings.includes("css"),
);
});
test("dense simple filled outlines retain existing import support within bounded work", () => {
const points = Array.from({ length: 6000 }, (_, i) => {
const a = (i * Math.PI * 2) / 6000;
return `${50 + 40 * Math.cos(a)},${50 + 40 * Math.sin(a)}`;
}).join(" ");
const result = convert(svg(``));
assert.equal(result.hasErrors, false, result.errors);
assert.equal(result.content.length, 1);
});
test("dense compound outlines retain transparent holes without quadratic work",()=>{
const ring=(count,radius,reverse)=>Array.from({length:count},(_,i)=>{const a=i*Math.PI*2/count*(reverse?-1:1);return `${50+radius*Math.cos(a)} ${50+radius*Math.sin(a)}`;});
for(const rule of ["evenodd","nonzero"]){
const a=ring(4000,40,false),b=ring(400,20,rule==="nonzero");
const output=elements(``);
assert.equal(output.length,1);assert.equal(output.some(e=>inside(e,50,50)),false);assert.equal(output.some(e=>inside(e,15,50)),true);
}
});