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

FilePreviewPanel.test.ts

144 lines4.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 formatFileCommentRange,
5 normalizeFileCommentRange,
6 remapFileCommentAnnotations,
7} from "./fileCommentAnnotations";
8import {
9 isMarkdownPreviewFile,
10 resolveFilePreviewPath,
11 setMarkdownTaskChecked,
12 shouldShowFileExplorer,
13} from "./filePreviewMode";
14​
15describe("file comment annotations", () => {
16 it("normalizes and formats selected line ranges", () => {
17 expect(normalizeFileCommentRange({ start: 16, end: 7 })).toEqual({
18 startLine: 7,
19 endLine: 16,
20 });
21 expect(formatFileCommentRange(7, 7)).toBe("L7");
22 expect(formatFileCommentRange(7, 16)).toBe("L7 to L16");
23 });
24​
25 it("keeps an annotation range attached when Pierre remaps its anchor line", () => {
26 expect(
27 remapFileCommentAnnotations([
28 {
29 lineNumber: 20,
30 metadata: {
31 entries: [
32 {
33 id: "comment-1",
34 kind: "comment",
35 startLine: 7,
36 endLine: 16,
37 text: "Keep this guarded.",
38 },
39 ],
40 },
41 },
42 ]),
43 ).toEqual([
44 {
45 lineNumber: 20,
46 metadata: {
47 entries: [
48 {
49 id: "comment-1",
50 kind: "comment",
51 startLine: 11,
52 endLine: 20,
53 text: "Keep this guarded.",
54 },
55 ],
56 },
57 },
58 ]);
59 });
60});
61​
62describe("isMarkdownPreviewFile", () => {
63 it("recognizes markdown and MDX files case-insensitively", () => {
64 expect(isMarkdownPreviewFile("README.md")).toBe(true);
65 expect(isMarkdownPreviewFile("docs/guide.MDX")).toBe(true);
66 });
67​
68 it("does not treat other text files as markdown", () => {
69 expect(isMarkdownPreviewFile("docs/guide.txt")).toBe(false);
70 expect(isMarkdownPreviewFile("docs/markdown.ts")).toBe(false);
71 });
72});
73​
74describe("shouldShowFileExplorer", () => {
75 it("hides the workspace tree for host files and attachments", () => {
76 expect(
77 shouldShowFileExplorer({
78 relativePath: "/tmp/report.pdf",
79 explorerOpen: true,
80 attachmentOpen: false,
81 }),
82 ).toBe(false);
83 expect(
84 shouldShowFileExplorer({
85 relativePath: "report.pdf",
86 explorerOpen: true,
87 attachmentOpen: true,
88 }),
89 ).toBe(false);
90 });
91​
92 it("keeps the saved explorer preference for workspace files", () => {
93 expect(
94 shouldShowFileExplorer({
95 relativePath: "docs/report.pdf",
96 explorerOpen: true,
97 attachmentOpen: false,
98 }),
99 ).toBe(true);
100 expect(
101 shouldShowFileExplorer({
102 relativePath: "docs/report.pdf",
103 explorerOpen: false,
104 attachmentOpen: false,
105 }),
106 ).toBe(false);
107 });
108});
109​
110describe("setMarkdownTaskChecked", () => {
111 const markdown = "- [ ] First\n- [x] Second\n";
112​
113 it("checks and unchecks the task marker at the supplied offset", () => {
114 expect(setMarkdownTaskChecked(markdown, 2, true)).toBe("- [x] First\n- [x] Second\n");
115 expect(setMarkdownTaskChecked(markdown, 14, false)).toBe("- [ ] First\n- [ ] Second\n");
116 expect(setMarkdownTaskChecked("1. [X] Ordered\n", 3, false)).toBe("1. [ ] Ordered\n");
117 });
118​
119 it("leaves the document unchanged for a stale or invalid marker offset", () => {
120 expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
121 expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
122 });
123});
124​
125describe("resolveFilePreviewPath", () => {
126 it.each([
127 ["/repo/project", null],
128 ["/repo/project/", null],
129 [".", null],
130 [null, null],
131 ["/repo/project/src", "/repo/project/src"],
132 ["/repo/project/src/main.ts", "/repo/project/src/main.ts"],
133 ["src/main.ts", "src/main.ts"],
134 ["/repo/project-other", "/repo/project-other"],
135 ])("opens %s in the appropriate workspace surface", (path, expected) => {
136 const relativePath = resolveFilePreviewPath(path, "/repo/project");
137 expect(relativePath).toBe(expected);
138 if (expected === null) {
139 expect(
140 shouldShowFileExplorer({ relativePath, explorerOpen: false, attachmentOpen: false }),
141 ).toBe(true);
142 }
143 });
144});