Files
RuneStone/tests/unit/editor/inlineMath.spec.ts
T
Knotty Wyvern e433bed28e Move the tests from scripts/ to tests/ by feature
Unit tests go to tests/unit/<area>/, browser smoke tests to
tests/smoke/<area>/, and tests against a running desktop app to
tests/native/. Files named after dates, work passes, or Kanban cards get
feature names, for example editorOctober12 -> editor/footnotes.

scripts/ keeps the build, release, and package tools. Plugin repositories
import scripts/packagePlugin.mjs, and the Terminal smoke test imports
scripts/pluginMocks.mjs, which now re-exports the harness copy.

The unit tests get their own type check (tests/tsconfig.json) with Node
types. pnpm check runs it after the app check. Add @types/node and fix
the three test mocks that it reported.
2026-10-27 19:47:39 -07:00

57 lines
2.6 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { markdown, markdownLanguage } from "@codemirror/lang-markdown";
import { noteFormats } from "../../../src/lib/editor/noteFormats.ts";
import { renderMarkdownHtml } from "../../../src/lib/workspace/noteHtml.ts";
const parser = markdown({ base: markdownLanguage, extensions: [noteFormats] }).language.parser;
const render = (source: string) => renderMarkdownHtml(source, new Map());
function mathNodes(source: string) {
const nodes: string[] = [];
parser.parse(source).iterate({ enter(node) {
if (node.name === "NoteMath") nodes.push(source.slice(node.from, node.to));
} });
return nodes;
}
test("dollar amounts stay literal in writing, reading, and table cells", () => {
for (const source of [
"$10", "$10 and $20", "$1,234.56 plus $78.90", "$10-$20", "$0.25-$.50",
"US$10 / CA$20", "$ 10 and $ 20", "$-10 and $-20", "($10) and ($20)",
"$10\n$20", "$10\r\n$20", "$10 **income**, $20 **cost**",
]) {
assert.deepEqual(mathNodes(source), [], source);
const html = render(source);
assert.doesNotMatch(html, /md-inline-math/, source);
assert.equal((html.match(/\$/g) || []).length, (source.match(/\$/g) || []).length, source);
assert.doesNotMatch(render(`| Amounts |\n| --- |\n| ${source.replace(/\r?\n/g, "<br>")} |`), /md-inline-math/, source);
}
});
test("inline math still works next to dollar amounts", () => {
for (const expression of ["$x^2$", "$2 + 2$", "$x + y$", String.raw`$\frac{1}{2}$`, String.raw`$x + \$5$`]) {
const source = `$10 and $20, then ${expression}, then $30 and $40.`;
assert.deepEqual(mathNodes(source), [expression], source);
assert.equal((render(source).match(/md-inline-math/g) || []).length, 1, source);
assert.match(render(source), /\$10 and \$20/);
assert.match(render(source), /\$30 and \$40/);
}
});
test("escaped, incomplete, spaced, multiline, and double delimiters do not create inline math", () => {
for (const source of [String.raw`\$x\$`, "$x", "$ x$", "$x $", "$x\ny$", "$$x$$", "$$\nx\n$$", "`$x$`", "```\n$x$\n```", "$x$2"]) {
assert.deepEqual(mathNodes(source), [], source);
assert.doesNotMatch(render(source), /md-inline-math/, source);
}
});
test("highlight and comment formats keep their existing behavior", () => {
const source = "==**highlight**== %%comment%%";
const tree = parser.parse(source).toString();
assert.match(tree, /NoteHighlight/);
assert.match(tree, /StrongEmphasis/);
assert.match(tree, /NoteComment/);
assert.match(render(source), /<mark><strong>highlight<\/strong><\/mark>/);
assert.match(render(source), /md-inline-comment/);
});