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

codexFileCitations.test.ts

192 lines6.8 KB

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