Files
X-Financial/web/tests/conversation-trusted-html.test.mjs
2026-06-23 11:21:18 +08:00

62 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from 'node:assert/strict'
import test from 'node:test'
import {
extractTrustedHtmlBlocks,
normalizeConversationText,
restoreTrustedHtmlBlocks
} from '../src/utils/conversationTrustedHtml.js'
test('conversation trusted html helper preserves valid document cards', () => {
const trustedBlock = [
'<!-- ai-trusted-html:start -->',
'<section class="ai-document-card-list" aria-label="单据结果">',
'<article class="ai-document-card" aria-label="单据详情">',
'<strong>差旅申请</strong>',
'<a class="ai-html-action-link" data-ai-action="open-document-detail" href="#ai-open-document-detail:CL-1">查看</a>',
'</article>',
'</section>',
'<!-- ai-trusted-html:end -->'
].join('')
const extracted = extractTrustedHtmlBlocks(`结果如下:\n\n${trustedBlock}`)
assert.equal(extracted.trustedHtmlBlocks.length, 1)
assert.match(extracted.content, /AI_TRUSTED_HTML_BLOCK_0/)
const restored = restoreTrustedHtmlBlocks(
'<p>AI_TRUSTED_HTML_BLOCK_0</p>\n',
extracted.trustedHtmlBlocks
)
assert.match(restored, /class="ai-document-card-list"/)
assert.match(restored, /href="#ai-open-document-detail:CL-1"/)
assert.doesNotMatch(restored, /AI_TRUSTED_HTML_BLOCK_0/)
})
test('conversation trusted html helper rejects unsafe trusted blocks', () => {
const extracted = extractTrustedHtmlBlocks([
'<!-- ai-trusted-html:start -->',
'<section class="ai-document-card-list">',
'<a href="javascript:alert(1)" onclick="alert(1)">危险</a>',
'</section>',
'<!-- ai-trusted-html:end -->'
].join(''))
assert.equal(extracted.trustedHtmlBlocks.length, 0)
assert.equal(extracted.content.trim(), '')
})
test('conversation trusted html helper normalizes business copy outside fences', () => {
const normalized = normalizeConversationText([
'基础信息识别结果:请核对',
'时间2026-02-20',
'',
'```',
'金额:不要改代码块',
'```'
].join('\n'), { trim: true })
assert.match(normalized, /### 基础信息识别结果/)
assert.match(normalized, /- \*\*时间\*\*2026-02-20/)
assert.match(normalized, /```\n金额不要改代码块\n```/)
})