smoke.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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('POST /api/reports/aitable/push(无主体 → 400)', async () => {
  275. const r = await request(port, 'POST', '/api/reports/aitable/push', {});
  276. expect(r.status === 400 && r.json().code === 'AITABLE_NO_ENTITY', `status=${r.status} code=${r.json().code}`);
  277. });
  278. // ---- 静态与路由行为 ----
  279. for (const p of ['/', '/index.html', '/app.js', '/crawl.js', '/gen.js', '/history.js', '/tasks.js', '/reports.js', '/lake.js', '/style.css', '/vendor/echarts.min.js']) {
  280. await check(`静态 ${p}`, async () => {
  281. const r = await request(port, 'GET', p);
  282. expect(r.status === 200, `status=${r.status}`);
  283. });
  284. }
  285. await check('GET /no-such-path → 404', async () => {
  286. const r = await request(port, 'GET', '/no-such-path');
  287. expect(r.status === 404, `status=${r.status}`);
  288. });
  289. await check('DELETE /api/settings → 405', async () => {
  290. const r = await request(port, 'DELETE', '/api/settings');
  291. expect(r.status === 405, `status=${r.status}`);
  292. });
  293. // ---- 优雅停机(SIGTERM 后应自行退出) ----
  294. await check('SIGTERM 优雅停机', async () => {
  295. process.kill(child.pid, 'SIGTERM');
  296. const exited = await new Promise((resolve) => {
  297. const t = setTimeout(() => resolve(false), 8000);
  298. child.on('exit', () => { clearTimeout(t); resolve(true); });
  299. });
  300. expect(exited, 'SIGTERM 后 8 秒内未退出');
  301. });
  302. // ---- 访问口令(需局域网 IP:本机回环地址在守卫中直接放行,测不到 401) ----
  303. const lanIp = getLanIpv4();
  304. if (!lanIp) {
  305. results.push({ name: '访问口令(无局域网 IP,跳过)', ok: true });
  306. } else {
  307. const tport = await pickPort();
  308. const tchild = spawn(process.execPath, [path.join(ROOT, 'server.js')], {
  309. env: { ...process.env, PORT: String(tport), HOST: '0.0.0.0', ACCESS_TOKEN: 'smoke-token' },
  310. stdio: ['ignore', 'ignore', 'ignore'],
  311. });
  312. try {
  313. await waitForHealthLan(lanIp, tport);
  314. await check('口令开启:无口令访问 API → 401', async () => {
  315. const r = await request(tport, 'GET', '/api/models', undefined, lanIp);
  316. expect(r.status === 401, `status=${r.status}`);
  317. });
  318. await check('口令开启:?token= 通过', async () => {
  319. const r = await request(tport, 'GET', `/api/models?token=smoke-token`, undefined, lanIp);
  320. expect(r.status === 200, `status=${r.status}`);
  321. });
  322. await check('口令开启:X-Access-Token 头通过', async () => {
  323. const r = await request(tport, 'GET', '/api/models', undefined, lanIp, { 'X-Access-Token': 'smoke-token' });
  324. expect(r.status === 200, `status=${r.status}`);
  325. });
  326. await check('口令开启:cookie 通过', async () => {
  327. const r = await request(tport, 'GET', '/api/models', undefined, lanIp, { Cookie: 'accessToken=smoke-token' });
  328. expect(r.status === 200, `status=${r.status}`);
  329. });
  330. await check('口令开启:/healthz 与静态页放行', async () => {
  331. const h = await request(tport, 'GET', '/healthz', undefined, lanIp);
  332. const s = await request(tport, 'GET', '/', undefined, lanIp);
  333. expect(h.status === 200 && s.status === 200, `healthz=${h.status} static=${s.status}`);
  334. });
  335. await check('口令开启:错误口令 → 401', async () => {
  336. const r = await request(tport, 'GET', '/api/models?token=wrong', undefined, lanIp);
  337. expect(r.status === 401, `status=${r.status}`);
  338. });
  339. } finally {
  340. try { tchild.kill('SIGKILL'); } catch { /* ignore */ }
  341. }
  342. }
  343. } catch (e) {
  344. results.push({ name: '(测试执行中断)', ok: false, error: e.message });
  345. } finally {
  346. try { child.kill('SIGKILL'); } catch { /* already dead */ }
  347. }
  348. const failed = results.filter((r) => !r.ok);
  349. console.log('\n========== 冒烟测试结果 ==========');
  350. for (const r of results) {
  351. console.log(`${r.ok ? '✓' : '✗'} ${r.name}${r.ok ? '' : ' ← ' + r.error}`);
  352. }
  353. console.log(`\n通过 ${results.length - failed.length}/${results.length}`);
  354. if (failed.length) {
  355. console.log('\n服务启动日志(末尾 500 字):');
  356. console.log(bootLog.slice(-500));
  357. process.exit(1);
  358. }
  359. }
  360. main().catch((e) => {
  361. console.error('冒烟脚本自身异常:', e);
  362. process.exit(1);
  363. });