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 {
6 openDiffFileInEditor,
7 openDiffFilePrimaryAction,
8 resolveDiffEditorLaunch,
9 resolveDiffPathForWorkspace,
10} from "./diffFileActions";
11import { selectThreadRightPanelState, useRightPanelStore } from "./rightPanelStore";
12
13const THREAD_REF = scopeThreadRef(
14 EnvironmentId.make("environment-local"),
15 ThreadId.make("thread-1"),
16);
17
18describe("openDiffFilePrimaryAction", () => {
19 beforeEach(() => {
20 useRightPanelStore.setState({ byThreadKey: {} });
21 });
22
23 it("opens diff files in the thread file viewer", () => {
24 const openInEditor = vi.fn();
25
26 openDiffFilePrimaryAction({
27 threadRef: THREAD_REF,
28 filePath: "apps/web/src/components/DiffPanel.tsx",
29 activeCwd: "/repo/project",
30 openInEditor,
31 });
32
33 expect(
34 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
35 ).toMatchObject({
36 isOpen: true,
37 activeSurfaceId: "file:apps/web/src/components/DiffPanel.tsx",
38 });
39 expect(openInEditor).not.toHaveBeenCalled();
40 });
41
42 it("falls back to the editor without thread context", () => {
43 const openInEditor = vi.fn();
44
45 openDiffFilePrimaryAction({
46 threadRef: null,
47 filePath: "apps/web/src/components/DiffPanel.tsx",
48 activeCwd: "/repo/project",
49 openInEditor,
50 });
51
52 expect(openInEditor).toHaveBeenCalledWith(
53 "/repo/project/apps/web/src/components/DiffPanel.tsx",
54 );
55 });
56
57 it("opens repository-relative diff files from a nested project", () => {
58 const openInEditor = vi.fn();
59
60 openDiffFilePrimaryAction({
61 threadRef: THREAD_REF,
62 filePath: "frontend/Dockerfile",
63 activeCwd: "/repo/frontend",
64 repositoryRoot: "/repo",
65 openInEditor,
66 });
67
68 expect(
69 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
70 ).toMatchObject({
71 isOpen: true,
72 activeSurfaceId: "file:Dockerfile",
73 });
74 expect(openInEditor).not.toHaveBeenCalled();
75 });
76
77 it("can explicitly open a repository-relative diff file in the editor", () => {
78 const openInEditor = vi.fn();
79
80 openDiffFileInEditor({
81 filePath: "frontend/Dockerfile",
82 activeCwd: "/repo/frontend",
83 repositoryRoot: "/repo",
84 openInEditor,
85 });
86
87 expect(openInEditor).toHaveBeenCalledWith("/repo/frontend/Dockerfile");
88 });
89
90 it("preserves repository-relative paths in a separate worktree", () => {
91 expect(
92 resolveDiffPathForWorkspace({
93 filePath: "frontend/Dockerfile",
94 workspaceRoot: "/worktrees/feature",
95 repositoryRoot: "/repo",
96 }),
97 ).toBe("frontend/Dockerfile");
98 });
99
100 it("handles Windows roots and mixed diff separators", () => {
101 expect(
102 resolveDiffPathForWorkspace({
103 filePath: "Frontend/src\\index.ts",
104 workspaceRoot: "C:\\repo\\frontend",
105 repositoryRoot: "C:\\repo",
106 }),
107 ).toBe("src/index.ts");
108 });
109
110 it.each([
111 { workspaceRoot: "/frontend", repositoryRoot: "/" },
112 { workspaceRoot: "C:\\frontend", repositoryRoot: "C:\\" },
113 ])("handles filesystem roots: $repositoryRoot", ({ workspaceRoot, repositoryRoot }) => {
114 expect(
115 resolveDiffPathForWorkspace({
116 filePath: "frontend/index.ts",
117 workspaceRoot,
118 repositoryRoot,
119 }),
120 ).toBe("index.ts");
121 });
122
123 it.each(["backend/server.ts", "frontend2/app.ts", "frontend/../secret.ts", "C:secret.ts"])(
124 "does not open an out-of-project diff path: %s",
125 (filePath) => {
126 const openInEditor = vi.fn();
127
128 openDiffFilePrimaryAction({
129 threadRef: THREAD_REF,
130 filePath,
131 activeCwd: "/repo/frontend",
132 repositoryRoot: "/repo",
133 openInEditor,
134 });
135
136 expect(
137 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
138 ).toMatchObject({ isOpen: false });
139 expect(openInEditor).not.toHaveBeenCalled();
140 },
141 );
142});
143
144describe("resolveDiffEditorLaunch", () => {
145 it("execs the preferred editor when the client shares the environment machine", () => {
146 expect(
147 resolveDiffEditorLaunch({
148 remoteOpen: { mode: "local-exec" },
149 editor: "cursor",
150 targetPath: "/repo/file.ts",
151 revealInFileManager: true,
152 }),
153 ).toEqual({ kind: "local-exec", editor: "cursor", reveal: false });
154 });
155
156 it("reveals instead of opening when the preferred editor is the file manager", () => {
157 expect(
158 resolveDiffEditorLaunch({
159 remoteOpen: { mode: "local-exec" },
160 editor: "file-manager",
161 targetPath: "/repo/file.ts",
162 revealInFileManager: true,
163 }),
164 ).toEqual({ kind: "local-exec", editor: "file-manager", reveal: true });
165 });
166
167 it("hands off-machine clients an SSH deep link", () => {
168 const launch = resolveDiffEditorLaunch({
169 remoteOpen: { mode: "remote-links", host: { kind: "ssh-alias", host: "devbox" } },
170 editor: "vscode",
171 targetPath: "/repo/file.ts",
172 revealInFileManager: false,
173 });
174 expect(launch.kind).toBe("remote-url");
175 if (launch.kind === "remote-url") {
176 expect(launch.url).toContain("devbox");
177 expect(launch.url).toContain("/repo/file.ts");
178 }
179 });
180
181 it("never falls back to a server-side exec without an SSH route", () => {
182 expect(
183 resolveDiffEditorLaunch({
184 remoteOpen: { mode: "remote-unavailable" },
185 editor: "vscode",
186 targetPath: "/repo/file.ts",
187 revealInFileManager: false,
188 }),
189 ).toEqual({ kind: "unavailable" });
190 });
191});