Add unit tests for nodb.go and daemon/discovery.go
- Added tests for extractIssuePrefix, loadIssuesFromJSONL, detectPrefix, writeIssuesToJSONL - Added tests for walkWithDepth depth limiting and hidden directory skipping - Added tests for DiscoverDaemons registry and legacy discovery paths - Improved test coverage for cmd/bd and internal/daemon
This commit is contained in:
193
cmd/bd/nodb_test.go
Normal file
193
cmd/bd/nodb_test.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/memory"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestExtractIssuePrefix(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
issueID string
|
||||
expected string
|
||||
}{
|
||||
{"standard ID", "bd-123", "bd"},
|
||||
{"custom prefix", "myproject-456", "myproject"},
|
||||
{"hash ID", "bd-abc123def", "bd"},
|
||||
{"no hyphen", "nohyphen", ""},
|
||||
{"empty", "", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := extractIssuePrefix(tt.issueID)
|
||||
if got != tt.expected {
|
||||
t.Errorf("extractIssuePrefix(%q) = %q, want %q", tt.issueID, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadIssuesFromJSONL(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
jsonlPath := filepath.Join(tempDir, "test.jsonl")
|
||||
|
||||
// Create test JSONL file
|
||||
content := `{"id":"bd-1","title":"Test Issue 1","description":"Test"}
|
||||
{"id":"bd-2","title":"Test Issue 2","description":"Another test"}
|
||||
|
||||
{"id":"bd-3","title":"Test Issue 3","description":"Third test"}
|
||||
`
|
||||
if err := os.WriteFile(jsonlPath, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("Failed to write test file: %v", err)
|
||||
}
|
||||
|
||||
issues, err := loadIssuesFromJSONL(jsonlPath)
|
||||
if err != nil {
|
||||
t.Fatalf("loadIssuesFromJSONL failed: %v", err)
|
||||
}
|
||||
|
||||
if len(issues) != 3 {
|
||||
t.Errorf("Expected 3 issues, got %d", len(issues))
|
||||
}
|
||||
|
||||
if issues[0].ID != "bd-1" || issues[0].Title != "Test Issue 1" {
|
||||
t.Errorf("First issue mismatch: %+v", issues[0])
|
||||
}
|
||||
if issues[1].ID != "bd-2" {
|
||||
t.Errorf("Second issue ID mismatch: %s", issues[1].ID)
|
||||
}
|
||||
if issues[2].ID != "bd-3" {
|
||||
t.Errorf("Third issue ID mismatch: %s", issues[2].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadIssuesFromJSONL_InvalidJSON(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
jsonlPath := filepath.Join(tempDir, "invalid.jsonl")
|
||||
|
||||
content := `{"id":"bd-1","title":"Valid"}
|
||||
invalid json here
|
||||
{"id":"bd-2","title":"Another valid"}
|
||||
`
|
||||
if err := os.WriteFile(jsonlPath, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("Failed to write test file: %v", err)
|
||||
}
|
||||
|
||||
_, err := loadIssuesFromJSONL(jsonlPath)
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid JSON, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadIssuesFromJSONL_NonExistent(t *testing.T) {
|
||||
_, err := loadIssuesFromJSONL("/nonexistent/file.jsonl")
|
||||
if err == nil {
|
||||
t.Error("Expected error for non-existent file, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectPrefix(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tempDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
||||
t.Fatalf("Failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
t.Run("from existing issues", func(t *testing.T) {
|
||||
memStore := memory.New(filepath.Join(beadsDir, "issues.jsonl"))
|
||||
|
||||
// Add issues with common prefix
|
||||
issues := []*types.Issue{
|
||||
{ID: "myapp-1", Title: "Issue 1"},
|
||||
{ID: "myapp-2", Title: "Issue 2"},
|
||||
}
|
||||
if err := memStore.LoadFromIssues(issues); err != nil {
|
||||
t.Fatalf("Failed to load issues: %v", err)
|
||||
}
|
||||
|
||||
prefix, err := detectPrefix(beadsDir, memStore)
|
||||
if err != nil {
|
||||
t.Fatalf("detectPrefix failed: %v", err)
|
||||
}
|
||||
if prefix != "myapp" {
|
||||
t.Errorf("Expected prefix 'myapp', got '%s'", prefix)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("mixed prefixes error", func(t *testing.T) {
|
||||
memStore := memory.New(filepath.Join(beadsDir, "issues.jsonl"))
|
||||
|
||||
issues := []*types.Issue{
|
||||
{ID: "app1-1", Title: "Issue 1"},
|
||||
{ID: "app2-2", Title: "Issue 2"},
|
||||
}
|
||||
if err := memStore.LoadFromIssues(issues); err != nil {
|
||||
t.Fatalf("Failed to load issues: %v", err)
|
||||
}
|
||||
|
||||
_, err := detectPrefix(beadsDir, memStore)
|
||||
if err == nil {
|
||||
t.Error("Expected error for mixed prefixes, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty database defaults to dir name", func(t *testing.T) {
|
||||
// Change to temp dir so we can control directory name
|
||||
origWd, _ := os.Getwd()
|
||||
namedDir := filepath.Join(tempDir, "myproject")
|
||||
if err := os.MkdirAll(namedDir, 0o755); err != nil {
|
||||
t.Fatalf("Failed to create named dir: %v", err)
|
||||
}
|
||||
if err := os.Chdir(namedDir); err != nil {
|
||||
t.Fatalf("Failed to chdir: %v", err)
|
||||
}
|
||||
defer func() { _ = os.Chdir(origWd) }()
|
||||
|
||||
memStore := memory.New(filepath.Join(beadsDir, "issues.jsonl"))
|
||||
prefix, err := detectPrefix(beadsDir, memStore)
|
||||
if err != nil {
|
||||
t.Fatalf("detectPrefix failed: %v", err)
|
||||
}
|
||||
if prefix != "myproject" {
|
||||
t.Errorf("Expected prefix 'myproject', got '%s'", prefix)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestWriteIssuesToJSONL(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tempDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
||||
t.Fatalf("Failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
memStore := memory.New(filepath.Join(beadsDir, "issues.jsonl"))
|
||||
|
||||
issues := []*types.Issue{
|
||||
{ID: "bd-1", Title: "Test Issue 1", Description: "Desc 1"},
|
||||
{ID: "bd-2", Title: "Test Issue 2", Description: "Desc 2"},
|
||||
}
|
||||
if err := memStore.LoadFromIssues(issues); err != nil {
|
||||
t.Fatalf("Failed to load issues: %v", err)
|
||||
}
|
||||
|
||||
if err := writeIssuesToJSONL(memStore, beadsDir); err != nil {
|
||||
t.Fatalf("writeIssuesToJSONL failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify file exists and contains correct data
|
||||
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
|
||||
loadedIssues, err := loadIssuesFromJSONL(jsonlPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load written JSONL: %v", err)
|
||||
}
|
||||
|
||||
if len(loadedIssues) != 2 {
|
||||
t.Errorf("Expected 2 issues in JSONL, got %d", len(loadedIssues))
|
||||
}
|
||||
}
|
||||
@@ -115,3 +115,146 @@ func TestCleanupStaleSockets(t *testing.T) {
|
||||
t.Error("stale socket still exists")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalkWithDepth(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create test directory structure
|
||||
// tmpDir/
|
||||
// file1.txt
|
||||
// dir1/
|
||||
// file2.txt
|
||||
// dir2/
|
||||
// file3.txt
|
||||
// dir3/
|
||||
// file4.txt
|
||||
|
||||
os.WriteFile(filepath.Join(tmpDir, "file1.txt"), []byte("test"), 0644)
|
||||
os.MkdirAll(filepath.Join(tmpDir, "dir1", "dir2", "dir3"), 0755)
|
||||
os.WriteFile(filepath.Join(tmpDir, "dir1", "file2.txt"), []byte("test"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "dir1", "dir2", "file3.txt"), []byte("test"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "dir1", "dir2", "dir3", "file4.txt"), []byte("test"), 0644)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
maxDepth int
|
||||
wantFiles int
|
||||
}{
|
||||
{"depth 0", 0, 1}, // Only file1.txt
|
||||
{"depth 1", 1, 2}, // file1.txt, file2.txt
|
||||
{"depth 2", 2, 3}, // file1.txt, file2.txt, file3.txt
|
||||
{"depth 3", 3, 4}, // All files
|
||||
{"depth 10", 10, 4}, // All files (max depth not reached)
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var foundFiles []string
|
||||
fn := func(path string, info os.FileInfo) error {
|
||||
if !info.IsDir() {
|
||||
foundFiles = append(foundFiles, path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := walkWithDepth(tmpDir, 0, tt.maxDepth, fn)
|
||||
if err != nil {
|
||||
t.Fatalf("walkWithDepth failed: %v", err)
|
||||
}
|
||||
|
||||
if len(foundFiles) != tt.wantFiles {
|
||||
t.Errorf("Expected %d files, got %d: %v", tt.wantFiles, len(foundFiles), foundFiles)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalkWithDepth_SkipsHiddenDirs(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create hidden directories (should skip)
|
||||
os.MkdirAll(filepath.Join(tmpDir, ".git"), 0755)
|
||||
os.MkdirAll(filepath.Join(tmpDir, ".hidden"), 0755)
|
||||
os.MkdirAll(filepath.Join(tmpDir, "node_modules"), 0755)
|
||||
os.MkdirAll(filepath.Join(tmpDir, "vendor"), 0755)
|
||||
|
||||
// Create .beads directory (should NOT skip)
|
||||
os.MkdirAll(filepath.Join(tmpDir, ".beads"), 0755)
|
||||
|
||||
// Add files
|
||||
os.WriteFile(filepath.Join(tmpDir, ".git", "config"), []byte("test"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, ".hidden", "secret"), []byte("test"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "node_modules", "package.json"), []byte("test"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, ".beads", "beads.db"), []byte("test"), 0644)
|
||||
|
||||
var foundFiles []string
|
||||
fn := func(path string, info os.FileInfo) error {
|
||||
if !info.IsDir() {
|
||||
foundFiles = append(foundFiles, filepath.Base(path))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := walkWithDepth(tmpDir, 0, 5, fn)
|
||||
if err != nil {
|
||||
t.Fatalf("walkWithDepth failed: %v", err)
|
||||
}
|
||||
|
||||
// Should only find beads.db from .beads directory
|
||||
if len(foundFiles) != 1 || foundFiles[0] != "beads.db" {
|
||||
t.Errorf("Expected only beads.db, got: %v", foundFiles)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverDaemons_Registry(t *testing.T) {
|
||||
// Test registry-based discovery (no search roots)
|
||||
daemons, err := DiscoverDaemons(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("DiscoverDaemons failed: %v", err)
|
||||
}
|
||||
|
||||
// Should return empty list (no daemons running in test environment)
|
||||
// Just verify it doesn't error
|
||||
_ = daemons
|
||||
}
|
||||
|
||||
func TestDiscoverDaemons_Legacy(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
os.MkdirAll(beadsDir, 0755)
|
||||
|
||||
// Start a test daemon
|
||||
dbPath := filepath.Join(beadsDir, "test.db")
|
||||
socketPath := filepath.Join(beadsDir, "bd.sock")
|
||||
store, err := sqlite.New(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create storage: %v", err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
server := rpc.NewServer(socketPath, store, tmpDir, dbPath)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go server.Start(ctx)
|
||||
<-server.WaitReady()
|
||||
defer server.Stop()
|
||||
|
||||
// Test legacy discovery with explicit search roots
|
||||
daemons, err := DiscoverDaemons([]string{tmpDir})
|
||||
if err != nil {
|
||||
t.Fatalf("DiscoverDaemons failed: %v", err)
|
||||
}
|
||||
|
||||
if len(daemons) != 1 {
|
||||
t.Fatalf("Expected 1 daemon, got %d", len(daemons))
|
||||
}
|
||||
|
||||
daemon := daemons[0]
|
||||
if !daemon.Alive {
|
||||
t.Errorf("Daemon not alive: %s", daemon.Error)
|
||||
}
|
||||
if daemon.WorkspacePath != tmpDir {
|
||||
t.Errorf("Wrong workspace path: expected %s, got %s", tmpDir, daemon.WorkspacePath)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user