beads/cmd/bd/schema_test.go
matt wilkie ecb9e92735
feat(cli): bd schema — JSON Schema for --json/export output (carries #4413, griels) (#5098)
* Add `bd schema` command emitting JSON Schema for output records

`bd schema` reflects the canonical issue/dependency output structs
(internal/types) into a JSON Schema via invopop/jsonschema, so consumers can
codegen typed models (datamodel-code-generator, quicktype, ...) instead of
hand-mirroring the --json / export format. Because the schema is generated from
the same structs bd serializes, it cannot drift from the actual output the way
docs/JSON_SCHEMA.md or a downstream consumer model (e.g. beads-mcp's Pydantic
models) can. Output carries schema_version (== JSONSchemaVersion).

Promotes invopop/jsonschema from an indirect to a direct dependency.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
(cherry picked from commit 2bb8d2c305)

* Enumerate status/type/dependency values in `bd schema` + add tests

- Add canonical AllStatuses / AllIssueTypes / AllDependencyTypes lists to
  internal/types and derive Status.IsValid / IssueType.IsValid from them, so
  validation and the schema enums share one source (behaviour-preserving;
  internal/types tests pass).
- `bd schema` now enriches the status, issue_type, and dependency `type`
  fields with their allowed values via a jsonschema Mapper sourced from those
  lists, so the published schema constrains them instead of emitting a bare
  "string" — exactly the constraint hand-mirrored consumers can't track.
- Extract schemaDocument() so the command is unit-testable without a workspace
  or DB; add cmd/bd/schema_test.go asserting valid JSON, schema_version, the
  issue/dependency records, required fields, and the populated enums.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
(cherry picked from commit 9beb73d023)

* Fix misspell + regenerate CLI docs for `bd schema`

- internal/types: "recognises" -> "recognizes" (misspell lint).
- Regenerate docs/CLI_REFERENCE.md, the website CLI reference pages, and
  llms-full.txt to cover the new `bd schema` command (via
  scripts/generate-cli-docs.sh + generate-llms-full.sh from a canonical
  pure-go build). The CLI-docs freshness gate now passes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
(cherry picked from commit 1cd9be76c6)

* fixup: errcheck on outputJSONRaw, godoc placement, go.mod tidy for bd schema

- cmd/bd/schema.go: switch schemaCmd.Run to RunE and return the
  outputJSONRaw error instead of discarding it, matching the
  return-outputJSON(...) pattern used elsewhere in cmd/bd (e.g.
  branch.go, audit.go, comment.go).
- internal/types/types.go: reorder the AllIssueTypes var and its
  godoc so each comment sits directly above its own declaration; the
  cherry-picked var block had split IsValid's godoc comment across
  the inserted var.
- go.mod: go mod tidy after the cherry-picked invopop/jsonschema
  v0.14.0 direct-require resolution; drops the now-unneeded
  mailru/easyjson indirect entry.

Agent-Signature: claude-code-sonnet-medium on behalf of matt wilkie

* fix(schema): skip store init — the command reflects static types only

Without skipStoreAnnotation, bd schema ran the PersistentPreRunE store open
and failed outside a workspace or on a CGO-less build, defeating the
command's storage-free design.

Agent-Signature: claude-fable-5-medium on behalf of matt wilkie

---------

Co-authored-by: Ellis Breen <1263585+griels@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-27 14:16:56 -07:00

90 lines
2.7 KiB
Go

package main
import (
"encoding/json"
"testing"
)
// TestSchemaDocument verifies `bd schema` emits a valid JSON Schema document
// pinned to JSONSchemaVersion, with the issue/dependency record schemas and the
// enriched string enums sourced from internal/types. It exercises the pure
// schemaDocument() builder, so it needs no workspace or database.
func TestSchemaDocument(t *testing.T) {
raw, err := json.Marshal(schemaDocument())
if err != nil {
t.Fatalf("marshal schema document: %v", err)
}
var doc map[string]any
if err := json.Unmarshal(raw, &doc); err != nil {
t.Fatalf("schema document is not valid JSON: %v", err)
}
if got, ok := doc["schema_version"].(float64); !ok || int(got) != JSONSchemaVersion {
t.Errorf("schema_version = %v, want %d", doc["schema_version"], JSONSchemaVersion)
}
recordTypes, ok := doc["types"].(map[string]any)
if !ok {
t.Fatalf("types missing or wrong shape: %T", doc["types"])
}
// --- issue record ---
issue := objectField(t, recordTypes, "issue")
mustContain(t, "issue.required", strSlice(issue["required"]), "id", "title")
issueProps := objectField(t, issue, "properties")
for _, p := range []string{"id", "title", "status", "priority", "issue_type"} {
if _, ok := issueProps[p]; !ok {
t.Errorf("issue schema missing property %q", p)
}
}
mustContain(t, "issue.status.enum", propEnum(t, issueProps, "status"), "open", "closed")
mustContain(t, "issue.issue_type.enum", propEnum(t, issueProps, "issue_type"), "bug", "task", "epic")
// --- dependency record ---
dep := objectField(t, recordTypes, "dependency")
depProps := objectField(t, dep, "properties")
for _, p := range []string{"issue_id", "depends_on_id", "type"} {
if _, ok := depProps[p]; !ok {
t.Errorf("dependency schema missing property %q", p)
}
}
mustContain(t, "dependency.type.enum", propEnum(t, depProps, "type"), "blocks", "parent-child")
}
func objectField(t *testing.T, m map[string]any, key string) map[string]any {
t.Helper()
obj, ok := m[key].(map[string]any)
if !ok {
t.Fatalf("%q missing or not an object: %T", key, m[key])
}
return obj
}
func propEnum(t *testing.T, props map[string]any, name string) []string {
t.Helper()
return strSlice(objectField(t, props, name)["enum"])
}
func strSlice(v any) []string {
arr, _ := v.([]any)
out := make([]string, 0, len(arr))
for _, x := range arr {
if s, ok := x.(string); ok {
out = append(out, s)
}
}
return out
}
func mustContain(t *testing.T, label string, got []string, want ...string) {
t.Helper()
set := make(map[string]bool, len(got))
for _, g := range got {
set[g] = true
}
for _, w := range want {
if !set[w] {
t.Errorf("%s = %v, missing %q", label, got, w)
}
}
}