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 it.each([
59 { workspaceRoot: "/frontend", repositoryRoot: "/" },
60 { workspaceRoot: "C:\\frontend", repositoryRoot: "C:\\" },
61 ])("handles filesystem roots: $repositoryRoot", ({ workspaceRoot, repositoryRoot }) => {
62 expect(
63 resolveDiffPathForWorkspace({
64 filePath: "frontend/index.ts",
65 workspaceRoot,
66 repositoryRoot,
67 }),
68 ).toBe("index.ts");
69 });
70
71 it.each(["backend/server.ts", "frontend2/app.ts", "frontend/../secret.ts", "C:secret.ts"])(
72 "does not open an out-of-project diff path: %s",
73 (filePath) => {
74 const openInEditor = vi.fn();
75
76 openDiffFilePrimaryAction({
77 threadRef: THREAD_REF,
78 filePath,
79 activeCwd: "/repo/frontend",
80 repositoryRoot: "/repo",
81 openInEditor,
82 });
83
84 expect(
85 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF),
86 ).toMatchObject({ isOpen: false });
87 expect(openInEditor).not.toHaveBeenCalled();
88 },
89 );
90});