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

FilePreviewPanel.test.ts

120 lines3.7 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 { remapFileCommentAnnotations } from "./fileCommentAnnotations";
7import {
8 filePreviewReadErrorMessage,
9 resolveFilePreviewPath,
10 setMarkdownTaskChecked,
11 shouldShowFileExplorer,
12 workspaceAssetResource,
13} from "./filePreviewMode";
14​
15const decodeReadError = Schema.decodeSync(ProjectReadFileError);
16​
17describe("file preview read errors", () => {
18 // regression: #15628 read errors from servers without attempted paths keep their own message
19 it("preserves the public message from older servers", () => {
20 const error = decodeReadError({
21 _tag: "ProjectReadFileError",
22 message: "Legacy file read failure.",
23 });
24 expect(filePreviewReadErrorMessage(error)).toBe("Legacy file read failure.");
25 });
26});
27​
28describe("file comment annotations", () => {
29 it("keeps an annotation range attached when Pierre remaps its anchor line", () => {
30 expect(
31 remapFileCommentAnnotations([
32 {
33 lineNumber: 20,
34 metadata: {
35 entries: [
36 {
37 id: "comment-1",
38 kind: "comment",
39 startLine: 7,
40 endLine: 16,
41 text: "Keep this guarded.",
42 },
43 ],
44 },
45 },
46 ]),
47 ).toEqual([
48 {
49 lineNumber: 20,
50 metadata: {
51 entries: [
52 {
53 id: "comment-1",
54 kind: "comment",
55 startLine: 11,
56 endLine: 20,
57 text: "Keep this guarded.",
58 },
59 ],
60 },
61 },
62 ]);
63 });
64});
65​
66describe("setMarkdownTaskChecked", () => {
67 const markdown = "- [ ] First\n- [x] Second\n";
68​
69 it("checks and unchecks the task marker at the supplied offset", () => {
70 expect(setMarkdownTaskChecked(markdown, 2, true)).toBe("- [x] First\n- [x] Second\n");
71 expect(setMarkdownTaskChecked(markdown, 14, false)).toBe("- [ ] First\n- [ ] Second\n");
72 expect(setMarkdownTaskChecked("1. [X] Ordered\n", 3, false)).toBe("1. [ ] Ordered\n");
73 });
74​
75 it("leaves the document unchanged for a stale or invalid marker offset", () => {
76 expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
77 expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
78 });
79});
80​
81describe("resolveFilePreviewPath", () => {
82 it.each([
83 ["/repo/project", null],
84 ["/repo/project/", null],
85 [".", null],
86 [null, null],
87 ["/repo/project/src", "/repo/project/src"],
88 ["/repo/project/src/main.ts", "/repo/project/src/main.ts"],
89 ["src/main.ts", "src/main.ts"],
90 ["/repo/project-other", "/repo/project-other"],
91 ])("opens %s in the appropriate workspace surface", (path, expected) => {
92 const relativePath = resolveFilePreviewPath(path, "/repo/project");
93 expect(relativePath).toBe(expected);
94 if (expected === null) {
95 expect(
96 shouldShowFileExplorer({ relativePath, explorerOpen: false, attachmentOpen: false }),
97 ).toBe(true);
98 }
99 });
100});
101​
102describe("workspaceAssetResource", () => {
103 const input = {
104 kind: "workspace-file" as const,
105 threadRef: {
106 environmentId: "env" as ScopedThreadRef["environmentId"],
107 threadId: "thread-1" as ScopedThreadRef["threadId"],
108 },
109 workspaceRoot: "/repo",
110 absolutePath: "/repo/docs/index.html",
111 };
112​
113 it("names the workspace root for a draft, which the server cannot resolve from a thread", () => {
114 expect(workspaceAssetResource({ ...input, draft: true })).toEqual({
115 _tag: "draft-workspace-file",
116 cwd: "/repo",
117 path: "/repo/docs/index.html",
118 });
119 });
120});