Fix remaining test database initialization errors (bd-207)

Fixed 38 tests failing with 'database not initialized: issue_prefix config is missing' by replacing manual sqlite.New() calls with test helper functions.

Modified files:
- dep_test.go (4 tests)
- merge_test.go (4 tests)
- export_import_test.go (4 instances)
- import_collision_test.go (10 instances)
- import_bug_test.go (1 instance)
- import_collision_regression_test.go (2 instances)
- import_idempotent_test.go (2 instances)
- init_test.go (4 instances)
- integrity_test.go (3 tests)
- main_test.go (multiple tests)

All database initialization errors are now resolved.
Remaining test failures (2) are unrelated to database initialization.

Amp-Thread-ID: https://ampcode.com/threads/T-a6b09458-b899-49eb-9a62-346fa67f62c7
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-27 20:00:49 -07:00
parent a6ecc87a09
commit 1e2e066dc4
10 changed files with 82 additions and 391 deletions

View File

@@ -3,12 +3,10 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/types"
) )
@@ -16,15 +14,7 @@ func TestDepAdd(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db") dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
if err := os.MkdirAll(filepath.Dir(dbPath), 0755); err != nil { sqliteStore := newTestStore(t, dbPath)
t.Fatal(err)
}
sqliteStore, err := sqlite.New(dbPath)
if err != nil {
t.Fatal(err)
}
defer sqliteStore.Close()
ctx := context.Background() ctx := context.Background()
@@ -85,15 +75,7 @@ func TestDepTypes(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db") dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
if err := os.MkdirAll(filepath.Dir(dbPath), 0755); err != nil { sqliteStore := newTestStore(t, dbPath)
t.Fatal(err)
}
sqliteStore, err := sqlite.New(dbPath)
if err != nil {
t.Fatal(err)
}
defer sqliteStore.Close()
ctx := context.Background() ctx := context.Background()
@@ -142,15 +124,7 @@ func TestDepCycleDetection(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db") dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
if err := os.MkdirAll(filepath.Dir(dbPath), 0755); err != nil { sqliteStore := newTestStore(t, dbPath)
t.Fatal(err)
}
sqliteStore, err := sqlite.New(dbPath)
if err != nil {
t.Fatal(err)
}
defer sqliteStore.Close()
ctx := context.Background() ctx := context.Background()
@@ -235,15 +209,7 @@ func TestDepRemove(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db") dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
if err := os.MkdirAll(filepath.Dir(dbPath), 0755); err != nil { sqliteStore := newTestStore(t, dbPath)
t.Fatal(err)
}
sqliteStore, err := sqlite.New(dbPath)
if err != nil {
t.Fatal(err)
}
defer sqliteStore.Close()
ctx := context.Background() ctx := context.Background()

View File

@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@@ -134,22 +133,9 @@ func (h *exportImportHelper) validateJSONLines(buf *bytes.Buffer, expectedCount
} }
func TestExportImport(t *testing.T) { func TestExportImport(t *testing.T) {
// Create temp directory for test database tmpDir := t.TempDir()
tmpDir, err := os.MkdirTemp("", "bd-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Logf("Warning: cleanup failed: %v", err)
}
}()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
store, err := sqlite.New(dbPath) store := newTestStoreWithPrefix(t, dbPath, "test")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
h := newExportImportHelper(t, store) h := newExportImportHelper(t, store)
now := time.Now() now := time.Now()
@@ -177,10 +163,7 @@ func TestExportImport(t *testing.T) {
t.Run("Import", func(t *testing.T) { t.Run("Import", func(t *testing.T) {
exported := h.searchIssues(types.IssueFilter{}) exported := h.searchIssues(types.IssueFilter{})
newDBPath := filepath.Join(tmpDir, "import-test.db") newDBPath := filepath.Join(tmpDir, "import-test.db")
newStore, err := sqlite.New(newDBPath) newStore := newTestStoreWithPrefix(t, newDBPath, "test")
if err != nil {
t.Fatalf("Failed to create new storage: %v", err)
}
newHelper := newExportImportHelper(t, newStore) newHelper := newExportImportHelper(t, newStore)
for _, issue := range exported { for _, issue := range exported {
newHelper.createIssue(issue.ID, issue.Title, issue.Description, issue.Status, issue.Priority, issue.IssueType, issue.Assignee, issue.ClosedAt) newHelper.createIssue(issue.ID, issue.Title, issue.Description, issue.Status, issue.Priority, issue.IssueType, issue.Assignee, issue.ClosedAt)
@@ -216,22 +199,9 @@ func TestExportImport(t *testing.T) {
} }
func TestExportEmpty(t *testing.T) { func TestExportEmpty(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "bd-test-*") tmpDir := t.TempDir()
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Logf("Warning: cleanup failed: %v", err)
}
}()
dbPath := filepath.Join(tmpDir, "empty.db") dbPath := filepath.Join(tmpDir, "empty.db")
store, err := sqlite.New(dbPath) store := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
ctx := context.Background() ctx := context.Background()
// Export from empty database // Export from empty database
@@ -263,23 +233,9 @@ func TestImportInvalidJSON(t *testing.T) {
} }
func TestRoundTrip(t *testing.T) { func TestRoundTrip(t *testing.T) {
// Create original database tmpDir := t.TempDir()
tmpDir, err := os.MkdirTemp("", "bd-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Logf("Warning: cleanup failed: %v", err)
}
}()
dbPath := filepath.Join(tmpDir, "original.db") dbPath := filepath.Join(tmpDir, "original.db")
store, err := sqlite.New(dbPath) store := newTestStoreWithPrefix(t, dbPath, "test")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
h := newExportImportHelper(t, store) h := newExportImportHelper(t, store)
original := h.createFullIssue("test-1", 120) original := h.createFullIssue("test-1", 120)

View File

@@ -7,7 +7,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/types"
) )
@@ -22,16 +21,8 @@ func TestImportReturnsCorrectCounts(t *testing.T) {
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
dbPath := filepath.Join(tmpDir, ".beads", "issues.db") dbPath := filepath.Join(tmpDir, ".beads", "issues.db")
if err := os.MkdirAll(filepath.Dir(dbPath), 0755); err != nil {
t.Fatalf("Failed to create .beads dir: %v", err)
}
// Initialize database // Initialize database
store, err := sqlite.New(dbPath) store := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
defer store.Close()
ctx := context.Background() ctx := context.Background()

View File

@@ -32,11 +32,7 @@ func TestRemapCollisionsRemapsImportedNotExisting(t *testing.T) {
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
store, err := sqlite.New(dbPath) store := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("failed to create storage: %v", err)
}
defer store.Close()
ctx := context.Background() ctx := context.Background()
@@ -201,11 +197,7 @@ func TestRemapCollisionsDoesNotUpdateNonexistentDependencies(t *testing.T) {
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
store, err := sqlite.New(dbPath) store := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("failed to create storage: %v", err)
}
defer store.Close()
ctx := context.Background() ctx := context.Background()

View File

@@ -27,15 +27,7 @@ func TestImportSimpleCollision(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -139,15 +131,7 @@ func TestImportMultipleCollisions(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -253,15 +237,7 @@ func TestImportDependencyUpdates(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -399,15 +375,7 @@ func TestImportTextReferenceUpdates(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -546,15 +514,7 @@ func TestImportChainDependencies(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -648,15 +608,7 @@ func TestImportPartialIDMatch(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -771,15 +723,7 @@ func TestImportExactMatch(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -836,15 +780,7 @@ func TestImportMixedScenario(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -922,15 +858,7 @@ func TestImportWithDependenciesInJSONL(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()
@@ -989,15 +917,7 @@ func TestImportCounterSyncAfterHighID(t *testing.T) {
}() }()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer func() {
if err := testStore.Close(); err != nil {
t.Logf("Warning: failed to close store: %v", err)
}
}()
ctx := context.Background() ctx := context.Background()

View File

@@ -8,7 +8,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/types"
) )
@@ -230,11 +229,7 @@ func TestIdempotentImportNoTimestampChurn(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -316,11 +311,7 @@ func TestImportMultipleUnchangedIssues(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()

View File

@@ -7,8 +7,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/steveyegge/beads/internal/storage/sqlite"
) )
func TestInitCommand(t *testing.T) { func TestInitCommand(t *testing.T) {
@@ -150,14 +148,15 @@ func TestInitCommand(t *testing.T) {
} }
// Verify database has correct prefix // Verify database has correct prefix
store, err := sqlite.New(dbPath) // Note: This database was already created by init command, just open it
store, err := openExistingTestDB(t, dbPath)
if err != nil { if err != nil {
t.Fatalf("Failed to open created database: %v", err) t.Fatalf("Failed to open database: %v", err)
} }
defer store.Close() defer store.Close()
ctx := context.Background() ctx := context.Background()
prefix, err := store.GetConfig(ctx, "issue_prefix") prefix, err := store.GetConfig(ctx, "issue_prefix")
if err != nil { if err != nil {
t.Fatalf("Failed to get issue prefix from database: %v", err) t.Fatalf("Failed to get issue prefix from database: %v", err)
} }
@@ -221,9 +220,9 @@ func TestInitAlreadyInitialized(t *testing.T) {
// Verify database still works (always beads.db now) // Verify database still works (always beads.db now)
dbPath := filepath.Join(tmpDir, ".beads", "beads.db") dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
store, err := sqlite.New(dbPath) store, err := openExistingTestDB(t, dbPath)
if err != nil { if err != nil {
t.Fatalf("Failed to open database after re-init: %v", err) t.Fatalf("Failed to open database: %v", err)
} }
defer store.Close() defer store.Close()
@@ -279,7 +278,7 @@ func TestInitWithCustomDBPath(t *testing.T) {
} }
// Verify database works // Verify database works
store, err := sqlite.New(customDBPath) store, err := openExistingTestDB(t, customDBPath)
if err != nil { if err != nil {
t.Fatalf("Failed to open database: %v", err) t.Fatalf("Failed to open database: %v", err)
} }
@@ -320,7 +319,7 @@ func TestInitWithCustomDBPath(t *testing.T) {
} }
// Verify database works // Verify database works
store, err := sqlite.New(envDBPath) store, err := openExistingTestDB(t, envDBPath)
if err != nil { if err != nil {
t.Fatalf("Failed to open database: %v", err) t.Fatalf("Failed to open database: %v", err)
} }

View File

@@ -6,7 +6,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/types"
) )
@@ -20,11 +19,7 @@ func TestValidatePreExport(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create empty database // Create empty database
store, err := sqlite.New(dbPath) store := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
defer store.Close()
// Create non-empty JSONL file // Create non-empty JSONL file
jsonlContent := `{"id":"bd-1","title":"Test","status":"open","priority":1} jsonlContent := `{"id":"bd-1","title":"Test","status":"open","priority":1}
@@ -34,7 +29,7 @@ func TestValidatePreExport(t *testing.T) {
} }
// Should fail validation // Should fail validation
err = validatePreExport(ctx, store, jsonlPath) err := validatePreExport(ctx, store, jsonlPath)
if err == nil { if err == nil {
t.Error("Expected error for empty DB over non-empty JSONL, got nil") t.Error("Expected error for empty DB over non-empty JSONL, got nil")
} }
@@ -47,13 +42,10 @@ func TestValidatePreExport(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create database with issues // Create database with issues
store, err := sqlite.New(dbPath) store := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
defer store.Close()
// Add an issue // Add an issue
ctx := context.Background()
issue := &types.Issue{ issue := &types.Issue{
ID: "bd-1", ID: "bd-1",
Title: "Test", Title: "Test",
@@ -74,7 +66,7 @@ func TestValidatePreExport(t *testing.T) {
} }
// Should pass validation // Should pass validation
err = validatePreExport(ctx, store, jsonlPath) err := validatePreExport(ctx, store, jsonlPath)
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %v", err) t.Errorf("Expected no error, got: %v", err)
} }
@@ -87,16 +79,12 @@ func TestValidatePreExport(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create empty database // Create empty database
store, err := sqlite.New(dbPath) store := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
defer store.Close()
// JSONL doesn't exist // JSONL doesn't exist
// Should pass validation (new repo scenario) // Should pass validation (new repo scenario)
err = validatePreExport(ctx, store, jsonlPath) err := validatePreExport(ctx, store, jsonlPath)
if err != nil { if err != nil {
t.Errorf("Expected no error for empty DB with no JSONL, got: %v", err) t.Errorf("Expected no error for empty DB with no JSONL, got: %v", err)
} }
@@ -109,11 +97,7 @@ func TestValidatePreExport(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create empty database // Create empty database
store, err := sqlite.New(dbPath) store := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
defer store.Close()
// Create corrupt/unreadable JSONL file with content // Create corrupt/unreadable JSONL file with content
corruptContent := `{"id":"bd-1","title":INVALID JSON` corruptContent := `{"id":"bd-1","title":INVALID JSON`
@@ -122,7 +106,7 @@ func TestValidatePreExport(t *testing.T) {
} }
// Should fail validation (can't verify JSONL content, DB is empty, file has content) // Should fail validation (can't verify JSONL content, DB is empty, file has content)
err = validatePreExport(ctx, store, jsonlPath) err := validatePreExport(ctx, store, jsonlPath)
if err == nil { if err == nil {
t.Error("Expected error for empty DB over unreadable non-empty JSONL, got nil") t.Error("Expected error for empty DB over unreadable non-empty JSONL, got nil")
} }
@@ -153,20 +137,15 @@ func TestValidatePostImport(t *testing.T) {
} }
func TestCountDBIssues(t *testing.T) { func TestCountDBIssues(t *testing.T) {
ctx := context.Background()
t.Run("count issues in database", func(t *testing.T) { t.Run("count issues in database", func(t *testing.T) {
// Create temp directory // Create temp directory
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
// Create database // Create database
store, err := sqlite.New(dbPath) store := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
defer store.Close()
ctx := context.Background()
// Initially 0 // Initially 0
count, err := countDBIssues(ctx, store) count, err := countDBIssues(ctx, store)
if err != nil { if err != nil {
@@ -203,20 +182,15 @@ func TestCountDBIssues(t *testing.T) {
} }
func TestCheckOrphanedDeps(t *testing.T) { func TestCheckOrphanedDeps(t *testing.T) {
ctx := context.Background()
t.Run("function executes without error", func(t *testing.T) { t.Run("function executes without error", func(t *testing.T) {
// Create temp directory // Create temp directory
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
// Create database // Create database
store, err := sqlite.New(dbPath) store := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
defer store.Close()
ctx := context.Background()
// Create two issues // Create two issues
issue1 := &types.Issue{ issue1 := &types.Issue{
ID: "bd-1", ID: "bd-1",
@@ -272,12 +246,9 @@ func TestCheckOrphanedDeps(t *testing.T) {
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
// Create database // Create database
store, err := sqlite.New(dbPath) store := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create store: %v", err)
}
defer store.Close()
ctx := context.Background()
// Create two issues // Create two issues
issue1 := &types.Issue{ issue1 := &types.Issue{
ID: "bd-1", ID: "bd-1",

View File

@@ -14,7 +14,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/types"
) )
@@ -103,11 +102,7 @@ func TestAutoFlushDebounce(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -232,10 +227,7 @@ func TestAutoFlushOnExit(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -411,10 +403,7 @@ func TestAutoFlushStoreInactive(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
store = testStore store = testStore
@@ -457,11 +446,7 @@ func TestAutoFlushJSONLContent(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -577,11 +562,7 @@ func TestAutoFlushErrorHandling(t *testing.T) {
dbPath = filepath.Join(tmpDir, "test.db") dbPath = filepath.Join(tmpDir, "test.db")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -686,11 +667,7 @@ func TestAutoImportIfNewer(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -786,11 +763,7 @@ func TestAutoImportDisabled(t *testing.T) {
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create store // Create store
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -863,11 +836,7 @@ func TestAutoImportWithCollision(t *testing.T) {
dbPath = filepath.Join(tmpDir, "test.db") dbPath = filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -942,11 +911,7 @@ func TestAutoImportNoCollision(t *testing.T) {
dbPath = filepath.Join(tmpDir, "test.db") dbPath = filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -1020,11 +985,7 @@ func TestAutoImportMergeConflict(t *testing.T) {
dbPath = filepath.Join(tmpDir, "test.db") dbPath = filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -1104,11 +1065,7 @@ func TestAutoImportClosedAtInvariant(t *testing.T) {
dbPath = filepath.Join(tmpDir, "test.db") dbPath = filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl") jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
testStore, err := sqlite.New(dbPath) testStore := newTestStore(t, dbPath)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
storeMutex.Lock() storeMutex.Lock()
@@ -1167,19 +1124,10 @@ func TestImportOpenToClosedTransition(t *testing.T) {
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
ctx := context.Background() ctx := context.Background()
// Initialize database with prefix
if err := testStore.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
t.Fatalf("Failed to set issue_prefix: %v", err)
}
// Step 1: Create an open issue in the database // Step 1: Create an open issue in the database
openIssue := &types.Issue{ openIssue := &types.Issue{
ID: "bd-transition-1", ID: "bd-transition-1",
@@ -1233,19 +1181,10 @@ func TestImportClosedToOpenTransition(t *testing.T) {
dbPath := filepath.Join(tmpDir, "test.db") dbPath := filepath.Join(tmpDir, "test.db")
testStore, err := sqlite.New(dbPath) testStore := newTestStoreWithPrefix(t, dbPath, "bd")
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
defer testStore.Close()
ctx := context.Background() ctx := context.Background()
// Initialize database with prefix
if err := testStore.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
t.Fatalf("Failed to set issue_prefix: %v", err)
}
// Step 1: Create a closed issue in the database // Step 1: Create a closed issue in the database
closedTime := time.Now() closedTime := time.Now()
closedIssue := &types.Issue{ closedIssue := &types.Issue{

View File

@@ -2,27 +2,17 @@ package main
import ( import (
"context" "context"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/types"
) )
func TestValidateMerge(t *testing.T) { func TestValidateMerge(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbFile := filepath.Join(tmpDir, ".beads", "issues.db") dbFile := filepath.Join(tmpDir, ".beads", "issues.db")
if err := os.MkdirAll(filepath.Dir(dbFile), 0755); err != nil {
t.Fatalf("Failed to create test directory: %v", err) testStore := newTestStoreWithPrefix(t, dbFile, "bd")
}
testStore, err := sqlite.New(dbFile)
if err != nil {
t.Fatalf("Failed to create test storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
ctx := context.Background() ctx := context.Background()
@@ -52,13 +42,13 @@ func TestValidateMerge(t *testing.T) {
Status: types.StatusOpen, Status: types.StatusOpen,
} }
if err := testStore.CreateIssue(ctx, issue1, "test"); err != nil { if err := testStore.CreateIssue(ctx, issue1, "bd"); err != nil {
t.Fatalf("Failed to create issue1: %v", err) t.Fatalf("Failed to create issue1: %v", err)
} }
if err := testStore.CreateIssue(ctx, issue2, "test"); err != nil { if err := testStore.CreateIssue(ctx, issue2, "bd"); err != nil {
t.Fatalf("Failed to create issue2: %v", err) t.Fatalf("Failed to create issue2: %v", err)
} }
if err := testStore.CreateIssue(ctx, issue3, "test"); err != nil { if err := testStore.CreateIssue(ctx, issue3, "bd"); err != nil {
t.Fatalf("Failed to create issue3: %v", err) t.Fatalf("Failed to create issue3: %v", err)
} }
@@ -132,16 +122,8 @@ func TestValidateMerge(t *testing.T) {
func TestValidateMergeMultipleSelfReferences(t *testing.T) { func TestValidateMergeMultipleSelfReferences(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbFile := filepath.Join(tmpDir, ".beads", "issues.db") dbFile := filepath.Join(tmpDir, ".beads", "issues.db")
if err := os.MkdirAll(filepath.Dir(dbFile), 0755); err != nil {
t.Fatalf("Failed to create test directory: %v", err) testStore := newTestStoreWithPrefix(t, dbFile, "bd")
}
testStore, err := sqlite.New(dbFile)
if err != nil {
t.Fatalf("Failed to create test storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
ctx := context.Background() ctx := context.Background()
@@ -154,12 +136,12 @@ func TestValidateMergeMultipleSelfReferences(t *testing.T) {
Status: types.StatusOpen, Status: types.StatusOpen,
} }
if err := testStore.CreateIssue(ctx, issue1, "test"); err != nil { if err := testStore.CreateIssue(ctx, issue1, "bd"); err != nil {
t.Fatalf("Failed to create issue: %v", err) t.Fatalf("Failed to create issue: %v", err)
} }
// Test merging multiple instances of same ID (should catch first one) // Test merging multiple instances of same ID (should catch first one)
err = validateMerge("bd-10", []string{"bd-10", "bd-10"}) err := validateMerge("bd-10", []string{"bd-10", "bd-10"})
if err == nil { if err == nil {
t.Error("validateMerge() expected error for duplicate self-merge, got nil") t.Error("validateMerge() expected error for duplicate self-merge, got nil")
} }
@@ -185,16 +167,8 @@ func containsSubstring(s, substr string) bool {
func TestPerformMergeIdempotent(t *testing.T) { func TestPerformMergeIdempotent(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbFile := filepath.Join(tmpDir, ".beads", "issues.db") dbFile := filepath.Join(tmpDir, ".beads", "issues.db")
if err := os.MkdirAll(filepath.Dir(dbFile), 0755); err != nil {
t.Fatalf("Failed to create test directory: %v", err) testStore := newTestStoreWithPrefix(t, dbFile, "bd")
}
testStore, err := sqlite.New(dbFile)
if err != nil {
t.Fatalf("Failed to create test storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
ctx := context.Background() ctx := context.Background()
@@ -225,7 +199,7 @@ func TestPerformMergeIdempotent(t *testing.T) {
} }
for _, issue := range []*types.Issue{issue1, issue2, issue3} { for _, issue := range []*types.Issue{issue1, issue2, issue3} {
if err := testStore.CreateIssue(ctx, issue, "test"); err != nil { if err := testStore.CreateIssue(ctx, issue, "bd"); err != nil {
t.Fatalf("Failed to create issue %s: %v", issue.ID, err) t.Fatalf("Failed to create issue %s: %v", issue.ID, err)
} }
} }
@@ -239,7 +213,7 @@ func TestPerformMergeIdempotent(t *testing.T) {
IssueType: types.TypeTask, IssueType: types.TypeTask,
Status: types.StatusOpen, Status: types.StatusOpen,
} }
if err := testStore.CreateIssue(ctx, issue4, "test"); err != nil { if err := testStore.CreateIssue(ctx, issue4, "bd"); err != nil {
t.Fatalf("Failed to create issue4: %v", err) t.Fatalf("Failed to create issue4: %v", err)
} }
@@ -305,16 +279,8 @@ func TestPerformMergeIdempotent(t *testing.T) {
func TestPerformMergePartialRetry(t *testing.T) { func TestPerformMergePartialRetry(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbFile := filepath.Join(tmpDir, ".beads", "issues.db") dbFile := filepath.Join(tmpDir, ".beads", "issues.db")
if err := os.MkdirAll(filepath.Dir(dbFile), 0755); err != nil {
t.Fatalf("Failed to create test directory: %v", err) testStore := newTestStoreWithPrefix(t, dbFile, "bd")
}
testStore, err := sqlite.New(dbFile)
if err != nil {
t.Fatalf("Failed to create test storage: %v", err)
}
defer testStore.Close()
store = testStore store = testStore
ctx := context.Background() ctx := context.Background()
@@ -345,13 +311,13 @@ func TestPerformMergePartialRetry(t *testing.T) {
} }
for _, issue := range []*types.Issue{issue1, issue2, issue3} { for _, issue := range []*types.Issue{issue1, issue2, issue3} {
if err := testStore.CreateIssue(ctx, issue, "test"); err != nil { if err := testStore.CreateIssue(ctx, issue, "bd"); err != nil {
t.Fatalf("Failed to create issue %s: %v", issue.ID, err) t.Fatalf("Failed to create issue %s: %v", issue.ID, err)
} }
} }
// Simulate partial failure: manually close one source issue // Simulate partial failure: manually close one source issue
if err := testStore.CloseIssue(ctx, "bd-201", "Manually closed", "test"); err != nil { if err := testStore.CloseIssue(ctx, "bd-201", "Manually closed", "bd"); err != nil {
t.Fatalf("Failed to manually close bd-201: %v", err) t.Fatalf("Failed to manually close bd-201: %v", err)
} }