Files
RuneStone/scripts/tableFormulas.spec.ts
T

39 lines
2.3 KiB
TypeScript

import assert from "node:assert/strict";
import test, { before, after } from "node:test";
import { ExternalSession } from "../src/lib/plugins/externalSession.ts";
// @ts-ignore The compiled package is generated by pnpm build:plugins.
import { activate } from "../plugin-packages/table-formulas/main.js";
const session = new ExternalSession("runestone.table-formulas", {
workspace: { list: async () => [], read: async () => "", write: async () => {} },
settings: { read: async () => null, write: async () => {} },
assets: { read: async () => new Uint8Array() },
}, () => {}, () => {});
before(() => session.start({ activate }));
after(() => session.stop());
import { tableCellMarkdown } from "../src/lib/editor/tableCellMarkdown.ts";
import { renderMarkdownHtml, renderMarkdownSections } from "../src/lib/workspace/noteHtml.ts";
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`");
});