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

@@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
@@ -12,160 +11,143 @@ import (
const testUserAlice = "alice"
func TestCommentsCommand(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "bd-test-comments-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
testDB := filepath.Join(tmpDir, "test.db")
func TestCommentsSuite(t *testing.T) {
tmpDir := t.TempDir()
testDB := filepath.Join(tmpDir, ".beads", "beads.db")
s := newTestStore(t, testDB)
defer s.Close()
ctx := context.Background()
// Create test issue
issue := &types.Issue{
Title: "Test Issue",
Description: "Test description",
Priority: 1,
IssueType: types.TypeBug,
Status: types.StatusOpen,
}
if err := s.CreateIssue(ctx, issue, "test-user"); err != nil {
t.Fatalf("Failed to create issue: %v", err)
}
t.Run("add comment", func(t *testing.T) {
comment, err := s.AddIssueComment(ctx, issue.ID, testUserAlice, "This is a test comment")
if err != nil {
t.Fatalf("Failed to add comment: %v", err)
t.Run("CommentsCommand", func(t *testing.T) {
// Create test issue
issue := &types.Issue{
Title: "Test Issue",
Description: "Test description",
Priority: 1,
IssueType: types.TypeBug,
Status: types.StatusOpen,
}
if comment.IssueID != issue.ID {
t.Errorf("Expected issue ID %s, got %s", issue.ID, comment.IssueID)
}
if comment.Author != testUserAlice {
t.Errorf("Expected author alice, got %s", comment.Author)
}
if comment.Text != "This is a test comment" {
t.Errorf("Expected text 'This is a test comment', got %s", comment.Text)
if err := s.CreateIssue(ctx, issue, "test-user"); err != nil {
t.Fatalf("Failed to create issue: %v", err)
}
t.Run("add comment", func(t *testing.T) {
comment, err := s.AddIssueComment(ctx, issue.ID, testUserAlice, "This is a test comment")
if err != nil {
t.Fatalf("Failed to add comment: %v", err)
}
if comment.IssueID != issue.ID {
t.Errorf("Expected issue ID %s, got %s", issue.ID, comment.IssueID)
}
if comment.Author != testUserAlice {
t.Errorf("Expected author alice, got %s", comment.Author)
}
if comment.Text != "This is a test comment" {
t.Errorf("Expected text 'This is a test comment', got %s", comment.Text)
}
})
t.Run("list comments", func(t *testing.T) {
comments, err := s.GetIssueComments(ctx, issue.ID)
if err != nil {
t.Fatalf("Failed to get comments: %v", err)
}
if len(comments) != 1 {
t.Errorf("Expected 1 comment, got %d", len(comments))
}
if comments[0].Text != "This is a test comment" {
t.Errorf("Expected comment text, got %s", comments[0].Text)
}
})
t.Run("multiple comments", func(t *testing.T) {
_, err := s.AddIssueComment(ctx, issue.ID, "bob", "Second comment")
if err != nil {
t.Fatalf("Failed to add second comment: %v", err)
}
comments, err := s.GetIssueComments(ctx, issue.ID)
if err != nil {
t.Fatalf("Failed to get comments: %v", err)
}
if len(comments) != 2 {
t.Errorf("Expected 2 comments, got %d", len(comments))
}
})
t.Run("comments on non-existent issue", func(t *testing.T) {
comments, err := s.GetIssueComments(ctx, "bd-nonexistent")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(comments) != 0 {
t.Errorf("Expected 0 comments for non-existent issue, got %d", len(comments))
}
})
})
t.Run("list comments", func(t *testing.T) {
comments, err := s.GetIssueComments(ctx, issue.ID)
if err != nil {
t.Fatalf("Failed to get comments: %v", err)
t.Run("CommentAlias", func(t *testing.T) {
// Create test issue
issue := &types.Issue{
Title: "Test Issue for Alias",
Description: "Test description",
Priority: 1,
IssueType: types.TypeBug,
Status: types.StatusOpen,
}
if len(comments) != 1 {
t.Errorf("Expected 1 comment, got %d", len(comments))
if err := s.CreateIssue(ctx, issue, "test-user"); err != nil {
t.Fatalf("Failed to create issue: %v", err)
}
if comments[0].Text != "This is a test comment" {
t.Errorf("Expected comment text, got %s", comments[0].Text)
}
})
t.Run("comment alias shares Run function with comments add", func(t *testing.T) {
// This verifies that commentCmd reuses commentsAddCmd.Run
if commentCmd.Run == nil {
t.Error("commentCmd.Run is nil")
}
t.Run("multiple comments", func(t *testing.T) {
_, err := s.AddIssueComment(ctx, issue.ID, "bob", "Second comment")
if err != nil {
t.Fatalf("Failed to add second comment: %v", err)
}
if commentsAddCmd.Run == nil {
t.Error("commentsAddCmd.Run is nil")
}
comments, err := s.GetIssueComments(ctx, issue.ID)
if err != nil {
t.Fatalf("Failed to get comments: %v", err)
}
// Verify they share the same Run function (same memory address)
// This is a compile-time guarantee from how we defined it
// Just verify the command structure is set up correctly
if commentCmd.Use != "comment [issue-id] [text]" {
t.Errorf("Expected Use to be 'comment [issue-id] [text]', got %s", commentCmd.Use)
}
if len(comments) != 2 {
t.Errorf("Expected 2 comments, got %d", len(comments))
}
})
if commentCmd.Short != "Add a comment to an issue (alias for 'comments add')" {
t.Errorf("Unexpected Short description: %s", commentCmd.Short)
}
})
t.Run("comments on non-existent issue", func(t *testing.T) {
comments, err := s.GetIssueComments(ctx, "bd-nonexistent")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
t.Run("comment added via storage API works", func(t *testing.T) {
// Test direct storage API (which is what the command uses under the hood)
comment, err := s.AddIssueComment(ctx, issue.ID, testUserAlice, "Test comment")
if err != nil {
t.Fatalf("Failed to add comment: %v", err)
}
if len(comments) != 0 {
t.Errorf("Expected 0 comments for non-existent issue, got %d", len(comments))
}
})
}
if comment.Text != "Test comment" {
t.Errorf("Expected 'Test comment', got %s", comment.Text)
}
func TestCommentAlias(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "bd-test-comment-alias-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Verify via GetIssueComments
comments, err := s.GetIssueComments(ctx, issue.ID)
if err != nil {
t.Fatalf("Failed to get comments: %v", err)
}
testDB := filepath.Join(tmpDir, "test.db")
s := newTestStore(t, testDB)
defer s.Close()
ctx := context.Background()
// Create test issue
issue := &types.Issue{
Title: "Test Issue",
Description: "Test description",
Priority: 1,
IssueType: types.TypeBug,
Status: types.StatusOpen,
}
if err := s.CreateIssue(ctx, issue, "test-user"); err != nil {
t.Fatalf("Failed to create issue: %v", err)
}
t.Run("comment alias shares Run function with comments add", func(t *testing.T) {
// This verifies that commentCmd reuses commentsAddCmd.Run
if commentCmd.Run == nil {
t.Error("commentCmd.Run is nil")
}
if commentsAddCmd.Run == nil {
t.Error("commentsAddCmd.Run is nil")
}
// Verify they share the same Run function (same memory address)
// This is a compile-time guarantee from how we defined it
// Just verify the command structure is set up correctly
if commentCmd.Use != "comment [issue-id] [text]" {
t.Errorf("Expected Use to be 'comment [issue-id] [text]', got %s", commentCmd.Use)
}
if commentCmd.Short != "Add a comment to an issue (alias for 'comments add')" {
t.Errorf("Unexpected Short description: %s", commentCmd.Short)
}
})
t.Run("comment added via storage API works", func(t *testing.T) {
// Test direct storage API (which is what the command uses under the hood)
comment, err := s.AddIssueComment(ctx, issue.ID, testUserAlice, "Test comment")
if err != nil {
t.Fatalf("Failed to add comment: %v", err)
}
if comment.Text != "Test comment" {
t.Errorf("Expected 'Test comment', got %s", comment.Text)
}
// Verify via GetIssueComments
comments, err := s.GetIssueComments(ctx, issue.ID)
if err != nil {
t.Fatalf("Failed to get comments: %v", err)
}
if len(comments) != 1 {
t.Fatalf("Expected 1 comment, got %d", len(comments))
}
if len(comments) != 1 {
t.Fatalf("Expected 1 comment, got %d", len(comments))
}
})
})
}