smoke.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. #!/usr/bin/env node
  2. 'use strict';
  3. // 零依赖冒烟测试:拉起真实服务(随机端口),全量核对端点行为后关停。
  4. // 安全边界:只做只读请求 + 无副作用 POST(校验失败类、空操作);
  5. // 绝不触碰 DELETE /api/reports/data、/api/crawl/start、/api/open-folder、
  6. // /api/image/test、/api/chat/test,也不在数据湖有主体时调用 /api/reports/ask。
  7. // 用法:node scripts/smoke.js
  8. const { spawn, spawnSync } = require('child_process');
  9. const net = require('net');
  10. const http = require('http');
  11. const os = require('os');
  12. const path = require('path');
  13. const ROOT = path.resolve(__dirname, '..');
  14. function pickPort() {
  15. return new Promise((resolve, reject) => {
  16. const srv = net.createServer();
  17. srv.unref();
  18. srv.on('error', reject);
  19. srv.listen(0, '127.0.0.1', () => {
  20. const port = srv.address().port;
  21. srv.close(() => resolve(port));
  22. });
  23. });
  24. }
  25. function request(port, method, urlPath, body, host = '127.0.0.1', extraHeaders = {}) {
  26. return new Promise((resolve, reject) => {
  27. const req = http.request({
  28. host, port, method, path: urlPath, timeout: 15000,
  29. headers: {
  30. ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}),
  31. ...extraHeaders,
  32. },
  33. }, (res) => {
  34. const chunks = [];
  35. res.on('data', (c) => chunks.push(c));
  36. res.on('end', () => resolve({
  37. status: res.statusCode,
  38. headers: res.headers,
  39. text: Buffer.concat(chunks).toString('utf8'),
  40. json: () => JSON.parse(Buffer.concat(chunks).toString('utf8')),
  41. }));
  42. });
  43. req.on('error', reject);
  44. req.on('timeout', () => req.destroy(new Error('timeout')));
  45. if (body !== undefined) req.write(JSON.stringify(body));
  46. req.end();
  47. });
  48. }
  49. /** 取本机第一个非内部 IPv4 局域网地址(用于访问口令测试;回环地址在服务端直接放行) */
  50. function getLanIpv4() {
  51. for (const list of Object.values(os.networkInterfaces())) {
  52. for (const ni of list || []) {
  53. if (ni.family === 'IPv4' && !ni.internal) return ni.address;
  54. }
  55. }
  56. return '';
  57. }
  58. const results = [];
  59. async function check(name, fn) {
  60. try {
  61. await fn();
  62. results.push({ name, ok: true });
  63. } catch (e) {
  64. results.push({ name, ok: false, error: e.message });
  65. }
  66. }
  67. function expect(cond, msg) {
  68. if (!cond) throw new Error(msg);
  69. }
  70. async function waitForHealth(port, timeoutMs = 20000) {
  71. const start = Date.now();
  72. for (;;) {
  73. try {
  74. const res = await request(port, 'GET', '/healthz');
  75. if (res.status === 200) return;
  76. } catch { /* not up yet */ }
  77. if (Date.now() - start > timeoutMs) throw new Error('服务未在超时内启动');
  78. await new Promise((r) => setTimeout(r, 300));
  79. }
  80. }
  81. function waitForHealthLan(lanIp, port, timeoutMs = 20000) {
  82. return new Promise((resolve, reject) => {
  83. const start = Date.now();
  84. const probe = () => {
  85. const req = http.request({ host: lanIp, port, path: '/healthz', timeout: 3000 }, (res) => {
  86. res.resume();
  87. if (res.statusCode === 200) return resolve();
  88. retry();
  89. });
  90. req.on('error', retry);
  91. req.on('timeout', () => { req.destroy(); retry(); });
  92. req.end();
  93. };
  94. const retry = () => {
  95. if (Date.now() - start > timeoutMs) return reject(new Error('服务未在超时内启动(LAN)'));
  96. setTimeout(probe, 300);
  97. };
  98. probe();
  99. });
  100. }
  101. function enc(s) { return encodeURIComponent(s); }
  102. async function main() {
  103. const port = await pickPort();
  104. const child = spawn(process.execPath, [path.join(ROOT, 'server.js')], {
  105. env: { ...process.env, PORT: String(port), HOST: '127.0.0.1' },
  106. stdio: ['ignore', 'pipe', 'pipe'],
  107. });
  108. let bootLog = '';
  109. child.stdout.on('data', (c) => { bootLog += c; });
  110. child.stderr.on('data', (c) => { bootLog += c; });
  111. try {
  112. await waitForHealth(port);
  113. let workspaceRoot = '';
  114. {
  115. const res = await request(port, 'GET', '/api/default-root');
  116. workspaceRoot = res.json().root;
  117. }
  118. const encRoot = enc(workspaceRoot);
  119. const b64Root = Buffer.from(workspaceRoot, 'utf8').toString('base64url');
  120. // ---- 基础与健康 ----
  121. await check('GET /healthz', async () => {
  122. const r = await request(port, 'GET', '/healthz');
  123. expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
  124. });
  125. await check('GET /readyz', async () => {
  126. const r = await request(port, 'GET', '/readyz');
  127. const j = r.json();
  128. expect(r.status === 200 && typeof j.ok === 'boolean' && typeof j.workspaceReady === 'boolean', `status=${r.status}`);
  129. });
  130. await check('GET /api/default-root', async () => {
  131. const r = await request(port, 'GET', '/api/default-root');
  132. expect(r.status === 200 && !!r.json().root, `status=${r.status}`);
  133. });
  134. // ---- 设置域 ----
  135. await check('GET /api/models', async () => {
  136. const r = await request(port, 'GET', '/api/models');
  137. const j = r.json();
  138. expect(r.status === 200 && Array.isArray(j.models) && j.models.length >= 5 && !!j.default, `status=${r.status}`);
  139. });
  140. await check('GET /api/settings(脱敏)', async () => {
  141. const r = await request(port, 'GET', '/api/settings');
  142. const j = r.json();
  143. expect(r.status === 200 && typeof j.rootDir === 'string' && typeof j.port === 'number', `status=${r.status}`);
  144. expect(!j.apiKey || j.apiKey.includes('••••'), 'apiKey 未脱敏');
  145. });
  146. await check('POST /api/settings 非法 pageSize → 400', async () => {
  147. const r = await request(port, 'POST', '/api/settings', { pageSize: 9999 });
  148. expect(r.status === 400, `status=${r.status}`);
  149. });
  150. await check('POST /api/settings 非法 delayMs → 400', async () => {
  151. const r = await request(port, 'POST', '/api/settings', { delayMs: -5 });
  152. expect(r.status === 400, `status=${r.status}`);
  153. });
  154. await check('POST /api/settings 非法 autoSyncTime → 400', async () => {
  155. const r = await request(port, 'POST', '/api/settings', { autoSyncTime: '99:99' });
  156. expect(r.status === 400, `status=${r.status}`);
  157. });
  158. await check('GET /api/settings 含 autoSync 字段', async () => {
  159. const r = await request(port, 'GET', '/api/settings');
  160. const j = r.json();
  161. expect(typeof j.autoSyncEnabled === 'boolean' && /^\d{1,2}:\d{2}$/.test(j.autoSyncTime || ''), 'autoSync 字段缺失');
  162. });
  163. // ---- 采集域 ----
  164. await check('GET /api/session', async () => {
  165. const r = await request(port, 'GET', '/api/session');
  166. expect(r.status === 200 && typeof r.json().cookieConfigured === 'boolean', `status=${r.status}`);
  167. });
  168. await check('GET /api/shops(base64url root)', async () => {
  169. const r = await request(port, 'GET', `/api/shops?root=${b64Root}`);
  170. expect(r.status === 200 && Array.isArray(r.json().shops), `status=${r.status}`);
  171. });
  172. await check('GET /api/shops 非法 root → 403', async () => {
  173. const bad = Buffer.from('/tmp/elsewhere', 'utf8').toString('base64url');
  174. const r = await request(port, 'GET', `/api/shops?root=${bad}`);
  175. expect(r.status === 403, `status=${r.status}`);
  176. });
  177. await check('GET /api/scan', async () => {
  178. const r = await request(port, 'GET', `/api/scan?root=${encRoot}`);
  179. const j = r.json();
  180. expect(r.status === 200 && Array.isArray(j.shops), `status=${r.status}`);
  181. });
  182. await check('GET /api/crawl/status', async () => {
  183. const r = await request(port, 'GET', '/api/crawl/status');
  184. expect(r.status === 200 && typeof r.json().status === 'string', `status=${r.status}`);
  185. });
  186. await check('POST /api/crawl/stop(空操作)', async () => {
  187. const r = await request(port, 'POST', '/api/crawl/stop');
  188. expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
  189. });
  190. await check('Origin 防护:跨站写请求 → 403', async () => {
  191. const r = await request(port, 'POST', '/api/crawl/stop', undefined, '127.0.0.1', { Origin: 'http://evil.example' });
  192. expect(r.status === 403, `status=${r.status}`);
  193. });
  194. await check('Origin 防护:同源写请求放行', async () => {
  195. const r = await request(port, 'POST', '/api/crawl/stop', undefined, '127.0.0.1', { Origin: `http://127.0.0.1:${port}` });
  196. expect(r.status === 200, `status=${r.status}`);
  197. });
  198. // ---- 生成/任务/历史域 ----
  199. await check('GET /api/tasks', async () => {
  200. const r = await request(port, 'GET', '/api/tasks');
  201. const j = r.json();
  202. expect(r.status === 200 && j.summary && Array.isArray(j.tasks), `status=${r.status}`);
  203. });
  204. await check('GET /api/job/未知ID → 404', async () => {
  205. const r = await request(port, 'GET', '/api/job/00000000-0000-0000-0000-000000000000');
  206. expect(r.status === 404, `status=${r.status}`);
  207. });
  208. await check('GET /api/creations', async () => {
  209. const r = await request(port, 'GET', `/api/creations?root=${encRoot}&limit=5`);
  210. const j = r.json();
  211. expect(r.status === 200 && typeof j.total === 'number' && Array.isArray(j.items), `status=${r.status}`);
  212. });
  213. await check('GET /files 越界 → 403', async () => {
  214. const r = await request(port, 'GET', `/files?path=${enc('/etc/passwd')}`);
  215. expect(r.status === 403, `status=${r.status}`);
  216. });
  217. await check('GET /files 缺参 → 400', async () => {
  218. const r = await request(port, 'GET', '/files');
  219. expect(r.status === 400, `status=${r.status}`);
  220. });
  221. // ---- 日志 ----
  222. await check('GET /api/logs', async () => {
  223. const r = await request(port, 'GET', '/api/logs');
  224. expect(r.status === 200 && Array.isArray(r.json().logs), `status=${r.status}`);
  225. });
  226. await check('POST /api/logs/clear', async () => {
  227. const r = await request(port, 'POST', '/api/logs/clear');
  228. expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
  229. });
  230. // ---- 数据报表域 ----
  231. await check('GET /api/reports/status(不限方法 GET)', async () => {
  232. const r = await request(port, 'GET', '/api/reports/status');
  233. expect(r.status === 200 && r.json().napos, `status=${r.status}`);
  234. });
  235. await check('POST /api/reports/status(不限方法 POST)', async () => {
  236. const r = await request(port, 'POST', '/api/reports/status');
  237. expect(r.status === 200, `status=${r.status}`);
  238. });
  239. await check('GET /api/reports/columns', async () => {
  240. const r = await request(port, 'GET', '/api/reports/columns');
  241. expect(r.status === 200 && Array.isArray(r.json().columns) && r.json().columns.length >= 10, `status=${r.status}`);
  242. });
  243. await check('GET /api/reports/entities', async () => {
  244. const r = await request(port, 'GET', '/api/reports/entities');
  245. expect(r.status === 200 && Array.isArray(r.json().entities), `status=${r.status}`);
  246. });
  247. await check('GET /api/reports/sync/status', async () => {
  248. const r = await request(port, 'GET', '/api/reports/sync/status');
  249. expect(r.status === 200 && typeof r.json().running === 'boolean', `status=${r.status}`);
  250. });
  251. await check('GET /api/reports/syncs', async () => {
  252. const r = await request(port, 'GET', '/api/reports/syncs');
  253. expect(r.status === 200 && Array.isArray(r.json().runs), `status=${r.status}`);
  254. });
  255. await check('GET /api/reports/daily', async () => {
  256. const r = await request(port, 'GET', '/api/reports/daily');
  257. expect(r.status === 200, `status=${r.status}`);
  258. });
  259. await check('GET /api/reports/aggregate', async () => {
  260. const r = await request(port, 'GET', '/api/reports/aggregate');
  261. expect(r.status === 200, `status=${r.status}`);
  262. });
  263. await check('GET /api/reports/export → CSV', async () => {
  264. const r = await request(port, 'GET', '/api/reports/export');
  265. expect(r.status === 200 && /text\/csv/.test(r.headers['content-type'] || ''), `status=${r.status}`);
  266. });
  267. await check('GET /api/reports/report-html → HTML', async () => {
  268. const r = await request(port, 'GET', '/api/reports/report-html');
  269. expect(r.status === 200 && /text\/html/.test(r.headers['content-type'] || ''), `status=${r.status}`);
  270. });
  271. await check('POST /api/reports/ask(有主体时跳过)', async () => {
  272. const ent = await request(port, 'GET', '/api/reports/entities');
  273. if (ent.json().entities.length > 0) return; // 有主体会真调对话模型,跳过
  274. const r = await request(port, 'POST', '/api/reports/ask', { q: 'test' });
  275. expect(r.status === 400 && r.json().code === 'ASK_NO_ENTITY', `status=${r.status}`);
  276. });
  277. await check('GET /api/reports/lake → 湖全景', async () => {
  278. const r = await request(port, 'GET', '/api/reports/lake');
  279. const d = r.json();
  280. expect(r.status === 200 && Array.isArray(d.entities) && typeof d.totalRows === 'number' && d.aitable && 'enabled' in d.aitable, `status=${r.status}`);
  281. });
  282. await check('GET /api/reports/daily → 分页字段', async () => {
  283. const r = await request(port, 'GET', '/api/reports/daily');
  284. const d = r.json();
  285. expect(r.status === 200 && typeof d.total === 'number' && d.page >= 1 && d.pageSize >= 50 && d.pages >= 1 && 'summary' in d, `status=${r.status}`);
  286. });
  287. await check('GET /api/reports/syncs → 同步历史', async () => {
  288. const r = await request(port, 'GET', '/api/reports/syncs');
  289. expect(r.status === 200 && Array.isArray(r.json().runs), `status=${r.status}`);
  290. });
  291. await check('POST /api/reports/aitable/push(无主体 → 400)', async () => {
  292. const r = await request(port, 'POST', '/api/reports/aitable/push', {});
  293. expect(r.status === 400 && r.json().code === 'AITABLE_NO_ENTITY', `status=${r.status} code=${r.json().code}`);
  294. });
  295. // ---- 钉钉三表拆分与行键(纯函数回归,不打真实钉钉 API) ----
  296. const aitableLib = require('../server/services/aitable.js');
  297. const { rowKey } = require('../reports-store.js');
  298. await check('splitRowsBySheet → 按维度分流且 summary 不上云', async () => {
  299. const s = aitableLib.splitRowsBySheet([
  300. { scope: 'shop', shopId: '1', chainId: 'c1' },
  301. { scope: 'shop', shopId: '2', chainId: 'c1' },
  302. { scope: 'chain', shopId: '', chainId: 'c1' },
  303. { scope: 'chain', shopId: '', chainId: 'c2' },
  304. { scope: 'company', shopId: '', chainId: '-1' },
  305. { scope: 'summary', shopId: '', chainId: '' },
  306. ]);
  307. expect(s.detail.length === 2 && s.chain.length === 2 && s.company.length === 1 && s.skippedSummary === 1,
  308. `detail=${s.detail.length} chain=${s.chain.length} company=${s.company.length} skipped=${s.skippedSummary}`);
  309. });
  310. await check('recordKey / rowKey → 同日多连锁不碰撞', async () => {
  311. const cloud = (chainId) => aitableLib.recordKey({ 日期: '20261017', 账号名: 'A', 维度: '连锁', 连锁ID: chainId, 门店ID: '' });
  312. expect(cloud('c1') !== cloud('c2'), '连锁行云端业务键仍碰撞');
  313. const local = (row) => rowKey(row);
  314. expect(local({ date: '20261017', scope: 'chain', chainId: 'c1', shopId: '' }) !== local({ date: '20261017', scope: 'chain', chainId: 'c2', shopId: '' }), '连锁行本地行键仍碰撞');
  315. expect(local({ date: '20261017', scope: 'summary', chainId: '', shopId: '' }) !== local({ date: '20261017', scope: 'summary', chainId: 'c1', shopId: '' }), '汇总行本地行键仍碰撞');
  316. });
  317. await check('resolveSheets → 缺省兜底三表名', async () => {
  318. const { sheets, fingerprint } = aitableLib.resolveSheets({});
  319. expect(sheets.detail === '门店明细' && sheets.chain === '连锁汇总' && sheets.company === '公司汇总' && fingerprint.split('|').length === 3,
  320. JSON.stringify(sheets));
  321. });
  322. // ---- 问数两段式:结果摘要与兜底话术(纯函数,不调对话模型) ----
  323. const nlQueryLib = require('../nl-query.js');
  324. await check('buildResultDigest → 口径/Top行/合计齐全且按列类型格式化', async () => {
  325. const digest = nlQueryLib.buildResultDigest(
  326. { mode: 'shop', from: '20260918', to: '20261017', metrics: ['grossAmt', 'clkExpRate'], shopIds: [] },
  327. {
  328. rows: [
  329. { shopName: '一食堂', grossAmt: 12345.6, clkExpRate: 0.0521 },
  330. { shopName: '二食堂', grossAmt: 0, clkExpRate: 0 },
  331. ],
  332. summary: { grossAmt: 12345.6, clkExpRate: 0.0521 },
  333. }
  334. );
  335. expect(digest.includes('按门店汇总') && digest.includes('2026-09-18') && digest.includes('2026-10-17'), digest);
  336. expect(digest.includes('12,345.60元') && digest.includes('5.21%'), digest); // money 两位小数 / pct 小数×100
  337. expect(digest.includes('区间合计') && digest.includes('均值'), digest); // 金额=合计、比率=均值
  338. });
  339. await check('buildResultDigest → 空结果有明确说明', async () => {
  340. const digest = nlQueryLib.buildResultDigest(
  341. { mode: 'date', from: '20260918', to: '20261017', metrics: ['grossAmt'], shopIds: [] },
  342. { rows: [], summary: {} }
  343. );
  344. expect(digest.includes('结果为空'), digest);
  345. });
  346. await check('fallbackReply → 模板句含区间与指标名', async () => {
  347. const t = nlQueryLib.fallbackReply({ mode: 'date', from: '20260918', to: '20261017', metrics: ['grossAmt'] });
  348. expect(t.includes('2026-09-18') && t.includes('营业额'), t);
  349. });
  350. // ---- 问数智能体:动作解析/图表钳制/循环侦测(纯函数,不调对话模型) ----
  351. const agentLlm = require('../server/agent/llm.js');
  352. const agentGuard = require('../server/agent/guard.js');
  353. const naposServiceLib = require('../napos-service.js');
  354. await check('parseAgentAction → 工具动作/终答/围栏解析', async () => {
  355. const a = agentLlm.parseAgentAction('{"thought":"查","action":{"tool":"query_report","args":{"mode":"shop"}}}');
  356. expect(a.type === 'tool' && a.tool === 'query_report' && a.args.mode === 'shop', JSON.stringify(a));
  357. const f = agentLlm.parseAgentAction('前言```json\n{"thought":"完","final":{"answer":"结论"}}\n```后记');
  358. expect(f.type === 'final' && f.final.answer === '结论', JSON.stringify(f));
  359. });
  360. await check('parseAgentAction → 非法输出全部给出可读拒绝', async () => {
  361. const bads = ['不是json', '{"thought":"只有thought"}', '{"final":{"answer":" "}}', '{"final":"字符串"}'];
  362. for (const bad of bads) {
  363. let rejected = false;
  364. try { agentLlm.parseAgentAction(bad); } catch { rejected = true; }
  365. expect(rejected, `未拒绝非法输出:${bad}`);
  366. }
  367. });
  368. await check('clampChart → 指标白名单与非法类型兜底', async () => {
  369. expect(agentGuard.clampChart({ type: 'pie', x: 'date', y: ['grossAmt'] }, naposServiceLib) === null, 'pie 应被拒绝');
  370. const c = agentGuard.clampChart({ type: 'line', x: 'date', y: ['grossAmt', 'hacked', 'validOrderCnt'] }, naposServiceLib);
  371. expect(c && c.type === 'line' && c.x === 'date' && c.y.length === 2 && !c.y.includes('hacked'), JSON.stringify(c));
  372. });
  373. await check('LoopDetector → 重复先提醒、三犯强制收敛、换参重置', async () => {
  374. const d = new agentGuard.LoopDetector();
  375. expect(d.record('t', { a: 1 }) === 'ok', '首次应为 ok');
  376. expect(d.record('t', { a: 1 }) === 'repeat', '第二次应为 repeat');
  377. expect(d.record('t', { a: 1 }) === 'loop', '第三次应为 loop');
  378. expect(d.record('t', { a: 2 }) === 'ok', '换参数应重置');
  379. });
  380. await check('技能 → 清单注入 prompt,点名技能有专属指令', async () => {
  381. const skillsLib = require('../server/agent/skills.js');
  382. const agentPrompt = require('../server/agent/prompt.js');
  383. const base = {
  384. svc: naposServiceLib, entityName: '测试主体', shops: [], dateRange: { from: '20260101', to: '20260201' },
  385. today: '20260201', toolsText: '- t:测试工具',
  386. };
  387. const p1 = agentPrompt.buildSystemPrompt({ ...base, skillsText: skillsLib.manifestText() });
  388. expect(p1.includes('内置技能') && p1.includes('周期对比分析') && p1.includes('数据体检'), '技能清单未注入');
  389. const p2 = agentPrompt.buildSystemPrompt({ ...base, skillsText: skillsLib.manifestText(), pointedSkill: skillsLib.get('data-checkup') });
  390. expect(p2.includes('点名技能【数据体检】') && p2.includes('严格按其打法'), '点名技能指令缺失');
  391. });
  392. await check('数字溯源 → 换算形态可溯源,无中生有被标记', async () => {
  393. const prov = require('../server/agent/provenance.js');
  394. const steps = [{
  395. type: 'tool', ok: true,
  396. summary: '本期营业额 10500 元,环比 25%。',
  397. data: { deltas: [{ cur: 10500, deltaPct: 0.25 }], curByDate: [{ date: '20261001', grossAmt: 1500 }] },
  398. }];
  399. const good = prov.checkProvenance('本期营业额 10500 元,环比 +25%;2026-10-01 当天 1,500 元。', steps);
  400. expect(good.total > 0 && good.untraced.length === 0, `应全部溯源:${JSON.stringify(good)}`);
  401. const bad = prov.checkProvenance('本期营业额 99999 元,环比 +25%。', steps);
  402. expect(bad.untraced.length === 1 && bad.untraced[0] === 99999, `编造数字未被标记:${JSON.stringify(bad)}`);
  403. });
  404. // ---- 安全加固回归(纯函数,不落盘、不打外部 API) ----
  405. const { createContext } = require('../server/context.js');
  406. const { createGenerationService } = require('../server/services/generation.js');
  407. await check('buildImagesAndPrompt → 参考图越界被钳制', async () => {
  408. const gen = createGenerationService(createContext());
  409. const out = gen.buildImagesAndPrompt(os.tmpdir(), '测试提示词', [
  410. '../../config.json', // 相对路径越界
  411. '/etc/passwd', // 绝对路径注入
  412. 'sub/../ok.png', // 归一化后仍在目录内的合法形态(文件不存在也会被读取失败跳过)
  413. ], {});
  414. expect(out.images.length === 0, `越界参考图未被拦截:images=${out.images.length}`);
  415. });
  416. await check('jsonForScript → </script> 不再原样进入脚本体', async () => {
  417. const { jsonForScript } = require('../report-html.js');
  418. const out = jsonForScript(['一食堂</script><img src=x onerror=alert(1)>']);
  419. expect(!out.includes('</script>') && out.includes('\\u003c'), out);
  420. });
  421. await check('mergeDefaults → 嵌套默认值不被 config.json 局部对象清掉', async () => {
  422. const { mergeDefaults, CONFIG_DEFAULTS } = require('../server/context.js');
  423. const merged = mergeDefaults(CONFIG_DEFAULTS, { aitable: { enabled: true, appSecret: 'sec' } });
  424. expect(merged.aitable.enabled === true && merged.aitable.appSecret === 'sec', '保存值未生效');
  425. expect(merged.aitable.appKey === CONFIG_DEFAULTS.aitable.appKey && !!merged.aitable.baseId && !!merged.aitable.operatorId,
  426. `组织级默认被清掉:appKey=${merged.aitable.appKey}`);
  427. expect(merged.autoSync.time === CONFIG_DEFAULTS.autoSync.time, 'autoSync 默认被清掉');
  428. });
  429. // ---- 静态与路由行为 ----
  430. 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']) {
  431. await check(`静态 ${p}`, async () => {
  432. const r = await request(port, 'GET', p);
  433. expect(r.status === 200, `status=${r.status}`);
  434. });
  435. }
  436. await check('GET /api/reports/bench(合法字段)', async () => {
  437. const r = await request(port, 'GET', '/api/reports/bench?field=gross_amt&from=20260101&to=20260102');
  438. expect(r.status === 200, `status=${r.status}`);
  439. const body = r.json();
  440. expect(Array.isArray(body.rows), 'rows 应为数组');
  441. });
  442. await check('GET /api/reports/bench(非法字段 → 400)', async () => {
  443. const r = await request(port, 'GET', '/api/reports/bench?field=no_such');
  444. expect(r.status === 400, `status=${r.status}`);
  445. });
  446. await check('GET /no-such-path → 404', async () => {
  447. const r = await request(port, 'GET', '/no-such-path');
  448. expect(r.status === 404, `status=${r.status}`);
  449. });
  450. await check('DELETE /api/settings → 405', async () => {
  451. const r = await request(port, 'DELETE', '/api/settings');
  452. expect(r.status === 405, `status=${r.status}`);
  453. });
  454. // ---- 问数智能体:主服务上只测只读会话接口(绝不在有主体/真模型时发起运行) ----
  455. await check('GET /api/agent/sessions(无主体 → 400;有主体 → 200)', async () => {
  456. const ent = await request(port, 'GET', '/api/reports/entities');
  457. const r = await request(port, 'GET', '/api/agent/sessions');
  458. if (ent.json().entities.length > 0) {
  459. expect(r.status === 200 && Array.isArray(r.json().sessions), `status=${r.status}`);
  460. } else {
  461. expect(r.status === 400 && r.json().code === 'ASK_NO_ENTITY', `status=${r.status} code=${r.json().code}`);
  462. }
  463. });
  464. // ---- 问数智能体端到端(隔离湖 + NAPOS_AGENT_MOCK 确定性假模型,零网络零真实数据) ----
  465. {
  466. const fs = require('fs');
  467. const tmpLake = fs.mkdtempSync(path.join(os.tmpdir(), 'genpic-lake-'));
  468. const tmpAgent = fs.mkdtempSync(path.join(os.tmpdir(), 'genpic-agent-'));
  469. const mkRow = (date, scope, ids, m) => ({
  470. date, scope,
  471. shopId: ids.shopId || '', shopName: ids.shopName || '',
  472. chainId: ids.chainId || '', chainName: ids.chainName || '',
  473. bizHours: null, abnormalCloseHours: null, goodsSaleAmt: null,
  474. syncedAt: new Date().toISOString(),
  475. validOrderCnt: m.orders || 0, incomeAmt: m.income || 0, grossAmt: m.gross || 0,
  476. expenseAmt: m.expense || 0, expUv: m.uv || 0, orderUserCnt: m.buyers || 0,
  477. clkExpRate: m.clk || 0, ordClkRate: 0, perActualPay: 0,
  478. actOrdCnt: 0, actSubsidyAmt: 0, shopRoi: 0, rebuyRate30d: 0,
  479. // 全字段落库模拟:同行均值/排名与环比差值(peer_benchmark / raw_inspect 用)
  480. raw: {
  481. gross_amt: m.gross || 0, valid_order_cnt: m.orders || 0,
  482. gross_amt_mall_avg: m.peerAvg != null ? m.peerAvg : 800,
  483. gross_amt_mall_avg_rank: m.rank != null ? m.rank : 2,
  484. valid_order_cnt_diff: m.orders || 0,
  485. },
  486. });
  487. // 健康主体:0924~0930(缺 0927)+ 1001~1007,两店;本期日营业额 1500 / 上期 1400(环比 +25%)
  488. const prevDays = ['20260924', '20260925', '20260926', '20260928', '20260929', '20260930'];
  489. const curDays = ['20261001', '20261002', '20261003', '20261004', '20261005', '20261006', '20261007'];
  490. const healthyRows = [];
  491. for (const d of prevDays) {
  492. 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 }));
  493. 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 }));
  494. healthyRows.push(mkRow(d, 'chain', { chainId: '9', chainName: '第一连锁' }, { gross: 1400, income: 1120, expense: 280, orders: 130, uv: 2700, buyers: 145, clk: 0.08 }));
  495. healthyRows.push(mkRow(d, 'company', { chainId: '-1' }, { gross: 1400, income: 1120, expense: 280, orders: 130, uv: 2700, buyers: 145, clk: 0.08 }));
  496. }
  497. for (const d of curDays) {
  498. 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 }));
  499. 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 }));
  500. healthyRows.push(mkRow(d, 'chain', { chainId: '9', chainName: '第一连锁' }, { gross: 1500, income: 1200, expense: 300, orders: 140, uv: 2900, buyers: 155, clk: 0.08 }));
  501. healthyRows.push(mkRow(d, 'company', { chainId: '-1' }, { gross: 1500, income: 1200, expense: 300, orders: 140, uv: 2900, buyers: 155, clk: 0.08 }));
  502. }
  503. // 全零主体:门店行全零而连锁/公司有数(已知 bug 形态,体检必须 fail)
  504. const badRows = [];
  505. for (const d of ['20261001', '20261002', '20261003']) {
  506. badRows.push(mkRow(d, 'shop', { shopId: '201', shopName: '全零店', chainId: '9', chainName: '第一连锁' }, {}));
  507. badRows.push(mkRow(d, 'chain', { chainId: '9', chainName: '第一连锁' }, { gross: 1000, income: 800, expense: 200, orders: 100 }));
  508. badRows.push(mkRow(d, 'company', { chainId: '-1' }, { gross: 1000, income: 800, expense: 200, orders: 100 }));
  509. }
  510. const nowIso = new Date().toISOString();
  511. fs.writeFileSync(path.join(tmpLake, 'entities.json'), JSON.stringify({ entities: [
  512. { key: 'smokeask', firstSeen: nowIso, lastSeen: nowIso, merchantId: 'smokeask', name: '冒烟主体A', chainCount: 1, shopCount: 2,
  513. chains: [{ chainId: '9', chainName: '第一连锁' }],
  514. shops: [
  515. { shopId: '101', shopName: '一店', chainId: '9', chainName: '第一连锁' },
  516. { shopId: '102', shopName: '二店', chainId: '9', chainName: '第一连锁' },
  517. ] },
  518. { key: 'smokebad', firstSeen: nowIso, lastSeen: nowIso, merchantId: 'smokebad', name: '冒烟主体B', chainCount: 1, shopCount: 1,
  519. chains: [{ chainId: '9', chainName: '第一连锁' }],
  520. shops: [{ shopId: '201', shopName: '全零店', chainId: '9', chainName: '第一连锁' }] },
  521. ] }));
  522. fs.mkdirSync(path.join(tmpLake, 'smokeask'), { recursive: true });
  523. fs.writeFileSync(path.join(tmpLake, 'smokeask', 'daily-202609.json'), JSON.stringify({ version: 2, updated: nowIso, rows: healthyRows.filter((r) => r.date < '20261000') }));
  524. fs.writeFileSync(path.join(tmpLake, 'smokeask', 'daily-202610.json'), JSON.stringify({ version: 2, updated: nowIso, rows: healthyRows.filter((r) => r.date >= '20261000') }));
  525. fs.mkdirSync(path.join(tmpLake, 'smokebad'), { recursive: true });
  526. fs.writeFileSync(path.join(tmpLake, 'smokebad', 'daily-202610.json'), JSON.stringify({ version: 2, updated: nowIso, rows: badRows }));
  527. const aport = await pickPort();
  528. const achild = spawn(process.execPath, [path.join(ROOT, 'server.js')], {
  529. env: {
  530. ...process.env, PORT: String(aport), HOST: '127.0.0.1',
  531. GENPIC_LAKE_DIR: tmpLake, GENPIC_AGENT_DIR: tmpAgent, NAPOS_AGENT_MOCK: '1',
  532. },
  533. stdio: ['ignore', 'pipe', 'pipe'],
  534. });
  535. let alog = '';
  536. achild.stdout.on('data', (c) => { alog += c; });
  537. achild.stderr.on('data', (c) => { alog += c; });
  538. try {
  539. await waitForHealth(aport);
  540. const waitRunDone = async (runId, entity, timeoutMs = 10000) => {
  541. const start = Date.now();
  542. for (;;) {
  543. const r = await request(aport, 'GET', `/api/agent/runs/${enc(runId)}?entity=${enc(entity)}`);
  544. const run = r.json().run;
  545. if (run && run.status !== 'running') return run;
  546. if (Date.now() - start > timeoutMs) throw new Error(`运行未在超时内结束 status=${run && run.status}`);
  547. await new Promise((res) => setTimeout(res, 200));
  548. }
  549. };
  550. const stepByTool = (run, tool) => (run.steps || []).find((s) => s.type === 'tool' && s.tool === tool);
  551. let askSessionId = null;
  552. let askRunId = null;
  553. const fetchAskRun = async () => {
  554. const r = await request(aport, 'GET', `/api/agent/runs/${enc(askRunId)}?entity=${enc('smokeask')}`);
  555. return r.json().run;
  556. };
  557. await check('智能体 mock 运行 → 四工具全成 + final + 用量', async () => {
  558. const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '最近营业额趋势如何?', mode: 'agent' });
  559. expect(r.status === 200 && r.json().run && r.json().run.runId, `status=${r.status}`);
  560. const run = await waitRunDone(r.json().run.runId, 'smokeask');
  561. expect(run.status === 'done', `status=${run.status} error=${run.error}`);
  562. const tools = (run.steps || []).filter((s) => s.type === 'tool');
  563. expect(JSON.stringify(tools.map((s) => s.tool)) === JSON.stringify(['lake_overview', 'query_report', 'compare_periods', 'data_quality', 'build_report']),
  564. `工具序列异常:${tools.map((s) => s.tool).join(',')}`);
  565. expect(tools.every((s) => s.ok), '存在失败的工具步骤');
  566. expect(run.usage.llmCalls === 6 && run.usage.toolCalls === 5, `用量异常 llm=${run.usage.llmCalls} tool=${run.usage.toolCalls}`);
  567. expect(run.result && run.result.answer.includes('mock'), JSON.stringify(run.result));
  568. expect(run.result.chart && run.result.chart.type === 'line', 'final 缺图表规格');
  569. expect(run.result.reportUrl && run.result.reportUrl.includes('/api/reports/report-html'), `缺报告链接:${run.result.reportUrl}`);
  570. askSessionId = run.sessionId;
  571. askRunId = run.runId;
  572. });
  573. await check('智能体 mock → compare_periods 环比数字正确(门店对齐 + +25%)', async () => {
  574. const run = await fetchAskRun();
  575. const cmp = stepByTool(run, 'compare_periods');
  576. expect(cmp && cmp.ok && cmp.data, 'compare_periods 步骤缺失');
  577. const gross = cmp.data.deltas.find((d) => d.key === 'grossAmt');
  578. expect(gross && gross.cur === 10500 && gross.prev === 8400, `环比数字异常 ${JSON.stringify(gross)}`);
  579. expect(Math.abs(gross.deltaPct - 0.25) < 1e-9, `变化率异常 ${gross.deltaPct}`);
  580. expect(cmp.data.comparableShopCount === 2, `可比门店数异常 ${cmp.data.comparableShopCount}`);
  581. });
  582. await check('智能体 mock → query_report 按日聚合 13 天', async () => {
  583. const run = await fetchAskRun();
  584. const q = stepByTool(run, 'query_report');
  585. expect(q && q.ok && q.data && q.data.rows.length === 13, `按日行数异常 ${q && q.data && q.data.rows.length}`);
  586. });
  587. await check('智能体 mock → data_quality 健康主体无 fail 且报缺 1 天', async () => {
  588. const run = await fetchAskRun();
  589. const dq = stepByTool(run, 'data_quality');
  590. expect(dq && dq.ok, 'data_quality 步骤缺失');
  591. expect(dq.summary.includes('缺 1/14'), `缺日未识别:${dq.summary}`);
  592. expect(!dq.summary.includes('[fail]'), `健康主体误报 fail:${dq.summary}`);
  593. });
  594. await check('智能体 mock → 全零主体体检必须 fail', async () => {
  595. const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokebad', question: '体检一下', mode: 'agent' });
  596. expect(r.status === 200, `status=${r.status}`);
  597. const run = await waitRunDone(r.json().run.runId, 'smokebad');
  598. expect(run.status === 'done', `status=${run.status} error=${run.error}`);
  599. const dq = stepByTool(run, 'data_quality');
  600. expect(dq && dq.summary.includes('[fail]') && dq.summary.includes('全零'), dq.summary);
  601. });
  602. await check('智能体 mock 快问 → 直接终答(同一会话追问)', async () => {
  603. const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '昨天营业额', mode: 'quick', sessionId: askSessionId });
  604. expect(r.status === 200, `status=${r.status}`);
  605. const run = await waitRunDone(r.json().run.runId, 'smokeask');
  606. expect(run.status === 'done' && run.mode === 'quick' && run.result.answer.includes('快问'), JSON.stringify(run.result));
  607. expect(run.sessionId === askSessionId, '快问未落入同一会话');
  608. });
  609. await check('智能体 会话索引 → 标题=首问、运行入册、带最近一问', async () => {
  610. const s = await request(aport, 'GET', `/api/agent/sessions?entity=${enc('smokeask')}`);
  611. const sessions = s.json().sessions;
  612. expect(sessions.length === 1 && sessions[0].title === '最近营业额趋势如何?' && sessions[0].runCount === 2,
  613. JSON.stringify(sessions));
  614. expect(sessions[0].lastQuestion === '昨天营业额', `lastQuestion 异常:${sessions[0].lastQuestion}`);
  615. const runs = await request(aport, 'GET', `/api/agent/runs?entity=${enc('smokeask')}&sessionId=${enc(sessions[0].sessionId)}`);
  616. expect(runs.json().runs.length === 2, '会话运行列表数量异常');
  617. });
  618. await check('智能体 删除会话 → 索引与运行文件清零、再删 404', async () => {
  619. const s = await request(aport, 'GET', `/api/agent/sessions?entity=${enc('smokeask')}`);
  620. const sid = s.json().sessions[0].sessionId;
  621. const del = await request(aport, 'DELETE', `/api/agent/sessions/${enc(sid)}?entity=${enc('smokeask')}`);
  622. expect(del.status === 200 && del.json().ok === true, `status=${del.status}`);
  623. const after = await request(aport, 'GET', `/api/agent/sessions?entity=${enc('smokeask')}`);
  624. expect(after.json().sessions.length === 0, '删除后索引未清空');
  625. const del2 = await request(aport, 'DELETE', `/api/agent/sessions/${enc(sid)}?entity=${enc('smokeask')}`);
  626. expect(del2.status === 404, `重复删除应 404,实际 ${del2.status}`);
  627. });
  628. await check('GET /api/agent/skills → 四个内置技能', async () => {
  629. const r = await request(aport, 'GET', '/api/agent/skills');
  630. const skills = r.json().skills || [];
  631. expect(r.status === 200 && skills.length === 4 && skills.every((s) => s.key && s.name), JSON.stringify(skills));
  632. });
  633. await check('智能体 技能点名 → run.skill 落档且运行完成', async () => {
  634. const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '对比本期与上期', mode: 'agent', skill: 'period-compare' });
  635. expect(r.status === 200, `status=${r.status}`);
  636. const run = await waitRunDone(r.json().run.runId, 'smokeask');
  637. expect(run.status === 'done' && run.skill === 'period-compare', `skill=${run.skill} status=${run.status} error=${run.error}`);
  638. });
  639. await check('智能体 verdicts 归档 → 校验工具结论入运行记录', async () => {
  640. const run = await fetchAskRun();
  641. const vs = run.verdicts || [];
  642. expect(vs.length >= 5, `verdicts 过少:${vs.length}`);
  643. expect(vs.some((v) => v.kind === 'identity') && vs.some((v) => v.kind === 'quality'), `缺 identity/quality verdict:${JSON.stringify(vs.map((v) => v.kind))}`);
  644. expect(vs.every((v) => v.id && ['ok', 'warn', 'fail'].includes(v.status)), `verdict 形状异常:${JSON.stringify(vs[0])}`);
  645. });
  646. await check('智能体 自动分流 → 短问走快问、分析问走深度', async () => {
  647. const r1 = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '昨天营业额多少', mode: 'auto' });
  648. const run1 = await waitRunDone(r1.json().run.runId, 'smokeask');
  649. expect(run1.status === 'done' && run1.mode === 'quick' && run1.autoPicked === true, `短问未分流快问:mode=${run1.mode}`);
  650. const r2 = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: '最近30天比上一个30天营业额变化多少,主要受哪些门店影响', mode: 'auto' });
  651. const run2 = await waitRunDone(r2.json().run.runId, 'smokeask');
  652. expect(run2.status === 'done' && run2.mode === 'agent' && run2.autoPicked === true, `分析问未分流深度:mode=${run2.mode}`);
  653. });
  654. await check('智能体 工具直测(隔离子进程)→ cross_check/peer_benchmark/raw_inspect', async () => {
  655. const toolProbe = [
  656. `const ROOT = ${JSON.stringify(ROOT)};`,
  657. 'const svc = require(ROOT + "/napos-service.js");',
  658. 'const nlq = require(ROOT + "/nl-query.js");',
  659. 'const cross = require(ROOT + "/server/agent/tools/cross.js");',
  660. 'const peer = require(ROOT + "/server/agent/tools/peer.js");',
  661. 'function ctxOf(entity) { const meta = svc.store.getEntity(entity); return { svc, entityKey: entity, meta, shops: meta.shops || [], dateRange: nlq.lakeDateRange(entity, svc) }; }',
  662. 'const out = {};',
  663. 'const healthy = ctxOf("smokeask");',
  664. 'const c1 = cross.crossHandler(healthy, cross.crossSanitize({}, healthy));',
  665. 'out.healthy = { checked: c1.data.checked, mismatch: c1.data.mismatchCount, severe: c1.data.severe };',
  666. 'const bad = ctxOf("smokebad");',
  667. 'const c2 = cross.crossHandler(bad, cross.crossSanitize({}, bad));',
  668. 'out.bad = { mismatch: c2.data.mismatchCount, severe: c2.data.severe };',
  669. 'const b1 = peer.benchHandler(healthy, peer.benchSanitize({ field: "gross_amt" }, healthy));',
  670. 'const bs = b1.data.shops || [];',
  671. 'out.bench = { shops: bs.length, firstPeer: bs[0] && bs[0].peerAvg, firstRank: bs[0] && bs[0].rankAvg };',
  672. 'const r1 = peer.rawHandler(healthy, peer.rawSanitize({ date: "20261001", shops: ["一店"] }, healthy));',
  673. 'out.raw = { found: r1.data.found, matched: (r1.data.matched || []).length, hasRaw: r1.data.hasRaw };',
  674. 'const entityTool = require(ROOT + "/server/agent/tools/entity.js");',
  675. 'const e1 = entityTool.entityHandler(healthy, entityTool.entitySanitize({}, healthy));',
  676. 'out.entity = { entities: (e1.data.entities || []).length, first: (e1.data.entities || [])[0] && (e1.data.entities || [])[0].name, skipped: (e1.data.skipped || []).length };',
  677. 'const cloudTool = require(ROOT + "/server/agent/tools/cloud.js");',
  678. 'cloudTool.cloudHandler(healthy, cloudTool.cloudSanitize({}, healthy)).then((c3) => {',
  679. ' out.cloud = { available: c3.data.available, hasVerdict: Array.isArray(c3.data.verdicts) && c3.data.verdicts.length === 1 };',
  680. ' console.log(JSON.stringify(out));',
  681. '});',
  682. ].join('\n');
  683. const probe = spawnSync(process.execPath, ['-e', toolProbe], {
  684. encoding: 'utf8', timeout: 20000,
  685. env: { ...process.env, GENPIC_LAKE_DIR: tmpLake },
  686. });
  687. expect(probe.status === 0, `探针失败:${(probe.stderr || probe.stdout || '').slice(-300)}`);
  688. const out = JSON.parse(probe.stdout.trim().split('\n').pop());
  689. expect(out.healthy.checked > 0 && out.healthy.mismatch === 0 && out.healthy.severe === false,
  690. `健康主体层间核对异常:${JSON.stringify(out.healthy)}`);
  691. expect(out.bad.severe === true && out.bad.mismatch > 0, `全零主体未检出层间不一致:${JSON.stringify(out.bad)}`);
  692. expect(out.bench.shops === 2 && out.bench.firstPeer === 800 && out.bench.firstRank === 2,
  693. `同行对标异常:${JSON.stringify(out.bench)}`);
  694. expect(out.raw.found === true && out.raw.hasRaw === true && out.raw.matched >= 3,
  695. `raw 检视异常:${JSON.stringify(out.raw)}`);
  696. expect(out.entity.entities === 2 && out.entity.first === '冒烟主体A' && out.entity.skipped === 0,
  697. `跨主体对比异常:${JSON.stringify(out.entity)}`);
  698. expect(out.cloud.available === false && out.cloud.hasVerdict === true,
  699. `云端未启用未优雅降级:${JSON.stringify(out.cloud)}`);
  700. });
  701. await check('智能体 空问题 → 400 ASK_EMPTY', async () => {
  702. const r = await request(aport, 'POST', '/api/agent/runs', { entity: 'smokeask', question: ' ' });
  703. expect(r.status === 400 && r.json().code === 'ASK_EMPTY', `status=${r.status} code=${r.json().code}`);
  704. });
  705. await check('智能体 取消不存在的运行 → 404', async () => {
  706. const r = await request(aport, 'POST', '/api/agent/runs/no-such-run/cancel', {});
  707. expect(r.status === 404, `status=${r.status}`);
  708. });
  709. } catch (e) {
  710. results.push({ name: '(智能体隔离测试执行中断)', ok: false, error: e.message + (alog ? ` | 服务日志:${alog.slice(-300)}` : '') });
  711. } finally {
  712. try { achild.kill('SIGKILL'); } catch { /* ignore */ }
  713. try { fs.rmSync(tmpLake, { recursive: true, force: true }); fs.rmSync(tmpAgent, { recursive: true, force: true }); } catch { /* ignore */ }
  714. }
  715. }
  716. // ---- 优雅停机(SIGTERM 后应自行退出) ----
  717. await check('SIGTERM 优雅停机', async () => {
  718. process.kill(child.pid, 'SIGTERM');
  719. const exited = await new Promise((resolve) => {
  720. const t = setTimeout(() => resolve(false), 8000);
  721. child.on('exit', () => { clearTimeout(t); resolve(true); });
  722. });
  723. expect(exited, 'SIGTERM 后 8 秒内未退出');
  724. });
  725. // ---- 访问口令(需局域网 IP:本机回环地址在守卫中直接放行,测不到 401) ----
  726. const lanIp = getLanIpv4();
  727. if (!lanIp) {
  728. results.push({ name: '访问口令(无局域网 IP,跳过)', ok: true });
  729. } else {
  730. const tport = await pickPort();
  731. const tchild = spawn(process.execPath, [path.join(ROOT, 'server.js')], {
  732. env: { ...process.env, PORT: String(tport), HOST: '0.0.0.0', ACCESS_TOKEN: 'smoke-token' },
  733. stdio: ['ignore', 'ignore', 'ignore'],
  734. });
  735. try {
  736. await waitForHealthLan(lanIp, tport);
  737. await check('口令开启:无口令访问 API → 401', async () => {
  738. const r = await request(tport, 'GET', '/api/models', undefined, lanIp);
  739. expect(r.status === 401, `status=${r.status}`);
  740. });
  741. await check('口令开启:?token= 通过', async () => {
  742. const r = await request(tport, 'GET', `/api/models?token=smoke-token`, undefined, lanIp);
  743. expect(r.status === 200, `status=${r.status}`);
  744. });
  745. await check('口令开启:X-Access-Token 头通过', async () => {
  746. const r = await request(tport, 'GET', '/api/models', undefined, lanIp, { 'X-Access-Token': 'smoke-token' });
  747. expect(r.status === 200, `status=${r.status}`);
  748. });
  749. await check('口令开启:cookie 通过', async () => {
  750. const r = await request(tport, 'GET', '/api/models', undefined, lanIp, { Cookie: 'accessToken=smoke-token' });
  751. expect(r.status === 200, `status=${r.status}`);
  752. });
  753. await check('口令开启:/healthz 与静态页放行', async () => {
  754. const h = await request(tport, 'GET', '/healthz', undefined, lanIp);
  755. const s = await request(tport, 'GET', '/', undefined, lanIp);
  756. expect(h.status === 200 && s.status === 200, `healthz=${h.status} static=${s.status}`);
  757. });
  758. await check('口令开启:错误口令 → 401', async () => {
  759. const r = await request(tport, 'GET', '/api/models?token=wrong', undefined, lanIp);
  760. expect(r.status === 401, `status=${r.status}`);
  761. });
  762. } finally {
  763. try { tchild.kill('SIGKILL'); } catch { /* ignore */ }
  764. }
  765. }
  766. } catch (e) {
  767. results.push({ name: '(测试执行中断)', ok: false, error: e.message });
  768. } finally {
  769. try { child.kill('SIGKILL'); } catch { /* already dead */ }
  770. }
  771. const failed = results.filter((r) => !r.ok);
  772. console.log('\n========== 冒烟测试结果 ==========');
  773. for (const r of results) {
  774. console.log(`${r.ok ? '✓' : '✗'} ${r.name}${r.ok ? '' : ' ← ' + r.error}`);
  775. }
  776. console.log(`\n通过 ${results.length - failed.length}/${results.length}`);
  777. if (failed.length) {
  778. console.log('\n服务启动日志(末尾 500 字):');
  779. console.log(bootLog.slice(-500));
  780. process.exit(1);
  781. }
  782. }
  783. main().catch((e) => {
  784. console.error('冒烟脚本自身异常:', e);
  785. process.exit(1);
  786. });