beads/internal/httpapi/edges_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

172 lines
6.9 KiB
Go

package httpapi
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// These cover the transport half of GET /v0/beads/dependencies: the two size
// refusals this operation owns, how a role refusal is named on the wire, and the
// two never-null members. Everything below the wire is the role's, and is pinned
// by backend/conformance/edge_reader_contract.go at all three backends.
func newEdgesServer(t *testing.T, edges *roleEdgeReader) *testServer {
t.Helper()
return newTestServer(t, rolesConfig(Config{EdgeReader: edges}))
}
// TestDependenciesFlattenTheRoleAnswer pins the wire projection: the per-anchor
// answer becomes ONE flat array of edges in anchor order, which is the shape
// `bd dep list a b c --json` emits, plus the ghost anchors on their own member.
func TestDependenciesFlattenTheRoleAnswer(t *testing.T) {
edges := &roleEdgeReader{result: issueops.EdgeReadResult{Anchors: []issueops.AnchorEdges{
{ID: "bd-1", Edges: []*types.Dependency{
{IssueID: "bd-1", DependsOnID: "bd-2", Type: types.DepBlocks},
{IssueID: "bd-1", DependsOnID: "external:ticket-9", Type: types.DepRelated},
}},
{ID: "bd-3", Missing: true},
{ID: "bd-4", Edges: []*types.Dependency{
{IssueID: "bd-4", DependsOnID: "bd-2", Type: types.DepBlocks},
}},
}}}
ts := newEdgesServer(t, edges)
body := decodeBody(t, ts.get(t, "/v0/beads/dependencies?issue_id=bd-1&issue_id=bd-3&issue_id=bd-4"))
items, _ := body["items"].([]any)
if len(items) != 3 {
t.Fatalf("items = %v, want the three edges the role returned", body["items"])
}
want := []string{"bd-2", "external:ticket-9", "bd-2"}
for i, item := range items {
edge, _ := item.(map[string]any)
if edge["depends_on_id"] != want[i] {
t.Errorf("items[%d].depends_on_id = %v, want %q (anchor order, then the role's edge order)", i, edge["depends_on_id"], want[i])
}
// The row's surrogate key is not selected by this read, so `omitempty`
// keeps it off the wire entirely rather than shipping an empty string.
if _, present := edge["id"]; present {
t.Errorf("items[%d] carries an `id`; the source-keyed read does not select it", i)
}
}
if missing, _ := body["missing"].([]any); len(missing) != 1 || missing[0] != "bd-3" {
t.Errorf("missing = %v, want just the ghost anchor", body["missing"])
}
}
// TestDependenciesNeverAnswerNull pins both members as empty arrays rather than
// null, which the document states for each of them: a client that ranges over
// the answer must not have to nil-check either one.
func TestDependenciesNeverAnswerNull(t *testing.T) {
edges := &roleEdgeReader{result: issueops.EdgeReadResult{Anchors: []issueops.AnchorEdges{
{ID: "bd-1"},
}}}
ts := newEdgesServer(t, edges)
raw := readAll(t, ts.get(t, "/v0/beads/dependencies?issue_id=bd-1"))
if strings.Contains(raw, "null") {
t.Fatalf("body = %s, want empty arrays rather than null", raw)
}
}
// TestDependenciesBoundTheQuestion pins the two refusals this operation owns.
// They are on the ANCHOR COUNT rather than on the answer because `bd dep list`
// has no limit, so a `limit` here would make the two front doors default
// differently; see maxDependencyAnchors.
func TestDependenciesBoundTheQuestion(t *testing.T) {
edges := &roleEdgeReader{}
ts := newEdgesServer(t, edges)
for _, tc := range []struct {
name string
query string
}{
{"no issue_id at all", "/v0/beads/dependencies"},
{"an empty issue_id list", "/v0/beads/dependencies?type=blocks"},
{"more anchors than the cap", "/v0/beads/dependencies?" + repeatedIssueIDs(maxDependencyAnchors+1)},
} {
t.Run(tc.name, func(t *testing.T) {
resp := ts.get(t, tc.query)
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
}
body := decodeBody(t, resp)
if body["code"] != string(CodeInvalidArgument) || body["param"] != "issue_id" || body["reason"] != string(ReasonInvalidValue) {
t.Errorf("problem = %v, want invalid_argument on issue_id with invalid_value", body)
}
})
}
// The cap is a bound, not a rejection of large requests: exactly the cap
// is served.
if resp := ts.get(t, "/v0/beads/dependencies?"+repeatedIssueIDs(maxDependencyAnchors)); resp.StatusCode != http.StatusOK {
t.Errorf("status = %d for exactly %d anchors, want 200: %s", resp.StatusCode, maxDependencyAnchors, readAll(t, resp))
}
}
// TestDependenciesRefuseAnUnknownParameter keeps this operation under the
// document's uniform rule: an unrecognized filter parameter WIDENS the result
// set, so it is version skew and not something to ignore.
func TestDependenciesRefuseAnUnknownParameter(t *testing.T) {
ts := newEdgesServer(t, &roleEdgeReader{})
resp := ts.get(t, "/v0/beads/dependencies?issue_id=bd-1&direction=up")
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
}
body := decodeBody(t, resp)
if body["param"] != "direction" || body["reason"] != string(ReasonUnknownParameter) {
t.Errorf("problem = %v, want unknown_parameter on direction", body)
}
}
// TestADependencyRoleRefusalIsTheDocumentedBadRequest pins the mapping from the
// role's ErrValidation onto the two parameters that can cause it.
//
// The role owns both refusals — an empty id, an unusable dependency type — so
// the handler names the parameter from the request it still holds: an empty
// entry can only have come from issue_id, so anything else is type.
func TestADependencyRoleRefusalIsTheDocumentedBadRequest(t *testing.T) {
for _, tc := range []struct {
name string
query string
param string
}{
{"an empty id", "/v0/beads/dependencies?issue_id=bd-1&issue_id=", "issue_id"},
{"an unusable type", "/v0/beads/dependencies?issue_id=bd-1&type=", "type"},
} {
t.Run(tc.name, func(t *testing.T) {
ts := newEdgesServer(t, &roleEdgeReader{
err: fmt.Errorf("read edges: %w", issueops.ErrValidation),
})
resp := ts.get(t, tc.query)
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
}
body := decodeBody(t, resp)
if body["code"] != string(CodeInvalidArgument) || body["param"] != tc.param || body["reason"] != string(ReasonInvalidValue) {
t.Errorf("problem = %v, want invalid_argument on %q with invalid_value", body, tc.param)
}
})
}
// Anything that is NOT a validation refusal keeps going through the one
// mapping in problem.go rather than becoming a wrong 400.
ts := newEdgesServer(t, &roleEdgeReader{err: errors.New("backend is unreachable")})
if resp := ts.get(t, "/v0/beads/dependencies?issue_id=bd-1"); resp.StatusCode != http.StatusInternalServerError {
t.Errorf("status = %d for an opaque role failure, want 500: %s", resp.StatusCode, readAll(t, resp))
}
}
func repeatedIssueIDs(n int) string {
parts := make([]string, 0, n)
for i := 0; i < n; i++ {
parts = append(parts, fmt.Sprintf("issue_id=bd-%d", i))
}
return strings.Join(parts, "&")
}