* fix(beads): prevent routes.jsonl corruption from bd auto-export When issues.jsonl doesn't exist, bd's auto-export mechanism writes issue data to routes.jsonl, corrupting the routing configuration. Changes: - install.go: Create issues.jsonl before routes.jsonl at town level - manager.go: Create issues.jsonl in rig beads; don't create routes.jsonl (rig-level routes.jsonl breaks bd's walk-up routing to town routes) - Add integration tests for routes.jsonl corruption prevention Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(doctor): add check to detect and fix rig-level routes.jsonl Add RigRoutesJSONLCheck to detect routes.jsonl files in rig .beads directories. These files break bd's walk-up routing to town-level routes.jsonl, causing cross-rig routing failures. The fix unconditionally deletes rig-level routes.jsonl files since bd will auto-export to issues.jsonl on next run. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(rig): add verification that routes.jsonl does NOT exist in rig .beads Add explicit test assertion and detailed comment explaining why rig-level routes.jsonl files must not exist (breaks bd walk-up routing to town routes). Also verify that issues.jsonl DOES exist (prevents bd auto-export corruption). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(doctor): ensure town root route exists in routes.jsonl The RoutesCheck now detects and fixes missing town root routes (hq- -> .). This can happen when routes.jsonl is corrupted or was created without the town route during initialization. Changes: - Detect missing hq- route in Run() - Add hq- route in Fix() when missing - Handle case where routes.jsonl is corrupted (regenerate with town route) - Add comprehensive unit tests for route detection and fixing Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(beads): fix routing integration test for routes.jsonl corruption The TestBeadsRoutingFromTownRoot test was failing because bd's auto-export mechanism writes issue data to routes.jsonl when issues.jsonl doesn't exist. This corrupts the routing configuration. Fix: Create empty issues.jsonl after bd init to prevent corruption. This mirrors what gt install does to prevent the same bug. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: julianknutsen <julianknutsen@users.noreply.github> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
305 lines
8.5 KiB
Go
305 lines
8.5 KiB
Go
package doctor
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestRoutesCheck_MissingTownRoute(t *testing.T) {
|
|
t.Run("detects missing town root route", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create .beads directory with routes.jsonl missing the hq- route
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create routes.jsonl with only a rig route (no hq- route)
|
|
routesPath := filepath.Join(beadsDir, "routes.jsonl")
|
|
routesContent := `{"prefix": "gt-", "path": "gastown/mayor/rig"}
|
|
`
|
|
if err := os.WriteFile(routesPath, []byte(routesContent), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create mayor directory
|
|
if err := os.MkdirAll(filepath.Join(tmpDir, "mayor"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
check := NewRoutesCheck()
|
|
ctx := &CheckContext{TownRoot: tmpDir}
|
|
result := check.Run(ctx)
|
|
|
|
if result.Status != StatusWarning {
|
|
t.Errorf("expected StatusWarning, got %v: %s", result.Status, result.Message)
|
|
}
|
|
// When no rigs.json exists, the message comes from the early return path
|
|
if result.Message != "Town root route is missing" {
|
|
t.Errorf("expected 'Town root route is missing', got %s", result.Message)
|
|
}
|
|
})
|
|
|
|
t.Run("passes when town root route exists", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create .beads directory with valid routes.jsonl
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create routes.jsonl with hq- route
|
|
routesPath := filepath.Join(beadsDir, "routes.jsonl")
|
|
routesContent := `{"prefix": "hq-", "path": "."}
|
|
`
|
|
if err := os.WriteFile(routesPath, []byte(routesContent), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create mayor directory
|
|
if err := os.MkdirAll(filepath.Join(tmpDir, "mayor"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
check := NewRoutesCheck()
|
|
ctx := &CheckContext{TownRoot: tmpDir}
|
|
result := check.Run(ctx)
|
|
|
|
if result.Status != StatusOK {
|
|
t.Errorf("expected StatusOK, got %v: %s", result.Status, result.Message)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestRoutesCheck_FixRestoresTownRoute(t *testing.T) {
|
|
t.Run("fix adds missing town root route", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create .beads directory with empty routes.jsonl
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create empty routes.jsonl
|
|
routesPath := filepath.Join(beadsDir, "routes.jsonl")
|
|
if err := os.WriteFile(routesPath, []byte(""), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create mayor directory (no rigs.json needed for this test)
|
|
if err := os.MkdirAll(filepath.Join(tmpDir, "mayor"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
check := NewRoutesCheck()
|
|
ctx := &CheckContext{TownRoot: tmpDir}
|
|
|
|
// Run fix
|
|
if err := check.Fix(ctx); err != nil {
|
|
t.Fatalf("Fix failed: %v", err)
|
|
}
|
|
|
|
// Verify routes.jsonl now contains hq- route
|
|
content, err := os.ReadFile(routesPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read routes.jsonl: %v", err)
|
|
}
|
|
|
|
if len(content) == 0 {
|
|
t.Error("routes.jsonl is still empty after fix")
|
|
}
|
|
|
|
contentStr := string(content)
|
|
if contentStr != `{"prefix":"hq-","path":"."}
|
|
` {
|
|
t.Errorf("unexpected routes.jsonl content: %s", contentStr)
|
|
}
|
|
|
|
// Verify the check now passes
|
|
result := check.Run(ctx)
|
|
if result.Status != StatusOK {
|
|
t.Errorf("expected StatusOK after fix, got %v: %s", result.Status, result.Message)
|
|
}
|
|
})
|
|
|
|
t.Run("fix preserves existing routes while adding town route", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create .beads directory
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create rig directory structure for route validation
|
|
rigPath := filepath.Join(tmpDir, "myrig", "mayor", "rig", ".beads")
|
|
if err := os.MkdirAll(rigPath, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create routes.jsonl with only a rig route (no hq- route)
|
|
routesPath := filepath.Join(beadsDir, "routes.jsonl")
|
|
routesContent := `{"prefix": "my-", "path": "myrig/mayor/rig"}
|
|
`
|
|
if err := os.WriteFile(routesPath, []byte(routesContent), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create mayor directory
|
|
if err := os.MkdirAll(filepath.Join(tmpDir, "mayor"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
check := NewRoutesCheck()
|
|
ctx := &CheckContext{TownRoot: tmpDir}
|
|
|
|
// Run fix
|
|
if err := check.Fix(ctx); err != nil {
|
|
t.Fatalf("Fix failed: %v", err)
|
|
}
|
|
|
|
// Verify routes.jsonl now contains both routes
|
|
content, err := os.ReadFile(routesPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read routes.jsonl: %v", err)
|
|
}
|
|
|
|
contentStr := string(content)
|
|
// Should have both the original rig route and the new hq- route
|
|
if contentStr != `{"prefix":"my-","path":"myrig/mayor/rig"}
|
|
{"prefix":"hq-","path":"."}
|
|
` {
|
|
t.Errorf("unexpected routes.jsonl content: %s", contentStr)
|
|
}
|
|
})
|
|
|
|
t.Run("fix does not duplicate existing town route", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create .beads directory with valid routes.jsonl
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create routes.jsonl with hq- route already present
|
|
routesPath := filepath.Join(beadsDir, "routes.jsonl")
|
|
originalContent := `{"prefix": "hq-", "path": "."}
|
|
`
|
|
if err := os.WriteFile(routesPath, []byte(originalContent), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create mayor directory
|
|
if err := os.MkdirAll(filepath.Join(tmpDir, "mayor"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
check := NewRoutesCheck()
|
|
ctx := &CheckContext{TownRoot: tmpDir}
|
|
|
|
// Run fix (should be a no-op)
|
|
if err := check.Fix(ctx); err != nil {
|
|
t.Fatalf("Fix failed: %v", err)
|
|
}
|
|
|
|
// Verify routes.jsonl is unchanged (no duplicate)
|
|
content, err := os.ReadFile(routesPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read routes.jsonl: %v", err)
|
|
}
|
|
|
|
// File should be unchanged - fix doesn't write when no modifications needed
|
|
if string(content) != originalContent {
|
|
t.Errorf("routes.jsonl was modified when it shouldn't have been: %s", string(content))
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestRoutesCheck_CorruptedRoutesJsonl(t *testing.T) {
|
|
t.Run("corrupted routes.jsonl results in empty routes", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create .beads directory with corrupted routes.jsonl
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create corrupted routes.jsonl (malformed lines are skipped by LoadRoutes)
|
|
routesPath := filepath.Join(beadsDir, "routes.jsonl")
|
|
if err := os.WriteFile(routesPath, []byte("not valid json"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create mayor directory
|
|
if err := os.MkdirAll(filepath.Join(tmpDir, "mayor"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
check := NewRoutesCheck()
|
|
ctx := &CheckContext{TownRoot: tmpDir}
|
|
result := check.Run(ctx)
|
|
|
|
// Corrupted/malformed lines are skipped, resulting in empty routes
|
|
// This triggers the "Town root route is missing" warning
|
|
if result.Status != StatusWarning {
|
|
t.Errorf("expected StatusWarning, got %v: %s", result.Status, result.Message)
|
|
}
|
|
if result.Message != "Town root route is missing" {
|
|
t.Errorf("expected 'Town root route is missing', got %s", result.Message)
|
|
}
|
|
})
|
|
|
|
t.Run("fix regenerates corrupted routes.jsonl with town route", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create .beads directory with corrupted routes.jsonl
|
|
beadsDir := filepath.Join(tmpDir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create corrupted routes.jsonl
|
|
routesPath := filepath.Join(beadsDir, "routes.jsonl")
|
|
if err := os.WriteFile(routesPath, []byte("not valid json"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create mayor directory
|
|
if err := os.MkdirAll(filepath.Join(tmpDir, "mayor"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
check := NewRoutesCheck()
|
|
ctx := &CheckContext{TownRoot: tmpDir}
|
|
|
|
// Run fix
|
|
if err := check.Fix(ctx); err != nil {
|
|
t.Fatalf("Fix failed: %v", err)
|
|
}
|
|
|
|
// Verify routes.jsonl now contains hq- route
|
|
content, err := os.ReadFile(routesPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read routes.jsonl: %v", err)
|
|
}
|
|
|
|
contentStr := string(content)
|
|
if contentStr != `{"prefix":"hq-","path":"."}
|
|
` {
|
|
t.Errorf("unexpected routes.jsonl content after fix: %s", contentStr)
|
|
}
|
|
|
|
// Verify the check now passes
|
|
result := check.Run(ctx)
|
|
if result.Status != StatusOK {
|
|
t.Errorf("expected StatusOK after fix, got %v: %s", result.Status, result.Message)
|
|
}
|
|
})
|
|
}
|