patchDiffOptions.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. getGitPaneDiffPreviewPlan,
  4. shouldRenderPlainTextDiffPreview,
  5. } from "../src/GitPane/helpers.js";
  6. import { createPatchDiffOptions } from "../src/lib/patchDiffOptions.js";
  7. const SETTINGS = {
  8. lightTheme: "github-light",
  9. darkTheme: "github-dark",
  10. showLineNumbers: true,
  11. wrapLongLines: false,
  12. } as const;
  13. describe("createPatchDiffOptions", () => {
  14. it("keeps line-info separators by default", () => {
  15. const options = createPatchDiffOptions(SETTINGS, "light");
  16. expect(options.hunkSeparators).toBe("line-info");
  17. });
  18. it("supports simple separators for compact diff previews", () => {
  19. const options = createPatchDiffOptions(SETTINGS, "light", {
  20. separatorStyle: "simple",
  21. });
  22. expect(options.hunkSeparators).toBe("simple");
  23. });
  24. it("patch diff 与 worker 池使用同一个 WASM 高亮引擎", () => {
  25. const options = createPatchDiffOptions(SETTINGS, "dark");
  26. expect(options.preferredHighlighter).toBe("shiki-wasm");
  27. });
  28. });
  29. describe("shouldRenderPlainTextDiffPreview", () => {
  30. it("falls back for created plain text patches on review surfaces", () => {
  31. const patch = [
  32. "--- /dev/null",
  33. "+++ b/demo.txt",
  34. "@@ -0,0 +1,2 @@",
  35. "+hello",
  36. "+world",
  37. ].join("\n");
  38. expect(shouldRenderPlainTextDiffPreview(patch)).toEqual([
  39. "+hello",
  40. "+world",
  41. ]);
  42. });
  43. it("hides unified diff metadata and keeps no-newline markers", () => {
  44. const patch = [
  45. "--- /dev/null",
  46. "+++ b/demo.txt",
  47. "@@ -0,0 +1,1 @@",
  48. "+hello world",
  49. "\\ No newline at end of file",
  50. "",
  51. ].join("\n");
  52. expect(shouldRenderPlainTextDiffPreview(patch)).toEqual([
  53. "+hello world",
  54. "\\ No newline at end of file",
  55. ]);
  56. });
  57. it("keeps structured file types on PatchDiff path", () => {
  58. const patch = [
  59. "--- /dev/null",
  60. "+++ b/demo.json",
  61. "@@ -0,0 +1,1 @@",
  62. "+{}",
  63. ].join("\n");
  64. expect(shouldRenderPlainTextDiffPreview(patch)).toBeNull();
  65. });
  66. });
  67. describe("getGitPaneDiffPreviewPlan", () => {
  68. it("uses patch-only rendering when full before/after content is large", () => {
  69. const beforeContent = Array.from(
  70. { length: 1_300 },
  71. (_, index) => `line-${index + 1}`,
  72. ).join("\n");
  73. const afterContent = beforeContent.replace("line-10", "line-10 changed");
  74. expect(
  75. getGitPaneDiffPreviewPlan({
  76. path: "/workspace/demo.ts",
  77. availability: "patch",
  78. patch: [
  79. "--- a/demo.ts",
  80. "+++ b/demo.ts",
  81. "@@ -8,5 +8,5 @@",
  82. " line-8",
  83. " line-9",
  84. "-line-10",
  85. "+line-10 changed",
  86. " line-11",
  87. ].join("\n"),
  88. beforeContent,
  89. afterContent,
  90. summary: null,
  91. }),
  92. ).toEqual({ kind: "patch" });
  93. });
  94. it("uses plain text when a large created structured file would be expensive", () => {
  95. const afterLines = Array.from(
  96. { length: 1_300 },
  97. (_, index) => `+ "line-${index + 1}": true,`,
  98. );
  99. const plan = getGitPaneDiffPreviewPlan({
  100. path: "/workspace/demo.json",
  101. availability: "patch",
  102. patch: [
  103. "--- /dev/null",
  104. "+++ b/demo.json",
  105. "@@ -0,0 +1,1300 @@",
  106. ...afterLines,
  107. ].join("\n"),
  108. beforeContent: "",
  109. afterContent: afterLines.map((line) => line.slice(1)).join("\n"),
  110. summary: null,
  111. });
  112. expect(plan.kind).toBe("plain-text");
  113. expect(plan.kind === "plain-text" ? plan.lines.at(0) : null).toBe(
  114. '+ "line-1": true,',
  115. );
  116. });
  117. it("keeps unsafe patches on the lightweight fallback when full contents are unavailable", () => {
  118. const afterLines = Array.from(
  119. { length: 1_300 },
  120. (_, index) => `+ "line-${index + 1}": true,`,
  121. );
  122. const plan = getGitPaneDiffPreviewPlan({
  123. path: "/workspace/demo.json",
  124. availability: "patch",
  125. patch: [
  126. "--- /dev/null",
  127. "+++ b/demo.json",
  128. "@@ -0,0 +1,1300 @@",
  129. ...afterLines,
  130. ].join("\n"),
  131. beforeContent: null,
  132. afterContent: null,
  133. summary: null,
  134. });
  135. expect(plan.kind).toBe("plain-text");
  136. expect(plan.kind === "plain-text" ? plan.lines.at(0) : null).toBe(
  137. '+ "line-1": true,',
  138. );
  139. });
  140. });