Open History to see the last commit
1import { ProjectReadFileError } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3import { describe, expect, it } from "vite-plus/test";
4
5import {
6 formatFileCommentRange,
7 normalizeFileCommentRange,
8 remapFileCommentAnnotations,
9} from "./fileCommentAnnotations";
10import {
11 filePreviewReadErrorMessage,
12 isMarkdownPreviewFile,
13 resolveFilePreviewPath,
14 setMarkdownTaskChecked,
15 shouldShowFileExplorer,
16} from "./filePreviewMode";
17
18const decodeReadError = Schema.decodeSync(ProjectReadFileError);
19
20describe("file preview read errors", () => {
21 it.each([
22 ["path_not_file", "The path is a directory or special file, not a regular file."],
23 ["binary_file", "The file is binary and cannot be displayed as text."],
24 ["workspace_path_outside_root", "The requested path is outside the workspace."],
25 ["resolved_path_outside_root", "The path resolves to a location outside the workspace."],
26 [
27 "operation_failed",
28 "The file could not be accessed or read. It may be missing or inaccessible.",
29 ],
30 ] as const)("describes %s without revealing the platform cause", (failure, message) => {
31 const error = new ProjectReadFileError({
32 cwd: "/workspace",
33 relativePath: "workspace/outline.md",
34 failure,
35 operation: "realpath-target",
36 resolvedPath: "/workspace/workspace/outline.md",
37 cause: new Error("EACCES: sensitive platform detail"),
38 });
39 expect(filePreviewReadErrorMessage(error)).toBe(message);
40 });
41
42 it("distinguishes an inaccessible workspace from an inaccessible file", () => {
43 const error = new ProjectReadFileError({
44 cwd: "/workspace",
45 relativePath: "outline.md",
46 failure: "operation_failed",
47 operation: "realpath-workspace-root",
48 operationPath: "/workspace",
49 });
50 expect(filePreviewReadErrorMessage(error)).toBe("The workspace folder could not be accessed.");
51 });
52
53 it("preserves the public message from older servers", () => {
54 const error = decodeReadError({
55 _tag: "ProjectReadFileError",
56 message: "Legacy file read failure.",
57 });
58 expect(filePreviewReadErrorMessage(error)).toBe("Legacy file read failure.");
59 });
60});
61
62describe("file comment annotations", () => {
63 it("normalizes and formats selected line ranges", () => {
64 expect(normalizeFileCommentRange({ start: 16, end: 7 })).toEqual({
65 startLine: 7,
66 endLine: 16,
67 });
68 expect(formatFileCommentRange(7, 7)).toBe("L7");
69 expect(formatFileCommentRange(7, 16)).toBe("L7 to L16");
70 });
71
72 it("keeps an annotation range attached when Pierre remaps its anchor line", () => {
73 expect(
74 remapFileCommentAnnotations([
75 {
76 lineNumber: 20,
77 metadata: {
78 entries: [
79 {
80 id: "comment-1",
81 kind: "comment",
82 startLine: 7,
83 endLine: 16,
84 text: "Keep this guarded.",
85 },
86 ],
87 },
88 },
89 ]),
90 ).toEqual([
91 {
92 lineNumber: 20,
93 metadata: {
94 entries: [
95 {
96 id: "comment-1",
97 kind: "comment",
98 startLine: 11,
99 endLine: 20,
100 text: "Keep this guarded.",
101 },
102 ],
103 },
104 },
105 ]);
106 });
107});
108
109describe("isMarkdownPreviewFile", () => {
110 it("recognizes markdown and MDX files case-insensitively", () => {
111 expect(isMarkdownPreviewFile("README.md")).toBe(true);
112 expect(isMarkdownPreviewFile("docs/guide.MDX")).toBe(true);
113 });
114
115 it("does not treat other text files as markdown", () => {
116 expect(isMarkdownPreviewFile("docs/guide.txt")).toBe(false);
117 expect(isMarkdownPreviewFile("docs/markdown.ts")).toBe(false);
118 });
119});
120
121describe("shouldShowFileExplorer", () => {
122 it("hides the workspace tree for host files and attachments", () => {
123 expect(
124 shouldShowFileExplorer({
125 relativePath: "/tmp/report.pdf",
126 explorerOpen: true,
127 attachmentOpen: false,
128 }),
129 ).toBe(false);
130 expect(
131 shouldShowFileExplorer({
132 relativePath: "report.pdf",
133 explorerOpen: true,
134 attachmentOpen: true,
135 }),
136 ).toBe(false);
137 });
138
139 it("keeps the saved explorer preference for workspace files", () => {
140 expect(
141 shouldShowFileExplorer({
142 relativePath: "docs/report.pdf",
143 explorerOpen: true,
144 attachmentOpen: false,
145 }),
146 ).toBe(true);
147 expect(
148 shouldShowFileExplorer({
149 relativePath: "docs/report.pdf",
150 explorerOpen: false,
151 attachmentOpen: false,
152 }),
153 ).toBe(false);
154 });
155});
156
157describe("setMarkdownTaskChecked", () => {
158 const markdown = "- [ ] First\n- [x] Second\n";
159
160 it("checks and unchecks the task marker at the supplied offset", () => {
161 expect(setMarkdownTaskChecked(markdown, 2, true)).toBe("- [x] First\n- [x] Second\n");
162 expect(setMarkdownTaskChecked(markdown, 14, false)).toBe("- [ ] First\n- [ ] Second\n");
163 expect(setMarkdownTaskChecked("1. [X] Ordered\n", 3, false)).toBe("1. [ ] Ordered\n");
164 });
165
166 it("leaves the document unchanged for a stale or invalid marker offset", () => {
167 expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
168 expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
169 });
170});
171
172describe("resolveFilePreviewPath", () => {
173 it.each([
174 ["/repo/project", null],
175 ["/repo/project/", null],
176 [".", null],
177 [null, null],
178 ["/repo/project/src", "/repo/project/src"],
179 ["/repo/project/src/main.ts", "/repo/project/src/main.ts"],
180 ["src/main.ts", "src/main.ts"],
181 ["/repo/project-other", "/repo/project-other"],
182 ])("opens %s in the appropriate workspace surface", (path, expected) => {
183 const relativePath = resolveFilePreviewPath(path, "/repo/project");
184 expect(relativePath).toBe(expected);
185 if (expected === null) {
186 expect(
187 shouldShowFileExplorer({ relativePath, explorerOpen: false, attachmentOpen: false }),
188 ).toBe(true);
189 }
190 });
191});