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, 04:45 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { splitMarkdownDetails } from "./markdownDetails"; |
| 4 | import { |
| 5 | markdownForNativeParsing, |
| 6 | markdownReferenceDefinitions, |
| 7 | withReferenceDefinitions, |
| 8 | } from "./markdownDocument"; |
| 9 | import { parseMarkdown } from "react-native-enriched-markdown/src/web/parseMarkdown"; |
| 10 | |
| 11 | type NativeNode = Awaited<ReturnType<typeof parseMarkdown>>; |
| 12 | |
| 13 | /** Where the native parser sends each link and image in one rendered fragment of a reply. */ |
| 14 | async function referenceDestinations(reply: string, pick: number) { |
| 15 | const segment = splitMarkdownDetails(reply)[pick]; |
| 16 | const fragment = segment?.kind === "details" ? segment.body : (segment?.markdown ?? ""); |
| 17 | const urls: Array<string | undefined> = []; |
| 18 | const visit = (node: NativeNode) => { |
| 19 | if (node.type === "Link" || node.type === "Image") urls.push(node.attributes?.url); |
| 20 | node.children?.forEach(visit); |
| 21 | }; |
| 22 | visit( |
| 23 | await parseMarkdown( |
| 24 | markdownForNativeParsing( |
| 25 | withReferenceDefinitions(fragment, markdownReferenceDefinitions(reply)), |
| 26 | ), |
| 27 | ), |
| 28 | ); |
| 29 | return urls; |
| 30 | } |
| 31 | |
| 32 | describe("splitMarkdownDetails", () => { |
| 33 | // regression: #28059 mobile chat showed every <details> fold open |
| 34 | it("splits a top-level fold into its summary and hidden body", () => { |
| 35 | const markdown = |
| 36 | "Short answer.\n\n<details>\n<summary>Long details</summary>\n\nA long paragraph.\n\n</details>\n\nAfter."; |
| 37 | expect(splitMarkdownDetails(markdown)).toEqual([ |
| 38 | { kind: "markdown", markdown: "Short answer.\n\n", sourceOffset: 0 }, |
| 39 | { |
| 40 | kind: "details", |
| 41 | summary: "Long details", |
| 42 | body: "\n\nA long paragraph.\n\n", |
| 43 | open: false, |
| 44 | sourceOffset: 15, |
| 45 | }, |
| 46 | { kind: "markdown", markdown: "\n\nAfter.", sourceOffset: 87 }, |
| 47 | ]); |
| 48 | }); |
| 49 | |
| 50 | it("keeps nested folds in the body and honours the open attribute", () => { |
| 51 | const markdown = |
| 52 | "<details open>\n<summary>Outer & <code>more</code></summary>\n\n<details>\n<summary>Inner</summary>\n\nx\n\n</details>\n\n</details>"; |
| 53 | const [fold] = splitMarkdownDetails(markdown); |
| 54 | expect(fold).toMatchObject({ kind: "details", summary: "Outer & more", open: true }); |
| 55 | expect(fold?.kind === "details" && splitMarkdownDetails(fold.body)).toMatchObject([ |
| 56 | { kind: "markdown" }, |
| 57 | { kind: "details", summary: "Inner", body: "\n\nx\n\n" }, |
| 58 | { kind: "markdown" }, |
| 59 | ]); |
| 60 | }); |
| 61 | |
| 62 | it("leaves folds in code and inline tags as markdown", () => { |
| 63 | for (const markdown of [ |
| 64 | "```html\n<details>\n<summary>Code</summary>\n</details>\n```", |
| 65 | "Use `<details>` for folds.", |
| 66 | "Text <details><summary>inline</summary>body</details> more", |
| 67 | "<!-- <details> -->\n\nVisible answer.", |
| 68 | ]) { |
| 69 | expect(splitMarkdownDetails(markdown)).toEqual([ |
| 70 | { kind: "markdown", markdown, sourceOffset: 0 }, |
| 71 | ]); |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | it("ignores a closing tag inside a code example in the fold", () => { |
| 76 | const markdown = |
| 77 | "<details>\n<summary>Example</summary>\n```html\n</details>\n```\n</details>\n\nAfter."; |
| 78 | expect(splitMarkdownDetails(markdown)).toMatchObject([ |
| 79 | { kind: "details", summary: "Example", body: "\n```html\n</details>\n```\n" }, |
| 80 | { kind: "markdown", markdown: "\n\nAfter." }, |
| 81 | ]); |
| 82 | }); |
| 83 | |
| 84 | it("closes a fold at a closing tag that ends a paragraph", () => { |
| 85 | expect( |
| 86 | splitMarkdownDetails("<details>\n<summary>Plan</summary>\n\nDone </details>\n\nNext step"), |
| 87 | ).toMatchObject([ |
| 88 | { kind: "details", summary: "Plan", body: "\n\nDone " }, |
| 89 | { kind: "markdown", markdown: "\n\nNext step" }, |
| 90 | ]); |
| 91 | }); |
| 92 | |
| 93 | it("folds the rest of a reply whose closing tag has not streamed yet", () => { |
| 94 | expect(splitMarkdownDetails("<details>\n<summary>Plan</summary>\n\nStep one")).toEqual([ |
| 95 | { kind: "details", summary: "Plan", body: "\n\nStep one", open: false, sourceOffset: 0 }, |
| 96 | ]); |
| 97 | expect( |
| 98 | splitMarkdownDetails("<details>\n<summary>Plan</summary>\n\n```html\n</details>"), |
| 99 | ).toMatchObject([{ kind: "details", body: "\n\n```html\n</details>" }]); |
| 100 | }); |
| 101 | |
| 102 | it("reads open only as an attribute name", () => { |
| 103 | expect( |
| 104 | splitMarkdownDetails('<details title="is open">\n<summary>Plan</summary>\n</details>'), |
| 105 | ).toMatchObject([{ kind: "details", open: false }]); |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | describe("withReferenceDefinitions", () => { |
| 110 | it("resolves a fragment's references from the whole reply, its own definitions first", async () => { |
| 111 | const fold = "\n\n<details><summary>A</summary>a</details>\n\n"; |
| 112 | for (const [reply, pick, expected] of [ |
| 113 | // Into a fold, with an escaped label and a Windows destination kept as written. |
| 114 | [ |
| 115 | "<details>\n<summary>A</summary>\n\n[config][c] ![B][a\\]b]\n\n</details>\n\n[c]: C:\\work\\.t3\\settings.json\n[a\\]b]: https://example.com/a", |
| 116 | 0, |
| 117 | ["C:\\work\\.t3\\settings.json", "https://example.com/a"], |
| 118 | ], |
| 119 | // Out of a fold. |
| 120 | [ |
| 121 | "See [d].\n\n<details>\n<summary>A</summary>\n\n[d]: https://example.com\n\n</details>", |
| 122 | 0, |
| 123 | ["https://example.com"], |
| 124 | ], |
| 125 | // A quoted definition that wraps onto continuation lines. |
| 126 | [`See [d].${fold}> [d]:\n> https://example.com\n> "Title"`, 0, ["https://example.com"]], |
| 127 | // A definition the compact HTML block hides from the reply's parse, with a label that wraps. |
| 128 | [ |
| 129 | "See [the\n doc].\n\n<details><summary>A</summary>a</details>\n[The Doc]: https://example.com", |
| 130 | 0, |
| 131 | ["https://example.com"], |
| 132 | ], |
| 133 | // A literal ">" in a wrapped label is part of the label, not a quote marker. |
| 134 | [ |
| 135 | `[part rest] and [part > rest]${fold}[part\n > rest]: https://example.com/a\n\n[part rest]: https://example.com/b`, |
| 136 | 0, |
| 137 | ["https://example.com/b", "https://example.com/a"], |
| 138 | ], |
| 139 | // A fragment's own definition wins, however the parser equates the two labels. |
| 140 | [ |
| 141 | "[ x ]: https://outside.example\n\n<details>\n<summary>S</summary>\n\nSee [x].\n\n[X]: https://inside.example\n\n</details>", |
| 142 | 1, |
| 143 | ["https://inside.example"], |
| 144 | ], |
| 145 | [ |
| 146 | "[SS]: https://outside.example\n\n<details>\n<summary>S</summary>\n\nSee [SS].\n\n[\u00df]: https://inside.example\n\n</details>", |
| 147 | 1, |
| 148 | ["https://inside.example"], |
| 149 | ], |
| 150 | ] as const) { |
| 151 | expect(await referenceDestinations(reply, pick)).toEqual(expected); |
| 152 | } |
| 153 | }); |
| 154 | |
| 155 | it("leaves a fragment with nothing to resolve as written", () => { |
| 156 | expect(withReferenceDefinitions("See [d].", [])).toBe("See [d]."); |
| 157 | expect(withReferenceDefinitions("Plain text.", ["[d]: https://example.com"])).toBe( |
| 158 | "Plain text.", |
| 159 | ); |
| 160 | }); |
| 161 | }); |