The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 3 | import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 4 | |
| 5 | import { openDiffFilePrimaryAction, resolveDiffPathForWorkspace } from "./diffFileActions"; |
| 6 | import { selectThreadRightPanelState, useRightPanelStore } from "./rightPanelStore"; |
| 7 | |
| 8 | const THREAD_REF = scopeThreadRef( |
| 9 | EnvironmentId.make("environment-local"), |
| 10 | ThreadId.make("thread-1"), |
| 11 | ); |
| 12 | |
| 13 | describe("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.each([ |
| 49 | { |
| 50 | filePath: "Frontend/src\\index.ts", |
| 51 | workspaceRoot: "C:\\repo\\frontend", |
| 52 | repositoryRoot: "C:\\repo", |
| 53 | expected: "src/index.ts", |
| 54 | }, |
| 55 | { |
| 56 | filePath: "Frontend\\src\\index.ts", |
| 57 | workspaceRoot: "C:\\repo\\frontend", |
| 58 | repositoryRoot: "C:\\repo", |
| 59 | expected: "src/index.ts", |
| 60 | }, |
| 61 | { |
| 62 | filePath: "frontend/back\\slash.txt", |
| 63 | workspaceRoot: "/repo/frontend", |
| 64 | repositoryRoot: "/repo", |
| 65 | expected: "back\\slash.txt", |
| 66 | }, |
| 67 | { |
| 68 | filePath: "back/slash/a.ts", |
| 69 | workspaceRoot: "/repo/back\\slash", |
| 70 | repositoryRoot: "/repo", |
| 71 | expected: null, |
| 72 | }, |
| 73 | ])( |
| 74 | "resolves $filePath in $workspaceRoot", |
| 75 | ({ filePath, workspaceRoot, repositoryRoot, expected }) => { |
| 76 | expect(resolveDiffPathForWorkspace({ filePath, workspaceRoot, repositoryRoot })).toBe( |
| 77 | expected, |
| 78 | ); |
| 79 | }, |
| 80 | ); |
| 81 | |
| 82 | // regression: #6174 opening diff files broke for repositories at a filesystem root |
| 83 | it.each([ |
| 84 | { workspaceRoot: "/frontend", repositoryRoot: "/" }, |
| 85 | { workspaceRoot: "C:\\frontend", repositoryRoot: "C:\\" }, |
| 86 | ])("handles filesystem roots: $repositoryRoot", ({ workspaceRoot, repositoryRoot }) => { |
| 87 | expect( |
| 88 | resolveDiffPathForWorkspace({ |
| 89 | filePath: "frontend/index.ts", |
| 90 | workspaceRoot, |
| 91 | repositoryRoot, |
| 92 | }), |
| 93 | ).toBe("index.ts"); |
| 94 | }); |
| 95 | |
| 96 | it.each([ |
| 97 | { |
| 98 | filePath: "/repo/frontend/src/index.ts", |
| 99 | workspaceRoot: "/repo/frontend", |
| 100 | expected: "src/index.ts", |
| 101 | }, |
| 102 | { filePath: "c:\\Repo\\src\\index.ts", workspaceRoot: "C:\\repo", expected: "src/index.ts" }, |
| 103 | ])( |
| 104 | "opens absolute diff paths inside the workspace: $filePath", |
| 105 | ({ filePath, workspaceRoot, expected }) => { |
| 106 | expect( |
| 107 | resolveDiffPathForWorkspace({ filePath, workspaceRoot, repositoryRoot: workspaceRoot }), |
| 108 | ).toBe(expected); |
| 109 | }, |
| 110 | ); |
| 111 | |
| 112 | it.each([ |
| 113 | "backend/server.ts", |
| 114 | "frontend2/app.ts", |
| 115 | "frontend/../secret.ts", |
| 116 | "frontend/..\\secret.ts", |
| 117 | "C:secret.ts", |
| 118 | "/repo/backend/server.ts", |
| 119 | "/repo/frontend/../backend/server.ts", |
| 120 | "/repo/frontend", |
| 121 | "/etc/passwd", |
| 122 | ])("does not open an out-of-project diff path: %s", (filePath) => { |
| 123 | const openInEditor = vi.fn(); |
| 124 | |
| 125 | openDiffFilePrimaryAction({ |
| 126 | threadRef: THREAD_REF, |
| 127 | filePath, |
| 128 | activeCwd: "/repo/frontend", |
| 129 | repositoryRoot: "/repo", |
| 130 | openInEditor, |
| 131 | }); |
| 132 | |
| 133 | expect( |
| 134 | selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, THREAD_REF), |
| 135 | ).toMatchObject({ isOpen: false }); |
| 136 | expect(openInEditor).not.toHaveBeenCalled(); |
| 137 | }); |
| 138 | }); |