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

codexMarkdownDirectives.test.ts

175 lines6.1 KB

1import { describe, expect, it } from "vite-plus/test";
2import remarkParse from "remark-parse";
3import { unified } from "unified";
4​
5import {
6 remarkCodexDirectives,
7 renderCodexDirectivesForCopy,
8 renderCodexFileCitationsAsMarkdown,
9 splitCodexArtifactTemplateMarkdown,
10} from "./codexMarkdownDirectives.ts";
11import { parseMarkdownFileLink } from "./markdownLinks.ts";
12​
13interface TestNode {
14 readonly type: string;
15 readonly value?: string;
16 readonly url?: string;
17 readonly position?: {
18 readonly start: { readonly offset?: number };
19 readonly end: { readonly offset?: number };
20 };
21 readonly data?: {
22 readonly hName?: string;
23 readonly hProperties?: Readonly<Record<string, unknown>>;
24 };
25 readonly children?: readonly TestNode[];
27​
28const FILE_CITATION = ':codex-file-citation{path="outputs/report.xlsx" purpose="output"}';
29const ARTIFACT_TEMPLATE =
30 '::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"}';
31​
32function parse(markdown: string): TestNode {
33 const processor = unified().use(remarkParse).use(remarkCodexDirectives);
34 return processor.runSync(processor.parse(markdown), { value: markdown }) as TestNode;
36​
37function parseOrdinaryMarkdown(markdown: string): TestNode {
38 return unified().use(remarkParse).parse(markdown) as TestNode;
40​
41describe("remarkCodexDirectives", () => {
42 it("renders a file citation as a link without changing its source position", () => {
43 const markdown = `Created ${FILE_CITATION}.`;
44 const link = parse(markdown).children?.[0]?.children?.[1];
45​
46 expect(link).toMatchObject({
47 type: "link",
48 url: "outputs/report.xlsx",
49 children: [{ type: "text", value: "report.xlsx" }],
50 position: {
51 start: { offset: markdown.indexOf(FILE_CITATION) },
52 end: { offset: markdown.indexOf(FILE_CITATION) + FILE_CITATION.length },
53 },
54 });
55 });
56​
57 it("renders an artifact template as semantic block metadata", () => {
58 expect(parse(ARTIFACT_TEMPLATE).children?.[0]).toMatchObject({
59 type: "paragraph",
60 children: [],
61 data: {
62 hName: "div",
63 hProperties: {
64 dataCodexArtifactTemplate: "true",
65 dataArtifactKind: "document",
66 dataDisplayName: "Hello World",
67 dataSkillName: "artifact-template-hello-world",
68 },
69 },
70 });
71 });
72​
73 it.each([
74 "Meeting at 10:30",
75 "Open src/main.ts:42",
76 "Use :hover and :tada:",
77 "::note",
78 ":::note\ncontent\n:::",
79 ':codex-file-citation-extra{path="outputs/report.xlsx"}',
80 "::artifact-template-extra",
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 '::artifact-template{skill_name="artifact-template-hello-world"}',
88 ])("keeps malformed supported directives literal: %s", (markdown) => {
89 expect(parse(markdown)).toEqual(parseOrdinaryMarkdown(markdown));
90 });
91});
92​
93describe.each([
94 { name: "renderCodexDirectivesForCopy", render: renderCodexDirectivesForCopy },
95 { name: "renderCodexFileCitationsAsMarkdown", render: renderCodexFileCitationsAsMarkdown },
96])("$name file citation round trips", ({ render }) => {
97 it.each([
98 "C:\\Users\\test\\[draft]\\report.md",
99 "\\\\server\\share\\report.md",
100 "outputs/report.md",
101 "/tmp/report%5C.md",
102 ])("preserves the literal path and line: %s", (path) => {
103 const markdown = render(`:codex-file-citation{path="${path}" line_range_start="7"}`);
104 const link = parseOrdinaryMarkdown(markdown).children?.[0]?.children?.[0];
105​
106 expect(link?.type).toBe("link");
107 expect(parseMarkdownFileLink(link?.url ?? "")).toEqual({ path, line: 7 });
108 });
109});
110​
111describe("native Markdown adapters", () => {
112 it("uses the same parser to render file citations as portable links", () => {
113 expect(renderCodexFileCitationsAsMarkdown(`Created ${FILE_CITATION}.`)).toBe(
114 "Created [report.xlsx](<outputs/report.xlsx>).",
115 );
116 });
117​
118 it.each([
119 `\\${FILE_CITATION}`,
120 `\`${FILE_CITATION}\``,
121 `\`\`\`text\n${FILE_CITATION}\n\`\`\``,
122 `[See ${FILE_CITATION}](https://example.com)`,
123 ])("does not render excluded citation syntax: %s", (markdown) => {
124 expect(renderCodexFileCitationsAsMarkdown(markdown)).toBe(markdown);
125 });
126​
127 it("splits artifact cards from surrounding native Markdown", () => {
128 expect(splitCodexArtifactTemplateMarkdown(`Before\n\n${ARTIFACT_TEMPLATE}\n\nAfter`)).toEqual([
129 { kind: "markdown", markdown: "Before\n\n", sourceOffset: 0 },
130 {
131 kind: "artifact-template",
132 sourceOffset: 8,
133 template: {
134 artifactKind: "document",
135 displayName: "Hello World",
136 skillDirectory: "/Users/test/.codex/skills/artifact-template-hello-world",
137 skillName: "artifact-template-hello-world",
138 },
139 },
140 {
141 kind: "markdown",
142 markdown: "\n\nAfter",
143 sourceOffset: 8 + ARTIFACT_TEMPLATE.length,
144 },
145 ]);
146 });
147​
148 it("leaves malformed and code artifact-template examples in Markdown", () => {
149 const malformed = '::artifact-template{display_name="Hello World"}';
150 const code = `\`${ARTIFACT_TEMPLATE}\``;
151 expect(splitCodexArtifactTemplateMarkdown(malformed)).toEqual([
152 { kind: "markdown", markdown: malformed, sourceOffset: 0 },
153 ]);
154 expect(splitCodexArtifactTemplateMarkdown(code)).toEqual([
155 { kind: "markdown", markdown: code, sourceOffset: 0 },
156 ]);
157 });
158});
159​
160describe("directive copy adapter", () => {
161 it("copies the Markdown representations shown by citation chips and template cards", () => {
162 expect(renderCodexDirectivesForCopy(`Created ${FILE_CITATION}.\n\n${ARTIFACT_TEMPLATE}`)).toBe(
163 "Created [report.xlsx](<outputs/report.xlsx>).\n\nHello World (Document template)",
164 );
165 });
166​
167 it("leaves excluded and malformed directive source unchanged", () => {
168 const markdown = [
169 `\`${FILE_CITATION}\``,
170 '::artifact-template{display_name="Hello World"}',
171 ].join("\n\n");
172​
173 expect(renderCodexDirectivesForCopy(markdown)).toBe(markdown);
174 });
175});