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

diffFileActions.test.ts

151 lines4.4 KB

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 "resolves $filePath in $workspaceRoot",
88 ({ filePath, workspaceRoot, repositoryRoot, expected }) => {
89 expect(resolveDiffPathForWorkspace({ filePath, workspaceRoot, repositoryRoot })).toBe(
90 expected,
91 );
92 },
93 );
94​
95 // regression: #6174 opening diff files broke for repositories at a filesystem root
96 it.each([
97 { workspaceRoot: "/frontend", repositoryRoot: "/" },
98 { workspaceRoot: "C:\\frontend", repositoryRoot: "C:\\" },
99 ])("handles filesystem roots: $repositoryRoot", ({ workspaceRoot, repositoryRoot }) => {
100 expect(
101 resolveDiffPathForWorkspace({
102 filePath: "frontend/index.ts",
103 workspaceRoot,
104 repositoryRoot,
105 }),
106 ).toBe("index.ts");
107 });
108​
109 it.each([
110 {
111 filePath: "/repo/frontend/src/index.ts",
112 workspaceRoot: "/repo/frontend",
113 expected: "src/index.ts",
114 },
115 { filePath: "c:\\Repo\\src\\index.ts", workspaceRoot: "C:\\repo", expected: "src/index.ts" },
116 ])(
117 "opens absolute diff paths inside the workspace: $filePath",
118 ({ filePath, workspaceRoot, expected }) => {
119 expect(
120 resolveDiffPathForWorkspace({ filePath, workspaceRoot, repositoryRoot: workspaceRoot }),
121 ).toBe(expected);
122 },
123 );
124​
125 it.each([
126 "backend/server.ts",
127 "frontend2/app.ts",
128 "frontend/../secret.ts",
129 "frontend/..\\secret.ts",
130 "C:secret.ts",
131 "/repo/backend/server.ts",
132 "/repo/frontend/../backend/server.ts",
133 "/repo/frontend",
134 "/etc/passwd",
135 ])("does not open an out-of-project diff path: %s", (filePath) => {
136 const openInEditor = vi.fn();
137​
138 openDiffFilePrimaryAction({
139 threadRef: THREAD_REF,
140 filePath,
141 activeCwd: "/repo/frontend",
142 repositoryRoot: "/repo",
143 openInEditor,
144 });
145​
146 expect(
147 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
148 ).toMatchObject({ isOpen: false });
149 expect(openInEditor).not.toHaveBeenCalled();
150 });
151});