import assert from 'node:assert/strict';
import { test } from 'vitest';
import type { Code } from 'mdast';
import {
isMcpAppCodeBlock,
extractMcpAppTitle,
extractMcpAppHeight,
ensureHtmlDocument,
extractHtmlTitle,
formatReceiptPrompt,
findComposerTextarea,
fillComposerDom,
} from '../src/client/mcp-app.js';
import { parseGfmWithMath } from '../src/client/markdown/parse.js';
test('isMcpAppCodeBlock detects all valid MCP App code block formats', () => {
// Direct lang tags
assert.equal(isMcpAppCodeBlock('mcp-app', null, '
test
'), true);
assert.equal(isMcpAppCodeBlock('mcpapp', null, 'test
'), true);
assert.equal(isMcpAppCodeBlock('mcp-ui', null, 'test
'), true);
assert.equal(isMcpAppCodeBlock('mcp', null, 'test
'), true);
assert.equal(isMcpAppCodeBlock('mcp-app:html', null, 'test
'), true);
assert.equal(isMcpAppCodeBlock('html:mcp-app', null, 'test
'), true);
// Meta indicators
assert.equal(isMcpAppCodeBlock('html', 'mcp-app title="测试"', 'test
'), true);
assert.equal(isMcpAppCodeBlock(null, 'sep-1865', 'test
'), true);
// Content heuristic for SEP-1865 JSON-RPC
const quizSnippet = `
window.parent.postMessage({
jsonrpc: "2.0",
method: "ui/submit",
params: { choice: "A" }
}, "*");
`;
assert.equal(isMcpAppCodeBlock('html', null, quizSnippet), true);
// Negative cases
assert.equal(isMcpAppCodeBlock('typescript', null, 'const x = 1;'), false);
assert.equal(isMcpAppCodeBlock('json', null, '{"a": 1}'), false);
assert.equal(isMcpAppCodeBlock('html', null, 'Hello World '), false);
assert.equal(isMcpAppCodeBlock(null, null, ''), false);
});
test('extractMcpAppTitle extracts title from meta or html content', () => {
assert.equal(extractMcpAppTitle('title="双盲方案盲选评测器"', 'test
'), '双盲方案盲选评测器');
assert.equal(extractMcpAppTitle("title='架构方案对比'", 'test
'), '架构方案对比');
assert.equal(extractMcpAppTitle(null, '网页标题评测 '), '网页标题评测');
assert.equal(extractMcpAppTitle(null, '无标题内容
'), undefined);
});
test('extractMcpAppHeight parses explicit height budget from meta', () => {
assert.equal(extractMcpAppHeight('title="看板" height=600'), 600);
assert.equal(extractMcpAppHeight('height="550px"'), 550);
assert.equal(extractMcpAppHeight('height=50'), undefined); // too small
assert.equal(extractMcpAppHeight('title="普通"'), undefined);
});
test('extractHtmlTitle extracts document title accurately', () => {
assert.equal(extractHtmlTitle('我的测试 App '), '我的测试 App');
assert.equal(extractHtmlTitle(' 性能看板 '), '性能看板');
assert.equal(extractHtmlTitle('没有标题
'), undefined);
});
test('ensureHtmlDocument wraps bare fragments and preserves complete documents with theme bridge', () => {
const completeHtml = '\nTest Content';
const processed = ensureHtmlDocument(completeHtml, 'dark');
assert.ok(processed.includes('Test '));
assert.ok(processed.includes('Content'));
assert.ok(processed.includes('applyTheme'));
const fragment = '
Question ';
const wrapped = ensureHtmlDocument(fragment, 'dark');
assert.ok(wrapped.startsWith(''));
assert.ok(wrapped.includes(' {
const markdown = `
这里是为您生成的方案评测器,请在下方直接点击交互:
\`\`\`mcp-app title="双盲方案盲选评测器"
双盲方案盲选评测器
题目
B: 延迟消息队列
\`\`\`
如有其他疑问,请随时告诉我。
`.trim();
const root = parseGfmWithMath(markdown);
const codeNode = root.children.find((child): child is Code => child.type === 'code');
assert.ok(codeNode, 'Must contain a code block node');
assert.equal(codeNode.lang, 'mcp-app');
assert.equal(codeNode.meta, 'title="双盲方案盲选评测器"');
assert.equal(isMcpAppCodeBlock(codeNode.lang, codeNode.meta, codeNode.value), true);
assert.equal(extractMcpAppTitle(codeNode.meta, codeNode.value), '双盲方案盲选评测器');
assert.ok(ensureHtmlDocument(codeNode.value).includes('延迟消息队列'));
});
test('formatReceiptPrompt formats clean, natural conversational text without extra whitespace or raw JSON dump', () => {
const quizParams = {
task: 'architecture_quiz_completed',
choice: 'B',
desc: '延迟消息队列',
};
const prompt = formatReceiptPrompt(quizParams, '双盲方案盲选评测器');
assert.equal(prompt, '我在「双盲方案盲选评测器」中选择了:B(延迟消息队列)。请根据我的选择继续。');
assert.ok(!prompt.includes('\n'), 'Must not contain multiline newlines');
assert.ok(!prompt.includes('{'), 'Must not dump raw JSON braces');
const variantParams = {
selectedVariant: 'Skill_v2',
score: 95,
};
assert.equal(formatReceiptPrompt(variantParams), '我在方案评测中选择了:Skill_v2,得分:95。请根据该方案继续分析。');
const actionParams = {
action: 'reset_counter',
payload: { count: 0 },
};
assert.equal(formatReceiptPrompt(actionParams, '计数器'), '[计数器] 已完成 reset_counter: {"count":0}');
});
test('ensureHtmlDocument injects theme bridge and auto-height reporter into complete documents', () => {
const completeHtml = '\nDemo hi
';
const processed = ensureHtmlDocument(completeHtml, 'dark');
assert.ok(processed.includes('Demo '));
assert.ok(processed.includes('hi
'));
// Theme bridge must apply the initial theme and listen for live host updates
// (host pushes ui/initialize result / host-context-changed carrying hostContext.theme).
assert.ok(processed.includes('applyTheme("dark")'));
assert.ok(processed.includes('hostContext'));
// Auto-height reporter must observe the body and emit SEP-1865 resize events.
assert.ok(processed.includes('ResizeObserver'));
assert.ok(processed.includes('ui/resize'));
});
test('findComposerTextarea is importable and null-safe without a DOM', () => {
assert.equal(findComposerTextarea(undefined as unknown as Document), null);
});
test('fillComposerDom refuses without a DOM instead of throwing', () => {
assert.equal(fillComposerDom('hello'), false);
});