Files
RuneStone/tests/unit/tables/formulas.spec.ts
T
2026-11-16 04:21:21 -07:00

71 lines
4.0 KiB
TypeScript

import assert from "node:assert/strict";
import test, { afterEach } from "node:test";
import { get } from "svelte/store";
import MarkdownIt from "markdown-it";
import { activeTableFormulas, renderTableFormulas, setTableFormulasEnabled, tableFormulaHtml, tableFormulas, tableFormulasEnabled } from "../../../src/lib/formulas/index.ts";
import { tableCellMarkdown } from "../../../src/lib/editor/tableCellMarkdown.ts";
import { renderMarkdownHtml, renderMarkdownSections } from "../../../src/lib/workspace/noteHtml.ts";
afterEach(() => setTableFormulasEnabled(true));
test("Markdown rendering isolates tables and preserves literal, formatted, and non-table text", () => {
const source = "| X | Y | Result |\r\n| --- | --- | --- |\r\n| 9 | 10 | =AVERAGE(A2:B2) |\r\n\r\n| X | Y | Result |\r\n| --- | --- | --- |\r\n| 1 | 3 | =A2*B2 |\r\n";
const html = renderMarkdownHtml(source, new Map());
assert.match(html, /aria-label="=AVERAGE\(A2:B2\) → 9.5">9.5<\/span>/);
assert.match(html, /aria-label="=A2\*B2 → 3">3<\/span>/);
const sections = renderMarkdownSections(source, new Map());
assert.equal(sections.map(s => source.slice(s.start, s.end)).join(""), source);
assert.equal(sections.map(s => s.html).join(""), html);
const literal = renderMarkdownHtml("=1+2\n\n| A | B | C |\n| --- | --- | --- |\n| `=1+2` | \\=1+2 | ==highlight== |", new Map());
assert.doesNotMatch(literal, /md-table-formula/);
assert.match(literal, /<mark>highlight<\/mark>/);
const hostile = renderMarkdownHtml('| A |\n| --- |\n| ="<img src=x onerror=alert(1)>" |', new Map());
assert.doesNotMatch(hostile, /<img/);
assert.match(hostile, /#ERROR!/);
});
test("editing keeps formulas readable and code or explicit escapes literal", () => {
const doc = (text: string, marks?: { type: string }[]) => ({ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text, marks }] }] });
assert.equal(tableCellMarkdown(doc("=SUM($B$2:C2)*50%")), "=SUM($B$2:C2)*50%");
assert.equal(tableCellMarkdown(doc("\\=1+2")), "\\=1+2");
assert.equal(tableCellMarkdown(doc("=1+2", [{ type: "code" }])), "`=1+2`");
});
test("the setting turns formulas off and back on without a change to the source", () => {
const source = "| A | B |\n| --- | --- |\n| 2 | =A2*3 |\n";
assert.equal(get(tableFormulasEnabled), true, "Formulas are on by default");
assert.equal(activeTableFormulas(), tableFormulas);
const enabled = renderMarkdownHtml(source, new Map());
assert.match(enabled, />6<\/span>/);
setTableFormulasEnabled(false);
assert.equal(activeTableFormulas(), undefined);
for (const html of [renderMarkdownHtml(source, new Map()), ...renderMarkdownSections(source, new Map()).map(section => section.html)]) {
assert.doesNotMatch(html, /md-table-formula/);
assert.match(html, /=A2\*3/);
assert.match(html, /<table>/);
}
setTableFormulasEnabled(true);
assert.equal(renderMarkdownHtml(source, new Map()), enabled);
});
// Ported from the Table Formulas plugin (tests/render.test.ts).
test("rendering separates tables and preserves literal expressions", () => {
const markdown = new MarkdownIt();
const source = "| A | B |\n| --- | --- |\n| 3 | =A2*2 |\n\n| A | B |\n| --- | --- |\n| 4 | =A2+1 |\n\n| Literal |\n| --- |\n| `=1+2` |\n| \\=1+2 |\n";
const tokens = markdown.parse(source, {});
renderTableFormulas(tokens);
const html = markdown.renderer.render(tokens, markdown.options, {});
assert.match(html, /aria-label="=A2\*2 → 6">6<\/span>/);
assert.match(html, /aria-label="=A2\+1 → 5">5<\/span>/);
assert.match(html, /<code>=1\+2<\/code>/);
assert.equal((html.match(/class="md-table-formula"/g) || []).length, 2);
});
test("formula labels escape markup and report errors", () => {
const html = tableFormulaHtml({ formula: '=SUM("<img>")', text: "#ERROR!", error: "#ERROR!" });
assert.doesNotMatch(html, /<img>/);
assert.match(html, /&quot;&lt;img&gt;&quot;/);
assert.match(html, /md-table-formula is-error/);
assert.match(html, /Invalid formula syntax or function arguments/);
});