Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5import { openDiffFilePrimaryAction, resolveDiffPathForWorkspace } from "./diffFileActions";
6import { selectThreadRightPanelState, useRightPanelStore } from "./rightPanelStore";
7
8const THREAD_REF = scopeThreadRef(
9 EnvironmentId.make("environment-local"),
10 ThreadId.make("thread-1"),
11);
12
13describe("openDiffFilePrimaryAction", () => {
14 beforeEach(() => {
15 useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} });
16 });
17
18 it("keeps a literal tilde folder inside the repository when opening in the editor", () => {
19 const openInEditor = vi.fn();
20
21 openDiffFilePrimaryAction({
22 threadRef: null,
23 filePath: "~/notes.txt",
24 activeCwd: "/home/alex/repo",
25 openInEditor,
26 });
27
28 expect(openInEditor).toHaveBeenCalledExactlyOnceWith("/home/alex/repo/~/notes.txt");
29 });
30
31 it("opens repository-relative diff files from a nested project", () => {
32 const openInEditor = vi.fn();
33
34 openDiffFilePrimaryAction({
35 threadRef: THREAD_REF,
36 filePath: "frontend/Dockerfile",
37 activeCwd: "/repo/frontend",
38 repositoryRoot: "/repo",
39 openInEditor,
40 });
41
42 expect(
43 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
44 ).toMatchObject({
45 isOpen: true,
46 activeSurfaceId: "file:Dockerfile",
47 });
48 expect(openInEditor).not.toHaveBeenCalled();
49 });
50
51 it("preserves repository-relative paths in a separate worktree", () => {
52 expect(
53 resolveDiffPathForWorkspace({
54 filePath: "frontend/Dockerfile",
55 workspaceRoot: "/worktrees/feature",
56 repositoryRoot: "/repo",
57 }),
58 ).toBe("frontend/Dockerfile");
59 });
60
61 it.each([
62 {
63 filePath: "Frontend/src\\index.ts",
64 workspaceRoot: "C:\\repo\\frontend",
65 repositoryRoot: "C:\\repo",
66 expected: "src/index.ts",
67 },
68 {
69 filePath: "Frontend\\src\\index.ts",
70 workspaceRoot: "C:\\repo\\frontend",
71 repositoryRoot: "C:\\repo",
72 expected: "src/index.ts",
73 },
74 {
75 filePath: "frontend/back\\slash.txt",
76 workspaceRoot: "/repo/frontend",
77 repositoryRoot: "/repo",
78 expected: "back\\slash.txt",
79 },
80 {
81 filePath: "back/slash/a.ts",
82 workspaceRoot: "/repo/back\\slash",
83 repositoryRoot: "/repo",
84 expected: null,
85 },
86 {
87 filePath: "sub/file.txt",
88 workspaceRoot: "/tmp/repo /sub",
89 repositoryRoot: "/tmp/repo ",
90 expected: "file.txt",
91 },
92 ])(
93 "resolves $filePath in $workspaceRoot",
94 ({ filePath, workspaceRoot, repositoryRoot, expected }) => {
95 expect(resolveDiffPathForWorkspace({ filePath, workspaceRoot, repositoryRoot })).toBe(
96 expected,
97 );
98 },
99 );
100
101 // regression: #6174 opening diff files broke for repositories at a filesystem root
102 it.each([
103 { workspaceRoot: "/frontend", repositoryRoot: "/" },
104 { workspaceRoot: "C:\\frontend", repositoryRoot: "C:\\" },
105 ])("handles filesystem roots: $repositoryRoot", ({ workspaceRoot, repositoryRoot }) => {
106 expect(
107 resolveDiffPathForWorkspace({
108 filePath: "frontend/index.ts",
109 workspaceRoot,
110 repositoryRoot,
111 }),
112 ).toBe("index.ts");
113 });
114
115 it.each([
116 {
117 filePath: "/repo/frontend/src/index.ts",
118 workspaceRoot: "/repo/frontend",
119 expected: "src/index.ts",
120 },
121 { filePath: "c:\\Repo\\src\\index.ts", workspaceRoot: "C:\\repo", expected: "src/index.ts" },
122 ])(
123 "opens absolute diff paths inside the workspace: $filePath",
124 ({ filePath, workspaceRoot, expected }) => {
125 expect(
126 resolveDiffPathForWorkspace({ filePath, workspaceRoot, repositoryRoot: workspaceRoot }),
127 ).toBe(expected);
128 },
129 );
130
131 it.each([
132 "backend/server.ts",
133 "frontend2/app.ts",
134 "frontend/../secret.ts",
135 "frontend/..\\secret.ts",
136 "C:secret.ts",
137 "/repo/backend/server.ts",
138 "/repo/frontend/../backend/server.ts",
139 "/repo/frontend",
140 "/etc/passwd",
141 ])("does not open an out-of-project diff path: %s", (filePath) => {
142 const openInEditor = vi.fn();
143
144 openDiffFilePrimaryAction({
145 threadRef: THREAD_REF,
146 filePath,
147 activeCwd: "/repo/frontend",
148 repositoryRoot: "/repo",
149 openInEditor,
150 });
151
152 expect(
153 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
154 ).toMatchObject({ isOpen: false });
155 expect(openInEditor).not.toHaveBeenCalled();
156 });
157});