Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 isLocalEditorFileHref,
5 resolveInlineCodeFileLinkMeta,
6 resolveMarkdownFileLinkMeta,
7 rewriteMarkdownFileUriHref,
8} from "./markdown-links";
9import {
10 markdownFileCopyPaths,
11 markdownFileEditorTarget,
12 resolveMarkdownFileHostPath,
13} from "./markdownFileHostPath";
14
15describe("isLocalEditorFileHref", () => {
16 it("accepts absolute file deep links of known editors", () => {
17 for (const href of [
18 "cursor://file/Users/me/repo/file.md:186",
19 "vscode://file/Users/me/repo/file.md:3:7",
20 "vscode-insiders://file/Users/me/repo/file.md",
21 "VSCODE://file/C:/Users/me/repo/file.md:3",
22 ]) {
23 expect(isLocalEditorFileHref(href), href).toBe(true);
24 }
25 });
26
27 it("rejects other hosts, userinfo, relative paths, and unknown schemes", () => {
28 for (const href of [
29 "cursor://anysphere.cursor-deeplink/prompt?text=hi",
30 "vscode://user@file/Users/me/repo/file.md",
31 "vscode://file:8080/Users/me/repo/file.md",
32 "vscode://vscode-remote/ssh-remote+example.com/home/user/project",
33 "cursor://file",
34 "cursor:file/Users/me/repo/file.md",
35 "foo://file/Users/me/repo/file.md",
36 "zed://file/Users/me/repo/file.md",
37 "javascript:alert(1)",
38 "./file.md",
39 "",
40 ]) {
41 expect(isLocalEditorFileHref(href), href).toBe(false);
42 }
43 });
44});
45
46describe("rewriteMarkdownFileUriHref", () => {
47 it("normalizes file uri hrefs for windows drive paths", () => {
48 expect(
49 rewriteMarkdownFileUriHref(
50 "file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
51 ),
52 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
53 });
54
55 it("keeps the scheme when only it marks the path as a file", () => {
56 const href = "file:///nix/store/test/bin/tool#L10-L20";
57 expect(rewriteMarkdownFileUriHref(href)).toBe(href);
58 expect(resolveMarkdownFileLinkMeta(href, "/repo")).toMatchObject({
59 targetPath: "/nix/store/test/bin/tool:10",
60 line: 10,
61 endLine: 20,
62 });
63 expect(resolveMarkdownFileLinkMeta("/nix/store/test/bin/tool#L10-L20", "/repo")).toBeNull();
64 });
65});
66
67describe("relative links inside a rendered host file", () => {
68 it("anchor to the file's directory while workspace membership follows cwd", () => {
69 const meta = resolveMarkdownFileLinkMeta("appendix.md", "/repo", "/tmp/report");
70 expect(meta).toMatchObject({
71 filePath: "/tmp/report/appendix.md",
72 workspaceRelativePath: null,
73 });
74 const inline = resolveInlineCodeFileLinkMeta("Makefile:12", "/repo", "/tmp/report");
75 expect(inline).toMatchObject({ filePath: "/tmp/report/Makefile", line: 12 });
76 expect(resolveMarkdownFileLinkMeta("src/main.ts", "/repo", "/repo/docs")).toMatchObject({
77 filePath: "/repo/docs/src/main.ts",
78 workspaceRelativePath: "docs/src/main.ts",
79 });
80 });
81});
82
83describe("resolveInlineCodeFileLinkMeta", () => {
84 it("links bare filenames only when a line suffix marks them as file references", () => {
85 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
86 targetPath: "/Users/julius/project/script.ts:10",
87 line: 10,
88 });
89 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
90 });
91
92 it("opens a line range at its first line and keeps the range for the label", () => {
93 expect(resolveInlineCodeFileLinkMeta("src/http.ts:42-64", "/repo")).toMatchObject({
94 targetPath: "/repo/src/http.ts:42",
95 line: 42,
96 endLine: 64,
97 });
98 });
99
100 it("does not treat arbitrary name:digits shapes as files", () => {
101 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
102 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
103 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
104 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
105 expect(resolveInlineCodeFileLinkMeta("port:3000-3010", "/Users/julius/project")).toBeNull();
106 expect(
107 resolveInlineCodeFileLinkMeta("localhost:3000-3010", "/Users/julius/project"),
108 ).toBeNull();
109 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
110 });
111
112 it("ignores hosts, ports, and versions", () => {
113 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
114 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
115 expect(
116 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
117 ).toBeNull();
118 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
119 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
120 expect(
121 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
122 ).toBeNull();
123 expect(
124 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
125 ).toBeNull();
126 });
127});
128
129describe("chat file chip paths", () => {
130 const cwd = String.raw`C:\Users\alice\project`;
131
132 it("opens and copies the indexed file for a bare filename, preserving its line and column", async () => {
133 const meta = resolveMarkdownFileLinkMeta("user.ts:261:7", cwd);
134 if (!meta) throw new Error("Expected a file link");
135 const hostPath = await resolveMarkdownFileHostPath({
136 meta,
137 cwd,
138 findWorkspaceMatch: async () => "src/discord/events/user.ts",
139 });
140
141 expect(markdownFileEditorTarget(meta, hostPath)).toBe(
142 String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
143 );
144 expect(markdownFileCopyPaths(meta, hostPath, cwd)).toEqual({
145 fullPath: String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
146 relativePath: "src/discord/events/user.ts:261:7",
147 });
148 });
149});