api.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { mkdtemp, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { createDebugApp } from "../server/index.js";
  6. describe("debug API", () => {
  7. it("returns trace details through Hono", async () => {
  8. const root = await mkdtemp(join(tmpdir(), "zcode-debug-api-"));
  9. const eventPath = join(root, "events.jsonl");
  10. await writeFile(
  11. eventPath,
  12. `${JSON.stringify({
  13. id: "evt-api",
  14. type: "turn_started",
  15. timestamp: "2026-05-04T02:00:00.000Z",
  16. traceId: "trace-api",
  17. sessionId: "session-api",
  18. payload: { input: "hello" },
  19. })}\n`,
  20. "utf8",
  21. );
  22. const app = createDebugApp();
  23. const response = await app.request(
  24. `/api/traces/trace-api?eventPath=${encodeURIComponent(eventPath)}&dbPath=${encodeURIComponent(
  25. join(root, "missing.sqlite"),
  26. )}`,
  27. );
  28. const body = (await response.json()) as { traceId: string; timeline: unknown[] };
  29. expect(response.status).toBe(200);
  30. expect(body.traceId).toBe("trace-api");
  31. expect(body.timeline).toHaveLength(1);
  32. });
  33. it("returns a stable disabled status when network capture is not attached", async () => {
  34. const app = createDebugApp();
  35. const response = await app.request("/api/network/status");
  36. const body = (await response.json()) as { enabled: boolean; running: boolean };
  37. expect(response.status).toBe(200);
  38. expect(body).toMatchObject({
  39. enabled: false,
  40. running: false,
  41. });
  42. });
  43. });