codeViewer.test.ts 11 KB

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