Skip to content
Source
Sign inSign up

diffFileActions.test.ts

113 lines3.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("opens repository-relative diff files from a nested project", () => {
19 const openInEditor = vi.fn();
20​
21 openDiffFilePrimaryAction({
22 threadRef: THREAD_REF,
23 filePath: "frontend/Dockerfile",
24 activeCwd: "/repo/frontend",
25 repositoryRoot: "/repo",
26 openInEditor,
27 });
28​
29 expect(
30 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
31 ).toMatchObject({
32 isOpen: true,
33 activeSurfaceId: "file:Dockerfile",
34 });
35 expect(openInEditor).not.toHaveBeenCalled();
36 });
37​
38 it("preserves repository-relative paths in a separate worktree", () => {
39 expect(
40 resolveDiffPathForWorkspace({
41 filePath: "frontend/Dockerfile",
42 workspaceRoot: "/worktrees/feature",
43 repositoryRoot: "/repo",
44 }),
45 ).toBe("frontend/Dockerfile");
46 });
47​
48 it("handles Windows roots and mixed diff separators", () => {
49 expect(
50 resolveDiffPathForWorkspace({
51 filePath: "Frontend/src\\index.ts",
52 workspaceRoot: "C:\\repo\\frontend",
53 repositoryRoot: "C:\\repo",
54 }),
55 ).toBe("src/index.ts");
56 });
57​
58 // regression: #6174 opening diff files broke for repositories at a filesystem root
59 it.each([
60 { workspaceRoot: "/frontend", repositoryRoot: "/" },
61 { workspaceRoot: "C:\\frontend", repositoryRoot: "C:\\" },
62 ])("handles filesystem roots: $repositoryRoot", ({ workspaceRoot, repositoryRoot }) => {
63 expect(
64 resolveDiffPathForWorkspace({
65 filePath: "frontend/index.ts",
66 workspaceRoot,
67 repositoryRoot,
68 }),
69 ).toBe("index.ts");
70 });
71​
72 it.each([
73 {
74 filePath: "/repo/frontend/src/index.ts",
75 workspaceRoot: "/repo/frontend",
76 expected: "src/index.ts",
77 },
78 { filePath: "c:\\Repo\\src\\index.ts", workspaceRoot: "C:\\repo", expected: "src/index.ts" },
79 ])(
80 "opens absolute diff paths inside the workspace: $filePath",
81 ({ filePath, workspaceRoot, expected }) => {
82 expect(
83 resolveDiffPathForWorkspace({ filePath, workspaceRoot, repositoryRoot: workspaceRoot }),
84 ).toBe(expected);
85 },
86 );
87​
88 it.each([
89 "backend/server.ts",
90 "frontend2/app.ts",
91 "frontend/../secret.ts",
92 "C:secret.ts",
93 "/repo/backend/server.ts",
94 "/repo/frontend/../backend/server.ts",
95 "/repo/frontend",
96 "/etc/passwd",
97 ])("does not open an out-of-project diff path: %s", (filePath) => {
98 const openInEditor = vi.fn();
99​
100 openDiffFilePrimaryAction({
101 threadRef: THREAD_REF,
102 filePath,
103 activeCwd: "/repo/frontend",
104 repositoryRoot: "/repo",
105 openInEditor,
106 });
107​
108 expect(
109 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
110 ).toMatchObject({ isOpen: false });
111 expect(openInEditor).not.toHaveBeenCalled();
112 });
113});