api.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { createDebugApp } from "../server/index.js";
  6. const roots: string[] = [];
  7. afterEach(async () => {
  8. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  9. });
  10. describe("debug API", () => {
  11. it("returns trace details through Hono", async () => {
  12. const root = await mkdtemp(join(tmpdir(), "zcode-debug-api-"));
  13. roots.push(root);
  14. const eventPath = join(root, "events.jsonl");
  15. await writeFile(
  16. eventPath,
  17. `${JSON.stringify({
  18. id: "evt-api",
  19. type: "turn_started",
  20. timestamp: "2026-05-04T02:00:00.000Z",
  21. traceId: "trace-api",
  22. sessionId: "session-api",
  23. payload: { input: "hello" },
  24. })}\n`,
  25. "utf8",
  26. );
  27. const app = createDebugApp();
  28. // 漏传 logDir 会扫描操作者的真实日志,导致全量并发时超时;所有数据源都必须指向夹具。
  29. const response = await app.request(
  30. `/api/traces/trace-api?logDir=${encodeURIComponent(join(root, "missing-logs"))}&eventPath=${encodeURIComponent(eventPath)}&dbPath=${encodeURIComponent(
  31. join(root, "missing.sqlite"),
  32. )}`,
  33. );
  34. const body = (await response.json()) as {
  35. traceId: string;
  36. timeline: unknown[];
  37. sources: { kind: string; path: string }[];
  38. };
  39. expect(response.status).toBe(200);
  40. expect(body.traceId).toBe("trace-api");
  41. expect(body.timeline).toHaveLength(1);
  42. expect(body.sources.find((source) => source.kind === "log")?.path).toBe(
  43. join(root, "missing-logs"),
  44. );
  45. });
  46. it("returns a stable disabled status when network capture is not attached", async () => {
  47. const app = createDebugApp();
  48. const response = await app.request("/api/network/status");
  49. const body = (await response.json()) as { enabled: boolean; running: boolean };
  50. expect(response.status).toBe(200);
  51. expect(body).toMatchObject({
  52. enabled: false,
  53. running: false,
  54. });
  55. });
  56. });