beads/cmd/bd/bootstrap_embedded_test.go
Kevin Glynn 0d4b979354
feat: JSON schema contract, bd ping, structured errors, import enhancements (#3368)
* feat: add schema_version to all --json output (beads-5uh)

All bd commands that emit --json output now include a schema_version
field so consumers (Jawnt MCP, BeadsX, gt mail, sync scripts) can
detect format changes without silently breaking.

- Object output: schema_version injected as top-level field
- Array output: wrapped as {"schema_version": 1, "items": [...]}
- Error output: schema_version added to stderr JSON

Central implementation in outputJSON() via wrapWithSchemaVersion()
means all 272 call sites get the field automatically.

Includes:
- Unit tests for wrapping logic (object, struct, slice, nil, roundtrip)
- Updated protocol contract tests for new envelope format
- New schema_version presence test across list/ready/show
- docs/JSON_SCHEMA.md documenting the field contract per command

* feat: add ready/blocked JSON contract tests (beads-clt)

bd ready --json and bd blocked --json already return full issue objects
with dependency counts and blocked_by fields. Add protocol contract
tests to pin this behavior:

- TestJSONContract_ReadyOutputHasFullObjects: verifies dependency_count,
  dependent_count fields on ready items
- TestJSONContract_BlockedOutputHasBlockedBy: verifies blocked_by and
  blocked_by_count fields on blocked items

Update docs/JSON_SCHEMA.md with blocked output contract.

* feat: add bd ping lightweight health-check command (beads-8cc)

New minimal command that confirms bd can reach its database:
1. Resolves the .beads workspace
2. Opens the store (embedded or server)
3. Runs a trivial query (SearchIssues with Limit=1)
4. Reports timing breakdown

Supports --json for structured output with resolve_ms, store_ms,
query_ms, and total_ms fields. Exit 0 on success, 1 on failure.

Registered as read-only command (no auto-push, no file writes).
Includes protocol contract test for JSON output.

* feat: structured JSON errors on stderr when --json active (beads-06n)

All fatal error functions (FatalError, FatalErrorWithHint,
FatalErrorRespectJSON, FatalErrorWithHintRespectJSON) now emit
structured JSON with schema_version when --json is active:

  {"schema_version": 1, "error": "message", "hint": "optional"}

Centralized via jsonStderrError() and jsonStdoutError() helpers.
Replaces ad-hoc json.MarshalIndent calls scattered across error
functions with a consistent, schema-versioned format.

Includes unit tests and updated protocol contract test verifying
schema_version presence in error JSON output.

* feat: enhance bd import with stdin, --dedup, --json (beads-c6i)

Enhance the existing bd import command for bulk JSONL ingest:

- Stdin support: bd import - reads JSONL from stdin for piped workflows
- --dedup flag: skips lines whose title matches an existing open issue
  (case-insensitive) to prevent duplicate creation
- --json flag: structured output with created/skipped counts and IDs

Refactored import to use runImportFromReader() for unified file/stdin
handling. Added filterDuplicatesByTitle() for dedup logic. Updated
docs/JSON_SCHEMA.md with import output contract.

Backward compatible: existing bd import [file] and --dry-run work
exactly as before.

* fix: update tests for schema_version envelope in JSON output

Three test files expected raw JSON arrays or map[string]string, which
broke when schema_version (a number) was added to the output envelope:

- ado_test.go: parse envelope with items array instead of raw []json.RawMessage
- bootstrap_embedded_test.go: use map[string]interface{} for mixed types
- where_embedded_test.go: same map[string]interface{} fix

* fix: drop array envelope wrapping to fix 200+ test failures

The schema_version envelope was wrapping arrays as
{"schema_version": N, "items": [...]} which broke every integration
test that parses --json array output. Objects (show, create, ping)
still get schema_version injected. Arrays (list, ready, blocked)
now pass through unchanged for backwards compatibility.

Also fixes: gofmt on import.go, doc-flags reference to bd import,
uses importIssuesCore result to satisfy unparam lint.

* fix: update remaining tests for schema_version number field

config_embedded_test.go, kv_embedded_test.go, config_nodb_embedded_test.go,
and show_test.go all parsed JSON output into map[string]string but
schema_version is a number. Parse to map[string]interface{} and
skip/assert schema_version appropriately.

---------

Co-authored-by: kev <kglynn@pryoninc.com>
2026-04-20 21:09:48 -07:00

245 lines
7.2 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
)
// bdBootstrap runs "bd bootstrap" with the given args and returns stdout.
func bdBootstrap(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"bootstrap"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd bootstrap %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
func bdBootstrapAllowError(t *testing.T, bd, dir string, args ...string) (string, error) {
t.Helper()
fullArgs := append([]string{"bootstrap"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
return string(out), err
}
func TestBootstrapNoWorkspace(t *testing.T) {
t.Parallel()
bd := buildEmbeddedBD(t)
dir := t.TempDir()
t.Run("default_output", func(t *testing.T) {
out, err := bdBootstrapAllowError(t, bd, dir)
if err == nil {
t.Fatal("expected bd bootstrap to exit non-zero without a workspace")
}
if !strings.Contains(out, activeWorkspaceNotFoundMessage()) {
t.Fatalf("expected no-workspace message, got: %s", out)
}
if !strings.Contains(out, "bd where") {
t.Fatalf("expected bootstrap hint to mention bd where, got: %s", out)
}
})
t.Run("json_output", func(t *testing.T) {
out, err := bdBootstrapAllowError(t, bd, dir, "--json")
if err == nil {
t.Fatal("expected bd bootstrap --json to exit non-zero without a workspace")
}
s := strings.TrimSpace(out)
start := strings.Index(s, "{")
if start < 0 {
t.Fatalf("expected JSON object in output, got: %s", out)
}
var payload map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &payload); err != nil {
t.Fatalf("parse bootstrap JSON: %v\n%s", err, s)
}
if action, _ := payload["action"].(string); action != "none" {
t.Fatalf("action = %q, want %q", action, "none")
}
if reason, _ := payload["reason"].(string); reason != activeWorkspaceNotFoundError() {
t.Fatalf("reason = %q, want %q", reason, activeWorkspaceNotFoundError())
}
if suggestion, _ := payload["suggestion"].(string); !strings.Contains(suggestion, "bd where") {
t.Fatalf("suggestion should mention bd where, got: %q", suggestion)
}
})
}
func TestEmbeddedBootstrap(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)
// ===== Already Exists =====
t.Run("bootstrap_existing_db", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "tb")
out := bdBootstrap(t, bd, dir, "--dry-run")
if !strings.Contains(out, "already exists") && !strings.Contains(out, "Nothing to do") {
t.Errorf("expected 'already exists' for initialized db: %s", out)
}
})
// ===== Dry Run (fresh .beads with no db) =====
t.Run("bootstrap_dry_run_fresh", func(t *testing.T) {
dir := t.TempDir()
cmd := exec.Command("git", "init", "-q")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = exec.Command("git", "config", "user.name", "Test")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = exec.Command("git", "config", "user.email", "test@test.com")
cmd.Dir = dir
cmd.CombinedOutput()
// Create .beads with metadata.json so FindBeadsDir detects it
beadsDir := filepath.Join(dir, ".beads")
os.MkdirAll(beadsDir, 0o750)
os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644)
out := bdBootstrap(t, bd, dir, "--dry-run")
if !strings.Contains(out, "create fresh") && !strings.Contains(out, "init") {
t.Errorf("expected fresh init plan: %s", out)
}
})
// ===== Full Bootstrap (init action) =====
t.Run("bootstrap_init", func(t *testing.T) {
dir := t.TempDir()
cmd := exec.Command("git", "init", "-q")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = exec.Command("git", "config", "user.name", "Test")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = exec.Command("git", "config", "user.email", "test@test.com")
cmd.Dir = dir
cmd.CombinedOutput()
beadsDir := filepath.Join(dir, ".beads")
os.MkdirAll(beadsDir, 0o750)
os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644)
bcmd := exec.Command(bd, "bootstrap")
bcmd.Dir = dir
bcmd.Env = bdEnv(dir)
bcmd.Stdin = strings.NewReader("y\n")
out, err := bcmd.CombinedOutput()
if err != nil {
t.Fatalf("bootstrap init failed: %v\n%s", err, out)
}
if !strings.Contains(string(out), "Created fresh database") {
t.Errorf("expected 'Created fresh database': %s", out)
}
})
// ===== JSONL Import =====
t.Run("bootstrap_jsonl_import", func(t *testing.T) {
// First create a db and export
srcDir, _, _ := bdInit(t, bd, "--prefix", "bs")
bdCreate(t, bd, srcDir, "Export for bootstrap", "--type", "task")
cmd := exec.Command(bd, "export", "-o", filepath.Join(srcDir, ".beads", "issues.jsonl"))
cmd.Dir = srcDir
cmd.Env = bdEnv(srcDir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("export failed: %v\n%s", err, out)
}
// Create new dir with .beads + issues.jsonl but no database
dir := t.TempDir()
gitCmd := exec.Command("git", "init", "-q")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
gitCmd = exec.Command("git", "config", "user.name", "Test")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
gitCmd = exec.Command("git", "config", "user.email", "test@test.com")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
destBeads := filepath.Join(dir, ".beads")
os.MkdirAll(destBeads, 0o750)
os.WriteFile(filepath.Join(destBeads, "metadata.json"), []byte("{}"), 0o644)
// Copy the JSONL file
data, _ := os.ReadFile(filepath.Join(srcDir, ".beads", "issues.jsonl"))
os.WriteFile(filepath.Join(dir, ".beads", "issues.jsonl"), data, 0o644)
// Bootstrap should detect and import JSONL
bcmd := exec.Command(bd, "bootstrap")
bcmd.Dir = dir
bcmd.Env = bdEnv(dir)
bcmd.Stdin = strings.NewReader("y\n")
out, err := bcmd.CombinedOutput()
if err != nil {
t.Fatalf("bootstrap jsonl-import failed: %v\n%s", err, out)
}
if !strings.Contains(string(out), "Imported") {
t.Errorf("expected 'Imported' in output: %s", out)
}
})
}
// TestEmbeddedBootstrapConcurrent exercises bootstrap --dry-run concurrently.
func TestEmbeddedBootstrapConcurrent(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", "bx")
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
cmd := exec.Command(bd, "bootstrap", "--dry-run")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("bootstrap --dry-run (worker %d): %v\n%s", worker, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}