This repository has been archived on 2026-11-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
agent-tools/cmd/agentws/rm_test.go
T
unkin-agent 0695c4c2e3
ci / go (pull_request) Failing after 11m25s
ci / actions (pull_request) Failing after 11m25s
ci / ci (pull_request) Failing after 11m26s
ci / release-snapshot (pull_request) Failing after 1s
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci / goreleaser-check (pull_request) Has been cancelled
Prepare for GitHub migration
2026-11-27 22:10:21 +11:00

104 lines
2.9 KiB
Go

package main
import (
"path/filepath"
"strings"
"testing"
"github.com/IUCLabs/agent-tools/internal/agent"
)
// addOtherRepoWorktree clones a second source repo "other" and gives it a
// managed worktree on branch, so two repos share one branch name.
func addOtherRepoWorktree(t *testing.T, f *fixture, branch string) string {
t.Helper()
src := filepath.Join(f.root, "src", "other")
git(t, filepath.Join(f.root, "src"), "clone", f.bare, src)
path := filepath.Join(f.wtRoot, agent.WorktreeDirName("other", branch))
git(t, src, "worktree", "add", path, "-b", branch, "origin/main")
return path
}
func TestResolveUniqueBranch(t *testing.T) {
f := newFixture(t)
want := f.addWorktree(t, "benvin/one")
f.addWorktree(t, "benvin/two")
wt, err := resolveWorktree("benvin/one", "")
if err != nil {
t.Fatal(err)
}
if wt.path != want {
t.Errorf("path %q, want %q", wt.path, want)
}
}
func TestResolveAmbiguousBranchRefused(t *testing.T) {
f := newFixture(t)
a := f.addWorktree(t, "benvin/shared")
b := addOtherRepoWorktree(t, f, "benvin/shared")
_, err := resolveWorktree("benvin/shared", "")
if err == nil {
t.Fatal("ambiguous branch resolved, want refusal")
}
for _, p := range []string{a, b} {
if !strings.Contains(err.Error(), p) {
t.Errorf("error %q does not list candidate %s", err, p)
}
}
}
func TestRmAmbiguousBranchRemovesNothing(t *testing.T) {
f := newFixture(t)
a := f.addWorktree(t, "benvin/shared")
b := addOtherRepoWorktree(t, f, "benvin/shared")
cmd := newRootCmd()
cmd.SetArgs([]string{"rm", "benvin/shared"})
cmd.SetOut(new(strings.Builder))
cmd.SetErr(new(strings.Builder))
if err := cmd.Execute(); err == nil {
t.Fatal("rm succeeded on an ambiguous branch")
}
if !exists(a) || !exists(b) {
t.Error("ambiguous rm removed a worktree")
}
}
func TestResolveByPathDespiteSharedBranch(t *testing.T) {
f := newFixture(t)
f.addWorktree(t, "benvin/shared")
b := addOtherRepoWorktree(t, f, "benvin/shared")
wt, err := resolveWorktree(b, "")
if err != nil {
t.Fatal(err)
}
if wt.path != b || wt.repo != "other" {
t.Errorf("got %s (%s), want %s (other)", wt.path, wt.repo, b)
}
}
func TestResolveRepoDisambiguates(t *testing.T) {
f := newFixture(t)
a := f.addWorktree(t, "benvin/shared")
b := addOtherRepoWorktree(t, f, "benvin/shared")
for repo, want := range map[string]string{"repo": a, "other": b} {
wt, err := resolveWorktree("benvin/shared", repo)
if err != nil {
t.Fatalf("--repo %s: %v", repo, err)
}
if wt.path != want {
t.Errorf("--repo %s: path %q, want %q", repo, wt.path, want)
}
}
if _, err := resolveWorktree("benvin/shared", "missing"); err == nil {
t.Error("--repo missing resolved, want no match")
}
}
func TestResolvePathOutsideRepoFilterRefused(t *testing.T) {
f := newFixture(t)
a := f.addWorktree(t, "benvin/one")
if _, err := resolveWorktree(a, "other"); err == nil {
t.Error("path in repo resolved under --repo other")
}
}