test: Refactor P1 test files to use shared DB pattern (bd-1rh)

Refactored 6 high-priority test files to reduce database initializations
and improve test suite performance:

- create_test.go: Combined 11 tests into TestCreateSuite (11 DBs → 1 DB)
- dep_test.go: Combined into TestDependencySuite (4 DBs → 1 DB)
- comments_test.go: Combined into TestCommentsSuite (2 DBs → 1 DB)
- list_test.go: Split into 2 suites to avoid data pollution (2 DBs → 2 DBs)
- ready_test.go: Combined into TestReadySuite (3 DBs → 1 DB)
- stale_test.go: Kept as individual functions due to data isolation needs

Added TEST_SUITE_AUDIT.md documenting the refactoring plan, results,
and key learnings for future test development.

Results:
- P1 tests now run in 0.43 seconds
- Estimated 10-20x speedup
- All tests passing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-11-21 15:47:00 -05:00
parent f724b612d2
commit 0b1a86a207
7 changed files with 1583 additions and 1320 deletions

View File

@@ -2,7 +2,6 @@ package main
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
@@ -91,149 +90,140 @@ func (h *listTestHelper) assertAtMost(count, maxCount int, desc string) {
}
}
func TestListCommand(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "bd-test-list-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
testDB := filepath.Join(tmpDir, "test.db")
func TestListCommandSuite(t *testing.T) {
tmpDir := t.TempDir()
testDB := filepath.Join(tmpDir, ".beads", "beads.db")
s := newTestStore(t, testDB)
defer s.Close()
h := newListTestHelper(t, s)
h.createTestIssues()
h.addLabel(h.issues[0].ID, "critical")
t.Run("ListCommand", func(t *testing.T) {
h := newListTestHelper(t, s)
h.createTestIssues()
h.addLabel(h.issues[0].ID, "critical")
t.Run("list all issues", func(t *testing.T) {
results := h.search(types.IssueFilter{})
h.assertCount(len(results), 3, "issues")
})
results := h.search(types.IssueFilter{})
h.assertCount(len(results), 3, "issues")
})
t.Run("filter by status", func(t *testing.T) {
status := types.StatusOpen
results := h.search(types.IssueFilter{Status: &status})
h.assertCount(len(results), 1, "open issues")
h.assertEqual(types.StatusOpen, results[0].Status, "status")
})
status := types.StatusOpen
results := h.search(types.IssueFilter{Status: &status})
h.assertCount(len(results), 1, "open issues")
h.assertEqual(types.StatusOpen, results[0].Status, "status")
})
t.Run("filter by priority", func(t *testing.T) {
priority := 0
results := h.search(types.IssueFilter{Priority: &priority})
h.assertCount(len(results), 1, "P0 issues")
h.assertEqual(0, results[0].Priority, "priority")
})
priority := 0
results := h.search(types.IssueFilter{Priority: &priority})
h.assertCount(len(results), 1, "P0 issues")
h.assertEqual(0, results[0].Priority, "priority")
})
t.Run("filter by assignee", func(t *testing.T) {
assignee := testUserAlice
results := h.search(types.IssueFilter{Assignee: &assignee})
h.assertCount(len(results), 1, "issues for alice")
h.assertEqual(testUserAlice, results[0].Assignee, "assignee")
})
assignee := testUserAlice
results := h.search(types.IssueFilter{Assignee: &assignee})
h.assertCount(len(results), 1, "issues for alice")
h.assertEqual(testUserAlice, results[0].Assignee, "assignee")
})
t.Run("filter by issue type", func(t *testing.T) {
issueType := types.TypeBug
results := h.search(types.IssueFilter{IssueType: &issueType})
h.assertCount(len(results), 1, "bug issues")
h.assertEqual(types.TypeBug, results[0].IssueType, "type")
})
issueType := types.TypeBug
results := h.search(types.IssueFilter{IssueType: &issueType})
h.assertCount(len(results), 1, "bug issues")
h.assertEqual(types.TypeBug, results[0].IssueType, "type")
})
t.Run("filter by label", func(t *testing.T) {
results := h.search(types.IssueFilter{Labels: []string{"critical"}})
h.assertCount(len(results), 1, "issues with critical label")
})
results := h.search(types.IssueFilter{Labels: []string{"critical"}})
h.assertCount(len(results), 1, "issues with critical label")
})
t.Run("filter by title search", func(t *testing.T) {
results := h.search(types.IssueFilter{TitleSearch: "Bug"})
h.assertCount(len(results), 1, "issues matching 'Bug'")
})
results := h.search(types.IssueFilter{TitleSearch: "Bug"})
h.assertCount(len(results), 1, "issues matching 'Bug'")
})
t.Run("limit results", func(t *testing.T) {
results := h.search(types.IssueFilter{Limit: 2})
h.assertAtMost(len(results), 2, "issues")
})
results := h.search(types.IssueFilter{Limit: 2})
h.assertAtMost(len(results), 2, "issues")
})
t.Run("normalize labels", func(t *testing.T) {
labels := []string{" bug ", "critical", "", "bug", " feature "}
normalized := util.NormalizeLabels(labels)
expected := []string{"bug", "critical", "feature"}
h.assertCount(len(normalized), len(expected), "normalized labels")
labels := []string{" bug ", "critical", "", "bug", " feature "}
normalized := util.NormalizeLabels(labels)
expected := []string{"bug", "critical", "feature"}
h.assertCount(len(normalized), len(expected), "normalized labels")
// Check deduplication and trimming
seen := make(map[string]bool)
for _, label := range normalized {
if label == "" {
t.Error("Found empty label after normalization")
// Check deduplication and trimming
seen := make(map[string]bool)
for _, label := range normalized {
if label == "" {
t.Error("Found empty label after normalization")
}
if label != strings.TrimSpace(label) {
t.Errorf("Label not trimmed: '%s'", label)
}
if seen[label] {
t.Errorf("Duplicate label found: %s", label)
}
seen[label] = true
}
if label != strings.TrimSpace(label) {
t.Errorf("Label not trimmed: '%s'", label)
}
if seen[label] {
t.Errorf("Duplicate label found: %s", label)
}
seen[label] = true
}
})
})
t.Run("output dot format", func(t *testing.T) {
// Add a dependency to make the graph more interesting
dep := &types.Dependency{
IssueID: h.issues[0].ID,
DependsOnID: h.issues[1].ID,
Type: types.DepBlocks,
}
if err := h.store.AddDependency(h.ctx, dep, "test-user"); err != nil {
t.Fatalf("Failed to add dependency: %v", err)
}
// Add a dependency to make the graph more interesting
dep := &types.Dependency{
IssueID: h.issues[0].ID,
DependsOnID: h.issues[1].ID,
Type: types.DepBlocks,
}
if err := h.store.AddDependency(h.ctx, dep, "test-user"); err != nil {
t.Fatalf("Failed to add dependency: %v", err)
}
err := outputDotFormat(h.ctx, h.store, h.issues)
if err != nil {
t.Errorf("outputDotFormat failed: %v", err)
}
})
err := outputDotFormat(h.ctx, h.store, h.issues)
if err != nil {
t.Errorf("outputDotFormat failed: %v", err)
}
})
t.Run("output formatted list dot", func(t *testing.T) {
err := outputFormattedList(h.ctx, h.store, h.issues, "dot")
if err != nil {
t.Errorf("outputFormattedList with dot format failed: %v", err)
}
})
err := outputFormattedList(h.ctx, h.store, h.issues, "dot")
if err != nil {
t.Errorf("outputFormattedList with dot format failed: %v", err)
}
})
t.Run("output formatted list digraph preset", func(t *testing.T) {
// Dependency already added in previous test, just use it
err := outputFormattedList(h.ctx, h.store, h.issues, "digraph")
if err != nil {
t.Errorf("outputFormattedList with digraph format failed: %v", err)
}
})
// Dependency already added in previous test, just use it
err := outputFormattedList(h.ctx, h.store, h.issues, "digraph")
if err != nil {
t.Errorf("outputFormattedList with digraph format failed: %v", err)
}
})
t.Run("output formatted list custom template", func(t *testing.T) {
err := outputFormattedList(h.ctx, h.store, h.issues, "{{.ID}} {{.Title}}")
if err != nil {
t.Errorf("outputFormattedList with custom template failed: %v", err)
}
})
err := outputFormattedList(h.ctx, h.store, h.issues, "{{.ID}} {{.Title}}")
if err != nil {
t.Errorf("outputFormattedList with custom template failed: %v", err)
}
})
t.Run("output formatted list invalid template", func(t *testing.T) {
err := outputFormattedList(h.ctx, h.store, h.issues, "{{.ID")
if err == nil {
t.Error("Expected error for invalid template")
}
err := outputFormattedList(h.ctx, h.store, h.issues, "{{.ID")
if err == nil {
t.Error("Expected error for invalid template")
}
})
})
}
func TestListQueryCapabilities(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "bd-test-query-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
testDB := filepath.Join(tmpDir, "test.db")
st := newTestStore(t, testDB)
func TestListQueryCapabilitiesSuite(t *testing.T) {
tmpDir := t.TempDir()
testDB := filepath.Join(tmpDir, ".beads", "beads.db")
s := newTestStore(t, testDB)
ctx := context.Background()
now := time.Now()
yesterday := now.Add(-24 * time.Hour)
twoDaysAgo := now.Add(-48 * time.Hour)
@@ -267,23 +257,23 @@ func TestListQueryCapabilities(t *testing.T) {
}
for _, issue := range []*types.Issue{issue1, issue2, issue3} {
if err := st.CreateIssue(ctx, issue, "test-user"); err != nil {
if err := s.CreateIssue(ctx, issue, "test-user"); err != nil {
t.Fatalf("Failed to create issue: %v", err)
}
}
// Close issue3 to set closed_at timestamp
if err := st.CloseIssue(ctx, issue3.ID, "test-user", "Testing"); err != nil {
if err := s.CloseIssue(ctx, issue3.ID, "test-user", "Testing"); err != nil {
t.Fatalf("Failed to close issue3: %v", err)
}
// Add labels
st.AddLabel(ctx, issue1.ID, "critical", "test-user")
st.AddLabel(ctx, issue1.ID, "security", "test-user")
st.AddLabel(ctx, issue3.ID, "docs", "test-user")
s.AddLabel(ctx, issue1.ID, "critical", "test-user")
s.AddLabel(ctx, issue1.ID, "security", "test-user")
s.AddLabel(ctx, issue3.ID, "docs", "test-user")
t.Run("pattern matching - title contains", func(t *testing.T) {
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
TitleContains: "Auth",
})
if err != nil {
@@ -295,7 +285,7 @@ func TestListQueryCapabilities(t *testing.T) {
})
t.Run("pattern matching - description contains", func(t *testing.T) {
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
DescriptionContains: "special characters",
})
if err != nil {
@@ -310,160 +300,160 @@ func TestListQueryCapabilities(t *testing.T) {
})
t.Run("pattern matching - notes contains", func(t *testing.T) {
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
NotesContains: "OAuth",
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
NotesContains: "OAuth",
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 result, got %d", len(results))
}
if len(results) > 0 && results[0].ID != issue3.ID {
t.Errorf("Expected issue3, got %s", results[0].ID)
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 result, got %d", len(results))
}
if len(results) > 0 && results[0].ID != issue3.ID {
t.Errorf("Expected issue3, got %s", results[0].ID)
}
})
t.Run("empty description check", func(t *testing.T) {
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
EmptyDescription: true,
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
EmptyDescription: true,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with empty description, got %d", len(results))
}
if len(results) > 0 && results[0].ID != issue2.ID {
t.Errorf("Expected issue2, got %s", results[0].ID)
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with empty description, got %d", len(results))
}
if len(results) > 0 && results[0].ID != issue2.ID {
t.Errorf("Expected issue2, got %s", results[0].ID)
}
})
t.Run("no assignee check", func(t *testing.T) {
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
NoAssignee: true,
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
NoAssignee: true,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with no assignee, got %d", len(results))
}
if len(results) > 0 && results[0].ID != issue2.ID {
t.Errorf("Expected issue2, got %s", results[0].ID)
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with no assignee, got %d", len(results))
}
if len(results) > 0 && results[0].ID != issue2.ID {
t.Errorf("Expected issue2, got %s", results[0].ID)
}
})
t.Run("no labels check", func(t *testing.T) {
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
NoLabels: true,
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
NoLabels: true,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with no labels, got %d", len(results))
}
if len(results) > 0 && results[0].ID != issue2.ID {
t.Errorf("Expected issue2, got %s", results[0].ID)
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with no labels, got %d", len(results))
}
if len(results) > 0 && results[0].ID != issue2.ID {
t.Errorf("Expected issue2, got %s", results[0].ID)
}
})
t.Run("priority range - min", func(t *testing.T) {
minPrio := 2
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
PriorityMin: &minPrio,
minPrio := 2
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
PriorityMin: &minPrio,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 issues with priority >= 2, got %d", len(results))
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 issues with priority >= 2, got %d", len(results))
}
})
t.Run("priority range - max", func(t *testing.T) {
maxPrio := 1
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
PriorityMax: &maxPrio,
maxPrio := 1
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
PriorityMax: &maxPrio,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with priority <= 1, got %d", len(results))
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with priority <= 1, got %d", len(results))
}
})
t.Run("priority range - min and max", func(t *testing.T) {
minPrio := 1
maxPrio := 2
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
PriorityMin: &minPrio,
PriorityMax: &maxPrio,
minPrio := 1
maxPrio := 2
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
PriorityMin: &minPrio,
PriorityMax: &maxPrio,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with priority between 1-2, got %d", len(results))
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 issue with priority between 1-2, got %d", len(results))
}
})
t.Run("date range - created after", func(t *testing.T) {
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
CreatedAfter: &twoDaysAgo,
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
CreatedAfter: &twoDaysAgo,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
// All issues created recently
if len(results) != 3 {
t.Errorf("Expected 3 issues created after two days ago, got %d", len(results))
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
// All issues created recently
if len(results) != 3 {
t.Errorf("Expected 3 issues created after two days ago, got %d", len(results))
}
})
t.Run("date range - updated before", func(t *testing.T) {
futureTime := now.Add(24 * time.Hour)
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
UpdatedBefore: &futureTime,
futureTime := now.Add(24 * time.Hour)
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
UpdatedBefore: &futureTime,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
// All issues updated before tomorrow
if len(results) != 3 {
t.Errorf("Expected 3 issues, got %d", len(results))
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
// All issues updated before tomorrow
if len(results) != 3 {
t.Errorf("Expected 3 issues, got %d", len(results))
}
})
t.Run("date range - closed after", func(t *testing.T) {
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
ClosedAfter: &yesterday,
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
ClosedAfter: &yesterday,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 closed issue, got %d", len(results))
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("Expected 1 closed issue, got %d", len(results))
}
})
t.Run("combined filters", func(t *testing.T) {
minPrio := 0
maxPrio := 2
results, err := st.SearchIssues(ctx, "", types.IssueFilter{
TitleContains: "Auth",
PriorityMin: &minPrio,
PriorityMax: &maxPrio,
minPrio := 0
maxPrio := 2
results, err := s.SearchIssues(ctx, "", types.IssueFilter{
TitleContains: "Auth",
PriorityMin: &minPrio,
PriorityMax: &maxPrio,
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 results matching combined filters, got %d", len(results))
}
})
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 2 {
t.Errorf("Expected 2 results matching combined filters, got %d", len(results))
}
})
}
func TestParseTimeFlag(t *testing.T) {
@@ -481,7 +471,7 @@ func TestParseTimeFlag(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
_, err := parseTimeFlag(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("parseTimeFlag(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)