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

180 lines
6.1 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"os"
"os/exec"
"strings"
"testing"
)
// doctorTestEnv extends bdEnv by forcing legacy JSON output (BD_JSON_ENVELOPE=0)
// so the test assertions can read top-level keys regardless of whether the
// developer's shell exports envelope mode.
func doctorTestEnv(dir string) []string {
return append(bdEnv(dir), "BD_JSON_ENVELOPE=0")
}
// runBDCombined runs `bd <args...>` in the given dir with the test embedded
// env and returns combined stdout+stderr.
func runBDCombined(t *testing.T, bd, dir string, args ...string) (string, error) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = doctorTestEnv(dir)
out, err := cmd.CombinedOutput()
return string(out), err
}
// runBDSplit runs `bd <args...>` capturing stdout and stderr separately so
// each stream can be asserted independently. The bd JSON contract puts errors
// (including embedded-unsupported stubs) on stderr per docs/JSON_SCHEMA.md.
func runBDSplit(t *testing.T, bd, dir string, args ...string) (stdout, stderr string, err error) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = doctorTestEnv(dir)
var stdoutBuf, stderrBuf strings.Builder
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
err = cmd.Run()
return stdoutBuf.String(), stderrBuf.String(), err
}
// TestEmbeddedDoctorSupportedChecks verifies that --check=artifacts,
// --check=conventions, and --check=pollution run in embedded mode rather
// than hitting the "not yet supported" stub (GH#3597).
func TestEmbeddedDoctorSupportedChecks(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", "de")
supported := []struct {
name string
args []string
}{
{"artifacts", []string{"doctor", "--check=artifacts"}},
{"artifacts_json", []string{"doctor", "--check=artifacts", "--json"}},
{"conventions", []string{"doctor", "--check=conventions"}},
{"conventions_json", []string{"doctor", "--check=conventions", "--json"}},
{"pollution", []string{"doctor", "--check=pollution"}},
{"pollution_json", []string{"doctor", "--check=pollution", "--json"}},
}
for _, tc := range supported {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
out, err := runBDCombined(t, bd, dir, tc.args...)
if err != nil {
t.Fatalf("bd %s failed: %v\n%s", strings.Join(tc.args, " "), err, out)
}
if strings.Contains(out, "not yet supported in embedded mode") {
t.Errorf("bd %s hit the embedded-mode stub unexpectedly:\n%s",
strings.Join(tc.args, " "), out)
}
})
}
}
// TestEmbeddedDoctorUnsupportedJSON verifies that variants which still require
// server mode (bare doctor, --check=validate) emit a structured JSON payload
// when --json or --agent --json is set, rather than the prose stub. Tooling
// can detect "unsupported" without text-matching (GH#3597).
func TestEmbeddedDoctorUnsupportedJSON(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", "du")
cases := []struct {
name string
args []string
wantCommand string
}{
{"bare_doctor_json", []string{"doctor", "--json"}, "doctor"},
{"agent_json", []string{"doctor", "--agent", "--json"}, "doctor"},
{"validate_json", []string{"doctor", "--check=validate", "--json"}, "doctor --check=validate"},
{"perf_json", []string{"doctor", "--perf", "--json"}, "doctor --perf"},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
stdout, stderr, err := runBDSplit(t, bd, dir, tc.args...)
if err != nil {
t.Fatalf("bd %s failed: %v\nstdout:\n%s\nstderr:\n%s",
strings.Join(tc.args, " "), err, stdout, stderr)
}
// JSON error payloads are written to stderr per the bd JSON
// contract (docs/JSON_SCHEMA.md). Stdout should be empty so
// stdout-only consumers don't accidentally swallow the stub.
if strings.TrimSpace(stdout) != "" {
t.Errorf("expected empty stdout for embedded-unsupported stub, got:\n%s", stdout)
}
var result map[string]interface{}
if err := json.Unmarshal([]byte(stderr), &result); err != nil {
t.Fatalf("bd %s did not emit valid JSON on stderr: %v\nstderr:\n%s",
strings.Join(tc.args, " "), err, stderr)
}
if result["unsupported"] != true {
t.Errorf("expected unsupported=true, got %v", result["unsupported"])
}
if result["mode"] != "embedded" {
t.Errorf("expected mode=embedded, got %v", result["mode"])
}
if result["command"] != tc.wantCommand {
t.Errorf("expected command=%q, got %v", tc.wantCommand, result["command"])
}
if result["code"] != "embedded_unsupported" {
t.Errorf("expected code=embedded_unsupported, got %v", result["code"])
}
errStr, _ := result["error"].(string)
if !strings.Contains(errStr, "not yet supported in embedded mode") {
t.Errorf("expected error to mention embedded-mode unsupported, got %q", errStr)
}
})
}
}
// TestEmbeddedDoctorUnsupportedProse verifies that bare bd doctor (without
// --json) still emits the human-readable prose stub for terminal users,
// pointing at the supported --check=* variants (GH#3597).
func TestEmbeddedDoctorUnsupportedProse(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", "dp")
out, err := runBDCombined(t, bd, dir, "doctor")
if err != nil {
t.Fatalf("bd doctor failed: %v\n%s", err, out)
}
if !strings.Contains(out, "not yet supported in embedded mode") {
t.Errorf("expected prose stub in plain bd doctor output:\n%s", out)
}
// The stub should advertise the embedded-mode-supported checks.
for _, want := range []string{"--check=artifacts", "--check=conventions", "--check=pollution"} {
if !strings.Contains(out, want) {
t.Errorf("expected stub to mention %q so users know what works:\n%s", want, out)
}
}
}