Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 resolveInlineCodeFileLinkMeta,
5 resolveMarkdownFileLinkMeta,
6 rewriteMarkdownFileUriHref,
7} from "./markdown-links";
8import {
9 markdownFileCopyPaths,
10 markdownFileEditorTarget,
11 resolveMarkdownFileHostPath,
12} from "./markdownFileHostPath";
13
14describe("rewriteMarkdownFileUriHref", () => {
15 it("preserves encoded octets so file paths are decoded only once later", () => {
16 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/file%2520name.md")).toBe(
17 "/Users/julius/project/file%2520name.md",
18 );
19 });
20
21 it("normalizes file uri hrefs for windows drive paths", () => {
22 expect(
23 rewriteMarkdownFileUriHref(
24 "file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
25 ),
26 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
27 });
28
29 it("preserves file uri authorities as windows UNC paths", () => {
30 expect(rewriteMarkdownFileUriHref("file://server/share/workspace-image.svg")).toBe(
31 "\\\\server\\share\\workspace-image.svg",
32 );
33 });
34
35 // regression: #6433 workspace images in chat markdown did not render
36 it("treats a localhost file uri as a local path", () => {
37 expect(rewriteMarkdownFileUriHref("file://localhost/home/me/notes.md")).toBe(
38 "/home/me/notes.md",
39 );
40 });
41});
42
43describe("relative links inside a rendered host file", () => {
44 it("anchor to the file's directory while workspace membership follows cwd", () => {
45 const meta = resolveMarkdownFileLinkMeta("appendix.md", "/repo", "/tmp/report");
46 expect(meta).toMatchObject({
47 filePath: "/tmp/report/appendix.md",
48 workspaceRelativePath: null,
49 });
50 const inline = resolveInlineCodeFileLinkMeta("Makefile:12", "/repo", "/tmp/report");
51 expect(inline).toMatchObject({ filePath: "/tmp/report/Makefile", line: 12 });
52 expect(resolveMarkdownFileLinkMeta("src/main.ts", "/repo", "/repo/docs")).toMatchObject({
53 filePath: "/repo/docs/src/main.ts",
54 workspaceRelativePath: "docs/src/main.ts",
55 });
56 });
57});
58
59describe("resolveInlineCodeFileLinkMeta", () => {
60 it("links bare filenames only when a line suffix marks them as file references", () => {
61 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
62 targetPath: "/Users/julius/project/script.ts:10",
63 line: 10,
64 });
65 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
66 });
67
68 it("does not treat arbitrary name:digits shapes as files", () => {
69 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
70 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
71 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
72 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
73 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
74 });
75
76 it("ignores hosts, ports, and versions", () => {
77 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
78 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
79 expect(
80 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
81 ).toBeNull();
82 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
83 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
84 expect(
85 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
86 ).toBeNull();
87 expect(
88 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
89 ).toBeNull();
90 });
91});
92
93describe("chat file chip paths", () => {
94 const cwd = String.raw`C:\Users\alice\project`;
95
96 it("opens and copies the indexed file for a bare filename, preserving its line and column", async () => {
97 const meta = resolveMarkdownFileLinkMeta("user.ts:261:7", cwd);
98 if (!meta) throw new Error("Expected a file link");
99 const hostPath = await resolveMarkdownFileHostPath({
100 meta,
101 cwd,
102 findWorkspaceMatch: async () => "src/discord/events/user.ts",
103 });
104
105 expect(markdownFileEditorTarget(meta, hostPath)).toBe(
106 String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
107 );
108 expect(markdownFileCopyPaths(meta, hostPath, cwd)).toEqual({
109 fullPath: String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
110 relativePath: "src/discord/events/user.ts:261:7",
111 });
112 });
113});