|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | +import { Worktree } from "./worktree"; |
| 4 | + |
| 5 | +type FakeResult = { |
| 6 | + cwd(directory: string): FakeResult; |
| 7 | + text(): Promise<string>; |
| 8 | +}; |
| 9 | + |
| 10 | +type FakeShell = ( |
| 11 | + strings: TemplateStringsArray, |
| 12 | + ...values: unknown[] |
| 13 | +) => FakeResult; |
| 14 | + |
| 15 | +type CommandCall = { |
| 16 | + command: string; |
| 17 | + cwd?: string; |
| 18 | +}; |
| 19 | + |
| 20 | +function createFakeShell(outputs: Record<string, string>): { |
| 21 | + shell: FakeShell; |
| 22 | + calls: CommandCall[]; |
| 23 | +} { |
| 24 | + const calls: CommandCall[] = []; |
| 25 | + |
| 26 | + const shell: FakeShell = (strings, ...values) => { |
| 27 | + const command = strings |
| 28 | + .reduce((acc, part, idx) => { |
| 29 | + const value = idx < values.length ? String(values[idx]) : ""; |
| 30 | + return `${acc}${part}${value}`; |
| 31 | + }, "") |
| 32 | + .trim(); |
| 33 | + |
| 34 | + const call: CommandCall = { command }; |
| 35 | + calls.push(call); |
| 36 | + |
| 37 | + return { |
| 38 | + cwd(directory: string) { |
| 39 | + call.cwd = directory; |
| 40 | + return this; |
| 41 | + }, |
| 42 | + async text() { |
| 43 | + return outputs[command] ?? ""; |
| 44 | + }, |
| 45 | + }; |
| 46 | + }; |
| 47 | + |
| 48 | + return { shell, calls }; |
| 49 | +} |
| 50 | + |
| 51 | +describe("Worktree", () => { |
| 52 | + it("creates a worktree with expected branch and path", async () => { |
| 53 | + const repoRoot = "/tmp/project/repo"; |
| 54 | + const { shell, calls } = createFakeShell({ |
| 55 | + "git rev-parse --show-toplevel": `${repoRoot}\n`, |
| 56 | + }); |
| 57 | + const worktree = new Worktree(shell); |
| 58 | + |
| 59 | + const info = await worktree.create("worker-1"); |
| 60 | + |
| 61 | + expect(info).toEqual({ |
| 62 | + name: "worker-1", |
| 63 | + path: resolve(repoRoot, "..", ".worktrees", "worker-1"), |
| 64 | + branch: "worktree/worker-1", |
| 65 | + }); |
| 66 | + expect(calls[1]).toEqual({ |
| 67 | + command: `git worktree add ${resolve(repoRoot, "..", ".worktrees", "worker-1")} -b worktree/worker-1`, |
| 68 | + cwd: repoRoot, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it("rejects invalid names before running git commands", async () => { |
| 73 | + const { shell, calls } = createFakeShell({ |
| 74 | + "git rev-parse --show-toplevel": "/tmp/project/repo\n", |
| 75 | + }); |
| 76 | + const worktree = new Worktree(shell); |
| 77 | + |
| 78 | + await expect(worktree.create("../escape")).rejects.toThrow("Invalid worktree name"); |
| 79 | + await expect(worktree.remove(" bad")).rejects.toThrow("Invalid worktree name"); |
| 80 | + await expect(worktree.merge("a/b")).rejects.toThrow("Invalid worktree name"); |
| 81 | + expect(calls).toHaveLength(0); |
| 82 | + }); |
| 83 | + |
| 84 | + it("lists attached and detached worktrees", async () => { |
| 85 | + const repoRoot = "/tmp/project/repo"; |
| 86 | + const { shell } = createFakeShell({ |
| 87 | + "git rev-parse --show-toplevel": `${repoRoot}\n`, |
| 88 | + "git worktree list --porcelain": [ |
| 89 | + "worktree /tmp/project/repo", |
| 90 | + "HEAD aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 91 | + "branch refs/heads/main", |
| 92 | + "", |
| 93 | + "worktree /tmp/project/.worktrees/worker-2", |
| 94 | + "HEAD bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 95 | + "detached", |
| 96 | + "", |
| 97 | + ].join("\n"), |
| 98 | + }); |
| 99 | + const worktree = new Worktree(shell); |
| 100 | + |
| 101 | + await expect(worktree.list()).resolves.toEqual([ |
| 102 | + { name: "repo", path: "/tmp/project/repo", branch: "main" }, |
| 103 | + { |
| 104 | + name: "worker-2", |
| 105 | + path: "/tmp/project/.worktrees/worker-2", |
| 106 | + branch: "HEAD", |
| 107 | + }, |
| 108 | + ]); |
| 109 | + }); |
| 110 | + |
| 111 | + it("fails fast on malformed worktree entries", async () => { |
| 112 | + const repoRoot = "/tmp/project/repo"; |
| 113 | + const { shell } = createFakeShell({ |
| 114 | + "git rev-parse --show-toplevel": `${repoRoot}\n`, |
| 115 | + "git worktree list --porcelain": ["HEAD deadbeef", "branch refs/heads/main", ""].join( |
| 116 | + "\n", |
| 117 | + ), |
| 118 | + }); |
| 119 | + const worktree = new Worktree(shell); |
| 120 | + |
| 121 | + await expect(worktree.list()).rejects.toThrow("Unable to parse worktree entry"); |
| 122 | + }); |
| 123 | + |
| 124 | + it("merge runs merge then remove", async () => { |
| 125 | + const repoRoot = "/tmp/project/repo"; |
| 126 | + const { shell, calls } = createFakeShell({ |
| 127 | + "git rev-parse --show-toplevel": `${repoRoot}\n`, |
| 128 | + }); |
| 129 | + const worktree = new Worktree(shell); |
| 130 | + |
| 131 | + await worktree.merge("worker-3"); |
| 132 | + |
| 133 | + expect(calls.map((call) => call.command)).toEqual([ |
| 134 | + "git rev-parse --show-toplevel", |
| 135 | + "git merge worktree/worker-3", |
| 136 | + `git worktree remove ${resolve(repoRoot, "..", ".worktrees", "worker-3")} --force`, |
| 137 | + ]); |
| 138 | + expect(calls[1]?.cwd).toBe(repoRoot); |
| 139 | + expect(calls[2]?.cwd).toBe(repoRoot); |
| 140 | + }); |
| 141 | +}); |
0 commit comments