Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import remarkGfm from "remark-gfm";
3import remarkParse from "remark-parse";
4import { unified } from "unified";
5
6import {
7 remarkCodexDirectives,
8 renderCodexDirectivesForCopy,
9 renderCodexInlineDirectivesAsMarkdown,
10 splitCodexArtifactTemplateMarkdown,
11} from "./codexMarkdownDirectives.ts";
12import { parseMarkdownFileLink } from "./markdownLinks.ts";
13
14interface TestNode {
15 readonly type: string;
16 readonly value?: string;
17 readonly url?: string;
18 readonly position?: {
19 readonly start: { readonly offset?: number };
20 readonly end: { readonly offset?: number };
21 };
22 readonly data?: {
23 readonly hName?: string;
24 readonly hProperties?: Readonly<Record<string, unknown>>;
25 };
26 readonly children?: readonly TestNode[];
27}
28
29const FILE_CITATION = ':codex-file-citation{path="outputs/report.xlsx" purpose="output"}';
30const FOLLOW_UP =
31 ':codex-followup[Prepare print version]{prompt="Prepare the document for printing."}';
32const ARTIFACT_TEMPLATE =
33 '::artifact-template{skill_name="artifact-template-hello-world" skill_directory="/Users/test/.codex/skills/artifact-template-hello-world" display_name="Hello World" artifact_kind="document"}';
34
35function parse(markdown: string): TestNode {
36 const processor = unified().use(remarkParse).use(remarkCodexDirectives);
37 return processor.runSync(processor.parse(markdown), { value: markdown }) as TestNode;
38}
39
40function parseOrdinaryMarkdown(markdown: string): TestNode {
41 return unified().use(remarkParse).parse(markdown) as TestNode;
42}
43
44describe("remarkCodexDirectives", () => {
45 it("renders a file citation as a link without changing its source position", () => {
46 const markdown = `Created ${FILE_CITATION}.`;
47 const link = parse(markdown).children?.[0]?.children?.[1];
48
49 expect(link).toMatchObject({
50 type: "link",
51 url: "outputs/report.xlsx",
52 children: [{ type: "text", value: "report.xlsx" }],
53 position: {
54 start: { offset: markdown.indexOf(FILE_CITATION) },
55 end: { offset: markdown.indexOf(FILE_CITATION) + FILE_CITATION.length },
56 },
57 });
58 });
59
60 it("renders an artifact template as semantic block metadata", () => {
61 expect(parse(ARTIFACT_TEMPLATE).children?.[0]).toMatchObject({
62 type: "paragraph",
63 children: [],
64 data: {
65 hName: "div",
66 hProperties: {
67 dataCodexArtifactTemplate: "true",
68 dataArtifactKind: "document",
69 dataDisplayName: "Hello World",
70 dataSkillName: "artifact-template-hello-world",
71 },
72 },
73 });
74 });
75
76 it.each([
77 "Meeting at 10:30",
78 ":::note\ncontent\n:::",
79 ':codex-file-citation-extra{path="outputs/report.xlsx"}',
80 ':codex-followup-extra[Label]{prompt="Prompt"}',
81 ])("does not change unrelated colon syntax: %s", (markdown) => {
82 expect(parse(markdown)).toEqual(parseOrdinaryMarkdown(markdown));
83 });
84
85 it.each([
86 ':codex-file-citation{purpose="output"}',
87 ":codex-followup[Prepare print version]",
88 ':codex-followup[ ]{prompt="Prepare the document for printing."}',
89 ])("keeps malformed supported directives literal: %s", (markdown) => {
90 expect(parse(markdown)).toEqual(parseOrdinaryMarkdown(markdown));
91 });
92});
93
94describe.each([
95 { name: "renderCodexDirectivesForCopy", render: renderCodexDirectivesForCopy },
96 { name: "renderCodexInlineDirectivesAsMarkdown", render: renderCodexInlineDirectivesAsMarkdown },
97])("$name file citation round trips", ({ render }) => {
98 // regression: #12243 copied Codex citations keep their backslashes
99 it.each([
100 ["C:\\Users\\test\\[draft]\\report.md", "C:\\Users\\test\\[draft]\\report.md"],
101 ["\\\\server\\share\\report.md", "\\\\server\\share\\report.md"],
102 ["/tmp/report%5C.md", "/tmp/report%5C.md"],
103 ["~\\reports\\draft.md", "~/reports/draft.md"],
104 ["reports/100% #1? draft.md", "./reports/100% #1? draft.md"],
105 ["./reports/draft.md", "./reports/draft.md"],
106 ])("preserves the literal path and line: %s", (path, linkPath) => {
107 const markdown = render(`:codex-file-citation{path="${path}" line_range_start="7"}`);
108 const link = parseOrdinaryMarkdown(markdown).children?.[0]?.children?.[0];
109
110 expect(link?.type).toBe("link");
111 expect(parseMarkdownFileLink(link?.url ?? "")).toEqual({ path: linkPath, line: 7 });
112 });
113
114 it("keeps an entity-like ampersand in the path literal", () => {
115 const markdown = render(
116 ':codex-file-citation{path="/tmp/a&copy;.txt" line_range_start="7"}',
117 );
118 const link = parseOrdinaryMarkdown(markdown).children?.[0]?.children?.[0];
119
120 expect(link?.type).toBe("link");
121 expect(parseMarkdownFileLink(link?.url ?? "")).toEqual({ path: "/tmp/a©.txt", line: 7 });
122 });
123
124 it("keeps literal tildes in the visible filename", () => {
125 const markdown = render(':codex-file-citation{path="reports/~~final~~.txt"}');
126 const parsed = unified().use(remarkParse).use(remarkGfm).parse(markdown) as TestNode;
127 const link = parsed.children?.[0]?.children?.[0];
128
129 expect(link).toMatchObject({
130 type: "link",
131 url: "./reports/~~final~~.txt",
132 children: [{ type: "text", value: "~~final~~.txt" }],
133 });
134 });
135
136 it("preserves a table row when the cited path contains a pipe", () => {
137 const markdown = render(
138 '| File | Note |\n| --- | --- |\n| :codex-file-citation{path="reports/a|b.txt"} | ok |',
139 );
140 const table = unified().use(remarkParse).use(remarkGfm).parse(markdown).children[0] as TestNode;
141 const cells = table.children?.[1]?.children;
142 const link = cells?.[0]?.children?.[0];
143
144 expect(cells).toHaveLength(2);
145 expect(link).toMatchObject({
146 type: "link",
147 url: "./reports/a%7Cb.txt",
148 children: [{ type: "text", value: "a|b.txt" }],
149 });
150 expect(parseMarkdownFileLink(link?.url ?? "")).toEqual({ path: "./reports/a|b.txt" });
151 expect(cells?.[1]?.children).toMatchObject([{ type: "text", value: "ok" }]);
152 });
153});
154
155describe("native Markdown adapters", () => {
156 it("renders follow-up suggestions as their label Markdown", () => {
157 expect(
158 renderCodexInlineDirectivesAsMarkdown(
159 `- ${FOLLOW_UP}\n- :codex-followup[Review **document**]{prompt="Review it."}`,
160 ),
161 ).toBe("- Prepare print version\n- Review **document**");
162 });
163
164 it("keeps a leading byte order mark from shifting directive source offsets", () => {
165 const bom = "\uFEFF";
166 expect(
167 renderCodexInlineDirectivesAsMarkdown(`${bom}:codex-file-citation{path="/tmp/x.md"}`),
168 ).toBe(`${bom}[x.md](</tmp/x.md>)`);
169 expect(renderCodexInlineDirectivesAsMarkdown(`${bom}${FOLLOW_UP} next`)).toBe(
170 `${bom}Prepare print version next`,
171 );
172 expect(splitCodexArtifactTemplateMarkdown(`${bom}${ARTIFACT_TEMPLATE}\n\nAfter`)).toEqual([
173 { kind: "markdown", markdown: bom, sourceOffset: 0 },
174 expect.objectContaining({ kind: "artifact-template", sourceOffset: 1 }),
175 { kind: "markdown", markdown: "\n\nAfter", sourceOffset: 1 + ARTIFACT_TEMPLATE.length },
176 ]);
177 const rejected = ":codex-followup[Next]{prompt=''}";
178 expect(parse(`${bom}${rejected}`).children?.[0]?.children?.[0]).toMatchObject({
179 type: "text",
180 value: rejected,
181 });
182 });
183
184 it.each([
185 `\\${FILE_CITATION}`,
186 `\`${FILE_CITATION}\``,
187 `[See ${FILE_CITATION}](https://example.com)`,
188 ])("does not render excluded citation syntax: %s", (markdown) => {
189 expect(renderCodexInlineDirectivesAsMarkdown(markdown)).toBe(markdown);
190 });
191
192 it("splits artifact cards from surrounding native Markdown", () => {
193 expect(splitCodexArtifactTemplateMarkdown(`Before\n\n${ARTIFACT_TEMPLATE}\n\nAfter`)).toEqual([
194 { kind: "markdown", markdown: "Before\n\n", sourceOffset: 0 },
195 {
196 kind: "artifact-template",
197 sourceOffset: 8,
198 template: {
199 artifactKind: "document",
200 displayName: "Hello World",
201 skillDirectory: "/Users/test/.codex/skills/artifact-template-hello-world",
202 skillName: "artifact-template-hello-world",
203 },
204 },
205 {
206 kind: "markdown",
207 markdown: "\n\nAfter",
208 sourceOffset: 8 + ARTIFACT_TEMPLATE.length,
209 },
210 ]);
211 });
212
213 it("leaves malformed and code artifact-template examples in Markdown", () => {
214 const malformed = '::artifact-template{display_name="Hello World"}';
215 const code = `\`${ARTIFACT_TEMPLATE}\``;
216 expect(splitCodexArtifactTemplateMarkdown(malformed)).toEqual([
217 { kind: "markdown", markdown: malformed, sourceOffset: 0 },
218 ]);
219 expect(splitCodexArtifactTemplateMarkdown(code)).toEqual([
220 { kind: "markdown", markdown: code, sourceOffset: 0 },
221 ]);
222 });
223});
224
225describe("directive copy adapter", () => {
226 it("copies the Markdown representations shown by citation chips and template cards", () => {
227 expect(
228 renderCodexDirectivesForCopy(
229 `Created ${FILE_CITATION}.\n\n${ARTIFACT_TEMPLATE}\n\n- ${FOLLOW_UP}`,
230 ),
231 ).toBe(
232 "Created [report.xlsx](<outputs/report.xlsx>).\n\nHello World (Document template)\n\n- Prepare print version",
233 );
234 });
235});