| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- #!/usr/bin/env node
- /**
- * 端到端联调测试:真实启动 Python 工具服务 + BFF,跑完整业务链路。
- *
- * node scripts/e2e.mjs # 数据面必测;有 DEEPSEEK_API_KEY 时自动加上对话测试
- * node scripts/e2e.mjs --no-chat # 只测数据面
- * node scripts/e2e.mjs --keep # 结束后保留测试数据目录,便于排查
- *
- * 覆盖:
- * 1. 两个服务健康检查
- * 2. 建工作区 → 上传 samples/small.csv → 启动处理 → 轮询进度 → 23 张结果表
- * 3. 对象/表清单、预览、行级血缘、文件下载
- * 4. 真实模型对话(有 key 时):SSE 事件、工具调用、数字核验结论
- * 5. 台账(哈希链)与自定义聚合
- */
- import { spawn, spawnSync } from 'node:child_process';
- import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
- import path from 'node:path';
- const ROOT = path.resolve(import.meta.dirname, '..');
- const RUN = path.join(ROOT, '.run', 'e2e');
- const DATA = path.join(RUN, 'data');
- const BFF_DATA = path.join(RUN, 'bff');
- const TOOLS_PORT = 18788;
- const BFF_PORT = 18787;
- const TOOLS_URL = `http://127.0.0.1:${TOOLS_PORT}`;
- const BFF_URL = `http://127.0.0.1:${BFF_PORT}`;
- const args = new Set(process.argv.slice(2));
- const C = { reset: '\u001b[0m', dim: '\u001b[2m', green: '\u001b[32m', red: '\u001b[31m', cyan: '\u001b[36m', yellow: '\u001b[33m', bold: '\u001b[1m' };
- let failures = 0;
- let checks = 0;
- const pass = (text) => {
- checks += 1;
- console.log(` ${C.green}✓${C.reset} ${text}`);
- };
- const failed = (text, detail) => {
- checks += 1;
- failures += 1;
- console.log(` ${C.red}✗ ${text}${C.reset}`);
- if (detail !== undefined) console.log(` ${C.dim}${typeof detail === 'string' ? detail : JSON.stringify(detail).slice(0, 800)}${C.reset}`);
- };
- const skip = (text) => console.log(` ${C.yellow}○${C.reset} ${C.dim}${text}${C.reset}`);
- const section = (text) => console.log(`\n${C.cyan}${C.bold}${text}${C.reset}`);
- function check(condition, text, detail) {
- if (condition) pass(text);
- else failed(text, detail);
- return Boolean(condition);
- }
- function loadDotEnv() {
- const file = path.join(ROOT, '.env');
- if (!existsSync(file)) return;
- for (const line of readFileSync(file, 'utf8').split(/\r?\n/)) {
- const trimmed = line.trim();
- if (!trimmed || trimmed.startsWith('#')) continue;
- const index = trimmed.indexOf('=');
- if (index <= 0) continue;
- const key = trimmed.slice(0, index).trim();
- const value = trimmed.slice(index + 1).trim().replace(/^["']|["']$/g, '');
- if (process.env[key] === undefined) process.env[key] = value;
- }
- }
- const children = [];
- function startProcess(label, command, argv, env) {
- const child = spawn(command, argv, {
- cwd: ROOT,
- env: { ...process.env, ...env },
- stdio: ['ignore', 'pipe', 'pipe'],
- });
- const log = [];
- child.stdout.on('data', (chunk) => log.push(chunk.toString()));
- child.stderr.on('data', (chunk) => log.push(chunk.toString()));
- child.on('exit', (code) => {
- if (code !== 0 && code !== null) {
- console.log(`${C.dim}[${label}] 退出 code=${code}${C.reset}`);
- }
- });
- children.push({ label, child, log });
- return child;
- }
- function stopAll() {
- for (const { child } of children.reverse()) {
- try {
- if (process.platform === 'win32') {
- spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore' });
- } else {
- child.kill('SIGKILL');
- }
- } catch {
- /* 忽略 */
- }
- }
- }
- async function waitFor(url, timeoutMs, label) {
- const deadline = Date.now() + timeoutMs;
- while (Date.now() < deadline) {
- try {
- const response = await fetch(url, { signal: AbortSignal.timeout(2000) });
- if (response.ok) return true;
- } catch {
- /* 还没起来 */
- }
- await new Promise((resolve) => setTimeout(resolve, 400));
- }
- for (const item of children) {
- if (item.log.length) console.log(`${C.dim}--- ${item.label} 输出 ---\n${item.log.join('').slice(-3000)}${C.reset}`);
- }
- failed(`${label} 未在 ${timeoutMs / 1000}s 内就绪:${url}`);
- return false;
- }
- async function api(pathname, init, base = BFF_URL) {
- const response = await fetch(`${base}${pathname}`, init);
- const text = await response.text();
- let payload = null;
- try {
- payload = text ? JSON.parse(text) : null;
- } catch {
- payload = text;
- }
- return { ok: response.ok, status: response.status, payload };
- }
- async function sse(url, body) {
- const response = await fetch(url, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(body),
- });
- if (!response.ok) throw new Error(`SSE 请求失败 HTTP ${response.status}: ${await response.text()}`);
- const text = await response.text();
- const events = [];
- for (const block of text.split('\n\n')) {
- let event = 'message';
- const data = [];
- for (const line of block.split('\n')) {
- if (line.startsWith('event:')) event = line.slice(6).trim();
- else if (line.startsWith('data:')) data.push(line.slice(5).trim());
- }
- if (data.length === 0) continue;
- try {
- events.push({ event, data: JSON.parse(data.join('\n')) });
- } catch {
- events.push({ event, data: data.join('\n') });
- }
- }
- return events;
- }
- function killPortHolders(ports) {
- if (process.platform !== 'win32') return;
- const result = spawnSync('netstat', ['-ano'], { encoding: 'utf8' });
- if (result.status !== 0 || !result.stdout) return;
- const pids = new Set();
- for (const line of result.stdout.split(/\r?\n/)) {
- if (!line.includes('LISTENING')) continue;
- for (const port of ports) {
- if (!line.includes(`:${port}`)) continue;
- const pid = line.trim().split(/\s+/).pop();
- if (pid && pid !== '0') pids.add(pid);
- }
- }
- for (const pid of pids) {
- console.log(`${C.dim}清理占用测试端口的进程 pid=${pid}${C.reset}`);
- spawnSync('taskkill', ['/pid', pid, '/T', '/F'], { stdio: 'ignore' });
- }
- }
- async function main() {
- console.log(C.bold + '\n海关问数 Agent · 端到端联调测试\n' + C.reset);
- loadDotEnv();
- // 上一轮异常退出可能留下占用端口的进程,先清理,避免"服务未就绪"的假失败
- killPortHolders([TOOLS_PORT, BFF_PORT]);
- await new Promise((resolve) => setTimeout(resolve, 500));
- try {
- rmSync(RUN, { recursive: true, force: true });
- } catch (error) {
- console.log(`${C.yellow}清理测试目录失败(可能有进程占用):${error.message}${C.reset}`);
- }
- mkdirSync(DATA, { recursive: true });
- mkdirSync(BFF_DATA, { recursive: true });
- const python = process.env.CDA_PYTHON ?? (process.platform === 'win32' ? 'python' : 'python3');
- const hasKey = Boolean(process.env.DEEPSEEK_API_KEY);
- section('1. 启动服务');
- startProcess('tools', python, ['-m', 'toolsvc'], {
- cwd: path.join(ROOT, 'tools-service'),
- PYTHONPATH: path.join(ROOT, 'tools-service'),
- CDA_DATA_DIR: DATA,
- CDA_TOOLS_PORT: String(TOOLS_PORT),
- CDA_LEDGER_JSONL: '1',
- });
- if (!(await waitFor(`${TOOLS_URL}/health`, 60_000, '工具服务'))) return finish();
- startProcess('bff', process.execPath, [path.join(ROOT, 'bff', 'dist', 'src', 'index.js')], {
- CDA_TOOLS_URL: TOOLS_URL,
- CDA_PORT: String(BFF_PORT),
- CDA_BFF_DATA_DIR: BFF_DATA,
- CDA_WEB_DIST: path.join(ROOT, 'web', 'dist'),
- DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'replay-placeholder',
- });
- if (!(await waitFor(`${BFF_URL}/api/health`, 60_000, 'BFF'))) return finish();
- pass('工具服务与 BFF 均已就绪');
- section('2. 健康检查');
- const health = await api('/api/health');
- check(health.ok && health.payload.tools?.ok === true, 'GET /api/health 报告工具服务在线', health.payload);
- check(health.payload.harness === 'embedded-dsh-compatible', 'Harness 运行时为内嵌实现', health.payload.harness);
- section('3. 工作区与文件上传');
- const created = await api('/api/workspaces', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ name: 'E2E 测试工作区', description: '端到端联调' }),
- });
- check(created.ok, '创建工作区', created.payload);
- const workspaceId = created.payload?.id;
- const samplePath = path.join(ROOT, 'samples', 'small.csv');
- if (!existsSync(samplePath)) {
- failed('缺少 samples/small.csv,请先运行 python scripts/gen_sample_data.py');
- return finish();
- }
- const csv = readFileSync(samplePath);
- const upload = await fetch(`${BFF_URL}/api/workspaces/${workspaceId}/files`, {
- method: 'POST',
- headers: { 'Content-Type': 'application/octet-stream', 'X-Filename': encodeURIComponent('small.csv') },
- body: csv,
- });
- const uploaded = await upload.json();
- check(upload.ok, `上传原始数据(${Math.round(csv.length / 1024)} KB)`, uploaded);
- check(uploaded.rows > 1000, `原始数据索引了 ${uploaded.rows} 行`);
- check(uploaded.sha256?.startsWith('sha256:'), '原始数据带 SHA256 指纹');
- section('4. 数据处理(16 张模板表 + 7 张补充表)');
- const run = await api('/api/process', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- workspace_id: workspaceId,
- input_object_id: uploaded.id,
- operation: 'full_pipeline',
- options: { start_year: 2025, end_year: 2026 },
- output_name: 'E2E统计结果.xlsx',
- }),
- });
- check(run.ok, '创建处理任务', run.payload);
- const runId = run.payload?.id;
- // 进度用 SSE 观察(契约要求的形态),顺便统计 progress 事件数量
- const streamResponse = await fetch(`${BFF_URL}/api/process/${runId}/stream`);
- check(streamResponse.ok, '进度 SSE 端点可用(text/event-stream)');
- const streamText = await streamResponse.text();
- const progressCount = (streamText.match(/event: progress/g) || []).length;
- const doneMatch = /event: done\ndata: (.*)/.exec(streamText);
- const done = doneMatch ? JSON.parse(doneMatch[1]) : null;
- check(progressCount >= 2, `通过 SSE 收到 ${progressCount} 个进度事件`);
- check(done?.status === 'succeeded', `SSE 结束事件 status=${done?.status}`);
- const current = done;
- const statId = current?.object_id;
- const statObject = (await api(`/api/objects/${statId}`)).payload;
- check(statObject?.tables?.length === 25, `生成 ${statObject?.tables?.length} 张结果表`, statObject?.tables?.map((t) => t.name));
- check(statObject?.parents?.includes(uploaded.id), '结果对象血缘指向原始数据');
- check(Boolean(statObject?.meta?.lineage?.output_hash), '结果对象带输出哈希与参数哈希');
- section('5. 数据读取与血缘');
- const tableName = '1.0-3.0_MLCC';
- const preview = await api(`/api/objects/${statId}/preview?table=${encodeURIComponent(tableName)}&limit=3`);
- check(preview.ok && preview.payload.rows.length === 3, `预览 ${tableName} 前 3 行`, preview.payload);
- const lineage = await api(`/api/objects/${statId}/lineage?table=${encodeURIComponent(tableName)}&row=0`);
- check(lineage.ok && lineage.payload.source_object_id === uploaded.id, '第 0 行可下钻到原始数据(行级血缘)', lineage.payload);
- // 溯源 chip 点开时用的"按行取数"
- const rowDetail = await api(`/api/objects/${statId}/row?table=${encodeURIComponent(tableName)}&row=0`);
- check(
- rowDetail.ok && rowDetail.payload.row_hash?.startsWith('sha256:') && rowDetail.payload.values?.['基础类目'],
- '按行取数返回原文 + 行哈希(溯源抽屉)',
- rowDetail.payload,
- );
- check(
- Boolean(rowDetail.payload?.lineage?.source_rows),
- '该行带下钻谓词(可回到原始记录范围)',
- rowDetail.payload?.lineage,
- );
- const rowMissing = await fetch(`${BFF_URL}/api/objects/${statId}/row?table=${encodeURIComponent(tableName)}&row=99999`);
- check(rowMissing.status === 404, '越界行号返回 404(不 500)');
- const download = await fetch(`${BFF_URL}/api/objects/${statId}/download?name=${encodeURIComponent('统计结果.xlsx')}`);
- const buffer = Buffer.from(await download.arrayBuffer());
- check(download.ok && buffer.subarray(0, 2).toString() === 'PK', `下载 统计结果.xlsx(${Math.round(buffer.length / 1024)} KB)`);
- section('6. 指数口径校验(基期必须为 1000)');
- const baseRows = await api(`/api/objects/${statId}/preview?table=${encodeURIComponent(tableName)}&limit=50`);
- const mlcc2025 = baseRows.payload?.rows?.filter((row) => String(row['基础类目'] ?? '').includes('MLCC') && String(row['年度']) === '2025' && String(row['月度']) === '1');
- const valueIndex = mlcc2025?.[0]?.['金额指数'];
- check(valueIndex !== undefined && Math.abs(Number(valueIndex) - 1000) < 1e-6, `2025-01 金额指数 = ${valueIndex}(应为 1000)`);
- section('7. 自定义聚合');
- const custom = await api('/api/process', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- workspace_id: workspaceId,
- input_object_id: uploaded.id,
- operation: 'custom_aggregate',
- options: { group_by: ['c1', 'c2'], directions: ['I'] },
- output_name: 'E2E自定义聚合',
- }),
- });
- const customRunId = custom.payload?.id;
- let customState = null;
- const customDeadline = Date.now() + 180_000;
- while (Date.now() < customDeadline) {
- const state = await api(`/api/process/${customRunId}`);
- customState = state.payload;
- if (customState.status === 'succeeded' || customState.status === 'failed') break;
- await new Promise((resolve) => setTimeout(resolve, 500));
- }
- check(customState?.status === 'succeeded', '自定义聚合任务成功', customState?.error);
- section('8. 台账');
- const ledger = await api('/api/ledger/stats');
- check(ledger.payload?.tools?.chain_ok === true, '工具服务台账哈希链完整', ledger.payload);
- section('9. Agent 对话');
- const wantChat = !args.has('--no-chat') && hasKey;
- if (!wantChat) {
- skip(hasKey ? '按要求跳过对话测试(--no-chat)' : '未配置 DEEPSEEK_API_KEY,跳过真实模型对话测试');
- } else {
- const session = await api('/api/sessions', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ workspace_id: workspaceId }),
- });
- check(session.ok, '创建会话', session.payload);
- const question = '只查一个数:统计结果里 2025 年 1 月「多层瓷介电容器(MLCC)」的金额指数是多少?请只回答这一个数字。';
- const events = await sse(`${BFF_URL}/api/sessions/${session.payload.id}/chat`, { message: question });
- const names = events.map((item) => item.event);
- check(names.includes('token'), 'SSE 至少推送一个 token 事件');
- check(names.includes('tool_call'), '模型调用了工具');
- const verification = events.find((item) => item.event === 'verification')?.data;
- const final = events.find((item) => item.event === 'final')?.data;
- check(Boolean(verification), '产出核验结论');
- // 模型的输出是随机的:这里断言"机制正确"(三种裁决都可能,且交付与否必须与裁决一致),
- // 而不是断言模型一定不编数字——后者属于模型质量问题,不是平台回归。
- const verdict = verification?.verdict;
- check(
- verdict === 'accept' || verdict === 'flag' || verdict === 'reject',
- `核验给出明确裁决:${verdict}(${verification?.summary})`,
- verification?.summary,
- );
- check(
- final?.delivered === (verdict !== 'reject'),
- verdict === 'reject' ? '核验未通过时拒绝交付(符合设计)' : '核验通过后交付答案',
- { verdict, delivered: final?.delivered },
- );
- if (final?.text) {
- console.log(`${C.dim} 回答摘要:${String(final.text).replace(/\n/g, ' ').slice(0, 160)}…${C.reset}`);
- }
- const history = await api(`/api/sessions/${session.payload.id}/messages`);
- check(history.payload?.items?.length === 2, '会话持久化了 user + assistant 两条消息');
- const sessionLedger = await api(`/api/sessions/${session.payload.id}/ledger`);
- check((sessionLedger.payload?.calls?.length ?? 0) > 0, '会话台账记录了工具调用');
- }
- section('10. 前端静态资源');
- const index = await fetch(`${BFF_URL}/`);
- const html = await index.text();
- check(index.ok && html.includes('<div id="root">'), 'BFF 已托管前端页面');
- const spa = await fetch(`${BFF_URL}/some/deep/route`);
- check(spa.ok, 'SPA 路由回退到 index.html');
- return finish();
- }
- function finish() {
- stopAll();
- console.log('');
- if (failures === 0) {
- console.log(`${C.green}${C.bold}全部 ${checks} 项检查通过${C.reset}`);
- } else {
- console.log(`${C.red}${C.bold}${failures} / ${checks} 项检查失败${C.reset}`);
- }
- if (!args.has('--keep') && failures === 0) {
- try {
- rmSync(RUN, { recursive: true, force: true });
- } catch {
- /* 忽略 */
- }
- } else {
- console.log(`${C.dim}测试数据保留在 ${RUN}${C.reset}`);
- }
- setTimeout(() => process.exit(failures === 0 ? 0 : 1), 200);
- return failures === 0 ? 0 : 1;
- }
- process.on('SIGINT', () => {
- stopAll();
- process.exit(130);
- });
- main().catch((error) => {
- console.error(error);
- failed('端到端测试异常', error?.stack ?? String(error));
- finish();
- });
|