beads/cmd/bd/diff_embedded_test.go
matt wilkie 234da63193
fix: resolve remaining 11 CI test failures (#2983)
* fix: resolve remaining 11 CI test failures (#2982)

Two categories of fixes:

1. Flock contention in test setup helpers: bdCreate, bdCreateSilent,
   bdUpdate, bdDelete, bdClose, and bdDepAdd now retry up to 5 times
   with exponential backoff when they encounter "one writer at a time"
   flock errors. This handles transient contention when parallel tests
   within the same CI shard compete for the embedded Dolt lock during
   their setup phase.

2. macOS path symlink mismatch: TestContext* tests in
   explicit_db_nodb_test.go now use filepath.EvalSymlinks() when
   comparing beads_dir paths, fixing failures where macOS resolves
   /var/folders to /private/var/folders.

Fixes #2982

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: increase flock retry budget and add retry to read helpers

The initial 5-retry/100ms-base backoff was insufficient for CI
contention. Refactored to a shared bdRunWithFlockRetry helper with
10 retries and 500ms base backoff (~30s total budget).

Also added flock retry to bdDiff, bdDiffJSON, bdShowRaw, and
bdShowDetails which were also failing on CI due to flock contention
during parallel test execution.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: close store before subprocess to release flock

The root cause of the persistent flock failures was openStore()
deferring Close() to t.Cleanup, keeping the exclusive flock held
while bd subprocess commands tried to acquire it. No amount of
retry can help when the same test holds the lock.

- getCommitHash: open/close store inline instead of via openStore
- show_json_includes_comments: close store after AddIssueComment
- show_as_of: close store after GetCurrentCommit

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 09:15:50 -07:00

349 lines
9.4 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
)
// bdDiff runs "bd diff" with the given args and returns raw stdout.
// Retries on flock contention.
func bdDiff(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"diff"}, args...)
out, err := bdRunWithFlockRetry(t, bd, dir, fullArgs...)
if err != nil {
t.Fatalf("bd diff %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
// bdDiffFail runs "bd diff" expecting failure.
func bdDiffFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"diff"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd diff %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
// bdDiffJSON runs "bd diff --json" and parses the result as a slice.
// Retries on flock contention.
func bdDiffJSON(t *testing.T, bd, dir string, args ...string) []map[string]interface{} {
t.Helper()
fullArgs := append([]string{"diff", "--json"}, args...)
out, err := bdRunWithFlockRetry(t, bd, dir, fullArgs...)
if err != nil {
t.Fatalf("bd diff --json %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
s := strings.TrimSpace(string(out))
start := strings.Index(s, "[")
if start < 0 {
// Could be "No changes" message — return empty
return nil
}
var entries []map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &entries); err != nil {
t.Fatalf("parse diff JSON: %v\n%s", err, s)
}
return entries
}
// getCommitHash returns the current HEAD commit hash via the store.
// The store is closed immediately to release the flock, allowing subsequent
// bd subprocess commands to acquire it.
func getCommitHash(t *testing.T, beadsDir, database string) string {
t.Helper()
s, err := embeddeddolt.New(t.Context(), beadsDir, database, "main")
if err != nil {
t.Fatalf("openStore for getCommitHash: %v", err)
}
hash, err := s.GetCurrentCommit(t.Context())
if err != nil {
s.Close()
t.Fatalf("GetCurrentCommit: %v", err)
}
s.Close()
return hash
}
func TestEmbeddedDiff(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "df")
// Capture initial commit (after init, before any issues).
hash0 := getCommitHash(t, beadsDir, "df")
// Create some issues — each create auto-commits.
issue1 := bdCreate(t, bd, dir, "Diff issue one", "--type", "task")
hash1 := getCommitHash(t, beadsDir, "df")
issue2 := bdCreate(t, bd, dir, "Diff issue two", "--type", "bug", "--priority", "1")
hash2 := getCommitHash(t, beadsDir, "df")
// ===== Basic diff between commits =====
t.Run("basic_diff_added", func(t *testing.T) {
out := bdDiff(t, bd, dir, hash0, hash2)
if !strings.Contains(out, issue1.ID) {
t.Errorf("expected issue1 ID in diff output: %s", out)
}
if !strings.Contains(out, issue2.ID) {
t.Errorf("expected issue2 ID in diff output: %s", out)
}
if !strings.Contains(out, "Added") {
t.Errorf("expected 'Added' section in diff output: %s", out)
}
})
// ===== Diff showing added, modified, removed =====
t.Run("diff_added_modified_removed", func(t *testing.T) {
// Modify issue1
bdUpdate(t, bd, dir, issue1.ID, "--status", "in_progress")
// Delete issue2
bdDelete(t, bd, dir, issue2.ID, "--force")
// Create a new issue
issue3 := bdCreate(t, bd, dir, "Diff issue three", "--type", "feature")
hashAfterAdd := getCommitHash(t, beadsDir, "df")
// Diff from hash2 (before modifications) to now should show:
// - issue1 modified (status changed)
// - issue2 removed
// - issue3 added
entries := bdDiffJSON(t, bd, dir, hash2, hashAfterAdd)
foundModified := false
foundRemoved := false
foundAdded := false
for _, e := range entries {
dt := e["DiffType"].(string)
id := e["IssueID"].(string)
if id == issue1.ID && dt == "modified" {
foundModified = true
}
if id == issue2.ID && dt == "removed" {
foundRemoved = true
}
if id == issue3.ID && dt == "added" {
foundAdded = true
}
}
if !foundModified {
t.Errorf("expected modified entry for %s", issue1.ID)
}
if !foundRemoved {
t.Errorf("expected removed entry for %s", issue2.ID)
}
if !foundAdded {
t.Errorf("expected added entry for %s", issue3.ID)
}
})
// ===== Diff with commit hashes =====
t.Run("diff_with_commit_hashes", func(t *testing.T) {
// Diff between two specific known commits
entries := bdDiffJSON(t, bd, dir, hash0, hash1)
if len(entries) == 0 {
t.Error("expected at least 1 diff entry between init and first create")
}
// Should show issue1 as added
found := false
for _, e := range entries {
if e["IssueID"] == issue1.ID && e["DiffType"] == "added" {
found = true
}
}
if !found {
t.Errorf("expected issue1 (%s) as 'added' in diff", issue1.ID)
}
})
// ===== Empty diff (same ref) =====
t.Run("empty_diff_same_ref", func(t *testing.T) {
out := bdDiff(t, bd, dir, hash1, hash1)
if !strings.Contains(out, "No changes") {
t.Errorf("expected 'No changes' for same ref, got: %s", out)
}
})
// ===== Invalid ref =====
t.Run("invalid_ref", func(t *testing.T) {
bdDiffFail(t, bd, dir, "not@valid!", hash1)
})
t.Run("invalid_ref_second_arg", func(t *testing.T) {
bdDiffFail(t, bd, dir, hash1, "not@valid!")
})
// ===== JSON output =====
t.Run("json_output_structure", func(t *testing.T) {
entries := bdDiffJSON(t, bd, dir, hash0, hash1)
if len(entries) == 0 {
t.Fatal("expected non-empty JSON array")
}
e := entries[0]
// Verify expected JSON keys
if _, ok := e["IssueID"]; !ok {
t.Error("expected 'IssueID' key in JSON entry")
}
if _, ok := e["DiffType"]; !ok {
t.Error("expected 'DiffType' key in JSON entry")
}
})
t.Run("json_modified_has_old_and_new", func(t *testing.T) {
// issue1 was modified between hash1 and current HEAD
currentHash := getCommitHash(t, beadsDir, "df")
entries := bdDiffJSON(t, bd, dir, hash1, currentHash)
for _, e := range entries {
if e["IssueID"] == issue1.ID && e["DiffType"] == "modified" {
if e["OldValue"] == nil {
t.Error("expected old_value for modified entry")
}
if e["NewValue"] == nil {
t.Error("expected new_value for modified entry")
}
return
}
}
// issue1 may have been modified — if not found, just log
t.Log("modified entry for issue1 not found in this range (may be expected)")
})
// ===== Human-readable format sections =====
t.Run("human_readable_sections", func(t *testing.T) {
out := bdDiff(t, bd, dir, hash0, hash1)
// Should contain the "Changes from ... to ..." header
if !strings.Contains(out, "Changes from") {
t.Errorf("expected 'Changes from' header: %s", out)
}
if !strings.Contains(out, "issues affected") {
t.Errorf("expected 'issues affected' in output: %s", out)
}
})
// ===== Wrong number of args =====
t.Run("too_few_args", func(t *testing.T) {
bdDiffFail(t, bd, dir, hash0)
})
t.Run("too_many_args", func(t *testing.T) {
bdDiffFail(t, bd, dir, hash0, hash1, hash2)
})
}
// TestEmbeddedDiffConcurrent exercises diff operations concurrently.
func TestEmbeddedDiffConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "dc")
hash0 := getCommitHash(t, beadsDir, "dc")
// Create several issues to build up commit history.
for i := 0; i < 5; i++ {
bdCreate(t, bd, dir, fmt.Sprintf("concurrent-diff-%d", i), "--type", "task")
}
hash1 := getCommitHash(t, beadsDir, "dc")
for i := 5; i < 10; i++ {
bdCreate(t, bd, dir, fmt.Sprintf("concurrent-diff-%d", i), "--type", "task")
}
hash2 := getCommitHash(t, beadsDir, "dc")
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
// Each worker diffs a different pair of refs.
refPairs := [][2]string{
{hash0, hash1},
{hash0, hash2},
{hash1, hash2},
{hash0, hash1},
{hash1, hash2},
{hash0, hash2},
{hash0, hash1},
{hash1, hash2},
}
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
pair := refPairs[worker]
// JSON diff
args := []string{"diff", "--json", pair[0], pair[1]}
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("worker %d diff %s..%s: %v\n%s", worker, pair[0][:8], pair[1][:8], err, out)
results[worker] = r
return
}
// Also run plain text diff
args = []string{"diff", pair[0], pair[1]}
cmd = exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err = cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("worker %d plain diff: %v\n%s", worker, err, out)
results[worker] = r
return
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}