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

129 lines
5.2 KiB
Go

//go:build cgo && unix
package main
import (
"bytes"
"os"
"os/exec"
"strings"
"testing"
)
// The two halves of #5985 at the front door: what `bd delete --actor` records,
// and what `bd events export` tells you when there is nothing to record into.
// Both are end-to-end because both failures are invisible from inside the
// process — the command succeeds either way, and only a later read shows the
// attribution missing or the reader polling a journal that was never on.
// runBDOut runs bd in dir and returns stdout and stderr separately. The notice
// under test is on stderr precisely so it does not corrupt the JSONL on stdout,
// so a CombinedOutput helper cannot check it.
func runBDOut(t *testing.T, bd, dir string, env []string, args ...string) (string, string) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = env
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("bd %s in %s: %v\nstdout:\n%s\nstderr:\n%s",
strings.Join(args, " "), dir, err, stdout.String(), stderr.String())
}
return stdout.String(), stderr.String()
}
// TestEmbeddedDeleteJournalsTheRequestingActor is #5985's repro, verbatim: create two
// beads with an edge between them, delete the blocker with an explicit --actor,
// and read the journal back. Both the delete record and the cascade dep_remove
// record must name that actor.
//
// The whole route is exercised — cobra flag to journal column — because the
// actor is resolved, threaded through the role, and written by the storage
// seam, and a unit test at any one of those cannot see the drop at the next.
func TestEmbeddedDeleteJournalsTheRequestingActor(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", "dja", "--skip-hooks", "--skip-agents")
env := append(envWithout(bdEnv(dir), "BD_EVENTS_JOURNAL"), "BD_EVENTS_JOURNAL=1")
run := func(args ...string) string {
t.Helper()
stdout, _ := runBDOut(t, bd, dir, env, args...)
return stdout
}
run("create", "--silent", "--id", "dja-blocker", "the blocker")
run("create", "--silent", "--id", "dja-dependent", "the dependent")
run("dep", "add", "dja-dependent", "dja-blocker")
// --force is what makes this a real delete rather than a preview, and it is
// also what lets it orphan the dependent instead of refusing.
run("delete", "dja-blocker", "--force", "--actor", "alice")
records := decodeEventRecords(t, run("events", "export"))
var sawDelete, sawCascadeDepRemove bool
for _, rec := range records {
switch {
case rec.Op == "delete" && rec.IssueID == "dja-blocker":
sawDelete = true
if rec.Actor != "alice" {
t.Errorf("delete record actor = %q, want %q (#5985)", rec.Actor, "alice")
}
// The cascade edge removal is journaled under the edge's SOURCE — the
// surviving dependent — and belongs to the identity that asked for the
// delete, not to whoever created the edge.
case rec.Op == "dep_remove" && rec.IssueID == "dja-dependent":
sawCascadeDepRemove = true
if rec.Actor != "alice" {
t.Errorf("cascade dep_remove record actor = %q, want %q (#5985)", rec.Actor, "alice")
}
}
}
if !sawDelete {
t.Errorf("no delete record for dja-blocker: %+v", records)
}
if !sawCascadeDepRemove {
t.Errorf("no cascade dep_remove record for dja-dependent: %+v", records)
}
}
// TestEmbeddedEventsReadNoticesADisabledJournal pins the "nothing to read and no way to
// tell why" fix. A workspace that never enabled the journal serves an empty
// export forever, which a consumer cannot distinguish from being caught up, so
// the read says so on stderr — and keeps stdout clean and the exit status zero,
// because exporting a historical ledger from a now-disabled workspace is
// legitimate.
func TestEmbeddedEventsReadNoticesADisabledJournal(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", "dnn", "--skip-hooks", "--skip-agents")
off := envWithout(bdEnv(dir), "BD_EVENTS_JOURNAL")
for _, sub := range []string{"export", "tail"} {
stdout, stderr := runBDOut(t, bd, dir, off, "events", sub)
if !strings.Contains(stderr, "events journal is disabled") {
t.Errorf("bd events %s on a disabled workspace printed no notice\nstderr:\n%s", sub, stderr)
}
if !strings.Contains(stderr, "events-journal") {
t.Errorf("bd events %s notice does not name the setting to flip\nstderr:\n%s", sub, stderr)
}
if strings.TrimSpace(stdout) != "" {
t.Errorf("bd events %s put the notice on stdout, which carries the JSONL stream:\n%s", sub, stdout)
}
}
// Enabled: no notice, so the message stays a signal rather than noise every
// consumer learns to filter out.
on := append(envWithout(bdEnv(dir), "BD_EVENTS_JOURNAL"), "BD_EVENTS_JOURNAL=1")
if _, stderr := runBDOut(t, bd, dir, on, "events", "export"); strings.Contains(stderr, "events journal is disabled") {
t.Errorf("an enabled workspace still printed the disabled notice:\n%s", stderr)
}
}