| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | filterNewTaskBranches, |
| 5 | resolveNewTaskBranchWorktreePath, |
| 6 | resolveNewTaskBranchLabel, |
| 7 | resolveNewTaskLocalWorkspaceSelection, |
| 8 | } from "./new-task-context-presentation"; |
| 9 | |
| 10 | describe("resolveNewTaskLocalWorkspaceSelection", () => { |
| 11 | it("waits for refs instead of carrying a worktree base into Current checkout", () => { |
| 12 | expect( |
| 13 | resolveNewTaskLocalWorkspaceSelection({ |
| 14 | branches: [], |
| 15 | projectCwd: "/repo", |
| 16 | }), |
| 17 | ).toEqual({ |
| 18 | branch: null, |
| 19 | worktreePath: null, |
| 20 | awaitsCurrentBranch: true, |
| 21 | }); |
| 22 | }); |
| 23 | |
| 24 | it("adopts the checkout's current branch once refs load", () => { |
| 25 | expect( |
| 26 | resolveNewTaskLocalWorkspaceSelection({ |
| 27 | branches: [ |
| 28 | { name: "feature/worktree-base", current: false, worktreePath: "/worktree" }, |
| 29 | { name: "main", current: true, worktreePath: "/repo" }, |
| 30 | ], |
| 31 | projectCwd: "/repo", |
| 32 | }), |
| 33 | ).toEqual({ |
| 34 | branch: "main", |
| 35 | worktreePath: null, |
| 36 | awaitsCurrentBranch: false, |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | it("carries the worktree path when the current branch lives in another worktree", () => { |
| 41 | expect( |
| 42 | resolveNewTaskLocalWorkspaceSelection({ |
| 43 | branches: [ |
| 44 | { name: "feature/split", current: true, worktreePath: "/repo/.t3/worktrees/split" }, |
| 45 | { name: "main", current: false, worktreePath: "/repo" }, |
| 46 | ], |
| 47 | projectCwd: "/repo", |
| 48 | }), |
| 49 | ).toEqual({ |
| 50 | branch: "feature/split", |
| 51 | worktreePath: "/repo/.t3/worktrees/split", |
| 52 | awaitsCurrentBranch: false, |
| 53 | }); |
| 54 | }); |
| 55 | }); |
| 56 | |
| 57 | describe("resolveNewTaskBranchWorktreePath", () => { |
| 58 | it("moves Current checkout to the selected existing worktree", () => { |
| 59 | expect( |
| 60 | resolveNewTaskBranchWorktreePath({ |
| 61 | workspaceMode: "local", |
| 62 | projectCwd: "/repo", |
| 63 | branchWorktreePath: "/repo/.t3/worktrees/feature", |
| 64 | }), |
| 65 | ).toBe("/repo/.t3/worktrees/feature"); |
| 66 | }); |
| 67 | |
| 68 | it("keeps the project checkout represented by a null override", () => { |
| 69 | expect( |
| 70 | resolveNewTaskBranchWorktreePath({ |
| 71 | workspaceMode: "local", |
| 72 | projectCwd: "/repo", |
| 73 | branchWorktreePath: "/repo", |
| 74 | }), |
| 75 | ).toBeNull(); |
| 76 | }); |
| 77 | |
| 78 | it("does not reuse an existing worktree while creating a new one", () => { |
| 79 | expect( |
| 80 | resolveNewTaskBranchWorktreePath({ |
| 81 | workspaceMode: "worktree", |
| 82 | projectCwd: "/repo", |
| 83 | branchWorktreePath: "/repo/.t3/worktrees/feature", |
| 84 | }), |
| 85 | ).toBeNull(); |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | describe("resolveNewTaskBranchLabel", () => { |
| 90 | it("identifies a deferred PR checkout independently of the origin default", () => { |
| 91 | expect( |
| 92 | resolveNewTaskBranchLabel({ |
| 93 | branchName: "fork/feature", |
| 94 | startFromOrigin: true, |
| 95 | workspaceMode: "worktree", |
| 96 | pullRequestReference: "https://github.com/acme/repo/pull/123", |
| 97 | }), |
| 98 | ).toBe("From PR #123"); |
| 99 | }); |
| 100 | it("shows the checked-out branch without a base-ref prefix", () => { |
| 101 | expect( |
| 102 | resolveNewTaskBranchLabel({ |
| 103 | branchName: "feature/mobile", |
| 104 | startFromOrigin: true, |
| 105 | workspaceMode: "local", |
| 106 | }), |
| 107 | ).toBe("feature/mobile"); |
| 108 | }); |
| 109 | |
| 110 | it("labels a local worktree base with From", () => { |
| 111 | expect( |
| 112 | resolveNewTaskBranchLabel({ |
| 113 | branchName: "main", |
| 114 | startFromOrigin: false, |
| 115 | workspaceMode: "worktree", |
| 116 | }), |
| 117 | ).toBe("From main"); |
| 118 | }); |
| 119 | |
| 120 | it("labels a remote worktree base with From origin", () => { |
| 121 | expect( |
| 122 | resolveNewTaskBranchLabel({ |
| 123 | branchName: "main", |
| 124 | startFromOrigin: true, |
| 125 | workspaceMode: "worktree", |
| 126 | }), |
| 127 | ).toBe("From origin/main"); |
| 128 | }); |
| 129 | |
| 130 | it("prompts when no branch is available", () => { |
| 131 | expect( |
| 132 | resolveNewTaskBranchLabel({ |
| 133 | branchName: null, |
| 134 | startFromOrigin: true, |
| 135 | workspaceMode: "worktree", |
| 136 | }), |
| 137 | ).toBe("Choose branch"); |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | describe("filterNewTaskBranches", () => { |
| 142 | const branches = [ |
| 143 | { name: "main", isRemote: false }, |
| 144 | { name: "Feature/Login-Page", isRemote: false }, |
| 145 | { name: "origin/fix/remote-only", isRemote: true }, |
| 146 | ]; |
| 147 | const search = (query: string) => |
| 148 | filterNewTaskBranches(branches, query).map((branch) => branch.name); |
| 149 | |
| 150 | it("ignores case in both the query and the branch name", () => { |
| 151 | expect(search("feature/login")).toEqual(["Feature/Login-Page"]); |
| 152 | expect(search("MAIN")).toEqual(["main"]); |
| 153 | }); |
| 154 | |
| 155 | it("keeps remote-only branches searchable", () => { |
| 156 | expect(search("remote-only")).toEqual(["origin/fix/remote-only"]); |
| 157 | }); |
| 158 | |
| 159 | it("matches a typed space against the dash a branch name uses", () => { |
| 160 | expect(search(" login page ")).toEqual(["Feature/Login-Page"]); |
| 161 | }); |
| 162 | }); |