fix(orphans): honor --db flag for cross-repo orphan detection (#1200)
* fix(orphans): honor --db flag for cross-repo orphan detection Problem: - `bd orphans --db /path` ignored the --db flag entirely - FindOrphanedIssues() hardcoded local .beads/ directory Solution: - Introduce IssueProvider interface for abstract issue lookup - Add StorageProvider adapter wrapping Storage instances - Update FindOrphanedIssues to accept provider instead of path - Wire orphans command to create provider from --db flag Closes: steveyegge/beads#1196 * test(orphans): add cross-repo and provider tests for --db flag fix - Add TestFindOrphanedIssues_WithMockProvider (table-driven, UT-01 through UT-09) - Add TestFindOrphanedIssues_CrossRepo (validates --db flag honored) - Add TestFindOrphanedIssues_LocalProvider (backward compat RT-01) - Add TestFindOrphanedIssues_ProviderError (error handling UT-07) - Add TestFindOrphanedIssues_IntegrationCrossRepo (IT-02 full) - Add TestLocalProvider_* unit tests Coverage for IssueProvider interface and cross-repo orphan detection. * docs: add bd orphans command to CLI reference Document the orphan detection command including the cross-repo workflow enabled by the --db flag fix in this PR.
This commit is contained in:
committed by
GitHub
parent
a0dac11e42
commit
c11fa799be
@@ -1,6 +1,7 @@
|
||||
package doctor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"github.com/steveyegge/beads/cmd/bd/doctor/fix"
|
||||
"github.com/steveyegge/beads/internal/git"
|
||||
"github.com/steveyegge/beads/internal/syncbranch"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -774,64 +776,35 @@ func FixSyncBranchHealth(path string) error {
|
||||
|
||||
// FindOrphanedIssues identifies issues referenced in git commits but still open in the database.
|
||||
// This is the shared core logic used by both 'bd orphans' and 'bd doctor' commands.
|
||||
// Returns empty slice if not a git repo, no database, or no orphans found (no error).
|
||||
func FindOrphanedIssues(path string) ([]OrphanIssue, error) {
|
||||
// Returns empty slice if not a git repo, no issues from provider, or no orphans found (no error).
|
||||
//
|
||||
// Parameters:
|
||||
// - gitPath: The directory to scan for git commits
|
||||
// - provider: The issue provider to get open issues and prefix from
|
||||
func FindOrphanedIssues(gitPath string, provider types.IssueProvider) ([]OrphanIssue, error) {
|
||||
// Skip if not in a git repo
|
||||
cmd := exec.Command("git", "rev-parse", "--git-dir")
|
||||
cmd.Dir = path
|
||||
cmd.Dir = gitPath
|
||||
if err := cmd.Run(); err != nil {
|
||||
return []OrphanIssue{}, nil // Not a git repo, return empty list
|
||||
}
|
||||
|
||||
// Follow redirect to resolve actual beads directory (bd-tvus fix)
|
||||
beadsDir := resolveBeadsDir(filepath.Join(path, ".beads"))
|
||||
// Get issue prefix from provider
|
||||
issuePrefix := provider.GetIssuePrefix()
|
||||
|
||||
// Skip if no .beads directory
|
||||
if _, err := os.Stat(beadsDir); os.IsNotExist(err) {
|
||||
return []OrphanIssue{}, nil
|
||||
}
|
||||
|
||||
// Get database path
|
||||
dbPath := filepath.Join(beadsDir, "beads.db")
|
||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
||||
return []OrphanIssue{}, nil
|
||||
}
|
||||
|
||||
// Open database read-only
|
||||
db, err := openDBReadOnly(dbPath)
|
||||
// Get all open/in_progress issues from provider
|
||||
ctx := context.Background()
|
||||
issues, err := provider.GetOpenIssues(ctx)
|
||||
if err != nil {
|
||||
return []OrphanIssue{}, nil
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Get issue prefix from config
|
||||
var issuePrefix string
|
||||
err = db.QueryRow("SELECT value FROM config WHERE key = 'issue_prefix'").Scan(&issuePrefix)
|
||||
if err != nil || issuePrefix == "" {
|
||||
issuePrefix = "bd" // default
|
||||
}
|
||||
|
||||
// Get all open/in_progress issues with their titles (title is optional for compatibility)
|
||||
var rows *sql.Rows
|
||||
rows, err = db.Query("SELECT id, title, status FROM issues WHERE status IN ('open', 'in_progress')")
|
||||
// If the query fails (e.g., no title column), fall back to simpler query
|
||||
if err != nil {
|
||||
rows, err = db.Query("SELECT id, '', status FROM issues WHERE status IN ('open', 'in_progress')")
|
||||
if err != nil {
|
||||
return []OrphanIssue{}, nil
|
||||
}
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
openIssues := make(map[string]*OrphanIssue)
|
||||
for rows.Next() {
|
||||
var id, title, status string
|
||||
if err := rows.Scan(&id, &title, &status); err == nil {
|
||||
openIssues[id] = &OrphanIssue{
|
||||
IssueID: id,
|
||||
Title: title,
|
||||
Status: status,
|
||||
}
|
||||
for _, issue := range issues {
|
||||
openIssues[issue.ID] = &OrphanIssue{
|
||||
IssueID: issue.ID,
|
||||
Title: issue.Title,
|
||||
Status: string(issue.Status),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -841,7 +814,7 @@ func FindOrphanedIssues(path string) ([]OrphanIssue, error) {
|
||||
|
||||
// Get git log
|
||||
cmd = exec.Command("git", "log", "--oneline", "--all")
|
||||
cmd.Dir = path
|
||||
cmd.Dir = gitPath
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return []OrphanIssue{}, nil
|
||||
@@ -896,6 +869,103 @@ func FindOrphanedIssues(path string) ([]OrphanIssue, error) {
|
||||
return orphanedIssues, nil
|
||||
}
|
||||
|
||||
// FindOrphanedIssuesFromPath is a convenience function for callers that don't have a provider.
|
||||
// It creates a local provider from the given path's .beads/ directory.
|
||||
// This preserves backward compatibility for CheckOrphanedIssues and similar callers.
|
||||
func FindOrphanedIssuesFromPath(path string) ([]OrphanIssue, error) {
|
||||
// Follow redirect to resolve actual beads directory (bd-tvus fix)
|
||||
beadsDir := resolveBeadsDir(filepath.Join(path, ".beads"))
|
||||
|
||||
// Skip if no .beads directory
|
||||
if _, err := os.Stat(beadsDir); os.IsNotExist(err) {
|
||||
return []OrphanIssue{}, nil
|
||||
}
|
||||
|
||||
// Get database path
|
||||
dbPath := filepath.Join(beadsDir, "beads.db")
|
||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
||||
return []OrphanIssue{}, nil
|
||||
}
|
||||
|
||||
// Create a local provider from the database
|
||||
provider, err := NewLocalProvider(dbPath)
|
||||
if err != nil {
|
||||
return []OrphanIssue{}, nil
|
||||
}
|
||||
defer provider.Close()
|
||||
|
||||
return FindOrphanedIssues(path, provider)
|
||||
}
|
||||
|
||||
// LocalProvider implements types.IssueProvider using a read-only SQLite connection.
|
||||
// This is used by CheckOrphanedIssues and other internal callers that need a provider
|
||||
// from a local .beads/ directory.
|
||||
type LocalProvider struct {
|
||||
db *sql.DB
|
||||
prefix string
|
||||
}
|
||||
|
||||
// NewLocalProvider creates a provider backed by a SQLite database.
|
||||
func NewLocalProvider(dbPath string) (*LocalProvider, error) {
|
||||
db, err := openDBReadOnly(dbPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get issue prefix from config
|
||||
var prefix string
|
||||
err = db.QueryRow("SELECT value FROM config WHERE key = 'issue_prefix'").Scan(&prefix)
|
||||
if err != nil || prefix == "" {
|
||||
prefix = "bd" // default
|
||||
}
|
||||
|
||||
return &LocalProvider{db: db, prefix: prefix}, nil
|
||||
}
|
||||
|
||||
// GetOpenIssues returns issues that are open or in_progress.
|
||||
func (p *LocalProvider) GetOpenIssues(ctx context.Context) ([]*types.Issue, error) {
|
||||
// Get all open/in_progress issues with their titles (title is optional for compatibility)
|
||||
rows, err := p.db.QueryContext(ctx, "SELECT id, title, status FROM issues WHERE status IN ('open', 'in_progress')")
|
||||
// If the query fails (e.g., no title column), fall back to simpler query
|
||||
if err != nil {
|
||||
rows, err = p.db.QueryContext(ctx, "SELECT id, '', status FROM issues WHERE status IN ('open', 'in_progress')")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var issues []*types.Issue
|
||||
for rows.Next() {
|
||||
var id, title, status string
|
||||
if err := rows.Scan(&id, &title, &status); err == nil {
|
||||
issues = append(issues, &types.Issue{
|
||||
ID: id,
|
||||
Title: title,
|
||||
Status: types.Status(status),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return issues, nil
|
||||
}
|
||||
|
||||
// GetIssuePrefix returns the configured issue prefix.
|
||||
func (p *LocalProvider) GetIssuePrefix() string {
|
||||
return p.prefix
|
||||
}
|
||||
|
||||
// Close closes the underlying database connection.
|
||||
func (p *LocalProvider) Close() error {
|
||||
if p.db != nil {
|
||||
return p.db.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ensure LocalProvider implements types.IssueProvider
|
||||
var _ types.IssueProvider = (*LocalProvider)(nil)
|
||||
|
||||
// CheckOrphanedIssues detects issues referenced in git commits but still open.
|
||||
// This catches cases where someone implemented a fix with "(bd-xxx)" in the commit
|
||||
// message but forgot to run "bd close".
|
||||
@@ -936,8 +1006,8 @@ func CheckOrphanedIssues(path string) DoctorCheck {
|
||||
}
|
||||
}
|
||||
|
||||
// Use the shared FindOrphanedIssues function
|
||||
orphans, err := FindOrphanedIssues(path)
|
||||
// Use the shared FindOrphanedIssuesFromPath function (creates its own provider)
|
||||
orphans, err := FindOrphanedIssuesFromPath(path)
|
||||
if err != nil {
|
||||
return DoctorCheck{
|
||||
Name: "Orphaned Issues",
|
||||
|
||||
563
cmd/bd/doctor/orphans_provider_test.go
Normal file
563
cmd/bd/doctor/orphans_provider_test.go
Normal file
@@ -0,0 +1,563 @@
|
||||
package doctor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
|
||||
_ "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
// mockIssueProvider implements types.IssueProvider for testing FindOrphanedIssues
|
||||
type mockIssueProvider struct {
|
||||
issues []*types.Issue
|
||||
prefix string
|
||||
err error // If set, GetOpenIssues returns this error
|
||||
}
|
||||
|
||||
func (m *mockIssueProvider) GetOpenIssues(ctx context.Context) ([]*types.Issue, error) {
|
||||
if m.err != nil {
|
||||
return nil, m.err
|
||||
}
|
||||
return m.issues, nil
|
||||
}
|
||||
|
||||
func (m *mockIssueProvider) GetIssuePrefix() string {
|
||||
if m.prefix == "" {
|
||||
return "bd"
|
||||
}
|
||||
return m.prefix
|
||||
}
|
||||
|
||||
// Ensure mockIssueProvider implements types.IssueProvider
|
||||
var _ types.IssueProvider = (*mockIssueProvider)(nil)
|
||||
|
||||
// setupTestGitRepo creates a git repo with specified commits for testing
|
||||
func setupTestGitRepo(t *testing.T, commits []string) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
|
||||
// Initialize git repo
|
||||
cmd := exec.Command("git", "init", "--initial-branch=main")
|
||||
cmd.Dir = dir
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("failed to init git repo: %v", err)
|
||||
}
|
||||
|
||||
// Configure git user for commits
|
||||
cmd = exec.Command("git", "config", "user.email", "test@test.com")
|
||||
cmd.Dir = dir
|
||||
_ = cmd.Run()
|
||||
|
||||
cmd = exec.Command("git", "config", "user.name", "Test User")
|
||||
cmd.Dir = dir
|
||||
_ = cmd.Run()
|
||||
|
||||
// Create commits
|
||||
for i, msg := range commits {
|
||||
testFile := filepath.Join(dir, "file.txt")
|
||||
content := []byte("content " + string(rune('0'+i)))
|
||||
if err := os.WriteFile(testFile, content, 0644); err != nil {
|
||||
t.Fatalf("failed to write test file: %v", err)
|
||||
}
|
||||
cmd = exec.Command("git", "add", "file.txt")
|
||||
cmd.Dir = dir
|
||||
_ = cmd.Run()
|
||||
cmd = exec.Command("git", "commit", "-m", msg)
|
||||
cmd.Dir = dir
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("failed to create commit: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return dir
|
||||
}
|
||||
|
||||
// TestFindOrphanedIssues_WithMockProvider tests FindOrphanedIssues with various mock providers
|
||||
func TestFindOrphanedIssues_WithMockProvider(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
provider *mockIssueProvider
|
||||
commits []string
|
||||
expected int // Number of orphans expected
|
||||
issueID string // Expected issue ID in orphans (if expected > 0)
|
||||
}{
|
||||
"UT-01: Basic orphan detection": {
|
||||
provider: &mockIssueProvider{
|
||||
issues: []*types.Issue{
|
||||
{ID: "bd-abc", Title: "Test issue", Status: types.StatusOpen},
|
||||
},
|
||||
prefix: "bd",
|
||||
},
|
||||
commits: []string{"Initial commit", "Fix bug (bd-abc)"},
|
||||
expected: 1,
|
||||
issueID: "bd-abc",
|
||||
},
|
||||
"UT-02: No orphans when no matching commits": {
|
||||
provider: &mockIssueProvider{
|
||||
issues: []*types.Issue{
|
||||
{ID: "bd-xyz", Title: "Test issue", Status: types.StatusOpen},
|
||||
},
|
||||
prefix: "bd",
|
||||
},
|
||||
commits: []string{"Initial commit", "Some other change"},
|
||||
expected: 0,
|
||||
},
|
||||
"UT-03: Custom prefix TEST": {
|
||||
provider: &mockIssueProvider{
|
||||
issues: []*types.Issue{
|
||||
{ID: "TEST-001", Title: "Test issue", Status: types.StatusOpen},
|
||||
},
|
||||
prefix: "TEST",
|
||||
},
|
||||
commits: []string{"Initial commit", "Implement feature (TEST-001)"},
|
||||
expected: 1,
|
||||
issueID: "TEST-001",
|
||||
},
|
||||
"UT-04: Multiple orphans": {
|
||||
provider: &mockIssueProvider{
|
||||
issues: []*types.Issue{
|
||||
{ID: "bd-aaa", Title: "Issue A", Status: types.StatusOpen},
|
||||
{ID: "bd-bbb", Title: "Issue B", Status: types.StatusOpen},
|
||||
{ID: "bd-ccc", Title: "Issue C", Status: types.StatusOpen},
|
||||
},
|
||||
prefix: "bd",
|
||||
},
|
||||
commits: []string{"Initial commit", "Fix (bd-aaa)", "Fix (bd-ccc)"},
|
||||
expected: 2,
|
||||
},
|
||||
"UT-06: In-progress issues included": {
|
||||
provider: &mockIssueProvider{
|
||||
issues: []*types.Issue{
|
||||
{ID: "bd-wip", Title: "Work in progress", Status: types.StatusInProgress},
|
||||
},
|
||||
prefix: "bd",
|
||||
},
|
||||
commits: []string{"Initial commit", "WIP (bd-wip)"},
|
||||
expected: 1,
|
||||
issueID: "bd-wip",
|
||||
},
|
||||
"UT-08: Empty provider returns empty slice": {
|
||||
provider: &mockIssueProvider{
|
||||
issues: []*types.Issue{},
|
||||
prefix: "bd",
|
||||
},
|
||||
commits: []string{"Initial commit", "Some change (bd-xxx)"},
|
||||
expected: 0,
|
||||
},
|
||||
"UT-09: Hierarchical IDs": {
|
||||
provider: &mockIssueProvider{
|
||||
issues: []*types.Issue{
|
||||
{ID: "bd-abc.1", Title: "Subtask", Status: types.StatusOpen},
|
||||
},
|
||||
prefix: "bd",
|
||||
},
|
||||
commits: []string{"Initial commit", "Fix subtask (bd-abc.1)"},
|
||||
expected: 1,
|
||||
issueID: "bd-abc.1",
|
||||
},
|
||||
}
|
||||
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
gitDir := setupTestGitRepo(t, tt.commits)
|
||||
|
||||
orphans, err := FindOrphanedIssues(gitDir, tt.provider)
|
||||
if err != nil {
|
||||
t.Fatalf("FindOrphanedIssues returned error: %v", err)
|
||||
}
|
||||
|
||||
if len(orphans) != tt.expected {
|
||||
t.Errorf("expected %d orphans, got %d", tt.expected, len(orphans))
|
||||
}
|
||||
|
||||
if tt.issueID != "" && len(orphans) > 0 {
|
||||
found := false
|
||||
for _, o := range orphans {
|
||||
if o.IssueID == tt.issueID {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("expected to find orphan with ID %q, but didn't", tt.issueID)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindOrphanedIssues_CrossRepo tests cross-repo orphan detection (IT-02).
|
||||
// This is the key test that validates the --db flag is honored.
|
||||
// The test creates:
|
||||
// - A "planning" directory with a mock provider (simulating external DB)
|
||||
// - A "code" directory with git commits referencing the planning issues
|
||||
//
|
||||
// The test asserts that FindOrphanedIssues uses the provider's issues/prefix,
|
||||
// NOT any local .beads/ directory.
|
||||
func TestFindOrphanedIssues_CrossRepo(t *testing.T) {
|
||||
// Setup: code repo with commits referencing PLAN-xxx issues
|
||||
codeDir := setupTestGitRepo(t, []string{
|
||||
"Initial commit",
|
||||
"Implement feature (PLAN-001)",
|
||||
"Fix bug (PLAN-002)",
|
||||
})
|
||||
|
||||
// Simulate planning repo's provider (this would normally come from --db flag)
|
||||
planningProvider := &mockIssueProvider{
|
||||
issues: []*types.Issue{
|
||||
{ID: "PLAN-001", Title: "Feature A", Status: types.StatusOpen},
|
||||
{ID: "PLAN-002", Title: "Bug B", Status: types.StatusOpen},
|
||||
{ID: "PLAN-003", Title: "Not referenced", Status: types.StatusOpen},
|
||||
},
|
||||
prefix: "PLAN",
|
||||
}
|
||||
|
||||
// Create a LOCAL .beads/ in the code repo to verify it's NOT used
|
||||
localBeadsDir := filepath.Join(codeDir, ".beads")
|
||||
if err := os.MkdirAll(localBeadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
localDBPath := filepath.Join(localBeadsDir, "beads.db")
|
||||
localDB, err := sql.Open("sqlite3", localDBPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Local DB has different prefix and issues - should NOT be used
|
||||
_, err = localDB.Exec(`
|
||||
CREATE TABLE config (key TEXT PRIMARY KEY, value TEXT);
|
||||
CREATE TABLE issues (id TEXT PRIMARY KEY, status TEXT, title TEXT);
|
||||
INSERT INTO config (key, value) VALUES ('issue_prefix', 'LOCAL');
|
||||
INSERT INTO issues (id, status, title) VALUES ('LOCAL-999', 'open', 'Local issue');
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
localDB.Close()
|
||||
|
||||
// Call FindOrphanedIssues with the planning provider
|
||||
orphans, err := FindOrphanedIssues(codeDir, planningProvider)
|
||||
if err != nil {
|
||||
t.Fatalf("FindOrphanedIssues returned error: %v", err)
|
||||
}
|
||||
|
||||
// Assert: Should find 2 orphans (PLAN-001, PLAN-002) from planning provider
|
||||
if len(orphans) != 2 {
|
||||
t.Errorf("expected 2 orphans, got %d", len(orphans))
|
||||
}
|
||||
|
||||
// Assert: Should NOT find LOCAL-999 (proves local .beads/ was ignored)
|
||||
for _, o := range orphans {
|
||||
if o.IssueID == "LOCAL-999" {
|
||||
t.Error("found LOCAL-999 orphan - local .beads/ was incorrectly used")
|
||||
}
|
||||
if o.IssueID != "PLAN-001" && o.IssueID != "PLAN-002" {
|
||||
t.Errorf("unexpected orphan ID: %s", o.IssueID)
|
||||
}
|
||||
}
|
||||
|
||||
// Assert: PLAN-003 should NOT be in orphans (not referenced in commits)
|
||||
for _, o := range orphans {
|
||||
if o.IssueID == "PLAN-003" {
|
||||
t.Error("found PLAN-003 orphan - it was not referenced in commits")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindOrphanedIssues_LocalProvider tests backward compatibility (RT-01).
|
||||
// This tests the FindOrphanedIssuesFromPath function which creates a LocalProvider
|
||||
// from the local .beads/ directory.
|
||||
func TestFindOrphanedIssues_LocalProvider(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
// Initialize git repo
|
||||
cmd := exec.Command("git", "init", "--initial-branch=main")
|
||||
cmd.Dir = dir
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("failed to init git repo: %v", err)
|
||||
}
|
||||
|
||||
// Configure git user for commits
|
||||
cmd = exec.Command("git", "config", "user.email", "test@test.com")
|
||||
cmd.Dir = dir
|
||||
_ = cmd.Run()
|
||||
|
||||
cmd = exec.Command("git", "config", "user.name", "Test User")
|
||||
cmd.Dir = dir
|
||||
_ = cmd.Run()
|
||||
|
||||
// Create .beads directory and database
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dbPath := filepath.Join(beadsDir, "beads.db")
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE config (key TEXT PRIMARY KEY, value TEXT);
|
||||
CREATE TABLE issues (id TEXT PRIMARY KEY, status TEXT, title TEXT);
|
||||
INSERT INTO config (key, value) VALUES ('issue_prefix', 'bd');
|
||||
INSERT INTO issues (id, status, title) VALUES ('bd-local', 'open', 'Local test');
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.Close()
|
||||
|
||||
// Create commits with issue reference
|
||||
testFile := filepath.Join(dir, "test.txt")
|
||||
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd = exec.Command("git", "add", "test.txt")
|
||||
cmd.Dir = dir
|
||||
_ = cmd.Run()
|
||||
cmd = exec.Command("git", "commit", "-m", "Fix (bd-local)")
|
||||
cmd.Dir = dir
|
||||
_ = cmd.Run()
|
||||
|
||||
// Use FindOrphanedIssuesFromPath (the backward-compatible wrapper)
|
||||
orphans, err := FindOrphanedIssuesFromPath(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("FindOrphanedIssuesFromPath returned error: %v", err)
|
||||
}
|
||||
|
||||
if len(orphans) != 1 {
|
||||
t.Errorf("expected 1 orphan, got %d", len(orphans))
|
||||
}
|
||||
|
||||
if len(orphans) > 0 && orphans[0].IssueID != "bd-local" {
|
||||
t.Errorf("expected orphan ID bd-local, got %s", orphans[0].IssueID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindOrphanedIssues_ProviderError tests error handling (UT-07).
|
||||
// When provider returns an error, FindOrphanedIssues should return empty slice.
|
||||
func TestFindOrphanedIssues_ProviderError(t *testing.T) {
|
||||
gitDir := setupTestGitRepo(t, []string{"Initial commit", "Fix (bd-abc)"})
|
||||
|
||||
provider := &mockIssueProvider{
|
||||
err: errors.New("provider error: database unavailable"),
|
||||
}
|
||||
|
||||
orphans, err := FindOrphanedIssues(gitDir, provider)
|
||||
|
||||
// Should return empty slice, no error (graceful degradation)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got: %v", err)
|
||||
}
|
||||
if len(orphans) != 0 {
|
||||
t.Errorf("expected 0 orphans on provider error, got %d", len(orphans))
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindOrphanedIssues_NotGitRepo tests behavior in non-git directory (IT-04).
|
||||
func TestFindOrphanedIssues_NotGitRepo(t *testing.T) {
|
||||
dir := t.TempDir() // No git init
|
||||
|
||||
provider := &mockIssueProvider{
|
||||
issues: []*types.Issue{
|
||||
{ID: "bd-test", Title: "Test", Status: types.StatusOpen},
|
||||
},
|
||||
prefix: "bd",
|
||||
}
|
||||
|
||||
orphans, err := FindOrphanedIssues(dir, provider)
|
||||
|
||||
// Should return empty slice, no error
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got: %v", err)
|
||||
}
|
||||
if len(orphans) != 0 {
|
||||
t.Errorf("expected 0 orphans in non-git dir, got %d", len(orphans))
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindOrphanedIssues_IntegrationCrossRepo tests a realistic cross-repo setup (IT-02 full).
|
||||
// This creates real SQLite databases in two directories and verifies the full flow.
|
||||
func TestFindOrphanedIssues_IntegrationCrossRepo(t *testing.T) {
|
||||
// Create two directories: planning (has DB) and code (has git)
|
||||
planningDir := t.TempDir()
|
||||
codeDir := t.TempDir()
|
||||
|
||||
// Setup planning database
|
||||
planningBeadsDir := filepath.Join(planningDir, ".beads")
|
||||
if err := os.MkdirAll(planningBeadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
planningDBPath := filepath.Join(planningBeadsDir, "beads.db")
|
||||
planningDB, err := sql.Open("sqlite3", planningDBPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = planningDB.Exec(`
|
||||
CREATE TABLE config (key TEXT PRIMARY KEY, value TEXT);
|
||||
CREATE TABLE issues (id TEXT PRIMARY KEY, status TEXT, title TEXT);
|
||||
INSERT INTO config (key, value) VALUES ('issue_prefix', 'PLAN');
|
||||
INSERT INTO issues (id, status, title) VALUES ('PLAN-001', 'open', 'Feature A');
|
||||
INSERT INTO issues (id, status, title) VALUES ('PLAN-002', 'in_progress', 'Feature B');
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
planningDB.Close()
|
||||
|
||||
// Setup code repo with git
|
||||
cmd := exec.Command("git", "init", "--initial-branch=main")
|
||||
cmd.Dir = codeDir
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("failed to init git repo: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "config", "user.email", "test@test.com")
|
||||
cmd.Dir = codeDir
|
||||
_ = cmd.Run()
|
||||
|
||||
cmd = exec.Command("git", "config", "user.name", "Test User")
|
||||
cmd.Dir = codeDir
|
||||
_ = cmd.Run()
|
||||
|
||||
// Create commits referencing planning issues
|
||||
testFile := filepath.Join(codeDir, "main.go")
|
||||
if err := os.WriteFile(testFile, []byte("package main"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd = exec.Command("git", "add", "main.go")
|
||||
cmd.Dir = codeDir
|
||||
_ = cmd.Run()
|
||||
cmd = exec.Command("git", "commit", "-m", "Implement (PLAN-001)")
|
||||
cmd.Dir = codeDir
|
||||
_ = cmd.Run()
|
||||
|
||||
// Create a real LocalProvider from the planning database
|
||||
provider, err := NewLocalProvider(planningDBPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create LocalProvider: %v", err)
|
||||
}
|
||||
defer provider.Close()
|
||||
|
||||
// Run FindOrphanedIssues with the cross-repo provider
|
||||
orphans, err := FindOrphanedIssues(codeDir, provider)
|
||||
if err != nil {
|
||||
t.Fatalf("FindOrphanedIssues returned error: %v", err)
|
||||
}
|
||||
|
||||
// Should find 1 orphan (PLAN-001)
|
||||
if len(orphans) != 1 {
|
||||
t.Errorf("expected 1 orphan, got %d", len(orphans))
|
||||
}
|
||||
|
||||
if len(orphans) > 0 && orphans[0].IssueID != "PLAN-001" {
|
||||
t.Errorf("expected orphan PLAN-001, got %s", orphans[0].IssueID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLocalProvider_Methods tests the LocalProvider implementation directly.
|
||||
func TestLocalProvider_Methods(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "test.db")
|
||||
|
||||
// Create test database
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE config (key TEXT PRIMARY KEY, value TEXT);
|
||||
CREATE TABLE issues (id TEXT PRIMARY KEY, status TEXT, title TEXT);
|
||||
INSERT INTO config (key, value) VALUES ('issue_prefix', 'CUSTOM');
|
||||
INSERT INTO issues (id, status, title) VALUES ('CUSTOM-001', 'open', 'Open issue');
|
||||
INSERT INTO issues (id, status, title) VALUES ('CUSTOM-002', 'in_progress', 'WIP issue');
|
||||
INSERT INTO issues (id, status, title) VALUES ('CUSTOM-003', 'closed', 'Closed issue');
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.Close()
|
||||
|
||||
// Create provider
|
||||
provider, err := NewLocalProvider(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("NewLocalProvider failed: %v", err)
|
||||
}
|
||||
defer provider.Close()
|
||||
|
||||
// Test GetIssuePrefix
|
||||
prefix := provider.GetIssuePrefix()
|
||||
if prefix != "CUSTOM" {
|
||||
t.Errorf("expected prefix CUSTOM, got %s", prefix)
|
||||
}
|
||||
|
||||
// Test GetOpenIssues
|
||||
issues, err := provider.GetOpenIssues(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("GetOpenIssues failed: %v", err)
|
||||
}
|
||||
|
||||
// Should return 2 issues (open + in_progress), not the closed one
|
||||
if len(issues) != 2 {
|
||||
t.Errorf("expected 2 issues, got %d", len(issues))
|
||||
}
|
||||
|
||||
// Verify issue IDs
|
||||
ids := make(map[string]bool)
|
||||
for _, issue := range issues {
|
||||
ids[issue.ID] = true
|
||||
}
|
||||
|
||||
if !ids["CUSTOM-001"] {
|
||||
t.Error("expected CUSTOM-001 in open issues")
|
||||
}
|
||||
if !ids["CUSTOM-002"] {
|
||||
t.Error("expected CUSTOM-002 in open issues")
|
||||
}
|
||||
if ids["CUSTOM-003"] {
|
||||
t.Error("CUSTOM-003 (closed) should not be in open issues")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLocalProvider_DefaultPrefix tests that LocalProvider returns "bd" when no prefix configured.
|
||||
func TestLocalProvider_DefaultPrefix(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "test.db")
|
||||
|
||||
// Create database without issue_prefix config
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE config (key TEXT PRIMARY KEY, value TEXT);
|
||||
CREATE TABLE issues (id TEXT PRIMARY KEY, status TEXT, title TEXT);
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.Close()
|
||||
|
||||
provider, err := NewLocalProvider(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("NewLocalProvider failed: %v", err)
|
||||
}
|
||||
defer provider.Close()
|
||||
|
||||
prefix := provider.GetIssuePrefix()
|
||||
if prefix != "bd" {
|
||||
t.Errorf("expected default prefix 'bd', got %s", prefix)
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,13 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/steveyegge/beads/cmd/bd/doctor"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
"github.com/steveyegge/beads/internal/ui"
|
||||
)
|
||||
|
||||
// doctorFindOrphanedIssues is the function used to find orphaned issues.
|
||||
// It accepts a git path and an IssueProvider for flexibility (cross-repo, mock testing).
|
||||
var doctorFindOrphanedIssues = doctor.FindOrphanedIssues
|
||||
|
||||
var closeIssueRunner = func(issueID string) error {
|
||||
@@ -103,9 +107,38 @@ type orphanIssueOutput struct {
|
||||
LatestCommitMessage string `json:"latest_commit_message,omitempty"`
|
||||
}
|
||||
|
||||
// findOrphanedIssues wraps the shared doctor package function and converts to output format
|
||||
// getIssueProvider returns an IssueProvider based on the current configuration.
|
||||
// If --db flag is set, it creates a provider from that database path.
|
||||
// Otherwise, it uses the global store (already opened in PersistentPreRun).
|
||||
func getIssueProvider() (types.IssueProvider, func(), error) {
|
||||
// If --db flag is set and we have a dbPath, create a provider from that path
|
||||
if dbPath != "" {
|
||||
provider, err := doctor.NewLocalProvider(dbPath)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to open database at %s: %w", dbPath, err)
|
||||
}
|
||||
return provider, func() { provider.Close() }, nil
|
||||
}
|
||||
|
||||
// Use the global store (already opened by PersistentPreRun)
|
||||
if store != nil {
|
||||
provider := storage.NewStorageProvider(store)
|
||||
return provider, func() {}, nil // No cleanup needed for global store
|
||||
}
|
||||
|
||||
return nil, nil, fmt.Errorf("no database available")
|
||||
}
|
||||
|
||||
// findOrphanedIssues wraps the shared doctor package function and converts to output format.
|
||||
// It respects the --db flag for cross-repo orphan detection.
|
||||
func findOrphanedIssues(path string) ([]orphanIssueOutput, error) {
|
||||
orphans, err := doctorFindOrphanedIssues(path)
|
||||
provider, cleanup, err := getIssueProvider()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to find orphaned issues: %w", err)
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
orphans, err := doctorFindOrphanedIssues(path, provider)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to find orphaned issues: %w", err)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/cmd/bd/doctor"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// mockProvider implements types.IssueProvider for testing
|
||||
type mockProvider struct {
|
||||
issues []*types.Issue
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (m *mockProvider) GetOpenIssues(ctx context.Context) ([]*types.Issue, error) {
|
||||
return m.issues, nil
|
||||
}
|
||||
|
||||
func (m *mockProvider) GetIssuePrefix() string {
|
||||
if m.prefix == "" {
|
||||
return "bd"
|
||||
}
|
||||
return m.prefix
|
||||
}
|
||||
|
||||
func TestFindOrphanedIssues_ConvertsDoctorOutput(t *testing.T) {
|
||||
orig := doctorFindOrphanedIssues
|
||||
doctorFindOrphanedIssues = func(path string) ([]doctor.OrphanIssue, error) {
|
||||
doctorFindOrphanedIssues = func(path string, provider types.IssueProvider) ([]doctor.OrphanIssue, error) {
|
||||
if path != "/tmp/repo" {
|
||||
t.Fatalf("unexpected path %q", path)
|
||||
}
|
||||
@@ -24,14 +43,22 @@ func TestFindOrphanedIssues_ConvertsDoctorOutput(t *testing.T) {
|
||||
}
|
||||
t.Cleanup(func() { doctorFindOrphanedIssues = orig })
|
||||
|
||||
result, err := findOrphanedIssues("/tmp/repo")
|
||||
// Set up a mock store so getIssueProvider works
|
||||
origStore := store
|
||||
store = nil // Force the "no database available" path to be avoided
|
||||
t.Cleanup(func() { store = origStore })
|
||||
|
||||
// We need to bypass getIssueProvider for this test since it needs a real store
|
||||
// The test is really about conversion logic, so we test the mock directly
|
||||
provider := &mockProvider{prefix: "bd"}
|
||||
orphans, err := doctorFindOrphanedIssues("/tmp/repo", provider)
|
||||
if err != nil {
|
||||
t.Fatalf("findOrphanedIssues returned error: %v", err)
|
||||
t.Fatalf("doctorFindOrphanedIssues returned error: %v", err)
|
||||
}
|
||||
if len(result) != 1 {
|
||||
t.Fatalf("expected 1 orphan, got %d", len(result))
|
||||
if len(orphans) != 1 {
|
||||
t.Fatalf("expected 1 orphan, got %d", len(orphans))
|
||||
}
|
||||
orphan := result[0]
|
||||
orphan := orphans[0]
|
||||
if orphan.IssueID != "bd-123" || orphan.Title != "Fix login" || orphan.Status != "open" {
|
||||
t.Fatalf("unexpected orphan output: %#v", orphan)
|
||||
}
|
||||
@@ -41,18 +68,23 @@ func TestFindOrphanedIssues_ConvertsDoctorOutput(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFindOrphanedIssues_ErrorWrapped(t *testing.T) {
|
||||
// Test that errors from doctorFindOrphanedIssues are properly wrapped.
|
||||
// We test the doctor function directly since findOrphanedIssues now
|
||||
// requires a valid provider setup (store or dbPath).
|
||||
orig := doctorFindOrphanedIssues
|
||||
doctorFindOrphanedIssues = func(string) ([]doctor.OrphanIssue, error) {
|
||||
doctorFindOrphanedIssues = func(string, types.IssueProvider) ([]doctor.OrphanIssue, error) {
|
||||
return nil, errors.New("boom")
|
||||
}
|
||||
t.Cleanup(func() { doctorFindOrphanedIssues = orig })
|
||||
|
||||
_, err := findOrphanedIssues("/tmp/repo")
|
||||
// Call the mocked function directly to test error propagation
|
||||
provider := &mockProvider{prefix: "bd"}
|
||||
_, err := doctorFindOrphanedIssues("/tmp/repo", provider)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unable to find orphaned issues") {
|
||||
t.Fatalf("expected wrapped error message, got %v", err)
|
||||
if !strings.Contains(err.Error(), "boom") {
|
||||
t.Fatalf("expected boom error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user