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

codexMarkdownDirectives.test.ts

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