import fs from 'node:fs/promises'; import { execFileSync } from 'node:child_process'; import { fileURLToPath } from 'node:url'; import { wechatTaskRouteScenarios } from '../wechat/intent/task-route-scenarios.mjs'; import { classifyWechatIntent } from '../wechat/intent/classifier.mjs'; import { readWechatTaskContext, resolveWechatTaskRoute } from '../wechat/intent/task-route.mjs'; function simulate(resolveRoute, readContext) { return wechatTaskRouteScenarios.map((scenario) => { const decision = resolveRoute({ wechatIntent: classifyWechatIntent({ msgType: 'text', agentText: scenario.text }), text: scenario.text, context: readContext(scenario.conversation), }); const passed = scenario.kind ? decision.kind === scenario.kind : decision.taskRoute?.mode === scenario.mode && (!scenario.target || decision.taskRoute?.targetPath === scenario.target); return { name: scenario.name, input: scenario.text, expected: { kind: scenario.kind, mode: scenario.mode, target: scenario.target }, actual: { kind: decision.kind, mode: decision.taskRoute?.mode, target: decision.taskRoute?.targetPath }, passed, }; }); } const results = simulate(resolveWechatTaskRoute, readWechatTaskContext); const report = { simulation: 'local deterministic routing with synthetic conversation history; no live WeChat or LLM calls', total: results.length, passed: results.filter((result) => result.passed).length, failed: results.filter((result) => !result.passed).length, results, }; const baseline = process.argv.find((arg) => arg.startsWith('--baseline='))?.slice(11); if (baseline) { if (!/^[a-f\d]{7,40}$/iu.test(baseline)) throw new Error('baseline must be a Git commit hash'); const source = execFileSync('git', ['show', `${baseline}:wechat/intent/task-route.mjs`], { cwd: fileURLToPath(new URL('..', import.meta.url)), encoding: 'utf8', }).replace(/from '([^']+)'/gu, (_match, specifier) => `from '${new URL(specifier, new URL('../wechat/intent/task-route.mjs', import.meta.url)).href}'`); const previous = await import(`data:text/javascript;base64,${Buffer.from(source).toString('base64')}`); const previousResults = simulate(previous.resolveWechatTaskRoute, previous.readWechatTaskContext); report.baseline = { commit: baseline, scope: 'historical task-route module with the same synthetic cases', total: previousResults.length, passed: previousResults.filter((result) => result.passed).length, failed: previousResults.filter((result) => !result.passed).length, results: previousResults, }; } const out = process.argv.find((arg) => arg.startsWith('--out='))?.slice(6); if (out) await fs.writeFile(out, JSON.stringify(report, null, 2) + '\n'); for (const result of results) console.log(`${result.passed ? 'PASS' : 'FAIL'} ${result.name}`); console.log(`Simulation: ${report.passed}/${report.total} passed, ${report.failed} failed`); if (report.baseline) console.log(`Baseline ${baseline}: ${report.baseline.passed}/${report.baseline.total} passed`); if (report.failed) process.exitCode = 1;