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

FilePreviewPanel.test.ts

221 lines6.8 KB

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: "report.pdf",
135 explorerOpen: true,
136 attachmentOpen: true,
137 }),
138 ).toBe(false);
139 });
140​
141 it("keeps the saved explorer preference for workspace files", () => {
142 expect(
143 shouldShowFileExplorer({
144 relativePath: "docs/report.pdf",
145 explorerOpen: true,
146 attachmentOpen: false,
147 }),
148 ).toBe(true);
149 expect(
150 shouldShowFileExplorer({
151 relativePath: "docs/report.pdf",
152 explorerOpen: false,
153 attachmentOpen: false,
154 }),
155 ).toBe(false);
156 });
157});
158​
159describe("setMarkdownTaskChecked", () => {
160 const markdown = "- [ ] First\n- [x] Second\n";
161​
162 it("checks and unchecks the task marker at the supplied offset", () => {
163 expect(setMarkdownTaskChecked(markdown, 2, true)).toBe("- [x] First\n- [x] Second\n");
164 expect(setMarkdownTaskChecked(markdown, 14, false)).toBe("- [ ] First\n- [ ] Second\n");
165 expect(setMarkdownTaskChecked("1. [X] Ordered\n", 3, false)).toBe("1. [ ] Ordered\n");
166 });
167​
168 it("leaves the document unchanged for a stale or invalid marker offset", () => {
169 expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
170 expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
171 });
172});
173​
174describe("resolveFilePreviewPath", () => {
175 it.each([
176 ["/repo/project", null],
177 ["/repo/project/", null],
178 [".", null],
179 [null, null],
180 ["/repo/project/src", "/repo/project/src"],
181 ["/repo/project/src/main.ts", "/repo/project/src/main.ts"],
182 ["src/main.ts", "src/main.ts"],
183 ["/repo/project-other", "/repo/project-other"],
184 ])("opens %s in the appropriate workspace surface", (path, expected) => {
185 const relativePath = resolveFilePreviewPath(path, "/repo/project");
186 expect(relativePath).toBe(expected);
187 if (expected === null) {
188 expect(
189 shouldShowFileExplorer({ relativePath, explorerOpen: false, attachmentOpen: false }),
190 ).toBe(true);
191 }
192 });
193});
194​
195describe("workspaceAssetResource", () => {
196 const input = {
197 kind: "workspace-file" as const,
198 threadRef: {
199 environmentId: "env" as ScopedThreadRef["environmentId"],
200 threadId: "thread-1" as ScopedThreadRef["threadId"],
201 },
202 workspaceRoot: "/repo",
203 absolutePath: "/repo/docs/index.html",
204 };
205​
206 it("names the thread once the server knows it", () => {
207 expect(workspaceAssetResource({ ...input, draft: false })).toEqual({
208 _tag: "workspace-file",
209 threadId: "thread-1",
210 path: "/repo/docs/index.html",
211 });
212 });
213​
214 it("names the workspace root for a draft, which the server cannot resolve from a thread", () => {
215 expect(workspaceAssetResource({ ...input, draft: true })).toEqual({
216 _tag: "draft-workspace-file",
217 cwd: "/repo",
218 path: "/repo/docs/index.html",
219 });
220 });
221});