import assert from 'node:assert/strict' import test from 'node:test' import { createChatMarkdownRenderer, preprocessMathDelimiters, renderChatMarkdown, replaceIncompleteImageWithPlaceholder, } from './chatMarkdownRenderer.ts' import { resolveCitationChunkId, joinCitationTagsToPreviousLine, collapseStandaloneCitationParagraphs } from './citationMarkdown.ts' const SAMPLE_DOC = 'example-report.docx' const SAMPLE_CHUNK_A = '00000001-0000-4000-8000-000000000001' const SAMPLE_CHUNK_B = '00000002-0000-4000-8000-000000000002' const SAMPLE_CHUNK_C = '00000003-0000-4000-8000-000000000003' const SAMPLE_CHUNK_PRESERVE = 'aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee' test('preprocessMathDelimiters converts escaped math delimiters for marked-katex', () => { assert.equal( preprocessMathDelimiters('inline \\(a+b\\) and block \\[x^2\\]'), 'inline $a+b$ and block $$x^2$$', ) }) test('replaceIncompleteImageWithPlaceholder hides an unfinished streaming image', () => { assert.equal( replaceIncompleteImageWithPlaceholder('before ![chart](local://bucket/path'), 'before ', ) }) test('renderChatMarkdown preserves citations, math, and sanitized output through one shared pipeline', () => { const renderer = createChatMarkdownRenderer({ imageRenderer: ({ href, title, text }) => `${text}`, isValidImageUrl: (href) => href.startsWith('https://'), }) const html = renderChatMarkdown( [ 'See ', '', 'Formula: \\(E=mc^2\\)', '', '| A | B |', '| --- | --- |', '| 1 | 2 |', '', '![ok](https://example.com/a.png "图")', '', '![bad](javascript:alert(1))', ].join('\n'), { renderer, escapeMarkdown: (text) => text, sanitizeHtml: (html) => html.replace(/javascript:alert\(1\)/g, ''), }, ) assert.match(html, /class="citation citation-kb"/) assert.match(html, /data-chunk-id="chunk-1"/) assert.match(html, /katex/) assert.match(html, /
/) assert.match(html, / { const refs = [ { id: 'uuid-chunk-1', knowledge_title: 'Doc A', chunk_type: 'faq' }, { id: 'uuid-chunk-2', knowledge_title: 'FAQ TEST - FAQ', chunk_type: 'faq' }, ] assert.equal( resolveCitationChunkId('2', { doc: 'FAQ TEST - FAQ' }, refs), 'uuid-chunk-2', ) assert.equal( resolveCitationChunkId('FAQ-2', { doc: 'FAQ TEST - FAQ' }, refs), 'uuid-chunk-2', ) assert.equal( resolveCitationChunkId('uuid-chunk-1', { doc: 'Doc A' }, refs), 'uuid-chunk-1', ) }) test('renderChatMarkdown preserves chunk UUIDs when escapeMarkdown strips UUIDs from prose', () => { const renderer = createChatMarkdownRenderer({ imageRenderer: ({ href, text }) => `${text}`, isValidImageUrl: () => true, }) const chunkId = SAMPLE_CHUNK_PRESERVE const stripUuids = (text: string) => text.replace( /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '', ) const html = renderChatMarkdown( `Sample text `, { renderer, escapeMarkdown: stripUuids, sanitizeHtml: (html) => html, }, ) assert.match(html, /class="citation citation-kb"/) assert.match(html, new RegExp(`data-chunk-id="${chunkId}"`)) }) test('renderChatMarkdown resolves indexed chunk_id when knowledge references are provided', () => { const renderer = createChatMarkdownRenderer({ imageRenderer: ({ href, text }) => `${text}`, isValidImageUrl: () => true, }) const html = renderChatMarkdown( 'See ', { renderer, escapeMarkdown: (text) => text, sanitizeHtml: (html) => html, knowledgeReferences: [ { id: 'resolved-chunk-id', knowledge_title: 'Sample FAQ', chunk_type: 'faq' }, { id: 'other-chunk', knowledge_title: 'Other FAQ', chunk_type: 'faq' }, ], }, ) assert.match(html, /data-chunk-id="resolved-chunk-id"/) assert.doesNotMatch(html, /data-chunk-id="2"/) }) test('joinCitationTagsToPreviousLine removes blank lines before citation tags', () => { const input = 'Setup is complete.\n\n' assert.equal(joinCitationTagsToPreviousLine(input), 'Setup is complete. ') }) test('joinCitationTagsToPreviousLine inlines consecutive citation tags across single newlines', () => { const tag1 = `` const tag2 = `` const tag3 = `` const input = `${tag1}\n${tag2}\n${tag3}` assert.equal(joinCitationTagsToPreviousLine(input), `${tag1} ${tag2} ${tag3}`) }) test('renderChatMarkdown inlines consecutive citation tags across newlines', () => { const renderer = createChatMarkdownRenderer({ imageRenderer: ({ href, text }) => `${text}`, isValidImageUrl: () => true, }) const html = renderChatMarkdown( [ ``, ``, ``, ].join('\n'), { renderer, escapeMarkdown: (text) => text, sanitizeHtml: (html) => html, }, ) assert.equal((html.match(/citation-kb/g) || []).length, 3) assert.doesNotMatch(html, /<\/p>\s*

\s* { const tag = '' const input = `1. first\n2. second\n\n${tag}` assert.equal(joinCitationTagsToPreviousLine(input), `1. first\n2. second\n\n${tag}`) }) test('collapseStandaloneCitationParagraphs merges citations across empty paragraphs', () => { const html = '

Steps:

doc

' const out = collapseStandaloneCitationParagraphs(html) assert.match(out, /Steps:.*citation-kb/s) assert.doesNotMatch(out, /

<\/p>/) })