Open History to see the last commit
1import { workspaceRelativeFilePath } from "./path.ts";
2import { describe, expect, it } from "vite-plus/test";
3import { extractMarkdownLinkHrefs, resolveMarkdownFileLinkTarget } from "./markdownLinks.ts";
4
5import {
6 inlineCodeFilePathCandidate,
7 isMarkdownFileLinkLabel,
8 parseMarkdownFileLink,
9} from "./markdownLinks.ts";
10
11describe("isMarkdownFileLinkLabel", () => {
12 // regression: #15509 descriptive labels are not mistaken for the linked file name
13 it.each([
14 ["validates the input", "/repo/src/example.ts:12", false],
15 ["example.ts:12", "/repo/src/example.ts:12", true],
16 ["example.ts:99", "/repo/src/example.ts:12", false],
17 ])("classifies %s for %s", (label, href, expected) => {
18 expect(isMarkdownFileLinkLabel(label, href)).toBe(expected);
19 });
20});
21
22describe("inlineCodeFilePathCandidate", () => {
23 // regression: #13909 model ids in inline code do not become file chips
24 it.each([
25 ["src\\main.ts", "src/main.ts"],
26 ["origin/main", null],
27 ["z-ai/glm-5.3", null],
28 ["share/man/ls.1", "share/man/ls.1"],
29 ])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {
30 expect(inlineCodeFilePathCandidate(source)).toBe(candidate);
31 });
32});
33
34describe("parseMarkdownFileLink", () => {
35 it.each([
36 ["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"],
37 ["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"],
38 ["file://localhost/home/me/notes.md", "/home/me/notes.md"],
39 ])("recognizes %s as a file", (href, path) => {
40 expect(parseMarkdownFileLink(href)?.path).toBe(path);
41 });
42
43 it.each(["https://example.com/docs", "/chat/settings#L3", "TODO:12"])(
44 "does not treat %s as a file",
45 (href) => {
46 expect(parseMarkdownFileLink(href)).toBeNull();
47 },
48 );
49
50 it("accepts conventional extensionless names with or without a position", () => {
51 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
52 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
53 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
54 });
55
56 it("reads positions from suffixes and line anchors", () => {
57 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
58 path: "/Users/julius/project/src/main.ts",
59 line: 42,
60 column: 7,
61 });
62 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
63 path: "\\\\server\\share\\src\\main.ts",
64 line: 42,
65 column: 7,
66 });
67 });
68});
69
70describe("workspaceRelativeFilePath", () => {
71 // regression: #12449 workspace root links resolve to "."
72 it.each([
73 ["/repo/project", "/repo/project", "."],
74 ["C:/", "c:/", "."],
75 ["/repo/project/a.ts", undefined, null],
76 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
77 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
78 });
79});
80
81describe("extractMarkdownLinkHrefs", () => {
82 it.each([
83 ["[".repeat(32_000), []],
84 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
85 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
86 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
87 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
88 });
89});
90
91describe("resolveMarkdownFileLinkTarget", () => {
92 it("does not treat filename line references as external schemes", () => {
93 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
94 "/Users/julius/project/script.ts:10",
95 );
96 });
97
98 it("maps #L line anchors to editor line suffixes", () => {
99 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
100 "/Users/julius/project/src/main.ts:42:7",
101 );
102 });
103
104 it("ignores external urls", () => {
105 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
106 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
107 });
108
109 it("does not double-decode file URLs", () => {
110 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
111 "/Users/julius/project/file%20name.md",
112 );
113 });
114
115 it("resolves file uri authorities as windows UNC paths", () => {
116 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg")).toBe(
117 "\\\\server\\share\\workspace-image.svg",
118 );
119 });
120
121 it("keeps an encoded final space in the absolute target", () => {
122 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
123 "/tmp/repo/file.ts ",
124 );
125 });
126
127 it("normalizes slash-prefixed windows drive paths before resolving", () => {
128 expect(
129 resolveMarkdownFileLinkTarget(
130 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
131 ),
132 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
133 });
134
135 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
136 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
137 "c:/Users/x/shot.png",
138 );
139 });
140});