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.
This commit is contained in:
Knotty Wyvern
2026-10-27 19:47:39 -07:00
parent 90d1940c73
commit e433bed28e
154 changed files with 269 additions and 227 deletions
+42
View File
@@ -0,0 +1,42 @@
import assert from "node:assert/strict";
import test, { before, after } from "node:test";
import { ExternalSession } from "../../../src/lib/plugins/externalSession.ts";
import { existsSync } from "node:fs";
// The Table Formulas package comes from a sibling repository. Run `pnpm build:plugins` to build it.
const tableFormulasEntry = new URL("../../../plugin-packages/table-formulas/main.js", import.meta.url);
const tableFormulas = existsSync(tableFormulasEntry) ? await import(tableFormulasEntry.href) : undefined;
const activate = tableFormulas?.activate;
const needsTableFormulas = { skip: tableFormulas ? false : "the Table Formulas package is not built; run pnpm build:plugins" };
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(() => activate && session.start({ activate }));
after(() => activate && 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", needsTableFormulas, () => {
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", needsTableFormulas, () => {
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`");
});
+120
View File
@@ -0,0 +1,120 @@
import assert from "node:assert/strict";
import test from "node:test";
import { appendTableColumn, appendTableRow, parseMarkdownTable, replaceTableCell } from "../../../src/lib/editor/markdownTable.ts";
import { tableCellMarkdown, extractedCellMarkdown } from "../../../src/lib/editor/tableCellMarkdown.ts";
import { renderMarkdownHtml, renderMarkdownSections } from "../../../src/lib/workspace/noteHtml.ts";
const table = "| Ticker | Notes |\n| :--- | ---: |\n| IBKR.US | First<br>Second |";
test("prose directly after a table stays outside the table and its edit range", () => {
for (const eol of ["\n", "\r\n"]) {
const body = table.replaceAll("\n", eol);
const tail = `${eol}Plain text${eol}More text with an escaped \\| pipe${eol}# Heading`;
const source = body + tail;
assert.equal(parseMarkdownTable(source)?.end, body.length);
assert.equal(parseMarkdownTable(source)?.rows.length, 3);
assert.equal(appendTableRow(source), body + `${eol}| | |` + tail);
assert.equal(appendTableColumn(source), appendTableColumn(body) + tail);
assert.equal(replaceTableCell(source, 3, 0, "changed"), source);
const html = renderMarkdownHtml(source, new Map());
assert.equal((html.match(/<tr>/g) || []).length, 2);
assert.match(html, /<\/table>[\s\S]*<p>Plain text/);
const sections = renderMarkdownSections(source, new Map());
assert.equal(sections[0].end, body.length + eol.length);
assert.equal(source.slice(sections[1].start).startsWith("Plain text"), true);
}
});
test("table boundaries distinguish escaped pipes from delimiters and work in nested blocks", () => {
const body = "| A | B |\n| --- | --- |\nOne | Two\n| short |\n| escaped \\| pipe | value |\n| even \\\\| delimiter |";
assert.equal(parseMarkdownTable(body + "\nProse \\| literal")?.rows.length, 6);
assert.equal((renderMarkdownHtml(body + "\nProse \\| literal", new Map()).match(/<tr>/g) || []).length, 5);
assert.match(renderMarkdownHtml("> | A |\n> | --- |\n> | B |\n> Paragraph", new Map()), /<\/table>[\s\S]*<p>Paragraph<\/p>/);
assert.match(renderMarkdownHtml("- Item\n\n | A |\n | --- |\n | B |\n Paragraph", new Map()), /<\/table>[\s\S]*<p>Paragraph<\/p>/);
});
test("extracted cell text keeps formatting and literal code while converting actual line breaks", () => {
assert.equal(extractedCellMarkdown([
{ type: "text", text: "Title", marks: [{ type: "bold" }] },
{ type: "hardBreak" },
{ type: "text", text: "<br>", marks: [{ type: "code" }] },
]), "**Title**\n`<br>`");
});
test("table column insertion retains alignment and existing cell contents", () => {
assert.equal(appendTableColumn(table), "| Ticker | Notes | |\n| :--- | ---: | --- |\n| IBKR.US | First<br>Second | |");
});
test("table row insertion keeps existing source byte-for-byte", () => {
assert.equal(appendTableRow(table + "\n\n"), table + "\n| | |\n\n");
});
test("table edits preserve CRLF and optional outer pipes", () => {
const source = "Name | Link\r\n--- | :---:\r\nOne | [note](note.md)\r\n";
assert.equal(appendTableRow(source), "Name | Link\r\n--- | :---:\r\nOne | [note](note.md)\r\n| | |\r\n");
assert.equal(appendTableColumn(source), "| Name | Link | |\r\n| --- | :---: | --- |\r\n| One | [note](note.md) | |\r\n");
});
test("escaped pipes in text, wiki aliases, and inline code stay inside cells", () => {
const source = "| One | Two |\n| --- | --- |\n| [[note\\|Alias]] | `a\\|b` and text\\|value |";
const parsed = parseMarkdownTable(source)!;
assert.equal(parsed.rows[2].cells.length, 2);
const changed = appendTableColumn(source);
assert.ok(changed.includes("| [[note\\|Alias]] | `a\\|b` and text\\|value | |"));
for (const row of parsed.rows) for (const cell of row.cells) assert.equal(source.slice(cell.from, cell.to), cell.text);
});
test("adding columns pads short rows without losing extra source cells", () => {
const source = "| A | B |\n| --- | --- |\n| short |\n| a | b | extra |";
assert.equal(appendTableColumn(source), "| A | B | |\n| --- | --- | --- |\n| short | | |\n| a | b | | extra |");
});
test("table controls handle header-only and single-column tables", () => {
assert.equal(appendTableRow("| Header |\n| --- |"), "| Header |\n| --- |\n| |");
assert.equal(parseMarkdownTable(appendTableColumn("| Header |\n| --- |"))?.columns, 2);
});
test("invalid tables are unchanged", () => {
for (const source of ["Just text", "| A | B |\n| --- |", "| A |\n| not a delimiter |"] ) {
assert.equal(parseMarkdownTable(source), null);
assert.equal(appendTableColumn(source), source);
assert.equal(appendTableRow(source), source);
}
});
test("editing one cell preserves padding, alignment, other cells and CRLF", () => {
const source = " | Name | Notes |\r\n | :--- | ---: |\r\n | One | first<br />~~old~~ |\r\n\r\nTail";
assert.equal(replaceTableCell(source, 2, 1, "next<br>line\\|pipe"), source.replace("first<br />~~old~~", "next<br>line\\|pipe"));
assert.equal(replaceTableCell(source, 0, 0, "Title"), source.replace("Name", "Title"));
assert.equal(replaceTableCell(source, 1, 0, "bad"), source);
assert.equal(replaceTableCell(source, 2, 1, "break\nrow"), source);
});
test("editing an empty or omitted cell does not overwrite its neighbor", () => {
assert.equal(replaceTableCell("| A | B |\n| --- | --- |\n| short |", 2, 1, "new"), "| A | B |\n| --- | --- |\n| short | new |");
const source = "| A | B |\n| --- | --- |\n| | value |";
assert.equal(replaceTableCell(source, 2, 0, "new"), source.replace("| | value", "| new| value"));
});
test("rich cell serialization retains breaks, formatting, links and literal punctuation", () => {
const text = tableCellMarkdown({ type: "doc", content: [{ type: "paragraph", content: [
{ type: "text", text: "old", marks: [{ type: "strike" }] },
{ type: "hardBreak" },
{ type: "text", text: "bold", marks: [{ type: "bold" }] },
{ type: "text", text: " | *literal* <br> & value" },
{ type: "hardBreak" },
{ type: "text", text: "a|`b", marks: [{ type: "code" }] },
{ type: "text", text: "note", marks: [{ type: "link", attrs: { href: "note.md", "data-kind": "wiki" } }] },
{ type: "text", text: "web", marks: [{ type: "link", attrs: { href: "https://example.com/a(b)" } }] },
] }, { type: "paragraph", content: [{ type: "text", text: "Pasted paragraph" }] }] });
const source = `| A |\n| --- |\n| ${text} |`;
assert.equal(parseMarkdownTable(source)?.rows[2].cells.length, 1);
const html = renderMarkdownHtml(source, new Map());
assert.ok(html.includes("<s>old</s>"));
assert.ok(html.includes("<strong>bold</strong>"));
assert.ok(html.includes("| *literal* &lt;br&gt; &amp; value"));
assert.ok(html.includes("<code>a|`b</code>"));
assert.ok(html.includes('href="note.md" data-kind="wiki"'));
assert.ok(html.includes('href="https://example.com/a(b)"'));
assert.equal((html.match(/<br>/g) || []).length, 3);
});
+83
View File
@@ -0,0 +1,83 @@
import assert from "node:assert/strict";
import test from "node:test";
import { alignTableCells, clipboardCells, copyTableCells, copyTableMarkdown, deleteTableCells, formatTableCells, mapTableCells, pasteTableCells } from "../../../src/lib/editor/tableSelection.ts";
import { htmlToMarkdown } from "../../../src/lib/workspace/htmlToMarkdown.ts";
import { parseMarkdownTable } from "../../../src/lib/editor/markdownTable.ts";
import { renderMarkdownHtml, plainInlineMarkdown } from "../../../src/lib/workspace/noteHtml.ts";
import { tableCellMarkdown } from "../../../src/lib/editor/tableCellMarkdown.ts";
const source = "| A | B | C |\r\n| --- | :---: | ---: |\r\n| one | two | three |\r\n| four | five | six |\r\n\r\nTail";
const range = { anchor: { row: 2, column: 1 }, head: { row: 1, column: 0 } };
test("selected cells retain table structure and line breaks through HTML clipboard", () => {
const source = "| Name | Notes |\n| --- | --- |\n| **Alpha** | First<br>Second |";
const range = { anchor: { row: 0, column: 0 }, head: { row: 1, column: 1 } };
const copied = copyTableMarkdown(source, range);
const pasted = htmlToMarkdown(renderMarkdownHtml(copied, new Map()));
const table = parseMarkdownTable(pasted)!;
assert.equal(table.columns, 2);
assert.equal(table.rows.length, 3);
assert.equal(table.rows[2].cells[0].text, "**Alpha**");
assert.equal(table.rows[2].cells[1].text, "First<br>Second");
});
test("reversed rectangular selections only change selected cells and preserve CRLF", () => {
const changed = formatTableCells(source, range, "bold");
assert.equal(changed, source.replace("one", "**one**").replace("two", "**two**").replace("four", "**four**").replace("five", "**five**"));
assert.equal(formatTableCells(changed, range, "bold"), source);
assert.equal(formatTableCells(source.replace("one", "**one**"), range, "bold"), changed);
assert.equal(copyTableCells(source, range), "one\ttwo\nfour\tfive");
assert.equal(alignTableCells(source, range, "left"), source.replace("| --- | :---: | ---: |", "| :--- | :--- | ---: |"));
});
test("all formats render and clear without interpreting escaped punctuation", () => {
const cell = { anchor: { row: 1, column: 0 }, head: { row: 1, column: 0 } };
for (const [format, html] of [["bold", "<strong>one</strong>"], ["italic", "<em>one</em>"], ["strike", "<s>one</s>"], ["highlight", "<mark>one</mark>"], ["code", "<code>one</code>"], ["math", 'class="md-inline-math">one</span>'], ["comment", 'class="md-inline-comment">one</span>']] as const) {
const changed = formatTableCells(source, cell, format);
assert.ok(renderMarkdownHtml(changed, new Map()).includes(html), format);
assert.equal(formatTableCells(changed, cell, "clear"), source);
}
assert.equal(plainInlineMarkdown("**bold** ==*highlight*== ~~strike~~ `a|b`<br>\\*literal\\*"), "bold highlight strike a\\|b<br>\\*literal\\*");
assert.equal(plainInlineMarkdown("**Photo** ![alt](image.png)"), "Photo ![alt](<image.png>)");
assert.ok(renderMarkdownHtml("`==not highlighted==` \\==literal\\==", new Map()).includes("<code>==not highlighted==</code>"));
for (const type of ["highlight", "math", "comment"]) {
const text = tableCellMarkdown({ type: "text", text: "a|b", marks: [{ type }] });
assert.equal(parseMarkdownTable(`| H |\n| --- |\n| ${text} |`)?.rows[2].cells.length, 1);
}
});
test("clipboard accepts Markdown and quoted TSV without splitting escaped pipes", () => {
assert.deepEqual(clipboardCells('"a\tb"\t"line 1\nline 2"\r\n"say ""hi"""\tx|y\r\n'), [["a\tb", "line 1<br>line 2"], ['say "hi"', "x\\|y"]]);
assert.deepEqual(clipboardCells("| A | B |\n| --- | --- |\n| a\\|b | `code` |"), [["A", "B"], ["a\\|b", "`code`"]]);
const pasted = pasteTableCells(source, range, "value");
assert.equal(copyTableCells(pasted, range), "value\tvalue\nvalue\tvalue");
const large = pasteTableCells(source, { anchor: { row: 2, column: 2 }, head: { row: 2, column: 2 } }, "a\tb\nc\td");
assert.equal(parseMarkdownTable(large)?.columns, 4);
assert.equal(parseMarkdownTable(large)?.rows.length, 5);
assert.ok(large.endsWith("\r\n\r\nTail"));
assert.equal(copyTableCells(large, { anchor: { row: 2, column: 2 }, head: { row: 3, column: 3 } }), "a\tb\nc\td");
});
test("clear preserves structure; delete removes whole rows/columns or shifts partial cells", () => {
assert.equal(copyTableCells(mapTableCells(source, range, () => ""), range), "\t\n\t");
assert.equal(copyTableCells(deleteTableCells(source, range), range), "three\t\nsix\t");
const rows = deleteTableCells(source, { anchor: { row: 1, column: 0 }, head: { row: 1, column: 2 } });
assert.equal(parseMarkdownTable(rows)?.rows.length, 3);
assert.ok(rows.includes("four")); assert.ok(!rows.includes("one"));
const columns = deleteTableCells(source, { anchor: { row: 0, column: 1 }, head: { row: 2, column: 1 } });
assert.equal(parseMarkdownTable(columns)?.columns, 2);
assert.ok(columns.includes("| one | three |"));
const header = deleteTableCells(source, { anchor: { row: 0, column: 0 }, head: { row: 0, column: 2 } });
assert.ok(header.startsWith("| one | two | three |\r\n| --- | :---: | ---: |"));
assert.equal(deleteTableCells(source, { anchor: { row: 0, column: 0 }, head: { row: 2, column: 2 } }), "\r\n\r\nTail");
});
test("deleting the empty columns shown on the card removes their structure", () => {
const emptyColumns = "| Column 2 | Column 1 | | | | |\n| --- | --- | --- | --- | --- | --- |\n| 2cm | Value | | | | |\n| text | 123 | | | | |\n| | | | | | |\n| | | | | | |\n\nAfter.";
const result = deleteTableCells(emptyColumns, { anchor: { row: 4, column: 5 }, head: { row: 0, column: 3 } });
assert.equal(parseMarkdownTable(result)?.columns, 3);
assert.equal(parseMarkdownTable(result)?.rows.length, 6);
assert.ok(result.includes("| 2cm | Value | |"));
assert.ok(result.endsWith("\n\nAfter."));
});
test("deleting the last body rows retains a valid header and alignment", () => {
const result = deleteTableCells(source, { anchor: { row: 2, column: 2 }, head: { row: 1, column: 0 } });
assert.equal(result, "| A | B | C |\r\n| --- | :---: | ---: |\r\n\r\nTail");
assert.equal(parseMarkdownTable(result)?.rows.length, 2);
});
+69
View File
@@ -0,0 +1,69 @@
import assert from "node:assert/strict";
import test from "node:test";
import { changeTableStructure, moveTableAxis, sortTableColumn } from "../../../src/lib/editor/tableStructure.ts";
import { parseMarkdownTable } from "../../../src/lib/editor/markdownTable.ts";
const source = "| Name | Value |\r\n| :--- | ---: |\r\n| Zebra | `2` |\r\n| **Apple** | 10 |\r\n| apple | 3 |\r\n| | 1 |\r\n\r\nTail";
const values = (text: string) => parseMarkdownTable(text)!.rows.map(row => row.cells.map(cell => cell.text));
test("dragging rows across multiple positions preserves raw Markdown, header, CRLF and suffix", () => {
const moved = moveTableAxis(source, "row", 1, 4);
assert.deepEqual(values(moved).slice(2).map(row => row[0]), ["**Apple**", "apple", "", "Zebra"]);
assert.ok(moved.includes("| Zebra | `2` |\r\n\r\nTail"));
assert.equal(moveTableAxis(moved, "row", 4, 1), source);
for (const [from, to] of [[0, 2], [2, 0], [1, 5], [-1, 2], [1, 1], [1.5, 2], [1, NaN]]) {
assert.equal(moveTableAxis(source, "row", from, to), source);
}
});
test("dragging columns carries alignment and rich contents across ragged rows", () => {
const text = " | A | B | C |\r\n | :--- | :---: | ---: |\r\n | a\\|b | **bold**<br>line | [[note]] | extra |\r\n | short |\r\n\r\nTail";
const moved = moveTableAxis(text, "column", 0, 2);
assert.deepEqual(values(moved), [["B", "C", "A"], [":---:", "---:", ":---"], ["**bold**<br>line", "[[note]]", "a\\|b", "extra"], ["", "", "short"]]);
assert.ok(moved.endsWith("\r\n\r\nTail"));
assert.deepEqual(values(moveTableAxis(moved, "column", 2, 0)).slice(0, 3), values(text).slice(0, 3));
assert.equal(moveTableAxis(text, "column", 0, 3), text);
assert.equal(moveTableAxis("| H |\n| --- |", "column", 0, 0), "| H |\n| --- |");
assert.equal(moveTableAxis("invalid", "row", 1, 2), "invalid");
});
test("row insertion, movement and duplication preserve header, raw cells and CRLF", () => {
const inserted = changeTableStructure(source, "row", 1, "before");
assert.equal(inserted, source.replace("| Zebra", "| | |\r\n| Zebra"));
assert.equal(changeTableStructure(source, "row", 0, "after"), inserted);
assert.equal(changeTableStructure(source, "row", 0, "before"), source);
const moved = changeTableStructure(source, "row", 2, "previous");
assert.equal(moved, source.replace("| Zebra | `2` |\r\n| **Apple** | 10 |", "| **Apple** | 10 |\r\n| Zebra | `2` |"));
assert.equal(changeTableStructure(moved, "row", 1, "next"), source);
assert.equal(changeTableStructure(source, "row", 1, "previous"), source);
assert.equal(changeTableStructure(source, "row", 0, "next"), source);
assert.equal(changeTableStructure(source, "row", 4, "next"), source);
assert.equal(changeTableStructure(source, "row", 1, "duplicate"), source.replace("| Zebra | `2` |", "| Zebra | `2` |\r\n| Zebra | `2` |"));
assert.ok(changeTableStructure(source, "row", 1, "delete").endsWith("\r\n\r\nTail"));
});
test("column movement and duplication carry header, contents and alignment together", () => {
const moved = changeTableStructure(source, "column", 1, "previous");
assert.deepEqual(values(moved), values(source).map(row => row.toReversed()));
assert.deepEqual(values(changeTableStructure(moved, "column", 0, "next")), values(source));
assert.deepEqual(values(changeTableStructure(source, "column", 0, "duplicate")), values(source).map(row => [row[0], ...row]));
assert.deepEqual(values(changeTableStructure(source, "column", 1, "before")), values(source).map((row, i) => [row[0], i === 1 ? "---" : "", row[1]]));
assert.deepEqual(values(changeTableStructure(source, "column", 1, "delete")), values(source).map(row => [row[0]]));
assert.equal(changeTableStructure(source, "column", 0, "previous"), source);
assert.equal(changeTableStructure(source, "column", 1, "next"), source);
});
test("sort uses visible text and natural numbers, is stable, leaves blanks last and header intact", () => {
assert.deepEqual(values(sortTableColumn(source, 0)).slice(2).map(row => row[0]), ["**Apple**", "apple", "Zebra", ""]);
assert.deepEqual(values(sortTableColumn(source, 0, true)).slice(2).map(row => row[0]), ["Zebra", "**Apple**", "apple", ""]);
assert.deepEqual(values(sortTableColumn(source, 1)).slice(2).map(row => row[1]), ["1", "`2`", "3", "10"]);
const sorted = sortTableColumn(source, 1, true);
assert.ok(sorted.startsWith("| Name | Value |\r\n| :--- | ---: |"));
assert.ok(sorted.includes("| Zebra | `2` |"));
assert.ok(sorted.endsWith("\r\n\r\nTail"));
});
test("header-only, ragged and single-column tables remain valid through operations", () => {
const header = "| H |\n| --- |";
assert.equal(changeTableStructure(header, "row", 0, "duplicate"), header + "\n| H |");
assert.equal(changeTableStructure(header, "column", 0, "delete"), "");
assert.equal(changeTableStructure(header, "row", 0, "delete"), "");
assert.equal(sortTableColumn(header, 0), header);
const ragged = "| A | B |\n| --- | --- |\n| short |\n| a\\|b | two | extra |";
assert.deepEqual(values(changeTableStructure(ragged, "column", 1, "previous"))[3], ["two", "a\\|b", "extra"]);
assert.equal(changeTableStructure("invalid", "row", 0, "after"), "invalid");
});