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