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

nativeMarkdownText.test.ts

1,322 lines39.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { MarkdownNode } from "react-native-nitro-markdown/headless";
3​
4import {
5 nativeMarkdownChunkSpacing,
6 nativeMarkdownDocumentChunks,
7 nativeMarkdownDocumentRuns,
8 nativeMarkdownListItemBlocks,
9 nativeMarkdownTextRuns,
10 nativeMarkdownWithAuthoredWindowsPaths,
11 nativeMarkdownWithPreservedSoftBreaks,
12 nativeMarkdownContextCopyRanges,
13 contextChipPresentation,
14} from "@t3tools/mobile-markdown-text/markdown";
15​
16describe("nativeMarkdownTextRuns", () => {
17 it("distinguishes video and pull-request context from generic file and review chips", () => {
18 expect(
19 contextChipPresentation("file", {
20 name: "recording.webm",
21 mimeType: "application/octet-stream",
22 }),
23 ).toEqual(contextChipPresentation("video"));
24 expect(contextChipPresentation("review-comment", { sectionId: "pull-request:284" })).toEqual(
25 contextChipPresentation("pull-request"),
26 );
27 expect(contextChipPresentation("review-comment", { sectionId: "git:working-tree" })).toEqual(
28 contextChipPresentation("review-comment"),
29 );
30 });
31​
32 it("renders a video-named file with a declared document MIME type as a file chip", () => {
33 expect(
34 contextChipPresentation("file", {
35 name: "recording.mp4",
36 mimeType: "application/pdf",
37 }),
38 ).toEqual(contextChipPresentation("file"));
39 });
40​
41 it("maps rendered selection offsets back to canonical references without losing repeated chips", () => {
42 const href = "t3-context://v1/image/screenshot";
43 expect(
44 nativeMarkdownContextCopyRanges([
45 { run: { text: "πŸ˜€ " }, text: "πŸ˜€ ", inlineImageLength: 0 },
46 { run: { href, text: "Checkout" }, text: "Checkout", inlineImageLength: 1 },
47 { run: { text: " then " }, text: " then ", inlineImageLength: 0 },
48 { run: { href, text: "Checkout" }, text: "\uFFFC\u00A0Checkout", inlineImageLength: 0 },
49 ]),
50 ).toEqual([
51 { start: 3, end: 12, text: "![Checkout](t3-context://v1/image/screenshot)" },
52 { start: 18, end: 28, text: "![Checkout](t3-context://v1/image/screenshot)" },
53 ]);
54 });
55 it("restores canonical skill and context text from Android's single-image chips", () => {
56 expect(
57 nativeMarkdownContextCopyRanges([
58 { run: { text: "Use " }, text: "Use ", inlineImageLength: 0 },
59 { run: { text: "Playwright", skillName: "playwright" }, text: "", inlineImageLength: 1 },
60 { run: { text: " on " }, text: " on ", inlineImageLength: 0 },
61 {
62 run: { text: "Screenshot", href: "t3-context://v1/image/screenshot" },
63 text: "",
64 inlineImageLength: 1,
65 },
66 ]),
67 ).toEqual([
68 { start: 4, end: 5, text: "$playwright" },
69 { start: 9, end: 10, text: "![Screenshot](t3-context://v1/image/screenshot)" },
70 ]);
71 });
72 it("links a path-shaped code span without changing the same path in prose", () => {
73 expect(
74 nativeMarkdownTextRuns({
75 type: "paragraph",
76 children: [
77 { type: "text", content: "/tmp/frame.png " },
78 { type: "code_inline", content: "/tmp/frame.png" },
79 ],
80 }),
81 ).toEqual([
82 { text: "/tmp/frame.png " },
83 { text: "frame.png", href: "/tmp/frame.png", fileIcon: "image" },
84 ]);
85 });
86​
87 it("preserves the destination of a link with a code-formatted label", () => {
88 expect(
89 nativeMarkdownTextRuns({
90 type: "paragraph",
91 children: [
92 {
93 type: "link",
94 href: "https://example.com/docs",
95 children: [{ type: "code_inline", content: "src/main.ts" }],
96 },
97 ],
98 }),
99 ).toEqual([
100 {
101 text: "src/main.ts",
102 code: true,
103 href: "https://example.com/docs",
104 externalHost: "example.com",
105 },
106 ]);
107 });
108​
109 it("preserves inline emphasis and code styles", () => {
110 const node: MarkdownNode = {
111 type: "paragraph",
112 children: [
113 { type: "text", content: "plain " },
114 { type: "bold", children: [{ type: "text", content: "bold" }] },
115 { type: "text", content: " " },
116 { type: "code_inline", content: "const value = 1" },
117 ],
118 };
119​
120 expect(nativeMarkdownTextRuns(node)).toEqual([
121 { text: "plain " },
122 { text: "bold", bold: true },
123 { text: " " },
124 { text: "const value = 1", code: true },
125 ]);
126 });
127​
128 it("normalizes external and file links for native presentation", () => {
129 const node: MarkdownNode = {
130 type: "paragraph",
131 children: [
132 {
133 type: "link",
134 href: "https://example.com/docs",
135 children: [{ type: "text", content: "Docs" }],
136 },
137 { type: "text", content: " " },
138 {
139 type: "link",
140 href: "file:///repo/README.md#L12",
141 children: [{ type: "text", content: "validates the input" }],
142 },
143 ],
144 };
145​
146 expect(nativeMarkdownTextRuns(node)).toEqual([
147 {
148 text: "Docs",
149 href: "https://example.com/docs",
150 externalHost: "example.com",
151 },
152 { text: " " },
153 {
154 text: "validates the input ",
155 href: "file:///repo/README.md#L12",
156 sourceText: "[validates the input](<file:///repo/README.md#L12>)",
157 },
158 {
159 text: "README.md:12",
160 href: "file:///repo/README.md#L12",
161 fileIcon: "markdown",
162 sourceText: "[validates the input](<file:///repo/README.md#L12>)",
163 },
164 ]);
165 });
166​
167 it.each([true, false])("copies descriptive file links with collapsed chips=%s", (collapsed) => {
168 const link: MarkdownNode = {
169 type: "link",
170 href: "/repo/src/example.ts:12",
171 children: [
172 { type: "text", content: "validates " },
173 { type: "bold", children: [{ type: "text", content: "the input" }] },
174 ],
175 };
176 const runs = nativeMarkdownTextRuns({ type: "paragraph", children: [link, link] });
177 expect(runs.map((run) => run.text).join("")).toBe(
178 "validates the input example.ts:12validates the input example.ts:12",
179 );
180 expect(runs.filter((run) => run.bold).map((run) => run.text)).toEqual([
181 "the input",
182 "the input",
183 ]);
184 const ranges = nativeMarkdownContextCopyRanges(
185 runs.map((run) => ({
186 run,
187 text: collapsed && run.fileIcon ? "" : run.text,
188 inlineImageLength: run.fileIcon ? 1 : 0,
189 })),
190 );
191 const length = "validates the input ".length + (collapsed ? 1 : "example.ts:12".length + 1);
192 expect(ranges).toEqual([
193 { start: 0, end: length, text: "[validates **the input**](</repo/src/example.ts:12>)" },
194 {
195 start: length,
196 end: length * 2,
197 text: "[validates **the input**](</repo/src/example.ts:12>)",
198 },
199 ]);
200 });
201​
202 it("keeps filename-labelled links as chips", () => {
203 expect(
204 nativeMarkdownTextRuns({
205 type: "paragraph",
206 children: [
207 {
208 type: "link",
209 href: "/repo/src/example.ts:12",
210 children: [{ type: "code_inline", content: "src/example.ts:12" }],
211 },
212 ],
213 }),
214 ).toEqual([{ text: "example.ts:12", href: "/repo/src/example.ts:12", fileIcon: "typescript" }]);
215 });
216​
217 it.each([
218 [{ type: "italic", children: [{ type: "text", content: "details" }] }, "*details*"],
219 [{ type: "strikethrough", children: [{ type: "text", content: "details" }] }, "~~details~~"],
220 [{ type: "code_inline", content: "`details`" }, "`` `details` ``"],
221 [{ type: "code_inline", content: " details " }, "` details `"],
222 [{ type: "text", content: "[details] *literal*" }, "\\[details\\] \\*literal\\*"],
223 [{ type: "soft_break" }, "\n"],
224 [{ type: "line_break" }, " \n"],
225 [
226 { type: "image", alt: "details", href: "https://example.com/icon.png" },
227 "![details](<https://example.com/icon.png>)",
228 ],
229 ] satisfies ReadonlyArray<readonly [MarkdownNode, string]>)(
230 "keeps descriptive label markup when copying %j",
231 (child, source) => {
232 const runs = nativeMarkdownTextRuns({
233 type: "paragraph",
234 children: [
235 {
236 type: "link",
237 href: "/repo/a.ts",
238 children: [{ type: "text", content: "see " }, child],
239 },
240 ],
241 });
242 expect(
243 nativeMarkdownContextCopyRanges(
244 runs.map((run) => ({
245 run,
246 text: run.fileIcon ? "" : run.text,
247 inlineImageLength: run.fileIcon ? 1 : 0,
248 })),
249 ),
250 ).toEqual([
251 {
252 start: 0,
253 end:
254 runs
255 .filter((run) => !run.fileIcon)
256 .reduce((length, run) => length + run.text.length, 0) + 1,
257 text: `[see ${source}](</repo/a.ts>)`,
258 },
259 ]);
260 },
261 );
262​
263 it("keeps hard breaks and collapses soft breaks", () => {
264 const node: MarkdownNode = {
265 type: "paragraph",
266 children: [
267 { type: "text", content: "first" },
268 { type: "soft_break" },
269 { type: "text", content: "second" },
270 { type: "line_break" },
271 { type: "text", content: "third" },
272 ],
273 };
274​
275 expect(nativeMarkdownTextRuns(node)).toEqual([{ text: "first second\nthird" }]);
276 });
277​
278 it("can preserve soft breaks for authored user messages", () => {
279 const node: MarkdownNode = {
280 type: "paragraph",
281 children: [
282 { type: "text", content: "first" },
283 { type: "soft_break" },
284 { type: "text", content: "second" },
285 ],
286 };
287​
288 expect(nativeMarkdownTextRuns(nativeMarkdownWithPreservedSoftBreaks(node))).toEqual([
289 { text: "first\nsecond" },
290 ]);
291 });
292​
293 it("keeps Windows path backslashes the parser reads as escapes", () => {
294 const markdown = [
295 String.raw`![shot](C:\Users\me\.t3\_build\shot.png "Shot")`,
296 String.raw`[settings](C:\Users\me\.claude\settings.json) and [site](https://example.com/a\.b)`,
297 "![ref][ref]",
298 String.raw`[ref]: \\wsl.localhost\Ubuntu\.t3\ref.png`,
299 ].join("\n\n");
300 // md4c drops each backslash that precedes punctuation.
301 const node: MarkdownNode = {
302 type: "document",
303 children: [
304 { type: "image", href: String.raw`C:\Users\me.t3_build\shot.png` },
305 { type: "link", href: String.raw`C:\Users\me.claude\settings.json` },
306 { type: "link", href: "https://example.com/a.b" },
307 { type: "image", href: String.raw`\wsl.localhost\Ubuntu.t3\ref.png` },
308 ],
309 };
310​
311 expect(
312 nativeMarkdownWithAuthoredWindowsPaths(node, markdown).children?.map(({ href }) => href),
313 ).toEqual([
314 String.raw`C:\Users\me\.t3\_build\shot.png`,
315 String.raw`C:\Users\me\.claude\settings.json`,
316 "https://example.com/a.b",
317 String.raw`\\wsl.localhost\Ubuntu\.t3\ref.png`,
318 ]);
319 });
320​
321 it("leaves a Windows path as parsed when two written paths could have produced it", () => {
322 const markdown = [
323 String.raw`\`![example](C:\Users\me\.t3\shot.png)\``,
324 String.raw`![real](C:\Users\me.t3\shot.png)`,
325 ].join("\n\n");
326 const node: MarkdownNode = {
327 type: "document",
328 children: [{ type: "image", href: String.raw`C:\Users\me.t3\shot.png` }],
329 };
330​
331 expect(nativeMarkdownWithAuthoredWindowsPaths(node, markdown).children?.[0]?.href).toBe(
332 String.raw`C:\Users\me.t3\shot.png`,
333 );
334 });
335​
336 it("normalizes common inline HTML and entities", () => {
337 const node: MarkdownNode = {
338 type: "paragraph",
339 children: [
340 { type: "text", content: "Less than: &lt; " },
341 { type: "html_inline", content: "<kbd>" },
342 { type: "text", content: "⌘" },
343 { type: "html_inline", content: "</kbd>" },
344 { type: "html_inline", content: "<br />" },
345 { type: "html_inline", content: "<mark>highlighted</mark>" },
346 ],
347 };
348​
349 expect(nativeMarkdownTextRuns(node)).toEqual([{ text: "Less than: < ⌘\nhighlighted" }]);
350 });
351​
352 it("normalizes double-encoded entities and inline tags emitted as text", () => {
353 const node: MarkdownNode = {
354 type: "paragraph",
355 children: [
356 {
357 type: "text",
358 content:
359 "Keyboard: <kbd>⌘</kbd> + <kbd>K</kbd>; Less than: &amp;lt;; Greater than: &amp;gt;",
360 },
361 ],
362 };
363​
364 expect(nativeMarkdownTextRuns(node)).toEqual([
365 { text: "Keyboard: ⌘ + K; Less than: <; Greater than: >" },
366 ]);
367 });
368​
369 it.each([
370 ["&#128512;", "πŸ˜€"],
371 ["&#x1f680;", "πŸš€"],
372 ["&#9999999999;", "&#9999999999;"],
373 ["&#x110000;", "&#x110000;"],
374 ["&amp;#9999999999;", "&#9999999999;"],
375 ["&amp;#x110000;", "&#x110000;"],
376 ])("normalizes numeric entity %s without throwing", (content, expected) => {
377 const node: MarkdownNode = {
378 type: "paragraph",
379 children: [{ type: "text", content }],
380 };
381​
382 expect(nativeMarkdownTextRuns(node)).toEqual([{ text: expected }]);
383 });
384​
385 it("reads inline content from nested text nodes", () => {
386 const node: MarkdownNode = {
387 type: "paragraph",
388 children: [
389 {
390 type: "text",
391 children: [{ type: "text", content: "Plain text" }],
392 },
393 { type: "text", content: " and " },
394 {
395 type: "code_inline",
396 children: [{ type: "text", content: "inline code" }],
397 },
398 ],
399 };
400​
401 expect(nativeMarkdownTextRuns(node)).toEqual([
402 { text: "Plain text and " },
403 { text: "inline code", code: true },
404 ]);
405 });
406});
407​
408describe("nativeMarkdownDocumentRuns", () => {
409 it("renders a file mention without swallowing sentence punctuation or changing package references", () => {
410 const runs = nativeMarkdownDocumentRuns({
411 type: "document",
412 children: [
413 {
414 type: "paragraph",
415 children: [
416 { type: "text", content: "Inspect @src/Checkout.tsx. Use @t3tools/contracts." },
417 ],
418 },
419 ],
420 });
421 expect(runs).toEqual([
422 { text: "Inspect ", role: "body" },
423 {
424 text: "Checkout.tsx",
425 role: "body",
426 href: "src/Checkout.tsx",
427 fileIcon: "react",
428 sourceText: "@src/Checkout.tsx",
429 },
430 { text: ". Use @t3tools/contracts.", role: "body" },
431 ]);
432 });
433​
434 it("copies collapsed skill and file chips back to their original references", () => {
435 expect(
436 nativeMarkdownContextCopyRanges([
437 { run: { text: "$ui", skillName: "ui" }, text: "\uFFFC", inlineImageLength: 0 },
438 { run: { text: " and " }, text: " and ", inlineImageLength: 0 },
439 {
440 run: {
441 text: "Checkout.tsx",
442 href: "src/Checkout.tsx",
443 fileIcon: "react",
444 sourceText: "@src/Checkout.tsx",
445 },
446 text: "\uFFFC",
447 inlineImageLength: 0,
448 },
449 ]),
450 ).toEqual([
451 { start: 0, end: 1, text: "$ui" },
452 { start: 6, end: 7, text: "@src/Checkout.tsx" },
453 ]);
454 });
455​
456 it.each(["$", "€", "Β£", "Β₯", "β‚Ή", "β‚©", "β‚Ώ", "𑿝"])(
457 "decorates %s skill references as selectable skill links",
458 (prefix) => {
459 const node: MarkdownNode = {
460 type: "document",
461 children: [
462 {
463 type: "paragraph",
464 children: [{ type: "text", content: `Use ${prefix}ui for this.` }],
465 },
466 ],
467 };
468​
469 expect(nativeMarkdownDocumentRuns(node, [{ name: "ui", displayName: "UI" }])).toEqual([
470 { text: "Use ", role: "body" },
471 {
472 text: `${prefix}ui`,
473 role: "body",
474 skillName: "ui",
475 skillLabel: "UI",
476 },
477 { text: " for this.", role: "body" },
478 ]);
479 },
480 );
481​
482 it("decorates known skill references that begin with a digit", () => {
483 const node: MarkdownNode = {
484 type: "document",
485 children: [
486 {
487 type: "paragraph",
488 children: [{ type: "text", content: "Use $2spec for this." }],
489 },
490 ],
491 };
492​
493 expect(nativeMarkdownDocumentRuns(node, [{ name: "2spec", displayName: "2Spec" }])).toEqual([
494 { text: "Use ", role: "body" },
495 {
496 text: "$2spec",
497 role: "body",
498 skillName: "2spec",
499 skillLabel: "2Spec",
500 },
501 { text: " for this.", role: "body" },
502 ]);
503 });
504​
505 it("decorates known skill references inside blockquotes", () => {
506 const node: MarkdownNode = {
507 type: "blockquote",
508 children: [
509 {
510 type: "paragraph",
511 children: [{ type: "text", content: "Use $ui for this." }],
512 },
513 ],
514 };
515​
516 expect(nativeMarkdownDocumentRuns(node, [{ name: "ui", displayName: "UI" }])).toContainEqual({
517 text: "$ui",
518 role: "body",
519 skillName: "ui",
520 skillLabel: "UI",
521 });
522 });
523​
524 it("leaves unknown skill-like text unchanged", () => {
525 const node: MarkdownNode = {
526 type: "document",
527 children: [
528 {
529 type: "paragraph",
530 children: [{ type: "text", content: "Use $unknown for this." }],
531 },
532 ],
533 };
534​
535 expect(nativeMarkdownDocumentRuns(node, [])).toEqual([
536 { text: "Use $unknown for this.", role: "body" },
537 ]);
538 });
539​
540 it("keeps headings, paragraphs, and lists in one continuous document", () => {
541 const node: MarkdownNode = {
542 type: "document",
543 children: [
544 {
545 type: "heading",
546 level: 1,
547 children: [{ type: "text", content: "Header One" }],
548 },
549 {
550 type: "paragraph",
551 children: [
552 { type: "text", content: "A paragraph with " },
553 { type: "bold", children: [{ type: "text", content: "bold text" }] },
554 { type: "text", content: "." },
555 ],
556 },
557 {
558 type: "list",
559 ordered: false,
560 children: [
561 {
562 type: "list_item",
563 children: [
564 {
565 type: "paragraph",
566 children: [{ type: "text", content: "First item" }],
567 },
568 ],
569 },
570 {
571 type: "list_item",
572 children: [
573 {
574 type: "paragraph",
575 children: [{ type: "text", content: "Second item" }],
576 },
577 ],
578 },
579 ],
580 },
581 ],
582 };
583​
584 const runs = nativeMarkdownDocumentRuns(node);
585 expect(runs.map((run) => run.text).join("")).toBe(
586 "Header One\n\nA paragraph with bold text.\n\nβ€’\tFirst item\nβ€’\tSecond item",
587 );
588 expect(runs).toContainEqual({
589 text: "Header One\n",
590 role: "heading",
591 headingLevel: 1,
592 });
593 expect(runs).toContainEqual({
594 text: "bold text",
595 bold: true,
596 role: "body",
597 });
598 expect(runs).toContainEqual({
599 text: "β€’\t",
600 role: "list-marker",
601 depth: 1,
602 firstLineHeadIndent: 0,
603 headIndent: 24,
604 paragraphSpacing: 2,
605 });
606 });
607​
608 it("uses distinct section, heading-content, and body spacing", () => {
609 const node: MarkdownNode = {
610 type: "document",
611 children: [
612 {
613 type: "paragraph",
614 children: [{ type: "text", content: "Intro" }],
615 },
616 {
617 type: "heading",
618 level: 2,
619 children: [{ type: "text", content: "Section" }],
620 },
621 {
622 type: "paragraph",
623 children: [{ type: "text", content: "First paragraph" }],
624 },
625 {
626 type: "paragraph",
627 children: [{ type: "text", content: "Second paragraph" }],
628 },
629 ],
630 };
631​
632 expect(
633 nativeMarkdownDocumentRuns(node)
634 .filter((run) => run.role === "spacer")
635 .map((run) => run.spacing),
636 ).toEqual([20, 10, 12]);
637 });
638​
639 it("renders tight list items whose inline nodes are direct children", () => {
640 const node: MarkdownNode = {
641 type: "document",
642 children: [
643 {
644 type: "list",
645 children: [
646 {
647 type: "list_item",
648 children: [
649 {
650 type: "bold",
651 children: [{ type: "text", content: "Finding:" }],
652 },
653 { type: "text", content: " details with " },
654 { type: "code_inline", content: "inline code" },
655 { type: "text", content: "." },
656 ],
657 },
658 ],
659 },
660 ],
661 };
662​
663 expect(nativeMarkdownDocumentRuns(node)).toEqual([
664 {
665 text: "β€’\t",
666 role: "list-marker",
667 depth: 1,
668 firstLineHeadIndent: 0,
669 headIndent: 24,
670 paragraphSpacing: 2,
671 },
672 { text: "Finding:", bold: true, role: "body", depth: 1 },
673 { text: " details with ", role: "body", depth: 1 },
674 { text: "inline code", code: true, role: "body", depth: 1 },
675 { text: ".", role: "body", depth: 1 },
676 ]);
677 });
678​
679 it("preserves quotes and fenced code in document runs", () => {
680 const node: MarkdownNode = {
681 type: "document",
682 children: [
683 {
684 type: "blockquote",
685 children: [
686 {
687 type: "paragraph",
688 children: [{ type: "text", content: "Read this" }],
689 },
690 ],
691 },
692 {
693 type: "code_block",
694 language: "ts",
695 content: "const answer = 42;",
696 },
697 ],
698 };
699​
700 const runs = nativeMarkdownDocumentRuns(node);
701 expect(runs.map((run) => run.text).join("")).toBe("β”‚\u00a0Read this\n\nTS\nconst answer = 42;");
702 expect(runs).toContainEqual({
703 text: "const answer = 42;",
704 code: true,
705 role: "code-block",
706 });
707 });
708​
709 it("reads fenced code content from child text nodes", () => {
710 const node: MarkdownNode = {
711 type: "document",
712 children: [
713 {
714 type: "code_block",
715 language: "bash",
716 children: [{ type: "text", content: "pnpm install\n" }],
717 },
718 ],
719 };
720​
721 expect(
722 nativeMarkdownDocumentRuns(node)
723 .map((run) => run.text)
724 .join(""),
725 ).toBe("BASH\npnpm install");
726 });
727​
728 it("keeps adjacent context links with the same href in separate runs", () => {
729 const href = "t3-context://v1/terminal/ctx-1";
730 const link = (content: string): MarkdownNode => ({
731 type: "link",
732 href,
733 children: [{ type: "text", content }],
734 });
735 const runs = nativeMarkdownDocumentRuns({
736 type: "document",
737 children: [{ type: "paragraph", children: [link("First"), link("Second")] }],
738 });
739​
740 // Merging these would render one chip and emit one copy range with a
741 // combined label for two distinct references.
742 expect(runs).toEqual([
743 { text: "First", role: "body", href, fileIcon: "bash" },
744 { text: "Second", role: "body", href, fileIcon: "bash" },
745 ]);
746 });
747});
748​
749describe("nativeMarkdownListItemBlocks", () => {
750 it("groups consecutive inline nodes into one paragraph block", () => {
751 const item: MarkdownNode = {
752 type: "list_item",
753 children: [
754 { type: "text", content: "Finding: " },
755 { type: "bold", children: [{ type: "text", content: "important" }] },
756 { type: "text", content: " details." },
757 {
758 type: "list",
759 children: [
760 {
761 type: "list_item",
762 children: [{ type: "text", content: "Nested" }],
763 },
764 ],
765 },
766 { type: "text", content: "Trailing prose." },
767 ],
768 };
769​
770 expect(nativeMarkdownListItemBlocks(item)).toEqual([
771 {
772 type: "paragraph",
773 children: item.children?.slice(0, 3),
774 },
775 item.children?.[3],
776 {
777 type: "paragraph",
778 children: [item.children?.[4]],
779 },
780 ]);
781 });
782});
783​
784describe("nativeMarkdownDocumentChunks", () => {
785 it("renders plain blockquotes as rich blocks so their marker spans wrapped lines", () => {
786 const blockquote: MarkdownNode = {
787 type: "blockquote",
788 beg: 0,
789 end: 120,
790 children: [
791 {
792 type: "paragraph",
793 children: [
794 {
795 type: "text",
796 content:
797 "Persistent random per-result keys are the strongest design, even when this text wraps.",
798 },
799 ],
800 },
801 ],
802 };
803​
804 expect(
805 nativeMarkdownDocumentChunks({
806 type: "document",
807 children: [blockquote],
808 }),
809 ).toEqual([
810 {
811 kind: "rich",
812 key: "rich:blockquote:offset:0",
813 node: blockquote,
814 },
815 ]);
816 });
817​
818 it("keeps headings and plain lists in one selectable document", () => {
819 const document: MarkdownNode = {
820 type: "document",
821 children: [
822 {
823 type: "heading",
824 level: 2,
825 children: [{ type: "text", content: "Tasks" }],
826 },
827 {
828 type: "list",
829 children: [
830 {
831 type: "task_list_item",
832 checked: true,
833 children: [
834 {
835 type: "paragraph",
836 children: [{ type: "text", content: "Completed" }],
837 },
838 ],
839 },
840 {
841 type: "list_item",
842 children: [
843 {
844 type: "paragraph",
845 children: [{ type: "text", content: "Parent" }],
846 },
847 {
848 type: "list",
849 children: [
850 {
851 type: "list_item",
852 children: [
853 {
854 type: "paragraph",
855 children: [{ type: "text", content: "Nested" }],
856 },
857 ],
858 },
859 ],
860 },
861 ],
862 },
863 ],
864 },
865 ],
866 };
867​
868 const chunks = nativeMarkdownDocumentChunks(document);
869 expect(chunks).toHaveLength(1);
870 expect(chunks[0]).toMatchObject({ kind: "selectable" });
871 expect(
872 nativeMarkdownDocumentRuns(chunks[0]?.node ?? document)
873 .map((run) => run.text)
874 .join(""),
875 ).toBe("Tasks\n\nβ˜‘οΈŽ\tCompleted\nβ€’\tParent\nβ—¦\tNested");
876 });
877​
878 it("aligns ordered markers while keeping the list in one selectable string", () => {
879 const document: MarkdownNode = {
880 type: "document",
881 children: [
882 {
883 type: "list",
884 ordered: true,
885 start: 9,
886 children: [
887 {
888 type: "list_item",
889 children: [{ type: "text", content: "Ninth" }],
890 },
891 {
892 type: "list_item",
893 children: [{ type: "text", content: "Tenth" }],
894 },
895 ],
896 },
897 ],
898 };
899​
900 expect(
901 nativeMarkdownDocumentRuns(document)
902 .map((run) => run.text)
903 .join(""),
904 ).toBe("\u20079.\tNinth\n10.\tTenth");
905 });
906​
907 it("keeps prose selectable while exposing rich AST blocks", () => {
908 const document: MarkdownNode = {
909 type: "document",
910 children: [
911 {
912 type: "heading",
913 level: 1,
914 beg: 0,
915 end: 9,
916 children: [{ type: "text", content: "Install" }],
917 },
918 {
919 type: "code_block",
920 language: "bash",
921 beg: 11,
922 end: 35,
923 children: [{ type: "text", content: "pnpm install\n" }],
924 },
925 {
926 type: "paragraph",
927 beg: 37,
928 end: 42,
929 children: [{ type: "text", content: "Done." }],
930 },
931 ],
932 };
933​
934 const chunks = nativeMarkdownDocumentChunks(document);
935 expect(chunks).toHaveLength(3);
936 expect(chunks[0]).toMatchObject({ kind: "selectable" });
937 expect(chunks[1]).toEqual({
938 kind: "rich",
939 key: "rich:code_block:offset:11",
940 node: document.children?.[1],
941 });
942 expect(chunks[2]).toMatchObject({ kind: "selectable" });
943 });
944​
945 it("keeps a list containing fenced code as one rich AST container", () => {
946 const document: MarkdownNode = {
947 type: "document",
948 children: [
949 {
950 type: "list",
951 beg: 0,
952 end: 45,
953 children: [
954 {
955 type: "list_item",
956 children: [
957 {
958 type: "paragraph",
959 children: [{ type: "text", content: "Install" }],
960 },
961 {
962 type: "code_block",
963 language: "bash",
964 children: [{ type: "text", content: "pnpm install\n" }],
965 },
966 ],
967 },
968 ],
969 },
970 ],
971 };
972​
973 expect(nativeMarkdownDocumentChunks(document)).toEqual([
974 {
975 kind: "rich",
976 key: "rich:list:offset:0",
977 node: document.children?.[0],
978 },
979 ]);
980 });
981​
982 it("keeps surrounding prose selectable when rich nodes have no source offsets", () => {
983 const document: MarkdownNode = {
984 type: "document",
985 children: [
986 {
987 type: "heading",
988 level: 1,
989 children: [{ type: "text", content: "Before" }],
990 },
991 { type: "horizontal_rule" },
992 {
993 type: "paragraph",
994 children: [{ type: "text", content: "After." }],
995 },
996 ],
997 };
998​
999 const chunks = nativeMarkdownDocumentChunks(document);
1000 expect(chunks).toHaveLength(3);
1001 expect(chunks[0]).toMatchObject({ kind: "selectable" });
1002 expect(chunks[1]).toEqual({
1003 kind: "rich",
1004 key: "rich:horizontal_rule:index:1",
1005 node: document.children?.[1],
1006 });
1007 expect(chunks[2]).toMatchObject({ kind: "selectable" });
1008 });
1009​
1010 it("keeps offset-free structural lists isolated without promoting the whole document", () => {
1011 const document: MarkdownNode = {
1012 type: "document",
1013 children: [
1015 type: "paragraph",
1016 children: [{ type: "text", content: "Before." }],
1017 },
1019 type: "list",
1020 ordered: true,
1021 children: [
1023 type: "list_item",
1024 children: [
1026 type: "paragraph",
1027 children: [{ type: "text", content: "Install" }],
1028 },
1030 type: "code_block",
1031 language: "bash",
1032 children: [{ type: "text", content: "pnpm install\n" }],
1033 },
1034 ],
1035 },
1036 ],
1037 },
1039 type: "paragraph",
1040 children: [{ type: "text", content: "After." }],
1041 },
1042 ],
1043 };
1044​
1045 const chunks = nativeMarkdownDocumentChunks(document);
1046 expect(chunks).toHaveLength(3);
1047 expect(chunks[0]).toMatchObject({ kind: "selectable" });
1048 expect(chunks[1]).toEqual({
1049 kind: "rich",
1050 key: "rich:list:index:1",
1051 node: document.children?.[1],
1052 });
1053 expect(chunks[2]).toMatchObject({ kind: "selectable" });
1054 });
1055​
1056 it("never collapses a rich subtree into a second markdown parsing pass", () => {
1057 const document: MarkdownNode = {
1058 type: "document",
1059 children: [
1061 type: "paragraph",
1062 children: [{ type: "text", content: "Before." }],
1063 },
1065 type: "blockquote",
1066 children: [
1068 type: "list",
1069 children: [
1071 type: "list_item",
1072 children: [
1073 { type: "text", content: "Run this" },
1075 type: "code_block",
1076 language: "sh",
1077 children: [{ type: "text", content: "vp check\n" }],
1078 },
1079 ],
1080 },
1081 ],
1082 },
1083 ],
1084 },
1086 type: "paragraph",
1087 children: [{ type: "text", content: "After." }],
1088 },
1089 ],
1090 };
1091​
1092 const chunks = nativeMarkdownDocumentChunks(document);
1093 expect(chunks.map((chunk) => chunk.kind)).toEqual(["selectable", "rich", "selectable"]);
1094 expect(chunks[1]).toMatchObject({
1095 kind: "rich",
1096 node: { type: "blockquote" },
1097 });
1098 });
1099​
1100 it("keys positioned and offset-free siblings in separate namespaces", () => {
1101 const positioned: MarkdownNode = {
1102 type: "blockquote",
1103 beg: 1,
1104 end: 10,
1105 children: [{ type: "paragraph", children: [{ type: "text", content: "Positioned" }] }],
1106 };
1107 const offsetFree: MarkdownNode = {
1108 type: "blockquote",
1109 children: [{ type: "paragraph", children: [{ type: "text", content: "Generated" }] }],
1110 };
1111 const chunks = nativeMarkdownDocumentChunks({
1112 type: "document",
1113 children: [
1114 { type: "paragraph", beg: 0, end: 0, children: [] },
1115 offsetFree,
1116 positioned,
1117 { type: "paragraph", children: [{ type: "text", content: "Tail" }] },
1118 ],
1119 });
1120​
1121 expect(chunks.map((chunk) => chunk.key)).toEqual([
1122 "selectable:offset:0",
1123 "rich:blockquote:index:1",
1124 "rich:blockquote:offset:1",
1125 "selectable:index:3",
1126 ]);
1127 });
1128​
1129 it("gives every offset-free selectable group its own key", () => {
1130 const chunks = nativeMarkdownDocumentChunks({
1131 type: "document",
1132 children: [
1133 { type: "paragraph", children: [{ type: "text", content: "One" }] },
1134 { type: "horizontal_rule" },
1135 { type: "paragraph", children: [{ type: "text", content: "Two" }] },
1136 { type: "horizontal_rule" },
1137 { type: "paragraph", children: [{ type: "text", content: "Three" }] },
1138 ],
1139 });
1140​
1141 expect(chunks.map((chunk) => chunk.key)).toEqual([
1142 "selectable:index:0",
1143 "rich:horizontal_rule:index:1",
1144 "selectable:index:2",
1145 "rich:horizontal_rule:index:3",
1146 "selectable:index:4",
1147 ]);
1148 });
1149​
1150 it("keeps positioned chunk keys stable while text streams in", () => {
1151 const before: MarkdownNode = {
1152 type: "document",
1153 children: [
1154 { type: "paragraph", beg: 0, end: 5, children: [{ type: "text", content: "Intro" }] },
1156 type: "code_block",
1157 language: "ts",
1158 beg: 7,
1159 end: 20,
1160 children: [{ type: "text", content: "const a" }],
1161 },
1162 ],
1163 };
1164 const after: MarkdownNode = {
1165 type: "document",
1166 children: [
1167 { type: "paragraph", beg: 0, end: 5, children: [{ type: "text", content: "Intro" }] },
1169 type: "code_block",
1170 language: "ts",
1171 beg: 7,
1172 end: 40,
1173 children: [{ type: "text", content: "const a = 1;\nconst b" }],
1174 },
1175 ],
1176 };
1177​
1178 expect(nativeMarkdownDocumentChunks(after).map((chunk) => chunk.key)).toEqual(
1179 nativeMarkdownDocumentChunks(before).map((chunk) => chunk.key),
1180 );
1181 });
1182​
1183 it("keeps a plain list in one selectable native text container", () => {
1184 const list: MarkdownNode = {
1185 type: "list",
1186 ordered: false,
1187 children: [
1189 type: "list_item",
1190 children: [{ type: "text", content: "First" }],
1191 },
1192 ],
1193 };
1194​
1195 const chunks = nativeMarkdownDocumentChunks({
1196 type: "document",
1197 children: [list],
1198 });
1199​
1200 expect(chunks).toHaveLength(1);
1201 expect(chunks[0]).toMatchObject({
1202 kind: "selectable",
1203 node: { type: "document", children: [list] },
1204 });
1205 });
1206​
1207 it("separates sections more than related rich blocks", () => {
1208 const headingChunk = {
1209 kind: "selectable" as const,
1210 key: "heading",
1211 node: {
1212 type: "document",
1213 children: [
1215 type: "heading",
1216 level: 2,
1217 children: [{ type: "text", content: "Section" }],
1218 },
1219 ],
1220 } satisfies MarkdownNode,
1221 };
1222 const firstList = {
1223 kind: "rich" as const,
1224 key: "list-1",
1225 node: { type: "list", children: [] } satisfies MarkdownNode,
1226 };
1227 const secondList = {
1228 kind: "rich" as const,
1229 key: "list-2",
1230 node: { type: "list", children: [] } satisfies MarkdownNode,
1231 };
1232​
1233 expect(nativeMarkdownChunkSpacing(undefined, headingChunk)).toBe(0);
1234 expect(nativeMarkdownChunkSpacing(headingChunk, firstList)).toBe(10);
1235 expect(nativeMarkdownChunkSpacing(firstList, secondList)).toBe(12);
1236 expect(nativeMarkdownChunkSpacing(firstList, headingChunk)).toBe(20);
1237 });
1238});
1239​
1240describe("composerChipSizeSuffix", () => {
1241 it("labels attachment records with a human size, matching web's chip", async () => {
1242 const { composerChipSizeSuffix } = await import("@t3tools/mobile-markdown-text/markdown");
1243 expect(composerChipSizeSuffix({ kind: "file", sizeBytes: 1024 })).toBe("1 KB");
1244 expect(composerChipSizeSuffix({ kind: "file", sizeBytes: 3_700_000 })).toBe("3.5 MB");
1245 expect(composerChipSizeSuffix({ kind: "image", sizeBytes: 2048 })).toBe("2 KB");
1246 });
1247​
1248 it("adds nothing for records that carry no bytes", async () => {
1249 const { composerChipSizeSuffix } = await import("@t3tools/mobile-markdown-text/markdown");
1250 // Terminal/review/PR chips have no size to show.
1251 expect(composerChipSizeSuffix({ kind: "terminal" })).toBe("");
1252 expect(composerChipSizeSuffix({ kind: "file" })).toBe("");
1253 expect(composerChipSizeSuffix(undefined)).toBe("");
1254 });
1255});
1256​
1257describe("contextChipPresentation image detection", () => {
1258 it("treats a picture attached through the file picker as an image", async () => {
1259 const { contextChipPresentation } = await import("@t3tools/mobile-markdown-text/markdown");
1260 // The document picker types every pick as `file`, so the name has to carry the intent.
1261 expect(
1262 contextChipPresentation("file", { kind: "file", name: "IMG_4997.PNG", mimeType: "" }),
1263 ).toEqual({ accent: "#d55665", symbol: "photo" });
1264 expect(
1265 contextChipPresentation("file", {
1266 kind: "file",
1267 name: "shot",
1268 mimeType: "image/jpeg",
1269 }),
1270 ).toEqual({ accent: "#d55665", symbol: "photo" });
1271 });
1272​
1273 it("leaves genuine documents and videos alone", async () => {
1274 const { contextChipPresentation } = await import("@t3tools/mobile-markdown-text/markdown");
1275 expect(
1276 contextChipPresentation("file", { kind: "file", name: "notes.txt", mimeType: "text/plain" }),
1277 ).toEqual({ accent: "#0090cd", symbol: "doc" });
1278 expect(
1279 contextChipPresentation("file", { kind: "file", name: "clip.mp4", mimeType: "video/mp4" }),
1280 ).toEqual({ accent: "#d06217", symbol: "play.rectangle" });
1281 });
1282});
1283​
1284describe("pull request chip status", () => {
1285 const chip = async (state: string, isDraft = false) => {
1286 const { contextChipPresentation } = await import("@t3tools/mobile-markdown-text/markdown");
1287 return contextChipPresentation("review-comment", {
1288 kind: "review-comment",
1289 sectionId: "pull-request:10978",
1290 pullRequest: { state, isDraft },
1291 });
1292 };
1293​
1294 it("colours a pull request by its state, the way web and the forge do", async () => {
1295 expect((await chip("open")).accent).toBe("#009f6e");
1296 expect((await chip("open", true)).accent).toBe("#7f8793");
1297 expect((await chip("merged")).accent).toBe("#8a70dd");
1298 expect((await chip("closed")).accent).toBe("#d55665");
1299 });
1300​
1301 it("keeps one glyph across every state, so only colour carries the status", async () => {
1302 // Web draws a fixed `git-pull-request` and encodes state in colour alone. A per-state glyph
1303 // here would put mobile out of step with it.
1304 const symbols = await Promise.all(
1305 [chip("open"), chip("open", true), chip("merged"), chip("closed")].map(
1306 async (pending) => (await pending).symbol,
1307 ),
1308 );
1309 expect(new Set(symbols)).toEqual(new Set(["git-pull-request"]));
1310 });
1311​
1312 it("falls back to the generic pull request chip when the state is unknown", async () => {
1313 const { contextChipPresentation } = await import("@t3tools/mobile-markdown-text/markdown");
1314 // An older server may send no metadata at all; the chip still has to render.
1315 expect(
1316 contextChipPresentation("review-comment", {
1317 kind: "review-comment",
1318 sectionId: "pull-request:1",
1319 }).accent,
1320 ).toBe("#7079e4");
1321 });
1322});