assistantPresentation.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. appendAssistantMessagePart,
  4. buildZCodeAssistantPresentation,
  5. type ZCodeAssistantMessagePart,
  6. } from "../src/index.js";
  7. describe("assistant presentation", () => {
  8. it("uses the final content block as latestPart", () => {
  9. const parts: ZCodeAssistantMessagePart[] = [
  10. { type: "content", content: "历史正文" },
  11. { type: "tool-call", toolId: "tool-1" },
  12. { type: "content", content: "最终正文" },
  13. ];
  14. const presentation = buildZCodeAssistantPresentation({
  15. content: "",
  16. parts,
  17. toolCalls: [
  18. {
  19. toolId: "tool-1",
  20. kind: "edit",
  21. title: "Edit file",
  22. input: {},
  23. status: "completed",
  24. },
  25. ],
  26. });
  27. expect(presentation.latestPart?.content).toBe("最终正文");
  28. expect(presentation.historyBlocks).toMatchObject([
  29. { type: "content", content: "历史正文" },
  30. { type: "tool-call", toolCall: { toolId: "tool-1" } },
  31. ]);
  32. });
  33. it("suppresses latestPart while streaming, interrupted, or settling", () => {
  34. for (const state of [
  35. { streaming: true },
  36. { interrupted: true },
  37. { settling: true },
  38. ]) {
  39. expect(
  40. buildZCodeAssistantPresentation({
  41. content: "正文",
  42. ...state,
  43. }).latestPart,
  44. ).toBeNull();
  45. }
  46. });
  47. it("merges continuous text chunks before presentation is built", () => {
  48. const parts = appendAssistantMessagePart(
  49. appendAssistantMessagePart(undefined, { type: "content", content: "A" }),
  50. { type: "content", content: "B" },
  51. );
  52. expect(buildZCodeAssistantPresentation({ content: "", parts }).blocks).toEqual([
  53. { type: "content", content: "AB" },
  54. ]);
  55. });
  56. it("keeps child tool calls inside the parent tree instead of rendering duplicate root blocks", () => {
  57. const presentation = buildZCodeAssistantPresentation({
  58. content: "",
  59. parts: [
  60. { type: "tool-call", toolId: "parent" },
  61. { type: "tool-call", toolId: "child" },
  62. ],
  63. toolCalls: [
  64. {
  65. toolId: "parent",
  66. kind: "task",
  67. title: "Task",
  68. input: {},
  69. status: "completed",
  70. },
  71. {
  72. toolId: "child",
  73. parentToolUseId: "parent",
  74. kind: "read",
  75. title: "Read",
  76. input: {},
  77. status: "completed",
  78. },
  79. ],
  80. });
  81. expect(presentation.blocks).toMatchObject([
  82. { type: "tool-call", toolCall: { toolId: "parent" } },
  83. ]);
  84. });
  85. });