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

nativeMarkdownText.test.ts

1,337 lines39.7 KB

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