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

nativeMarkdownText.test.ts

1,176 lines34.1 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.each(["$", "€", "Β£", "Β₯", "β‚Ή", "β‚©", "β‚Ώ", "𑿝"])(
311 "decorates %s skill references as selectable skill links",
312 (prefix) => {
313 const node: MarkdownNode = {
314 type: "document",
315 children: [
316 {
317 type: "paragraph",
318 children: [{ type: "text", content: `Use ${prefix}ui for this.` }],
319 },
320 ],
321 };
322​
323 expect(nativeMarkdownDocumentRuns(node, [{ name: "ui", displayName: "UI" }])).toEqual([
324 { text: "Use ", role: "body" },
325 {
326 text: `${prefix}ui`,
327 role: "body",
328 skillName: "ui",
329 skillLabel: "UI",
330 },
331 { text: " for this.", role: "body" },
332 ]);
333 },
334 );
335​
336 it("decorates known skill references that begin with a digit", () => {
337 const node: MarkdownNode = {
338 type: "document",
339 children: [
340 {
341 type: "paragraph",
342 children: [{ type: "text", content: "Use $2spec for this." }],
343 },
344 ],
345 };
346​
347 expect(nativeMarkdownDocumentRuns(node, [{ name: "2spec", displayName: "2Spec" }])).toEqual([
348 { text: "Use ", role: "body" },
349 {
350 text: "$2spec",
351 role: "body",
352 skillName: "2spec",
353 skillLabel: "2Spec",
354 },
355 { text: " for this.", role: "body" },
356 ]);
357 });
358​
359 it("decorates known skill references inside blockquotes", () => {
360 const node: MarkdownNode = {
361 type: "blockquote",
362 children: [
363 {
364 type: "paragraph",
365 children: [{ type: "text", content: "Use $ui for this." }],
366 },
367 ],
368 };
369​
370 expect(nativeMarkdownDocumentRuns(node, [{ name: "ui", displayName: "UI" }])).toContainEqual({
371 text: "$ui",
372 role: "body",
373 skillName: "ui",
374 skillLabel: "UI",
375 });
376 });
377​
378 it("leaves unknown skill-like text unchanged", () => {
379 const node: MarkdownNode = {
380 type: "document",
381 children: [
382 {
383 type: "paragraph",
384 children: [{ type: "text", content: "Use $unknown for this." }],
385 },
386 ],
387 };
388​
389 expect(nativeMarkdownDocumentRuns(node, [])).toEqual([
390 { text: "Use $unknown for this.", role: "body" },
391 ]);
392 });
393​
394 it("keeps headings, paragraphs, and lists in one continuous document", () => {
395 const node: MarkdownNode = {
396 type: "document",
397 children: [
398 {
399 type: "heading",
400 level: 1,
401 children: [{ type: "text", content: "Header One" }],
402 },
403 {
404 type: "paragraph",
405 children: [
406 { type: "text", content: "A paragraph with " },
407 { type: "bold", children: [{ type: "text", content: "bold text" }] },
408 { type: "text", content: "." },
409 ],
410 },
411 {
412 type: "list",
413 ordered: false,
414 children: [
415 {
416 type: "list_item",
417 children: [
418 {
419 type: "paragraph",
420 children: [{ type: "text", content: "First item" }],
421 },
422 ],
423 },
424 {
425 type: "list_item",
426 children: [
427 {
428 type: "paragraph",
429 children: [{ type: "text", content: "Second item" }],
430 },
431 ],
432 },
433 ],
434 },
435 ],
436 };
437​
438 const runs = nativeMarkdownDocumentRuns(node);
439 expect(runs.map((run) => run.text).join("")).toBe(
440 "Header One\n\nA paragraph with bold text.\n\nβ€’\tFirst item\nβ€’\tSecond item",
441 );
442 expect(runs).toContainEqual({
443 text: "Header One\n",
444 role: "heading",
445 headingLevel: 1,
446 });
447 expect(runs).toContainEqual({
448 text: "bold text",
449 bold: true,
450 role: "body",
451 });
452 expect(runs).toContainEqual({
453 text: "β€’\t",
454 role: "list-marker",
455 depth: 1,
456 firstLineHeadIndent: 0,
457 headIndent: 24,
458 paragraphSpacing: 2,
459 });
460 });
461​
462 it("uses distinct section, heading-content, and body spacing", () => {
463 const node: MarkdownNode = {
464 type: "document",
465 children: [
466 {
467 type: "paragraph",
468 children: [{ type: "text", content: "Intro" }],
469 },
470 {
471 type: "heading",
472 level: 2,
473 children: [{ type: "text", content: "Section" }],
474 },
475 {
476 type: "paragraph",
477 children: [{ type: "text", content: "First paragraph" }],
478 },
479 {
480 type: "paragraph",
481 children: [{ type: "text", content: "Second paragraph" }],
482 },
483 ],
484 };
485​
486 expect(
487 nativeMarkdownDocumentRuns(node)
488 .filter((run) => run.role === "spacer")
489 .map((run) => run.spacing),
490 ).toEqual([20, 10, 12]);
491 });
492​
493 it("renders tight list items whose inline nodes are direct children", () => {
494 const node: MarkdownNode = {
495 type: "document",
496 children: [
497 {
498 type: "list",
499 children: [
500 {
501 type: "list_item",
502 children: [
503 {
504 type: "bold",
505 children: [{ type: "text", content: "Finding:" }],
506 },
507 { type: "text", content: " details with " },
508 { type: "code_inline", content: "inline code" },
509 { type: "text", content: "." },
510 ],
511 },
512 ],
513 },
514 ],
515 };
516​
517 expect(nativeMarkdownDocumentRuns(node)).toEqual([
518 {
519 text: "β€’\t",
520 role: "list-marker",
521 depth: 1,
522 firstLineHeadIndent: 0,
523 headIndent: 24,
524 paragraphSpacing: 2,
525 },
526 { text: "Finding:", bold: true, role: "body", depth: 1 },
527 { text: " details with ", role: "body", depth: 1 },
528 { text: "inline code", code: true, role: "body", depth: 1 },
529 { text: ".", role: "body", depth: 1 },
530 ]);
531 });
532​
533 it("preserves quotes and fenced code in document runs", () => {
534 const node: MarkdownNode = {
535 type: "document",
536 children: [
537 {
538 type: "blockquote",
539 children: [
540 {
541 type: "paragraph",
542 children: [{ type: "text", content: "Read this" }],
543 },
544 ],
545 },
546 {
547 type: "code_block",
548 language: "ts",
549 content: "const answer = 42;",
550 },
551 ],
552 };
553​
554 const runs = nativeMarkdownDocumentRuns(node);
555 expect(runs.map((run) => run.text).join("")).toBe("β”‚\u00a0Read this\n\nTS\nconst answer = 42;");
556 expect(runs).toContainEqual({
557 text: "const answer = 42;",
558 code: true,
559 role: "code-block",
560 });
561 });
562​
563 it("reads fenced code content from child text nodes", () => {
564 const node: MarkdownNode = {
565 type: "document",
566 children: [
567 {
568 type: "code_block",
569 language: "bash",
570 children: [{ type: "text", content: "pnpm install\n" }],
571 },
572 ],
573 };
574​
575 expect(
576 nativeMarkdownDocumentRuns(node)
577 .map((run) => run.text)
578 .join(""),
579 ).toBe("BASH\npnpm install");
580 });
581​
582 it("keeps adjacent context links with the same href in separate runs", () => {
583 const href = "t3-context://v1/terminal/ctx-1";
584 const link = (content: string): MarkdownNode => ({
585 type: "link",
586 href,
587 children: [{ type: "text", content }],
588 });
589 const runs = nativeMarkdownDocumentRuns({
590 type: "document",
591 children: [{ type: "paragraph", children: [link("First"), link("Second")] }],
592 });
593​
594 // Merging these would render one chip and emit one copy range with a
595 // combined label for two distinct references.
596 expect(runs).toEqual([
597 { text: "First", role: "body", href, fileIcon: "bash" },
598 { text: "Second", role: "body", href, fileIcon: "bash" },
599 ]);
600 });
601});
602​
603describe("nativeMarkdownListItemBlocks", () => {
604 it("groups consecutive inline nodes into one paragraph block", () => {
605 const item: MarkdownNode = {
606 type: "list_item",
607 children: [
608 { type: "text", content: "Finding: " },
609 { type: "bold", children: [{ type: "text", content: "important" }] },
610 { type: "text", content: " details." },
611 {
612 type: "list",
613 children: [
614 {
615 type: "list_item",
616 children: [{ type: "text", content: "Nested" }],
617 },
618 ],
619 },
620 { type: "text", content: "Trailing prose." },
621 ],
622 };
623​
624 expect(nativeMarkdownListItemBlocks(item)).toEqual([
625 {
626 type: "paragraph",
627 children: item.children?.slice(0, 3),
628 },
629 item.children?.[3],
630 {
631 type: "paragraph",
632 children: [item.children?.[4]],
633 },
634 ]);
635 });
636});
637​
638describe("nativeMarkdownDocumentChunks", () => {
639 it("renders plain blockquotes as rich blocks so their marker spans wrapped lines", () => {
640 const blockquote: MarkdownNode = {
641 type: "blockquote",
642 beg: 0,
643 end: 120,
644 children: [
645 {
646 type: "paragraph",
647 children: [
648 {
649 type: "text",
650 content:
651 "Persistent random per-result keys are the strongest design, even when this text wraps.",
652 },
653 ],
654 },
655 ],
656 };
657​
658 expect(
659 nativeMarkdownDocumentChunks({
660 type: "document",
661 children: [blockquote],
662 }),
663 ).toEqual([
664 {
665 kind: "rich",
666 key: "rich:blockquote:offset:0",
667 node: blockquote,
668 },
669 ]);
670 });
671​
672 it("keeps headings and plain lists in one selectable document", () => {
673 const document: MarkdownNode = {
674 type: "document",
675 children: [
676 {
677 type: "heading",
678 level: 2,
679 children: [{ type: "text", content: "Tasks" }],
680 },
681 {
682 type: "list",
683 children: [
684 {
685 type: "task_list_item",
686 checked: true,
687 children: [
688 {
689 type: "paragraph",
690 children: [{ type: "text", content: "Completed" }],
691 },
692 ],
693 },
694 {
695 type: "list_item",
696 children: [
697 {
698 type: "paragraph",
699 children: [{ type: "text", content: "Parent" }],
700 },
701 {
702 type: "list",
703 children: [
704 {
705 type: "list_item",
706 children: [
707 {
708 type: "paragraph",
709 children: [{ type: "text", content: "Nested" }],
710 },
711 ],
712 },
713 ],
714 },
715 ],
716 },
717 ],
718 },
719 ],
720 };
721​
722 const chunks = nativeMarkdownDocumentChunks(document);
723 expect(chunks).toHaveLength(1);
724 expect(chunks[0]).toMatchObject({ kind: "selectable" });
725 expect(
726 nativeMarkdownDocumentRuns(chunks[0]?.node ?? document)
727 .map((run) => run.text)
728 .join(""),
729 ).toBe("Tasks\n\nβ˜‘οΈŽ\tCompleted\nβ€’\tParent\nβ—¦\tNested");
730 });
731​
732 it("aligns ordered markers while keeping the list in one selectable string", () => {
733 const document: MarkdownNode = {
734 type: "document",
735 children: [
736 {
737 type: "list",
738 ordered: true,
739 start: 9,
740 children: [
741 {
742 type: "list_item",
743 children: [{ type: "text", content: "Ninth" }],
744 },
745 {
746 type: "list_item",
747 children: [{ type: "text", content: "Tenth" }],
748 },
749 ],
750 },
751 ],
752 };
753​
754 expect(
755 nativeMarkdownDocumentRuns(document)
756 .map((run) => run.text)
757 .join(""),
758 ).toBe("\u20079.\tNinth\n10.\tTenth");
759 });
760​
761 it("keeps prose selectable while exposing rich AST blocks", () => {
762 const document: MarkdownNode = {
763 type: "document",
764 children: [
765 {
766 type: "heading",
767 level: 1,
768 beg: 0,
769 end: 9,
770 children: [{ type: "text", content: "Install" }],
771 },
772 {
773 type: "code_block",
774 language: "bash",
775 beg: 11,
776 end: 35,
777 children: [{ type: "text", content: "pnpm install\n" }],
778 },
779 {
780 type: "paragraph",
781 beg: 37,
782 end: 42,
783 children: [{ type: "text", content: "Done." }],
784 },
785 ],
786 };
787​
788 const chunks = nativeMarkdownDocumentChunks(document);
789 expect(chunks).toHaveLength(3);
790 expect(chunks[0]).toMatchObject({ kind: "selectable" });
791 expect(chunks[1]).toEqual({
792 kind: "rich",
793 key: "rich:code_block:offset:11",
794 node: document.children?.[1],
795 });
796 expect(chunks[2]).toMatchObject({ kind: "selectable" });
797 });
798​
799 it("keeps a list containing fenced code as one rich AST container", () => {
800 const document: MarkdownNode = {
801 type: "document",
802 children: [
803 {
804 type: "list",
805 beg: 0,
806 end: 45,
807 children: [
808 {
809 type: "list_item",
810 children: [
811 {
812 type: "paragraph",
813 children: [{ type: "text", content: "Install" }],
814 },
815 {
816 type: "code_block",
817 language: "bash",
818 children: [{ type: "text", content: "pnpm install\n" }],
819 },
820 ],
821 },
822 ],
823 },
824 ],
825 };
826​
827 expect(nativeMarkdownDocumentChunks(document)).toEqual([
828 {
829 kind: "rich",
830 key: "rich:list:offset:0",
831 node: document.children?.[0],
832 },
833 ]);
834 });
835​
836 it("keeps surrounding prose selectable when rich nodes have no source offsets", () => {
837 const document: MarkdownNode = {
838 type: "document",
839 children: [
840 {
841 type: "heading",
842 level: 1,
843 children: [{ type: "text", content: "Before" }],
844 },
845 { type: "horizontal_rule" },
846 {
847 type: "paragraph",
848 children: [{ type: "text", content: "After." }],
849 },
850 ],
851 };
852​
853 const chunks = nativeMarkdownDocumentChunks(document);
854 expect(chunks).toHaveLength(3);
855 expect(chunks[0]).toMatchObject({ kind: "selectable" });
856 expect(chunks[1]).toEqual({
857 kind: "rich",
858 key: "rich:horizontal_rule:index:1",
859 node: document.children?.[1],
860 });
861 expect(chunks[2]).toMatchObject({ kind: "selectable" });
862 });
863​
864 it("keeps offset-free structural lists isolated without promoting the whole document", () => {
865 const document: MarkdownNode = {
866 type: "document",
867 children: [
868 {
869 type: "paragraph",
870 children: [{ type: "text", content: "Before." }],
871 },
872 {
873 type: "list",
874 ordered: true,
875 children: [
876 {
877 type: "list_item",
878 children: [
879 {
880 type: "paragraph",
881 children: [{ type: "text", content: "Install" }],
882 },
883 {
884 type: "code_block",
885 language: "bash",
886 children: [{ type: "text", content: "pnpm install\n" }],
887 },
888 ],
889 },
890 ],
891 },
892 {
893 type: "paragraph",
894 children: [{ type: "text", content: "After." }],
895 },
896 ],
897 };
898​
899 const chunks = nativeMarkdownDocumentChunks(document);
900 expect(chunks).toHaveLength(3);
901 expect(chunks[0]).toMatchObject({ kind: "selectable" });
902 expect(chunks[1]).toEqual({
903 kind: "rich",
904 key: "rich:list:index:1",
905 node: document.children?.[1],
906 });
907 expect(chunks[2]).toMatchObject({ kind: "selectable" });
908 });
909​
910 it("never collapses a rich subtree into a second markdown parsing pass", () => {
911 const document: MarkdownNode = {
912 type: "document",
913 children: [
914 {
915 type: "paragraph",
916 children: [{ type: "text", content: "Before." }],
917 },
918 {
919 type: "blockquote",
920 children: [
921 {
922 type: "list",
923 children: [
924 {
925 type: "list_item",
926 children: [
927 { type: "text", content: "Run this" },
928 {
929 type: "code_block",
930 language: "sh",
931 children: [{ type: "text", content: "vp check\n" }],
932 },
933 ],
934 },
935 ],
936 },
937 ],
938 },
939 {
940 type: "paragraph",
941 children: [{ type: "text", content: "After." }],
942 },
943 ],
944 };
945​
946 const chunks = nativeMarkdownDocumentChunks(document);
947 expect(chunks.map((chunk) => chunk.kind)).toEqual(["selectable", "rich", "selectable"]);
948 expect(chunks[1]).toMatchObject({
949 kind: "rich",
950 node: { type: "blockquote" },
951 });
952 });
953​
954 it("keys positioned and offset-free siblings in separate namespaces", () => {
955 const positioned: MarkdownNode = {
956 type: "blockquote",
957 beg: 1,
958 end: 10,
959 children: [{ type: "paragraph", children: [{ type: "text", content: "Positioned" }] }],
960 };
961 const offsetFree: MarkdownNode = {
962 type: "blockquote",
963 children: [{ type: "paragraph", children: [{ type: "text", content: "Generated" }] }],
964 };
965 const chunks = nativeMarkdownDocumentChunks({
966 type: "document",
967 children: [
968 { type: "paragraph", beg: 0, end: 0, children: [] },
969 offsetFree,
970 positioned,
971 { type: "paragraph", children: [{ type: "text", content: "Tail" }] },
972 ],
973 });
974​
975 expect(chunks.map((chunk) => chunk.key)).toEqual([
976 "selectable:offset:0",
977 "rich:blockquote:index:1",
978 "rich:blockquote:offset:1",
979 "selectable:index:3",
980 ]);
981 });
982​
983 it("gives every offset-free selectable group its own key", () => {
984 const chunks = nativeMarkdownDocumentChunks({
985 type: "document",
986 children: [
987 { type: "paragraph", children: [{ type: "text", content: "One" }] },
988 { type: "horizontal_rule" },
989 { type: "paragraph", children: [{ type: "text", content: "Two" }] },
990 { type: "horizontal_rule" },
991 { type: "paragraph", children: [{ type: "text", content: "Three" }] },
992 ],
993 });
994​
995 expect(chunks.map((chunk) => chunk.key)).toEqual([
996 "selectable:index:0",
997 "rich:horizontal_rule:index:1",
998 "selectable:index:2",
999 "rich:horizontal_rule:index:3",
1000 "selectable:index:4",
1001 ]);
1002 });
1003​
1004 it("keeps positioned chunk keys stable while text streams in", () => {
1005 const before: MarkdownNode = {
1006 type: "document",
1007 children: [
1008 { type: "paragraph", beg: 0, end: 5, children: [{ type: "text", content: "Intro" }] },
1010 type: "code_block",
1011 language: "ts",
1012 beg: 7,
1013 end: 20,
1014 children: [{ type: "text", content: "const a" }],
1015 },
1016 ],
1017 };
1018 const after: MarkdownNode = {
1019 type: "document",
1020 children: [
1021 { type: "paragraph", beg: 0, end: 5, children: [{ type: "text", content: "Intro" }] },
1023 type: "code_block",
1024 language: "ts",
1025 beg: 7,
1026 end: 40,
1027 children: [{ type: "text", content: "const a = 1;\nconst b" }],
1028 },
1029 ],
1030 };
1031​
1032 expect(nativeMarkdownDocumentChunks(after).map((chunk) => chunk.key)).toEqual(
1033 nativeMarkdownDocumentChunks(before).map((chunk) => chunk.key),
1034 );
1035 });
1036​
1037 it("keeps a plain list in one selectable native text container", () => {
1038 const list: MarkdownNode = {
1039 type: "list",
1040 ordered: false,
1041 children: [
1043 type: "list_item",
1044 children: [{ type: "text", content: "First" }],
1045 },
1046 ],
1047 };
1048​
1049 const chunks = nativeMarkdownDocumentChunks({
1050 type: "document",
1051 children: [list],
1052 });
1053​
1054 expect(chunks).toHaveLength(1);
1055 expect(chunks[0]).toMatchObject({
1056 kind: "selectable",
1057 node: { type: "document", children: [list] },
1058 });
1059 });
1060​
1061 it("separates sections more than related rich blocks", () => {
1062 const headingChunk = {
1063 kind: "selectable" as const,
1064 key: "heading",
1065 node: {
1066 type: "document",
1067 children: [
1069 type: "heading",
1070 level: 2,
1071 children: [{ type: "text", content: "Section" }],
1072 },
1073 ],
1074 } satisfies MarkdownNode,
1075 };
1076 const firstList = {
1077 kind: "rich" as const,
1078 key: "list-1",
1079 node: { type: "list", children: [] } satisfies MarkdownNode,
1080 };
1081 const secondList = {
1082 kind: "rich" as const,
1083 key: "list-2",
1084 node: { type: "list", children: [] } satisfies MarkdownNode,
1085 };
1086​
1087 expect(nativeMarkdownChunkSpacing(undefined, headingChunk)).toBe(0);
1088 expect(nativeMarkdownChunkSpacing(headingChunk, firstList)).toBe(10);
1089 expect(nativeMarkdownChunkSpacing(firstList, secondList)).toBe(12);
1090 expect(nativeMarkdownChunkSpacing(firstList, headingChunk)).toBe(20);
1091 });
1092});
1093​
1094describe("composerChipSizeSuffix", () => {
1095 it("labels attachment records with a human size, matching web's chip", async () => {
1096 const { composerChipSizeSuffix } = await import("@t3tools/mobile-markdown-text/markdown");
1097 expect(composerChipSizeSuffix({ kind: "file", sizeBytes: 1024 })).toBe("1 KB");
1098 expect(composerChipSizeSuffix({ kind: "file", sizeBytes: 3_700_000 })).toBe("3.5 MB");
1099 expect(composerChipSizeSuffix({ kind: "image", sizeBytes: 2048 })).toBe("2 KB");
1100 });
1101​
1102 it("adds nothing for records that carry no bytes", async () => {
1103 const { composerChipSizeSuffix } = await import("@t3tools/mobile-markdown-text/markdown");
1104 // Terminal/review/PR chips have no size to show.
1105 expect(composerChipSizeSuffix({ kind: "terminal" })).toBe("");
1106 expect(composerChipSizeSuffix({ kind: "file" })).toBe("");
1107 expect(composerChipSizeSuffix(undefined)).toBe("");
1108 });
1109});
1110​
1111describe("contextChipPresentation image detection", () => {
1112 it("treats a picture attached through the file picker as an image", async () => {
1113 const { contextChipPresentation } = await import("@t3tools/mobile-markdown-text/markdown");
1114 // The document picker types every pick as `file`, so the name has to carry the intent.
1115 expect(
1116 contextChipPresentation("file", { kind: "file", name: "IMG_4997.PNG", mimeType: "" }),
1117 ).toEqual({ accent: "#d55665", symbol: "photo" });
1118 expect(
1119 contextChipPresentation("file", {
1120 kind: "file",
1121 name: "shot",
1122 mimeType: "image/jpeg",
1123 }),
1124 ).toEqual({ accent: "#d55665", symbol: "photo" });
1125 });
1126​
1127 it("leaves genuine documents and videos alone", async () => {
1128 const { contextChipPresentation } = await import("@t3tools/mobile-markdown-text/markdown");
1129 expect(
1130 contextChipPresentation("file", { kind: "file", name: "notes.txt", mimeType: "text/plain" }),
1131 ).toEqual({ accent: "#0090cd", symbol: "doc" });
1132 expect(
1133 contextChipPresentation("file", { kind: "file", name: "clip.mp4", mimeType: "video/mp4" }),
1134 ).toEqual({ accent: "#d06217", symbol: "play.rectangle" });
1135 });
1136});
1137​
1138describe("pull request chip status", () => {
1139 const chip = async (state: string, isDraft = false) => {
1140 const { contextChipPresentation } = await import("@t3tools/mobile-markdown-text/markdown");
1141 return contextChipPresentation("review-comment", {
1142 kind: "review-comment",
1143 sectionId: "pull-request:10978",
1144 pullRequest: { state, isDraft },
1145 });
1146 };
1147​
1148 it("colours a pull request by its state, the way web and the forge do", async () => {
1149 expect((await chip("open")).accent).toBe("#009f6e");
1150 expect((await chip("open", true)).accent).toBe("#7f8793");
1151 expect((await chip("merged")).accent).toBe("#8a70dd");
1152 expect((await chip("closed")).accent).toBe("#d55665");
1153 });
1154​
1155 it("keeps one glyph across every state, so only colour carries the status", async () => {
1156 // Web draws a fixed `git-pull-request` and encodes state in colour alone. A per-state glyph
1157 // here would put mobile out of step with it.
1158 const symbols = await Promise.all(
1159 [chip("open"), chip("open", true), chip("merged"), chip("closed")].map(
1160 async (pending) => (await pending).symbol,
1161 ),
1162 );
1163 expect(new Set(symbols)).toEqual(new Set(["git-pull-request"]));
1164 });
1165​
1166 it("falls back to the generic pull request chip when the state is unknown", async () => {
1167 const { contextChipPresentation } = await import("@t3tools/mobile-markdown-text/markdown");
1168 // An older server may send no metadata at all; the chip still has to render.
1169 expect(
1170 contextChipPresentation("review-comment", {
1171 kind: "review-comment",
1172 sectionId: "pull-request:1",
1173 }).accent,
1174 ).toBe("#7079e4");
1175 });
1176});