beads/cmd/bd/assign_fence_embedded_test.go
Julian Knutsen 8988340994
ci(embedded): run every BEADS_TEST_EMBEDDED_DOLT-gated cmd/bd test in the embedded lane (#7708)
* test(cmd/bd): repair the embedded-gated tests that rotted while no lane ran them

These tests skip unless BEADS_TEST_EMBEDDED_DOLT=1 but no CI lane selected
them, so they rotted unseen. Running them shows:

- TestEventsTailFollowReportsTruncationOnTheStream raced on a plain
  bytes.Buffer that os/exec's copy goroutine writes while the test polls it.
  Use the package's syncBuffer.
- TestCorruptMetadataDiagnosticsRunAndDataFailsLoud still asserted that
  doctor and init run on a corrupt metadata.json. #4857 deliberately made
  every workspace command refuse instead (pinned for doctor by
  TestDoctorRejectsCorruptMetadataBeforeSharedServerChecks). Rewrite it to
  the shipped contract as TestEmbeddedCorruptMetadataFailsLoudUntilRestored:
  version warns, doctor/list/init refuse naming the file without touching
  it, and restoring the file brings the data back.
- dolt_remote_origin_guard_embedded_test.go was tagged
  `embeddeddolt && cgo`, a tag no lane sets, so it never compiled in CI
  (gazelle left it out of bd_test srcs). Retag it `cgo`, as #4153 did for
  its dolt_local_only sibling, and add it to srcs. Drop its four bd doctor
  git-origin tests: bd doctor runs no checks on an embedded workspace
  (GH#3794), so two could never pass and two passed vacuously on empty
  output. Rename the surviving tests into the TestEmbedded* prefix.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* ci(embedded): run every BEADS_TEST_EMBEDDED_DOLT-gated cmd/bd test in the embedded lane

The embedded-Dolt cmd lane selected tests with
grep '^func TestEmbedded' cmd/bd/*_embedded_test.go, and only its two
Bazel targets set BEADS_TEST_EMBEDDED_DOLT. A test that skips without the
variable but missed the name prefix or the file suffix skipped in every
lane and reported green: 92 of them, including #7203's --if-revision CLI
coverage (TestIfRevisionMatchAndMismatch, TestReassignFenceCLI, ...).

- embedded-test-shard.sh and gen_embedded_cmd_shard_manifest.py select
  TestEmbedded* by name across cmd/bd/*_test.go, as proxied-test-shard.sh
  does, leaving authors one rule.
- Rename the gated tests into the TestEmbedded* prefix
  (TestDoctorEmbedded<X> -> TestEmbeddedDoctor<X>) and update every
  reference.
- Place the new names with gen_embedded_cmd_shard_manifest.py 100
  --weights=duration --write (incremental).
- Add TestEmbeddedGatedTestsRunInTheEmbeddedLane: it walks cmd/bd's tests
  for any that reach os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" and fails
  if a shard of the lane never lists one. The proxied guard's helpers now
  take the variable and the script; a fixture test pins the walk.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-19 22:02:22 +00:00

245 lines
11 KiB
Go

//go:build cgo
package main
import (
"errors"
"os"
"os/exec"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
// bdRunFailCode runs an arbitrary bd command expecting failure and returns
// combined output plus the process exit code.
func bdRunFailCode(t *testing.T, bd, dir string, args ...string) (string, int) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd %s to fail, but it succeeded:\n%s", strings.Join(args, " "), out)
}
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Fatalf("bd %s failed without an exit code: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out), ee.ExitCode()
}
// bdRunOK runs an arbitrary bd command expecting success.
func bdRunOK(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
// TestEmbeddedReassignFenceCLI drives the bd-98s5c live-claim reassign fence
// end-to-end: plain `bd update -a` and `bd assign` were the last unfenced
// cross-actor takeover path (--claim, unclaim, and close all refuse without
// --force). The fence must refuse ONLY the silent steamroll — a different
// actor overwriting a live in_progress claim with a third assignee — and must
// leave routine dispatch untouched: open-bead reassigns, self-edits,
// idempotent re-asserts, pool-alias takes, and holder-aware --if-assignee
// transfers all proceed without --force.
func TestEmbeddedReassignFenceCLI(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", "rf")
claimAs := func(t *testing.T, holder string) *types.Issue {
t.Helper()
issue := bdCreate(t, bd, dir, "Held work", "--type", "task")
bdUpdate(t, bd, dir, issue.ID, "--actor", holder, "--assignee", holder, "--status", "in_progress")
return issue
}
t.Run("update_refuses_live_foreign_claim", func(t *testing.T) {
issue := claimAs(t, "holder")
out, code := bdUpdateFailCode(t, bd, dir, issue.ID, "--actor", "thief", "--assignee", "thief")
if code != 1 {
t.Errorf("fence refusal exit code = %d, want 1 (policy refusal, never 13/guard-mismatch)\n%s", code, out)
}
for _, frag := range []string{"holder", "held by", "--force"} {
if !strings.Contains(out, frag) {
t.Errorf("refusal should contain %q, got:\n%s", frag, out)
}
}
// The old --claim refusal copy taught the steamroller ("use bd
// unclaim"); the fence copy must never reintroduce that shape.
if strings.Contains(out, "use bd unclaim") {
t.Errorf("refusal copy teaches the unclaim steamroller:\n%s", out)
}
got := bdShow(t, bd, dir, issue.ID)
if got.Assignee != "holder" || got.Status != types.StatusInProgress {
t.Errorf("refused reassign clobbered the row: assignee=%q status=%s, want holder/in_progress", got.Assignee, got.Status)
}
// --force is the sanctioned override for abandoned claims.
bdUpdate(t, bd, dir, issue.ID, "--actor", "thief", "--assignee", "thief", "--force")
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "thief" {
t.Errorf("--force reassign did not apply: assignee=%q, want thief", got.Assignee)
}
})
t.Run("assign_refuses_live_foreign_claim", func(t *testing.T) {
issue := claimAs(t, "holder")
out, code := bdRunFailCode(t, bd, dir, "assign", issue.ID, "thief", "--actor", "thief")
if code != 1 {
t.Errorf("assign fence exit code = %d, want 1\n%s", code, out)
}
if !strings.Contains(out, "holder") {
t.Errorf("assign refusal should name the holder, got:\n%s", out)
}
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "holder" {
t.Errorf("refused assign clobbered the row: assignee=%q, want holder", got.Assignee)
}
bdRunOK(t, bd, dir, "assign", issue.ID, "thief", "--actor", "thief", "--force")
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "thief" {
t.Errorf("assign --force did not apply: assignee=%q, want thief", got.Assignee)
}
})
t.Run("unassign_of_live_foreign_claim_refused", func(t *testing.T) {
issue := claimAs(t, "holder")
// Stripping a live claim via -a "" is unclaim in disguise; same fence.
_, code := bdUpdateFailCode(t, bd, dir, issue.ID, "--actor", "thief", "--assignee", "")
if code != 1 {
t.Errorf("unassign fence exit code = %d, want 1", code)
}
out, _ := bdRunFailCode(t, bd, dir, "assign", issue.ID, "", "--actor", "thief")
if !strings.Contains(out, "holder") {
t.Errorf("assign-to-empty refusal should name the holder, got:\n%s", out)
}
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "holder" {
t.Errorf("refused unassign clobbered the row: assignee=%q, want holder", got.Assignee)
}
})
// NEGATIVE regression fences for the load-bearing status==in_progress
// clause and the self/idempotent exemptions: routine dispatch must stay
// frictionless or every dispatcher grows a habitual --force.
t.Run("open_assigned_reassign_needs_no_force", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Open dispatch", "--type", "task", "--assignee", "alice")
bdUpdate(t, bd, dir, issue.ID, "--actor", "dispatcher", "--assignee", "bob")
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "bob" {
t.Errorf("open-bead reassign should not be fenced: assignee=%q, want bob", got.Assignee)
}
})
t.Run("holder_edits_own_claim_freely", func(t *testing.T) {
issue := claimAs(t, "holder")
bdUpdate(t, bd, dir, issue.ID, "--actor", "holder", "--assignee", "successor")
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "successor" {
t.Errorf("self-reassign should not be fenced: assignee=%q, want successor", got.Assignee)
}
})
t.Run("idempotent_reassert_of_holder_passes", func(t *testing.T) {
issue := claimAs(t, "holder")
bdUpdate(t, bd, dir, issue.ID, "--actor", "replayer", "--assignee", "holder")
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "holder" {
t.Errorf("idempotent re-assert should pass: assignee=%q, want holder", got.Assignee)
}
})
t.Run("if_assignee_transfer_needs_no_force", func(t *testing.T) {
// The holder-aware CAS (bd-wsqvw park transition) names the holder
// explicitly — nothing silent — so the fence must not fire under the
// guard, with or without --force.
issue := claimAs(t, "holder")
bdUpdate(t, bd, dir, issue.ID, "--actor", "supervisor", "--if-assignee", "holder", "--assignee", "parked")
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "parked" {
t.Errorf("guarded park should pass without --force: assignee=%q, want parked", got.Assignee)
}
})
t.Run("force_and_if_assignee_combine_via_the_cas", func(t *testing.T) {
// --force and --if-assignee are no longer mutually exclusive at the
// flag level: a caller pairing --notes with --if-assignee needs
// --force to opt into overwriting existing notes, and cobra can no
// longer refuse the combination outright. An assignee edit riding the
// same command still transfers ONLY through the --if-assignee CAS,
// never through --force: runCommandUpdateMutation never asserts
// ForceAssigneeTransfer when ExpectedAssignee is set, so this is the
// SAME guarded park as the case above, --force along for the ride.
issue := claimAs(t, "holder")
bdUpdate(t, bd, dir, issue.ID, "--actor", "supervisor", "--force", "--if-assignee", "holder", "--assignee", "parked")
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "parked" {
t.Errorf("guarded park should pass with --force along for the ride: assignee=%q, want parked", got.Assignee)
}
})
t.Run("force_with_if_assignee_arms_close_policy", func(t *testing.T) {
// The close-policy half of --force stays armed under --if-assignee:
// only the assignee-transfer half defers to the CAS. A guarded close
// of a parent with an open child was unreachable while cobra rejected
// the flag pair; now it must behave exactly like an unguarded one —
// refused unforced, authorized with --force.
parent := claimAs(t, "holder")
child := bdCreate(t, bd, dir, "Open child", "--type", "task")
bdRunOK(t, bd, dir, "dep", "add", child.ID, parent.ID, "--type", "parent-child")
out, code := bdUpdateFailCode(t, bd, dir, parent.ID, "--actor", "holder", "--if-assignee", "holder", "--status", "closed")
if code != 1 {
t.Errorf("unforced guarded close exit = %d, want 1 (policy refusal, never 13/guard-mismatch)\n%s", code, out)
}
if got := bdShow(t, bd, dir, parent.ID); got.Status != types.StatusInProgress {
t.Errorf("refused close changed the row: status=%s, want in_progress", got.Status)
}
bdUpdate(t, bd, dir, parent.ID, "--actor", "holder", "--force", "--if-assignee", "holder", "--status", "closed")
if got := bdShow(t, bd, dir, parent.ID); got.Status != types.StatusClosed {
t.Errorf("forced guarded close did not apply: status=%s, want closed", got.Status)
}
})
t.Run("claim_conflict_copy_preserved_under_claim_flag", func(t *testing.T) {
// --claim -a X on a foreign live claim must keep failing through the
// claim CAS with the canonical "already claimed" copy — the fence
// defers to it (the CAS is itself the anti-steal gate, and existing
// automation keys on that message).
issue := claimAs(t, "holder")
out, _ := bdUpdateFailCode(t, bd, dir, issue.ID, "--actor", "thief", "--claim", "--assignee", "thief")
if !strings.Contains(out, "already claimed") && !strings.Contains(out, "already assigned") {
t.Errorf("expected the claim CAS conflict copy, got:\n%s", out)
}
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "holder" {
t.Errorf("claim+assignee combo clobbered the row: assignee=%q, want holder", got.Assignee)
}
})
t.Run("pool_alias_holder_is_takeable", func(t *testing.T) {
// The claim.pools carve-out: --claim deliberately treats a
// pool-assigned issue as claimable by any actor; without the same
// carve-out, --claim and -a would give opposite answers on the same
// bead — and the refusal would say "coordinate with the holder" when
// the holder is a queue alias with nobody behind it.
bdRunOK(t, bd, dir, "config", "set", "claim.pools", "fable-crew")
issue := bdCreate(t, bd, dir, "Queue item", "--type", "task")
bdUpdate(t, bd, dir, issue.ID, "--actor", "dispatcher", "--assignee", "fable-crew", "--status", "in_progress")
bdUpdate(t, bd, dir, issue.ID, "--actor", "worker-1", "--assignee", "worker-1")
if got := bdShow(t, bd, dir, issue.ID); got.Assignee != "worker-1" {
t.Errorf("pool-alias take should pass without --force: assignee=%q, want worker-1", got.Assignee)
}
})
}