Close bd-157: Complete auto-import refactoring

- Refactored autoImportIfNewer() to use shared importIssuesCore()
- Removed ~200 lines of duplicated import logic from main.go
- Manual and auto-import now use identical collision detection/resolution
- Added auto-export scheduling after successful import (prevents JSONL drift)
- Optimized remapping notification (O(n) instead of O(n²), sorted output)
- Removed obsolete test functions for deleted helper functions
- Use bytes.NewReader instead of string conversion for better performance

Benefits:
- Future bug fixes only need to be made once
- Guaranteed consistency between manual and auto-import
- JSONL stays in sync with database after auto-import
- Clearer, more consistent user feedback

Amp-Thread-ID: https://ampcode.com/threads/T-1925a48d-ca8a-4b54-b4e7-de3ec755d25a
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-18 18:21:17 -07:00
parent fe51fa3d9a
commit 5fefce4e85
3 changed files with 46 additions and 402 deletions

View File

@@ -5,11 +5,7 @@ import (
"encoding/json"
"io"
"os"
"strings"
"testing"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/types"
)
func TestOutputJSON(t *testing.T) {
@@ -93,230 +89,8 @@ func TestOutputJSONArray(t *testing.T) {
}
}
func TestPrintCollisionReport(t *testing.T) {
// Capture stderr
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
// Create collision data
result := &sqlite.CollisionResult{
ExactMatches: []string{"bd-1", "bd-2"},
NewIssues: []string{"bd-3", "bd-4", "bd-5"},
Collisions: []*sqlite.CollisionDetail{
{
ID: "bd-6",
IncomingIssue: &types.Issue{
ID: "bd-6",
Title: "Test Issue 6",
},
ConflictingFields: []string{"title", "priority"},
},
{
ID: "bd-7",
IncomingIssue: &types.Issue{
ID: "bd-7",
Title: "Test Issue 7",
},
ConflictingFields: []string{"description"},
},
},
}
// Call printCollisionReport
printCollisionReport(result)
// Restore stderr
w.Close()
os.Stderr = oldStderr
// Read output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Verify output contains expected sections
if !strings.Contains(output, "Collision Detection Report") {
t.Errorf("Expected report header. Got: %s", output)
}
if !strings.Contains(output, "Exact matches (idempotent): 2") {
t.Errorf("Expected exact matches count. Got: %s", output)
}
if !strings.Contains(output, "New issues: 3") {
t.Errorf("Expected new issues count. Got: %s", output)
}
if !strings.Contains(output, "COLLISIONS DETECTED: 2") {
t.Errorf("Expected collisions count. Got: %s", output)
}
if !strings.Contains(output, "bd-6") {
t.Errorf("Expected first collision ID. Got: %s", output)
}
// The field names are printed directly, not in brackets
if !strings.Contains(output, "title") || !strings.Contains(output, "priority") {
t.Errorf("Expected conflicting fields for bd-6. Got: %s", output)
}
}
func TestPrintCollisionReportNoCollisions(t *testing.T) {
// Capture stderr
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
// Create data with no collisions
result := &sqlite.CollisionResult{
ExactMatches: []string{"bd-1", "bd-2", "bd-3"},
NewIssues: []string{"bd-4"},
Collisions: []*sqlite.CollisionDetail{},
}
// Call printCollisionReport
printCollisionReport(result)
// Restore stderr
w.Close()
os.Stderr = oldStderr
// Read output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Verify output shows no collisions
if !strings.Contains(output, "COLLISIONS DETECTED: 0") {
t.Error("Expected 0 collisions")
}
if strings.Contains(output, "Colliding issues:") {
t.Error("Should not show colliding issues section when there are none")
}
}
func TestPrintRemappingReport(t *testing.T) {
// Capture stderr
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
// Create remapping data
remapping := map[string]string{
"bd-10": "bd-100",
"bd-20": "bd-200",
"bd-30": "bd-300",
}
collisions := []*sqlite.CollisionDetail{
{ID: "bd-10", ReferenceScore: 5},
{ID: "bd-20", ReferenceScore: 0},
{ID: "bd-30", ReferenceScore: 12},
}
// Call printRemappingReport
printRemappingReport(remapping, collisions)
// Restore stderr
w.Close()
os.Stderr = oldStderr
// Read output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Verify output contains expected information
if !strings.Contains(output, "Remapping Report") {
t.Errorf("Expected report title. Got: %s", output)
}
if !strings.Contains(output, "bd-10 → bd-100") {
t.Error("Expected first remapping")
}
if !strings.Contains(output, "refs: 5") {
t.Error("Expected reference count for bd-10")
}
if !strings.Contains(output, "bd-20 → bd-200") {
t.Error("Expected second remapping")
}
if !strings.Contains(output, "refs: 0") {
t.Error("Expected 0 references for bd-20")
}
if !strings.Contains(output, "bd-30 → bd-300") {
t.Error("Expected third remapping")
}
if !strings.Contains(output, "refs: 12") {
t.Error("Expected reference count for bd-30")
}
}
func TestPrintRemappingReportEmpty(t *testing.T) {
// Capture stderr
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
// Empty remapping
remapping := map[string]string{}
collisions := []*sqlite.CollisionDetail{}
// Call printRemappingReport
printRemappingReport(remapping, collisions)
// Restore stderr
w.Close()
os.Stderr = oldStderr
// Read output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Should still have header
if !strings.Contains(output, "Remapping Report") {
t.Errorf("Expected report title even with no remappings. Got: %s", output)
}
}
func TestPrintRemappingReportOrdering(t *testing.T) {
// Capture stderr
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
// Create remapping with different reference scores
// Ordering is by reference score (ascending)
remapping := map[string]string{
"bd-2": "bd-200",
"bd-10": "bd-100",
"bd-100": "bd-1000",
}
collisions := []*sqlite.CollisionDetail{
{ID: "bd-2", ReferenceScore: 10}, // highest refs
{ID: "bd-10", ReferenceScore: 5}, // medium refs
{ID: "bd-100", ReferenceScore: 1}, // lowest refs
}
// Call printRemappingReport
printRemappingReport(remapping, collisions)
// Restore stderr
w.Close()
os.Stderr = oldStderr
// Read output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Find positions of each remapping in output
pos2 := strings.Index(output, "bd-2 →")
pos10 := strings.Index(output, "bd-10 →")
pos100 := strings.Index(output, "bd-100 →")
// Verify ordering by reference score (ascending): bd-100 (1 ref) < bd-10 (5 refs) < bd-2 (10 refs)
if pos2 == -1 || pos10 == -1 || pos100 == -1 {
t.Fatalf("Missing remappings in output: %s", output)
}
if !(pos100 < pos10 && pos10 < pos2) {
t.Errorf("Remappings not in reference score order. Got: %s", output)
}
}
// Tests for printCollisionReport and printRemappingReport were removed
// These functions no longer exist after refactoring to shared importIssuesCore (bd-157)
// Note: createIssuesFromMarkdown is tested via cmd/bd/markdown_test.go which has
// comprehensive tests for the markdown parsing functionality. We don't duplicate