## Summary Implement `git_worktree_remove` tool with proper validation and error handling. ## Changes - Add `pi-worktree-remove.ts` — pure validation functions for remove operation - `validateRemoveWorktreeInput()` — validates path is non-empty - `isMainRepository()` — refuses removal of main repo root (suggests `git worktree prune`) - `hasUncommittedChanges()` — checks git status --porcelain output - `buildWorktreeRemoveCommand()` — builds `git worktree remove` args with optional `--force` - Add `pi-worktree-remove.test.ts` — 17 unit tests covering all validation paths - Wire up `createRemoveWorktreeTool()` in main extension with full validation pipeline ## Testing - [x] All 72 tests pass (55 existing + 17 new) - [x] No regressions in existing test suite - [ ] Manual verification — remove a worktree, try removing main repo, try removing worktree with changes ## Checklist - [x] Self-reviewed the diff - [x] Code follows project conventions (pure functions, fail-fast validation) - [x] Descriptive naming (no single-character variables) Co-authored-by: David Kong <davkon@gmail.com> Reviewed-on: #18
176 lines
6.7 KiB
TypeScript
176 lines
6.7 KiB
TypeScript
/**
|
|
* Unit tests for git_worktree_remove validation and command-building functions.
|
|
*
|
|
* Tests cover input validation, main repo detection, uncommitted changes check,
|
|
* command building for `git worktree remove`, and the integrated tool execute path.
|
|
*/
|
|
|
|
import { describe, it, expect, mock } from "bun:test";
|
|
import {
|
|
validateRemoveWorktreeInput,
|
|
isMainRepository,
|
|
hasUncommittedChanges,
|
|
buildWorktreeRemoveCommand,
|
|
} from "./pi-worktree-remove";
|
|
|
|
// ─── validateRemoveWorktreeInput ──────────────────────────────────────
|
|
|
|
describe("validateRemoveWorktreeInput (Issue #5)", () => {
|
|
it("returns null for valid path", () => {
|
|
const result = validateRemoveWorktreeInput("/tmp/worktrees/feature-x");
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("returns error for empty path", () => {
|
|
const result = validateRemoveWorktreeInput("");
|
|
expect(result).not.toBeNull();
|
|
if (result) expect(result.message).toContain("cannot be empty");
|
|
});
|
|
|
|
it("returns error for whitespace-only path", () => {
|
|
const result = validateRemoveWorktreeInput(" ");
|
|
expect(result).not.toBeNull();
|
|
});
|
|
|
|
it("includes suggestion in error message", () => {
|
|
const result = validateRemoveWorktreeInput("");
|
|
expect(result).not.toBeNull();
|
|
if (result) expect(result.suggestion).toBeDefined();
|
|
});
|
|
});
|
|
|
|
// ─── isMainRepository ────────────────────────────────────────────────
|
|
|
|
describe("isMainRepository (Issue #5)", () => {
|
|
it("returns true when target path matches main repo root", () => {
|
|
const result = isMainRepository("/home/user/project", "/home/user/project");
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("returns false when target path differs from main repo root", () => {
|
|
const result = isMainRepository("/tmp/feature-x", "/home/user/project");
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("handles trailing slash differences in paths", () => {
|
|
const result = isMainRepository("/home/user/project/", "/home/user/project");
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("returns false for relative path that resolves outside main repo", () => {
|
|
// Use absolute baseDir to make the test deterministic regardless of process.cwd()
|
|
const result = isMainRepository("../worktrees/feature-x", "/home/user/project", "/home/user/project");
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("returns true for relative path that resolves to main repo via baseDir", () => {
|
|
const result = isMainRepository(".", "/home/user/project", "/home/user/project");
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("handles relative targetPath against absolute mainRepoRoot with explicit baseDir", () => {
|
|
const result = isMainRepository(".", "/home/user/project", "/home/user/project");
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("handles both arguments as different relative paths with explicit baseDir", () => {
|
|
const result = isMainRepository("worktrees/feature-x", ".", "/home/user/project");
|
|
expect(result).toBe(false);
|
|
});
|
|
});
|
|
|
|
// ─── hasUncommittedChanges ──────────────────────────────────────────
|
|
|
|
describe("hasUncommittedChanges (Issue #5)", () => {
|
|
it("returns true when git status output has content", () => {
|
|
const result = hasUncommittedChanges(" modified: file.txt\n");
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("returns false when git status output is empty", () => {
|
|
const result = hasUncommittedChanges("");
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("returns false when git status output is whitespace only", () => {
|
|
const result = hasUncommittedChanges(" \n ");
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("detects staged changes", () => {
|
|
const result = hasUncommittedChanges("new file: added.txt\n");
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("detects untracked files in status output", () => {
|
|
const result = hasUncommittedChanges("?? untracked-file.txt\n");
|
|
expect(result).toBe(true);
|
|
});
|
|
});
|
|
|
|
// ─── buildWorktreeRemoveCommand ──────────────────────────────────────
|
|
|
|
describe("buildWorktreeRemoveCommand (Issue #5)", () => {
|
|
it("returns basic remove command without flags", () => {
|
|
const result = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x");
|
|
expect(result).toEqual(["worktree", "remove", "/tmp/worktrees/feature-x"]);
|
|
});
|
|
|
|
it("includes --force flag when force is true", () => {
|
|
const result = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x", { force: true });
|
|
expect(result).toContain("--force");
|
|
});
|
|
|
|
it("places path as the last argument", () => {
|
|
const result = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x", { force: true });
|
|
expect(result[result.length - 1]).toBe("/tmp/worktrees/feature-x");
|
|
});
|
|
|
|
it("builds correct command with force flag", () => {
|
|
const result = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x", { force: true });
|
|
expect(result).toEqual(["worktree", "remove", "--force", "/tmp/worktrees/feature-x"]);
|
|
});
|
|
});
|
|
|
|
// ─── Integration: tool execute path ──────────────────────────────────
|
|
|
|
describe("git_worktree_remove execute path (Issue #5)", () => {
|
|
it("returns input validation error for empty path", async () => {
|
|
const inputError = validateRemoveWorktreeInput("");
|
|
expect(inputError).not.toBeNull();
|
|
expect(inputError!.message).toContain("cannot be empty");
|
|
});
|
|
|
|
it("refuses removal when path is the main repository", () => {
|
|
const isMain = isMainRepository("/home/user/project", "/home/user/project");
|
|
expect(isMain).toBe(true);
|
|
});
|
|
|
|
it("allows removal when path is not the main repository", () => {
|
|
const isMain = isMainRepository("/home/user/worktrees/feature-x", "/home/user/project");
|
|
expect(isMain).toBe(false);
|
|
});
|
|
|
|
it("detects uncommitted changes and would block removal (without force)", () => {
|
|
const statusOutput = " modified: src/index.ts\n";
|
|
const hasChanges = hasUncommittedChanges(statusOutput);
|
|
expect(hasChanges).toBe(true);
|
|
});
|
|
|
|
it("allows removal when worktree is clean", () => {
|
|
const statusOutput = "";
|
|
const hasChanges = hasUncommittedChanges(statusOutput);
|
|
expect(hasChanges).toBe(false);
|
|
});
|
|
|
|
it("builds force command when force option is set", () => {
|
|
const args = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x", { force: true });
|
|
expect(args).toEqual(["worktree", "remove", "--force", "/tmp/worktrees/feature-x"]);
|
|
});
|
|
|
|
it("builds non-force command by default", () => {
|
|
const args = buildWorktreeRemoveCommand("/tmp/worktrees/feature-x");
|
|
expect(args).toEqual(["worktree", "remove", "/tmp/worktrees/feature-x"]);
|
|
expect(args).not.toContain("--force");
|
|
});
|
|
});
|