verify-policy-card-list.mjs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * 验证「更多」→「政策列表」侧边栏的纯逻辑:去重、排序、关键词过滤。
  3. *
  4. * 注:侧边栏列的是**本条回答里的政策**(用户 2026-09-18 调整;此前一度做成
  5. * 「整个会话跨消息聚合」,那个采集函数已随之删除)。
  6. *
  7. * 为什么要单独测:列表要经过「去重 → 排序 → 关键词过滤」三步,
  8. * 边界容易出错(精简格式的占位符 declaration_item、同分排序抖动、空关键词、
  9. * 空/脏输入)。这些都是纯逻辑,用断言守住比在浏览器里点便宜得多。
  10. *
  11. * 怎么跑(在项目根目录):
  12. * npx esbuild harness/tools/_entry-policy-cards.ts --bundle --format=esm \
  13. * --outfile=harness/tools/_policy-cards.mjs
  14. * node harness/tools/verify-policy-card-list.mjs
  15. */
  16. import {
  17. buildPolicyCardDedupKey,
  18. dedupePolicyItems,
  19. sortPolicyCardsByMatchScore,
  20. matchesPolicyCardKeyword,
  21. } from './_policy-cards.mjs';
  22. let pass = 0;
  23. let fail = 0;
  24. const check = (name, cond, extra = '') => {
  25. if (cond) {
  26. pass++;
  27. console.log(' ok ' + name);
  28. } else {
  29. fail++;
  30. console.log(' FAIL ' + name + ' ' + extra);
  31. }
  32. };
  33. /** 三条测试用卡片(后续各节共用) */
  34. const cardA = { declaration_item: '政策甲 - 事项一', title: '政策甲', match_score: 90 };
  35. const cardB = { declaration_item: '政策乙 - 事项一', title: '政策乙', match_score: 45 };
  36. const cardC = { declaration_item: '政策丙 - 事项一', title: '政策丙', match_score: 60 };
  37. console.log('【1】去重键(buildPolicyCardDedupKey)');
  38. check(
  39. '同 declaration_item → 同键',
  40. buildPolicyCardDedupKey(cardA) === buildPolicyCardDedupKey({ ...cardA, match_score: 10 }),
  41. buildPolicyCardDedupKey(cardA)
  42. );
  43. check('不同 declaration_item → 不同键', buildPolicyCardDedupKey(cardA) !== buildPolicyCardDedupKey(cardB));
  44. // 精简格式:declaration_item 是占位符,不能把不同卡片并成一条
  45. const minimalA = { declaration_item: '查看政策详情', title: '甲', match_reason: '理由A' };
  46. const minimalB = { declaration_item: '查看政策详情', title: '乙', match_reason: '理由B' };
  47. check('占位符「查看政策详情」不按它去重(不同卡片不同键)', buildPolicyCardDedupKey(minimalA) !== buildPolicyCardDedupKey(minimalB), buildPolicyCardDedupKey(minimalA));
  48. check('占位符场景仍可用同内容命中同键', buildPolicyCardDedupKey(minimalA) === buildPolicyCardDedupKey({ ...minimalA }));
  49. console.log('\n【3】去重(dedupePolicyItems,首见优先)');
  50. const withDup = [cardA, cardB, { ...cardA, match_score: 999 }, cardC, { ...cardB }];
  51. const deduped = dedupePolicyItems(withDup, buildPolicyCardDedupKey);
  52. check('去重后 3 条', deduped.length === 3, `实际 ${deduped.length}`);
  53. check('保留的是**首见**那条(match_score=90 而非 999)', deduped[0].match_score === 90, String(deduped[0].match_score));
  54. check('保持首见顺序', deduped.map((c) => c.title).join(',') === '政策甲,政策乙,政策丙');
  55. check('空输入安全', dedupePolicyItems(null, buildPolicyCardDedupKey).length === 0);
  56. console.log('\n【4】排序(sortPolicyCardsByMatchScore)');
  57. const sorted = sortPolicyCardsByMatchScore([cardB, cardA, cardC]); // 45, 90, 60
  58. check('按 match_score 降序', sorted.map((c) => c.match_score).join(',') === '90,60,45', sorted.map((c) => c.match_score).join(','));
  59. const tieA = { declaration_item: 'a', match_score: 90, tag2: 'first' };
  60. const tieB = { declaration_item: 'b', match_score: 90, tag2: 'second' };
  61. const tieSorted = sortPolicyCardsByMatchScore([tieA, tieB]);
  62. check('同分保持原相对顺序(稳定排序)', tieSorted[0].tag2 === 'first' && tieSorted[1].tag2 === 'second', tieSorted.map((c) => c.tag2).join(','));
  63. check('缺 match_score 视为 0 排最后', sortPolicyCardsByMatchScore([{ declaration_item: 'x' }, cardB])[1].declaration_item === 'x');
  64. check('空输入安全', sortPolicyCardsByMatchScore([]).length === 0);
  65. console.log('\n【5】关键词过滤(matchesPolicyCardKeyword)');
  66. const searchable = {
  67. title: '关于创业扶持的通知',
  68. desc: '面向本区创业者',
  69. declaration_item: '创业开办费补贴',
  70. match_reason: '用户询问开办补贴',
  71. source: '区人社局',
  72. enterprise_benefit: '最高2万元补贴',
  73. };
  74. check('空关键词恒真', matchesPolicyCardKeyword(searchable, '') === true && matchesPolicyCardKeyword(searchable, ' ') === true);
  75. check('命中 title', matchesPolicyCardKeyword(searchable, '创业扶持') === true);
  76. check('命中 desc', matchesPolicyCardKeyword(searchable, '本区创业者') === true);
  77. check('命中 declaration_item', matchesPolicyCardKeyword(searchable, '开办费') === true);
  78. check('命中 match_reason', matchesPolicyCardKeyword(searchable, '询问') === true);
  79. check('命中 source(部门)', matchesPolicyCardKeyword(searchable, '区人社局') === true);
  80. check('命中 enterprise_benefit', matchesPolicyCardKeyword(searchable, '2万元') === true);
  81. check('大小写不敏感', matchesPolicyCardKeyword({ title: 'ABC Policy' }, 'abc policy') === true);
  82. check('不命中返回 false', matchesPolicyCardKeyword(searchable, '不存在的词') === false);
  83. check('item 为 null 不抛错', matchesPolicyCardKeyword(null, 'x') === false);
  84. console.log('\n【6】端到端组合(去重 → 排序 → 过滤)');
  85. const e2e = sortPolicyCardsByMatchScore(
  86. dedupePolicyItems([cardA, cardB, cardC], buildPolicyCardDedupKey)
  87. );
  88. check('组合后 3 条且按分数降序', e2e.length === 3 && e2e[0].match_score === 90, e2e.map((c) => c.match_score).join(','));
  89. const e2eFiltered = e2e.filter((c) => matchesPolicyCardKeyword(c, '政策乙'));
  90. check('组合后可按关键词筛出 1 条', e2eFiltered.length === 1 && e2eFiltered[0].title === '政策乙', String(e2eFiltered.length));
  91. console.log(`\n===== 通过 ${pass} 项,失败 ${fail} 项 =====`);
  92. process.exit(fail ? 1 : 0);