@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 184 lines 6.75 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2 import remarkParse from "remark-parse";
3 import { unified } from "unified";
4
5 import {
6 remarkCodexDirectives,
7 renderCodexDirectivesForCopy,
8 renderCodexInlineDirectivesAsMarkdown,
9 splitCodexArtifactTemplateMarkdown,
10 } from "./codexMarkdownDirectives.ts";
11 import { parseMarkdownFileLink } from "./markdownLinks.ts";
12
13 interface 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[];
26 }
27
28 const FILE_CITATION = ':codex-file-citation{path="outputs/report.xlsx" purpose="output"}';
29 const FOLLOW_UP =
30 ':codex-followup[Prepare print version]{prompt="Prepare the document for printing."}';
31 const ARTIFACT_TEMPLATE =
32 '::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"}';
33
34 function parse(markdown: string): TestNode {
35 const processor = unified().use(remarkParse).use(remarkCodexDirectives);
36 return processor.runSync(processor.parse(markdown), { value: markdown }) as TestNode;
37 }
38
39 function parseOrdinaryMarkdown(markdown: string): TestNode {
40 return unified().use(remarkParse).parse(markdown) as TestNode;
41 }
42
43 describe("remarkCodexDirectives", () => {
44 it("renders a file citation as a link without changing its source position", () => {
45 const markdown = `Created ${FILE_CITATION}.`;
46 const link = parse(markdown).children?.[0]?.children?.[1];
47
48 expect(link).toMatchObject({
49 type: "link",
50 url: "outputs/report.xlsx",
51 children: [{ type: "text", value: "report.xlsx" }],
52 position: {
53 start: { offset: markdown.indexOf(FILE_CITATION) },
54 end: { offset: markdown.indexOf(FILE_CITATION) + FILE_CITATION.length },
55 },
56 });
57 });
58
59 it("renders an artifact template as semantic block metadata", () => {
60 expect(parse(ARTIFACT_TEMPLATE).children?.[0]).toMatchObject({
61 type: "paragraph",
62 children: [],
63 data: {
64 hName: "div",
65 hProperties: {
66 dataCodexArtifactTemplate: "true",
67 dataArtifactKind: "document",
68 dataDisplayName: "Hello World",
69 dataSkillName: "artifact-template-hello-world",
70 },
71 },
72 });
73 });
74
75 it.each([
76 "Meeting at 10:30",
77 ":::note\ncontent\n:::",
78 ':codex-file-citation-extra{path="outputs/report.xlsx"}',
79 ':codex-followup-extra[Label]{prompt="Prompt"}',
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 ":codex-followup[Prepare print version]",
87 ':codex-followup[ ]{prompt="Prepare the document for printing."}',
88 ])("keeps malformed supported directives literal: %s", (markdown) => {
89 expect(parse(markdown)).toEqual(parseOrdinaryMarkdown(markdown));
90 });
91 });
92
93 describe.each([
94 { name: "renderCodexDirectivesForCopy", render: renderCodexDirectivesForCopy },
95 { name: "renderCodexInlineDirectivesAsMarkdown", render: renderCodexInlineDirectivesAsMarkdown },
96 ])("$name file citation round trips", ({ render }) => {
97 // regression: #12243 copied Codex citations keep their backslashes
98 it.each([
99 ["C:\\Users\\test\\[draft]\\report.md", "C:\\Users\\test\\[draft]\\report.md"],
100 ["\\\\server\\share\\report.md", "\\\\server\\share\\report.md"],
101 ["/tmp/report%5C.md", "/tmp/report%5C.md"],
102 ["~\\reports\\draft.md", "~/reports/draft.md"],
103 ["reports/100% #1? draft.md", "./reports/100% #1? draft.md"],
104 ["./reports/draft.md", "./reports/draft.md"],
105 ])("preserves the literal path and line: %s", (path, linkPath) => {
106 const markdown = render(`:codex-file-citation{path="${path}" line_range_start="7"}`);
107 const link = parseOrdinaryMarkdown(markdown).children?.[0]?.children?.[0];
108
109 expect(link?.type).toBe("link");
110 expect(parseMarkdownFileLink(link?.url ?? "")).toEqual({ path: linkPath, line: 7 });
111 });
112
113 it("keeps an entity-like ampersand in the path literal", () => {
114 const markdown = render(
115 ':codex-file-citation{path="/tmp/a&amp;copy;.txt" line_range_start="7"}',
116 );
117 const link = parseOrdinaryMarkdown(markdown).children?.[0]?.children?.[0];
118
119 expect(link?.type).toBe("link");
120 expect(parseMarkdownFileLink(link?.url ?? "")).toEqual({ path: "/tmp/a&copy;.txt", line: 7 });
121 });
122 });
123
124 describe("native Markdown adapters", () => {
125 it("renders follow-up suggestions as their label Markdown", () => {
126 expect(
127 renderCodexInlineDirectivesAsMarkdown(
128 `- ${FOLLOW_UP}\n- :codex-followup[Review **document**]{prompt="Review it."}`,
129 ),
130 ).toBe("- Prepare print version\n- Review **document**");
131 });
132
133 it.each([
134 `\\${FILE_CITATION}`,
135 `\`${FILE_CITATION}\``,
136 `[See ${FILE_CITATION}](https://example.com)`,
137 ])("does not render excluded citation syntax: %s", (markdown) => {
138 expect(renderCodexInlineDirectivesAsMarkdown(markdown)).toBe(markdown);
139 });
140
141 it("splits artifact cards from surrounding native Markdown", () => {
142 expect(splitCodexArtifactTemplateMarkdown(`Before\n\n${ARTIFACT_TEMPLATE}\n\nAfter`)).toEqual([
143 { kind: "markdown", markdown: "Before\n\n", sourceOffset: 0 },
144 {
145 kind: "artifact-template",
146 sourceOffset: 8,
147 template: {
148 artifactKind: "document",
149 displayName: "Hello World",
150 skillDirectory: "/Users/test/.codex/skills/artifact-template-hello-world",
151 skillName: "artifact-template-hello-world",
152 },
153 },
154 {
155 kind: "markdown",
156 markdown: "\n\nAfter",
157 sourceOffset: 8 + ARTIFACT_TEMPLATE.length,
158 },
159 ]);
160 });
161
162 it("leaves malformed and code artifact-template examples in Markdown", () => {
163 const malformed = '::artifact-template{display_name="Hello World"}';
164 const code = `\`${ARTIFACT_TEMPLATE}\``;
165 expect(splitCodexArtifactTemplateMarkdown(malformed)).toEqual([
166 { kind: "markdown", markdown: malformed, sourceOffset: 0 },
167 ]);
168 expect(splitCodexArtifactTemplateMarkdown(code)).toEqual([
169 { kind: "markdown", markdown: code, sourceOffset: 0 },
170 ]);
171 });
172 });
173
174 describe("directive copy adapter", () => {
175 it("copies the Markdown representations shown by citation chips and template cards", () => {
176 expect(
177 renderCodexDirectivesForCopy(
178 `Created ${FILE_CITATION}.\n\n${ARTIFACT_TEMPLATE}\n\n- ${FOLLOW_UP}`,
179 ),
180 ).toBe(
181 "Created [report.xlsx](<outputs/report.xlsx>).\n\nHello World (Document template)\n\n- Prepare print version",
182 );
183 });
184 });