Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 formatFileCommentRange,
5 normalizeFileCommentRange,
6 remapFileCommentAnnotations,
7} from "./fileCommentAnnotations";
8import { isMarkdownPreviewFile, setMarkdownTaskChecked } from "./filePreviewMode";
9
10describe("file comment annotations", () => {
11 it("normalizes and formats selected line ranges", () => {
12 expect(normalizeFileCommentRange({ start: 16, end: 7 })).toEqual({
13 startLine: 7,
14 endLine: 16,
15 });
16 expect(formatFileCommentRange(7, 7)).toBe("L7");
17 expect(formatFileCommentRange(7, 16)).toBe("L7 to L16");
18 });
19
20 it("keeps an annotation range attached when Pierre remaps its anchor line", () => {
21 expect(
22 remapFileCommentAnnotations([
23 {
24 lineNumber: 20,
25 metadata: {
26 entries: [
27 {
28 id: "comment-1",
29 kind: "comment",
30 startLine: 7,
31 endLine: 16,
32 text: "Keep this guarded.",
33 },
34 ],
35 },
36 },
37 ]),
38 ).toEqual([
39 {
40 lineNumber: 20,
41 metadata: {
42 entries: [
43 {
44 id: "comment-1",
45 kind: "comment",
46 startLine: 11,
47 endLine: 20,
48 text: "Keep this guarded.",
49 },
50 ],
51 },
52 },
53 ]);
54 });
55});
56
57describe("isMarkdownPreviewFile", () => {
58 it("recognizes markdown and MDX files case-insensitively", () => {
59 expect(isMarkdownPreviewFile("README.md")).toBe(true);
60 expect(isMarkdownPreviewFile("docs/guide.MDX")).toBe(true);
61 });
62
63 it("does not treat other text files as markdown", () => {
64 expect(isMarkdownPreviewFile("docs/guide.txt")).toBe(false);
65 expect(isMarkdownPreviewFile("docs/markdown.ts")).toBe(false);
66 });
67});
68
69describe("setMarkdownTaskChecked", () => {
70 const markdown = "- [ ] First\n- [x] Second\n";
71
72 it("checks and unchecks the task marker at the supplied offset", () => {
73 expect(setMarkdownTaskChecked(markdown, 2, true)).toBe("- [x] First\n- [x] Second\n");
74 expect(setMarkdownTaskChecked(markdown, 14, false)).toBe("- [ ] First\n- [ ] Second\n");
75 expect(setMarkdownTaskChecked("1. [X] Ordered\n", 3, false)).toBe("1. [ ] Ordered\n");
76 });
77
78 it("leaves the document unchanged for a stale or invalid marker offset", () => {
79 expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
80 expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
81 });
82});