readSessionContextToolCallBlock.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import type { IBroadcastService } from "@zcode/services";
  2. import { createElement } from "react";
  3. import type { ReactElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { describe, expect, it } from "vitest";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import { StoreProvider } from "@/store/StoreProvider.js";
  8. import { ToolCallBlock } from "../src/ToolCallBlocks.js";
  9. import { ReadSessionContextToolCallBlock } from "../src/ToolCallBlocks/renderers/read-session-context.js";
  10. const localStorageState = new Map<string, string>();
  11. Object.defineProperty(globalThis, "localStorage", {
  12. configurable: true,
  13. value: {
  14. clear: () => {
  15. localStorageState.clear();
  16. },
  17. getItem: (key: string) => localStorageState.get(key) ?? null,
  18. removeItem: (key: string) => {
  19. localStorageState.delete(key);
  20. },
  21. setItem: (key: string, value: string) => {
  22. localStorageState.set(key, value);
  23. },
  24. },
  25. });
  26. Object.defineProperty(globalThis, "document", {
  27. configurable: true,
  28. value: {
  29. documentElement: {
  30. classList: {
  31. contains: () => false,
  32. toggle: () => {},
  33. },
  34. },
  35. },
  36. });
  37. const mockBroadcastService: IBroadcastService = {
  38. send: async () => {},
  39. onMessage: () => ({ dispose: () => {} }),
  40. };
  41. const displayModel = {
  42. inlinePreview: { type: "none" as const },
  43. planResult: null,
  44. viewerSource: null,
  45. viewerLabelId: "codeViewer.viewCode" as const,
  46. showSummaryFileLink: false,
  47. showInput: false,
  48. showOutput: true,
  49. showKind: false,
  50. };
  51. function renderWithProviders(element: ReactElement) {
  52. return renderToStaticMarkup(
  53. createElement(
  54. ZCodeIntlProvider,
  55. { initialLocale: "zh-CN" },
  56. createElement(StoreProvider, { broadcastService: mockBroadcastService }, element),
  57. ),
  58. );
  59. }
  60. describe("ReadSessionContextToolCallBlock", () => {
  61. it("renders query and returned context instead of raw parameters", () => {
  62. const html = renderWithProviders(
  63. createElement(ReadSessionContextToolCallBlock, {
  64. toolCallNode: {
  65. toolCall: {
  66. toolId: "tool-read-session-context",
  67. toolName: "ReadSessionContext",
  68. kind: "ReadSessionContext",
  69. title: "ReadSessionContext",
  70. input: {
  71. sessionId: "sess_alpha",
  72. query: "能否操作浏览器 browser automation 结果 result",
  73. },
  74. output: {
  75. status: "success",
  76. sessionId: "sess_alpha",
  77. query: "能否操作浏览器 browser automation 结果 result",
  78. strategy: "relevant",
  79. source: "lite",
  80. content: "- **结论**:不能直接操作浏览器。\n- 可读取截图和 URL 内容。",
  81. messageCount: 2,
  82. selectedMessageCount: 2,
  83. truncated: false,
  84. },
  85. status: "completed",
  86. raw: {
  87. rawInput: {
  88. sessionId: "sess_alpha",
  89. query: "能否操作浏览器 browser automation 结果 result",
  90. },
  91. rawOutput: {
  92. content: "raw content should not be used first",
  93. },
  94. },
  95. },
  96. childToolCalls: [],
  97. },
  98. workspacePath: "/workspace",
  99. displayModel,
  100. viewerSource: null,
  101. rawFileSummaries: [],
  102. isRunning: false,
  103. statusLabel: "已执行",
  104. childToolList: null,
  105. forceOpen: true,
  106. canToggle: true,
  107. showIcon: true,
  108. }),
  109. );
  110. expect(html).toContain("会话上下文");
  111. expect(html).toContain("查询");
  112. expect(html).toContain("能否操作浏览器 browser automation 结果 result");
  113. expect(html).toContain("结果");
  114. expect(html).toContain("不能直接操作浏览器");
  115. expect(html).toContain("可读取截图和 URL 内容");
  116. expect(html).not.toContain("Parameters");
  117. expect(html).not.toContain("&quot;sessionId&quot;");
  118. expect(html).not.toContain("&quot;messageCount&quot;");
  119. expect(html).not.toContain("raw content should not be used first");
  120. });
  121. it("routes ReadSessionContext through the dedicated renderer", () => {
  122. const html = renderWithProviders(
  123. createElement(ToolCallBlock, {
  124. toolCallNode: {
  125. toolCall: {
  126. toolId: "tool-read-session-context-route",
  127. toolName: "ReadSessionContext",
  128. kind: "ReadSessionContext",
  129. title: "ReadSessionContext",
  130. input: {
  131. sessionId: "sess_route",
  132. query: "总结迁移任务状态",
  133. },
  134. output: "ReadSessionContext returned lite context for sess_route.",
  135. status: "completed",
  136. raw: {},
  137. },
  138. childToolCalls: [],
  139. },
  140. workspacePath: "/workspace",
  141. }),
  142. );
  143. expect(html).toContain("会话上下文");
  144. expect(html).toContain("总结迁移任务状态");
  145. expect(html).not.toContain("工具调用");
  146. });
  147. });