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:
@@ -1,51 +1,15 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
sqlitestorage "github.com/steveyegge/beads/internal/storage/sqlite"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestCommentOperationsViaRPC(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
dbPath := filepath.Join(tmpDir, "test.db")
|
||||
socketPath := filepath.Join(tmpDir, "bd.sock")
|
||||
|
||||
store, err := sqlitestorage.New(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create store: %v", err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
server := NewServer(socketPath, store)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
serverErr := make(chan error, 1)
|
||||
go func() {
|
||||
serverErr <- server.Start(ctx)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-server.WaitReady():
|
||||
case err := <-serverErr:
|
||||
t.Fatalf("server failed to start: %v", err)
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("timeout waiting for server to start")
|
||||
}
|
||||
|
||||
client, err := TryConnect(socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to connect to server: %v", err)
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("client is nil after successful connection")
|
||||
}
|
||||
defer client.Close()
|
||||
_, client, cleanup := setupTestServer(t)
|
||||
defer cleanup()
|
||||
|
||||
createResp, err := client.Create(&CreateArgs{
|
||||
Title: "Comment test",
|
||||
@@ -98,16 +62,4 @@ func TestCommentOperationsViaRPC(t *testing.T) {
|
||||
if comments[0].Text != "first comment" {
|
||||
t.Fatalf("expected comment text 'first comment', got %q", comments[0].Text)
|
||||
}
|
||||
|
||||
if err := server.Stop(); err != nil {
|
||||
t.Fatalf("failed to stop server: %v", err)
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case err := <-serverErr:
|
||||
if err != nil && err != context.Canceled {
|
||||
t.Fatalf("server returned error: %v", err)
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user