@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 94 lines 3.49 KB
Raw
1 import { expect, it, vi } from "vite-plus/test";
2 import { unified } from "unified";
3 import remarkParse from "remark-parse";
4
5 import { remarkKeepWindowsPaths, remarkKeepWindowsProsePaths } from "./markdownWindowsPaths.ts";
6
7 const plugins = [
8 ["paths and destinations", remarkKeepWindowsPaths],
9 ["prose paths", remarkKeepWindowsProsePaths],
10 ] as const;
11
12 it.each(plugins)("%s limits drive-path checks to the word containing each escape", (_, plugin) => {
13 const processor = unified().use(remarkParse).use(plugin);
14 const identifiers = Array.from({ length: 200 }, (_, index) => `metric_name_${index}`);
15 const markdown = `Supported metrics: ${identifiers.map((name) => name.replaceAll("_", "\\_")).join(", ")}.`;
16 const pattern = /(?:^|\W)[A-Za-z]:(?:[\\/]\S*)?$/.source;
17 const test = RegExp.prototype.test;
18 let inspectedCharacters = 0;
19 using _test = vi.spyOn(RegExp.prototype, "test").mockImplementation(function (
20 this: RegExp,
21 value: string,
22 ) {
23 if (this.source === pattern) inspectedCharacters += value.length;
24 return test.call(this, value);
25 });
26
27 expect(processor.parse(markdown)).toMatchObject({
28 children: [
29 {
30 type: "paragraph",
31 children: [{ type: "text", value: `Supported metrics: ${identifiers.join(", ")}.` }],
32 },
33 ],
34 });
35 expect(inspectedCharacters).toBeGreaterThan(0);
36 expect(inspectedCharacters).toBeLessThan(markdown.length * 2);
37 });
38
39 it.each(plugins)(
40 "%s preserves prose paths and ordinary escapes across whitespace boundaries",
41 (_, plugin) => {
42 const processor = unified().use(remarkParse).use(plugin);
43 for (const whitespace of [" ", "\t", "\u00a0", "\u2003", "\u202f", "\ufeff"]) {
44 const markdown = String.raw`Before${whitespace}C:\Users\me\_build${whitespace}\*literal\*`;
45 expect(processor.parse(markdown)).toMatchObject({
46 children: [
47 {
48 type: "paragraph",
49 children: [
50 {
51 type: "text",
52 value: String.raw`Before${whitespace}C:\Users\me\_build${whitespace}*literal*`,
53 },
54 ],
55 },
56 ],
57 });
58 }
59 },
60 );
61
62 it.each(plugins)("%s finds each escape's word after earlier escapes in the text", (_, plugin) => {
63 const processor = unified().use(remarkParse).use(plugin);
64 const markdown = String.raw`one\_two C:\a\_b \*x\* three\_four D:\c\_d \*y\* *em*E:\e\_f \*z\*`;
65 expect(processor.parse(markdown)).toMatchObject({
66 children: [
67 {
68 type: "paragraph",
69 children: [
70 { type: "text", value: String.raw`one_two C:\a\_b *x* three_four D:\c\_d *y* ` },
71 { type: "emphasis" },
72 { type: "text", value: String.raw`E:\e\_f *z*` },
73 ],
74 },
75 ],
76 });
77 });
78
79 it.each([
80 [String.raw`C:\R&D folder\.t3\shot.png`, String.raw`C:\R&D folder\.t3\shot.png`],
81 [String.raw`C:\R&D folder\.t3\shot.png`, String.raw`C:\R&D folder\.t3\shot.png`],
82 [String.raw`C:\work\&stuff\.t3\shot.png`, String.raw`C:\work\&stuff\.t3\shot.png`],
83 [String.raw`C:\work\<\.t3\shot.png`, String.raw`C:\work\<\.t3\shot.png`],
84 ])("preserves separators and decodes references once in %s", (authored, expected) => {
85 const parser = unified().use(remarkParse).use(remarkKeepWindowsPaths);
86 for (const prefix of ["", "!"]) {
87 expect(parser.parse(`${prefix}[shot](<${authored}>)`)).toMatchObject({
88 children: [{ children: [{ url: expected }] }],
89 });
90 }
91 expect(parser.parse(`[shot]: <${authored}>`)).toMatchObject({
92 children: [{ type: "definition", url: expected }],
93 });
94 });