Add test coverage for daemon lifecycle and config modules

- Add config_test.go: tests for daemon configuration (local/global, sync behavior)
- Add daemon_test.go: tests for daemon lifecycle (creation, shutdown, database path resolution)
- Add process_test.go: tests for daemon lock acquisition and release
- Closes bd-2b34.6, bd-2b34.7

Amp-Thread-ID: https://ampcode.com/threads/T-4419d1ab-4105-4e75-bea8-1837ee80e2c2
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-01 21:26:56 -07:00
parent 8a76b52cfc
commit 6e8907335f
5 changed files with 700 additions and 68 deletions

File diff suppressed because one or more lines are too long

293
cmd/bd/daemon_sync_test.go Normal file
View File

@@ -0,0 +1,293 @@
package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types"
)
func TestExportToJSONLWithStore(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
jsonlPath := filepath.Join(tmpDir, ".beads", "issues.jsonl")
// Create storage
store, err := sqlite.New(dbPath)
if err != nil {
t.Fatalf("failed to create store: %v", err)
}
defer store.Close()
ctx := context.Background()
// Set issue_prefix to prevent "database not initialized" errors
if err := store.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("failed to set issue_prefix: %v", err)
}
// Create test issue
issue := &types.Issue{
ID: "test-1",
Title: "Test Issue",
Description: "Test description",
IssueType: types.TypeBug,
Priority: 1,
Status: types.StatusOpen,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
t.Fatalf("failed to create issue: %v", err)
}
// Export to JSONL
if err := exportToJSONLWithStore(ctx, store, jsonlPath); err != nil {
t.Fatalf("exportToJSONLWithStore failed: %v", err)
}
// Verify file exists
if _, err := os.Stat(jsonlPath); os.IsNotExist(err) {
t.Fatal("JSONL file was not created")
}
// Read and verify content
data, err := os.ReadFile(jsonlPath)
if err != nil {
t.Fatalf("failed to read JSONL: %v", err)
}
var exported types.Issue
if err := json.Unmarshal(data, &exported); err != nil {
t.Fatalf("failed to unmarshal JSONL: %v", err)
}
if exported.ID != "test-1" {
t.Errorf("expected ID 'test-1', got %s", exported.ID)
}
if exported.Title != "Test Issue" {
t.Errorf("expected title 'Test Issue', got %s", exported.Title)
}
}
func TestExportToJSONLWithStore_EmptyDatabase(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
jsonlPath := filepath.Join(tmpDir, ".beads", "issues.jsonl")
// Create storage (empty)
store, err := sqlite.New(dbPath)
if err != nil {
t.Fatalf("failed to create store: %v", err)
}
defer store.Close()
ctx := context.Background()
// Create existing JSONL with content
if err := os.MkdirAll(filepath.Dir(jsonlPath), 0755); err != nil {
t.Fatalf("failed to create directory: %v", err)
}
existingIssue := &types.Issue{
ID: "existing-1",
Title: "Existing",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeBug,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
data, _ := json.Marshal(existingIssue)
if err := os.WriteFile(jsonlPath, append(data, '\n'), 0644); err != nil {
t.Fatalf("failed to write existing JSONL: %v", err)
}
// Should refuse to export empty DB over non-empty JSONL
err = exportToJSONLWithStore(ctx, store, jsonlPath)
if err == nil {
t.Fatal("expected error when exporting empty DB over non-empty JSONL")
}
}
func TestImportToJSONLWithStore(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
jsonlPath := filepath.Join(tmpDir, ".beads", "issues.jsonl")
// Create storage first to initialize database
store, err := sqlite.New(dbPath)
if err != nil {
t.Fatalf("failed to create store: %v", err)
}
defer store.Close()
ctx := context.Background()
// Set issue_prefix to prevent "database not initialized" errors
if err := store.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("failed to set issue_prefix: %v", err)
}
// Create JSONL with test data
if err := os.MkdirAll(filepath.Dir(jsonlPath), 0755); err != nil {
t.Fatalf("failed to create directory: %v", err)
}
issue := &types.Issue{
ID: "test-1",
Title: "Test Issue",
Description: "Test description",
IssueType: types.TypeBug,
Priority: 1,
Status: types.StatusOpen,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
data, _ := json.Marshal(issue)
if err := os.WriteFile(jsonlPath, append(data, '\n'), 0644); err != nil {
t.Fatalf("failed to write JSONL: %v", err)
}
// Import from JSONL
if err := importToJSONLWithStore(ctx, store, jsonlPath); err != nil {
t.Fatalf("importToJSONLWithStore failed: %v", err)
}
// Verify issue was imported
imported, err := store.GetIssue(ctx, "test-1")
if err != nil {
t.Fatalf("failed to get imported issue: %v", err)
}
if imported.Title != "Test Issue" {
t.Errorf("expected title 'Test Issue', got %s", imported.Title)
}
}
func TestExportImportRoundTrip(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
jsonlPath := filepath.Join(tmpDir, ".beads", "issues.jsonl")
// Create storage and add issues
store, err := sqlite.New(dbPath)
if err != nil {
t.Fatalf("failed to create store: %v", err)
}
defer store.Close()
ctx := context.Background()
// Set issue_prefix to prevent "database not initialized" errors
if err := store.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("failed to set issue_prefix: %v", err)
}
// Create multiple issues with dependencies
issue1 := &types.Issue{
ID: "test-1",
Title: "Issue 1",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeBug,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
issue2 := &types.Issue{
ID: "test-2",
Title: "Issue 2",
Status: types.StatusOpen,
Priority: 2,
IssueType: types.TypeFeature,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if err := store.CreateIssue(ctx, issue1, "test"); err != nil {
t.Fatalf("failed to create issue1: %v", err)
}
if err := store.CreateIssue(ctx, issue2, "test"); err != nil {
t.Fatalf("failed to create issue2: %v", err)
}
// Add dependency
dep := &types.Dependency{
IssueID: "test-2",
DependsOnID: "test-1",
Type: types.DepBlocks,
}
if err := store.AddDependency(ctx, dep, "test"); err != nil {
t.Fatalf("failed to add dependency: %v", err)
}
// Add labels
if err := store.AddLabel(ctx, "test-1", "bug", "test"); err != nil {
t.Fatalf("failed to add label: %v", err)
}
// Export
if err := exportToJSONLWithStore(ctx, store, jsonlPath); err != nil {
t.Fatalf("export failed: %v", err)
}
// Create new database
dbPath2 := filepath.Join(tmpDir, ".beads", "beads2.db")
store2, err := sqlite.New(dbPath2)
if err != nil {
t.Fatalf("failed to create store2: %v", err)
}
defer store2.Close()
// Set issue_prefix for second database
if err := store2.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("failed to set issue_prefix for store2: %v", err)
}
// Import
if err := importToJSONLWithStore(ctx, store2, jsonlPath); err != nil {
t.Fatalf("import failed: %v", err)
}
// Verify issues
imported1, err := store2.GetIssue(ctx, "test-1")
if err != nil {
t.Fatalf("failed to get imported issue1: %v", err)
}
if imported1.Title != "Issue 1" {
t.Errorf("expected title 'Issue 1', got %s", imported1.Title)
}
imported2, err := store2.GetIssue(ctx, "test-2")
if err != nil {
t.Fatalf("failed to get imported issue2: %v", err)
}
if imported2.Title != "Issue 2" {
t.Errorf("expected title 'Issue 2', got %s", imported2.Title)
}
// Verify dependency
deps, err := store2.GetDependencies(ctx, "test-2")
if err != nil {
t.Fatalf("failed to get dependencies: %v", err)
}
if len(deps) != 1 || deps[0].ID != "test-1" {
t.Errorf("expected dependency test-2 -> test-1, got %v", deps)
}
// Verify labels
labels, err := store2.GetLabels(ctx, "test-1")
if err != nil {
t.Fatalf("failed to get labels: %v", err)
}
if len(labels) != 1 || labels[0] != "bug" {
t.Errorf("expected label 'bug', got %v", labels)
}
}

View File

@@ -0,0 +1,98 @@
package daemonrunner
import (
"testing"
"time"
)
func TestConfigDefaults(t *testing.T) {
cfg := Config{
Interval: 5 * time.Second,
AutoCommit: true,
AutoPush: false,
Global: false,
}
if cfg.Interval != 5*time.Second {
t.Errorf("Expected Interval 5s, got %v", cfg.Interval)
}
if !cfg.AutoCommit {
t.Error("Expected AutoCommit to be true")
}
if cfg.AutoPush {
t.Error("Expected AutoPush to be false")
}
if cfg.Global {
t.Error("Expected Global to be false")
}
}
func TestConfigLocalDaemon(t *testing.T) {
cfg := Config{
Global: false,
WorkspacePath: "/tmp/test-workspace",
BeadsDir: "/tmp/test-workspace/.beads",
DBPath: "/tmp/test-workspace/.beads/beads.db",
SocketPath: "/tmp/test-workspace/.beads/bd.sock",
LogFile: "/tmp/test-workspace/.beads/daemon.log",
PIDFile: "/tmp/test-workspace/.beads/daemon.pid",
}
if cfg.Global {
t.Error("Expected local daemon (Global=false)")
}
if cfg.WorkspacePath == "" {
t.Error("Expected WorkspacePath to be set for local daemon")
}
if cfg.DBPath == "" {
t.Error("Expected DBPath to be set for local daemon")
}
}
func TestConfigGlobalDaemon(t *testing.T) {
cfg := Config{
Global: true,
BeadsDir: "/home/user/.beads",
SocketPath: "/home/user/.beads/global.sock",
LogFile: "/home/user/.beads/global-daemon.log",
PIDFile: "/home/user/.beads/global-daemon.pid",
}
if !cfg.Global {
t.Error("Expected global daemon (Global=true)")
}
if cfg.WorkspacePath != "" {
t.Error("Expected WorkspacePath to be empty for global daemon")
}
if cfg.DBPath != "" {
t.Error("Expected DBPath to be empty for global daemon")
}
}
func TestConfigSyncBehavior(t *testing.T) {
tests := []struct {
name string
autoCommit bool
autoPush bool
}{
{"no sync", false, false},
{"commit only", true, false},
{"commit and push", true, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := Config{
AutoCommit: tt.autoCommit,
AutoPush: tt.autoPush,
}
if cfg.AutoCommit != tt.autoCommit {
t.Errorf("Expected AutoCommit=%v, got %v", tt.autoCommit, cfg.AutoCommit)
}
if cfg.AutoPush != tt.autoPush {
t.Errorf("Expected AutoPush=%v, got %v", tt.autoPush, cfg.AutoPush)
}
})
}
}

View File

@@ -0,0 +1,129 @@
package daemonrunner
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestNew(t *testing.T) {
cfg := Config{
Interval: 5 * time.Second,
AutoCommit: true,
Global: false,
}
daemon := New(cfg, "0.19.0")
if daemon == nil {
t.Fatal("Expected non-nil daemon")
}
if daemon.cfg.Interval != cfg.Interval {
t.Errorf("Expected interval %v, got %v", cfg.Interval, daemon.cfg.Interval)
}
if daemon.Version != "0.19.0" {
t.Errorf("Expected version 0.19.0, got %s", daemon.Version)
}
}
func TestStop(t *testing.T) {
cfg := Config{
Interval: 5 * time.Second,
}
daemon := New(cfg, "0.19.0")
// Stop should not error even with no server running
if err := daemon.Stop(); err != nil {
t.Errorf("Stop() returned unexpected error: %v", err)
}
}
func TestDetermineDatabasePath(t *testing.T) {
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
dbPath := filepath.Join(beadsDir, "beads.db")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
t.Fatalf("Failed to create beads dir: %v", err)
}
if err := os.WriteFile(dbPath, []byte("test"), 0644); err != nil {
t.Fatalf("Failed to create db file: %v", err)
}
cfg := Config{
WorkspacePath: tmpDir,
}
daemon := New(cfg, "0.19.0")
// Override working directory for test
oldWd, _ := os.Getwd()
defer func() { _ = os.Chdir(oldWd) }()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
if err := daemon.determineDatabasePath(); err != nil {
t.Errorf("determineDatabasePath() failed: %v", err)
}
// Use EvalSymlinks to handle /var vs /private/var on macOS
expectedDB, _ := filepath.EvalSymlinks(dbPath)
actualDB, _ := filepath.EvalSymlinks(daemon.cfg.DBPath)
if actualDB != expectedDB {
t.Errorf("Expected DBPath %s, got %s", expectedDB, actualDB)
}
expectedBeadsDir, _ := filepath.EvalSymlinks(beadsDir)
actualBeadsDir, _ := filepath.EvalSymlinks(daemon.cfg.BeadsDir)
if actualBeadsDir != expectedBeadsDir {
t.Errorf("Expected BeadsDir %s, got %s", expectedBeadsDir, actualBeadsDir)
}
expectedWS, _ := filepath.EvalSymlinks(tmpDir)
actualWS, _ := filepath.EvalSymlinks(daemon.cfg.WorkspacePath)
if actualWS != expectedWS {
t.Errorf("Expected WorkspacePath %s, got %s", expectedWS, actualWS)
}
}
func TestDetermineDatabasePathAlreadySet(t *testing.T) {
existingPath := "/already/set/beads.db"
cfg := Config{
DBPath: existingPath,
}
daemon := New(cfg, "0.19.0")
if err := daemon.determineDatabasePath(); err != nil {
t.Errorf("determineDatabasePath() failed: %v", err)
}
if daemon.cfg.DBPath != existingPath {
t.Errorf("Expected DBPath unchanged: %s, got %s", existingPath, daemon.cfg.DBPath)
}
}
func TestGetGlobalBeadsDir(t *testing.T) {
beadsDir, err := getGlobalBeadsDir()
if err != nil {
t.Fatalf("getGlobalBeadsDir() failed: %v", err)
}
if beadsDir == "" {
t.Error("Expected non-empty beads directory")
}
// Check directory was created
if stat, err := os.Stat(beadsDir); err != nil {
t.Errorf("Global beads directory not created: %v", err)
} else if !stat.IsDir() {
t.Error("Global beads path is not a directory")
}
// Verify it's in home directory
home, _ := os.UserHomeDir()
expectedPath := filepath.Join(home, ".beads")
if beadsDir != expectedPath {
t.Errorf("Expected %s, got %s", expectedPath, beadsDir)
}
}

View File

@@ -0,0 +1,118 @@
package daemonrunner
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
func TestDaemonLockBasics(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "beads.db")
// Acquire lock
lock, err := acquireDaemonLock(tmpDir, dbPath, "0.19.0")
if err != nil {
t.Fatalf("Failed to acquire lock: %v", err)
}
defer lock.Close()
// Verify lock file was created
lockPath := filepath.Join(tmpDir, "daemon.lock")
if _, err := os.Stat(lockPath); os.IsNotExist(err) {
t.Error("Lock file was not created")
}
// Verify PID file was created
pidPath := filepath.Join(tmpDir, "daemon.pid")
if _, err := os.Stat(pidPath); os.IsNotExist(err) {
t.Error("PID file was not created")
}
// Read and verify lock metadata
data, err := os.ReadFile(lockPath)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}
var info DaemonLockInfo
if err := json.Unmarshal(data, &info); err != nil {
t.Fatalf("Failed to parse lock file: %v", err)
}
if info.PID != os.Getpid() {
t.Errorf("Expected PID %d, got %d", os.Getpid(), info.PID)
}
if info.Database != dbPath {
t.Errorf("Expected database %s, got %s", dbPath, info.Database)
}
if info.Version != "0.19.0" {
t.Errorf("Expected version 0.19.0, got %s", info.Version)
}
if info.StartedAt.IsZero() {
t.Error("Expected non-zero StartedAt timestamp")
}
}
func TestDaemonLockExclusive(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "beads.db")
// Acquire first lock
lock1, err := acquireDaemonLock(tmpDir, dbPath, "0.19.0")
if err != nil {
t.Fatalf("Failed to acquire first lock: %v", err)
}
defer lock1.Close()
// Try to acquire second lock (should fail)
lock2, err := acquireDaemonLock(tmpDir, dbPath, "0.19.0")
if err != ErrDaemonLocked {
if lock2 != nil {
lock2.Close()
}
t.Errorf("Expected ErrDaemonLocked, got %v", err)
}
}
func TestDaemonLockRelease(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "beads.db")
// Acquire lock
lock, err := acquireDaemonLock(tmpDir, dbPath, "0.19.0")
if err != nil {
t.Fatalf("Failed to acquire lock: %v", err)
}
// Release lock
if err := lock.Close(); err != nil {
t.Fatalf("Failed to release lock: %v", err)
}
// Should be able to acquire again after release
lock2, err := acquireDaemonLock(tmpDir, dbPath, "0.19.0")
if err != nil {
t.Fatalf("Failed to acquire lock after release: %v", err)
}
defer lock2.Close()
}
func TestDaemonLockCloseIdempotent(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "beads.db")
lock, err := acquireDaemonLock(tmpDir, dbPath, "0.19.0")
if err != nil {
t.Fatalf("Failed to acquire lock: %v", err)
}
// Close multiple times should not error
if err := lock.Close(); err != nil {
t.Errorf("First close failed: %v", err)
}
if err := lock.Close(); err != nil {
t.Errorf("Second close failed: %v", err)
}
}