#!/usr/bin/env node /** * Matrix test for WeChat page phrasing: intent, session continuation, delivery policy. * Run: node scripts/verify-wechat-page-phrasing.mjs */ import assert from 'node:assert/strict'; import { classifyWechatIntent } from '../wechat/intent/classifier.mjs'; import { isWechatSessionPageContinuation, isWechatImmediateContextPageCreate, } from '../wechat/intent/page-continuation.mjs'; import { resolveWechatImageGenerationPolicy, WECHAT_PAGE_THUMBNAIL_MODE } from '../wechat/image-generation-policy.mjs'; import { isWechatImmediateContextFollowup, shouldDeliverWechatHtmlArtifacts, } from '../wechat-mp.mjs'; import { buildPageGenerateAgentPrompt } from '../wechat/prompts/page-generate.mjs'; import { buildWechatAgentPrompt } from '../wechat/prompts/chat-general.mjs'; const cases = [ { label: '完整单条页面需求', text: '帮我做一个上海旅游攻略页面', expect: { kind: 'page.generate', continuation: false, freshThumbnail: true, deliverHtml: true, }, }, { label: '写诗(普通聊天)', text: '帮我写一首诗吧', expect: { kind: 'chat.general', continuation: false, freshThumbnail: false, deliverHtml: false, }, }, { label: '短指代做成页面', text: '把刚才的诗做成页面', expect: { kind: 'page.generate', continuation: true, freshThumbnail: false, deliverHtml: true, }, }, { label: '精美 HTML 页面', text: '生成一个精美的 HTML 页面', expect: { kind: 'page.generate', continuation: true, freshThumbnail: false, deliverHtml: true, }, }, { label: '重试上次页面', text: '重试上次页面', expect: { kind: 'page.generate', continuation: true, freshThumbnail: false, deliverHtml: true, }, }, { label: '修改刚才页面样式', text: '修改刚才生成的页面,背景改成浅蓝色', expect: { kind: 'page.generate', continuation: true, freshThumbnail: false, deliverHtml: true, }, }, { label: '改页面标题', text: '把页面标题改成《七月清晨》', expect: { kind: 'page.generate', continuation: true, freshThumbnail: false, deliverHtml: true, }, }, { label: '改诗内容(会话续作)', text: '把诗里第三段改长一点', expect: { kind: 'chat.general', continuation: true, freshThumbnail: false, deliverHtml: true, }, }, ]; let passed = 0; let failed = 0; for (const item of cases) { const intent = { msgType: 'text', agentText: item.text, displayText: item.text }; const wechatIntent = classifyWechatIntent(intent); const continuation = isWechatSessionPageContinuation(wechatIntent, item.text); const policy = resolveWechatImageGenerationPolicy({ text: item.text, isPageGenerate: wechatIntent.kind === 'page.generate', requireFreshPageThumbnail: continuation ? false : true, }); const deliverHtml = shouldDeliverWechatHtmlArtifacts(wechatIntent, intent); const errors = []; if (wechatIntent.kind !== item.expect.kind) { errors.push(`kind expected ${item.expect.kind}, got ${wechatIntent.kind}`); } if (continuation !== item.expect.continuation) { errors.push(`continuation expected ${item.expect.continuation}, got ${continuation}`); } const freshRequired = policy.pageThumbnailMode === WECHAT_PAGE_THUMBNAIL_MODE.REQUIRED_FRESH; if (freshRequired !== item.expect.freshThumbnail) { errors.push(`freshThumbnail expected ${item.expect.freshThumbnail}, got ${freshRequired}`); } if (deliverHtml !== item.expect.deliverHtml) { errors.push(`deliverHtml expected ${item.expect.deliverHtml}, got ${deliverHtml}`); } if (item.expect.continuation && wechatIntent.kind === 'page.generate') { const prompt = buildPageGenerateAgentPrompt(intent, { preferImmediateContext: continuation, imagePolicy: policy, }); if (!/即时上下文优先/.test(prompt)) errors.push('page prompt missing immediate context block'); if (!/write_file 硬门槛/.test(prompt)) errors.push('page prompt missing write_file gate'); } if (item.expect.continuation && wechatIntent.kind === 'chat.general') { const prompt = buildWechatAgentPrompt(intent, { preferSessionPageContinuation: continuation }); if (!/会话页面续作/.test(prompt)) errors.push('chat prompt missing session continuation block'); } if (errors.length) { failed += 1; console.error(`✘ ${item.label}: ${errors.join('; ')}`); } else { passed += 1; console.log(`✔ ${item.label}`); } } assert.equal(isWechatImmediateContextFollowup({ kind: 'page.generate' }, '把刚才的诗做成页面'), true); assert.equal(isWechatImmediateContextPageCreate({ kind: 'page.generate' }, '生成一个精美的 HTML 页面'), true); console.log(`\n=== 汇总: ${passed}/${cases.length} 话术矩阵通过 ===`); if (failed > 0) process.exit(1);