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