process-diagnostic.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(parseZCodeProcessDiagnostic(line)).toEqual(diagnostic);
  22. expect(
  23. hostResponseMessageSchema.parse({
  24. type: "agent-process-exception",
  25. diagnostic,
  26. pid: 123,
  27. provider: "glm",
  28. workspacePath: "C:\\repo",
  29. runtimeGeneration: 1,
  30. runtimeInstanceId: "agent-instance",
  31. }),
  32. ).toMatchObject({ diagnostic });
  33. });
  34. it("rejects unknown versions, extra fields, malformed/oversized data and ordinary logs", () => {
  35. for (const value of [
  36. { ...diagnostic, version: 2 },
  37. { ...diagnostic, taskId: "private" },
  38. { ...diagnostic, kind: "console.error" },
  39. { ...diagnostic, message: "x".repeat(4001) },
  40. ]) {
  41. expect(
  42. parseZCodeProcessDiagnostic(ZCODE_PROCESS_DIAGNOSTIC_PREFIX + JSON.stringify(value)),
  43. ).toBeUndefined();
  44. }
  45. for (const line of [
  46. "Error: ordinary stderr",
  47. ZCODE_PROCESS_DIAGNOSTIC_PREFIX + "{",
  48. ZCODE_PROCESS_DIAGNOSTIC_PREFIX + "x".repeat(ZCODE_PROCESS_DIAGNOSTIC_MAX_LINE_CHARS),
  49. ]) {
  50. expect(parseZCodeProcessDiagnostic(line)).toBeUndefined();
  51. }
  52. });
  53. });