* 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>
279 lines
7 KiB
Go
279 lines
7 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// bdKV runs "bd kv" with the given args and returns stdout.
|
|
func bdKV(t *testing.T, bd, dir string, args ...string) string {
|
|
t.Helper()
|
|
fullArgs := append([]string{"kv"}, args...)
|
|
cmd := exec.Command(bd, fullArgs...)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("bd kv %s failed: %v\n%s", strings.Join(args, " "), err, out)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
// bdKVFail runs "bd kv" expecting failure.
|
|
func bdKVFail(t *testing.T, bd, dir string, args ...string) string {
|
|
t.Helper()
|
|
fullArgs := append([]string{"kv"}, args...)
|
|
cmd := exec.Command(bd, fullArgs...)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err == nil {
|
|
t.Fatalf("expected bd kv %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
// bdKVListJSON runs "bd kv list --json" and returns parsed map.
|
|
func bdKVListJSON(t *testing.T, bd, dir string) map[string]string {
|
|
t.Helper()
|
|
cmd := exec.Command(bd, "kv", "list", "--json")
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("bd kv list --json failed: %v\n%s", err, out)
|
|
}
|
|
s := strings.TrimSpace(string(out))
|
|
start := strings.Index(s, "{")
|
|
if start < 0 {
|
|
return map[string]string{}
|
|
}
|
|
var raw map[string]interface{}
|
|
if err := json.Unmarshal([]byte(s[start:]), &raw); err != nil {
|
|
t.Fatalf("parse kv list JSON: %v\n%s", err, s)
|
|
}
|
|
m := make(map[string]string, len(raw))
|
|
for k, v := range raw {
|
|
if k == "schema_version" {
|
|
continue
|
|
}
|
|
if sv, ok := v.(string); ok {
|
|
m[k] = sv
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestEmbeddedKV(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", "tk")
|
|
|
|
// ===== Set and Get =====
|
|
|
|
t.Run("kv_set_and_get", func(t *testing.T) {
|
|
bdKV(t, bd, dir, "set", "mykey", "myvalue")
|
|
out := bdKV(t, bd, dir, "get", "mykey")
|
|
if !strings.Contains(out, "myvalue") {
|
|
t.Errorf("expected 'myvalue' in get output: %s", out)
|
|
}
|
|
})
|
|
|
|
t.Run("kv_set_overwrite", func(t *testing.T) {
|
|
bdKV(t, bd, dir, "set", "overkey", "first")
|
|
bdKV(t, bd, dir, "set", "overkey", "second")
|
|
out := bdKV(t, bd, dir, "get", "overkey")
|
|
if !strings.Contains(out, "second") {
|
|
t.Errorf("expected 'second' after overwrite: %s", out)
|
|
}
|
|
})
|
|
|
|
t.Run("kv_set_special_chars", func(t *testing.T) {
|
|
bdKV(t, bd, dir, "set", "special", "hello world with spaces")
|
|
out := bdKV(t, bd, dir, "get", "special")
|
|
if !strings.Contains(out, "hello world with spaces") {
|
|
t.Errorf("expected value with spaces: %s", out)
|
|
}
|
|
})
|
|
|
|
// ===== List =====
|
|
|
|
t.Run("kv_list", func(t *testing.T) {
|
|
out := bdKV(t, bd, dir, "list")
|
|
if !strings.Contains(out, "mykey") {
|
|
t.Errorf("expected 'mykey' in list output: %s", out)
|
|
}
|
|
})
|
|
|
|
t.Run("kv_list_json", func(t *testing.T) {
|
|
m := bdKVListJSON(t, bd, dir)
|
|
if v, ok := m["mykey"]; !ok || v != "myvalue" {
|
|
t.Errorf("expected mykey=myvalue in JSON, got %q (exists=%v)", v, ok)
|
|
}
|
|
})
|
|
|
|
// ===== Clear =====
|
|
|
|
t.Run("kv_clear", func(t *testing.T) {
|
|
bdKV(t, bd, dir, "set", "clearme", "temporary")
|
|
out := bdKV(t, bd, dir, "get", "clearme")
|
|
if !strings.Contains(out, "temporary") {
|
|
t.Fatalf("expected 'temporary' before clear: %s", out)
|
|
}
|
|
bdKV(t, bd, dir, "clear", "clearme")
|
|
// Verify it's gone from list
|
|
m := bdKVListJSON(t, bd, dir)
|
|
if _, ok := m["clearme"]; ok {
|
|
t.Error("expected clearme to be absent from kv list after clear")
|
|
}
|
|
})
|
|
|
|
// ===== Error Cases =====
|
|
|
|
t.Run("kv_get_missing_key", func(t *testing.T) {
|
|
// get on nonexistent key — check behavior
|
|
cmd := exec.Command(bd, "kv", "get", "nonexistent_key_xyz")
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
// May succeed with empty or fail — either way no crash
|
|
_ = err
|
|
_ = out
|
|
})
|
|
|
|
t.Run("kv_set_no_args", func(t *testing.T) {
|
|
bdKVFail(t, bd, dir, "set")
|
|
})
|
|
|
|
t.Run("kv_get_no_args", func(t *testing.T) {
|
|
bdKVFail(t, bd, dir, "get")
|
|
})
|
|
|
|
t.Run("kv_clear_no_args", func(t *testing.T) {
|
|
bdKVFail(t, bd, dir, "clear")
|
|
})
|
|
}
|
|
|
|
// TestEmbeddedKVConcurrent exercises kv operations concurrently.
|
|
func TestEmbeddedKVConcurrent(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", "kx")
|
|
|
|
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}
|
|
|
|
// Each worker sets its own keys
|
|
for i := 0; i < 5; i++ {
|
|
key := fmt.Sprintf("w%d-k%d", worker, i)
|
|
value := fmt.Sprintf("v%d-%d", worker, i)
|
|
|
|
cmd := exec.Command(bd, "kv", "set", key, value)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
r.err = fmt.Errorf("set %s: %v\n%s", key, err, out)
|
|
results[worker] = r
|
|
return
|
|
}
|
|
}
|
|
|
|
// Read back
|
|
for i := 0; i < 5; i++ {
|
|
key := fmt.Sprintf("w%d-k%d", worker, i)
|
|
expected := fmt.Sprintf("v%d-%d", worker, i)
|
|
|
|
cmd := exec.Command(bd, "kv", "get", key)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
r.err = fmt.Errorf("get %s: %v\n%s", key, err, out)
|
|
results[worker] = r
|
|
return
|
|
}
|
|
if !strings.Contains(string(out), expected) {
|
|
r.err = fmt.Errorf("key %s expected %q, got %q", key, expected, string(out))
|
|
results[worker] = r
|
|
return
|
|
}
|
|
}
|
|
|
|
// Clear one key
|
|
clearKey := fmt.Sprintf("w%d-k0", worker)
|
|
cmd := exec.Command(bd, "kv", "clear", clearKey)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
r.err = fmt.Errorf("clear %s: %v\n%s", clearKey, err, out)
|
|
results[worker] = r
|
|
return
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
// Verify remaining keys only for workers that succeeded (err==nil).
|
|
// With exclusive flock, some workers may fail with "one writer at a time".
|
|
m := bdKVListJSON(t, bd, dir)
|
|
var successCount int
|
|
for _, r := range results {
|
|
if r.err != nil {
|
|
continue
|
|
}
|
|
successCount++
|
|
w := r.worker
|
|
clearedKey := fmt.Sprintf("w%d-k0", w)
|
|
if _, ok := m[clearedKey]; ok {
|
|
t.Errorf("expected %s to be cleared", clearedKey)
|
|
}
|
|
for i := 1; i < 5; i++ {
|
|
key := fmt.Sprintf("w%d-k%d", w, i)
|
|
expected := fmt.Sprintf("v%d-%d", w, i)
|
|
if v, ok := m[key]; !ok || v != expected {
|
|
t.Errorf("key %s expected %q, got %q (exists=%v)", key, expected, v, ok)
|
|
}
|
|
}
|
|
}
|
|
if successCount == 0 {
|
|
t.Fatal("expected at least 1 worker to succeed")
|
|
}
|
|
}
|