'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { parse, parseInline, safeHref, Growing, MAX_CHARS } = require('../src/markdown');
const { chatHtml } = require('../src/webview');
const page = require('../media/chat.js');
// A document that can only do what the chat page is allowed to do: make elements and text.
// It has no way to parse markup, so a test that passes here cannot have used one.
class Node {
constructor(tag, text) {
this.tag = tag;
this.text = text;
this.children = [];
this.attrs = {};
this.className = '';
this.listeners = {};
}
appendChild(c) {
this.children.push(c);
return c;
}
setAttribute(k, v) {
this.attrs[k] = String(v);
}
addEventListener(name, fn) {
this.listeners[name] = fn;
}
set textContent(v) {
this.children = [new Node('#text', String(v))];
}
get textContent() {
return this.tag === '#text' ? this.text : this.children.map((c) => c.textContent).join('');
}
all(out) {
const list = out || [];
for (const c of this.children) {
list.push(c);
c.all(list);
}
return list;
}
/** A picture of the tree, for comparing: text in quotes, elements as tag(children). */
show() {
if (this.tag === '#text') return JSON.stringify(this.text);
return `${this.tag}(${this.children.map((c) => c.show()).join(' ')})`;
}
}
const doc = { createElement: (tag) => new Node(tag), createTextNode: (text) => new Node('#text', text) };
function render(src) {
const root = new Node('div');
page.buildBlocks(doc, parse(src), root, 0);
return root;
}
const ALLOWED = new Set(['#text', 'div', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre', 'code', 'hr', 'blockquote', 'ul', 'ol', 'li', 'strong', 'em', 'del', 'span', 'br', 'table', 'thead', 'tbody', 'tr', 'th', 'td']);
test('paragraphs, emphasis, code and line breaks', () => {
assert.equal(render('Hello **bold** and *it* and `x < y`.\nnext').show(), 'div(p("Hello " strong("bold") " and " em("it") " and " code("x < y") "." br() "next"))');
assert.equal(render('one\n\ntwo').show(), 'div(p("one") p("two"))');
assert.equal(render('snake_case_name and 2 * 3 * 4').show(), 'div(p("snake_case_name and 2 * 3 * 4"))');
assert.equal(render('__bold__ _it_ **a *b* c**').show(), 'div(p(strong("bold") " " em("it") " " strong("a " em("b") " c")))');
assert.equal(render('\\*not\\* \\`code\\`').show(), 'div(p("*not* `code`"))');
});
test('headings, rules, quotes and lists', () => {
assert.equal(render('# One\n## Two ##\n####### seven').show(), 'div(h1("One") h2("Two") p("####### seven"))');
assert.equal(render('a\n\n---\n\nb').show(), 'div(p("a") hr() p("b"))');
assert.equal(render('> quoted **text**\n> more\n\nafter').show(), 'div(blockquote(p("quoted " strong("text") br() "more")) p("after"))');
const list = render('- one\n- two\n - inner\n still inner\n- three\n\n1. first\n2) second');
assert.equal(list.show(), 'div(ul(li("one") li("two") li("inner" br() "still inner") li("three")) ol(li("first") li("second")))');
assert.equal(list.children[0].children[2].className, 'depth-1');
});
test('a fenced block is text as written, and one still being written is a block already', () => {
assert.equal(render('```python\nprint("hi")\n**not bold**\n```\nafter').show(), 'div(pre(code("print(\\"hi\\")\\n**not bold**")) p("after"))');
assert.equal(render('text\n```js\nconst a = 1;').show(), 'div(p("text") pre(code("const a = 1;")))');
assert.equal(parse('```js title=""\n1\n```')[0].lang, 'js');
assert.equal(render('~~~\na\n~~~').show(), 'div(pre(code("a")))');
});
test('a GFM table is rendered as a table inside a scrollable wrapper, with alignment and inline formatting', () => {
assert.equal(render('| a | b |\n|---|---|\n| 1 | 2 |').show(), 'div(div(table(thead(tr(th("a") th("b"))) tbody(tr(td("1") td("2"))))))');
const aligned = render('| Left | Mid | Right |\n|:---|:---:|---:|\n| **x** | `y` | ~~z~~ |');
const ths = aligned.all().filter((n) => n.tag === 'th');
assert.deepEqual(ths.map((n) => n.className), ['align-left', 'align-center', 'align-right']);
assert.ok(aligned.all().some((n) => n.tag === 'strong' && n.textContent === 'x'));
assert.ok(aligned.all().some((n) => n.tag === 'del' && n.textContent === 'z'));
// Pipe lines without a separator row fall back to a preformatted block.
assert.equal(render('| just | pipes |').show(), 'div(pre(code("| just | pipes |")))');
});
test('task list checkboxes and strikethrough inline markup', () => {
const out = render('- [x] done\n- [ ] todo\n\n~~old~~ text');
const lis = out.all().filter((n) => n.tag === 'li');
assert.equal(lis.length, 2);
assert.ok(lis[0].className.includes('checked'));
assert.ok(lis[0].textContent.includes('done'));
assert.ok(!lis[1].className.includes('checked'));
assert.ok(out.all().some((n) => n.tag === 'del' && n.textContent === 'old'));
});
test('fenced code block with post handler renders copy and insert toolbar buttons', () => {
const posted = [];
const root = new Node('div');
page.buildBlocks(doc, parse('```python\nprint(1)\n```'), root, 0, (m) => posted.push(m));
const buttons = root.all().filter((n) => n.tag === 'button');
assert.deepEqual(buttons.map((b) => b.textContent), ['Copy', 'Insert']);
buttons[0].listeners.click();
buttons[1].listeners.click();
assert.deepEqual(posted, [
{ type: 'copyCode', text: 'print(1)' },
{ type: 'insertCode', text: 'print(1)' },
]);
});
test('markup a model writes is text, never elements', () => {
const hostile = [
'',
'
',
'Click here',
'****',
'# ',
'-