|
@@ -7,7 +7,7 @@
|
|
|
// /api/image/test、/api/chat/test,也不在数据湖有主体时调用 /api/reports/ask。
|
|
// /api/image/test、/api/chat/test,也不在数据湖有主体时调用 /api/reports/ask。
|
|
|
// 用法:node scripts/smoke.js
|
|
// 用法:node scripts/smoke.js
|
|
|
|
|
|
|
|
-const { spawn } = require('child_process');
|
|
|
|
|
|
|
+const { spawn, spawnSync } = require('child_process');
|
|
|
const net = require('net');
|
|
const net = require('net');
|
|
|
const http = require('http');
|
|
const http = require('http');
|
|
|
const os = require('os');
|
|
const os = require('os');
|
|
@@ -203,6 +203,14 @@ async function main() {
|
|
|
const r = await request(port, 'POST', '/api/crawl/stop');
|
|
const r = await request(port, 'POST', '/api/crawl/stop');
|
|
|
expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
|
|
expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
|
|
|
});
|
|
});
|
|
|
|
|
+ await check('Origin 防护:跨站写请求 → 403', async () => {
|
|
|
|
|
+ const r = await request(port, 'POST', '/api/crawl/stop', undefined, '127.0.0.1', { Origin: 'http://evil.example' });
|
|
|
|
|
+ expect(r.status === 403, `status=${r.status}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('Origin 防护:同源写请求放行', async () => {
|
|
|
|
|
+ const r = await request(port, 'POST', '/api/crawl/stop', undefined, '127.0.0.1', { Origin: `http://127.0.0.1:${port}` });
|
|
|
|
|
+ expect(r.status === 200, `status=${r.status}`);
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
// ---- 生成/任务/历史域 ----
|
|
// ---- 生成/任务/历史域 ----
|
|
|
await check('GET /api/tasks', async () => {
|
|
await check('GET /api/tasks', async () => {
|
|
@@ -361,8 +369,89 @@ async function main() {
|
|
|
expect(t.includes('2026-09-18') && t.includes('营业额'), t);
|
|
expect(t.includes('2026-09-18') && t.includes('营业额'), t);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // ---- 问数智能体:动作解析/图表钳制/循环侦测(纯函数,不调对话模型) ----
|
|
|
|
|
+ const agentLlm = require('../server/agent/llm.js');
|
|
|
|
|
+ const agentGuard = require('../server/agent/guard.js');
|
|
|
|
|
+ const naposServiceLib = require('../napos-service.js');
|
|
|
|
|
+ await check('parseAgentAction → 工具动作/终答/围栏解析', async () => {
|
|
|
|
|
+ const a = agentLlm.parseAgentAction('{"thought":"查","action":{"tool":"query_report","args":{"mode":"shop"}}}');
|
|
|
|
|
+ expect(a.type === 'tool' && a.tool === 'query_report' && a.args.mode === 'shop', JSON.stringify(a));
|
|
|
|
|
+ const f = agentLlm.parseAgentAction('前言```json\n{"thought":"完","final":{"answer":"结论"}}\n```后记');
|
|
|
|
|
+ expect(f.type === 'final' && f.final.answer === '结论', JSON.stringify(f));
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('parseAgentAction → 非法输出全部给出可读拒绝', async () => {
|
|
|
|
|
+ const bads = ['不是json', '{"thought":"只有thought"}', '{"final":{"answer":" "}}', '{"final":"字符串"}'];
|
|
|
|
|
+ for (const bad of bads) {
|
|
|
|
|
+ let rejected = false;
|
|
|
|
|
+ try { agentLlm.parseAgentAction(bad); } catch { rejected = true; }
|
|
|
|
|
+ expect(rejected, `未拒绝非法输出:${bad}`);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('clampChart → 指标白名单与非法类型兜底', async () => {
|
|
|
|
|
+ expect(agentGuard.clampChart({ type: 'pie', x: 'date', y: ['grossAmt'] }, naposServiceLib) === null, 'pie 应被拒绝');
|
|
|
|
|
+ const c = agentGuard.clampChart({ type: 'line', x: 'date', y: ['grossAmt', 'hacked', 'validOrderCnt'] }, naposServiceLib);
|
|
|
|
|
+ expect(c && c.type === 'line' && c.x === 'date' && c.y.length === 2 && !c.y.includes('hacked'), JSON.stringify(c));
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('LoopDetector → 重复先提醒、三犯强制收敛、换参重置', async () => {
|
|
|
|
|
+ const d = new agentGuard.LoopDetector();
|
|
|
|
|
+ expect(d.record('t', { a: 1 }) === 'ok', '首次应为 ok');
|
|
|
|
|
+ expect(d.record('t', { a: 1 }) === 'repeat', '第二次应为 repeat');
|
|
|
|
|
+ expect(d.record('t', { a: 1 }) === 'loop', '第三次应为 loop');
|
|
|
|
|
+ expect(d.record('t', { a: 2 }) === 'ok', '换参数应重置');
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('技能 → 清单注入 prompt,点名技能有专属指令', async () => {
|
|
|
|
|
+ const skillsLib = require('../server/agent/skills.js');
|
|
|
|
|
+ const agentPrompt = require('../server/agent/prompt.js');
|
|
|
|
|
+ const base = {
|
|
|
|
|
+ svc: naposServiceLib, entityName: '测试主体', shops: [], dateRange: { from: '20260101', to: '20260201' },
|
|
|
|
|
+ today: '20260201', toolsText: '- t:测试工具',
|
|
|
|
|
+ };
|
|
|
|
|
+ const p1 = agentPrompt.buildSystemPrompt({ ...base, skillsText: skillsLib.manifestText() });
|
|
|
|
|
+ expect(p1.includes('内置技能') && p1.includes('周期对比分析') && p1.includes('数据体检'), '技能清单未注入');
|
|
|
|
|
+ const p2 = agentPrompt.buildSystemPrompt({ ...base, skillsText: skillsLib.manifestText(), pointedSkill: skillsLib.get('data-checkup') });
|
|
|
|
|
+ expect(p2.includes('点名技能【数据体检】') && p2.includes('严格按其打法'), '点名技能指令缺失');
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('数字溯源 → 换算形态可溯源,无中生有被标记', async () => {
|
|
|
|
|
+ const prov = require('../server/agent/provenance.js');
|
|
|
|
|
+ const steps = [{
|
|
|
|
|
+ type: 'tool', ok: true,
|
|
|
|
|
+ summary: '本期营业额 10500 元,环比 25%。',
|
|
|
|
|
+ data: { deltas: [{ cur: 10500, deltaPct: 0.25 }], curByDate: [{ date: '20261001', grossAmt: 1500 }] },
|
|
|
|
|
+ }];
|
|
|
|
|
+ const good = prov.checkProvenance('本期营业额 10500 元,环比 +25%;2026-10-01 当天 1,500 元。', steps);
|
|
|
|
|
+ expect(good.total > 0 && good.untraced.length === 0, `应全部溯源:${JSON.stringify(good)}`);
|
|
|
|
|
+ const bad = prov.checkProvenance('本期营业额 99999 元,环比 +25%。', steps);
|
|
|
|
|
+ expect(bad.untraced.length === 1 && bad.untraced[0] === 99999, `编造数字未被标记:${JSON.stringify(bad)}`);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // ---- 安全加固回归(纯函数,不落盘、不打外部 API) ----
|
|
|
|
|
+ const { createContext } = require('../server/context.js');
|
|
|
|
|
+ const { createGenerationService } = require('../server/services/generation.js');
|
|
|
|
|
+ await check('buildImagesAndPrompt → 参考图越界被钳制', async () => {
|
|
|
|
|
+ const gen = createGenerationService(createContext());
|
|
|
|
|
+ const out = gen.buildImagesAndPrompt(os.tmpdir(), '测试提示词', [
|
|
|
|
|
+ '../../config.json', // 相对路径越界
|
|
|
|
|
+ '/etc/passwd', // 绝对路径注入
|
|
|
|
|
+ 'sub/../ok.png', // 归一化后仍在目录内的合法形态(文件不存在也会被读取失败跳过)
|
|
|
|
|
+ ], {});
|
|
|
|
|
+ expect(out.images.length === 0, `越界参考图未被拦截:images=${out.images.length}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('jsonForScript → </script> 不再原样进入脚本体', async () => {
|
|
|
|
|
+ const { jsonForScript } = require('../report-html.js');
|
|
|
|
|
+ const out = jsonForScript(['一食堂</script><img src=x onerror=alert(1)>']);
|
|
|
|
|
+ expect(!out.includes('</script>') && out.includes('\\u003c'), out);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('mergeDefaults → 嵌套默认值不被 config.json 局部对象清掉', async () => {
|
|
|
|
|
+ const { mergeDefaults, CONFIG_DEFAULTS } = require('../server/context.js');
|
|
|
|
|
+ const merged = mergeDefaults(CONFIG_DEFAULTS, { aitable: { enabled: true, appSecret: 'sec' } });
|
|
|
|
|
+ expect(merged.aitable.enabled === true && merged.aitable.appSecret === 'sec', '保存值未生效');
|
|
|
|
|
+ expect(merged.aitable.appKey === CONFIG_DEFAULTS.aitable.appKey && !!merged.aitable.baseId && !!merged.aitable.operatorId,
|
|
|
|
|
+ `组织级默认被清掉:appKey=${merged.aitable.appKey}`);
|
|
|
|
|
+ expect(merged.autoSync.time === CONFIG_DEFAULTS.autoSync.time, 'autoSync 默认被清掉');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
// ---- 静态与路由行为 ----
|
|
// ---- 静态与路由行为 ----
|
|
|
- for (const p of ['/', '/index.html', '/app.js', '/crawl.js', '/gen.js', '/history.js', '/tasks.js', '/reports.js', '/analysis-templates.js', '/lake.js', '/style.css', '/vendor/echarts.min.js']) {
|
|
|
|
|
|
|
+ for (const p of ['/', '/index.html', '/app.js', '/crawl.js', '/gen.js', '/history.js', '/tasks.js', '/reports.js', '/analysis-templates.js', '/lake.js', '/ask.js', '/style.css', '/vendor/echarts.min.js']) {
|
|
|
await check(`静态 ${p}`, async () => {
|
|
await check(`静态 ${p}`, async () => {
|
|
|
const r = await request(port, 'GET', p);
|
|
const r = await request(port, 'GET', p);
|
|
|
expect(r.status === 200, `status=${r.status}`);
|
|
expect(r.status === 200, `status=${r.status}`);
|
|
@@ -387,6 +476,272 @@ async function main() {
|
|
|
expect(r.status === 405, `status=${r.status}`);
|
|
expect(r.status === 405, `status=${r.status}`);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // ---- 问数智能体:主服务上只测只读会话接口(绝不在有主体/真模型时发起运行) ----
|
|
|
|
|
+ await check('GET /api/agent/sessions(无主体 → 400;有主体 → 200)', async () => {
|
|
|
|
|
+ const ent = await request(port, 'GET', '/api/reports/entities');
|
|
|
|
|
+ const r = await request(port, 'GET', '/api/agent/sessions');
|
|
|
|
|
+ if (ent.json().entities.length > 0) {
|
|
|
|
|
+ expect(r.status === 200 && Array.isArray(r.json().sessions), `status=${r.status}`);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ expect(r.status === 400 && r.json().code === 'ASK_NO_ENTITY', `status=${r.status} code=${r.json().code}`);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // ---- 问数智能体端到端(隔离湖 + NAPOS_AGENT_MOCK 确定性假模型,零网络零真实数据) ----
|
|
|
|
|
+ {
|
|
|
|
|
+ const fs = require('fs');
|
|
|
|
|
+ const tmpLake = fs.mkdtempSync(path.join(os.tmpdir(), 'genpic-lake-'));
|
|
|
|
|
+ const tmpAgent = fs.mkdtempSync(path.join(os.tmpdir(), 'genpic-agent-'));
|
|
|
|
|
+ const mkRow = (date, scope, ids, m) => ({
|
|
|
|
|
+ date, scope,
|
|
|
|
|
+ shopId: ids.shopId || '', shopName: ids.shopName || '',
|
|
|
|
|
+ chainId: ids.chainId || '', chainName: ids.chainName || '',
|
|
|
|
|
+ bizHours: null, abnormalCloseHours: null, goodsSaleAmt: null,
|
|
|
|
|
+ syncedAt: new Date().toISOString(),
|
|
|
|
|
+ validOrderCnt: m.orders || 0, incomeAmt: m.income || 0, grossAmt: m.gross || 0,
|
|
|
|
|
+ expenseAmt: m.expense || 0, expUv: m.uv || 0, orderUserCnt: m.buyers || 0,
|
|
|
|
|
+ clkExpRate: m.clk || 0, ordClkRate: 0, perActualPay: 0,
|
|
|
|
|
+ actOrdCnt: 0, actSubsidyAmt: 0, shopRoi: 0, rebuyRate30d: 0,
|
|
|
|
|
+ // 全字段落库模拟:同行均值/排名与环比差值(peer_benchmark / raw_inspect 用)
|
|
|
|
|
+ raw: {
|
|
|
|
|
+ gross_amt: m.gross || 0, valid_order_cnt: m.orders || 0,
|
|
|
|
|
+ gross_amt_mall_avg: m.peerAvg != null ? m.peerAvg : 800,
|
|
|
|
|
+ gross_amt_mall_avg_rank: m.rank != null ? m.rank : 2,
|
|
|
|
|
+ valid_order_cnt_diff: m.orders || 0,
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+ // 健康主体:0924~0930(缺 0927)+ 1001~1007,两店;本期日营业额 1500 / 上期 1400(环比 +25%)
|
|
|
|
|
+ const prevDays = ['20260924', '20260925', '20260926', '20260928', '20260929', '20260930'];
|
|
|
|
|
+ const curDays = ['20261001', '20261002', '20261003', '20261004', '20261005', '20261006', '20261007'];
|
|
|
|
|
+ const healthyRows = [];
|
|
|
|
|
+ for (const d of prevDays) {
|
|
|
|
|
+ healthyRows.push(mkRow(d, 'shop', { shopId: '101', shopName: '一店', chainId: '9', chainName: '第一连锁' }, { gross: 900, income: 720, expense: 180, orders: 90, uv: 1800, buyers: 100, clk: 0.08 }));
|
|
|
|
|
+ healthyRows.push(mkRow(d, 'shop', { shopId: '102', shopName: '二店', chainId: '9', chainName: '第一连锁' }, { gross: 500, income: 400, expense: 100, orders: 40, uv: 900, buyers: 45, clk: 0.08 }));
|
|
|
|
|
+ healthyRows.push(mkRow(d, 'chain', { chainId: '9', chainName: '第一连锁' }, { gross: 1400, income: 1120, expense: 280, orders: 130, uv: 2700, buyers: 145, clk: 0.08 }));
|
|
|
|
|
+ healthyRows.push(mkRow(d, 'company', { chainId: '-1' }, { gross: 1400, income: 1120, expense: 280, orders: 130, uv: 2700, buyers: 145, clk: 0.08 }));
|
|
|
|
|
+ }
|
|
|
|
|
+ for (const d of curDays) {
|
|
|
|
|
+ healthyRows.push(mkRow(d, 'shop', { shopId: '101', shopName: '一店', chainId: '9', chainName: '第一连锁' }, { gross: 1000, income: 800, expense: 200, orders: 100, uv: 2000, buyers: 110, clk: 0.08 }));
|
|
|
|
|
+ healthyRows.push(mkRow(d, 'shop', { shopId: '102', shopName: '二店', chainId: '9', chainName: '第一连锁' }, { gross: 500, income: 400, expense: 100, orders: 40, uv: 900, buyers: 45, clk: 0.08 }));
|
|
|
|
|
+ healthyRows.push(mkRow(d, 'chain', { chainId: '9', chainName: '第一连锁' }, { gross: 1500, income: 1200, expense: 300, orders: 140, uv: 2900, buyers: 155, clk: 0.08 }));
|
|
|
|
|
+ healthyRows.push(mkRow(d, 'company', { chainId: '-1' }, { gross: 1500, income: 1200, expense: 300, orders: 140, uv: 2900, buyers: 155, clk: 0.08 }));
|
|
|
|
|
+ }
|
|
|
|
|
+ // 全零主体:门店行全零而连锁/公司有数(已知 bug 形态,体检必须 fail)
|
|
|
|
|
+ const badRows = [];
|
|
|
|
|
+ for (const d of ['20261001', '20261002', '20261003']) {
|
|
|
|
|
+ badRows.push(mkRow(d, 'shop', { shopId: '201', shopName: '全零店', chainId: '9', chainName: '第一连锁' }, {}));
|
|
|
|
|
+ badRows.push(mkRow(d, 'chain', { chainId: '9', chainName: '第一连锁' }, { gross: 1000, income: 800, expense: 200, orders: 100 }));
|
|
|
|
|
+ badRows.push(mkRow(d, 'company', { chainId: '-1' }, { gross: 1000, income: 800, expense: 200, orders: 100 }));
|
|
|
|
|
+ }
|
|
|
|
|
+ const nowIso = new Date().toISOString();
|
|
|
|
|
+ fs.writeFileSync(path.join(tmpLake, 'entities.json'), JSON.stringify({ entities: [
|
|
|
|
|
+ { key: 'smokeask', firstSeen: nowIso, lastSeen: nowIso, merchantId: 'smokeask', name: '冒烟主体A', chainCount: 1, shopCount: 2,
|
|
|
|
|
+ chains: [{ chainId: '9', chainName: '第一连锁' }],
|
|
|
|
|
+ shops: [
|
|
|
|
|
+ { shopId: '101', shopName: '一店', chainId: '9', chainName: '第一连锁' },
|
|
|
|
|
+ { shopId: '102', shopName: '二店', chainId: '9', chainName: '第一连锁' },
|
|
|
|
|
+ ] },
|
|
|
|
|
+ { key: 'smokebad', firstSeen: nowIso, lastSeen: nowIso, merchantId: 'smokebad', name: '冒烟主体B', chainCount: 1, shopCount: 1,
|
|
|
|
|
+ chains: [{ chainId: '9', chainName: '第一连锁' }],
|
|
|
|
|
+ shops: [{ shopId: '201', shopName: '全零店', chainId: '9', chainName: '第一连锁' }] },
|
|
|
|
|
+ ] }));
|
|
|
|
|
+ fs.mkdirSync(path.join(tmpLake, 'smokeask'), { recursive: true });
|
|
|
|
|
+ fs.writeFileSync(path.join(tmpLake, 'smokeask', 'daily-202609.json'), JSON.stringify({ version: 2, updated: nowIso, rows: healthyRows.filter((r) => r.date < '20261000') }));
|
|
|
|
|
+ fs.writeFileSync(path.join(tmpLake, 'smokeask', 'daily-202610.json'), JSON.stringify({ version: 2, updated: nowIso, rows: healthyRows.filter((r) => r.date >= '20261000') }));
|
|
|
|
|
+ fs.mkdirSync(path.join(tmpLake, 'smokebad'), { recursive: true });
|
|
|
|
|
+ fs.writeFileSync(path.join(tmpLake, 'smokebad', 'daily-202610.json'), JSON.stringify({ version: 2, updated: nowIso, rows: badRows }));
|
|
|
|
|
+
|
|
|
|
|
+ const aport = await pickPort();
|
|
|
|
|
+ const achild = spawn(process.execPath, [path.join(ROOT, 'server.js')], {
|
|
|
|
|
+ env: {
|
|
|
|
|
+ ...process.env, PORT: String(aport), HOST: '127.0.0.1',
|
|
|
|
|
+ GENPIC_LAKE_DIR: tmpLake, GENPIC_AGENT_DIR: tmpAgent, NAPOS_AGENT_MOCK: '1',
|
|
|
|
|
+ },
|
|
|
|
|
+ stdio: ['ignore', 'pipe', 'pipe'],
|
|
|
|
|
+ });
|
|
|
|
|
+ let alog = '';
|
|
|
|
|
+ achild.stdout.on('data', (c) => { alog += c; });
|
|
|
|
|
+ achild.stderr.on('data', (c) => { alog += c; });
|
|
|
|
|
+ try {
|
|
|
|
|
+ await waitForHealth(aport);
|
|
|
|
|
+ const waitRunDone = async (runId, entity, timeoutMs = 10000) => {
|
|
|
|
|
+ const start = Date.now();
|
|
|
|
|
+ for (;;) {
|
|
|
|
|
+ const r = await request(aport, 'GET', `/api/agent/runs/${enc(runId)}?entity=${enc(entity)}`);
|
|
|
|
|
+ const run = r.json().run;
|
|
|
|
|
+ if (run && run.status !== 'running') return run;
|
|
|
|
|
+ if (Date.now() - start > timeoutMs) throw new Error(`运行未在超时内结束 status=${run && run.status}`);
|
|
|
|
|
+ await new Promise((res) => setTimeout(res, 200));
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ const stepByTool = (run, tool) => (run.steps || []).find((s) => s.type === 'tool' && s.tool === tool);
|
|
|
|
|
+ let askSessionId = null;
|
|
|
|
|
+ let askRunId = null;
|
|
|
|
|
+ const fetchAskRun = async () => {
|
|
|
|
|
+ const r = await request(aport, 'GET', `/api/agent/runs/${enc(askRunId)}?entity=${enc('smokeask')}`);
|
|
|
|
|
+ return r.json().run;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ await check('智能体 mock 运行 → 四工具全成 + final + 用量', async () => {
|
|
|
|
|
+ const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '最近营业额趋势如何?', mode: 'agent' });
|
|
|
|
|
+ expect(r.status === 200 && r.json().run && r.json().run.runId, `status=${r.status}`);
|
|
|
|
|
+ const run = await waitRunDone(r.json().run.runId, 'smokeask');
|
|
|
|
|
+ expect(run.status === 'done', `status=${run.status} error=${run.error}`);
|
|
|
|
|
+ const tools = (run.steps || []).filter((s) => s.type === 'tool');
|
|
|
|
|
+ expect(JSON.stringify(tools.map((s) => s.tool)) === JSON.stringify(['lake_overview', 'query_report', 'compare_periods', 'data_quality', 'build_report']),
|
|
|
|
|
+ `工具序列异常:${tools.map((s) => s.tool).join(',')}`);
|
|
|
|
|
+ expect(tools.every((s) => s.ok), '存在失败的工具步骤');
|
|
|
|
|
+ expect(run.usage.llmCalls === 6 && run.usage.toolCalls === 5, `用量异常 llm=${run.usage.llmCalls} tool=${run.usage.toolCalls}`);
|
|
|
|
|
+ expect(run.result && run.result.answer.includes('mock'), JSON.stringify(run.result));
|
|
|
|
|
+ expect(run.result.chart && run.result.chart.type === 'line', 'final 缺图表规格');
|
|
|
|
|
+ expect(run.result.reportUrl && run.result.reportUrl.includes('/api/reports/report-html'), `缺报告链接:${run.result.reportUrl}`);
|
|
|
|
|
+ askSessionId = run.sessionId;
|
|
|
|
|
+ askRunId = run.runId;
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 mock → compare_periods 环比数字正确(门店对齐 + +25%)', async () => {
|
|
|
|
|
+ const run = await fetchAskRun();
|
|
|
|
|
+ const cmp = stepByTool(run, 'compare_periods');
|
|
|
|
|
+ expect(cmp && cmp.ok && cmp.data, 'compare_periods 步骤缺失');
|
|
|
|
|
+ const gross = cmp.data.deltas.find((d) => d.key === 'grossAmt');
|
|
|
|
|
+ expect(gross && gross.cur === 10500 && gross.prev === 8400, `环比数字异常 ${JSON.stringify(gross)}`);
|
|
|
|
|
+ expect(Math.abs(gross.deltaPct - 0.25) < 1e-9, `变化率异常 ${gross.deltaPct}`);
|
|
|
|
|
+ expect(cmp.data.comparableShopCount === 2, `可比门店数异常 ${cmp.data.comparableShopCount}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 mock → query_report 按日聚合 13 天', async () => {
|
|
|
|
|
+ const run = await fetchAskRun();
|
|
|
|
|
+ const q = stepByTool(run, 'query_report');
|
|
|
|
|
+ expect(q && q.ok && q.data && q.data.rows.length === 13, `按日行数异常 ${q && q.data && q.data.rows.length}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 mock → data_quality 健康主体无 fail 且报缺 1 天', async () => {
|
|
|
|
|
+ const run = await fetchAskRun();
|
|
|
|
|
+ const dq = stepByTool(run, 'data_quality');
|
|
|
|
|
+ expect(dq && dq.ok, 'data_quality 步骤缺失');
|
|
|
|
|
+ expect(dq.summary.includes('缺 1/14'), `缺日未识别:${dq.summary}`);
|
|
|
|
|
+ expect(!dq.summary.includes('[fail]'), `健康主体误报 fail:${dq.summary}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 mock → 全零主体体检必须 fail', async () => {
|
|
|
|
|
+ const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokebad', question: '体检一下', mode: 'agent' });
|
|
|
|
|
+ expect(r.status === 200, `status=${r.status}`);
|
|
|
|
|
+ const run = await waitRunDone(r.json().run.runId, 'smokebad');
|
|
|
|
|
+ expect(run.status === 'done', `status=${run.status} error=${run.error}`);
|
|
|
|
|
+ const dq = stepByTool(run, 'data_quality');
|
|
|
|
|
+ expect(dq && dq.summary.includes('[fail]') && dq.summary.includes('全零'), dq.summary);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 mock 快问 → 直接终答(同一会话追问)', async () => {
|
|
|
|
|
+ const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '昨天营业额', mode: 'quick', sessionId: askSessionId });
|
|
|
|
|
+ expect(r.status === 200, `status=${r.status}`);
|
|
|
|
|
+ const run = await waitRunDone(r.json().run.runId, 'smokeask');
|
|
|
|
|
+ expect(run.status === 'done' && run.mode === 'quick' && run.result.answer.includes('快问'), JSON.stringify(run.result));
|
|
|
|
|
+ expect(run.sessionId === askSessionId, '快问未落入同一会话');
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 会话索引 → 标题=首问、运行入册、带最近一问', async () => {
|
|
|
|
|
+ const s = await request(aport, 'GET', `/api/agent/sessions?entity=${enc('smokeask')}`);
|
|
|
|
|
+ const sessions = s.json().sessions;
|
|
|
|
|
+ expect(sessions.length === 1 && sessions[0].title === '最近营业额趋势如何?' && sessions[0].runCount === 2,
|
|
|
|
|
+ JSON.stringify(sessions));
|
|
|
|
|
+ expect(sessions[0].lastQuestion === '昨天营业额', `lastQuestion 异常:${sessions[0].lastQuestion}`);
|
|
|
|
|
+ const runs = await request(aport, 'GET', `/api/agent/runs?entity=${enc('smokeask')}&sessionId=${enc(sessions[0].sessionId)}`);
|
|
|
|
|
+ expect(runs.json().runs.length === 2, '会话运行列表数量异常');
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 删除会话 → 索引与运行文件清零、再删 404', async () => {
|
|
|
|
|
+ const s = await request(aport, 'GET', `/api/agent/sessions?entity=${enc('smokeask')}`);
|
|
|
|
|
+ const sid = s.json().sessions[0].sessionId;
|
|
|
|
|
+ const del = await request(aport, 'DELETE', `/api/agent/sessions/${enc(sid)}?entity=${enc('smokeask')}`);
|
|
|
|
|
+ expect(del.status === 200 && del.json().ok === true, `status=${del.status}`);
|
|
|
|
|
+ const after = await request(aport, 'GET', `/api/agent/sessions?entity=${enc('smokeask')}`);
|
|
|
|
|
+ expect(after.json().sessions.length === 0, '删除后索引未清空');
|
|
|
|
|
+ const del2 = await request(aport, 'DELETE', `/api/agent/sessions/${enc(sid)}?entity=${enc('smokeask')}`);
|
|
|
|
|
+ expect(del2.status === 404, `重复删除应 404,实际 ${del2.status}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('GET /api/agent/skills → 四个内置技能', async () => {
|
|
|
|
|
+ const r = await request(aport, 'GET', '/api/agent/skills');
|
|
|
|
|
+ const skills = r.json().skills || [];
|
|
|
|
|
+ expect(r.status === 200 && skills.length === 4 && skills.every((s) => s.key && s.name), JSON.stringify(skills));
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 技能点名 → run.skill 落档且运行完成', async () => {
|
|
|
|
|
+ const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '对比本期与上期', mode: 'agent', skill: 'period-compare' });
|
|
|
|
|
+ expect(r.status === 200, `status=${r.status}`);
|
|
|
|
|
+ const run = await waitRunDone(r.json().run.runId, 'smokeask');
|
|
|
|
|
+ expect(run.status === 'done' && run.skill === 'period-compare', `skill=${run.skill} status=${run.status} error=${run.error}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 verdicts 归档 → 校验工具结论入运行记录', async () => {
|
|
|
|
|
+ const run = await fetchAskRun();
|
|
|
|
|
+ const vs = run.verdicts || [];
|
|
|
|
|
+ expect(vs.length >= 5, `verdicts 过少:${vs.length}`);
|
|
|
|
|
+ expect(vs.some((v) => v.kind === 'identity') && vs.some((v) => v.kind === 'quality'), `缺 identity/quality verdict:${JSON.stringify(vs.map((v) => v.kind))}`);
|
|
|
|
|
+ expect(vs.every((v) => v.id && ['ok', 'warn', 'fail'].includes(v.status)), `verdict 形状异常:${JSON.stringify(vs[0])}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 自动分流 → 短问走快问、分析问走深度', async () => {
|
|
|
|
|
+ const r1 = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '昨天营业额多少', mode: 'auto' });
|
|
|
|
|
+ const run1 = await waitRunDone(r1.json().run.runId, 'smokeask');
|
|
|
|
|
+ expect(run1.status === 'done' && run1.mode === 'quick' && run1.autoPicked === true, `短问未分流快问:mode=${run1.mode}`);
|
|
|
|
|
+ const r2 = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '最近30天比上一个30天营业额变化多少,主要受哪些门店影响', mode: 'auto' });
|
|
|
|
|
+ const run2 = await waitRunDone(r2.json().run.runId, 'smokeask');
|
|
|
|
|
+ expect(run2.status === 'done' && run2.mode === 'agent' && run2.autoPicked === true, `分析问未分流深度:mode=${run2.mode}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 工具直测(隔离子进程)→ cross_check/peer_benchmark/raw_inspect', async () => {
|
|
|
|
|
+ const toolProbe = [
|
|
|
|
|
+ `const ROOT = ${JSON.stringify(ROOT)};`,
|
|
|
|
|
+ 'const svc = require(ROOT + "/napos-service.js");',
|
|
|
|
|
+ 'const nlq = require(ROOT + "/nl-query.js");',
|
|
|
|
|
+ 'const cross = require(ROOT + "/server/agent/tools/cross.js");',
|
|
|
|
|
+ 'const peer = require(ROOT + "/server/agent/tools/peer.js");',
|
|
|
|
|
+ 'function ctxOf(entity) { const meta = svc.store.getEntity(entity); return { svc, entityKey: entity, meta, shops: meta.shops || [], dateRange: nlq.lakeDateRange(entity, svc) }; }',
|
|
|
|
|
+ 'const out = {};',
|
|
|
|
|
+ 'const healthy = ctxOf("smokeask");',
|
|
|
|
|
+ 'const c1 = cross.crossHandler(healthy, cross.crossSanitize({}, healthy));',
|
|
|
|
|
+ 'out.healthy = { checked: c1.data.checked, mismatch: c1.data.mismatchCount, severe: c1.data.severe };',
|
|
|
|
|
+ 'const bad = ctxOf("smokebad");',
|
|
|
|
|
+ 'const c2 = cross.crossHandler(bad, cross.crossSanitize({}, bad));',
|
|
|
|
|
+ 'out.bad = { mismatch: c2.data.mismatchCount, severe: c2.data.severe };',
|
|
|
|
|
+ 'const b1 = peer.benchHandler(healthy, peer.benchSanitize({ field: "gross_amt" }, healthy));',
|
|
|
|
|
+ 'const bs = b1.data.shops || [];',
|
|
|
|
|
+ 'out.bench = { shops: bs.length, firstPeer: bs[0] && bs[0].peerAvg, firstRank: bs[0] && bs[0].rankAvg };',
|
|
|
|
|
+ 'const r1 = peer.rawHandler(healthy, peer.rawSanitize({ date: "20261001", shops: ["一店"] }, healthy));',
|
|
|
|
|
+ 'out.raw = { found: r1.data.found, matched: (r1.data.matched || []).length, hasRaw: r1.data.hasRaw };',
|
|
|
|
|
+ 'const entityTool = require(ROOT + "/server/agent/tools/entity.js");',
|
|
|
|
|
+ 'const e1 = entityTool.entityHandler(healthy, entityTool.entitySanitize({}, healthy));',
|
|
|
|
|
+ 'out.entity = { entities: (e1.data.entities || []).length, first: (e1.data.entities || [])[0] && (e1.data.entities || [])[0].name, skipped: (e1.data.skipped || []).length };',
|
|
|
|
|
+ 'const cloudTool = require(ROOT + "/server/agent/tools/cloud.js");',
|
|
|
|
|
+ 'cloudTool.cloudHandler(healthy, cloudTool.cloudSanitize({}, healthy)).then((c3) => {',
|
|
|
|
|
+ ' out.cloud = { available: c3.data.available, hasVerdict: Array.isArray(c3.data.verdicts) && c3.data.verdicts.length === 1 };',
|
|
|
|
|
+ ' console.log(JSON.stringify(out));',
|
|
|
|
|
+ '});',
|
|
|
|
|
+ ].join('\n');
|
|
|
|
|
+ const probe = spawnSync(process.execPath, ['-e', toolProbe], {
|
|
|
|
|
+ encoding: 'utf8', timeout: 20000,
|
|
|
|
|
+ env: { ...process.env, GENPIC_LAKE_DIR: tmpLake },
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(probe.status === 0, `探针失败:${(probe.stderr || probe.stdout || '').slice(-300)}`);
|
|
|
|
|
+ const out = JSON.parse(probe.stdout.trim().split('\n').pop());
|
|
|
|
|
+ expect(out.healthy.checked > 0 && out.healthy.mismatch === 0 && out.healthy.severe === false,
|
|
|
|
|
+ `健康主体层间核对异常:${JSON.stringify(out.healthy)}`);
|
|
|
|
|
+ expect(out.bad.severe === true && out.bad.mismatch > 0, `全零主体未检出层间不一致:${JSON.stringify(out.bad)}`);
|
|
|
|
|
+ expect(out.bench.shops === 2 && out.bench.firstPeer === 800 && out.bench.firstRank === 2,
|
|
|
|
|
+ `同行对标异常:${JSON.stringify(out.bench)}`);
|
|
|
|
|
+ expect(out.raw.found === true && out.raw.hasRaw === true && out.raw.matched >= 3,
|
|
|
|
|
+ `raw 检视异常:${JSON.stringify(out.raw)}`);
|
|
|
|
|
+ expect(out.entity.entities === 2 && out.entity.first === '冒烟主体A' && out.entity.skipped === 0,
|
|
|
|
|
+ `跨主体对比异常:${JSON.stringify(out.entity)}`);
|
|
|
|
|
+ expect(out.cloud.available === false && out.cloud.hasVerdict === true,
|
|
|
|
|
+ `云端未启用未优雅降级:${JSON.stringify(out.cloud)}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 空问题 → 400 ASK_EMPTY', async () => {
|
|
|
|
|
+ const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: ' ' });
|
|
|
|
|
+ expect(r.status === 400 && r.json().code === 'ASK_EMPTY', `status=${r.status} code=${r.json().code}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ await check('智能体 取消不存在的运行 → 404', async () => {
|
|
|
|
|
+ const r = await request(aport, 'POST', '/api/agent/runs/no-such-run/cancel', {});
|
|
|
|
|
+ expect(r.status === 404, `status=${r.status}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ results.push({ name: '(智能体隔离测试执行中断)', ok: false, error: e.message + (alog ? ` | 服务日志:${alog.slice(-300)}` : '') });
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try { achild.kill('SIGKILL'); } catch { /* ignore */ }
|
|
|
|
|
+ try { fs.rmSync(tmpLake, { recursive: true, force: true }); fs.rmSync(tmpAgent, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// ---- 优雅停机(SIGTERM 后应自行退出) ----
|
|
// ---- 优雅停机(SIGTERM 后应自行退出) ----
|
|
|
await check('SIGTERM 优雅停机', async () => {
|
|
await check('SIGTERM 优雅停机', async () => {
|
|
|
process.kill(child.pid, 'SIGTERM');
|
|
process.kill(child.pid, 'SIGTERM');
|