Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { markdownTaskMarkerOffset } from "./markdownTaskList.ts";
4import { setMarkdownTaskChecked } from "./markdownTaskEdits.ts";
5
6describe("setMarkdownTaskChecked", () => {
7 const markdown = "- [ ] First\n- [x] Second\n";
8
9 it("checks and unchecks the task marker at the supplied offset", () => {
10 expect(setMarkdownTaskChecked(markdown, 2, true)).toBe("- [x] First\n- [x] Second\n");
11 expect(setMarkdownTaskChecked(markdown, 14, false)).toBe("- [ ] First\n- [ ] Second\n");
12 expect(setMarkdownTaskChecked("1. [X] Ordered\n", 3, false)).toBe("1. [ ] Ordered\n");
13 });
14
15 it("leaves the document unchanged for a stale or invalid marker offset", () => {
16 expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
17 expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
18 });
19});
20
21describe("markdownTaskMarkerOffset", () => {
22 const markdown = [
23 "## Test plan",
24 "- [ ] `vp lint` passes",
25 " - [x] Nested **step**",
26 "```md",
27 "- [ ] Inside a fence",
28 "```",
29 "> 1. [X] Quoted [link](https://example.com)",
30 "- [ ] Wrapped",
31 " onto two lines",
32 ].join("\n");
33 const marker = (offset: number | null) =>
34 offset === null ? null : markdown.slice(offset, offset + 3);
35
36 it("finds the tapped checkbox, skipping look-alikes in code", () => {
37 expect(marker(markdownTaskMarkerOffset(markdown, 0, "vp lint passes", true))).toBe("[ ]");
38 expect(marker(markdownTaskMarkerOffset(markdown, 1, "Nested step", false))).toBe("[x]");
39 // Native numbering restarts after code blocks and blockquotes.
40 expect(marker(markdownTaskMarkerOffset(markdown, 0, "Quoted link", false))).toBe("[X]");
41 // Android cuts the text at a line break, a soft one too with hard line breaks; iOS doesn't.
42 for (const text of ["Wrapped", "Wrapped onto two lines"]) {
43 expect(markdownTaskMarkerOffset(markdown, 0, text, true)).toBe(markdown.lastIndexOf("[ ]"));
44 }
45 });
46
47 it("tells identical labels apart by count within one list and by state", () => {
48 const same = "- [ ] Same\n- [ ] Same\n\n```\nx\n```\n\n- [x] Same\n";
49 expect(markdownTaskMarkerOffset(same, 0, "Same", true)).toBe(2);
50 expect(markdownTaskMarkerOffset(same, 1, "Same", true)).toBe(13);
51 expect(markdownTaskMarkerOffset(same, 0, "Same", false)).toBe(same.lastIndexOf("[x]"));
52 });
53
54 it("refuses a checkbox it can't trace to exactly one task", () => {
55 expect(markdownTaskMarkerOffset(markdown, 1, "vp lint passes", true)).toBeNull();
56 expect(markdownTaskMarkerOffset(markdown, 0, "vp lint passes", false)).toBeNull();
57 expect(markdownTaskMarkerOffset(markdown, 0, "Wrapped onto", true)).toBeNull();
58 expect(markdownTaskMarkerOffset(markdown, 4, "", true)).toBeNull();
59
60 const similar = "- [ ] Similar\n\n```text\nseparator\n```\n\n- [ ] Similar<br>different\n";
61 // iOS keeps hard breaks in the text, so it names the second task.
62 expect(markdownTaskMarkerOffset(similar, 0, "Similar
different", true)).toBe(
63 similar.lastIndexOf("[ ]"),
64 );
65 // Android cuts it to "Similar", which both tasks could be.
66 expect(markdownTaskMarkerOffset(similar, 0, "Similar", true)).toBeNull();
67
68 for (const label of ["Similar [docs](https://github.com/pingdotgg/t3code)", "Similar $x$"]) {
69 const attachment = similar.replace("Similar<br>different", label);
70 expect(markdownTaskMarkerOffset(attachment, 0, "Similar \uFFFC", true)).toBeNull();
71 }
72 });
73});