bashOutputDisplay.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { describe, expect, it } from "vitest";
  2. import { zcodeSessionEventSchema } from "../src/zcode-protocol/index.js";
  3. import { conversationRowSchema } from "../src/zcode-protocol-v4/index.js";
  4. const display = {
  5. kind: "bash_output",
  6. output: "head",
  7. truncated: true,
  8. outputPath: "C:\\中文 空格\\output.log",
  9. };
  10. function parseBoth(value: unknown) {
  11. const event = {
  12. eventId: "bash-result-event",
  13. type: "tool.updated",
  14. sessionId: "session",
  15. seq: 1,
  16. timestamp: 1,
  17. payload: {
  18. kind: "result",
  19. toolCallId: "bash",
  20. toolName: "Bash",
  21. duration: 1,
  22. result: { success: true, content: "model preview", display: value },
  23. },
  24. };
  25. const row = {
  26. kind: "toolCall",
  27. rowId: 1,
  28. turnId: "turn",
  29. createdAt: 1,
  30. createdAtSeq: 1,
  31. toolCallId: "bash",
  32. toolName: "Bash",
  33. inputText: "",
  34. status: "success",
  35. output: { text: "model preview", display: value },
  36. };
  37. return [zcodeSessionEventSchema.safeParse(event), conversationRowSchema.safeParse(row)];
  38. }
  39. describe("Bash output display wire contract", () => {
  40. it("preserves display on the legacy event and V4 row", () => {
  41. for (const parsed of parseBoth(display)) {
  42. expect(parsed.success).toBe(true);
  43. expect(JSON.stringify(parsed)).toContain(JSON.stringify(display));
  44. }
  45. });
  46. // 两条通道对"读不懂的 display"给出同一个产品结果——**这张卡没有载荷**——但拒收的位置不同,
  47. // 而位置决定代价:
  48. // v3 `session/event`:整条事件被丢,consumer 记 zod issues 后继续(zcodeAgentService.ts)。
  49. // v4 row:display 在信封层不设门(`.catch(undefined)`),载荷被丢、row/帧照常投影。
  50. // v4 曾经也在信封层拒收,但那一层的"拒"是拒整帧,会连带打掉订阅(见
  51. // zcodeProtocolV4DisplayTolerance.test.ts 的事故说明)。有界性没有放松:越界载荷同样到不了 UI。
  52. it.each([
  53. { ...display, output: "x".repeat(150001) },
  54. { ...display, truncated: "true" },
  55. { ...display, outputPath: "" },
  56. { ...display, unbounded: true },
  57. ])("两条通道都不携带越界/畸形 display", (invalid) => {
  58. const [event, row] = parseBoth(invalid);
  59. expect(event.success).toBe(false);
  60. expect(row.success).toBe(true);
  61. if (row.success && row.data.kind === "toolCall") {
  62. expect(row.data.output?.display).toBeUndefined();
  63. }
  64. });
  65. });