/** * 临时校验:拦截规则防误判守卫(INTERCEPT_GUARD)修复效果。 * 背景:机器人格式 "物流单号:SF123 拦截" 因含"物流"被守卫拦掉 → 规则未命中 → 静默不回信。 * 用 fake ctx 直接实例化 inbox.rules Service,不连库、不写库。 * 运行:node script/tmp-check-inbox-intercept-guard.cjs */ 'use strict'; const RulesService = require('../app/service/inbox/rules'); function buildService(groupType, calls) { const svc = Object.create(RulesService.prototype); const registry = new Map([ [ 'interceptWaybill', { groups: [ 'bound:store:after_sales' ] }], [ 'cancelIntercept', { groups: [ 'bound:store:after_sales' ] }], [ 'queryWeight', { groups: [ 'bound:store:after_sales' ] }], [ 'changeAddr', { groups: [ 'bound:store:after_sales' ] }], ]); svc.ctx = { inboxContext: { group_type: groupType }, logger: { info() {}, warn() {}, error() {} }, service: { inbox: { groups: { resolveActiveGroups: () => [ 'bound:store:after_sales' ], isFunctionAvailable: def => Array.isArray(def.groups) && def.groups.length > 0, }, functions: { registry, callFunction: async (fn, params) => { calls.push({ fn, params }); return { success: true, data: { direct_reply: true, message: `MOCK ${fn}` } }; }, }, }, }, }; return svc; } const CASES = [ // [说明, 正文, 期望是否已处理, 期望函数, 期望提取到的运单号(可选,'' 表示应为空)] [ '机器人格式-物流单号+拦截', '优禾康官方旗舰店LSFJY202609143442\n物流单号:SF5151786672496 拦截', true, 'interceptWaybill' ], [ '机器人格式-单号中间带空格', '罗斯福医疗器械专营店LSFJY202608307192\n物流单 号:SF5117392937919 拦截', true, 'interceptWaybill' ], [ '机器人格式-物流二字被空格断开', '优禾康番茄健康监测专卖店LSFJY202609022796\n物 流单号:SF6048940605674 拦截', true, 'interceptWaybill' ], [ '机器人格式-快递单号标签', '快递单号:SF5151786672496 拦截', true, 'interceptWaybill' ], [ '普通拦截请求', '拦截 SF5151786672496', true, 'interceptWaybill' ], [ '取消拦截(长词优先)', '取消拦截 SF5151786672496', true, 'cancelIntercept' ], [ '查询物流到哪了(不应拦截)', '这个件拦截的物流到哪了 SF5151786672496', false, null ], [ '标签外仍有物流查询词(不应拦截)', '物流单号:SF5151786672496 拦截,顺便查下这个物流到哪了', false, null ], [ '查快递(无拦截词)', '帮我查快递 SF5151786672496 到哪了', false, null ], [ '客服对话-物流显示签收+拦截有风险', '772077887170843:已登记退回,物流显示签收,已通知站点核实处理,因签收之后的包裹拦截有风险', false, null ], [ '客服对话-物流轨迹查询', '麻烦看下这个件拦截了没有,物流轨迹一直没更新 SF5151786672496', false, null ], [ '确认语静默', '好的', true, null ], // 2026-09-15 回归:14 位纯数字运单号被“日期时间戳”过滤器误杀,规则只能回引导语 [ '改地址+14位中通运单号(不应被日期戳过滤)', '79140215263762 改地址 广东省广州市番禺区洛浦街道西三村友谊路1号菜鸟站(不打电话)收件人:王小姐19875119016', true, 'changeAddr', '79140215263762' ], [ '改地址+截断的11位手机号仍应被过滤(保留原行为)', '改地址 19875119016', true, 'changeAddr', '' ], [ '日期时间戳仍应被过滤(保留原行为)', '改地址 20260914201201', true, 'changeAddr', '' ], ]; (async () => { let pass = 0; const fails = []; for (const [ desc, text, expectHandled, expectFn, expectKeyword ] of CASES) { const calls = []; const svc = buildService('store', calls); const res = await svc.handle(text); const handled = !!res.handled; const fn = res.fn || null; const keyword = res.params && res.params.keyword != null ? String(res.params.keyword) : ''; let ok = handled === expectHandled && fn === expectFn; if (ok && expectKeyword !== undefined) { ok = expectKeyword === '' ? keyword === '' : keyword.includes(expectKeyword); } if (ok) pass++; else fails.push({ desc, expectHandled, expectFn, expectKeyword, handled, fn, keyword, silent: !!res.silent, calls }); console.log( `${ok ? '✅' : '❌'} ${desc} → handled=${handled} fn=${fn} silent=${!!res.silent} ` + `keyword=${JSON.stringify(keyword)} ` + `reply=${res.reply ? JSON.stringify(String(res.reply).slice(0, 40)) : '-'}` ); } console.log(`\n结果:${pass}/${CASES.length} 通过`); if (fails.length > 0) { console.log('失败明细:', JSON.stringify(fails, null, 2)); process.exitCode = 1; } // ---- 生产群上下文模拟:真实分组解析 + 函数可用性(groups.js 为纯逻辑,不连库)---- const GroupsService = require('../app/service/inbox/groups'); const prodCtx = { inboxContext: { warehouse_id: 1000028, store_id: 1000249, group_type: 'store', sub_type: 'after_sales', group_id: '47357811653@chatroom', }, logger: { info() {}, warn() {}, error() {} }, }; const groupsSvc = Object.create(GroupsService.prototype); groupsSvc.ctx = prodCtx; const active = groupsSvc.resolveActiveGroups(); const available = groupsSvc.isFunctionAvailable( { groups: [ 'bound:warehouse:customer_service', 'bound:store:after_sales' ] }, active ); console.log( '\n生产群 47357811653@chatroom(store/after_sales/store_id=1000249) ' + `activeGroups=${JSON.stringify(active)} interceptWaybill可用=${available}` ); if (!available) process.exitCode = 1; })();