beads/cmd/bd/context_binding_integration_test.go
Steve Yegge 48cbef2430
fix(test): prebuild bd for subprocess tests; fit cmd/bd inside the package deadline (wy-4mtr0) (#5067)
'./scripts/test.sh' with no flags could never produce a green full-suite
receipt: cmd/bd tripped the 3m per-package go test deadline (panic
'test timed out', one test running at 0s — the package alarm, not a
hang). Measured on the reporting machine class: ~494s of serial test
time across ~770 tests, dominated by (1) a dozen test helpers that each
'go build' the full cgo bd binary inside the test run and (2) subprocess
tests spawning bd + embedded Dolt per invocation.

Three-part fix, no naked timeout bump:

- scripts/test.sh now prebuilds bd once and exports BEADS_TEST_BD_BINARY
  — the same fast path CI has used all along (main.yml prebuilt
  artifact); the local runner just never got it. Binary lands in the
  test-env root so cleanup is inherited; caller-supplied values win;
  skipped when the requested packages can't include cmd/bd.
- Every in-test builder now honors BEADS_TEST_BD_BINARY (six sites
  built unconditionally before). The repo-root ./bd reuse in
  buildBDForInitTests and cli_fast_test is REMOVED: it silently ran
  subprocess tests against whatever stale binary sat in the checkout
  root — a two-day-old one produced phantom TestCreateDepsAtomicity
  failures during this investigation. Tests must exercise the
  checkout's source or an explicitly supplied binary, never an
  incidental artifact.
- Default TIMEOUT raised 3m -> 10m with the measurement recorded in the
  comment: it is cmd/bd's floor, not an arbitrary pick.


Claude-Session: https://claude.ai/code/session_01EF8QbCbF6JnTuoCKvu9xiG

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 15:10:21 -07:00

139 lines
4.3 KiB
Go

//go:build cgo
package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/types"
)
func filteredEnvForContextBinding(keys ...string) []string {
strip := make(map[string]struct{}, len(keys))
for _, key := range keys {
strip[key+"="] = struct{}{}
}
env := os.Environ()
filtered := make([]string, 0, len(env))
for _, entry := range env {
if strings.HasPrefix(entry, "BEADS_") || strings.HasPrefix(entry, "BD_") {
continue
}
trim := false
for prefix := range strip {
if strings.HasPrefix(entry, prefix) {
trim = true
break
}
}
if !trim {
filtered = append(filtered, entry)
}
}
return append(filtered, "BD_DISABLE_METRICS=1", "BD_DISABLE_EVENT_FLUSH=1")
}
func TestListExplicitDBPathRebindsTargetContext(t *testing.T) {
if testDoltServerPort == 0 {
t.Skip("Dolt test server not available, skipping")
}
tmpDir := t.TempDir()
callerRepo := filepath.Join(tmpDir, "caller")
callerBeadsDir := filepath.Join(callerRepo, ".beads")
writeTestConfigYAML(t, callerBeadsDir, "dolt.auto-commit: invalid\nactor: caller-actor\n")
if err := os.WriteFile(filepath.Join(callerBeadsDir, ".env"), []byte("BEADS_DOLT_SERVER_PORT=1\n"), 0o600); err != nil {
t.Fatalf("write caller .env: %v", err)
}
targetRepo := filepath.Join(tmpDir, "target")
targetBeadsDir := filepath.Join(targetRepo, ".beads")
writeTestConfigYAML(t, targetBeadsDir, "dolt.auto-commit: off\nactor: target-actor\n")
database := uniqueTestDBName(t)
if err := (&configfile.Config{
Backend: configfile.BackendDolt,
DoltMode: configfile.DoltModeServer,
DoltServerHost: "127.0.0.1",
DoltServerPort: testDoltServerPort,
DoltDatabase: database,
}).Save(targetBeadsDir); err != nil {
t.Fatalf("save target metadata: %v", err)
}
ctx := context.Background()
testStore, err := dolt.New(ctx, &dolt.Config{
Path: filepath.Join(targetBeadsDir, "dolt"),
BeadsDir: targetBeadsDir,
ServerHost: "127.0.0.1",
ServerPort: testDoltServerPort,
Database: database,
CreateIfMissing: true,
})
if err != nil {
t.Fatalf("create test store: %v", err)
}
defer func() {
_ = testStore.Close()
dropTestDatabase(database, testDoltServerPort)
}()
if err := testStore.SetConfig(ctx, "issue_prefix", "ctx"); err != nil {
t.Fatalf("set issue_prefix: %v", err)
}
now := time.Now()
nowIssue := &types.Issue{
ID: "ctx-1",
Title: "Context binding proof",
Description: "Proves explicit --db commands use the target workspace config",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
CreatedAt: now,
UpdatedAt: now,
}
if err := testStore.CreateIssue(ctx, nowIssue, "test-user"); err != nil {
t.Fatalf("create issue: %v", err)
}
// Shared once-per-process binary (honors BEADS_TEST_BD_BINARY) instead of
// a per-test go build — the in-test link steps dominated cmd/bd's wall
// clock (wy-4mtr0).
binPath := buildBDForInitTests(t)
listCmd := exec.Command(binPath, "list", "--db", filepath.Join(targetBeadsDir, "dolt"), "--json")
listCmd.Dir = callerRepo
listCmd.Env = append(filteredEnvForContextBinding("BEADS_DIR", "BEADS_DB", "BD_DB", "BEADS_DOLT_SERVER_PORT", "BEADS_DOLT_SERVER_DATABASE"),
"HOME="+t.TempDir(),
"XDG_CONFIG_HOME="+t.TempDir(),
"BEADS_TEST_MODE=1",
// AD-01 (be-c5p): the subprocess connects to a testdb_*-named DB on
// the test container; pass the firewall opt-in so dolt.New allows it.
"BEADS_TEST_SERVER=1",
"BEADS_DIR="+callerBeadsDir,
"BEADS_DB=",
)
output, err := listCmd.CombinedOutput()
if err != nil {
t.Fatalf("bd list failed: %v\n%s", err, output)
}
if !strings.Contains(string(output), "Context binding proof") {
t.Fatalf("expected list output to include target issue\n%s", output)
}
if _, err := os.Stat(filepath.Join(callerBeadsDir, localVersionFile)); err == nil {
t.Fatalf("caller workspace unexpectedly created %s", filepath.Join(callerBeadsDir, localVersionFile))
} else if !os.IsNotExist(err) {
t.Fatalf("stat caller %s: %v", localVersionFile, err)
}
if _, err := os.Stat(filepath.Join(targetBeadsDir, localVersionFile)); err != nil {
t.Fatalf("target workspace should create %s: %v", localVersionFile, err)
}
}