Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

markdown-links.test.ts

157 lines6.1 KB

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("preserves encoded octets so file paths are decoded only once later", () => {
48 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/file%2520name.md")).toBe(
49 "/Users/julius/project/file%2520name.md",
50 );
51 });
52​
53 it("normalizes file uri hrefs for windows drive paths", () => {
54 expect(
55 rewriteMarkdownFileUriHref(
56 "file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
57 ),
58 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
59 });
60​
61 it("preserves file uri authorities as windows UNC paths", () => {
62 expect(rewriteMarkdownFileUriHref("file://server/share/workspace-image.svg")).toBe(
63 "\\\\server\\share\\workspace-image.svg",
64 );
65 });
66​
67 // regression: #6433 workspace images in chat markdown did not render
68 it("treats a localhost file uri as a local path", () => {
69 expect(rewriteMarkdownFileUriHref("file://localhost/home/me/notes.md")).toBe(
70 "/home/me/notes.md",
71 );
72 });
73});
74​
75describe("relative links inside a rendered host file", () => {
76 it("anchor to the file's directory while workspace membership follows cwd", () => {
77 const meta = resolveMarkdownFileLinkMeta("appendix.md", "/repo", "/tmp/report");
78 expect(meta).toMatchObject({
79 filePath: "/tmp/report/appendix.md",
80 workspaceRelativePath: null,
81 });
82 const inline = resolveInlineCodeFileLinkMeta("Makefile:12", "/repo", "/tmp/report");
83 expect(inline).toMatchObject({ filePath: "/tmp/report/Makefile", line: 12 });
84 expect(resolveMarkdownFileLinkMeta("src/main.ts", "/repo", "/repo/docs")).toMatchObject({
85 filePath: "/repo/docs/src/main.ts",
86 workspaceRelativePath: "docs/src/main.ts",
87 });
88 });
89});
90​
91describe("resolveInlineCodeFileLinkMeta", () => {
92 it("links bare filenames only when a line suffix marks them as file references", () => {
93 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
94 targetPath: "/Users/julius/project/script.ts:10",
95 line: 10,
96 });
97 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
98 });
99​
100 it("opens a line range at its first line and keeps the range for the label", () => {
101 expect(resolveInlineCodeFileLinkMeta("src/http.ts:42-64", "/repo")).toMatchObject({
102 targetPath: "/repo/src/http.ts:42",
103 line: 42,
104 endLine: 64,
105 });
106 });
107​
108 it("does not treat arbitrary name:digits shapes as files", () => {
109 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
110 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
111 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
112 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
113 expect(resolveInlineCodeFileLinkMeta("port:3000-3010", "/Users/julius/project")).toBeNull();
114 expect(
115 resolveInlineCodeFileLinkMeta("localhost:3000-3010", "/Users/julius/project"),
116 ).toBeNull();
117 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
118 });
119​
120 it("ignores hosts, ports, and versions", () => {
121 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
122 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
123 expect(
124 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
125 ).toBeNull();
126 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
127 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
128 expect(
129 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
130 ).toBeNull();
131 expect(
132 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
133 ).toBeNull();
134 });
135});
136​
137describe("chat file chip paths", () => {
138 const cwd = String.raw`C:\Users\alice\project`;
139​
140 it("opens and copies the indexed file for a bare filename, preserving its line and column", async () => {
141 const meta = resolveMarkdownFileLinkMeta("user.ts:261:7", cwd);
142 if (!meta) throw new Error("Expected a file link");
143 const hostPath = await resolveMarkdownFileHostPath({
144 meta,
145 cwd,
146 findWorkspaceMatch: async () => "src/discord/events/user.ts",
147 });
148​
149 expect(markdownFileEditorTarget(meta, hostPath)).toBe(
150 String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
151 );
152 expect(markdownFileCopyPaths(meta, hostPath, cwd)).toEqual({
153 fullPath: String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
154 relativePath: "src/discord/events/user.ts:261:7",
155 });
156 });
157});