| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- #!/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}`);
- });
- // ---- 静态与路由行为 ----
- 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);
- });
|