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

markdown-links.test.ts

128 lines4.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 resolveMarkdownFileLinkMeta,
5 resolveMarkdownFileLinkTarget,
6 rewriteMarkdownFileUriHref,
7} from "./markdown-links";
8​
9describe("rewriteMarkdownFileUriHref", () => {
10 it("rewrites file uri hrefs into direct path hrefs", () => {
11 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/src/main.ts#L42")).toBe(
12 "/Users/julius/project/src/main.ts#L42",
13 );
14 });
15​
16 it("preserves encoded octets so file paths are decoded only once later", () => {
17 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/file%2520name.md")).toBe(
18 "/Users/julius/project/file%2520name.md",
19 );
20 });
21​
22 it("normalizes file uri hrefs for windows drive paths", () => {
23 expect(
24 rewriteMarkdownFileUriHref(
25 "file:///D:/Programme/v12/apps/web/src/components/chat/OpenInPicker.tsx#L69",
26 ),
27 ).toBe("D:/Programme/v12/apps/web/src/components/chat/OpenInPicker.tsx#L69");
28 });
29​
30 it("unwraps angle-bracketed file uri hrefs", () => {
31 expect(
32 rewriteMarkdownFileUriHref(" <file:///D:/Programme/v12/apps/web/src/markdown-links.ts> "),
33 ).toBe("D:/Programme/v12/apps/web/src/markdown-links.ts");
34 });
35});
36​
37describe("resolveMarkdownFileLinkTarget", () => {
38 it("resolves absolute posix file paths", () => {
39 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/AGENTS.md")).toBe(
40 "/Users/julius/project/AGENTS.md",
41 );
42 });
43​
44 it("resolves relative file paths against cwd", () => {
45 expect(resolveMarkdownFileLinkTarget("src/processRunner.ts:71", "/Users/julius/project")).toBe(
46 "/Users/julius/project/src/processRunner.ts:71",
47 );
48 });
49​
50 it("does not treat filename line references as external schemes", () => {
51 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
52 "/Users/julius/project/script.ts:10",
53 );
54 });
55​
56 it("resolves bare file names against cwd", () => {
57 expect(resolveMarkdownFileLinkTarget("AGENTS.md", "/Users/julius/project")).toBe(
58 "/Users/julius/project/AGENTS.md",
59 );
60 });
61​
62 it("maps #L line anchors to editor line suffixes", () => {
63 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
64 "/Users/julius/project/src/main.ts:42:7",
65 );
66 });
67​
68 it("ignores external urls", () => {
69 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
70 });
71​
72 it("does not double-decode file URLs", () => {
73 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
74 "/Users/julius/project/file%20name.md",
75 );
76 });
77​
78 it("formats tooltip display paths relative to the cwd when possible", () => {
79 expect(
80 resolveMarkdownFileLinkMeta(
81 "file:///C:/Users/mike/dev-stuff/v12/apps/web/src/session-logic.ts#L501",
82 "C:/Users/mike/dev-stuff/v12",
83 ),
84 ).toMatchObject({
85 displayPath: "v12/apps/web/src/session-logic.ts:501",
86 workspaceRelativePath: "apps/web/src/session-logic.ts",
87 });
88 });
89​
90 it("formats tooltip display paths relative to the cwd for slash-prefixed windows paths", () => {
91 expect(
92 resolveMarkdownFileLinkMeta(
93 "/C:/Users/mike/dev-stuff/v12/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
94 "C:/Users/mike/dev-stuff/v12",
95 ),
96 ).toMatchObject({
97 displayPath: "v12/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
98 workspaceRelativePath:
99 "apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
100 });
101 });
102​
103 it("does not create a preview path for files outside the workspace", () => {
104 expect(resolveMarkdownFileLinkMeta("/tmp/report.ts", "/repo/project")).toMatchObject({
105 workspaceRelativePath: null,
106 });
107 });
108​
109 it("normalizes slash-prefixed windows drive paths before resolving", () => {
110 expect(
111 resolveMarkdownFileLinkTarget(
112 "/D:/Programme/v12/apps/web/src/components/chat/OpenInPicker.tsx#L69",
113 ),
114 ).toBe("D:/Programme/v12/apps/web/src/components/chat/OpenInPicker.tsx:69");
115 });
116​
117 it("resolves angle-bracketed windows drive paths", () => {
118 expect(
119 resolveMarkdownFileLinkTarget(
120 "</D:/Programme/v12/apps/web/src/components/ChatMarkdown.tsx:1>",
121 ),
122 ).toBe("D:/Programme/v12/apps/web/src/components/ChatMarkdown.tsx:1");
123 });
124​
125 it("does not treat app routes as file links", () => {
126 expect(resolveMarkdownFileLinkTarget("/chat/settings")).toBeNull();
127 });
128});