This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/aitimemachine/beads/cmd/bd/init_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

221 lines
6.4 KiB
Go

package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/git"
)
func TestCountExistingIssues_WorktreeFallback(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "beads-worktree-init-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(dir string, args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
}
run(mainRepoDir, "init")
run(mainRepoDir, "config", "user.email", "test@example.com")
run(mainRepoDir, "config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatal(err)
}
run(mainRepoDir, "add", "README.md")
run(mainRepoDir, "commit", "-m", "Initial commit")
worktreeDir := filepath.Join(tmpDir, "worktree")
cmd := exec.Command("git", "worktree", "add", worktreeDir, "HEAD")
cmd.Dir = mainRepoDir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git worktree add failed: %v\n%s", err, out)
}
t.Cleanup(func() {
cmd := exec.Command("git", "worktree", "remove", "--force", worktreeDir)
cmd.Dir = mainRepoDir
_ = cmd.Run()
})
mainBeadsDir := filepath.Join(mainRepoDir, ".beads")
if err := os.MkdirAll(mainBeadsDir, 0755); err != nil {
t.Fatal(err)
}
worktreeBeadsDir := filepath.Join(worktreeDir, ".beads")
os.RemoveAll(worktreeBeadsDir)
t.Chdir(worktreeDir)
git.ResetCaches()
if !isGitRepo() {
t.Fatal("expected isGitRepo() to return true in worktree")
}
if !git.IsWorktree() {
t.Fatal("expected git.IsWorktree() to return true in worktree")
}
fallback := beads.GetWorktreeFallbackBeadsDir()
fallback = strings.TrimSpace(fallback)
fallback = filepath.Clean(fallback)
if fallback == "" {
t.Fatal("expected GetWorktreeFallbackBeadsDir() to return non-empty path in worktree")
}
expectedBeads := filepath.Clean(mainBeadsDir)
fallbackResolved, err := filepath.EvalSymlinks(fallback)
if err != nil {
t.Fatalf("EvalSymlinks(%q) failed: %v", fallback, err)
}
expectedResolved, err := filepath.EvalSymlinks(expectedBeads)
if err != nil {
t.Fatalf("EvalSymlinks(%q) failed: %v", expectedBeads, err)
}
if fallbackResolved != expectedResolved {
t.Errorf("GetWorktreeFallbackBeadsDir() = %q (resolved: %q), want %q (resolved: %q)",
fallback, fallbackResolved, expectedBeads, expectedResolved)
}
}
func TestCountExistingIssues_WorktreeLocalBeadsPreferred(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "beads-worktree-init-local-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(dir string, args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
}
run(mainRepoDir, "init")
run(mainRepoDir, "config", "user.email", "test@example.com")
run(mainRepoDir, "config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatal(err)
}
run(mainRepoDir, "add", "README.md")
run(mainRepoDir, "commit", "-m", "Initial commit")
worktreeDir := filepath.Join(tmpDir, "worktree")
cmd := exec.Command("git", "worktree", "add", worktreeDir, "HEAD")
cmd.Dir = mainRepoDir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git worktree add failed: %v\n%s", err, out)
}
t.Cleanup(func() {
cleanupCmd := exec.Command("git", "worktree", "remove", "--force", worktreeDir)
cleanupCmd.Dir = mainRepoDir
_ = cleanupCmd.Run()
})
mainBeadsDir := filepath.Join(mainRepoDir, ".beads")
worktreeBeadsDir := filepath.Join(worktreeDir, ".beads")
if err := os.MkdirAll(mainBeadsDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(worktreeBeadsDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(worktreeBeadsDir, "metadata.json"), []byte("{}"), 0644); err != nil {
t.Fatal(err)
}
t.Chdir(worktreeDir)
git.ResetCaches()
if !git.IsWorktree() {
t.Fatal("expected git.IsWorktree() to return true")
}
found := beads.FindBeadsDir()
if found == "" {
t.Fatal("FindBeadsDir() returned empty")
}
foundResolved, _ := filepath.EvalSymlinks(filepath.Clean(found))
worktreeResolved, _ := filepath.EvalSymlinks(filepath.Clean(worktreeBeadsDir))
if foundResolved != worktreeResolved {
t.Errorf("FindBeadsDir() = %q, want %q (local worktree .beads should take precedence over main repo fallback)",
foundResolved, worktreeResolved)
}
}
func TestCountExistingIssues_WorktreeNoBeadsAnywhere(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "beads-worktree-init-empty-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(dir string, args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
}
run(mainRepoDir, "init")
run(mainRepoDir, "config", "user.email", "test@example.com")
run(mainRepoDir, "config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(mainRepoDir, "README.md"), []byte("# Test\n"), 0644); err != nil {
t.Fatal(err)
}
run(mainRepoDir, "add", "README.md")
run(mainRepoDir, "commit", "-m", "Initial commit")
worktreeDir := filepath.Join(tmpDir, "worktree")
cmd := exec.Command("git", "worktree", "add", worktreeDir, "HEAD")
cmd.Dir = mainRepoDir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git worktree add failed: %v\n%s", err, out)
}
t.Cleanup(func() {
cleanupCmd := exec.Command("git", "worktree", "remove", "--force", worktreeDir)
cleanupCmd.Dir = mainRepoDir
_ = cleanupCmd.Run()
})
os.RemoveAll(filepath.Join(worktreeDir, ".beads"))
t.Chdir(worktreeDir)
git.ResetCaches()
found := beads.FindBeadsDir()
if found != "" {
t.Errorf("FindBeadsDir() = %q, want empty string when no .beads exists anywhere", found)
}
}