smoke.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 } = 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. // ---- 生成/任务/历史域 ----
  191. await check('GET /api/tasks', async () => {
  192. const r = await request(port, 'GET', '/api/tasks');
  193. const j = r.json();
  194. expect(r.status === 200 && j.summary && Array.isArray(j.tasks), `status=${r.status}`);
  195. });
  196. await check('GET /api/job/未知ID → 404', async () => {
  197. const r = await request(port, 'GET', '/api/job/00000000-0000-0000-0000-000000000000');
  198. expect(r.status === 404, `status=${r.status}`);
  199. });
  200. await check('GET /api/creations', async () => {
  201. const r = await request(port, 'GET', `/api/creations?root=${encRoot}&limit=5`);
  202. const j = r.json();
  203. expect(r.status === 200 && typeof j.total === 'number' && Array.isArray(j.items), `status=${r.status}`);
  204. });
  205. await check('GET /files 越界 → 403', async () => {
  206. const r = await request(port, 'GET', `/files?path=${enc('/etc/passwd')}`);
  207. expect(r.status === 403, `status=${r.status}`);
  208. });
  209. await check('GET /files 缺参 → 400', async () => {
  210. const r = await request(port, 'GET', '/files');
  211. expect(r.status === 400, `status=${r.status}`);
  212. });
  213. // ---- 日志 ----
  214. await check('GET /api/logs', async () => {
  215. const r = await request(port, 'GET', '/api/logs');
  216. expect(r.status === 200 && Array.isArray(r.json().logs), `status=${r.status}`);
  217. });
  218. await check('POST /api/logs/clear', async () => {
  219. const r = await request(port, 'POST', '/api/logs/clear');
  220. expect(r.status === 200 && r.json().ok === true, `status=${r.status}`);
  221. });
  222. // ---- 数据报表域 ----
  223. await check('GET /api/reports/status(不限方法 GET)', async () => {
  224. const r = await request(port, 'GET', '/api/reports/status');
  225. expect(r.status === 200 && r.json().napos, `status=${r.status}`);
  226. });
  227. await check('POST /api/reports/status(不限方法 POST)', async () => {
  228. const r = await request(port, 'POST', '/api/reports/status');
  229. expect(r.status === 200, `status=${r.status}`);
  230. });
  231. await check('GET /api/reports/columns', async () => {
  232. const r = await request(port, 'GET', '/api/reports/columns');
  233. expect(r.status === 200 && Array.isArray(r.json().columns) && r.json().columns.length >= 10, `status=${r.status}`);
  234. });
  235. await check('GET /api/reports/entities', async () => {
  236. const r = await request(port, 'GET', '/api/reports/entities');
  237. expect(r.status === 200 && Array.isArray(r.json().entities), `status=${r.status}`);
  238. });
  239. await check('GET /api/reports/sync/status', async () => {
  240. const r = await request(port, 'GET', '/api/reports/sync/status');
  241. expect(r.status === 200 && typeof r.json().running === 'boolean', `status=${r.status}`);
  242. });
  243. await check('GET /api/reports/syncs', async () => {
  244. const r = await request(port, 'GET', '/api/reports/syncs');
  245. expect(r.status === 200 && Array.isArray(r.json().runs), `status=${r.status}`);
  246. });
  247. await check('GET /api/reports/daily', async () => {
  248. const r = await request(port, 'GET', '/api/reports/daily');
  249. expect(r.status === 200, `status=${r.status}`);
  250. });
  251. await check('GET /api/reports/aggregate', async () => {
  252. const r = await request(port, 'GET', '/api/reports/aggregate');
  253. expect(r.status === 200, `status=${r.status}`);
  254. });
  255. await check('GET /api/reports/export → CSV', async () => {
  256. const r = await request(port, 'GET', '/api/reports/export');
  257. expect(r.status === 200 && /text\/csv/.test(r.headers['content-type'] || ''), `status=${r.status}`);
  258. });
  259. await check('GET /api/reports/report-html → HTML', async () => {
  260. const r = await request(port, 'GET', '/api/reports/report-html');
  261. expect(r.status === 200 && /text\/html/.test(r.headers['content-type'] || ''), `status=${r.status}`);
  262. });
  263. await check('POST /api/reports/ask(有主体时跳过)', async () => {
  264. const ent = await request(port, 'GET', '/api/reports/entities');
  265. if (ent.json().entities.length > 0) return; // 有主体会真调对话模型,跳过
  266. const r = await request(port, 'POST', '/api/reports/ask', { q: 'test' });
  267. expect(r.status === 400 && r.json().code === 'ASK_NO_ENTITY', `status=${r.status}`);
  268. });
  269. await check('GET /api/reports/lake → 湖全景', async () => {
  270. const r = await request(port, 'GET', '/api/reports/lake');
  271. const d = r.json();
  272. expect(r.status === 200 && Array.isArray(d.entities) && typeof d.totalRows === 'number' && d.aitable && 'enabled' in d.aitable, `status=${r.status}`);
  273. });
  274. await check('GET /api/reports/daily → 分页字段', async () => {
  275. const r = await request(port, 'GET', '/api/reports/daily');
  276. const d = r.json();
  277. expect(r.status === 200 && typeof d.total === 'number' && d.page >= 1 && d.pageSize >= 50 && d.pages >= 1 && 'summary' in d, `status=${r.status}`);
  278. });
  279. await check('GET /api/reports/syncs → 同步历史', async () => {
  280. const r = await request(port, 'GET', '/api/reports/syncs');
  281. expect(r.status === 200 && Array.isArray(r.json().runs), `status=${r.status}`);
  282. });
  283. await check('POST /api/reports/aitable/push(无主体 → 400)', async () => {
  284. const r = await request(port, 'POST', '/api/reports/aitable/push', {});
  285. expect(r.status === 400 && r.json().code === 'AITABLE_NO_ENTITY', `status=${r.status} code=${r.json().code}`);
  286. });
  287. // ---- 钉钉三表拆分与行键(纯函数回归,不打真实钉钉 API) ----
  288. const aitableLib = require('../server/services/aitable.js');
  289. const { rowKey } = require('../reports-store.js');
  290. await check('splitRowsBySheet → 按维度分流且 summary 不上云', async () => {
  291. const s = aitableLib.splitRowsBySheet([
  292. { scope: 'shop', shopId: '1', chainId: 'c1' },
  293. { scope: 'shop', shopId: '2', chainId: 'c1' },
  294. { scope: 'chain', shopId: '', chainId: 'c1' },
  295. { scope: 'chain', shopId: '', chainId: 'c2' },
  296. { scope: 'company', shopId: '', chainId: '-1' },
  297. { scope: 'summary', shopId: '', chainId: '' },
  298. ]);
  299. expect(s.detail.length === 2 && s.chain.length === 2 && s.company.length === 1 && s.skippedSummary === 1,
  300. `detail=${s.detail.length} chain=${s.chain.length} company=${s.company.length} skipped=${s.skippedSummary}`);
  301. });
  302. await check('recordKey / rowKey → 同日多连锁不碰撞', async () => {
  303. const cloud = (chainId) => aitableLib.recordKey({ 日期: '20261017', 账号名: 'A', 维度: '连锁', 连锁ID: chainId, 门店ID: '' });
  304. expect(cloud('c1') !== cloud('c2'), '连锁行云端业务键仍碰撞');
  305. const local = (row) => rowKey(row);
  306. expect(local({ date: '20261017', scope: 'chain', chainId: 'c1', shopId: '' }) !== local({ date: '20261017', scope: 'chain', chainId: 'c2', shopId: '' }), '连锁行本地行键仍碰撞');
  307. expect(local({ date: '20261017', scope: 'summary', chainId: '', shopId: '' }) !== local({ date: '20261017', scope: 'summary', chainId: 'c1', shopId: '' }), '汇总行本地行键仍碰撞');
  308. });
  309. await check('resolveSheets → 缺省兜底三表名', async () => {
  310. const { sheets, fingerprint } = aitableLib.resolveSheets({});
  311. expect(sheets.detail === '门店明细' && sheets.chain === '连锁汇总' && sheets.company === '公司汇总' && fingerprint.split('|').length === 3,
  312. JSON.stringify(sheets));
  313. });
  314. // ---- 问数两段式:结果摘要与兜底话术(纯函数,不调对话模型) ----
  315. const nlQueryLib = require('../nl-query.js');
  316. await check('buildResultDigest → 口径/Top行/合计齐全且按列类型格式化', async () => {
  317. const digest = nlQueryLib.buildResultDigest(
  318. { mode: 'shop', from: '20260918', to: '20261017', metrics: ['grossAmt', 'clkExpRate'], shopIds: [] },
  319. {
  320. rows: [
  321. { shopName: '一食堂', grossAmt: 12345.6, clkExpRate: 0.0521 },
  322. { shopName: '二食堂', grossAmt: 0, clkExpRate: 0 },
  323. ],
  324. summary: { grossAmt: 12345.6, clkExpRate: 0.0521 },
  325. }
  326. );
  327. expect(digest.includes('按门店汇总') && digest.includes('2026-09-18') && digest.includes('2026-10-17'), digest);
  328. expect(digest.includes('12,345.60元') && digest.includes('5.21%'), digest); // money 两位小数 / pct 小数×100
  329. expect(digest.includes('区间合计') && digest.includes('均值'), digest); // 金额=合计、比率=均值
  330. });
  331. await check('buildResultDigest → 空结果有明确说明', async () => {
  332. const digest = nlQueryLib.buildResultDigest(
  333. { mode: 'date', from: '20260918', to: '20261017', metrics: ['grossAmt'], shopIds: [] },
  334. { rows: [], summary: {} }
  335. );
  336. expect(digest.includes('结果为空'), digest);
  337. });
  338. await check('fallbackReply → 模板句含区间与指标名', async () => {
  339. const t = nlQueryLib.fallbackReply({ mode: 'date', from: '20260918', to: '20261017', metrics: ['grossAmt'] });
  340. expect(t.includes('2026-09-18') && t.includes('营业额'), t);
  341. });
  342. // ---- 静态与路由行为 ----
  343. for (const p of ['/', '/index.html', '/app.js', '/crawl.js', '/gen.js', '/history.js', '/tasks.js', '/reports.js', '/analysis-templates.js', '/lake.js', '/style.css', '/vendor/echarts.min.js']) {
  344. await check(`静态 ${p}`, async () => {
  345. const r = await request(port, 'GET', p);
  346. expect(r.status === 200, `status=${r.status}`);
  347. });
  348. }
  349. await check('GET /api/reports/bench(合法字段)', async () => {
  350. const r = await request(port, 'GET', '/api/reports/bench?field=gross_amt&from=20260101&to=20260102');
  351. expect(r.status === 200, `status=${r.status}`);
  352. const body = r.json();
  353. expect(Array.isArray(body.rows), 'rows 应为数组');
  354. });
  355. await check('GET /api/reports/bench(非法字段 → 400)', async () => {
  356. const r = await request(port, 'GET', '/api/reports/bench?field=no_such');
  357. expect(r.status === 400, `status=${r.status}`);
  358. });
  359. await check('GET /no-such-path → 404', async () => {
  360. const r = await request(port, 'GET', '/no-such-path');
  361. expect(r.status === 404, `status=${r.status}`);
  362. });
  363. await check('DELETE /api/settings → 405', async () => {
  364. const r = await request(port, 'DELETE', '/api/settings');
  365. expect(r.status === 405, `status=${r.status}`);
  366. });
  367. // ---- 优雅停机(SIGTERM 后应自行退出) ----
  368. await check('SIGTERM 优雅停机', async () => {
  369. process.kill(child.pid, 'SIGTERM');
  370. const exited = await new Promise((resolve) => {
  371. const t = setTimeout(() => resolve(false), 8000);
  372. child.on('exit', () => { clearTimeout(t); resolve(true); });
  373. });
  374. expect(exited, 'SIGTERM 后 8 秒内未退出');
  375. });
  376. // ---- 访问口令(需局域网 IP:本机回环地址在守卫中直接放行,测不到 401) ----
  377. const lanIp = getLanIpv4();
  378. if (!lanIp) {
  379. results.push({ name: '访问口令(无局域网 IP,跳过)', ok: true });
  380. } else {
  381. const tport = await pickPort();
  382. const tchild = spawn(process.execPath, [path.join(ROOT, 'server.js')], {
  383. env: { ...process.env, PORT: String(tport), HOST: '0.0.0.0', ACCESS_TOKEN: 'smoke-token' },
  384. stdio: ['ignore', 'ignore', 'ignore'],
  385. });
  386. try {
  387. await waitForHealthLan(lanIp, tport);
  388. await check('口令开启:无口令访问 API → 401', async () => {
  389. const r = await request(tport, 'GET', '/api/models', undefined, lanIp);
  390. expect(r.status === 401, `status=${r.status}`);
  391. });
  392. await check('口令开启:?token= 通过', async () => {
  393. const r = await request(tport, 'GET', `/api/models?token=smoke-token`, undefined, lanIp);
  394. expect(r.status === 200, `status=${r.status}`);
  395. });
  396. await check('口令开启:X-Access-Token 头通过', async () => {
  397. const r = await request(tport, 'GET', '/api/models', undefined, lanIp, { 'X-Access-Token': 'smoke-token' });
  398. expect(r.status === 200, `status=${r.status}`);
  399. });
  400. await check('口令开启:cookie 通过', async () => {
  401. const r = await request(tport, 'GET', '/api/models', undefined, lanIp, { Cookie: 'accessToken=smoke-token' });
  402. expect(r.status === 200, `status=${r.status}`);
  403. });
  404. await check('口令开启:/healthz 与静态页放行', async () => {
  405. const h = await request(tport, 'GET', '/healthz', undefined, lanIp);
  406. const s = await request(tport, 'GET', '/', undefined, lanIp);
  407. expect(h.status === 200 && s.status === 200, `healthz=${h.status} static=${s.status}`);
  408. });
  409. await check('口令开启:错误口令 → 401', async () => {
  410. const r = await request(tport, 'GET', '/api/models?token=wrong', undefined, lanIp);
  411. expect(r.status === 401, `status=${r.status}`);
  412. });
  413. } finally {
  414. try { tchild.kill('SIGKILL'); } catch { /* ignore */ }
  415. }
  416. }
  417. } catch (e) {
  418. results.push({ name: '(测试执行中断)', ok: false, error: e.message });
  419. } finally {
  420. try { child.kill('SIGKILL'); } catch { /* already dead */ }
  421. }
  422. const failed = results.filter((r) => !r.ok);
  423. console.log('\n========== 冒烟测试结果 ==========');
  424. for (const r of results) {
  425. console.log(`${r.ok ? '✓' : '✗'} ${r.name}${r.ok ? '' : ' ← ' + r.error}`);
  426. }
  427. console.log(`\n通过 ${results.length - failed.length}/${results.length}`);
  428. if (failed.length) {
  429. console.log('\n服务启动日志(末尾 500 字):');
  430. console.log(bootLog.slice(-500));
  431. process.exit(1);
  432. }
  433. }
  434. main().catch((e) => {
  435. console.error('冒烟脚本自身异常:', e);
  436. process.exit(1);
  437. });