gitgram/aitimemachine/beads/cmd/bd/hooks_worktree_test.go
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

284 lines
7.8 KiB
Go

package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/git"
)
func TestConfigureBeadsHooksPath_WorktreeUsesMainRepo(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "beads-hooks-worktree-test-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(tmpDir) })
mainRepoDir := filepath.Join(tmpDir, "main-repo")
if err := os.MkdirAll(mainRepoDir, 0755); err != nil {
t.Fatal(err)
}
run := func(args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Skipf("git %v failed: %v", args, err)
}
}
run("init")
run("config", "user.email", "test@example.com")
run("config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatal(err)
}
run("add", "README.md")
run("commit", "-m", "Initial commit")
worktreeDir := filepath.Join(tmpDir, "worktree")
cmd := exec.Command("git", "worktree", "add", worktreeDir, "HEAD")
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Fatalf("git worktree add failed: %v", err)
}
t.Cleanup(func() {
cmd := exec.Command("git", "worktree", "remove", "--force", worktreeDir)
cmd.Dir = mainRepoDir
_ = cmd.Run()
})
if err := os.MkdirAll(filepath.Join(mainRepoDir, ".beads", "hooks"), 0755); err != nil {
t.Fatal(err)
}
t.Chdir(worktreeDir)
git.ResetCaches()
if !git.IsWorktree() {
t.Fatal("expected git.IsWorktree() to return true")
}
if err := configureBeadsHooksPath(); err != nil {
t.Fatalf("configureBeadsHooksPath failed: %v", err)
}
cmd = exec.Command("git", "config", "--get", "core.hooksPath")
cmd.Dir = mainRepoDir
out, err := cmd.Output()
if err != nil {
t.Fatalf("git config --get core.hooksPath failed: %v", err)
}
hooksPath := filepath.Clean(strings.TrimSpace(string(out)))
expected := filepath.Join(mainRepoDir, ".beads", "hooks")
hooksPath, _ = filepath.EvalSymlinks(hooksPath)
expected, _ = filepath.EvalSymlinks(expected)
if hooksPath != expected {
t.Errorf("core.hooksPath = %q, want %q", hooksPath, expected)
}
}
func TestConfigureSharedHooksPath_WorktreeUsesMainRepo(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "beads-shared-hooks-worktree-test-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(tmpDir) })
mainRepoDir := filepath.Join(tmpDir, "main-repo")
if err := os.MkdirAll(mainRepoDir, 0755); err != nil {
t.Fatal(err)
}
run := func(args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Skipf("git %v failed: %v", args, err)
}
}
run("init")
run("config", "user.email", "test@example.com")
run("config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatal(err)
}
run("add", "README.md")
run("commit", "-m", "Initial commit")
worktreeDir := filepath.Join(tmpDir, "worktree")
cmd := exec.Command("git", "worktree", "add", worktreeDir, "HEAD")
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Fatalf("git worktree add failed: %v", err)
}
t.Cleanup(func() {
cmd := exec.Command("git", "worktree", "remove", "--force", worktreeDir)
cmd.Dir = mainRepoDir
_ = cmd.Run()
})
if err := os.MkdirAll(filepath.Join(mainRepoDir, ".beads-hooks"), 0755); err != nil {
t.Fatal(err)
}
t.Chdir(worktreeDir)
git.ResetCaches()
if !git.IsWorktree() {
t.Fatal("expected git.IsWorktree() to return true")
}
if err := configureSharedHooksPath(); err != nil {
t.Fatalf("configureSharedHooksPath failed: %v", err)
}
cmd = exec.Command("git", "config", "--get", "core.hooksPath")
cmd.Dir = mainRepoDir
out, err := cmd.Output()
if err != nil {
t.Fatalf("git config --get core.hooksPath failed: %v", err)
}
hooksPath := filepath.Clean(strings.TrimSpace(string(out)))
expected := filepath.Join(mainRepoDir, ".beads-hooks")
hooksPath, _ = filepath.EvalSymlinks(hooksPath)
expected, _ = filepath.EvalSymlinks(expected)
if hooksPath != expected {
t.Errorf("core.hooksPath = %q, want %q", hooksPath, expected)
}
}
func TestResetHooksPathIfBeadsManaged_Worktree(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "beads-reset-hooks-worktree-test-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(tmpDir) })
mainRepoDir := filepath.Join(tmpDir, "main-repo")
if err := os.MkdirAll(mainRepoDir, 0755); err != nil {
t.Fatal(err)
}
run := func(args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Skipf("git %v failed: %v", args, err)
}
}
run("init")
run("config", "user.email", "test@example.com")
run("config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatal(err)
}
run("add", "README.md")
run("commit", "-m", "Initial commit")
worktreeDir := filepath.Join(tmpDir, "worktree")
cmd := exec.Command("git", "worktree", "add", worktreeDir, "HEAD")
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Fatalf("git worktree add failed: %v", err)
}
t.Cleanup(func() {
cmd := exec.Command("git", "worktree", "remove", "--force", worktreeDir)
cmd.Dir = mainRepoDir
_ = cmd.Run()
})
if err := os.MkdirAll(filepath.Join(mainRepoDir, ".beads", "hooks"), 0755); err != nil {
t.Fatal(err)
}
hooksPathToSet := filepath.Join(mainRepoDir, ".beads", "hooks")
evaluated, _ := filepath.EvalSymlinks(hooksPathToSet)
if evaluated != "" {
hooksPathToSet = evaluated
}
cmd = exec.Command("git", "config", "core.hooksPath", hooksPathToSet)
cmd.Dir = mainRepoDir
if err := cmd.Run(); err != nil {
t.Fatalf("git config core.hooksPath failed: %v", err)
}
t.Chdir(worktreeDir)
git.ResetCaches()
if !git.IsWorktree() {
t.Fatal("expected git.IsWorktree() to return true")
}
if err := resetHooksPathIfBeadsManaged(); err != nil {
t.Fatalf("resetHooksPathIfBeadsManaged failed: %v", err)
}
cmd = exec.Command("git", "config", "--get", "core.hooksPath")
cmd.Dir = mainRepoDir
out, _ := cmd.Output()
if strings.TrimSpace(string(out)) != "" {
t.Errorf("core.hooksPath = %q after reset, want empty", strings.TrimSpace(string(out)))
}
}
func TestConfigureBeadsHooksPath_NormalRepoUnchanged(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "beads-hooks-normal-test-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(tmpDir) })
repoDir := filepath.Join(tmpDir, "repo")
if err := os.MkdirAll(repoDir, 0755); err != nil {
t.Fatal(err)
}
run := func(args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = repoDir
if err := cmd.Run(); err != nil {
t.Skipf("git %v failed: %v", args, err)
}
}
run("init")
run("config", "user.email", "test@example.com")
run("config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(repoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatal(err)
}
run("add", "README.md")
run("commit", "-m", "Initial commit")
if err := os.MkdirAll(filepath.Join(repoDir, ".beads", "hooks"), 0755); err != nil {
t.Fatal(err)
}
t.Chdir(repoDir)
git.ResetCaches()
if git.IsWorktree() {
t.Fatal("expected git.IsWorktree() to return false in normal repo")
}
if err := configureBeadsHooksPath(); err != nil {
t.Fatalf("configureBeadsHooksPath failed: %v", err)
}
cmd := exec.Command("git", "config", "--get", "core.hooksPath")
cmd.Dir = repoDir
out, err := cmd.Output()
if err != nil {
t.Fatalf("git config --get core.hooksPath failed: %v", err)
}
hooksPath := filepath.Clean(strings.TrimSpace(string(out)))
expected := filepath.Join(repoDir, ".beads", "hooks")
hooksPath, _ = filepath.EvalSymlinks(hooksPath)
expected, _ = filepath.EvalSymlinks(expected)
if hooksPath != expected {
t.Errorf("core.hooksPath = %q, want %q", hooksPath, expected)
}
}