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

73 lines
2.6 KiB
Go

//go:build cgo && integration
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestCLI_Import_ForeignPrefix_E2E pins that `bd import` accepts records whose
// id prefix is not the local workspace's — no flag required.
//
// This is a protocol requirement, not an accident: §J6.6 round-trips a stream
// through an EMPTY store, which necessarily carries its own prefix, and §J7.1
// makes ids opaque strings a reader must not parse semantics out of. Every
// import path passes SkipPrefixValidation (import.go, import_shared.go, repo.go).
//
// This test used to assert the opposite — that import REJECTED a foreign prefix
// unless given --skip-prefix-validation, a flag that no longer exists. Being
// build-tagged out of CI (cgo && integration), it rotted unnoticed; it now pins
// the behavior bd actually has.
func TestCLI_Import_ForeignPrefix_E2E(t *testing.T) {
if testDoltServerPort == 0 {
t.Skip("skipping: Dolt test container not available")
}
// Step 0: Locate the bd binary — shared once-per-process build (honors
// BEADS_TEST_BD_BINARY) instead of a per-test go build (wy-4mtr0).
tmpDir := t.TempDir()
bdBinary := buildBDForInitTests(t)
// Step 1: Setup a database with a specific prefix
projDir := filepath.Join(tmpDir, "proj")
if err := os.MkdirAll(projDir, 0755); err != nil {
t.Fatalf("Failed to create proj dir: %v", err)
}
runCmd := func(args ...string) (string, error) {
cmd := exec.Command(bdBinary, args...)
cmd.Dir = projDir
out, err := cmd.CombinedOutput()
return string(out), err
}
if out, err := runCmd("init", "--prefix", "current", "--quiet"); err != nil {
t.Fatalf("bd init failed: %v\nOutput: %s", err, out)
}
// Step 2: A JSONL record minted by a DIFFERENT tracker (foreign prefix)
legacyIssue := `{"id":"legacy-123","title":"Legacy issue","status":"open","priority":2,"issue_type":"task","created_at":"2026-01-01T00:00:00Z"}`
jsonlPath := filepath.Join(projDir, "legacy.jsonl")
if err := os.WriteFile(jsonlPath, []byte(legacyIssue+"\n"), 0644); err != nil {
t.Fatalf("Failed to write legacy JSONL: %v", err)
}
// Step 3: Import must succeed with no special flag
out, err := runCmd("import", "-i", "legacy.jsonl")
if err != nil {
t.Fatalf("Import of a foreign-prefix record failed: %v\nOutput: %s", err, out)
}
// Step 4: Verify the issue landed under its original id
out, err = runCmd("list", "--id", "legacy-123", "--json")
if err != nil {
t.Errorf("bd list failed: %v\nOutput: %s", err, out)
}
if !strings.Contains(out, "legacy-123") {
t.Errorf("Expected legacy-123 to be imported, but list output was: %s", out)
}
}