Files
beads/cmd/bd/import_bug_test.go
Steve Yegge 1e2e066dc4 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>
2025-10-27 20:00:49 -07:00

92 lines
2.4 KiB
Go

package main
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestImportReturnsCorrectCounts reproduces bd-88
// Import should report correct "created" count when importing new issues
func TestImportReturnsCorrectCounts(t *testing.T) {
// Create temporary database
tmpDir, err := os.MkdirTemp("", "beads-test-")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
dbPath := filepath.Join(tmpDir, ".beads", "issues.db")
// Initialize database
store := newTestStore(t, dbPath)
ctx := context.Background()
// Create test issues to import
issues := make([]*types.Issue, 0, 5)
for i := 1; i <= 5; i++ {
id := fmt.Sprintf("test-%d", i)
issues = append(issues, &types.Issue{
ID: id,
Title: fmt.Sprintf("Test Issue %d", i),
Description: "Test description",
Status: types.StatusOpen,
Priority: 2,
IssueType: types.TypeTask,
})
}
// Import with default options
opts := ImportOptions{
ResolveCollisions: false,
DryRun: false,
SkipUpdate: false,
Strict: false,
}
result, err := importIssuesCore(ctx, dbPath, store, issues, opts)
if err != nil {
t.Fatalf("Import failed: %v", err)
}
// Check that Created count matches
if result.Created != len(issues) {
t.Errorf("Expected Created=%d, got %d", len(issues), result.Created)
}
// Verify issues are actually in the database
for _, issue := range issues {
retrieved, err := store.GetIssue(ctx, issue.ID)
if err != nil {
t.Errorf("Failed to get issue %s: %v", issue.ID, err)
}
if retrieved == nil {
t.Errorf("Issue %s not found in database", issue.ID)
}
}
// Now test re-importing the same issues (idempotent case)
result2, err := importIssuesCore(ctx, dbPath, store, issues, opts)
if err != nil {
t.Fatalf("Second import failed: %v", err)
}
// bd-88: When reimporting unchanged issues, should report them as "Unchanged"
if result2.Created != 0 {
t.Errorf("Second import: expected Created=0, got %d", result2.Created)
}
if result2.Updated != 0 {
t.Errorf("Second import: expected Updated=0, got %d", result2.Updated)
}
if result2.Unchanged != len(issues) {
t.Errorf("Second import: expected Unchanged=%d, got %d", len(issues), result2.Unchanged)
}
t.Logf("Second import: Created=%d, Updated=%d, Unchanged=%d, Skipped=%d",
result2.Created, result2.Updated, result2.Unchanged, result2.Skipped)
}