Fix bd-1: Prevent test database pollution

Tests were connecting to test daemon but daemon routed to production DB via
findDatabaseForCwd(). Fixed by ensuring tests use isolated .beads directories
and change working directory to tmpDir.

Changes:
- bench_test.go: Added .beads subdir, chdir, and client.dbPath to setupBenchServer
- bench_test.go: Set dbPath for goroutine clients in BenchmarkConcurrentAgents
- comments_test.go: Refactored to use setupTestServer
- version_test.go: Fixed 4 tests to use setupTestServerIsolated with proper isolation
- rpc_test.go: Added setupTestServerIsolated() helper for custom test setup

Verified: RPC test suite runs with no database pollution (151→151 issues)
Amp-Thread-ID: https://ampcode.com/threads/T-348b7ba8-4292-4ed3-b143-0ad07d226c21
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-22 00:42:50 -07:00
parent 78e8cb914c
commit 97e5df1180
5 changed files with 301 additions and 128 deletions

View File

@@ -49,7 +49,7 @@ func BenchmarkDirectCreate(b *testing.B) {
// BenchmarkDaemonCreate benchmarks RPC create operations
func BenchmarkDaemonCreate(b *testing.B) {
_, client, cleanup := setupBenchServer(b)
_, client, cleanup, _ := setupBenchServer(b)
defer cleanup()
b.ResetTimer()
@@ -107,7 +107,7 @@ func BenchmarkDirectUpdate(b *testing.B) {
// BenchmarkDaemonUpdate benchmarks RPC update operations
func BenchmarkDaemonUpdate(b *testing.B) {
_, client, cleanup := setupBenchServer(b)
_, client, cleanup, _ := setupBenchServer(b)
defer cleanup()
createArgs := &CreateArgs{
@@ -181,7 +181,7 @@ func BenchmarkDirectList(b *testing.B) {
// BenchmarkDaemonList benchmarks RPC list operations
func BenchmarkDaemonList(b *testing.B) {
_, client, cleanup := setupBenchServer(b)
_, client, cleanup, _ := setupBenchServer(b)
defer cleanup()
for i := 0; i < 100; i++ {
@@ -207,7 +207,7 @@ func BenchmarkDaemonList(b *testing.B) {
// BenchmarkDaemonLatency measures round-trip latency
func BenchmarkDaemonLatency(b *testing.B) {
_, client, cleanup := setupBenchServer(b)
_, client, cleanup, _ := setupBenchServer(b)
defer cleanup()
b.ResetTimer()
@@ -220,7 +220,7 @@ func BenchmarkDaemonLatency(b *testing.B) {
// BenchmarkConcurrentAgents benchmarks concurrent agent throughput
func BenchmarkConcurrentAgents(b *testing.B) {
server, _, cleanup := setupBenchServer(b)
server, _, cleanup, dbPath := setupBenchServer(b)
defer cleanup()
numAgents := 4
@@ -239,6 +239,9 @@ func BenchmarkConcurrentAgents(b *testing.B) {
}
defer client.Close()
// Set dbPath so client validates it's connected to the right daemon
client.dbPath = dbPath
for j := 0; j < opsPerAgent; j++ {
args := &CreateArgs{
Title: fmt.Sprintf("Issue %d", j),
@@ -260,14 +263,21 @@ func BenchmarkConcurrentAgents(b *testing.B) {
}
}
func setupBenchServer(b *testing.B) (*Server, *Client, func()) {
func setupBenchServer(b *testing.B) (*Server, *Client, func(), string) {
tmpDir, err := os.MkdirTemp("", "bd-rpc-bench-*")
if err != nil {
b.Fatalf("Failed to create temp dir: %v", err)
}
dbPath := filepath.Join(tmpDir, "test.db")
socketPath := filepath.Join(tmpDir, "bd.sock")
// Create .beads subdirectory so findDatabaseForCwd finds THIS database, not project's
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
os.RemoveAll(tmpDir)
b.Fatalf("Failed to create .beads dir: %v", err)
}
dbPath := filepath.Join(beadsDir, "test.db")
socketPath := filepath.Join(beadsDir, "bd.sock")
store, err := sqlitestorage.New(dbPath)
if err != nil {
@@ -286,22 +296,44 @@ func setupBenchServer(b *testing.B) (*Server, *Client, func()) {
time.Sleep(100 * time.Millisecond)
client, err := TryConnect(socketPath)
// Change to tmpDir so client's os.Getwd() finds the test database
originalWd, err := os.Getwd()
if err != nil {
cancel()
server.Stop()
store.Close()
os.RemoveAll(tmpDir)
b.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
cancel()
server.Stop()
store.Close()
os.RemoveAll(tmpDir)
b.Fatalf("Failed to change directory: %v", err)
}
client, err := TryConnect(socketPath)
if err != nil {
cancel()
server.Stop()
store.Close()
os.Chdir(originalWd)
os.RemoveAll(tmpDir)
b.Fatalf("Failed to connect client: %v", err)
}
// Set the client's dbPath to the test database so it doesn't route to the wrong DB
client.dbPath = dbPath
cleanup := func() {
client.Close()
cancel()
server.Stop()
store.Close()
os.Chdir(originalWd) // Restore original working directory
os.RemoveAll(tmpDir)
}
return server, client, cleanup
return server, client, cleanup, dbPath
}