import test from 'node:test'; import assert from 'node:assert/strict'; import { parseMarkdown, toPlainText, emojiForShortcode } from '../app/renderer/js/markdown/parse.js'; import { maliciousMessages, richMessage } from './fixtures/chat-markup.mjs'; function types(value, found = new Set()) { if (!value || typeof value !== 'object') return found; if (value.type) found.add(value.type); for (const child of Object.values(value)) if (child && typeof child === 'object') types(child, found); return found; } test('official dialect produces rich nodes rather than escaped advertised features', () => { const tree = parseMarkdown(richMessage); const found = types(tree); for (const type of ['heading', 'strong', 'em', 'u', 'mark', 'spoiler', 'emoji', 'blockquote', 'details', 'align', 'table', 'list', 'fnref', 'math', 'mermaid']) assert.ok(found.has(type), type); assert.ok(toPlainText(tree).includes('legacy')); assert.ok(emojiForShortcode('smile')); }); test('generated malicious inputs stay data and parser has bounded nesting', () => { for (const source of maliciousMessages) { const tree = parseMarkdown(source); assert.equal(tree.type, 'doc'); assert.equal(types(tree).has('html'), false); assert.equal(types(tree).has('script'), false); } const deep = parseMarkdown('[b]'.repeat(1000) + 'text' + '[/b]'.repeat(1000)); assert.ok(toPlainText(deep).includes('text')); });