beads/internal/httpapi/tree_test.go
CI Bot 67d38d2620 Trim this series' comments toward the repo's own density
Ten agents over 216 files, one directory-balanced group each, working to a
rubric that named what to cut (duplication narration, restatement of the next
line, the governing rule echoed in every command file, throat-clearing) and
what to keep (traps, invariants, security, tracker ids, behaviour changes, and
the "why a naive simplification destroys this assertion" rationale).

Net 1,523 lines. Density against this repo's own per-directory norm on main:

  cmd/bd              33.6% -> 27.3%   (main 10.7%)
  internal/storage    26.1% -> 23.1%   (main 14.1%)
  backend/conformance 34.0% -> 29.6%   (main 22.8%)
  internal/httpapi    30.9% -> 27.8%   (main 30.6%, now below)
  overall             34.7% -> 31.6%

Every agent landed at 18-25% rather than the 30% asked for, and every one gave
the same reason independently: what remained was on the keep list. Ten separate
judgements agreeing is the most useful result here — the prose is dense, but it
is mostly load-bearing, and the shortfall is the honest answer rather than a
failure to try.

TWO REVERTS, both to protect something worth more than the lines.

issueops/ is back to its pre-trim state, giving up 610 lines. The conformance
contracts cite the leaf docs BY LINE — "pins treewalker.go:52-66" — 155 times.
Trimming the cited files silently repointed those at whatever text moved into
those numbers, across agent boundaries so no agent could see it. A semantic
remapper recovered 141 of them, but 23 needed judgement and several landed on
struct fields. The leaf docs are the spec the contracts are written against;
they are the one place in this change where dense prose IS the deliverable, and
keeping them stable is worth more than the 610 lines. tree_walker_contract.go
and version_reconciler_contract.go went back for the same reason: an agent had
deleted a paragraph containing a citation, so positional restoration could not
align them.

After the reverts, exactly 5 citations point into a still-trimmed file and 2 of
those are weak — both in cmd/bd/config_guard_test.go, which this series never
touched and whose citations were already wrong on main (init.go:361 pointed at
a closing brace; the guard it names is at init.go:1499).

VERIFICATION. A guard strips every comment and all whitespace from each of the
193 modified files at HEAD and in the worktree and requires the two to be
byte-identical, so a code change cannot hide in a comment diff. It handles `//`
inside string and raw-string literals, and was tested against a deliberate code
change before being trusted. gofmt clean, go build, go vet, golangci-lint 0
issues, and the two AST-based tests that PARSE trimmed files — the timed
provider binding pin over claim.go and the serve database-source gate over
serve.go — both pass.

One real defect found while trimming: checkDatabaseSource's doc block ran into
`// sourceRoles is ...` with no blank line, so godoc attached thirty-one lines
of the wrong prose to func sourceRoles and left checkDatabaseSource
undocumented. Fixed by separating them.
2026-08-06 08:06:59 +00:00

282 lines
9.7 KiB
Go

package httpapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"testing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
const treePath = "/v0/beads/dependencies/tree"
// treeServer binds a roles-backed server whose tree walker answers with walker.
// Listen requires a COMPLETE source, so rolesConfig fills in the rest.
func treeServer(t *testing.T, walker *roleTreeWalker) *testServer {
t.Helper()
return newTestServer(t, rolesConfig(Config{TreeWalker: walker}))
}
func treeNode(id string, depth int, parentID string, edge types.DependencyType) *types.TreeNode {
return &types.TreeNode{
Issue: types.Issue{ID: id, Title: strings.ToUpper(id), Status: types.StatusOpen, Priority: 2},
Depth: depth,
ParentID: parentID,
EdgeFromParent: edge,
}
}
// TestTreeCarriesTheRolesAnswerToTheWire is the whole point of the operation:
// the body is the role's node list projected onto the envelope, in order, with
// no shaping in between. It asserts the JSON rather than the Go value because
// the alias is what makes them the same thing — apigen.TreeNode IS
// types.TreeNode — so the only way for this to fail is a handler that reordered,
// renamed or dropped something.
func TestTreeCarriesTheRolesAnswerToTheWire(t *testing.T) {
ts := treeServer(t, &roleTreeWalker{result: issueops.TreeResult{Nodes: []*types.TreeNode{
treeNode("bd-root", 0, "", ""),
treeNode("bd-child", 1, "bd-root", types.DepBlocks),
treeNode("bd-grand", 2, "bd-child", types.DepParentChild),
}}})
resp := ts.get(t, treePath+"?root_id=bd-root")
if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200", resp.StatusCode)
}
body := decodeBody(t, resp)
if hasMore, _ := body["has_more"].(bool); hasMore {
t.Error("has_more = true; this operation takes no limit, so nothing can truncate the walk")
}
items, ok := body["items"].([]any)
if !ok || len(items) != 3 {
t.Fatalf("items = %#v, want the role's three nodes", body["items"])
}
for i, want := range []struct {
id string
depth float64
parent string
}{
{"bd-root", 0, ""},
{"bd-child", 1, "bd-root"},
{"bd-grand", 2, "bd-child"},
} {
node, _ := items[i].(map[string]any)
if node["id"] != want.id {
t.Errorf("item %d id = %v, want %q: the answer is in walk order", i, node["id"], want.id)
}
if node["depth"] != want.depth {
t.Errorf("item %d depth = %v, want %v", i, node["depth"], want.depth)
}
if node["parent_id"] != want.parent {
t.Errorf("item %d parent_id = %v, want %q", i, node["parent_id"], want.parent)
}
if node["truncated"] != false {
t.Errorf("item %d truncated = %v, want an explicit false: the field is documented as always false", i, node["truncated"])
}
}
// The root carries no edge, so `edge_from_parent` is omitted rather than
// emitted empty.
root, _ := items[0].(map[string]any)
if _, present := root["edge_from_parent"]; present {
t.Errorf("the root carries an edge_from_parent key (%#v); it was reached from nothing", root["edge_from_parent"])
}
child, _ := items[1].(map[string]any)
if child["edge_from_parent"] != string(types.DepBlocks) {
t.Errorf("child edge_from_parent = %v, want %q", child["edge_from_parent"], types.DepBlocks)
}
}
// TestTreeHandsTheWiresParametersToTheRoleUnchanged is the half that proves the
// handler decides nothing: every parameter the document publishes arrives on the
// request, spelled as the role spells it.
func TestTreeHandsTheWiresParametersToTheRoleUnchanged(t *testing.T) {
walker := &roleTreeWalker{}
ts := treeServer(t, walker)
ts.get(t, treePath+"?root_id=bd-1&direction=up&max_depth=3&status=in_progress")
reqs := walker.walkRequests()
if len(reqs) != 1 {
t.Fatalf("walk requests = %+v, want exactly one", reqs)
}
want := issueops.WalkTreeRequest{
RootID: "bd-1",
Direction: issueops.TreeUp,
MaxDepth: 3,
Status: types.StatusInProgress,
}
if reqs[0] != want {
t.Errorf("walk request = %+v, want %+v", reqs[0], want)
}
// No max_rows reaches the role from this surface: the defensive cap is a CLI
// circuit breaker and the document does not publish one here.
if reqs[0].MaxRows != 0 || reqs[0].MaxRowsSource != "" {
t.Errorf("walk request carries a cap (%d, %q); this surface publishes none",
reqs[0].MaxRows, reqs[0].MaxRowsSource)
}
}
// TestTreeDefaultsTheDirectionAndTheDepth pins the two defaults the DOCUMENT
// states, which are the front door's rather than the role's: the role refuses a
// zero depth outright, so a handler that forwarded an unset one would 400 every
// request that left max_depth off.
func TestTreeDefaultsTheDirectionAndTheDepth(t *testing.T) {
walker := &roleTreeWalker{}
ts := treeServer(t, walker)
ts.get(t, treePath+"?root_id=bd-1")
reqs := walker.walkRequests()
if len(reqs) != 1 {
t.Fatalf("walk requests = %+v, want exactly one", reqs)
}
if reqs[0].Direction != issueops.TreeDown {
t.Errorf("direction = %q, want the documented default %q", reqs[0].Direction, issueops.TreeDown)
}
if reqs[0].MaxDepth != defaultTreeDepth {
t.Errorf("max_depth = %d, want the documented default %d", reqs[0].MaxDepth, defaultTreeDepth)
}
}
// TestTreeAnswersAnEmptyArrayRatherThanNull pins the `never null` half of the
// envelope. The role's own slice is empty rather than nil, but the wire promise
// must not depend on it keeping that.
func TestTreeAnswersAnEmptyArrayRatherThanNull(t *testing.T) {
ts := treeServer(t, &roleTreeWalker{})
var body struct {
Items *[]json.RawMessage `json:"items"`
HasMore bool `json:"has_more"`
}
resp := ts.get(t, treePath+"?root_id=bd-1")
defer func() { _ = resp.Body.Close() }()
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
t.Fatalf("decode: %v", err)
}
if body.Items == nil {
t.Fatal("items is null, want an empty array")
}
if len(*body.Items) != 0 {
t.Fatalf("items = %v, want empty", *body.Items)
}
}
// TestTreeRefusesTheDocumentedBadRequests pins each 400 the operation can raise
// AND which parameter it attributes it to: `param` is what a client dispatches
// on, and the three have different recoveries.
func TestTreeRefusesTheDocumentedBadRequests(t *testing.T) {
for _, test := range []struct {
name string
query string
param string
role *roleTreeWalker
}{
{
name: "no root_id",
query: "",
param: "root_id",
},
{
name: "an empty root_id",
query: "?root_id=",
param: "root_id",
},
{
name: "a direction outside the closed set",
query: "?root_id=bd-1&direction=sideways",
param: "direction",
},
{
name: "a non-integer max_depth",
query: "?root_id=bd-1&max_depth=deep",
param: "max_depth",
},
{
// This one is the ROLE's refusal reaching the wire rather than the
// handler's: the decode accepts 0 happily, and treewalker.go is what
// says a zero depth is not "unbounded".
name: "a zero max_depth",
query: "?root_id=bd-1&max_depth=0",
param: "max_depth",
role: &roleTreeWalker{err: fmt.Errorf("%w: max depth must be at least 1, got 0",
issueops.ErrValidation)},
},
{
name: "an unknown parameter",
query: "?root_id=bd-1&limit=5",
param: "limit",
},
} {
t.Run(test.name, func(t *testing.T) {
role := test.role
if role == nil {
role = &roleTreeWalker{}
}
ts := treeServer(t, role)
resp := ts.get(t, treePath+test.query)
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", resp.StatusCode)
}
body := decodeBody(t, resp)
if body["code"] != string(CodeInvalidArgument) {
t.Errorf("code = %v, want invalid_argument", body["code"])
}
if body["param"] != test.param {
t.Errorf("param = %v, want %q", body["param"], test.param)
}
})
}
}
// TestTreeAnswers404ForARootThatIsNotThere pins the difference from
// GET /v0/beads/dependencies, which reports a miss in the body: this operation
// has ONE anchor, so there is no other answer to preserve.
func TestTreeAnswers404ForARootThatIsNotThere(t *testing.T) {
ts := treeServer(t, &roleTreeWalker{err: fmt.Errorf("%w: issue bd-ghost", storage.ErrNotFound)})
resp := ts.get(t, treePath+"?root_id=bd-ghost")
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("status = %d, want 404", resp.StatusCode)
}
if code := decodeBody(t, resp)["code"]; code != string(CodeNotFound) {
t.Errorf("code = %v, want not_found", code)
}
}
// TestTreeMapsARoleFailureThroughTheOneErrorMapping keeps the walk on the same
// classification every other route uses.
func TestTreeMapsARoleFailureThroughTheOneErrorMapping(t *testing.T) {
ts := treeServer(t, &roleTreeWalker{err: errors.New("backend is unreachable")})
resp := ts.get(t, treePath+"?root_id=bd-1")
if resp.StatusCode != http.StatusInternalServerError {
t.Fatalf("status = %d, want 500", resp.StatusCode)
}
if code := decodeBody(t, resp)["code"]; code != string(CodeInternal) {
t.Errorf("code = %v, want internal", code)
}
}
// TestTreeReachesTheProviderAccessor covers the OTHER database source: the
// handler must reach the role through uow.TreeWalkerSource on the timed
// provider, not through the roles fields, which are nil on this server.
func TestTreeReachesTheProviderAccessor(t *testing.T) {
ts := newTestServer(t, Config{})
resp := ts.get(t, treePath+"?root_id=bd-1")
// A handler wired to the roles field, which this source leaves nil, would
// panic into a recovered 500 rather than reaching the provider's accessor.
if strings.Contains(ts.stderr.String(), "event=panic") {
t.Fatalf("the handler panicked; it read a role this source does not set:\n%s", ts.stderr.String())
}
// That the request got past the accessor is the assertion.
if resp.StatusCode == http.StatusInternalServerError {
t.Fatalf("status = 500 from the provider-backed source: %s", readAll(t, resp))
}
}