| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { codexFileCitationMarkdown, resolveCodexFileCitationLink } from "./codexFileCitations.ts"; |
| 4 | |
| 5 | describe("resolveCodexFileCitationLink", () => { |
| 6 | it("resolves the attributes emitted by Codex", () => { |
| 7 | expect( |
| 8 | resolveCodexFileCitationLink({ |
| 9 | path: "/workspace/outputs/issue-2387-sparse-diagonal.xlsx", |
| 10 | purpose: "output", |
| 11 | }), |
| 12 | ).toEqual({ |
| 13 | path: "/workspace/outputs/issue-2387-sparse-diagonal.xlsx", |
| 14 | href: "/workspace/outputs/issue-2387-sparse-diagonal.xlsx", |
| 15 | label: "issue-2387-sparse-diagonal.xlsx", |
| 16 | }); |
| 17 | }); |
| 18 | |
| 19 | it("carries the first cited line into the file href", () => { |
| 20 | expect( |
| 21 | resolveCodexFileCitationLink({ |
| 22 | path: "src/main.ts", |
| 23 | line_range_start: "42", |
| 24 | line_range_end: "48", |
| 25 | git_url: "https://example.com/main.ts", |
| 26 | }), |
| 27 | ).toEqual({ |
| 28 | path: "src/main.ts", |
| 29 | href: "src/main.ts#L42", |
| 30 | label: "main.ts", |
| 31 | lineRangeStart: 42, |
| 32 | }); |
| 33 | }); |
| 34 | |
| 35 | it("rejects missing paths and invalid line numbers", () => { |
| 36 | expect(resolveCodexFileCitationLink({ purpose: "output" })).toBeNull(); |
| 37 | expect( |
| 38 | resolveCodexFileCitationLink({ path: "src/main.ts", line_range_start: "not-a-line" }), |
| 39 | ).toEqual({ |
| 40 | path: "src/main.ts", |
| 41 | href: "src/main.ts", |
| 42 | label: "main.ts", |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | it("preserves URL syntax characters in file paths", () => { |
| 47 | expect( |
| 48 | resolveCodexFileCitationLink({ |
| 49 | path: "reports/100% #1? draft.md", |
| 50 | line_range_start: "7", |
| 51 | }), |
| 52 | ).toEqual({ |
| 53 | path: "reports/100% #1? draft.md", |
| 54 | href: "reports/100%25 %231%3F draft.md#L7", |
| 55 | label: "100% #1? draft.md", |
| 56 | lineRangeStart: 7, |
| 57 | }); |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | describe("codexFileCitationMarkdown", () => { |
| 62 | it("produces a portable Markdown link", () => { |
| 63 | const citation = resolveCodexFileCitationLink({ path: "reports/profit and loss.xlsx" }); |
| 64 | expect(citation && codexFileCitationMarkdown(citation)).toBe( |
| 65 | "[profit and loss.xlsx](<reports/profit and loss.xlsx>)", |
| 66 | ); |
| 67 | }); |
| 68 | |
| 69 | it("escapes Markdown syntax in the visible filename", () => { |
| 70 | const citation = resolveCodexFileCitationLink({ |
| 71 | path: "reports/*draft*_[copy]`<&.txt", |
| 72 | }); |
| 73 | expect(citation && codexFileCitationMarkdown(citation)).toBe( |
| 74 | "[\\*draft\\*\\_\\[copy\\]\\`\\<\\&.txt](<reports/*draft*_[copy]`%3C&.txt>)", |
| 75 | ); |
| 76 | }); |
| 77 | }); |