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

177 lines
7.8 KiB
Go

package httpapi
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
"github.com/steveyegge/beads/issueops"
)
// These cover the transport half of GET /v0/beads/dependencies/blocking: that
// the request reaches the role unaltered, the two size refusals this operation
// owns, how a role refusal is named on the wire, and the never-null members.
// Everything below the wire — which edge types count, which row's status decides
// an edge is live, the order within each list — is the role's, and is pinned by
// backend/conformance/blocking_annotator_contract.go at all three backends.
func newBlockingServer(t *testing.T, annotator *roleBlockingAnnotator) *testServer {
t.Helper()
return newTestServer(t, rolesConfig(Config{BlockingAnnotator: annotator}))
}
// TestBlockingAnnotationsPassTheIDsThroughUnaltered pins the half of this
// handler that is easiest to get wrong by helping: the ids go to the role in
// the order they arrived, repeats and all. Collapsing them is the ROLE's
// promise, and a handler that did it first would make the two front doors
// collapse in different places.
func TestBlockingAnnotationsPassTheIDsThroughUnaltered(t *testing.T) {
annotator := &roleBlockingAnnotator{}
ts := newBlockingServer(t, annotator)
ts.get(t, "/v0/beads/dependencies/blocking?issue_id=bd-2&issue_id=bd-1&issue_id=bd-2")
reads := annotator.blockingRequests()
if len(reads) != 1 {
t.Fatalf("the role saw %d requests, want exactly one", len(reads))
}
want := []string{"bd-2", "bd-1", "bd-2"}
if len(reads[0].IDs) != len(want) {
t.Fatalf("role saw IDs = %v, want %v", reads[0].IDs, want)
}
for i := range want {
if reads[0].IDs[i] != want[i] {
t.Fatalf("role saw IDs = %v, want %v", reads[0].IDs, want)
}
}
}
// TestBlockingAnnotationsCarryTheRoleAnswer pins the wire projection: one entry
// per annotated id, in the role's order, with the role's own struct as the
// element — which is what makes this body and `bd list`'s decoration one
// compatibility domain.
func TestBlockingAnnotationsCarryTheRoleAnswer(t *testing.T) {
annotator := &roleBlockingAnnotator{result: issueops.BlockingResult{Items: []issueops.IssueBlocking{
{ID: "bd-1", BlockedBy: []string{"bd-2", "external:ticket-9"}, Blocks: []string{}, Parent: "bd-9"},
{ID: "bd-3", BlockedBy: []string{}, Blocks: []string{"bd-1"}},
}}}
ts := newBlockingServer(t, annotator)
body := decodeBody(t, ts.get(t, "/v0/beads/dependencies/blocking?issue_id=bd-1&issue_id=bd-3"))
items, _ := body["items"].([]any)
if len(items) != 2 {
t.Fatalf("items = %v, want one entry per annotated id", body["items"])
}
first, _ := items[0].(map[string]any)
if first["id"] != "bd-1" || first["parent"] != "bd-9" {
t.Errorf("items[0] = %v, want bd-1 with parent bd-9", first)
}
blockedBy, _ := first["blocked_by"].([]any)
if len(blockedBy) != 2 || blockedBy[0] != "bd-2" || blockedBy[1] != "external:ticket-9" {
t.Errorf("items[0].blocked_by = %v, want the role's list in the role's order", first["blocked_by"])
}
second, _ := items[1].(map[string]any)
// An entry with no parent omits the member rather than shipping "": the
// document types it as optional and a client reads absence as "no parent".
if _, present := second["parent"]; present {
t.Errorf("items[1] carries a `parent` %v; an id with none omits the member", second["parent"])
}
}
// TestBlockingAnnotationsNeverAnswerNull pins both lists and the envelope as
// empty arrays rather than null: a client that ranges over the answer must not
// have to nil-check any of them, which is what "always present" in the document
// means.
func TestBlockingAnnotationsNeverAnswerNull(t *testing.T) {
annotator := &roleBlockingAnnotator{result: issueops.BlockingResult{Items: []issueops.IssueBlocking{
{ID: "bd-1", BlockedBy: []string{}, Blocks: []string{}},
}}}
ts := newBlockingServer(t, annotator)
raw := readAll(t, ts.get(t, "/v0/beads/dependencies/blocking?issue_id=bd-1"))
if strings.Contains(raw, "null") {
t.Fatalf("body = %s, want empty arrays rather than null", raw)
}
// And the envelope itself, for a role that answered with a nil slice: the
// wire promise does not depend on the role keeping its own.
bare := newBlockingServer(t, &roleBlockingAnnotator{})
if got := readAll(t, bare.get(t, "/v0/beads/dependencies/blocking?issue_id=bd-1")); strings.Contains(got, "null") {
t.Fatalf("body = %s, want an empty items array rather than null", got)
}
}
// TestBlockingAnnotationsBoundTheQuestion pins the two refusals this operation
// owns. They are on the ID COUNT rather than on the answer, and they share
// maxDependencyAnchors with the stored-edge read: the two operations are asked
// about the same page of ids, and two different numbers would let a client ask
// one of them about a page the other refuses.
func TestBlockingAnnotationsBoundTheQuestion(t *testing.T) {
ts := newBlockingServer(t, &roleBlockingAnnotator{})
for _, tc := range []struct {
name string
query string
}{
{"no issue_id at all", "/v0/beads/dependencies/blocking"},
{"more ids than the cap", "/v0/beads/dependencies/blocking?" + 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/blocking?"+repeatedIssueIDs(maxDependencyAnchors)); resp.StatusCode != http.StatusOK {
t.Errorf("status = %d for exactly %d ids, want 200: %s", resp.StatusCode, maxDependencyAnchors, readAll(t, resp))
}
}
// TestBlockingAnnotationsRefuseAnUnknownParameter keeps this operation under
// the document's uniform rule. It publishes exactly ONE parameter, so a client
// that guessed at a `type` or `direction` filter would otherwise receive the
// unfiltered decoration and believe it narrowed.
func TestBlockingAnnotationsRefuseAnUnknownParameter(t *testing.T) {
ts := newBlockingServer(t, &roleBlockingAnnotator{})
resp := ts.get(t, "/v0/beads/dependencies/blocking?issue_id=bd-1&type=blocks")
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
}
body := decodeBody(t, resp)
if body["param"] != "type" || body["reason"] != string(ReasonUnknownParameter) {
t.Errorf("problem = %v, want unknown_parameter on type", body)
}
}
// TestABlockingRoleRefusalIsTheDocumentedBadRequest pins the mapping from the
// role's ErrValidation onto the one parameter that can cause it, and that
// nothing else becomes a 400.
func TestABlockingRoleRefusalIsTheDocumentedBadRequest(t *testing.T) {
ts := newBlockingServer(t, &roleBlockingAnnotator{
err: fmt.Errorf("annotate blocking: %w", issueops.ErrValidation),
})
resp := ts.get(t, "/v0/beads/dependencies/blocking?issue_id=bd-1&issue_id=")
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)
}
opaque := newBlockingServer(t, &roleBlockingAnnotator{err: errors.New("backend is unreachable")})
if resp := opaque.get(t, "/v0/beads/dependencies/blocking?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))
}
}