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("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(["backend/server.ts", "frontend2/app.ts", "frontend/../secret.ts", "C:secret.ts"])(
73 "does not open an out-of-project diff path: %s",
74 (filePath) => {
75 const openInEditor = vi.fn();
76
77 openDiffFilePrimaryAction({
78 threadRef: THREAD_REF,
79 filePath,
80 activeCwd: "/repo/frontend",
81 repositoryRoot: "/repo",
82 openInEditor,
83 });
84
85 expect(
86 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
87 ).toMatchObject({ isOpen: false });
88 expect(openInEditor).not.toHaveBeenCalled();
89 },
90 );
91});