switchModeToolCallBlock.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import type { IBroadcastService, IServiceAccessor } from "@zcode/services";
  2. import { ServiceProvider } from "@/hooks/useServices.js";
  3. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  4. import { StoreProvider } from "@/store/StoreProvider.js";
  5. import { createElement } from "react";
  6. import { renderToStaticMarkup } from "react-dom/server";
  7. import { describe, expect, it } from "vitest";
  8. import { SwitchModeToolCallBlock } from "../src/ToolCallBlocks/renderers/switch-mode.js";
  9. const localStorageState = new Map<string, string>();
  10. Object.defineProperty(globalThis, "localStorage", {
  11. configurable: true,
  12. value: {
  13. getItem: (key: string) => localStorageState.get(key) ?? null,
  14. setItem: (key: string, value: string) => {
  15. localStorageState.set(key, value);
  16. },
  17. removeItem: (key: string) => {
  18. localStorageState.delete(key);
  19. },
  20. clear: () => {
  21. localStorageState.clear();
  22. },
  23. },
  24. });
  25. Object.defineProperty(globalThis, "document", {
  26. configurable: true,
  27. value: {
  28. documentElement: {
  29. classList: {
  30. contains: () => false,
  31. toggle: () => {},
  32. },
  33. },
  34. },
  35. });
  36. const mockBroadcastService: IBroadcastService = {
  37. send: async () => {},
  38. onMessage: () => ({ dispose: () => {} }),
  39. };
  40. const mockServices = {
  41. fileService: {
  42. readMediaPreview: async () => ({
  43. path: "/workspace/assets/logo.png",
  44. mediaType: "image/png",
  45. dataBase64: "",
  46. totalBytes: 0,
  47. }),
  48. },
  49. } as IServiceAccessor;
  50. function renderSwitchModeBlock(
  51. toolCall: Parameters<typeof SwitchModeToolCallBlock>[0]["toolCallNode"]["toolCall"],
  52. ) {
  53. return renderToStaticMarkup(
  54. createElement(
  55. ZCodeIntlProvider,
  56. { initialLocale: "zh-CN" },
  57. createElement(
  58. ServiceProvider,
  59. { services: mockServices },
  60. createElement(
  61. StoreProvider,
  62. { broadcastService: mockBroadcastService },
  63. createElement(SwitchModeToolCallBlock, {
  64. toolCallNode: {
  65. toolCall,
  66. childToolCalls: [],
  67. },
  68. workspacePath: "/workspace",
  69. displayModel: {
  70. inlinePreview: { type: "none" },
  71. planResult: null,
  72. viewerSource: null,
  73. viewerLabelId: "codeViewer.viewCode",
  74. showSummaryFileLink: false,
  75. showInput: false,
  76. showOutput: false,
  77. showKind: false,
  78. },
  79. viewerSource: null,
  80. rawFileSummaries: [],
  81. isRunning: false,
  82. statusLabel: "Completed",
  83. errorText: undefined,
  84. childToolList: null,
  85. forceOpen: true,
  86. canToggle: true,
  87. showIcon: true,
  88. onOpenCodeViewer: () => {},
  89. onOpenBrowserUrl: () => {},
  90. onOpenPlanDetail: () => {},
  91. }),
  92. ),
  93. ),
  94. ),
  95. );
  96. }
  97. describe("SwitchModeToolCallBlock", () => {
  98. it("renders markdown from input.plan instead of raw json", () => {
  99. const html = renderSwitchModeBlock({
  100. toolId: "tool-switch-mode",
  101. kind: "switch_mode",
  102. title: "Exited Plan Mode",
  103. input: {
  104. plan: "# 输出计划\n\n- 渲染 markdown",
  105. planFilePath: "plans/rosy-orbiting-backus.md",
  106. },
  107. output: "...",
  108. status: "completed",
  109. raw: {
  110. rawOutput: "...",
  111. },
  112. });
  113. expect(html).toContain("输出计划");
  114. expect(html).toContain("渲染 markdown");
  115. expect(html).toContain("rosy-orbiting-backus.md");
  116. expect(html).toContain("计划");
  117. expect(html).not.toContain("3 行");
  118. expect(html).toContain("复制计划");
  119. expect(html).toContain("在侧边栏查看计划");
  120. expect(html).toContain("查看完整计划");
  121. expect(html.match(/在侧边栏查看计划/g)).toHaveLength(1);
  122. expect(html).not.toContain("展开计划");
  123. expect(html).not.toContain("收起计划");
  124. expect(html).not.toContain("&quot;plan&quot;");
  125. });
  126. });