beads/cmd/bd/unclaim_conditional_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

145 lines
6.3 KiB
Go

//go:build cgo
package main
import (
"os"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestEmbeddedUnclaimIfAssigneeCLI drives `bd unclaim --if-assignee` end-to-end against
// the embedded Dolt backend: the conditional release must be a compare-and-swap,
// not a read-then-clobber. A stale expectation exits nonzero, names the current
// holder, and leaves the claim untouched; the matching expectation releases the
// claim; and releasing an already-released issue is a distinct failure, not a
// silent success (the exactly-once property a release-if-current supervisor
// depends on).
func TestEmbeddedUnclaimIfAssigneeCLI(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", "ur")
issue := bdCreate(t, bd, dir, "Conditional release", "--type", "task")
bdUpdate(t, bd, dir, issue.ID, "--assignee", "alice", "--status", "in_progress")
// Stale expectation: distinct failure that names the current holder, claim intact.
out := bdUnclaimFail(t, bd, dir, issue.ID, "--if-assignee", "bob")
if !strings.Contains(out, "alice") {
t.Errorf("mismatch error should name the current holder alice, got:\n%s", out)
}
got := bdShow(t, bd, dir, issue.ID)
if got.Assignee != "alice" {
t.Errorf("stale --if-assignee clobbered the claim: assignee = %q, want alice", got.Assignee)
}
if got.Status != types.StatusInProgress {
t.Errorf("stale --if-assignee changed status to %q, want in_progress", got.Status)
}
// Matching expectation: releases the claim.
bdUnclaim(t, bd, dir, issue.ID, "--if-assignee", "alice")
got = bdShow(t, bd, dir, issue.ID)
if got.Assignee != "" {
t.Errorf("after matching --if-assignee: assignee = %q, want empty", got.Assignee)
}
if got.Status != types.StatusOpen {
t.Errorf("after matching --if-assignee: status = %q, want open", got.Status)
}
// Releasing an already-released issue with --if-assignee is also a distinct
// failure (exactly-once), not a silent success.
_ = bdUnclaimFail(t, bd, dir, issue.ID, "--if-assignee", "alice")
}
// TestEmbeddedUnclaimIfAssigneeCLIDifferentSpelling drives `bd unclaim --if-assignee`
// with an expectation that names the current holder under a different Gas
// Town identity spelling than the stored assignee (dot vs underscore
// separator — ga-5ksp5). This must count as a match, not a stale-expectation
// refusal: both the Go-side actorMatches precheck and the row_lock CAS that
// replaced the old verbatim `assignee = ?` SQL predicate need to agree it is
// the same identity, or the release silently affects zero rows even though
// the precheck said the CAS should succeed.
func TestEmbeddedUnclaimIfAssigneeCLIDifferentSpelling(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", "uw")
issue := bdCreate(t, bd, dir, "Conditional release, spelling variant", "--type", "task")
bdUpdate(t, bd, dir, issue.ID, "--assignee", "gastown.mayor", "--status", "in_progress")
bdUnclaim(t, bd, dir, issue.ID, "--if-assignee", "gastown_mayor")
got := bdShow(t, bd, dir, issue.ID)
if got.Assignee != "" {
t.Errorf("after same-identity --if-assignee: assignee = %q, want empty", got.Assignee)
}
if got.Status != types.StatusOpen {
t.Errorf("after same-identity --if-assignee: status = %q, want open", got.Status)
}
}
// TestEmbeddedUnclaimIfAssigneeFlagValidation drives the CLI guards that keep the
// conditional-release contract unambiguous. A conditional unclaim is selected by
// the *presence* of --if-assignee, so an explicitly empty --if-assignee "" (an
// unset variable that expanded into the flag) must be rejected rather than
// silently downgraded to an unconditional, --force-capable release; and --force
// cannot be combined with --if-assignee. Both cases must exit nonzero and leave
// the claim untouched. Each subtest runs as the holder so the pre-fix behavior
// (fall through to UnclaimIssue / let one flag win) would actually have released
// the claim, making these true regression guards.
func TestEmbeddedUnclaimIfAssigneeFlagValidation(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", "uv")
t.Run("empty_if_assignee_rejected", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Empty expectation", "--type", "task")
bdUpdate(t, bd, dir, issue.ID, "--assignee", "alice", "--status", "in_progress")
// As the holder (alice), a pre-fix empty --if-assignee "" would have
// fallen through to an unconditional owner release and cleared the claim.
out := bdUnclaimFail(t, bd, dir, issue.ID, "--if-assignee", "", "--actor", "alice")
if !strings.Contains(out, "if-assignee requires a non-empty assignee") {
t.Errorf("empty --if-assignee should be rejected with a clear error, got:\n%s", out)
}
got := bdShow(t, bd, dir, issue.ID)
if got.Assignee != "alice" {
t.Errorf("empty --if-assignee released the claim: assignee = %q, want alice", got.Assignee)
}
if got.Status != types.StatusInProgress {
t.Errorf("empty --if-assignee changed status to %q, want in_progress", got.Status)
}
})
t.Run("force_and_if_assignee_mutually_exclusive", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Conflicting flags", "--type", "task")
bdUpdate(t, bd, dir, issue.ID, "--assignee", "alice", "--status", "in_progress")
// --force + a matching --if-assignee would, pre-fix, take the CAS path and
// release the claim. The combination must instead be rejected outright.
out := bdUnclaimFail(t, bd, dir, issue.ID, "--force", "--if-assignee", "alice")
if !strings.Contains(out, "force") || !strings.Contains(out, "if-assignee") {
t.Errorf("--force with --if-assignee should be rejected as mutually exclusive, got:\n%s", out)
}
got := bdShow(t, bd, dir, issue.ID)
if got.Assignee != "alice" {
t.Errorf("--force --if-assignee released the claim: assignee = %q, want alice", got.Assignee)
}
if got.Status != types.StatusInProgress {
t.Errorf("--force --if-assignee changed status to %q, want in_progress", got.Status)
}
})
}