#!/usr/bin/env node 'use strict'; // 零依赖冒烟测试:拉起真实服务(随机端口),全量核对端点行为后关停。 // 安全边界:只做只读请求 + 无副作用 POST(校验失败类、空操作); // 绝不触碰 DELETE /api/reports/data、/api/crawl/start、/api/open-folder、 // /api/image/test、/api/chat/test,也不在数据湖有主体时调用 /api/reports/ask。 // 用法:node scripts/smoke.js const { spawn } = require('child_process'); const net = require('net'); const http = require('http'); const os = require('os'); const path = require('path'); const ROOT = path.resolve(__dirname, '..'); function pickPort() { return new Promise((resolve, reject) => { const srv = net.createServer(); srv.unref(); srv.on('error', reject); srv.listen(0, '127.0.0.1', () => { const port = srv.address().port; srv.close(() => resolve(port)); }); }); } function request(port, method, urlPath, body, host = '127.0.0.1', extraHeaders = {}) { return new Promise((resolve, reject) => { const req = http.request({ host, port, method, path: urlPath, timeout: 15000, headers: { ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}), ...extraHeaders, }, }, (res) => { const chunks = []; res.on('data', (c) => chunks.push(c)); res.on('end', () => resolve({ status: res.statusCode, headers: res.headers, text: Buffer.concat(chunks).toString('utf8'), json: () => JSON.parse(Buffer.concat(chunks).toString('utf8')), })); }); req.on('error', reject); req.on('timeout', () => req.destroy(new Error('timeout'))); if (body !== undefined) req.write(JSON.stringify(body)); req.end(); }); } /** 取本机第一个非内部 IPv4 局域网地址(用于访问口令测试;回环地址在服务端直接放行) */ function getLanIpv4() { for (const list of Object.values(os.networkInterfaces())) { for (const ni of list || []) { if (ni.family === 'IPv4' && !ni.internal) return ni.address; } } return ''; } const results = []; async function check(name, fn) { try { await fn(); results.push({ name, ok: true }); } catch (e) { results.push({ name, ok: false, error: e.message }); } } function expect(cond, msg) { if (!cond) throw new Error(msg); } async function waitForHealth(port, timeoutMs = 20000) { const start = Date.now(); for (;;) { try { const res = await request(port, 'GET', '/healthz'); if (res.status === 200) return; } catch { /* not up yet */ } if (Date.now() - start > timeoutMs) throw new Error('服务未在超时内启动'); await new Promise((r) => setTimeout(r, 300)); } } function waitForHealthLan(lanIp, port, timeoutMs = 20000) { return new Promise((resolve, reject) => { const start = Date.now(); const probe = () => { const req = http.request({ host: lanIp, port, path: '/healthz', timeout: 3000 }, (res) => { res.resume(); if (res.statusCode === 200) return resolve(); retry(); }); req.on('error', retry); req.on('timeout', () => { req.destroy(); retry(); }); req.end(); }; const retry = () => { if (Date.now() - start > timeoutMs) return reject(new Error('服务未在超时内启动(LAN)')); setTimeout(probe, 300); }; probe(); }); } function enc(s) { return encodeURIComponent(s); } async function main() { const port = await pickPort(); const child = spawn(process.execPath, [path.join(ROOT, 'server.js')], { env: { ...process.env, PORT: String(port), HOST: '127.0.0.1' }, stdio: ['ignore', 'pipe', 'pipe'], }); let bootLog = ''; child.stdout.on('data', (c) => { bootLog += c; }); child.stderr.on('data', (c) => { bootLog += c; }); try { await waitForHealth(port); let workspaceRoot = ''; { const res = await request(port, 'GET', '/api/default-root'); workspaceRoot = res.json().root; } const encRoot = enc(workspaceRoot); const b64Root = Buffer.from(workspaceRoot, 'utf8').toString('base64url'); // ---- 基础与健康 ---- await check('GET /healthz', async () => { const r = await request(port, 'GET', '/healthz'); expect(r.status === 200 && r.json().ok === true, `status=${r.status}`); }); await check('GET /readyz', async () => { const r = await request(port, 'GET', '/readyz'); const j = r.json(); expect(r.status === 200 && typeof j.ok === 'boolean' && typeof j.workspaceReady === 'boolean', `status=${r.status}`); }); await check('GET /api/default-root', async () => { const r = await request(port, 'GET', '/api/default-root'); expect(r.status === 200 && !!r.json().root, `status=${r.status}`); }); // ---- 设置域 ---- await check('GET /api/models', async () => { const r = await request(port, 'GET', '/api/models'); const j = r.json(); expect(r.status === 200 && Array.isArray(j.models) && j.models.length >= 5 && !!j.default, `status=${r.status}`); }); await check('GET /api/settings(脱敏)', async () => { const r = await request(port, 'GET', '/api/settings'); const j = r.json(); expect(r.status === 200 && typeof j.rootDir === 'string' && typeof j.port === 'number', `status=${r.status}`); expect(!j.apiKey || j.apiKey.includes('••••'), 'apiKey 未脱敏'); }); await check('POST /api/settings 非法 pageSize → 400', async () => { const r = await request(port, 'POST', '/api/settings', { pageSize: 9999 }); expect(r.status === 400, `status=${r.status}`); }); await check('POST /api/settings 非法 delayMs → 400', async () => { const r = await request(port, 'POST', '/api/settings', { delayMs: -5 }); expect(r.status === 400, `status=${r.status}`); }); await check('POST /api/settings 非法 autoSyncTime → 400', async () => { const r = await request(port, 'POST', '/api/settings', { autoSyncTime: '99:99' }); expect(r.status === 400, `status=${r.status}`); }); await check('GET /api/settings 含 autoSync 字段', async () => { const r = await request(port, 'GET', '/api/settings'); const j = r.json(); expect(typeof j.autoSyncEnabled === 'boolean' && /^\d{1,2}:\d{2}$/.test(j.autoSyncTime || ''), 'autoSync 字段缺失'); }); // ---- 采集域 ---- await check('GET /api/session', async () => { const r = await request(port, 'GET', '/api/session'); expect(r.status === 200 && typeof r.json().cookieConfigured === 'boolean', `status=${r.status}`); }); await check('GET /api/shops(base64url root)', async () => { const r = await request(port, 'GET', `/api/shops?root=${b64Root}`); expect(r.status === 200 && Array.isArray(r.json().shops), `status=${r.status}`); }); await check('GET /api/shops 非法 root → 403', async () => { const bad = Buffer.from('/tmp/elsewhere', 'utf8').toString('base64url'); const r = await request(port, 'GET', `/api/shops?root=${bad}`); expect(r.status === 403, `status=${r.status}`); }); await check('GET /api/scan', async () => { const r = await request(port, 'GET', `/api/scan?root=${encRoot}`); const j = r.json(); expect(r.status === 200 && Array.isArray(j.shops), `status=${r.status}`); }); await check('GET /api/crawl/status', async () => { const r = await request(port, 'GET', '/api/crawl/status'); expect(r.status === 200 && typeof r.json().status === 'string', `status=${r.status}`); }); await check('POST /api/crawl/stop(空操作)', async () => { const r = await request(port, 'POST', '/api/crawl/stop'); expect(r.status === 200 && r.json().ok === true, `status=${r.status}`); }); // ---- 生成/任务/历史域 ---- await check('GET /api/tasks', async () => { const r = await request(port, 'GET', '/api/tasks'); const j = r.json(); expect(r.status === 200 && j.summary && Array.isArray(j.tasks), `status=${r.status}`); }); await check('GET /api/job/未知ID → 404', async () => { const r = await request(port, 'GET', '/api/job/00000000-0000-0000-0000-000000000000'); expect(r.status === 404, `status=${r.status}`); }); await check('GET /api/creations', async () => { const r = await request(port, 'GET', `/api/creations?root=${encRoot}&limit=5`); const j = r.json(); expect(r.status === 200 && typeof j.total === 'number' && Array.isArray(j.items), `status=${r.status}`); }); await check('GET /files 越界 → 403', async () => { const r = await request(port, 'GET', `/files?path=${enc('/etc/passwd')}`); expect(r.status === 403, `status=${r.status}`); }); await check('GET /files 缺参 → 400', async () => { const r = await request(port, 'GET', '/files'); expect(r.status === 400, `status=${r.status}`); }); // ---- 日志 ---- await check('GET /api/logs', async () => { const r = await request(port, 'GET', '/api/logs'); expect(r.status === 200 && Array.isArray(r.json().logs), `status=${r.status}`); }); await check('POST /api/logs/clear', async () => { const r = await request(port, 'POST', '/api/logs/clear'); expect(r.status === 200 && r.json().ok === true, `status=${r.status}`); }); // ---- 数据报表域 ---- await check('GET /api/reports/status(不限方法 GET)', async () => { const r = await request(port, 'GET', '/api/reports/status'); expect(r.status === 200 && r.json().napos, `status=${r.status}`); }); await check('POST /api/reports/status(不限方法 POST)', async () => { const r = await request(port, 'POST', '/api/reports/status'); expect(r.status === 200, `status=${r.status}`); }); await check('GET /api/reports/columns', async () => { const r = await request(port, 'GET', '/api/reports/columns'); expect(r.status === 200 && Array.isArray(r.json().columns) && r.json().columns.length >= 10, `status=${r.status}`); }); await check('GET /api/reports/entities', async () => { const r = await request(port, 'GET', '/api/reports/entities'); expect(r.status === 200 && Array.isArray(r.json().entities), `status=${r.status}`); }); await check('GET /api/reports/sync/status', async () => { const r = await request(port, 'GET', '/api/reports/sync/status'); expect(r.status === 200 && typeof r.json().running === 'boolean', `status=${r.status}`); }); await check('GET /api/reports/syncs', async () => { const r = await request(port, 'GET', '/api/reports/syncs'); expect(r.status === 200 && Array.isArray(r.json().runs), `status=${r.status}`); }); await check('GET /api/reports/daily', async () => { const r = await request(port, 'GET', '/api/reports/daily'); expect(r.status === 200, `status=${r.status}`); }); await check('GET /api/reports/aggregate', async () => { const r = await request(port, 'GET', '/api/reports/aggregate'); expect(r.status === 200, `status=${r.status}`); }); await check('GET /api/reports/export → CSV', async () => { const r = await request(port, 'GET', '/api/reports/export'); expect(r.status === 200 && /text\/csv/.test(r.headers['content-type'] || ''), `status=${r.status}`); }); await check('GET /api/reports/report-html → HTML', async () => { const r = await request(port, 'GET', '/api/reports/report-html'); expect(r.status === 200 && /text\/html/.test(r.headers['content-type'] || ''), `status=${r.status}`); }); await check('POST /api/reports/ask(有主体时跳过)', async () => { const ent = await request(port, 'GET', '/api/reports/entities'); if (ent.json().entities.length > 0) return; // 有主体会真调对话模型,跳过 const r = await request(port, 'POST', '/api/reports/ask', { q: 'test' }); expect(r.status === 400 && r.json().code === 'ASK_NO_ENTITY', `status=${r.status}`); }); await check('GET /api/reports/lake → 湖全景', async () => { const r = await request(port, 'GET', '/api/reports/lake'); const d = r.json(); expect(r.status === 200 && Array.isArray(d.entities) && typeof d.totalRows === 'number' && d.aitable && 'enabled' in d.aitable, `status=${r.status}`); }); await check('GET /api/reports/daily → 分页字段', async () => { const r = await request(port, 'GET', '/api/reports/daily'); const d = r.json(); expect(r.status === 200 && typeof d.total === 'number' && d.page >= 1 && d.pageSize >= 50 && d.pages >= 1 && 'summary' in d, `status=${r.status}`); }); await check('GET /api/reports/syncs → 同步历史', async () => { const r = await request(port, 'GET', '/api/reports/syncs'); expect(r.status === 200 && Array.isArray(r.json().runs), `status=${r.status}`); }); await check('POST /api/reports/aitable/push(无主体 → 400)', async () => { const r = await request(port, 'POST', '/api/reports/aitable/push', {}); expect(r.status === 400 && r.json().code === 'AITABLE_NO_ENTITY', `status=${r.status} code=${r.json().code}`); }); // ---- 钉钉三表拆分与行键(纯函数回归,不打真实钉钉 API) ---- const aitableLib = require('../server/services/aitable.js'); const { rowKey } = require('../reports-store.js'); await check('splitRowsBySheet → 按维度分流且 summary 不上云', async () => { const s = aitableLib.splitRowsBySheet([ { scope: 'shop', shopId: '1', chainId: 'c1' }, { scope: 'shop', shopId: '2', chainId: 'c1' }, { scope: 'chain', shopId: '', chainId: 'c1' }, { scope: 'chain', shopId: '', chainId: 'c2' }, { scope: 'company', shopId: '', chainId: '-1' }, { scope: 'summary', shopId: '', chainId: '' }, ]); expect(s.detail.length === 2 && s.chain.length === 2 && s.company.length === 1 && s.skippedSummary === 1, `detail=${s.detail.length} chain=${s.chain.length} company=${s.company.length} skipped=${s.skippedSummary}`); }); await check('recordKey / rowKey → 同日多连锁不碰撞', async () => { const cloud = (chainId) => aitableLib.recordKey({ 日期: '20261017', 账号名: 'A', 维度: '连锁', 连锁ID: chainId, 门店ID: '' }); expect(cloud('c1') !== cloud('c2'), '连锁行云端业务键仍碰撞'); const local = (row) => rowKey(row); expect(local({ date: '20261017', scope: 'chain', chainId: 'c1', shopId: '' }) !== local({ date: '20261017', scope: 'chain', chainId: 'c2', shopId: '' }), '连锁行本地行键仍碰撞'); expect(local({ date: '20261017', scope: 'summary', chainId: '', shopId: '' }) !== local({ date: '20261017', scope: 'summary', chainId: 'c1', shopId: '' }), '汇总行本地行键仍碰撞'); }); await check('resolveSheets → 缺省兜底三表名', async () => { const { sheets, fingerprint } = aitableLib.resolveSheets({}); expect(sheets.detail === '门店明细' && sheets.chain === '连锁汇总' && sheets.company === '公司汇总' && fingerprint.split('|').length === 3, JSON.stringify(sheets)); }); // ---- 问数两段式:结果摘要与兜底话术(纯函数,不调对话模型) ---- const nlQueryLib = require('../nl-query.js'); await check('buildResultDigest → 口径/Top行/合计齐全且按列类型格式化', async () => { const digest = nlQueryLib.buildResultDigest( { mode: 'shop', from: '20260918', to: '20261017', metrics: ['grossAmt', 'clkExpRate'], shopIds: [] }, { rows: [ { shopName: '一食堂', grossAmt: 12345.6, clkExpRate: 0.0521 }, { shopName: '二食堂', grossAmt: 0, clkExpRate: 0 }, ], summary: { grossAmt: 12345.6, clkExpRate: 0.0521 }, } ); expect(digest.includes('按门店汇总') && digest.includes('2026-09-18') && digest.includes('2026-10-17'), digest); expect(digest.includes('12,345.60元') && digest.includes('5.21%'), digest); // money 两位小数 / pct 小数×100 expect(digest.includes('区间合计') && digest.includes('均值'), digest); // 金额=合计、比率=均值 }); await check('buildResultDigest → 空结果有明确说明', async () => { const digest = nlQueryLib.buildResultDigest( { mode: 'date', from: '20260918', to: '20261017', metrics: ['grossAmt'], shopIds: [] }, { rows: [], summary: {} } ); expect(digest.includes('结果为空'), digest); }); await check('fallbackReply → 模板句含区间与指标名', async () => { const t = nlQueryLib.fallbackReply({ mode: 'date', from: '20260918', to: '20261017', metrics: ['grossAmt'] }); expect(t.includes('2026-09-18') && t.includes('营业额'), t); }); // ---- 静态与路由行为 ---- 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']) { await check(`静态 ${p}`, async () => { const r = await request(port, 'GET', p); expect(r.status === 200, `status=${r.status}`); }); } await check('GET /no-such-path → 404', async () => { const r = await request(port, 'GET', '/no-such-path'); expect(r.status === 404, `status=${r.status}`); }); await check('DELETE /api/settings → 405', async () => { const r = await request(port, 'DELETE', '/api/settings'); expect(r.status === 405, `status=${r.status}`); }); // ---- 优雅停机(SIGTERM 后应自行退出) ---- await check('SIGTERM 优雅停机', async () => { process.kill(child.pid, 'SIGTERM'); const exited = await new Promise((resolve) => { const t = setTimeout(() => resolve(false), 8000); child.on('exit', () => { clearTimeout(t); resolve(true); }); }); expect(exited, 'SIGTERM 后 8 秒内未退出'); }); // ---- 访问口令(需局域网 IP:本机回环地址在守卫中直接放行,测不到 401) ---- const lanIp = getLanIpv4(); if (!lanIp) { results.push({ name: '访问口令(无局域网 IP,跳过)', ok: true }); } else { const tport = await pickPort(); const tchild = spawn(process.execPath, [path.join(ROOT, 'server.js')], { env: { ...process.env, PORT: String(tport), HOST: '0.0.0.0', ACCESS_TOKEN: 'smoke-token' }, stdio: ['ignore', 'ignore', 'ignore'], }); try { await waitForHealthLan(lanIp, tport); await check('口令开启:无口令访问 API → 401', async () => { const r = await request(tport, 'GET', '/api/models', undefined, lanIp); expect(r.status === 401, `status=${r.status}`); }); await check('口令开启:?token= 通过', async () => { const r = await request(tport, 'GET', `/api/models?token=smoke-token`, undefined, lanIp); expect(r.status === 200, `status=${r.status}`); }); await check('口令开启:X-Access-Token 头通过', async () => { const r = await request(tport, 'GET', '/api/models', undefined, lanIp, { 'X-Access-Token': 'smoke-token' }); expect(r.status === 200, `status=${r.status}`); }); await check('口令开启:cookie 通过', async () => { const r = await request(tport, 'GET', '/api/models', undefined, lanIp, { Cookie: 'accessToken=smoke-token' }); expect(r.status === 200, `status=${r.status}`); }); await check('口令开启:/healthz 与静态页放行', async () => { const h = await request(tport, 'GET', '/healthz', undefined, lanIp); const s = await request(tport, 'GET', '/', undefined, lanIp); expect(h.status === 200 && s.status === 200, `healthz=${h.status} static=${s.status}`); }); await check('口令开启:错误口令 → 401', async () => { const r = await request(tport, 'GET', '/api/models?token=wrong', undefined, lanIp); expect(r.status === 401, `status=${r.status}`); }); } finally { try { tchild.kill('SIGKILL'); } catch { /* ignore */ } } } } catch (e) { results.push({ name: '(测试执行中断)', ok: false, error: e.message }); } finally { try { child.kill('SIGKILL'); } catch { /* already dead */ } } const failed = results.filter((r) => !r.ok); console.log('\n========== 冒烟测试结果 =========='); for (const r of results) { console.log(`${r.ok ? '✓' : '✗'} ${r.name}${r.ok ? '' : ' ← ' + r.error}`); } console.log(`\n通过 ${results.length - failed.length}/${results.length}`); if (failed.length) { console.log('\n服务启动日志(末尾 500 字):'); console.log(bootLog.slice(-500)); process.exit(1); } } main().catch((e) => { console.error('冒烟脚本自身异常:', e); process.exit(1); });