beads/cmd/bd/state_embedded_test.go
matt wilkie f443df1f2a
fix: resolve CI failures from flock, version bump, and lint (#2981)
* fix: resolve CI failures from flock, version bump, and lint

Three categories of CI failure on main after the v1.0.0 release:

1. rules.go lint/format: Fix gofmt alignment, errcheck on tw.Flush(),
   gosec G304 (add nosec comment), gosec G306 (use 0o600 perms).

2. doctor_test.go version tracking: The 'slightly outdated version'
   test used 0.55.0 which now has a major version diff from 1.0.0,
   correctly triggering a warning. Update expected status to match.

3. Embedded Dolt concurrent tests: The exclusive flock from #2571
   causes concurrent bd processes to fail with 'one writer at a time'.
   Update 58 concurrent test files to tolerate this expected error
   while still failing on unexpected errors and panics.

Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>

* fix: concurrent test post-conditions tolerate flock contention

With exclusive flock (commit 543e1943), only some workers can acquire
the lock in concurrent tests. Update post-conditions in 5 tests to:
- Only verify side effects from workers that succeeded (r.err == nil)
- Require at least 1 worker succeeds (fail if all hit flock)
- Log success/failure counts for visibility
- Still verify correct behavior for successful operations

Tests fixed:
- TestEmbeddedCloseConcurrent: remove Fatalf on failures, skip total count check
- TestEmbeddedDeferConcurrent: only check deferred status for successful workers
- TestEmbeddedUndeferConcurrent: only check open status for successful workers
- TestEmbeddedReopenConcurrent: only check open status for successful workers
- TestEmbeddedPromoteCLIConcurrent: only check permanent for successful workers

Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>

---------

Co-authored-by: Amp <amp@ampcode.com>
2026-04-03 07:27:00 -07:00

197 lines
5 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdState runs "bd state" with the given args and returns stdout.
func bdState(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"state"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd state %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
// bdSetState runs "bd set-state" with the given args and returns stdout.
func bdSetState(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"set-state"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd set-state %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
func TestEmbeddedState(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, _, _ := bdInit(t, bd, "--prefix", "st")
issue := bdCreate(t, bd, dir, "State test issue", "--type", "task")
// ===== set-state =====
t.Run("set_state_basic", func(t *testing.T) {
out := bdSetState(t, bd, dir, issue.ID, "phase=planning")
if !strings.Contains(out, "planning") {
t.Logf("set-state output: %s", out)
}
})
t.Run("set_state_json", func(t *testing.T) {
cmd := exec.Command(bd, "set-state", issue.ID, "env=staging", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd set-state --json failed: %v\n%s", err, out)
}
s := strings.TrimSpace(string(out))
start := strings.Index(s, "{")
if start >= 0 {
var m map[string]interface{}
if jsonErr := json.Unmarshal([]byte(s[start:]), &m); jsonErr != nil {
t.Errorf("invalid JSON: %v\n%s", jsonErr, s)
}
}
})
t.Run("set_state_with_reason", func(t *testing.T) {
out := bdSetState(t, bd, dir, issue.ID, "risk=high", "--reason", "New vulnerability found")
_ = out
})
t.Run("set_state_overwrites", func(t *testing.T) {
bdSetState(t, bd, dir, issue.ID, "phase=development")
bdSetState(t, bd, dir, issue.ID, "phase=testing")
out := bdState(t, bd, dir, issue.ID, "phase")
if !strings.Contains(out, "testing") {
t.Errorf("expected 'testing' after overwrite, got: %s", out)
}
})
// ===== state query =====
t.Run("state_query", func(t *testing.T) {
out := bdState(t, bd, dir, issue.ID, "phase")
if !strings.Contains(out, "testing") {
t.Logf("state query output: %s", out)
}
})
t.Run("state_query_json", func(t *testing.T) {
cmd := exec.Command(bd, "state", issue.ID, "phase", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd state --json failed: %v\n%s", err, out)
}
_ = out
})
t.Run("state_query_nonexistent_dimension", func(t *testing.T) {
out := bdState(t, bd, dir, issue.ID, "nonexistent")
// Should return empty/not-set, not error
_ = out
})
// ===== state list =====
t.Run("state_list", func(t *testing.T) {
out := bdState(t, bd, dir, "list", issue.ID)
// Should show the dimensions we set
if !strings.Contains(out, "phase") {
t.Logf("state list output: %s", out)
}
})
t.Run("state_list_json", func(t *testing.T) {
cmd := exec.Command(bd, "state", "list", issue.ID, "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd state list --json failed: %v\n%s", err, out)
}
_ = out
})
t.Run("state_list_no_dimensions", func(t *testing.T) {
fresh := bdCreate(t, bd, dir, "No state", "--type", "task")
out := bdState(t, bd, dir, "list", fresh.ID)
_ = out
})
}
// TestEmbeddedStateConcurrent exercises set-state concurrently on different dimensions.
func TestEmbeddedStateConcurrent(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, _, _ := bdInit(t, bd, "--prefix", "sx")
issue := bdCreate(t, bd, dir, "Concurrent state", "--type", "task")
const numWorkers = 6
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
dim := fmt.Sprintf("dim%d=val%d", worker, worker)
cmd := exec.Command(bd, "set-state", issue.ID, dim)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("set-state %s: %v\n%s", dim, err, out)
}
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)
}
}
}