Files
beads/cmd/bd/config_test.go
Steve Yegge 57253f93a3 Context propagation with graceful cancellation (bd-rtp, bd-yb8, bd-2o2)
Complete implementation of signal-aware context propagation for graceful
cancellation across all commands and storage operations.

Key changes:

1. Signal-aware contexts (bd-rtp):
   - Added rootCtx/rootCancel in main.go using signal.NotifyContext()
   - Set up in PersistentPreRun, cancelled in PersistentPostRun
   - Daemon uses same pattern in runDaemonLoop()
   - Handles SIGINT/SIGTERM for graceful shutdown

2. Context propagation (bd-yb8):
   - All commands now use rootCtx instead of context.Background()
   - sqlite.New() receives context for cancellable operations
   - Database operations respect context cancellation
   - Storage layer propagates context through all queries

3. Cancellation tests (bd-2o2):
   - Added import_cancellation_test.go with comprehensive tests
   - Added export cancellation test in export_test.go
   - Tests verify database integrity after cancellation
   - All cancellation tests passing

Fixes applied during review:
   - Fixed rootCtx lifecycle (removed premature defer from PersistentPreRun)
   - Fixed test context contamination (reset rootCtx in test cleanup)
   - Fixed export tests missing context setup

Impact:
   - Pressing Ctrl+C during import/export now cancels gracefully
   - No database corruption or hanging transactions
   - Clean shutdown of all operations

Tested:
   - go build ./cmd/bd ✓
   - go test ./cmd/bd -run TestImportCancellation ✓
   - go test ./cmd/bd -run TestExportCommand ✓
   - Manual Ctrl+C testing verified

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-20 21:57:23 -05:00

180 lines
4.6 KiB
Go

package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/storage/sqlite"
)
func TestConfigCommands(t *testing.T) {
ctx := context.Background()
store, cleanup := setupTestDB(t)
defer cleanup()
// Test SetConfig
err := store.SetConfig(ctx, "test.key", "test-value")
if err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
// Test GetConfig
value, err := store.GetConfig(ctx, "test.key")
if err != nil {
t.Fatalf("GetConfig failed: %v", err)
}
if value != "test-value" {
t.Errorf("Expected 'test-value', got '%s'", value)
}
// Test GetConfig for non-existent key
value, err = store.GetConfig(ctx, "nonexistent.key")
if err != nil {
t.Fatalf("GetConfig for nonexistent key failed: %v", err)
}
if value != "" {
t.Errorf("Expected empty string for nonexistent key, got '%s'", value)
}
// Test SetConfig update
err = store.SetConfig(ctx, "test.key", "updated-value")
if err != nil {
t.Fatalf("SetConfig update failed: %v", err)
}
value, err = store.GetConfig(ctx, "test.key")
if err != nil {
t.Fatalf("GetConfig after update failed: %v", err)
}
if value != "updated-value" {
t.Errorf("Expected 'updated-value', got '%s'", value)
}
// Test GetAllConfig
err = store.SetConfig(ctx, "jira.url", "https://example.atlassian.net")
if err != nil {
t.Fatalf("SetConfig for jira.url failed: %v", err)
}
err = store.SetConfig(ctx, "jira.project", "PROJ")
if err != nil {
t.Fatalf("SetConfig for jira.project failed: %v", err)
}
config, err := store.GetAllConfig(ctx)
if err != nil {
t.Fatalf("GetAllConfig failed: %v", err)
}
// Should have at least our test keys (may have default compaction config too)
if len(config) < 3 {
t.Errorf("Expected at least 3 config entries, got %d", len(config))
}
if config["test.key"] != "updated-value" {
t.Errorf("Expected 'updated-value' for test.key, got '%s'", config["test.key"])
}
if config["jira.url"] != "https://example.atlassian.net" {
t.Errorf("Expected jira.url in config, got '%s'", config["jira.url"])
}
if config["jira.project"] != "PROJ" {
t.Errorf("Expected jira.project in config, got '%s'", config["jira.project"])
}
// Test DeleteConfig
err = store.DeleteConfig(ctx, "test.key")
if err != nil {
t.Fatalf("DeleteConfig failed: %v", err)
}
value, err = store.GetConfig(ctx, "test.key")
if err != nil {
t.Fatalf("GetConfig after delete failed: %v", err)
}
if value != "" {
t.Errorf("Expected empty string after delete, got '%s'", value)
}
// Test DeleteConfig for non-existent key (should not error)
err = store.DeleteConfig(ctx, "nonexistent.key")
if err != nil {
t.Fatalf("DeleteConfig for nonexistent key failed: %v", err)
}
}
func TestConfigNamespaces(t *testing.T) {
ctx := context.Background()
store, cleanup := setupTestDB(t)
defer cleanup()
// Test various namespace conventions
namespaces := map[string]string{
"jira.url": "https://example.atlassian.net",
"jira.project": "PROJ",
"jira.status_map.todo": "open",
"linear.team_id": "team-123",
"github.org": "myorg",
"custom.my_integration.field": "value",
}
for key, val := range namespaces {
err := store.SetConfig(ctx, key, val)
if err != nil {
t.Fatalf("SetConfig for %s failed: %v", key, err)
}
}
// Verify all set correctly
for key, expected := range namespaces {
value, err := store.GetConfig(ctx, key)
if err != nil {
t.Fatalf("GetConfig for %s failed: %v", key, err)
}
if value != expected {
t.Errorf("Expected '%s' for %s, got '%s'", expected, key, value)
}
}
// Test GetAllConfig returns all
config, err := store.GetAllConfig(ctx)
if err != nil {
t.Fatalf("GetAllConfig failed: %v", err)
}
for key, expected := range namespaces {
if config[key] != expected {
t.Errorf("Expected '%s' for %s in GetAllConfig, got '%s'", expected, key, config[key])
}
}
}
// setupTestDB creates a temporary test database
func setupTestDB(t *testing.T) (*sqlite.SQLiteStorage, func()) {
tmpDir, err := os.MkdirTemp("", "bd-test-config-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
testDB := filepath.Join(tmpDir, "test.db")
store, err := sqlite.New(context.Background(), testDB)
if err != nil {
os.RemoveAll(tmpDir)
t.Fatalf("Failed to create test database: %v", err)
}
// CRITICAL (bd-166): Set issue_prefix to prevent "database not initialized" errors
ctx := context.Background()
if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
store.Close()
os.RemoveAll(tmpDir)
t.Fatalf("Failed to set issue_prefix: %v", err)
}
cleanup := func() {
store.Close()
os.RemoveAll(tmpDir)
}
return store, cleanup
}