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

nativeMarkdownText.test.ts

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