import assert from "node:assert/strict";
import test from "node:test";
import { loadConverter } from "./svgConverterHarness.mjs";
const convert = loadConverter();
const options = {
measureText: (text, fontSize, _fontFamily, lineHeight) => ({
width:
(Math.max(...text.split("\n").map((line) => Array.from(line).length)) *
fontSize) /
2,
height: text.split("\n").length * fontSize * lineHeight,
}),
};
const svg = (body) =>
``;
const texts = (result) => {
assert.equal(result.hasErrors, false, result.errors);
return result.content.filter((e) => e.type === "text");
};
test("SVG text remains editable text with inherited color, alpha, anchor and baseline", () => {
const result = convert(
svg(
'Hello',
),
options,
);
const [text] = texts(result);
assert.equal(text.text, "Hello");
assert.match(text.id, /^[a-zA-Z0-9]{8}$/);
assert.equal(text.rawText, "Hello");
assert.equal(text.originalText, "Hello");
assert.equal(text.fontSize, 20);
assert.equal(text.strokeColor, "#0000ff");
assert.equal(text.opacity, 50);
assert.equal(text.textAlign, "center");
assert.equal(text.x, 75);
assert.equal(text.y, 47.5);
assert.equal(text.width, 50);
assert.equal(text.height, 25);
assert.equal(text.containerId, null);
assert.ok(text.groupIds.length);
});
test("positioned tspan lines retain text, relative font units and baseline offsets", () => {
const result = convert(
svg(
'FirstSecond',
),
options,
);
const imported = texts(result);
assert.deepEqual(
Array.from(imported, (e) => e.text),
["First", "Second"],
);
assert.equal(imported[1].y - imported[0].y, 30);
assert.equal(imported[0].x, 20);
assert.equal(imported[1].x, 20);
});
test("uniform SVG transforms scale and rotate text with its group", () => {
const [text] = texts(
convert(
svg(
'Hi',
),
options,
),
);
assert.equal(text.fontSize, 32);
assert.equal(text.width, 32);
assert.equal(text.height, 40);
assert.ok(Math.abs(text.angle - Math.PI / 2) < 1e-9);
assert.ok(Math.abs(text.x - 64) < 1e-9);
assert.ok(Math.abs(text.y - 96) < 1e-9);
});
test("Mermaid foreignObject labels import as native text rather than empty SVG placeholders", () => {
const result = convert(
svg(
'Plan & build',
),
options,
);
const [text] = texts(result);
assert.equal(texts(result).length, 1);
assert.equal(text.text, "Plan & build");
assert.equal(text.strokeColor, "#ffffff");
assert.equal(text.textAlign, "center");
assert.equal(text.x + text.width / 2, 150);
assert.equal(text.y, 108);
assert.equal(text.height, 24);
assert.equal(text.lineHeight, 1.5);
assert.ok(text.groupIds.length >= 2);
});
test("plain HTML label line breaks survive without executing or importing active content", () => {
const result = convert(
svg(
'FirstSecondthrow Error("executed")hidden payload',
),
options,
);
const [text] = texts(result);
assert.equal(text.text, "First\nSecond");
assert.ok(result.warnings.includes("unsupported"));
});
test("empty or hidden SVG/HTML text does not manufacture visible labels", () => {
const result = convert(
svg(
'HiddenHidden HTML Visible ',
),
options,
);
assert.deepEqual(
Array.from(texts(result), (e) => e.text),
[" Visible "],
);
});
test("excessive text fails atomically instead of publishing a partial drawing", () => {
const result = convert(
svg(
'' + "x".repeat(100001) + "",
),
options,
);
assert.equal(result.hasErrors, true);
assert.equal(result.content.length, 0);
});
test("text visibility can be restored by a child without exposing hidden siblings", () => {
const result = convert(
svg(
'hiddenShownhiddenHTML shown',
),
options,
);
assert.deepEqual(
Array.from(texts(result), (e) => e.text),
["Shown", "HTML shown"],
);
});
test("unsupported font sizes do not abort ordinary geometry and zero-size text stays hidden", () => {
const result = convert(
svg(
'FallbackZero',
),
options,
);
assert.equal(result.hasErrors, false, result.errors);
assert.equal(result.content.length, 2);
assert.equal(texts(result)[0].text, "Fallback");
assert.ok(result.warnings.includes("text"));
});