|
@@ -0,0 +1,292 @@
|
|
|
|
|
+#!/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 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) {
|
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
|
+ const req = http.request({
|
|
|
|
|
+ host: '127.0.0.1', port, method, path: urlPath, timeout: 15000,
|
|
|
|
|
+ headers: body !== undefined ? { 'Content-Type': 'application/json' } : {},
|
|
|
|
|
+ }, (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();
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+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 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('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}`);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // ---- 静态与路由行为 ----
|
|
|
|
|
+ for (const p of ['/', '/index.html', '/app.js', '/crawl.js', '/gen.js', '/history.js', '/tasks.js', '/reports.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 秒内未退出');
|
|
|
|
|
+ });
|
|
|
|
|
+ } 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);
|
|
|
|
|
+});
|