Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { codexFileCitationMarkdown, resolveCodexFileCitationLink } from "./codexFileCitations.ts";
4import { sanitizeChatHast } from "./markdownHast.ts";
5import {
6 extractMarkdownLinkHrefs,
7 isMarkdownFileLinkLabel,
8 parseMarkdownFileLink,
9 resolveMarkdownFileLink,
10} from "./markdownLinks.ts";
11
12describe("resolveCodexFileCitationLink", () => {
13 it("resolves the attributes emitted by Codex", () => {
14 expect(
15 resolveCodexFileCitationLink({
16 path: "/workspace/outputs/issue-2387-sparse-diagonal.xlsx",
17 purpose: "output",
18 }),
19 ).toEqual({
20 path: "/workspace/outputs/issue-2387-sparse-diagonal.xlsx",
21 href: "/workspace/outputs/issue-2387-sparse-diagonal.xlsx",
22 label: "issue-2387-sparse-diagonal.xlsx",
23 });
24 });
25
26 it("carries the first cited line into the file href", () => {
27 expect(
28 resolveCodexFileCitationLink({
29 path: "src/main.ts",
30 line_range_start: "42",
31 line_range_end: "48",
32 git_url: "https://example.com/main.ts",
33 }),
34 ).toEqual({
35 path: "src/main.ts",
36 href: "src/main.ts#L42",
37 label: "main.ts",
38 lineRangeStart: 42,
39 });
40 });
41
42 it("opens forward-slash UNC citations inside the current workspace", () => {
43 for (const host of ["files", "localhost"]) {
44 const cwd = String.raw`\\${host}\team\repo`;
45 const citation = resolveCodexFileCitationLink({
46 path: `//${host}/team/repo/src/app.ts`,
47 line_range_start: "7",
48 });
49 expect(citation).not.toBeNull();
50 if (!citation) return;
51 const copiedHref = extractMarkdownLinkHrefs(codexFileCitationMarkdown(citation))[0];
52 for (const href of [citation.href, copiedHref]) {
53 expect(resolveMarkdownFileLink(href, cwd)).toMatchObject({
54 targetPath: `${cwd}\\src\\app.ts:7`,
55 position: { path: `${cwd}\\src\\app.ts`, line: 7 },
56 });
57 }
58 }
59 });
60
61 it("keeps UNC citations outside the workspace from becoming file links", () => {
62 for (const path of [
63 "//other/team/repo/src/app.ts",
64 "//files/other/repo/src/app.ts",
65 "//localhost/team/repo/src/app.ts",
66 ]) {
67 const citation = resolveCodexFileCitationLink({ path, line_range_start: "7" });
68 expect(resolveMarkdownFileLink(citation?.href, "\\\\files\\team\\repo")).toBeNull();
69 }
70 expect(resolveCodexFileCitationLink({ path: "///Users/me/repo/src/app.ts" })?.href).toBe(
71 "///Users/me/repo/src/app.ts",
72 );
73 });
74
75 it("rejects missing paths and invalid line numbers", () => {
76 expect(resolveCodexFileCitationLink({ purpose: "output" })).toBeNull();
77 expect(resolveCodexFileCitationLink({ path: " " })).toBeNull();
78 expect(
79 resolveCodexFileCitationLink({ path: "src/main.ts", line_range_start: "not-a-line" }),
80 ).toEqual({
81 path: "src/main.ts",
82 href: "src/main.ts",
83 label: "main.ts",
84 });
85 });
86
87 it.each([
88 ["/tmp/draft.md ", "/tmp/draft.md%20", undefined],
89 ["/repo/draft.md ", "/repo/draft.md%20", undefined],
90 [" draft.md ", "./%20draft.md%20%20", undefined],
91 ["/tmp/draft%20.md ", "/tmp/draft%2520.md%20%20#L7", "7"],
92 ["\u00a0draft.md\n", "./%C2%A0draft.md%0A", undefined],
93 ])("preserves boundary spaces in literal filenames: %s", (path, href, line) => {
94 const citation = resolveCodexFileCitationLink({ path, line_range_start: line });
95
96 expect(citation?.path).toBe(path);
97 expect(citation?.href).toBe(href);
98 expect(parseMarkdownFileLink(citation?.href ?? "")).toEqual({
99 path: /^\s/.test(path) ? `./${path}` : path,
100 ...(line === undefined ? {} : { line: Number(line) }),
101 });
102 expect(citation && codexFileCitationMarkdown(citation)).toContain(`](<${href}>)`);
103 expect(citation && isMarkdownFileLinkLabel(citation.label, citation.href)).toBe(true);
104 });
105
106 it("preserves URL syntax characters in file paths", () => {
107 expect(
108 resolveCodexFileCitationLink({
109 path: "reports/100% #1? draft.md",
110 line_range_start: "7",
111 }),
112 ).toEqual({
113 path: "reports/100% #1? draft.md",
114 href: "./reports/100%25 %231%3F draft.md#L7",
115 label: "100% #1? draft.md",
116 lineRangeStart: 7,
117 });
118 });
119
120 it.each([
121 [
122 "notes:final.txt",
123 "7",
124 "./notes:final.txt#L7",
125 { path: "/workspace/notes:final.txt", line: 7 },
126 ],
127 ["a:b/c.txt", undefined, "./a:b/c.txt", { path: "/workspace/a:b/c.txt" }],
128 ["file:notes.txt", undefined, "./file:notes.txt", { path: "/workspace/file:notes.txt" }],
129 ])(
130 "disambiguates relative colon-bearing paths: %s (line %s)",
131 (path, lineRangeStart, expectedHref, expectedPosition) => {
132 const citation = resolveCodexFileCitationLink({
133 path,
134 ...(lineRangeStart === undefined ? {} : { line_range_start: lineRangeStart }),
135 });
136 expect(citation?.href).toBe(expectedHref);
137 const sanitized = sanitizeChatHast({
138 type: "root",
139 children: [
140 {
141 type: "element",
142 tagName: "a",
143 properties: { href: citation?.href },
144 children: [{ type: "text", value: citation?.label ?? "" }],
145 },
146 ],
147 });
148 const sanitizedHref =
149 sanitized.children[0]?.type === "element"
150 ? sanitized.children[0].properties.href
151 : undefined;
152 expect(sanitizedHref).toBe(expectedHref);
153 expect(resolveMarkdownFileLink(String(sanitizedHref), "/workspace")?.position).toEqual(
154 expectedPosition,
155 );
156 },
157 );
158
159 it.each([
160 "https://example.com/report.pdf",
161 "file:/workspace/notes.txt",
162 "sandbox:/mnt/data/report.pdf",
163 "/workspace/notes:final.txt",
164 "C:\\Users\\test\\report.md",
165 "C:/Users/test/report.md",
166 ])("preserves supported URIs, absolute paths, and Windows drive paths: %s", (path) => {
167 expect(resolveCodexFileCitationLink({ path })?.href).toBe(path);
168 });
169});
170
171describe("codexFileCitationMarkdown", () => {
172 it("escapes Markdown syntax in the visible filename", () => {
173 const citation = resolveCodexFileCitationLink({
174 path: "reports/*draft*_[copy]`~<&|.txt",
175 });
176 expect(citation && codexFileCitationMarkdown(citation)).toBe(
177 "[\\*draft\\*\\_\\[copy\\]\\`\\~\\<\\&\\|.txt](<./reports/*draft*_[copy]`~%3C%26%7C.txt>)",
178 );
179 });
180});