Files
beads/cmd/bd/migrate_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

178 lines
5.0 KiB
Go

package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/storage/sqlite"
)
func TestMigrateCommand(t *testing.T) {
// Create temporary test directory
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0750); err != nil {
t.Fatalf("Failed to create .beads directory: %v", err)
}
t.Run("no databases", func(t *testing.T) {
databases, err := detectDatabases(beadsDir)
if err != nil {
t.Fatalf("detectDatabases failed: %v", err)
}
if len(databases) != 0 {
t.Errorf("Expected 0 databases, got %d", len(databases))
}
})
t.Run("single old database", func(t *testing.T) {
// Create old database
oldDBPath := filepath.Join(beadsDir, "vc.db")
store, err := sqlite.New(context.Background(), oldDBPath)
if err != nil {
t.Fatalf("Failed to create old database: %v", err)
}
// Set old version
ctx := context.Background()
if err := store.SetMetadata(ctx, "bd_version", "0.16.0"); err != nil {
t.Fatalf("Failed to set old version: %v", err)
}
_ = store.Close()
// Detect databases
databases, err := detectDatabases(beadsDir)
if err != nil {
t.Fatalf("detectDatabases failed: %v", err)
}
if len(databases) != 1 {
t.Fatalf("Expected 1 database, got %d", len(databases))
}
if databases[0].version != "0.16.0" {
t.Errorf("Expected version 0.16.0, got %s", databases[0].version)
}
// Migrate to beads.db
targetPath := filepath.Join(beadsDir, "beads.db")
if err := os.Rename(oldDBPath, targetPath); err != nil {
t.Fatalf("Failed to migrate database: %v", err)
}
// Verify migration
databases, err = detectDatabases(beadsDir)
if err != nil {
t.Fatalf("detectDatabases failed after migration: %v", err)
}
if len(databases) != 1 {
t.Fatalf("Expected 1 database after migration, got %d", len(databases))
}
if filepath.Base(databases[0].path) != "beads.db" {
t.Errorf("Expected beads.db, got %s", filepath.Base(databases[0].path))
}
})
t.Run("version detection", func(t *testing.T) {
// Test getDBVersion with beads.db from previous test
dbPath := filepath.Join(beadsDir, "beads.db")
version := getDBVersion(dbPath)
if version != "0.16.0" {
t.Errorf("Expected version 0.16.0, got %s", version)
}
// Update version
store, err := sqlite.New(context.Background(), dbPath)
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
ctx := context.Background()
if err := store.SetMetadata(ctx, "bd_version", Version); err != nil {
t.Fatalf("Failed to update version: %v", err)
}
_ = store.Close()
// Verify updated version
version = getDBVersion(dbPath)
if version != Version {
t.Errorf("Expected version %s, got %s", Version, version)
}
})
}
func TestFormatDBList(t *testing.T) {
dbs := []*dbInfo{
{path: "/tmp/.beads/beads.db", version: "0.17.5"},
{path: "/tmp/.beads/old.db", version: "0.16.0"},
}
result := formatDBList(dbs)
if len(result) != 2 {
t.Fatalf("Expected 2 results, got %d", len(result))
}
if result[0]["name"] != "beads.db" {
t.Errorf("Expected name beads.db, got %s", result[0]["name"])
}
if result[0]["version"] != "0.17.5" {
t.Errorf("Expected version 0.17.5, got %s", result[0]["version"])
}
if result[1]["name"] != "old.db" {
t.Errorf("Expected name old.db, got %s", result[1]["name"])
}
if result[1]["version"] != "0.16.0" {
t.Errorf("Expected version 0.16.0, got %s", result[1]["version"])
}
}
func TestMigrateRespectsConfigJSON(t *testing.T) {
// Test that migrate respects custom database name from metadata.json
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0750); err != nil {
t.Fatalf("Failed to create .beads directory: %v", err)
}
// Create metadata.json with custom database name
configPath := filepath.Join(beadsDir, "metadata.json")
configData := `{"database": "beady.db", "version": "0.21.1", "jsonl_export": "beady.jsonl"}`
if err := os.WriteFile(configPath, []byte(configData), 0600); err != nil {
t.Fatalf("Failed to create metadata.json: %v", err)
}
// Create old database with custom name
oldDBPath := filepath.Join(beadsDir, "beady.db")
store, err := sqlite.New(context.Background(), oldDBPath)
if err != nil {
t.Fatalf("Failed to create database: %v", err)
}
ctx := context.Background()
if err := store.SetMetadata(ctx, "bd_version", "0.21.1"); err != nil {
t.Fatalf("Failed to set version: %v", err)
}
_ = store.Close()
// Load config
cfg, err := loadOrCreateConfig(beadsDir)
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
// Verify config respects custom database name
if cfg.Database != "beady.db" {
t.Errorf("Expected database name 'beady.db', got %s", cfg.Database)
}
expectedPath := filepath.Join(beadsDir, "beady.db")
actualPath := cfg.DatabasePath(beadsDir)
if actualPath != expectedPath {
t.Errorf("Expected path %s, got %s", expectedPath, actualPath)
}
// Verify database exists at custom path
if _, err := os.Stat(actualPath); os.IsNotExist(err) {
t.Errorf("Database does not exist at custom path: %s", actualPath)
}
}