Files
beads/cmd/bd/doctor/fix/database_config_test.go
Steve Yegge d1641c7d2e Fix bd-afd: Auto-fix metadata.json jsonl_export mismatch
## Summary

When metadata.json gets deleted (git clean, merge conflict, rebase), the
version tracking code auto-recreates it using DefaultConfig() which hardcoded
jsonl_export to 'issues.jsonl'. But many repos (including beads itself) use
'beads.jsonl', causing a mismatch between config and actual JSONL file.

## Changes

1. **bd doctor --fix auto-detection** (cmd/bd/doctor/fix/database_config.go)
   - New DatabaseConfig() fix function that auto-detects actual JSONL file
   - Prefers beads.jsonl over issues.jsonl (canonical name)
   - Skips backup files and merge artifacts
   - Wired into doctor.go applyFixes()

2. **Version tracking auto-detection** (cmd/bd/version_tracking.go)
   - trackBdVersion() now scans for existing JSONL files before defaulting
   - Prevents mismatches when metadata.json gets recreated
   - Added findActualJSONLFile() helper function

3. **Canonical default name** (internal/configfile/configfile.go)
   - DefaultConfig() changed from issues.jsonl to beads.jsonl
   - Aligns with canonical naming convention

4. **FindJSONLPath preference** (internal/beads/beads.go)
   - Now prefers beads.jsonl over issues.jsonl when scanning
   - Default changed from issues.jsonl to beads.jsonl

5. **Test coverage**
   - Added comprehensive tests for DatabaseConfig fix
   - Updated configfile tests for new default
   - Verified backup file skipping logic

## Testing

- All existing tests pass
- New tests verify auto-fix behavior
- Integration tested with simulated mismatches

Closes: bd-afd
2025-11-23 23:11:08 -08:00

125 lines
3.7 KiB
Go

package fix
import (
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/configfile"
)
// TestDatabaseConfigFix_JSONLMismatch tests that DatabaseConfig fixes JSONL mismatches.
// bd-afd: Verify auto-fix for metadata.json jsonl_export mismatch
func TestDatabaseConfigFix_JSONLMismatch(t *testing.T) {
// Create temporary directory
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.Mkdir(beadsDir, 0755); err != nil {
t.Fatalf("Failed to create .beads dir: %v", err)
}
// Create beads.jsonl file (actual JSONL)
jsonlPath := filepath.Join(beadsDir, "beads.jsonl")
if err := os.WriteFile(jsonlPath, []byte(`{"id":"test-123"}`), 0644); err != nil {
t.Fatalf("Failed to create beads.jsonl: %v", err)
}
// Create metadata.json with wrong JSONL filename (issues.jsonl)
cfg := &configfile.Config{
Database: "beads.db",
JSONLExport: "issues.jsonl", // Wrong - should be beads.jsonl
}
if err := cfg.Save(beadsDir); err != nil {
t.Fatalf("Failed to save config: %v", err)
}
// Run the fix
if err := DatabaseConfig(tmpDir); err != nil {
t.Fatalf("DatabaseConfig failed: %v", err)
}
// Verify the config was updated
updatedCfg, err := configfile.Load(beadsDir)
if err != nil {
t.Fatalf("Failed to load updated config: %v", err)
}
if updatedCfg.JSONLExport != "beads.jsonl" {
t.Errorf("Expected JSONLExport to be 'beads.jsonl', got %q", updatedCfg.JSONLExport)
}
}
// TestDatabaseConfigFix_PrefersBeadsJSONL tests that DatabaseConfig prefers beads.jsonl over issues.jsonl.
func TestDatabaseConfigFix_PrefersBeadsJSONL(t *testing.T) {
// Create temporary directory
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.Mkdir(beadsDir, 0755); err != nil {
t.Fatalf("Failed to create .beads dir: %v", err)
}
// Create both beads.jsonl and issues.jsonl
beadsJSONL := filepath.Join(beadsDir, "beads.jsonl")
if err := os.WriteFile(beadsJSONL, []byte(`{"id":"test-123"}`), 0644); err != nil {
t.Fatalf("Failed to create beads.jsonl: %v", err)
}
issuesJSONL := filepath.Join(beadsDir, "issues.jsonl")
if err := os.WriteFile(issuesJSONL, []byte(`{"id":"test-456"}`), 0644); err != nil {
t.Fatalf("Failed to create issues.jsonl: %v", err)
}
// Create metadata.json with wrong JSONL filename (old.jsonl)
cfg := &configfile.Config{
Database: "beads.db",
JSONLExport: "old.jsonl", // Wrong - should prefer beads.jsonl
}
if err := cfg.Save(beadsDir); err != nil {
t.Fatalf("Failed to save config: %v", err)
}
// Run the fix
if err := DatabaseConfig(tmpDir); err != nil {
t.Fatalf("DatabaseConfig failed: %v", err)
}
// Verify the config was updated to beads.jsonl (not issues.jsonl)
updatedCfg, err := configfile.Load(beadsDir)
if err != nil {
t.Fatalf("Failed to load updated config: %v", err)
}
if updatedCfg.JSONLExport != "beads.jsonl" {
t.Errorf("Expected JSONLExport to be 'beads.jsonl', got %q", updatedCfg.JSONLExport)
}
}
// TestFindActualJSONLFile_SkipsBackups tests that backup files are skipped.
func TestFindActualJSONLFile_SkipsBackups(t *testing.T) {
// Create temporary directory
tmpDir := t.TempDir()
// Create beads.jsonl and various backup files
files := []string{
"beads.jsonl",
"beads.jsonl.backup",
"backup_beads.jsonl",
"beads.jsonl.orig",
"beads.jsonl.bak",
"beads.jsonl~",
}
for _, name := range files {
path := filepath.Join(tmpDir, name)
if err := os.WriteFile(path, []byte(`{"id":"test"}`), 0644); err != nil {
t.Fatalf("Failed to create %s: %v", name, err)
}
}
// findActualJSONLFile should return beads.jsonl (not backups)
result := findActualJSONLFile(tmpDir)
if result != "beads.jsonl" {
t.Errorf("Expected 'beads.jsonl', got %q", result)
}
}