e2e.mjs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #!/usr/bin/env node
  2. /**
  3. * 端到端联调测试:真实启动 Python 工具服务 + BFF,跑完整业务链路。
  4. *
  5. * node scripts/e2e.mjs # 数据面必测;有 DEEPSEEK_API_KEY 时自动加上对话测试
  6. * node scripts/e2e.mjs --no-chat # 只测数据面
  7. * node scripts/e2e.mjs --keep # 结束后保留测试数据目录,便于排查
  8. *
  9. * 覆盖:
  10. * 1. 两个服务健康检查
  11. * 2. 建工作区 → 上传 samples/small.csv → 启动处理 → 轮询进度 → 23 张结果表
  12. * 3. 对象/表清单、预览、行级血缘、文件下载
  13. * 4. 真实模型对话(有 key 时):SSE 事件、工具调用、数字核验结论
  14. * 5. 台账(哈希链)与自定义聚合
  15. */
  16. import { spawn, spawnSync } from 'node:child_process';
  17. import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  18. import path from 'node:path';
  19. const ROOT = path.resolve(import.meta.dirname, '..');
  20. const RUN = path.join(ROOT, '.run', 'e2e');
  21. const DATA = path.join(RUN, 'data');
  22. const BFF_DATA = path.join(RUN, 'bff');
  23. const TOOLS_PORT = 18788;
  24. const BFF_PORT = 18787;
  25. const TOOLS_URL = `http://127.0.0.1:${TOOLS_PORT}`;
  26. const BFF_URL = `http://127.0.0.1:${BFF_PORT}`;
  27. const args = new Set(process.argv.slice(2));
  28. const C = { reset: '\u001b[0m', dim: '\u001b[2m', green: '\u001b[32m', red: '\u001b[31m', cyan: '\u001b[36m', yellow: '\u001b[33m', bold: '\u001b[1m' };
  29. let failures = 0;
  30. let checks = 0;
  31. const pass = (text) => {
  32. checks += 1;
  33. console.log(` ${C.green}✓${C.reset} ${text}`);
  34. };
  35. const failed = (text, detail) => {
  36. checks += 1;
  37. failures += 1;
  38. console.log(` ${C.red}✗ ${text}${C.reset}`);
  39. if (detail !== undefined) console.log(` ${C.dim}${typeof detail === 'string' ? detail : JSON.stringify(detail).slice(0, 800)}${C.reset}`);
  40. };
  41. const skip = (text) => console.log(` ${C.yellow}○${C.reset} ${C.dim}${text}${C.reset}`);
  42. const section = (text) => console.log(`\n${C.cyan}${C.bold}${text}${C.reset}`);
  43. function check(condition, text, detail) {
  44. if (condition) pass(text);
  45. else failed(text, detail);
  46. return Boolean(condition);
  47. }
  48. function loadDotEnv() {
  49. const file = path.join(ROOT, '.env');
  50. if (!existsSync(file)) return;
  51. for (const line of readFileSync(file, 'utf8').split(/\r?\n/)) {
  52. const trimmed = line.trim();
  53. if (!trimmed || trimmed.startsWith('#')) continue;
  54. const index = trimmed.indexOf('=');
  55. if (index <= 0) continue;
  56. const key = trimmed.slice(0, index).trim();
  57. const value = trimmed.slice(index + 1).trim().replace(/^["']|["']$/g, '');
  58. if (process.env[key] === undefined) process.env[key] = value;
  59. }
  60. }
  61. const children = [];
  62. function startProcess(label, command, argv, env) {
  63. const child = spawn(command, argv, {
  64. cwd: ROOT,
  65. env: { ...process.env, ...env },
  66. stdio: ['ignore', 'pipe', 'pipe'],
  67. });
  68. const log = [];
  69. child.stdout.on('data', (chunk) => log.push(chunk.toString()));
  70. child.stderr.on('data', (chunk) => log.push(chunk.toString()));
  71. child.on('exit', (code) => {
  72. if (code !== 0 && code !== null) {
  73. console.log(`${C.dim}[${label}] 退出 code=${code}${C.reset}`);
  74. }
  75. });
  76. children.push({ label, child, log });
  77. return child;
  78. }
  79. function stopAll() {
  80. for (const { child } of children.reverse()) {
  81. try {
  82. if (process.platform === 'win32') {
  83. spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore' });
  84. } else {
  85. child.kill('SIGKILL');
  86. }
  87. } catch {
  88. /* 忽略 */
  89. }
  90. }
  91. }
  92. async function waitFor(url, timeoutMs, label) {
  93. const deadline = Date.now() + timeoutMs;
  94. while (Date.now() < deadline) {
  95. try {
  96. const response = await fetch(url, { signal: AbortSignal.timeout(2000) });
  97. if (response.ok) return true;
  98. } catch {
  99. /* 还没起来 */
  100. }
  101. await new Promise((resolve) => setTimeout(resolve, 400));
  102. }
  103. for (const item of children) {
  104. if (item.log.length) console.log(`${C.dim}--- ${item.label} 输出 ---\n${item.log.join('').slice(-3000)}${C.reset}`);
  105. }
  106. failed(`${label} 未在 ${timeoutMs / 1000}s 内就绪:${url}`);
  107. return false;
  108. }
  109. async function api(pathname, init, base = BFF_URL) {
  110. const response = await fetch(`${base}${pathname}`, init);
  111. const text = await response.text();
  112. let payload = null;
  113. try {
  114. payload = text ? JSON.parse(text) : null;
  115. } catch {
  116. payload = text;
  117. }
  118. return { ok: response.ok, status: response.status, payload };
  119. }
  120. async function sse(url, body) {
  121. const response = await fetch(url, {
  122. method: 'POST',
  123. headers: { 'Content-Type': 'application/json' },
  124. body: JSON.stringify(body),
  125. });
  126. if (!response.ok) throw new Error(`SSE 请求失败 HTTP ${response.status}: ${await response.text()}`);
  127. const text = await response.text();
  128. const events = [];
  129. for (const block of text.split('\n\n')) {
  130. let event = 'message';
  131. const data = [];
  132. for (const line of block.split('\n')) {
  133. if (line.startsWith('event:')) event = line.slice(6).trim();
  134. else if (line.startsWith('data:')) data.push(line.slice(5).trim());
  135. }
  136. if (data.length === 0) continue;
  137. try {
  138. events.push({ event, data: JSON.parse(data.join('\n')) });
  139. } catch {
  140. events.push({ event, data: data.join('\n') });
  141. }
  142. }
  143. return events;
  144. }
  145. function killPortHolders(ports) {
  146. if (process.platform !== 'win32') return;
  147. const result = spawnSync('netstat', ['-ano'], { encoding: 'utf8' });
  148. if (result.status !== 0 || !result.stdout) return;
  149. const pids = new Set();
  150. for (const line of result.stdout.split(/\r?\n/)) {
  151. if (!line.includes('LISTENING')) continue;
  152. for (const port of ports) {
  153. if (!line.includes(`:${port}`)) continue;
  154. const pid = line.trim().split(/\s+/).pop();
  155. if (pid && pid !== '0') pids.add(pid);
  156. }
  157. }
  158. for (const pid of pids) {
  159. console.log(`${C.dim}清理占用测试端口的进程 pid=${pid}${C.reset}`);
  160. spawnSync('taskkill', ['/pid', pid, '/T', '/F'], { stdio: 'ignore' });
  161. }
  162. }
  163. async function main() {
  164. console.log(C.bold + '\n海关问数 Agent · 端到端联调测试\n' + C.reset);
  165. loadDotEnv();
  166. // 上一轮异常退出可能留下占用端口的进程,先清理,避免"服务未就绪"的假失败
  167. killPortHolders([TOOLS_PORT, BFF_PORT]);
  168. await new Promise((resolve) => setTimeout(resolve, 500));
  169. try {
  170. rmSync(RUN, { recursive: true, force: true });
  171. } catch (error) {
  172. console.log(`${C.yellow}清理测试目录失败(可能有进程占用):${error.message}${C.reset}`);
  173. }
  174. mkdirSync(DATA, { recursive: true });
  175. mkdirSync(BFF_DATA, { recursive: true });
  176. const python = process.env.CDA_PYTHON ?? (process.platform === 'win32' ? 'python' : 'python3');
  177. const hasKey = Boolean(process.env.DEEPSEEK_API_KEY);
  178. section('1. 启动服务');
  179. startProcess('tools', python, ['-m', 'toolsvc'], {
  180. cwd: path.join(ROOT, 'tools-service'),
  181. PYTHONPATH: path.join(ROOT, 'tools-service'),
  182. CDA_DATA_DIR: DATA,
  183. CDA_TOOLS_PORT: String(TOOLS_PORT),
  184. CDA_LEDGER_JSONL: '1',
  185. });
  186. if (!(await waitFor(`${TOOLS_URL}/health`, 60_000, '工具服务'))) return finish();
  187. startProcess('bff', process.execPath, [path.join(ROOT, 'bff', 'dist', 'src', 'index.js')], {
  188. CDA_TOOLS_URL: TOOLS_URL,
  189. CDA_PORT: String(BFF_PORT),
  190. CDA_BFF_DATA_DIR: BFF_DATA,
  191. CDA_WEB_DIST: path.join(ROOT, 'web', 'dist'),
  192. DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY ?? 'replay-placeholder',
  193. });
  194. if (!(await waitFor(`${BFF_URL}/api/health`, 60_000, 'BFF'))) return finish();
  195. pass('工具服务与 BFF 均已就绪');
  196. section('2. 健康检查');
  197. const health = await api('/api/health');
  198. check(health.ok && health.payload.tools?.ok === true, 'GET /api/health 报告工具服务在线', health.payload);
  199. check(health.payload.harness === 'embedded-dsh-compatible', 'Harness 运行时为内嵌实现', health.payload.harness);
  200. section('3. 工作区与文件上传');
  201. const created = await api('/api/workspaces', {
  202. method: 'POST',
  203. headers: { 'Content-Type': 'application/json' },
  204. body: JSON.stringify({ name: 'E2E 测试工作区', description: '端到端联调' }),
  205. });
  206. check(created.ok, '创建工作区', created.payload);
  207. const workspaceId = created.payload?.id;
  208. const samplePath = path.join(ROOT, 'samples', 'small.csv');
  209. if (!existsSync(samplePath)) {
  210. failed('缺少 samples/small.csv,请先运行 python scripts/gen_sample_data.py');
  211. return finish();
  212. }
  213. const csv = readFileSync(samplePath);
  214. const upload = await fetch(`${BFF_URL}/api/workspaces/${workspaceId}/files`, {
  215. method: 'POST',
  216. headers: { 'Content-Type': 'application/octet-stream', 'X-Filename': encodeURIComponent('small.csv') },
  217. body: csv,
  218. });
  219. const uploaded = await upload.json();
  220. check(upload.ok, `上传原始数据(${Math.round(csv.length / 1024)} KB)`, uploaded);
  221. check(uploaded.rows > 1000, `原始数据索引了 ${uploaded.rows} 行`);
  222. check(uploaded.sha256?.startsWith('sha256:'), '原始数据带 SHA256 指纹');
  223. section('4. 数据处理(16 张模板表 + 7 张补充表)');
  224. const run = await api('/api/process', {
  225. method: 'POST',
  226. headers: { 'Content-Type': 'application/json' },
  227. body: JSON.stringify({
  228. workspace_id: workspaceId,
  229. input_object_id: uploaded.id,
  230. operation: 'full_pipeline',
  231. options: { start_year: 2025, end_year: 2026 },
  232. output_name: 'E2E统计结果.xlsx',
  233. }),
  234. });
  235. check(run.ok, '创建处理任务', run.payload);
  236. const runId = run.payload?.id;
  237. // 进度用 SSE 观察(契约要求的形态),顺便统计 progress 事件数量
  238. const streamResponse = await fetch(`${BFF_URL}/api/process/${runId}/stream`);
  239. check(streamResponse.ok, '进度 SSE 端点可用(text/event-stream)');
  240. const streamText = await streamResponse.text();
  241. const progressCount = (streamText.match(/event: progress/g) || []).length;
  242. const doneMatch = /event: done\ndata: (.*)/.exec(streamText);
  243. const done = doneMatch ? JSON.parse(doneMatch[1]) : null;
  244. check(progressCount >= 2, `通过 SSE 收到 ${progressCount} 个进度事件`);
  245. check(done?.status === 'succeeded', `SSE 结束事件 status=${done?.status}`);
  246. const current = done;
  247. const statId = current?.object_id;
  248. const statObject = (await api(`/api/objects/${statId}`)).payload;
  249. check(statObject?.tables?.length === 25, `生成 ${statObject?.tables?.length} 张结果表`, statObject?.tables?.map((t) => t.name));
  250. check(statObject?.parents?.includes(uploaded.id), '结果对象血缘指向原始数据');
  251. check(Boolean(statObject?.meta?.lineage?.output_hash), '结果对象带输出哈希与参数哈希');
  252. section('5. 数据读取与血缘');
  253. const tableName = '1.0-3.0_MLCC';
  254. const preview = await api(`/api/objects/${statId}/preview?table=${encodeURIComponent(tableName)}&limit=3`);
  255. check(preview.ok && preview.payload.rows.length === 3, `预览 ${tableName} 前 3 行`, preview.payload);
  256. const lineage = await api(`/api/objects/${statId}/lineage?table=${encodeURIComponent(tableName)}&row=0`);
  257. check(lineage.ok && lineage.payload.source_object_id === uploaded.id, '第 0 行可下钻到原始数据(行级血缘)', lineage.payload);
  258. // 溯源 chip 点开时用的"按行取数"
  259. const rowDetail = await api(`/api/objects/${statId}/row?table=${encodeURIComponent(tableName)}&row=0`);
  260. check(
  261. rowDetail.ok && rowDetail.payload.row_hash?.startsWith('sha256:') && rowDetail.payload.values?.['基础类目'],
  262. '按行取数返回原文 + 行哈希(溯源抽屉)',
  263. rowDetail.payload,
  264. );
  265. check(
  266. Boolean(rowDetail.payload?.lineage?.source_rows),
  267. '该行带下钻谓词(可回到原始记录范围)',
  268. rowDetail.payload?.lineage,
  269. );
  270. const rowMissing = await fetch(`${BFF_URL}/api/objects/${statId}/row?table=${encodeURIComponent(tableName)}&row=99999`);
  271. check(rowMissing.status === 404, '越界行号返回 404(不 500)');
  272. const download = await fetch(`${BFF_URL}/api/objects/${statId}/download?name=${encodeURIComponent('统计结果.xlsx')}`);
  273. const buffer = Buffer.from(await download.arrayBuffer());
  274. check(download.ok && buffer.subarray(0, 2).toString() === 'PK', `下载 统计结果.xlsx(${Math.round(buffer.length / 1024)} KB)`);
  275. section('6. 指数口径校验(基期必须为 1000)');
  276. const baseRows = await api(`/api/objects/${statId}/preview?table=${encodeURIComponent(tableName)}&limit=50`);
  277. const mlcc2025 = baseRows.payload?.rows?.filter((row) => String(row['基础类目'] ?? '').includes('MLCC') && String(row['年度']) === '2025' && String(row['月度']) === '1');
  278. const valueIndex = mlcc2025?.[0]?.['金额指数'];
  279. check(valueIndex !== undefined && Math.abs(Number(valueIndex) - 1000) < 1e-6, `2025-01 金额指数 = ${valueIndex}(应为 1000)`);
  280. section('7. 自定义聚合');
  281. const custom = await api('/api/process', {
  282. method: 'POST',
  283. headers: { 'Content-Type': 'application/json' },
  284. body: JSON.stringify({
  285. workspace_id: workspaceId,
  286. input_object_id: uploaded.id,
  287. operation: 'custom_aggregate',
  288. options: { group_by: ['c1', 'c2'], directions: ['I'] },
  289. output_name: 'E2E自定义聚合',
  290. }),
  291. });
  292. const customRunId = custom.payload?.id;
  293. let customState = null;
  294. const customDeadline = Date.now() + 180_000;
  295. while (Date.now() < customDeadline) {
  296. const state = await api(`/api/process/${customRunId}`);
  297. customState = state.payload;
  298. if (customState.status === 'succeeded' || customState.status === 'failed') break;
  299. await new Promise((resolve) => setTimeout(resolve, 500));
  300. }
  301. check(customState?.status === 'succeeded', '自定义聚合任务成功', customState?.error);
  302. section('8. 台账');
  303. const ledger = await api('/api/ledger/stats');
  304. check(ledger.payload?.tools?.chain_ok === true, '工具服务台账哈希链完整', ledger.payload);
  305. section('9. Agent 对话');
  306. const wantChat = !args.has('--no-chat') && hasKey;
  307. if (!wantChat) {
  308. skip(hasKey ? '按要求跳过对话测试(--no-chat)' : '未配置 DEEPSEEK_API_KEY,跳过真实模型对话测试');
  309. } else {
  310. const session = await api('/api/sessions', {
  311. method: 'POST',
  312. headers: { 'Content-Type': 'application/json' },
  313. body: JSON.stringify({ workspace_id: workspaceId }),
  314. });
  315. check(session.ok, '创建会话', session.payload);
  316. const question = '只查一个数:统计结果里 2025 年 1 月「多层瓷介电容器(MLCC)」的金额指数是多少?请只回答这一个数字。';
  317. const events = await sse(`${BFF_URL}/api/sessions/${session.payload.id}/chat`, { message: question });
  318. const names = events.map((item) => item.event);
  319. check(names.includes('token'), 'SSE 至少推送一个 token 事件');
  320. check(names.includes('tool_call'), '模型调用了工具');
  321. const verification = events.find((item) => item.event === 'verification')?.data;
  322. const final = events.find((item) => item.event === 'final')?.data;
  323. check(Boolean(verification), '产出核验结论');
  324. // 模型的输出是随机的:这里断言"机制正确"(三种裁决都可能,且交付与否必须与裁决一致),
  325. // 而不是断言模型一定不编数字——后者属于模型质量问题,不是平台回归。
  326. const verdict = verification?.verdict;
  327. check(
  328. verdict === 'accept' || verdict === 'flag' || verdict === 'reject',
  329. `核验给出明确裁决:${verdict}(${verification?.summary})`,
  330. verification?.summary,
  331. );
  332. check(
  333. final?.delivered === (verdict !== 'reject'),
  334. verdict === 'reject' ? '核验未通过时拒绝交付(符合设计)' : '核验通过后交付答案',
  335. { verdict, delivered: final?.delivered },
  336. );
  337. if (final?.text) {
  338. console.log(`${C.dim} 回答摘要:${String(final.text).replace(/\n/g, ' ').slice(0, 160)}…${C.reset}`);
  339. }
  340. const history = await api(`/api/sessions/${session.payload.id}/messages`);
  341. check(history.payload?.items?.length === 2, '会话持久化了 user + assistant 两条消息');
  342. const sessionLedger = await api(`/api/sessions/${session.payload.id}/ledger`);
  343. check((sessionLedger.payload?.calls?.length ?? 0) > 0, '会话台账记录了工具调用');
  344. }
  345. section('10. 前端静态资源');
  346. const index = await fetch(`${BFF_URL}/`);
  347. const html = await index.text();
  348. check(index.ok && html.includes('<div id="root">'), 'BFF 已托管前端页面');
  349. const spa = await fetch(`${BFF_URL}/some/deep/route`);
  350. check(spa.ok, 'SPA 路由回退到 index.html');
  351. return finish();
  352. }
  353. function finish() {
  354. stopAll();
  355. console.log('');
  356. if (failures === 0) {
  357. console.log(`${C.green}${C.bold}全部 ${checks} 项检查通过${C.reset}`);
  358. } else {
  359. console.log(`${C.red}${C.bold}${failures} / ${checks} 项检查失败${C.reset}`);
  360. }
  361. if (!args.has('--keep') && failures === 0) {
  362. try {
  363. rmSync(RUN, { recursive: true, force: true });
  364. } catch {
  365. /* 忽略 */
  366. }
  367. } else {
  368. console.log(`${C.dim}测试数据保留在 ${RUN}${C.reset}`);
  369. }
  370. setTimeout(() => process.exit(failures === 0 ? 0 : 1), 200);
  371. return failures === 0 ? 0 : 1;
  372. }
  373. process.on('SIGINT', () => {
  374. stopAll();
  375. process.exit(130);
  376. });
  377. main().catch((error) => {
  378. console.error(error);
  379. failed('端到端测试异常', error?.stack ?? String(error));
  380. finish();
  381. });