smoke.js 63 KB

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