markdownFileLink.test.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. parseMarkdownFileLinkTarget,
  4. resolveMarkdownFileLink,
  5. } from "../src/lib/markdownFileLink.js";
  6. import { stripBalancedAssistantPathQuotes } from "../src/lib/assistantPathQuotes.js";
  7. describe("parseMarkdownFileLinkTarget", () => {
  8. it("去除 rehype 产生的相对路径智能引号保护层", () => {
  9. expect(stripBalancedAssistantPathQuotes("./‘docs/说明.md’")).toBe("./docs/说明.md");
  10. expect(stripBalancedAssistantPathQuotes("/‘docs/说明.md’")).toBe("/docs/说明.md");
  11. expect(stripBalancedAssistantPathQuotes("/“./docs/说明.md”")).toBe("./docs/说明.md");
  12. expect(stripBalancedAssistantPathQuotes("%E2%80%9C./docs/说明%2520文档.md%E2%80%9D")).toBe(
  13. "./docs/说明%2520文档.md",
  14. );
  15. });
  16. it("extracts trailing line and column from absolute file paths", () => {
  17. expect(
  18. parseMarkdownFileLinkTarget(
  19. "/Users/dev/project/src/components/FeedbackSurvey/useFeedbackSurvey.tsx:30:4",
  20. ),
  21. ).toEqual({
  22. path: "/Users/dev/project/src/components/FeedbackSurvey/useFeedbackSurvey.tsx",
  23. lineNumber: 30,
  24. columnNumber: 4,
  25. });
  26. });
  27. it("keeps plain file paths unchanged when no line suffix exists", () => {
  28. expect(parseMarkdownFileLinkTarget("./src/app.ts")).toEqual({
  29. path: "./src/app.ts",
  30. lineNumber: null,
  31. columnNumber: null,
  32. });
  33. });
  34. it("extracts GitHub-style hash line ranges used by Wiki Sources", () => {
  35. expect(parseMarkdownFileLinkTarget("src/main.ts#L12-L80")).toEqual({
  36. path: "src/main.ts",
  37. lineNumber: 12,
  38. columnNumber: null,
  39. });
  40. });
  41. it.each([
  42. ["中文双引号", "“./src/report.pdf:12:3”"],
  43. ["中文单引号", "‘./src/report.pdf#L12-L80’"],
  44. ["ASCII 单引号", "'./src/report.pdf'"],
  45. ])("支持 %s 包裹的本地链接目标", (_label, href) => {
  46. expect(parseMarkdownFileLinkTarget(href)).toMatchObject({
  47. path: "./src/report.pdf",
  48. });
  49. });
  50. it("decodes uri-encoded path text while preserving line suffixes", () => {
  51. expect(
  52. parseMarkdownFileLinkTarget(
  53. "/Users/dev/ZCodeProject/323/Auto%20Snake/src/game/engine.ts:42",
  54. ),
  55. ).toEqual({
  56. path: "/Users/dev/ZCodeProject/323/Auto Snake/src/game/engine.ts",
  57. lineNumber: 42,
  58. columnNumber: null,
  59. });
  60. });
  61. it("只解码一次字面 percent-escape 文件名", () => {
  62. expect(parseMarkdownFileLinkTarget("./report%2520final.md")).toEqual({
  63. path: "./report%20final.md",
  64. lineNumber: null,
  65. columnNumber: null,
  66. });
  67. expect(parseMarkdownFileLinkTarget("./report%2520final.md:12")).toEqual({
  68. path: "./report%20final.md",
  69. lineNumber: 12,
  70. columnNumber: null,
  71. });
  72. expect(parseMarkdownFileLinkTarget("%E2%80%9C./report%2520final.md%E2%80%9D")).toEqual({
  73. path: "./report%20final.md",
  74. lineNumber: null,
  75. columnNumber: null,
  76. });
  77. });
  78. it("converts file URL targets to local file paths", () => {
  79. expect(
  80. parseMarkdownFileLinkTarget(
  81. "file:///Users/dev/ZCodeProject/todolist/src/app/gomoku/components/GameControls.tsx:18",
  82. ),
  83. ).toEqual({
  84. path: "/Users/dev/ZCodeProject/todolist/src/app/gomoku/components/GameControls.tsx",
  85. lineNumber: 18,
  86. columnNumber: null,
  87. });
  88. });
  89. it("file URL 只解码一次字面 percent-escape 文件名", () => {
  90. expect(parseMarkdownFileLinkTarget("file:///workspace/report%2520final.md")).toEqual({
  91. path: "/workspace/report%20final.md",
  92. lineNumber: null,
  93. columnNumber: null,
  94. });
  95. });
  96. it("converts Windows file URL targets to drive-letter paths", () => {
  97. expect(parseMarkdownFileLinkTarget("file:///C:/Users/dev/project/src/app.ts:12:3")).toEqual({
  98. path: "C:/Users/dev/project/src/app.ts",
  99. lineNumber: 12,
  100. columnNumber: 3,
  101. });
  102. });
  103. });
  104. describe("resolveMarkdownFileLink", () => {
  105. it("resolves workspace-relative file links against the workspace root", () => {
  106. expect(resolveMarkdownFileLink("/workspace", "./src/app.ts:12")).toEqual({
  107. path: "/workspace/src/app.ts",
  108. lineNumber: 12,
  109. columnNumber: null,
  110. });
  111. });
  112. it("解析智能引号包裹的 Markdown 文件链接", () => {
  113. expect(resolveMarkdownFileLink("/workspace", "“docs/report.pdf”")).toEqual({
  114. path: "/workspace/docs/report.pdf",
  115. lineNumber: null,
  116. columnNumber: null,
  117. });
  118. expect(resolveMarkdownFileLink("/workspace", "./‘docs/report.pdf’")).toEqual({
  119. path: "/workspace/docs/report.pdf",
  120. lineNumber: null,
  121. columnNumber: null,
  122. });
  123. });
  124. it("resolves Home-relative links against the injected Host Home", () => {
  125. expect(
  126. resolveMarkdownFileLink("/workspace", "~/Desktop/report.pdf:12", {
  127. homePath: "/Users/demo",
  128. }),
  129. ).toEqual({
  130. path: "/Users/demo/Desktop/report.pdf",
  131. lineNumber: 12,
  132. columnNumber: null,
  133. });
  134. expect(
  135. resolveMarkdownFileLink("C:\\workspace", String.raw`~\Desktop\deck.pptx`, {
  136. homePath: String.raw`C:\Users\demo`,
  137. }),
  138. ).toEqual({
  139. path: String.raw`C:\Users\demo\Desktop\deck.pptx`,
  140. lineNumber: null,
  141. columnNumber: null,
  142. });
  143. });
  144. it("fails closed for Home-relative links when Host Home is unavailable", () => {
  145. expect(resolveMarkdownFileLink("/workspace", "~/Desktop/report.pdf")).toBeNull();
  146. expect(
  147. resolveMarkdownFileLink("/workspace", "~other/Desktop/report.pdf", {
  148. homePath: "/Users/demo",
  149. }),
  150. ).toBeNull();
  151. });
  152. it("resolves bare workspace-relative file links against the workspace root", () => {
  153. expect(resolveMarkdownFileLink("/workspace", "src/app/page.tsx")).toEqual({
  154. path: "/workspace/src/app/page.tsx",
  155. lineNumber: null,
  156. columnNumber: null,
  157. });
  158. });
  159. it("rejects nested relative traversal that escapes the workspace root", () => {
  160. expect(resolveMarkdownFileLink("/workspace", "foo/../../private.pdf")).toBeNull();
  161. expect(resolveMarkdownFileLink("/workspace", "/foo/../../private.pdf")).toBeNull();
  162. expect(resolveMarkdownFileLink("/workspace", "foo/%2E%2E/%2E%2E/private.pdf")).toBeNull();
  163. expect(resolveMarkdownFileLink("C:\\workspace", "dir\\..\\..\\private.pdf")).toBeNull();
  164. });
  165. it("normalizes contained relative traversal before returning the file path", () => {
  166. expect(resolveMarkdownFileLink("/workspace", "./a/../report.pdf")).toEqual({
  167. path: "/workspace/report.pdf",
  168. lineNumber: null,
  169. columnNumber: null,
  170. });
  171. expect(resolveMarkdownFileLink("C:\\workspace", "dir\\draft\\..\\report.pdf")).toEqual({
  172. path: "C:\\workspace\\dir\\report.pdf",
  173. lineNumber: null,
  174. columnNumber: null,
  175. });
  176. });
  177. it("resolves Wiki Sources hash line ranges against the workspace root", () => {
  178. expect(resolveMarkdownFileLink("/workspace", "src/main.ts#L12-L80")).toEqual({
  179. path: "/workspace/src/main.ts",
  180. lineNumber: 12,
  181. columnNumber: null,
  182. });
  183. });
  184. it("keeps unix absolute file links absolute instead of prefixing the workspace path", () => {
  185. expect(
  186. resolveMarkdownFileLink(
  187. "/workspace",
  188. "/Users/dev/project/src/components/FeedbackSurvey/useFeedbackSurvey.tsx:30",
  189. ),
  190. ).toEqual({
  191. path: "/Users/dev/project/src/components/FeedbackSurvey/useFeedbackSurvey.tsx",
  192. lineNumber: 30,
  193. columnNumber: null,
  194. });
  195. });
  196. it("keeps absolute links inside a non-standard workspace root absolute", () => {
  197. expect(resolveMarkdownFileLink("/workspace", "/workspace/report.pdf")).toEqual({
  198. path: "/workspace/report.pdf",
  199. lineNumber: null,
  200. columnNumber: null,
  201. });
  202. });
  203. it("supports windows absolute paths with drive letters", () => {
  204. expect(
  205. resolveMarkdownFileLink(
  206. "/workspace",
  207. "C:/Users/dev/project/src/components/FeedbackSurvey/useFeedbackSurvey.tsx:30",
  208. ),
  209. ).toEqual({
  210. path: "C:/Users/dev/project/src/components/FeedbackSurvey/useFeedbackSurvey.tsx",
  211. lineNumber: 30,
  212. columnNumber: null,
  213. });
  214. });
  215. it("restores the harden-safe Windows drive path used by Markdown rendering", () => {
  216. expect(
  217. resolveMarkdownFileLink("C:\\workspace", "/C:/Users/demo/project/report.md:12:3"),
  218. ).toEqual({
  219. path: "C:/Users/demo/project/report.md",
  220. lineNumber: 12,
  221. columnNumber: 3,
  222. });
  223. });
  224. it("restores the harden-safe Windows drive path in a UNC workspace", () => {
  225. expect(
  226. resolveMarkdownFileLink("\\\\server\\share\\repo", "/C:/Users/demo/project/report.md"),
  227. ).toEqual({
  228. path: "C:/Users/demo/project/report.md",
  229. lineNumber: null,
  230. columnNumber: null,
  231. });
  232. });
  233. it("does not reinterpret a Unix /C:/ path as a Windows drive path", () => {
  234. expect(resolveMarkdownFileLink("/workspace", "/C:/Users/demo/project/report.md")).toBeNull();
  235. });
  236. it("resolves uri-encoded workspace-relative file links", () => {
  237. expect(resolveMarkdownFileLink("/workspace", "./src/Auto%20Snake/engine.ts:12")).toEqual({
  238. path: "/workspace/src/Auto Snake/engine.ts",
  239. lineNumber: 12,
  240. columnNumber: null,
  241. });
  242. });
  243. it("resolves file URL links as absolute local file links", () => {
  244. expect(
  245. resolveMarkdownFileLink(
  246. "/workspace",
  247. "file:///Users/dev/ZCodeProject/todolist/src/app/gomoku/components/GameControls.tsx",
  248. ),
  249. ).toEqual({
  250. path: "/Users/dev/ZCodeProject/todolist/src/app/gomoku/components/GameControls.tsx",
  251. lineNumber: null,
  252. columnNumber: null,
  253. });
  254. });
  255. it("resolves normalized root-relative markdown links against the workspace root", () => {
  256. expect(resolveMarkdownFileLink("/workspace", "/flappy.html")).toEqual({
  257. path: "/workspace/flappy.html",
  258. lineNumber: null,
  259. columnNumber: null,
  260. });
  261. });
  262. it("ignores non-file markdown links", () => {
  263. expect(resolveMarkdownFileLink("/workspace", "https://example.com/docs")).toBeNull();
  264. });
  265. });