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