| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /**
- * 验证「更多」→「政策列表」侧边栏的纯逻辑:跨消息聚合、去重、排序、关键词过滤。
- *
- * 为什么要单独测:卡片区每个 PolicyMatch 实例只持有**自己那条消息**的卡片,
- * 侧边栏要的是**整个会话**的聚合,这段逻辑(解析历史消息 → 去重 → 排序 → 过滤)
- * 容易在边界上出错(坏 JSON、重复卡片、精简格式占位符、同分排序抖动)。
- *
- * 怎么跑(在项目根目录):
- * npx esbuild harness/tools/_entry-policy-cards.ts --bundle --format=esm \
- * --outfile=harness/tools/_policy-cards.mjs
- * node harness/tools/verify-policy-card-list.mjs
- */
- import {
- collectPolicyCardsFromMessages,
- buildPolicyCardDedupKey,
- dedupePolicyItems,
- sortPolicyCardsByMatchScore,
- matchesPolicyCardKeyword,
- } from './_policy-cards.mjs';
- let pass = 0;
- let fail = 0;
- const check = (name, cond, extra = '') => {
- if (cond) {
- pass++;
- console.log(' ok ' + name);
- } else {
- fail++;
- console.log(' FAIL ' + name + ' ' + extra);
- }
- };
- /** 造一条带 POLICY_TABLE 块的 AI 消息(格式与适配层 flushContent 产出一致) */
- const aiMessage = (cards, extraText = '正文') =>
- `${extraText}\n<!-- POLICY_TABLE ${JSON.stringify({ data: cards })} POLICY_TABLE -->\n`;
- console.log('【1】跨消息聚合(collectPolicyCardsFromMessages)');
- const cardA = { declaration_item: '政策甲 - 事项一', title: '政策甲', match_score: 90 };
- const cardB = { declaration_item: '政策乙 - 事项一', title: '政策乙', match_score: 45 };
- const cardC = { declaration_item: '政策丙 - 事项一', title: '政策丙', match_score: 60 };
- const messages = [
- { role: 'user', content: '我有什么政策' }, // 用户消息不该被解析
- { role: 'ai', content: aiMessage([cardA, cardB]) },
- { role: 'ai', content: '这条没有卡片' },
- { role: 'ai', content: aiMessage([cardC]) },
- ];
- const collected = collectPolicyCardsFromMessages(messages);
- check('收集到 3 张卡片', collected.length === 3, `实际 ${collected.length}`);
- check('保持消息顺序(甲→乙→丙)', collected.map((c) => c.title).join(',') === '政策甲,政策乙,政策丙', collected.map((c) => c.title).join(','));
- check('用户消息被跳过', !collected.some((c) => c.title === '我有什么政策'));
- check('空输入不抛错 → []', Array.isArray(collectPolicyCardsFromMessages([])) && collectPolicyCardsFromMessages([]).length === 0);
- check('null 输入不抛错 → []', collectPolicyCardsFromMessages(null).length === 0);
- check('坏 JSON 不抛错且不贡献卡片', collectPolicyCardsFromMessages([{ role: 'ai', content: '<!-- POLICY_TABLE {坏JSON POLICY_TABLE -->' }]).length === 0);
- check('无 POLICY_TABLE 块 → []', collectPolicyCardsFromMessages([{ role: 'ai', content: '纯文本回答' }]).length === 0);
- console.log('\n【2】去重键(buildPolicyCardDedupKey)');
- check(
- '同 declaration_item → 同键',
- buildPolicyCardDedupKey(cardA) === buildPolicyCardDedupKey({ ...cardA, match_score: 10 }),
- buildPolicyCardDedupKey(cardA)
- );
- check('不同 declaration_item → 不同键', buildPolicyCardDedupKey(cardA) !== buildPolicyCardDedupKey(cardB));
- // 精简格式:declaration_item 是占位符,不能把不同卡片并成一条
- const minimalA = { declaration_item: '查看政策详情', title: '甲', match_reason: '理由A' };
- const minimalB = { declaration_item: '查看政策详情', title: '乙', match_reason: '理由B' };
- check('占位符「查看政策详情」不按它去重(不同卡片不同键)', buildPolicyCardDedupKey(minimalA) !== buildPolicyCardDedupKey(minimalB), buildPolicyCardDedupKey(minimalA));
- check('占位符场景仍可用同内容命中同键', buildPolicyCardDedupKey(minimalA) === buildPolicyCardDedupKey({ ...minimalA }));
- console.log('\n【3】去重(dedupePolicyItems,首见优先)');
- const withDup = [cardA, cardB, { ...cardA, match_score: 999 }, cardC, { ...cardB }];
- const deduped = dedupePolicyItems(withDup, buildPolicyCardDedupKey);
- check('去重后 3 条', deduped.length === 3, `实际 ${deduped.length}`);
- check('保留的是**首见**那条(match_score=90 而非 999)', deduped[0].match_score === 90, String(deduped[0].match_score));
- check('保持首见顺序', deduped.map((c) => c.title).join(',') === '政策甲,政策乙,政策丙');
- check('空输入安全', dedupePolicyItems(null, buildPolicyCardDedupKey).length === 0);
- console.log('\n【4】排序(sortPolicyCardsByMatchScore)');
- const sorted = sortPolicyCardsByMatchScore([cardB, cardA, cardC]); // 45, 90, 60
- check('按 match_score 降序', sorted.map((c) => c.match_score).join(',') === '90,60,45', sorted.map((c) => c.match_score).join(','));
- const tieA = { declaration_item: 'a', match_score: 90, tag2: 'first' };
- const tieB = { declaration_item: 'b', match_score: 90, tag2: 'second' };
- const tieSorted = sortPolicyCardsByMatchScore([tieA, tieB]);
- check('同分保持原相对顺序(稳定排序)', tieSorted[0].tag2 === 'first' && tieSorted[1].tag2 === 'second', tieSorted.map((c) => c.tag2).join(','));
- check('缺 match_score 视为 0 排最后', sortPolicyCardsByMatchScore([{ declaration_item: 'x' }, cardB])[1].declaration_item === 'x');
- check('空输入安全', sortPolicyCardsByMatchScore([]).length === 0);
- console.log('\n【5】关键词过滤(matchesPolicyCardKeyword)');
- const searchable = {
- title: '关于创业扶持的通知',
- desc: '面向本区创业者',
- declaration_item: '创业开办费补贴',
- match_reason: '用户询问开办补贴',
- source: '区人社局',
- enterprise_benefit: '最高2万元补贴',
- };
- check('空关键词恒真', matchesPolicyCardKeyword(searchable, '') === true && matchesPolicyCardKeyword(searchable, ' ') === true);
- check('命中 title', matchesPolicyCardKeyword(searchable, '创业扶持') === true);
- check('命中 desc', matchesPolicyCardKeyword(searchable, '本区创业者') === true);
- check('命中 declaration_item', matchesPolicyCardKeyword(searchable, '开办费') === true);
- check('命中 match_reason', matchesPolicyCardKeyword(searchable, '询问') === true);
- check('命中 source(部门)', matchesPolicyCardKeyword(searchable, '区人社局') === true);
- check('命中 enterprise_benefit', matchesPolicyCardKeyword(searchable, '2万元') === true);
- check('大小写不敏感', matchesPolicyCardKeyword({ title: 'ABC Policy' }, 'abc policy') === true);
- check('不命中返回 false', matchesPolicyCardKeyword(searchable, '不存在的词') === false);
- check('item 为 null 不抛错', matchesPolicyCardKeyword(null, 'x') === false);
- console.log('\n【6】端到端组合(聚合 → 去重 → 排序 → 过滤)');
- const e2e = sortPolicyCardsByMatchScore(
- dedupePolicyItems(collectPolicyCardsFromMessages(messages), buildPolicyCardDedupKey)
- );
- check('组合后 3 条且按分数降序', e2e.length === 3 && e2e[0].match_score === 90, e2e.map((c) => c.match_score).join(','));
- const e2eFiltered = e2e.filter((c) => matchesPolicyCardKeyword(c, '政策乙'));
- check('组合后可按关键词筛出 1 条', e2eFiltered.length === 1 && e2eFiltered[0].title === '政策乙', String(e2eFiltered.length));
- console.log(`\n===== 通过 ${pass} 项,失败 ${fail} 项 =====`);
- process.exit(fail ? 1 : 0);
|