The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { workspaceRelativeFilePath } from "./path.ts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | import { serializeComposerFileLink } from "./composerTrigger.ts"; |
| 4 | import { extractMarkdownLinkHrefs, resolveMarkdownFileLinkTarget } from "./markdownLinks.ts"; |
| 5 | |
| 6 | import { |
| 7 | inlineCodeFilePathCandidate, |
| 8 | isMarkdownFileLinkLabel, |
| 9 | parseMarkdownFileLink, |
| 10 | } from "./markdownLinks.ts"; |
| 11 | |
| 12 | describe("isMarkdownFileLinkLabel", () => { |
| 13 | // regression: #15509 descriptive labels are not mistaken for the linked file name |
| 14 | it.each([ |
| 15 | ["validates the input", "/repo/src/example.ts:12", false], |
| 16 | ["example.ts:12", "/repo/src/example.ts:12", true], |
| 17 | ["example.ts:99", "/repo/src/example.ts:12", false], |
| 18 | ["example.ts:12-20", "/repo/src/example.ts:12-20", true], |
| 19 | ["example.ts:12-20", "/repo/src/example.ts:12", false], |
| 20 | ])("classifies %s for %s", (label, href, expected) => { |
| 21 | expect(isMarkdownFileLinkLabel(label, href)).toBe(expected); |
| 22 | }); |
| 23 | }); |
| 24 | |
| 25 | describe("inlineCodeFilePathCandidate", () => { |
| 26 | // regression: #13909 model ids in inline code do not become file chips |
| 27 | it.each([ |
| 28 | ["src\\main.ts", "src/main.ts"], |
| 29 | ["allocator.cc:217", "allocator.cc:217"], |
| 30 | ["src/http.ts:42-64", "src/http.ts:42-64"], |
| 31 | ["http.ts:42-64", "http.ts:42-64"], |
| 32 | ["origin/main", null], |
| 33 | ["z-ai/glm-5.3", null], |
| 34 | ["share/man/ls.1", "share/man/ls.1"], |
| 35 | ["docker/Dockerfile", "docker/Dockerfile"], |
| 36 | ["src/main.ts/", null], |
| 37 | ["./patches/", "./patches/"], |
| 38 | // regression: #30757 an agent's shortened path is not a file |
| 39 | ["/tmp/claude-1000/...", null], |
| 40 | ["/tmp/claude-1000/...:12", null], |
| 41 | ["/tmp/.../scratchpad/notes.md", null], |
| 42 | ["~/projects/…/notes.md", null], |
| 43 | ["src/.../main.ts", null], |
| 44 | // regression: #22181 a `:line` suffix does not make an extensionless basename a file |
| 45 | ["I/W217:220", null], |
| 46 | ["bin/run:12", null], |
| 47 | ["docs/README:12", "docs/README:12"], |
| 48 | ["./bin/run:12", "./bin/run:12"], |
| 49 | ["src/資料.備份:12", "src/資料.備份:12"], |
| 50 | ["src/foo.c++:12", "src/foo.c++:12"], |
| 51 | // regression: #37740 absolute paths with spaces are files, commands are not |
| 52 | ["/Users/me/Path Test/notes with spaces.md", "/Users/me/Path Test/notes with spaces.md"], |
| 53 | ["~/Library/Mobile Documents/notes.md", "~/Library/Mobile Documents/notes.md"], |
| 54 | [ |
| 55 | "/Users/me/Library/CloudStorage/OneDrive-Org/Alex/Partners/Acme/Finance Use Cases/working/2026-11-16 message - draft.md", |
| 56 | "/Users/me/Library/CloudStorage/OneDrive-Org/Alex/Partners/Acme/Finance Use Cases/working/2026-11-16 message - draft.md", |
| 57 | ], |
| 58 | ["cd /tmp && ls", null], |
| 59 | ["/usr/bin/python3 -m venv .venv", null], |
| 60 | ["/usr/local/bin/node server.js", null], |
| 61 | ["/usr/bin/env python3 foo.py", null], |
| 62 | ["/opt/homebrew/bin/python3 manage.py", null], |
| 63 | ["/bin/echo hello world.txt", null], |
| 64 | ["~/bin/run tests.sh", null], |
| 65 | ["/Users/me/notes with spaces.md", "/Users/me/notes with spaces.md"], |
| 66 | ])("distinguishes file paths from code and hostnames in %s", (source, candidate) => { |
| 67 | expect(inlineCodeFilePathCandidate(source)).toBe(candidate); |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | describe("parseMarkdownFileLink", () => { |
| 72 | it.each([ |
| 73 | ["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"], |
| 74 | ["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"], |
| 75 | ["file://localhost/home/me/notes.md", "/home/me/notes.md"], |
| 76 | ["app/routes/posts.$postId.tsx", "app/routes/posts.$postId.tsx"], |
| 77 | ["src/routes/+page.svelte", "src/routes/+page.svelte"], |
| 78 | ["app/(marketing)/page.tsx:12", "app/(marketing)/page.tsx"], |
| 79 | ["pages/posts/%5Bid%5D.tsx", "pages/posts/[id].tsx"], |
| 80 | ["+page.svelte", "+page.svelte"], |
| 81 | ["[id].tsx", "[id].tsx"], |
| 82 | ["/\\server\\share\\x.ts", "/\\server\\share\\x.ts"], |
| 83 | ])("recognizes %s as a file", (href, path) => { |
| 84 | expect(parseMarkdownFileLink(href)?.path).toBe(path); |
| 85 | }); |
| 86 | |
| 87 | it.each([ |
| 88 | "app/routes/posts.$postId.tsx", |
| 89 | "pages/posts/[id].tsx", |
| 90 | "docs/My File (draft).md", |
| 91 | "@scope/package.json", |
| 92 | "$postId.tsx", |
| 93 | ])("reads back the composer's link to %s", (path) => { |
| 94 | const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1]; |
| 95 | expect(parseMarkdownFileLink(destination ?? "")?.path).toBe(path); |
| 96 | }); |
| 97 | |
| 98 | it.each([ |
| 99 | "https://example.com/docs", |
| 100 | "/chat/settings#L3", |
| 101 | "TODO:12", |
| 102 | "patches", |
| 103 | "me@example.com", |
| 104 | "foo(bar)", |
| 105 | "$HOME", |
| 106 | "/tmp/claude-1000/...", |
| 107 | "/tmp/claude-1000/....:12", |
| 108 | "/tmp/claude-1000/%2E%2E%2E", |
| 109 | "\\foo.ts", |
| 110 | ])("does not treat %s as a file", (href) => { |
| 111 | expect(parseMarkdownFileLink(href)).toBeNull(); |
| 112 | }); |
| 113 | |
| 114 | it("recognizes a relative folder that ends in a slash", () => { |
| 115 | expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" }); |
| 116 | expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/"); |
| 117 | }); |
| 118 | |
| 119 | // regression: #16096 relative links with non-ASCII names open as files |
| 120 | it("recognizes relative paths with non-ASCII names", () => { |
| 121 | expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe( |
| 122 | "pastexam/線性代數/exam_易志偉.pdf", |
| 123 | ); |
| 124 | expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf"); |
| 125 | expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份"); |
| 126 | }); |
| 127 | |
| 128 | it("accepts conventional extensionless names with or without a position", () => { |
| 129 | expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" }); |
| 130 | expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 }); |
| 131 | expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" }); |
| 132 | }); |
| 133 | |
| 134 | it("reads line ranges and opens them at their first line", () => { |
| 135 | expect(parseMarkdownFileLink("/repo/src/http.ts:42-64")).toEqual({ |
| 136 | path: "/repo/src/http.ts", |
| 137 | line: 42, |
| 138 | endLine: 64, |
| 139 | }); |
| 140 | expect(parseMarkdownFileLink("/repo/src/http.ts#L42-L64")).toEqual({ |
| 141 | path: "/repo/src/http.ts", |
| 142 | line: 42, |
| 143 | endLine: 64, |
| 144 | }); |
| 145 | }); |
| 146 | |
| 147 | it("reads positions from suffixes and line anchors", () => { |
| 148 | expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({ |
| 149 | path: "/Users/julius/project/src/main.ts", |
| 150 | line: 42, |
| 151 | column: 7, |
| 152 | }); |
| 153 | expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({ |
| 154 | path: "\\\\server\\share\\src\\main.ts", |
| 155 | line: 42, |
| 156 | column: 7, |
| 157 | }); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | describe("workspaceRelativeFilePath", () => { |
| 162 | // regression: #12449 workspace root links resolve to "." |
| 163 | it.each([ |
| 164 | ["/repo/project", "/repo/project", "."], |
| 165 | ["C:/", "c:/", "."], |
| 166 | ["/repo/project/a.ts", undefined, null], |
| 167 | ])("relates %s to %s", (path, workspaceRoot, relativePath) => { |
| 168 | expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath); |
| 169 | }); |
| 170 | }); |
| 171 | |
| 172 | describe("extractMarkdownLinkHrefs", () => { |
| 173 | it.each([ |
| 174 | ["[".repeat(32_000), []], |
| 175 | ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]], |
| 176 | ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]], |
| 177 | [`See ${serializeComposerFileLink("pages/posts/[id].tsx")} now`, ["pages/posts/%5Bid%5D.tsx"]], |
| 178 | ])("extracts links from incomplete and nested text", (markdown, hrefs) => { |
| 179 | expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs); |
| 180 | }); |
| 181 | |
| 182 | it.each([ |
| 183 | [ |
| 184 | "[a](/x/app/(a)/page.tsx) [b](/x/app/(b)/page.tsx)", |
| 185 | ["/x/app/(a)/page.tsx", "/x/app/(b)/page.tsx"], |
| 186 | ], |
| 187 | ['[a](/tmp/a(1).txt "title") after', ["/tmp/a(1).txt"]], |
| 188 | ["[a](a(b(c))d) [b](ok)", ["a(b(c))d", "ok"]], |
| 189 | ["[a](x\\)y) [b](x\\(y)", ["x\\)y", "x\\(y"]], |
| 190 | ["[a](unclosed(paren) [b](ok)", ["ok"]], |
| 191 | ["[a](" + "(".repeat(32) + "x" + ")".repeat(32) + ")", ["(".repeat(32) + "x" + ")".repeat(32)]], |
| 192 | ["[a](" + "(".repeat(33) + "x" + ")".repeat(33) + ") [b](ok)", ["ok"]], |
| 193 | ])("keeps balanced parentheses in a destination like the parser: %s", (markdown, hrefs) => { |
| 194 | expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs); |
| 195 | }); |
| 196 | }); |
| 197 | |
| 198 | describe("resolveMarkdownFileLinkTarget", () => { |
| 199 | it("resolves a relative folder with a trailing slash against cwd", () => { |
| 200 | expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe( |
| 201 | "/repo/project/patches/", |
| 202 | ); |
| 203 | expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull(); |
| 204 | }); |
| 205 | |
| 206 | // regression: #33625 backslash-separated relative links open the workspace file |
| 207 | it.each([ |
| 208 | ["src/Example.cs", "C:\\repo\\src\\Example.cs"], |
| 209 | ["src\\Example.cs", "C:\\repo\\src\\Example.cs"], |
| 210 | ["src%5CExample.cs", "C:\\repo\\src\\Example.cs"], |
| 211 | ["src\\Example.cs:3", "C:\\repo\\src\\Example.cs:3"], |
| 212 | ])("resolves the relative link %s against a windows cwd", (href, target) => { |
| 213 | expect(resolveMarkdownFileLinkTarget(href, "C:\\repo")).toBe(target); |
| 214 | }); |
| 215 | |
| 216 | it("keeps a backslash in a relative link that already names a file", () => { |
| 217 | expect(resolveMarkdownFileLinkTarget("./foo%5Cbar.ts", "/repo")).toBe("/repo/foo\\bar.ts"); |
| 218 | }); |
| 219 | |
| 220 | it("does not treat filename line references as external schemes", () => { |
| 221 | expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe( |
| 222 | "/Users/julius/project/script.ts:10", |
| 223 | ); |
| 224 | }); |
| 225 | |
| 226 | it("maps #L line anchors to editor line suffixes", () => { |
| 227 | expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe( |
| 228 | "/Users/julius/project/src/main.ts:42:7", |
| 229 | ); |
| 230 | }); |
| 231 | |
| 232 | it("ignores external urls", () => { |
| 233 | expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull(); |
| 234 | expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull(); |
| 235 | }); |
| 236 | |
| 237 | it("does not double-decode file URLs", () => { |
| 238 | expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe( |
| 239 | "/Users/julius/project/file%20name.md", |
| 240 | ); |
| 241 | }); |
| 242 | |
| 243 | it("links a network path only inside the project's share", () => { |
| 244 | const cwd = "\\\\server\\share"; |
| 245 | expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe( |
| 246 | "\\\\server\\share\\workspace-image.svg", |
| 247 | ); |
| 248 | // Opening these sent the Windows user's NTLM credentials to attacker.test. |
| 249 | for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) { |
| 250 | expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull(); |
| 251 | } |
| 252 | }); |
| 253 | |
| 254 | it("keeps an encoded final space in the absolute target", () => { |
| 255 | expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe( |
| 256 | "/tmp/repo/file.ts ", |
| 257 | ); |
| 258 | }); |
| 259 | |
| 260 | it("normalizes slash-prefixed windows drive paths before resolving", () => { |
| 261 | expect( |
| 262 | resolveMarkdownFileLinkTarget( |
| 263 | "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69", |
| 264 | ), |
| 265 | ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69"); |
| 266 | }); |
| 267 | |
| 268 | it("decodes an encoded drive colon in a file uri before dropping its slash", () => { |
| 269 | expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe( |
| 270 | "c:/Users/x/shot.png", |
| 271 | ); |
| 272 | }); |
| 273 | }); |