planGuidanceToolCallBlock.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import type { IBroadcastService, IServiceAccessor } from "@zcode/services";
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { describe, expect, it } from "vitest";
  5. import { ServiceProvider } from "@/hooks/useServices.js";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import { StoreProvider } from "@/store/StoreProvider.js";
  8. import { PlanGuidanceToolCallBlock } from "../src/ToolCallBlocks/renderers/plan-guidance.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 renderPlanGuidanceBlock(
  51. toolCall: Parameters<typeof PlanGuidanceToolCallBlock>[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(PlanGuidanceToolCallBlock, {
  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. }),
  91. ),
  92. ),
  93. ),
  94. );
  95. }
  96. describe("PlanGuidanceToolCallBlock", () => {
  97. it("renders EnterPlanMode guidance as markdown inside a tool card", () => {
  98. const html = renderPlanGuidanceBlock({
  99. toolId: "tool-enter-plan-guidance",
  100. kind: "other",
  101. title: "EnterPlanMode",
  102. input: {},
  103. output:
  104. "Entered plan mode.\n\n1. Thoroughly explore the codebase\n2. Consider trade-offs",
  105. status: "completed",
  106. raw: {
  107. _meta: {
  108. claudeCode: {
  109. toolName: "EnterPlanMode",
  110. },
  111. },
  112. },
  113. });
  114. expect(html).toContain("已开启 Plan Mode");
  115. expect(html).toContain("EnterPlanMode");
  116. expect(html).toContain("Entered plan mode.");
  117. expect(html).toContain("Thoroughly explore the codebase");
  118. expect(html).not.toContain("&quot;rawOutput&quot;");
  119. });
  120. });