beads/cmd/bd/ready_embedded_test.go
Julian Knutsen 0d865460b1
ci(bazel): split the slow embedded cmd tests and run the cmd block as 2x50 shards (#7438)
//cmd/bd:bd_embedded_test was the embedded lane's floor: at Bazel's 50-shard
cap per rule, each shard's wall time was its slowest top-level test, and
~40 tests ran 65-130s on rbe-west.

- 34 slow TestEmbedded* functions are split into 89 smaller top-level
  functions. Subtests and table cases are moved verbatim: no assertion,
  flag or data changes, the same t.Run set per file, the same skip guard and
  t.Parallel. Subtests that read state earlier ones left stay together, in
  order; each new function repeats the shared setup it needs.
- The manifest's cmd/bd block is now 100 shards, run by two targets:
  bd_embedded_test (shards 1-50) and the new bd_embedded_part2_test
  (51-100), through go_test_manifest_shard.sh --shard-offset/--shard-total.
  check_shard_coverage.py takes SHARDS@OFFSET/TOTAL and fails unless the
  targets' ranges tile the block exactly once, so every test runs in exactly
  one shard of one target; the policy tests read both rules' ranges from
  cmd/bd/BUILD.bazel.
- Durations re-measured on rbe-west with the split tests and repacked. The
  frozen legacy 20-shard block drops the renamed names (no workflow reads
  it; new names hash-distribute as before).

Not split: the *Concurrent stress tests (their bd subprocesses queue on the
workspace's single writer lock: TestEmbeddedUpdateConcurrent is ~100
serialized bd commands), TestEmbeddedPurgeWispsPlaneRetention (one stateful
sequence), and the MaxRowsList* tests (setup is 21 sequential creates every
subtest reads).

rbe-west, both targets, --nocache_test_results: slowest shard 138-163s ->
109s (TestEmbeddedUpdateConcurrent alone; every other shard <=86s), median
80-84s -> 40s, shard-seconds 4.1-4.4k -> 4.2k, critical path 213-306s ->
113s.

Bead: ga-vnycm2.17

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-09 20:30:52 +00:00

546 lines
19 KiB
Go

//go:build cgo
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/steveyegge/beads/internal/types"
)
func TestEmbeddedReadyDefaultJSONAndFlags(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", "rd")
bdCreate(t, bd, dir, "Ready test issue", "--type", "task")
// ===== Default =====
t.Run("ready_includes_open_issue_with_zero_dependencies", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "GH3268 zero dependency ready issue", "--type", "task", "--label", "gh3268-zero-deps")
cmd := exec.Command(bd, "ready", "--json", "--label", "gh3268-zero-deps")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
var ready []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), &ready); err != nil {
t.Fatalf("parse ready JSON: %v\n%s", err, stdout.String())
}
if len(ready) != 1 {
t.Fatalf("ready count = %d, want 1: %s", len(ready), stdout.String())
}
if ready[0].ID != issue.ID {
t.Fatalf("ready ID = %s, want %s", ready[0].ID, issue.ID)
}
if ready[0].DependencyCount != 0 {
t.Fatalf("dependency_count = %d, want 0", ready[0].DependencyCount)
}
})
t.Run("ready_default", func(t *testing.T) {
cmd := exec.Command(bd, "ready")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "Ready test issue") {
t.Errorf("expected issue in ready output: %s", stdout.String())
}
})
// ===== --json =====
t.Run("ready_json", func(t *testing.T) {
cmd := exec.Command(bd, "ready", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
s := strings.TrimSpace(stdout.String())
start := strings.IndexAny(s, "[{")
if start < 0 {
t.Fatalf("no JSON in ready --json output: %s", s)
}
if !json.Valid([]byte(s[start:])) {
t.Errorf("invalid JSON in ready output: %s", s[:min(200, len(s))])
}
})
t.Run("ready_json_truncation_hint", func(t *testing.T) {
for i := 0; i < 3; i++ {
bdCreate(t, bd, dir, fmt.Sprintf("Ready capped issue %d", i), "--type", "task")
}
cmd := exec.Command(bd, "ready", "--json", "--limit", "2")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
t.Fatalf("bd ready --json --limit 2 failed: %v\nstderr: %s\nstdout: %s", err, stderr.String(), out)
}
if !json.Valid(bytes.TrimSpace(out)) {
t.Fatalf("ready JSON stdout should remain parseable, got: %s", out)
}
if !strings.Contains(stderr.String(), "Use --limit 0 for all") {
t.Fatalf("expected truncation hint on stderr, got: %q", stderr.String())
}
// The "of N" is the whole ready set, now taken in the page's own
// transaction: it must be exactly what an unbounded listing returns,
// in the hint and in the envelope's pagination alike.
all := exec.Command(bd, "ready", "--json", "--limit", "0")
all.Dir = dir
all.Env = bdEnv(dir)
allOut, err := all.Output()
if err != nil {
t.Fatalf("bd ready --json --limit 0 failed: %v", err)
}
var unbounded []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(allOut), &unbounded); err != nil {
t.Fatalf("parse unbounded ready JSON: %v\n%s", err, allOut)
}
wantHint := fmt.Sprintf("Showing 2 of %d ready issues.", len(unbounded))
if !strings.Contains(stderr.String(), wantHint) {
t.Fatalf("hint = %q, want it to contain %q", stderr.String(), wantHint)
}
env := exec.Command(bd, "ready", "--json", "--limit", "2")
env.Dir = dir
env.Env = append(bdEnv(dir), "BD_JSON_ENVELOPE=1")
envOut, err := env.Output()
if err != nil {
t.Fatalf("bd ready --json --limit 2 (envelope) failed: %v", err)
}
var envelope struct {
Data []types.IssueWithCounts `json:"data"`
Pagination *PaginationMeta `json:"pagination"`
}
if err := json.Unmarshal(bytes.TrimSpace(envOut), &envelope); err != nil {
t.Fatalf("parse envelope: %v\n%s", err, envOut)
}
if envelope.Pagination == nil || !envelope.Pagination.Truncated ||
envelope.Pagination.Returned != 2 || envelope.Pagination.Total != len(unbounded) {
t.Fatalf("pagination = %+v, want returned=2 total=%d truncated", envelope.Pagination, len(unbounded))
}
})
t.Run("ready_claim_json", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Ready claim json", "--type", "task", "--label", "ready-claim-json")
cmd := exec.Command(bd, "ready", "--claim", "--json", "--label", "missing-label")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --claim --json with no matches failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
var empty []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), &empty); err != nil {
t.Fatalf("parse empty claim JSON: %v\n%s", err, stdout.String())
}
if len(empty) != 0 {
t.Fatalf("expected no claimed issues for unmatched label, got %d", len(empty))
}
cmd = exec.Command(bd, "ready", "--claim", "--json", "--label", "ready-claim-json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout.Reset()
stderr.Reset()
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("bd ready --claim --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
var claimed []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), &claimed); err != nil {
t.Fatalf("parse claim JSON: %v\n%s", err, stdout.String())
}
if len(claimed) != 1 {
t.Fatalf("expected one claimed issue, got %d: %s", len(claimed), stdout.String())
}
if claimed[0].ID != issue.ID {
t.Fatalf("claimed ID = %s, want %s", claimed[0].ID, issue.ID)
}
if claimed[0].Status != types.StatusInProgress {
t.Fatalf("claimed status = %s, want %s", claimed[0].Status, types.StatusInProgress)
}
if claimed[0].Assignee == "" {
t.Fatal("expected claimed issue to have assignee")
}
})
// ===== -C flag =====
t.Run("ready_with_C_flag", func(t *testing.T) {
// Run bd ready from a different directory using -C to point at the beads project
tmpDir := t.TempDir()
cmd := exec.Command(bd, "-C", dir, "ready")
cmd.Dir = tmpDir // Run from a directory with no .beads/
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd -C %s ready failed: %v\nstdout:\n%s\nstderr:\n%s", dir, err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "Ready test issue") {
t.Errorf("expected issue in ready -C output: %s", stdout.String())
}
})
t.Run("ready_with_C_flag_invalid_path", func(t *testing.T) {
tmpDir := t.TempDir()
cmd := exec.Command(bd, "-C", filepath.Join(tmpDir, "missing"), "ready")
cmd.Dir = tmpDir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd -C missing ready succeeded unexpectedly:\n%s", out)
}
if !strings.Contains(string(out), "cannot use -C directory") {
t.Errorf("expected invalid -C path error, got: %s", out)
}
})
t.Run("ready_with_C_flag_does_not_leak_cwd", func(t *testing.T) {
// Verify that -C does not permanently mutate the process cwd.
// Two sequential invocations from the same tmpDir: the first uses -C to
// reach the project; the second omits -C and must fail (no .beads/ in tmpDir),
// proving BEADS_DIR was not leaked into the test process environment.
tmpDir := t.TempDir()
env := bdEnv(dir) // strips all BEADS_* vars
cmd1 := exec.Command(bd, "-C", dir, "ready")
cmd1.Dir = tmpDir
cmd1.Env = env
if out, err := cmd1.CombinedOutput(); err != nil {
t.Fatalf("first bd -C ready failed: %v\n%s", err, out)
}
cmd2 := exec.Command(bd, "ready")
cmd2.Dir = tmpDir
cmd2.Env = env // same env — BEADS_DIR must not have leaked
out2, err2 := cmd2.CombinedOutput()
if err2 == nil {
t.Fatalf("second bd ready (no -C) should have failed in tmpDir, got: %s", out2)
}
})
t.Run("offset_rejected_outside_proxied", func(t *testing.T) {
cmd := exec.Command(bd, "ready", "--offset", "1")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd ready --offset 1 in embedded mode should have failed, got: %s", out)
}
if !strings.Contains(string(out), "--offset is only supported under --proxied-server") {
t.Errorf("expected '--offset is only supported under --proxied-server' error, got: %s", out)
}
})
// Only the proxied route pages, so only it validates --offset. Outside
// proxied mode a negative value has always been ignored, not rejected:
// the RunE rejects --offset > 0 as proxied-only and never reads the flag
// again. Pinned live because the shared flag gatherer is one edit away
// from turning it into an exit-1 usage error.
// The cap is resolved before the sort policy is validated, so a typo'd
// BEADS_MAX_ROWS still tells the user their cap is being ignored even
// though the command is about to fail for an unrelated reason. Pinned
// live: the warning is a side effect of resolving, so it disappears the
// moment the resolution moves later in the sequence.
t.Run("malformed_max_rows_env_warns_before_the_sort_error", func(t *testing.T) {
cmd := exec.Command(bd, "ready", "--sort", "bogus")
cmd.Dir = dir
cmd.Env = append(bdEnv(dir), "BEADS_MAX_ROWS=bogus")
stdout, stderr, err := runCommandBuffers(t, cmd)
if err == nil {
t.Fatalf("bd ready --sort bogus should have failed; stdout:\n%s\nstderr:\n%s", stdout.String(), stderr.String())
}
warning := strings.Index(stderr.String(), "BEADS_MAX_ROWS=\"bogus\" is not a non-negative integer")
sortErr := strings.Index(stderr.String(), "invalid sort policy")
switch {
case warning < 0:
t.Errorf("expected the BEADS_MAX_ROWS warning, got stderr:\n%s", stderr.String())
case sortErr < 0:
t.Errorf("expected the sort-policy error, got stderr:\n%s", stderr.String())
case warning > sortErr:
t.Errorf("warning must precede the sort error, got stderr:\n%s", stderr.String())
}
})
t.Run("negative_offset_ignored_outside_proxied", func(t *testing.T) {
cmd := exec.Command(bd, "ready", "--offset", "-1")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --offset -1 in embedded mode should have listed ready work: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "Ready test issue") {
t.Errorf("expected ready work in output, got: %s", stdout.String())
}
})
}
func TestEmbeddedReadyBlockersAndLabels(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", "rd")
bdCreate(t, bd, dir, "Ready test issue", "--type", "task")
// ===== With Blockers =====
t.Run("ready_excludes_blocked", func(t *testing.T) {
blocker := bdCreate(t, bd, dir, "Blocker issue", "--type", "task")
blocked := bdCreate(t, bd, dir, "Blocked by blocker", "--type", "task")
// Add blocking dependency: blocked depends on blocker
cmd := exec.Command(bd, "dep", "add", blocked.ID, blocker.ID)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("dep add failed: %v\n%s", err, out)
}
cmd = exec.Command(bd, "ready")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
// The blocked issue should not appear in ready output
if strings.Contains(stdout.String(), "Blocked by blocker") {
t.Errorf("blocked issue should not appear in ready output: %s", stdout.String())
}
})
// ===== Exclude Label =====
t.Run("ready_exclude_label", func(t *testing.T) {
bdCreate(t, bd, dir, "Triage pending item", "--type", "task", "--label", "triage:pending")
bdCreate(t, bd, dir, "Normal ready item", "--type", "task")
cmd := exec.Command(bd, "ready", "--exclude-label", "triage:pending")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --exclude-label failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if strings.Contains(stdout.String(), "Triage pending item") {
t.Errorf("triage:pending issue should not appear with --exclude-label: %s", stdout.String())
}
if !strings.Contains(stdout.String(), "Normal ready item") {
t.Errorf("normal issue should still appear with --exclude-label: %s", stdout.String())
}
})
readyIDs := func(t *testing.T, args ...string) ([]string, string) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd %s failed: %v\nstdout:\n%s\nstderr:\n%s",
strings.Join(args, " "), err, stdout.String(), stderr.String())
}
var issues []types.IssueWithCounts
if err := json.Unmarshal(bytes.TrimSpace(stdout.Bytes()), &issues); err != nil {
t.Fatalf("parse JSON: %v\n%s", err, stdout.String())
}
ids := make([]string, 0, len(issues))
for _, issue := range issues {
ids = append(ids, issue.ID)
}
return ids, stdout.String()
}
t.Run("ready_label_any", func(t *testing.T) {
laneA := bdCreate(t, bd, dir, "Lane A work", "--type", "task", "--label", "lany:lane-a")
bdCreate(t, bd, dir, "Lane B work", "--type", "task", "--label", "lany:lane-b")
laneC := bdCreate(t, bd, dir, "Lane C work", "--type", "task", "--label", "lany:lane-c")
ids, raw := readyIDs(t, "ready", "--json", "--label-any", "lany:lane-a,lany:lane-c")
want := map[string]bool{laneA.ID: true, laneC.ID: true}
if len(ids) != len(want) {
t.Fatalf("--label-any returned %d issues, want %d — an unfiltered ready set means the OR-set clause was dropped: %s",
len(ids), len(want), raw)
}
for _, id := range ids {
if !want[id] {
t.Fatalf("--label-any returned %s, which carries neither requested label: %s", id, raw)
}
}
})
t.Run("ready_label_any_intersects_with_label", func(t *testing.T) {
both := bdCreate(t, bd, dir, "Tiered lane A work", "--type", "task",
"--label", "lmix:tier", "--label", "lmix:lane-a")
bdCreate(t, bd, dir, "Untiered lane A work", "--type", "task", "--label", "lmix:lane-a")
bdCreate(t, bd, dir, "Tiered lane B work", "--type", "task",
"--label", "lmix:tier", "--label", "lmix:lane-b")
ids, raw := readyIDs(t, "ready", "--json",
"--label", "lmix:tier", "--label-any", "lmix:lane-a,lmix:lane-c")
if len(ids) != 1 || ids[0] != both.ID {
t.Fatalf("--label with --label-any returned %v, want only %s (AND-set and OR-set must both apply): %s",
ids, both.ID, raw)
}
})
t.Run("ready_claim_label_any_fences_to_its_lane", func(t *testing.T) {
// An exhausted lane must claim nothing. If --label-any is dropped here
// the claim silently takes unfenced work while the caller believes it
// is fenced to its own lane.
bdCreate(t, bd, dir, "Fence lane B work", "--type", "task", "--label", "lfence:lane-b")
ids, raw := readyIDs(t, "ready", "--claim", "--json", "--label-any", "lfence:lane-a")
if len(ids) != 0 {
t.Fatalf("--claim --label-any on an empty lane claimed %v, want nothing: %s", ids, raw)
}
})
}
func TestEmbeddedReadyConcurrent(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", "rx")
bdCreate(t, bd, dir, "Ready concurrent issue", "--type", "task")
const numWorkers = 8
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}
cmd := exec.Command(bd, "ready")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("ready (worker %d): %v\n%s", worker, 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)
}
}
}
func TestEmbeddedReadyClaimConcurrent(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", "rc")
issue := bdCreate(t, bd, dir, "Ready claim concurrent issue", "--type", "task")
const numWorkers = 8
type workerResult struct {
worker int
claimed []types.IssueWithCounts
err error
out string
}
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}
out, err := bdRunWithFlockRetry(t, bd, dir, "ready", "--claim", "--json")
r.out = string(out)
if err != nil {
r.err = fmt.Errorf("ready --claim (worker %d): %v\n%s", worker, err, out)
results[worker] = r
return
}
if err := json.Unmarshal(bytes.TrimSpace(out), &r.claimed); err != nil {
r.err = fmt.Errorf("parse ready --claim JSON (worker %d): %v\n%s", worker, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
claimCount := 0
for _, r := range results {
if r.err != nil {
t.Errorf("worker %d failed: %v", r.worker, r.err)
continue
}
if len(r.claimed) > 1 {
t.Errorf("worker %d claimed %d issues: %s", r.worker, len(r.claimed), r.out)
continue
}
if len(r.claimed) == 1 {
claimCount++
if r.claimed[0].ID != issue.ID {
t.Errorf("worker %d claimed %s, want %s", r.worker, r.claimed[0].ID, issue.ID)
}
}
}
if claimCount != 1 {
t.Fatalf("expected exactly one successful claim, got %d", claimCount)
}
got := bdShow(t, bd, dir, issue.ID)
if got.Status != types.StatusInProgress {
t.Fatalf("final status = %s, want %s", got.Status, types.StatusInProgress)
}
if got.Assignee == "" {
t.Fatal("expected final assignee to be set")
}
}