process-diagnostic.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. parseZCodeProcessDiagnostic,
  4. ZCODE_PROCESS_DIAGNOSTIC_PREFIX,
  5. ZCODE_PROCESS_DIAGNOSTIC_MAX_LINE_CHARS,
  6. } from "@zcode/shared/process-diagnostic";
  7. import { hostResponseMessageSchema } from "@zcode/shared";
  8. const diagnostic = {
  9. version: 1,
  10. errorId: "1f51b7dc-c52a-46ad-bc97-d2f984e6e3de",
  11. kind: "uncaughtException",
  12. origin: "uncaughtException",
  13. name: "TypeError",
  14. message: "test exception",
  15. stack: "TypeError: test exception\n at run (C:\\repo\\cli.js:1:2)",
  16. occurredAt: 123,
  17. };
  18. describe("CLI process diagnostic contract", () => {
  19. it("preserves multiline stack in one stderr frame and validates Host IPC", () => {
  20. const line = ZCODE_PROCESS_DIAGNOSTIC_PREFIX + JSON.stringify(diagnostic);
  21. expect(line.split("\n")).toHaveLength(1);
  22. expect(parseZCodeProcessDiagnostic(line)).toEqual(diagnostic);
  23. expect(
  24. hostResponseMessageSchema.parse({
  25. type: "agent-process-exception",
  26. diagnostic,
  27. pid: 123,
  28. provider: "glm",
  29. workspacePath: "C:\\repo",
  30. runtimeGeneration: 1,
  31. runtimeInstanceId: "agent-instance",
  32. }),
  33. ).toMatchObject({ diagnostic });
  34. });
  35. it("rejects unknown versions, extra fields, malformed/oversized data and ordinary logs", () => {
  36. for (const value of [
  37. { ...diagnostic, version: 2 },
  38. { ...diagnostic, taskId: "private" },
  39. { ...diagnostic, kind: "console.error" },
  40. { ...diagnostic, message: "x".repeat(4001) },
  41. ]) {
  42. expect(
  43. parseZCodeProcessDiagnostic(ZCODE_PROCESS_DIAGNOSTIC_PREFIX + JSON.stringify(value)),
  44. ).toBeUndefined();
  45. }
  46. for (const line of [
  47. "Error: ordinary stderr",
  48. ZCODE_PROCESS_DIAGNOSTIC_PREFIX + "{",
  49. ZCODE_PROCESS_DIAGNOSTIC_PREFIX + "x".repeat(ZCODE_PROCESS_DIAGNOSTIC_MAX_LINE_CHARS),
  50. ]) {
  51. expect(parseZCodeProcessDiagnostic(line)).toBeUndefined();
  52. }
  53. });
  54. });