editInlineDiffContent.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import {
  5. buildInlineDiffHighlightCode,
  6. EditInlineDiffContent,
  7. getInlineDiffHighlightLine,
  8. } from "@/ToolCallBlocks/renderers/EditInlineDiffContent.js";
  9. import type { PatchCodeViewerSource } from "@/lib/codeViewer.js";
  10. // M5②.5 store 耦合剥离:EditInlineDiffContent 不再自取 store,
  11. // theme / codePreviewSettings 改为 props 传入(见下方 render 调用),无需再 mock StoreProvider。
  12. const TEST_CODE_PREVIEW_SETTINGS = {
  13. darkTheme: "github-dark",
  14. fontSizePx: 12,
  15. lightTheme: "github-light",
  16. showLineNumbers: true,
  17. wrapLongLines: false,
  18. } as const;
  19. vi.mock("@/i18n/IntlProvider.js", () => ({
  20. useZCodeIntl: () => ({
  21. intl: {
  22. formatMessage: (
  23. { id }: { id: string },
  24. values?: Record<string, string>,
  25. ) =>
  26. id === "diff.preview.truncatedLines"
  27. ? `omitted ${values?.count ?? ""}`
  28. : id,
  29. },
  30. }),
  31. }));
  32. describe("EditInlineDiffContent", () => {
  33. it("builds async syntax highlight input without diff markers", () => {
  34. expect(getInlineDiffHighlightLine("+ const next = 1;")).toEqual({
  35. code: " const next = 1;",
  36. marker: "+",
  37. });
  38. expect(getInlineDiffHighlightLine("- const prev = 0;")).toEqual({
  39. code: " const prev = 0;",
  40. marker: "-",
  41. });
  42. expect(getInlineDiffHighlightLine(" return next;")).toEqual({
  43. code: " return next;",
  44. marker: " ",
  45. });
  46. expect(
  47. buildInlineDiffHighlightCode([
  48. "+ const next = 1;",
  49. "- const prev = 0;",
  50. ]),
  51. ).toBe(" const next = 1;\n const prev = 0;");
  52. });
  53. it("renders lightweight hunk code without diff protocol markers", () => {
  54. const preview: PatchCodeViewerSource = {
  55. path: "/workspace/demo.ts",
  56. patch: [
  57. "--- a/demo.ts",
  58. "+++ b/demo.ts",
  59. "@@ -1,2 +1,2 @@",
  60. " const a = 1;",
  61. "-old",
  62. "+new",
  63. ].join("\n"),
  64. title: "demo.ts",
  65. type: "patch",
  66. };
  67. const html = renderToStaticMarkup(
  68. createElement(EditInlineDiffContent, {
  69. preview,
  70. theme: "zai-dark",
  71. codePreviewSettings: TEST_CODE_PREVIEW_SETTINGS,
  72. }),
  73. );
  74. expect(html).toContain("data-inline-diff-preview");
  75. expect(html).toContain("data-lightweight-diff-preview");
  76. expect(html).toContain('data-inline-diff-highlight-language="typescript"');
  77. expect(html).toContain('data-inline-diff-highlight-theme="github-dark"');
  78. expect(html).toContain('data-lightweight-diff-highlighted="false"');
  79. expect(html).not.toContain("diffs-container");
  80. expect(html).toContain("const a = 1;");
  81. expect(html).toContain("old");
  82. expect(html).toContain("new");
  83. expect(html).not.toContain("-old");
  84. expect(html).not.toContain("+new");
  85. expect(html).not.toContain("--- a/demo.ts");
  86. expect(html).not.toContain("@@ -1");
  87. });
  88. });