* feat(ready): add pagination metadata to BD_JSON_ENVELOPE=1 output when truncated
bd ready --json with BD_JSON_ENVELOPE=1 now includes a "pagination" key
in the envelope when results are capped by --limit:
{
"schema_version": 1,
"data": [...],
"pagination": {"returned": 100, "total": 149, "truncated": true}
}
Without the envelope (default bare-array mode), the existing stderr text
hint ("Showing 100 of 149 ready issues. Use --limit 0 for all...") is
unchanged. The pagination key is absent when results are not truncated,
so callers can check for its presence / the truncated field rather than
comparing returned vs total.
Fixes the silent truncation problem for programmatic consumers (gc,
agents, sweep scripts) that call bd ready --json and consume the JSON
payload from stdout — they cannot read the stderr prose hint, so they
received no signal that the list was capped.
Closes #4892
* fix(ready): emit pagination metadata on the proxied route too
runReadyProxiedServer's JSON branch called the bare outputJSON(results)
even though page.HasMore already carried enough information to detect
truncation, so BD_JSON_ENVELOPE=1 consumers on the proxied path (the
default for non-CGO/proxied builds) had no way to see the pagination
signal that the direct route already exposes. Build a PaginationMeta
with Returned/Truncated from page.HasMore && in.filter.Limit > 0 and
route through outputJSONWithPagination instead; Total is left unset
since this backend's domain page has no cheap total-count equivalent.
Adds proxied-route coverage (pagination_envelope_truncated and
pagination_envelope_not_truncated subtests) exercising the envelope
under BD_JSON_ENVELOPE=1 for both truncated and non-truncated results.
Closes the blocking item from the 2026-07-23 CHANGES_REQUESTED review
on gastownhall/beads#4897.
Agent-Signature: claude-sonnet-5-medium on behalf of maphew
---------
Co-authored-by: Jacob Hausler <jacob@hausler.cc>
Co-authored-by: matt wilkie <maphew@gmail.com>
205 lines
5.6 KiB
Go
205 lines
5.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestWrapWithSchemaVersion_Legacy_Object(t *testing.T) {
|
|
input := map[string]string{"id": "beads-123", "title": "Test"}
|
|
result := wrapWithSchemaVersion(input)
|
|
|
|
m, ok := result.(map[string]interface{})
|
|
if !ok {
|
|
t.Fatalf("expected map[string]interface{}, got %T", result)
|
|
}
|
|
if m["schema_version"] != JSONSchemaVersion {
|
|
t.Errorf("schema_version = %v, want %d", m["schema_version"], JSONSchemaVersion)
|
|
}
|
|
if m["id"] != "beads-123" {
|
|
t.Errorf("id = %v, want beads-123", m["id"])
|
|
}
|
|
}
|
|
|
|
func TestWrapWithSchemaVersion_Legacy_Slice(t *testing.T) {
|
|
input := []string{"a", "b", "c"}
|
|
result := wrapWithSchemaVersion(input)
|
|
|
|
arr, ok := result.([]string)
|
|
if !ok {
|
|
t.Fatalf("expected []string (passthrough), got %T", result)
|
|
}
|
|
if len(arr) != 3 {
|
|
t.Errorf("slice length = %d, want 3", len(arr))
|
|
}
|
|
}
|
|
|
|
func TestWrapWithSchemaVersion_Legacy_Nil(t *testing.T) {
|
|
result := wrapWithSchemaVersion(nil)
|
|
m, ok := result.(map[string]interface{})
|
|
if !ok {
|
|
t.Fatalf("expected map[string]interface{}, got %T", result)
|
|
}
|
|
if m["schema_version"] != JSONSchemaVersion {
|
|
t.Errorf("schema_version = %v, want %d", m["schema_version"], JSONSchemaVersion)
|
|
}
|
|
}
|
|
|
|
func TestWrapWithSchemaVersion_Envelope_Object(t *testing.T) {
|
|
t.Setenv("BD_JSON_ENVELOPE", "1")
|
|
|
|
input := map[string]string{"id": "beads-123", "title": "Test"}
|
|
result := wrapWithSchemaVersion(input)
|
|
|
|
m, ok := result.(map[string]interface{})
|
|
if !ok {
|
|
t.Fatalf("expected map[string]interface{}, got %T", result)
|
|
}
|
|
if m["schema_version"] != JSONSchemaVersion {
|
|
t.Errorf("schema_version = %v, want %d", m["schema_version"], JSONSchemaVersion)
|
|
}
|
|
data, ok := m["data"]
|
|
if !ok {
|
|
t.Fatal("missing 'data' key in envelope")
|
|
}
|
|
inner, ok := data.(map[string]string)
|
|
if !ok {
|
|
t.Fatalf("data type = %T, want map[string]string", data)
|
|
}
|
|
if inner["id"] != "beads-123" {
|
|
t.Errorf("data.id = %v, want beads-123", inner["id"])
|
|
}
|
|
}
|
|
|
|
func TestWrapWithSchemaVersion_Envelope_Slice(t *testing.T) {
|
|
t.Setenv("BD_JSON_ENVELOPE", "1")
|
|
|
|
input := []string{"a", "b", "c"}
|
|
result := wrapWithSchemaVersion(input)
|
|
|
|
m, ok := result.(map[string]interface{})
|
|
if !ok {
|
|
t.Fatalf("expected envelope map, got %T", result)
|
|
}
|
|
if m["schema_version"] != JSONSchemaVersion {
|
|
t.Errorf("schema_version = %v, want %d", m["schema_version"], JSONSchemaVersion)
|
|
}
|
|
data, ok := m["data"]
|
|
if !ok {
|
|
t.Fatal("missing 'data' key in envelope")
|
|
}
|
|
arr, ok := data.([]string)
|
|
if !ok {
|
|
t.Fatalf("data type = %T, want []string", data)
|
|
}
|
|
if len(arr) != 3 {
|
|
t.Errorf("data length = %d, want 3", len(arr))
|
|
}
|
|
}
|
|
|
|
func TestWrapWithSchemaVersion_Envelope_Nil(t *testing.T) {
|
|
t.Setenv("BD_JSON_ENVELOPE", "1")
|
|
|
|
result := wrapWithSchemaVersion(nil)
|
|
m, ok := result.(map[string]interface{})
|
|
if !ok {
|
|
t.Fatalf("expected envelope map, got %T", result)
|
|
}
|
|
if m["schema_version"] != JSONSchemaVersion {
|
|
t.Errorf("schema_version = %v, want %d", m["schema_version"], JSONSchemaVersion)
|
|
}
|
|
if m["data"] != nil {
|
|
t.Errorf("data = %v, want nil", m["data"])
|
|
}
|
|
}
|
|
|
|
func TestWrapWithSchemaVersion_Envelope_RoundTrip(t *testing.T) {
|
|
t.Setenv("BD_JSON_ENVELOPE", "1")
|
|
|
|
input := map[string]interface{}{"count": 42, "name": "test"}
|
|
result := wrapWithSchemaVersion(input)
|
|
|
|
data, err := json.Marshal(result)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
var parsed map[string]interface{}
|
|
if err := json.Unmarshal(data, &parsed); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
if parsed["schema_version"] != float64(JSONSchemaVersion) {
|
|
t.Errorf("schema_version = %v, want %v", parsed["schema_version"], float64(JSONSchemaVersion))
|
|
}
|
|
innerData, ok := parsed["data"].(map[string]interface{})
|
|
if !ok {
|
|
t.Fatalf("data type = %T, want map[string]interface{}", parsed["data"])
|
|
}
|
|
if innerData["count"] != float64(42) {
|
|
t.Errorf("data.count = %v, want 42", innerData["count"])
|
|
}
|
|
}
|
|
|
|
func TestOutputJSONWithPagination_NoTruncation(t *testing.T) {
|
|
items := []string{"a", "b"}
|
|
out := captureStdout(t, func() error {
|
|
return outputJSONWithPagination(items, nil)
|
|
})
|
|
var arr []string
|
|
if err := json.Unmarshal([]byte(bytes.TrimSpace([]byte(out))), &arr); err != nil {
|
|
t.Fatalf("unmarshal: %v\noutput: %s", err, out)
|
|
}
|
|
if len(arr) != 2 {
|
|
t.Errorf("len = %d, want 2", len(arr))
|
|
}
|
|
}
|
|
|
|
func TestOutputJSONWithPagination_EnvelopeTruncated(t *testing.T) {
|
|
t.Setenv("BD_JSON_ENVELOPE", "1")
|
|
|
|
items := []string{"a", "b", "c"}
|
|
pag := &PaginationMeta{Returned: 3, Total: 10, Truncated: true}
|
|
out := captureStdout(t, func() error {
|
|
return outputJSONWithPagination(items, pag)
|
|
})
|
|
|
|
var m map[string]json.RawMessage
|
|
if err := json.Unmarshal([]byte(bytes.TrimSpace([]byte(out))), &m); err != nil {
|
|
t.Fatalf("unmarshal: %v\noutput: %s", err, out)
|
|
}
|
|
if _, ok := m["pagination"]; !ok {
|
|
t.Fatal("missing 'pagination' key in envelope")
|
|
}
|
|
var got PaginationMeta
|
|
if err := json.Unmarshal(m["pagination"], &got); err != nil {
|
|
t.Fatalf("unmarshal pagination: %v", err)
|
|
}
|
|
if !got.Truncated {
|
|
t.Error("pagination.truncated = false, want true")
|
|
}
|
|
if got.Returned != 3 {
|
|
t.Errorf("pagination.returned = %d, want 3", got.Returned)
|
|
}
|
|
if got.Total != 10 {
|
|
t.Errorf("pagination.total = %d, want 10", got.Total)
|
|
}
|
|
}
|
|
|
|
func TestOutputJSONWithPagination_EnvelopeNotTruncated(t *testing.T) {
|
|
t.Setenv("BD_JSON_ENVELOPE", "1")
|
|
|
|
items := []string{"a", "b"}
|
|
out := captureStdout(t, func() error {
|
|
return outputJSONWithPagination(items, nil)
|
|
})
|
|
|
|
var m map[string]json.RawMessage
|
|
if err := json.Unmarshal([]byte(bytes.TrimSpace([]byte(out))), &m); err != nil {
|
|
t.Fatalf("unmarshal: %v\noutput: %s", err, out)
|
|
}
|
|
if _, ok := m["pagination"]; ok {
|
|
t.Error("unexpected 'pagination' key when not truncated")
|
|
}
|
|
}
|