codeViewer.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import { describe, expect, it } from "vitest";
  2. import { getSingularPatch } from "@pierre/diffs";
  3. import {
  4. buildUnifiedDiff,
  5. getToolCallCodeContentPreview,
  6. getToolCallCodePreview,
  7. inferImageMediaType,
  8. isPdfPreviewPath,
  9. isPptxPreviewPath,
  10. } from "../src/lib/codeViewer.js";
  11. describe("codeViewer helpers", () => {
  12. it("infers image media types from file extensions", () => {
  13. expect(inferImageMediaType("/tmp/example.png")).toBe("image/png");
  14. expect(inferImageMediaType("/tmp/example.svg")).toBe("image/svg+xml");
  15. expect(inferImageMediaType("/tmp/example.txt")).toBeNull();
  16. });
  17. it("detects pdf preview paths case-insensitively", () => {
  18. expect(isPdfPreviewPath("/tmp/report.pdf")).toBe(true);
  19. expect(isPdfPreviewPath("/tmp/REPORT.PDF")).toBe(true);
  20. expect(isPdfPreviewPath("/tmp/archive.pdf.zip")).toBe(false);
  21. expect(isPdfPreviewPath("/tmp/example.txt")).toBe(false);
  22. expect(isPdfPreviewPath(undefined)).toBe(false);
  23. });
  24. it("detects pptx preview paths case-insensitively", () => {
  25. expect(isPptxPreviewPath("/tmp/slides.pptx")).toBe(true);
  26. expect(isPptxPreviewPath("/tmp/SLIDES.PPTX")).toBe(true);
  27. expect(isPptxPreviewPath("/tmp/archive.pptx.zip")).toBe(false);
  28. expect(isPptxPreviewPath("/tmp/legacy.ppt")).toBe(false);
  29. expect(isPptxPreviewPath(undefined)).toBe(false);
  30. });
  31. it("builds a unified diff for simple edits", () => {
  32. const diff = buildUnifiedDiff("const a = 1;\nreturn a;", "const a = 2;\nreturn a;", "demo.ts");
  33. expect(diff).toContain("--- a/demo.ts");
  34. expect(diff).toContain("+++ b/demo.ts");
  35. expect(diff).toContain("-const a = 1;");
  36. expect(diff).toContain("+const a = 2;");
  37. });
  38. it("keeps deleted SQL comments inside one generated file diff", () => {
  39. const diff = buildUnifiedDiff(
  40. "-- old table comment\nSELECT 1;",
  41. "-- new table comment\nSELECT 1;",
  42. "schema.sql",
  43. );
  44. expect(diff).toMatch(/^diff --git a\/schema\.sql b\/schema\.sql/);
  45. expect(() => getSingularPatch(diff ?? "")).not.toThrow();
  46. });
  47. it("marks newly created files with a /dev/null old path", () => {
  48. const diff = buildUnifiedDiff("", "export const value = 1;", "demo.ts");
  49. expect(diff).toContain("--- /dev/null");
  50. expect(diff).toContain("+++ b/demo.ts");
  51. expect(diff).toContain("@@ -0,0 +1,1 @@");
  52. expect(diff).not.toContain("--- a/demo.ts");
  53. });
  54. it("keeps diff preview available for large files with small edits", () => {
  55. const beforeLines = Array.from({ length: 280 }, (_, index) =>
  56. index === 140 ? "const snakeHeadColor = '#2ecc71';" : `line ${index};`,
  57. );
  58. const afterLines = [...beforeLines];
  59. afterLines[140] = "const snakeHeadColor = '#f39c12';";
  60. const diff = buildUnifiedDiff(beforeLines.join("\n"), afterLines.join("\n"), "snake.html");
  61. expect(diff).not.toBeNull();
  62. expect(diff).toContain("-const snakeHeadColor = '#2ecc71';");
  63. expect(diff).toContain("+const snakeHeadColor = '#f39c12';");
  64. });
  65. it("can trim unchanged context around edits", () => {
  66. const beforeLines = Array.from({ length: 20 }, (_, index) =>
  67. index === 10 ? "const snakeHeadColor = '#2ecc71';" : `line ${index};`,
  68. );
  69. const afterLines = [...beforeLines];
  70. afterLines[10] = "const snakeHeadColor = '#f39c12';";
  71. const diff = buildUnifiedDiff(beforeLines.join("\n"), afterLines.join("\n"), "snake.html", {
  72. contextLines: 3,
  73. });
  74. expect(diff).toContain("@@ -8,7 +8,7 @@");
  75. expect(diff).toContain(" line 7;");
  76. expect(diff).toContain(" line 13;");
  77. expect(diff).not.toContain(" line 1;");
  78. expect(diff).not.toContain(" line 18;");
  79. expect(diff).toContain("-const snakeHeadColor = '#2ecc71';");
  80. expect(diff).toContain("+const snakeHeadColor = '#f39c12';");
  81. });
  82. it("preserves distant small insertions as separate hunks instead of a giant replacement", () => {
  83. const beforeLines = [
  84. ...Array.from({ length: 33 }, (_, index) => `head ${index};`),
  85. "type ActiveSession = RuntimeSessionState",
  86. ...Array.from({ length: 301 }, (_, index) => `middle ${index};`),
  87. "let tools = runtime.tools",
  88. ...Array.from({ length: 115 }, (_, index) => `tail ${index};`),
  89. ];
  90. const afterLines = [
  91. ...beforeLines.slice(0, 33),
  92. "import { reportAppLaunch, reportDailyActive } from './telemetry/index.js'",
  93. ...beforeLines.slice(33, 335),
  94. "// Fire-and-forget telemetry for app launch and daily active",
  95. "reportAppLaunch().catch(() => {})",
  96. "reportDailyActive().catch(() => {})",
  97. "",
  98. ...beforeLines.slice(335),
  99. ];
  100. const diff = buildUnifiedDiff(beforeLines.join("\n"), afterLines.join("\n"), "src/cli.ts", {
  101. contextLines: 3,
  102. });
  103. expect(diff).toContain(
  104. "+import { reportAppLaunch, reportDailyActive } from './telemetry/index.js'",
  105. );
  106. expect(diff).toContain("+// Fire-and-forget telemetry for app launch and daily active");
  107. expect(diff).toContain("+reportAppLaunch().catch(() => {})");
  108. expect(diff).toContain("+reportDailyActive().catch(() => {})");
  109. expect(diff?.match(/^@@ /gm)).toHaveLength(2);
  110. expect(diff).not.toContain("-type ActiveSession = RuntimeSessionState");
  111. expect(diff).not.toContain("-middle 0;");
  112. });
  113. it("extracts inline code preview from write tools", () => {
  114. const preview = getToolCallCodePreview(
  115. {
  116. toolId: "tool-1",
  117. kind: "write",
  118. title: "写入 src/app.ts",
  119. input: { path: "src/app.ts", content: "export const value = 1;\n" },
  120. status: "completed",
  121. },
  122. "/workspace",
  123. );
  124. expect(preview).toEqual({
  125. type: "text",
  126. title: "写入 src/app.ts",
  127. path: "/workspace/src/app.ts",
  128. content: "export const value = 1;\n",
  129. language: "typescript",
  130. });
  131. });
  132. it("decodes uri-encoded file paths before opening a file preview", () => {
  133. const preview = getToolCallCodePreview(
  134. {
  135. toolId: "tool-uri-path",
  136. kind: "read",
  137. title: "读取 engine.ts",
  138. input: {
  139. file_path: "/Users/dev/ZCodeProject/323/Auto%20Snake/src/game/engine.ts",
  140. },
  141. status: "completed",
  142. },
  143. "/workspace",
  144. );
  145. expect(preview).toEqual({
  146. type: "file",
  147. title: "读取 engine.ts",
  148. path: "/Users/dev/ZCodeProject/323/Auto Snake/src/game/engine.ts",
  149. });
  150. });
  151. it("decodes uri-encoded relative paths against the workspace root", () => {
  152. const preview = getToolCallCodePreview(
  153. {
  154. toolId: "tool-relative-uri-path",
  155. kind: "read",
  156. title: "",
  157. input: { path: "src/Auto%20Snake/engine.ts" },
  158. status: "completed",
  159. },
  160. "/workspace",
  161. );
  162. expect(preview).toEqual({
  163. type: "file",
  164. title: "engine.ts",
  165. path: "/workspace/src/Auto Snake/engine.ts",
  166. });
  167. });
  168. it("extracts patch preview from edit tools", () => {
  169. const preview = getToolCallCodePreview(
  170. {
  171. toolId: "tool-2",
  172. kind: "edit",
  173. title: "修改 src/app.ts",
  174. input: {
  175. path: "src/app.ts",
  176. old_string: "const a = 1;",
  177. new_string: "const a = 2;",
  178. },
  179. status: "completed",
  180. },
  181. "/workspace",
  182. );
  183. expect(preview?.type).toBe("patch");
  184. if (!preview || preview.type !== "patch") {
  185. throw new Error("Expected patch preview");
  186. }
  187. expect(preview.path).toBe("/workspace/src/app.ts");
  188. expect(preview.patch).toContain("-const a = 1;");
  189. expect(preview.patch).toContain("+const a = 2;");
  190. });
  191. it("extracts updated code content preview from edit tools", () => {
  192. const preview = getToolCallCodeContentPreview(
  193. {
  194. toolId: "tool-2-content",
  195. kind: "edit",
  196. title: "修改 src/app.ts",
  197. input: {
  198. path: "src/app.ts",
  199. old_string: "const a = 1;",
  200. new_string: "const a = 2;",
  201. },
  202. status: "completed",
  203. },
  204. "/workspace",
  205. );
  206. expect(preview).toEqual({
  207. type: "text",
  208. title: "修改 src/app.ts",
  209. path: "/workspace/src/app.ts",
  210. content: "const a = 2;",
  211. language: "typescript",
  212. });
  213. });
  214. it("extracts patch preview from raw diff content blocks", () => {
  215. const preview = getToolCallCodePreview(
  216. {
  217. toolId: "tool-raw-diff",
  218. kind: "edit",
  219. title: "/workspace/src/app.ts",
  220. input: {
  221. content: "const a = 2;\n",
  222. filePath: "/workspace/src/app.ts",
  223. },
  224. output: {
  225. output: "Wrote file successfully.",
  226. },
  227. raw: {
  228. content: [
  229. {
  230. type: "content",
  231. content: {
  232. type: "text",
  233. text: "Wrote file successfully.",
  234. },
  235. },
  236. {
  237. type: "diff",
  238. path: "/workspace/src/app.ts",
  239. oldText: "const a = 1;\n",
  240. newText: "const a = 2;\n",
  241. },
  242. ],
  243. },
  244. status: "completed",
  245. },
  246. "/workspace",
  247. );
  248. expect(preview?.type).toBe("patch");
  249. if (!preview || preview.type !== "patch") {
  250. throw new Error("Expected patch preview");
  251. }
  252. expect(preview.path).toBe("/workspace/src/app.ts");
  253. expect(preview.patch).toContain("-const a = 1;");
  254. expect(preview.patch).toContain("+const a = 2;");
  255. });
  256. it("extracts updated code content from raw diff blocks", () => {
  257. const preview = getToolCallCodeContentPreview(
  258. {
  259. toolId: "tool-raw-diff-content",
  260. kind: "edit",
  261. title: "/workspace/src/app.ts",
  262. input: {
  263. content: "const a = 2;\n",
  264. filePath: "/workspace/src/app.ts",
  265. },
  266. output: {
  267. output: "Wrote file successfully.",
  268. },
  269. raw: {
  270. content: [
  271. {
  272. type: "diff",
  273. path: "/workspace/src/app.ts",
  274. oldText: "const a = 1;\n",
  275. newText: "const a = 2;\n",
  276. },
  277. ],
  278. },
  279. status: "completed",
  280. },
  281. "/workspace",
  282. );
  283. expect(preview).toEqual({
  284. type: "text",
  285. title: "/workspace/src/app.ts",
  286. path: "/workspace/src/app.ts",
  287. content: "const a = 2;\n",
  288. language: "typescript",
  289. });
  290. });
  291. it("extracts image previews from file-oriented tools", () => {
  292. const preview = getToolCallCodePreview(
  293. {
  294. toolId: "tool-image-read",
  295. kind: "read",
  296. title: "查看 logo.png",
  297. input: {
  298. path: "assets/logo.png",
  299. },
  300. status: "completed",
  301. },
  302. "/workspace",
  303. );
  304. expect(preview).toEqual({
  305. type: "image",
  306. title: "查看 logo.png",
  307. path: "/workspace/assets/logo.png",
  308. mediaType: "image/png",
  309. });
  310. });
  311. });