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:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -80,15 +79,9 @@ func TestVersionCompatibility(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Setup temp environment
|
||||
tmpDir, err := os.MkdirTemp("", "bd-version-test-*")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
socketPath := filepath.Join(tmpDir, "bd.sock")
|
||||
// Setup isolated test environment
|
||||
tmpDir, _, dbPath, socketPath, cleanup := setupTestServerIsolated(t)
|
||||
defer cleanup()
|
||||
|
||||
store, err := sqlitestorage.New(dbPath)
|
||||
if err != nil {
|
||||
@@ -118,6 +111,16 @@ func TestVersionCompatibility(t *testing.T) {
|
||||
ClientVersion = tt.clientVersion
|
||||
defer func() { ClientVersion = originalClientVersion }()
|
||||
|
||||
// Change to tmpDir so client's os.Getwd() finds the test database
|
||||
originalWd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(tmpDir); err != nil {
|
||||
t.Fatalf("Failed to change directory: %v", err)
|
||||
}
|
||||
defer os.Chdir(originalWd)
|
||||
|
||||
client, err := TryConnect(socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to connect: %v", err)
|
||||
@@ -127,6 +130,9 @@ func TestVersionCompatibility(t *testing.T) {
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// Set dbPath so client validates it's connected to the right daemon
|
||||
client.dbPath = dbPath
|
||||
|
||||
// Try to create an issue (this triggers version check)
|
||||
args := &CreateArgs{
|
||||
Title: "Version test issue",
|
||||
@@ -166,14 +172,8 @@ func TestVersionCompatibility(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHealthCheckIncludesVersionInfo(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp("", "bd-health-version-*")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
socketPath := filepath.Join(tmpDir, "bd.sock")
|
||||
tmpDir, _, dbPath, socketPath, cleanup := setupTestServerIsolated(t)
|
||||
defer cleanup()
|
||||
|
||||
store, err := sqlitestorage.New(dbPath)
|
||||
if err != nil {
|
||||
@@ -196,12 +196,25 @@ func TestHealthCheckIncludesVersionInfo(t *testing.T) {
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Change to tmpDir so client's os.Getwd() finds the test database
|
||||
originalWd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(tmpDir); err != nil {
|
||||
t.Fatalf("Failed to change directory: %v", err)
|
||||
}
|
||||
defer os.Chdir(originalWd)
|
||||
|
||||
client, err := TryConnect(socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to connect: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// Set dbPath so client validates it's connected to the right daemon
|
||||
client.dbPath = dbPath
|
||||
|
||||
health, err := client.Health()
|
||||
if err != nil {
|
||||
t.Fatalf("Health check failed: %v", err)
|
||||
@@ -223,14 +236,8 @@ func TestHealthCheckIncludesVersionInfo(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIncompatibleVersionInHealth(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp("", "bd-health-incompatible-*")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
socketPath := filepath.Join(tmpDir, "bd.sock")
|
||||
tmpDir, _, dbPath, socketPath, cleanup := setupTestServerIsolated(t)
|
||||
defer cleanup()
|
||||
|
||||
store, err := sqlitestorage.New(dbPath)
|
||||
if err != nil {
|
||||
@@ -253,12 +260,25 @@ func TestIncompatibleVersionInHealth(t *testing.T) {
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Change to tmpDir so client's os.Getwd() finds the test database
|
||||
originalWd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(tmpDir); err != nil {
|
||||
t.Fatalf("Failed to change directory: %v", err)
|
||||
}
|
||||
defer os.Chdir(originalWd)
|
||||
|
||||
client, err := TryConnect(socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to connect: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// Set dbPath so client validates it's connected to the right daemon
|
||||
client.dbPath = dbPath
|
||||
|
||||
// Health check should succeed but report incompatible
|
||||
health, err := client.Health()
|
||||
if err != nil {
|
||||
@@ -336,14 +356,8 @@ func TestVersionCheckMessage(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPingAndHealthBypassVersionCheck(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp("", "bd-bypass-version-*")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
socketPath := filepath.Join(tmpDir, "bd.sock")
|
||||
tmpDir, _, dbPath, socketPath, cleanup := setupTestServerIsolated(t)
|
||||
defer cleanup()
|
||||
|
||||
store, err := sqlitestorage.New(dbPath)
|
||||
if err != nil {
|
||||
@@ -366,12 +380,25 @@ func TestPingAndHealthBypassVersionCheck(t *testing.T) {
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Change to tmpDir so client's os.Getwd() finds the test database
|
||||
originalWd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(tmpDir); err != nil {
|
||||
t.Fatalf("Failed to change directory: %v", err)
|
||||
}
|
||||
defer os.Chdir(originalWd)
|
||||
|
||||
client, err := TryConnect(socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to connect: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// Set dbPath so client validates it's connected to the right daemon
|
||||
client.dbPath = dbPath
|
||||
|
||||
// Ping should work despite version mismatch
|
||||
if err := client.Ping(); err != nil {
|
||||
t.Errorf("Ping should work despite version mismatch, got: %v", err)
|
||||
@@ -404,14 +431,8 @@ func TestPingAndHealthBypassVersionCheck(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMetricsOperation(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp("", "bd-metrics-test-*")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
socketPath := filepath.Join(tmpDir, "bd.sock")
|
||||
tmpDir, _, dbPath, socketPath, cleanup := setupTestServerIsolated(t)
|
||||
defer cleanup()
|
||||
|
||||
store, err := sqlitestorage.New(dbPath)
|
||||
if err != nil {
|
||||
@@ -433,12 +454,25 @@ func TestMetricsOperation(t *testing.T) {
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Change to tmpDir so client's os.Getwd() finds the test database
|
||||
originalWd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(tmpDir); err != nil {
|
||||
t.Fatalf("Failed to change directory: %v", err)
|
||||
}
|
||||
defer os.Chdir(originalWd)
|
||||
|
||||
client, err := TryConnect(socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to connect: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// Set dbPath so client validates it's connected to the right daemon
|
||||
client.dbPath = dbPath
|
||||
|
||||
metrics, err := client.Metrics()
|
||||
if err != nil {
|
||||
t.Fatalf("Metrics call failed: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user