verify-export.mjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env node
  2. /**
  3. * 导出接口回归校验(BUG-004)。
  4. *
  5. * 覆盖三处导出:任务管理 / 会话与消息审计 / 用量与配额。
  6. * 断言:空筛选条件下不再出现 `agentId=null` 之类的非法参数,接口返回 200 且响应非空。
  7. *
  8. * 用法(后端已启动):
  9. * node ai-server/scripts/verify-export.mjs
  10. * ZSJZ_BASE_URL=http://localhost:8980/js/a ZSJZ_USERNAME=admin ZSJZ_PASSWORD=admin123 node ai-server/scripts/verify-export.mjs
  11. *
  12. * 说明:脚本会以 force=true 登录(会顶掉同一账号的浏览器会话),执行后浏览器需重新登录。
  13. */
  14. const BASE = process.env.ZSJZ_BASE_URL ?? 'http://localhost:8980/js/a';
  15. const USERNAME = process.env.ZSJZ_USERNAME ?? 'admin';
  16. const PASSWORD = process.env.ZSJZ_PASSWORD ?? 'admin123';
  17. async function login() {
  18. const res = await fetch(`${BASE}/auth/login`, {
  19. method: 'POST',
  20. headers: { 'Content-Type': 'application/json' },
  21. body: JSON.stringify({ username: USERNAME, password: PASSWORD, force: true }),
  22. });
  23. const body = await res.json();
  24. if (body?.code !== 200 || !body?.data?.token) {
  25. throw new Error(`登录失败:HTTP ${res.status} / ${body?.message ?? body?.code ?? 'unknown'}`);
  26. }
  27. return body.data.token;
  28. }
  29. async function expectExport(token, path, name) {
  30. const res = await fetch(`${BASE}${path}`, {
  31. method: 'POST',
  32. headers: {
  33. 'x-token': token,
  34. 'Content-Type': 'application/x-www-form-urlencoded',
  35. },
  36. body: '',
  37. });
  38. const bytes = Buffer.from(await res.arrayBuffer());
  39. const contentType = res.headers.get('content-type') ?? '';
  40. const ok = res.status === 200 && bytes.length > 0;
  41. console.log(
  42. `${ok ? 'PASS' : 'FAIL'} ${name} HTTP=${res.status} bytes=${bytes.length} content-type=${contentType}`,
  43. );
  44. if (!ok) {
  45. const text = bytes.subarray(0, 300).toString('utf8');
  46. console.log(` 响应片段:${text}`);
  47. }
  48. return ok;
  49. }
  50. const token = await login();
  51. const results = await Promise.all([
  52. expectExport(token, '/ai/session/export', '任务管理导出'),
  53. expectExport(token, '/ai/audit/message/export', '会话与消息审计导出'),
  54. expectExport(token, '/ai/usage/export', '用量与配额导出'),
  55. ]);
  56. if (results.some((ok) => !ok)) {
  57. process.exitCode = 1;
  58. }