beads/issueops/gates_test.go
Julian Knutsen dff9492c61
fix(http): gate check records a bead gate's sighting through Lifecycle.Update (#7604)
* fix(http): gate check's bead sighting goes through Lifecycle.Update (issues.update)

The bead-gate sighting was written through the legacy map-based
UpdateIssue, which the http store does not implement, so the refusal
happened client-side and no issues.update was ever sent: every open bead
gate with an open target failed on every sweep over HTTP.

- recordSeen and the gh:run await_id persist now go through the Lifecycle
  role (updateGateThroughLifecycle): a single-key metadata set for the
  sighting, an AwaitID patch for the run. The http client still refuses the
  await_id patch before the dial (W-IssuePatch.AwaitID); the gate's error now
  names the await_id update instead of a Go method.
- backend/http/corpus TestServedGateCheckOverAwaitTypedGates seeds
  await-typed gates in the served store and proves, through the reference
  proxy: --dry-run writes nothing, timers close, the bead sighting persists,
  merged gh:pr gates close, and the gh:run persist refuses by name with
  await_id untouched.
- CHANGELOG corrected.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(cmd/bd): gate check skips template gates; narrow the await_id reword

Review fixes for the gate-check sighting change.

- A persisted proto's gates (bd cook --persist) are templates, and
  issueops.ListGates lists them with the open gates. bd gate check now
  leaves them out of its listing's query: GateListRequest gains
  ExcludeTemplates, which only listOpenGatesWith sets, and it adds
  `template=false` to the expression, so the cut at --limit counts only
  the gates that remain. bd gate list leaves it unset and still lists
  templates. Dropping them from the page after the cut would let a page
  of a proto's gates (newer than the live ones at the same priority)
  fill --limit: the sweep would print "No open gates found." and exit 0
  with the live gates unchecked. filterCheckableGates, which both the
  direct and the proxied-server routes select through, keeps the skip
  as a backstop. Before this, each route mishandled templates in its
  own way:
  - Direct route: the Lifecycle role refused the sighting and await_id
    writes, so the sweep failed every time, after a gh:run template had
    already queried GitHub. In a local workspace, an expired template
    timer gate was closed in the proto.
  - Over HTTP: bd serve refused the close with 409 template_read_only.
  - Proxied-server route: the template timer gate was closed and a
    sighting was stamped on the template bead gate, silently.
- persistGateCheckAwaitID rewords only the HTTP wire's
  W-IssuePatch.AwaitID refusal. The new message names the run, await_id
  and the bd serve URL, and advises checking the gate in a local
  workspace: giving it a numeric run ID would be the same await_id edit
  the wire refuses. The error chain is kept, so *storage.ErrUnsupported
  and the ledger row stay reachable. Every other error comes back as the
  role raised it, including a capability the server does not advertise
  (#7589's issues.update.gates) and a local backend's ErrUnsupported.
  Before, every ErrUnsupported got the same sentence.
- Tests:
  - Unit: the template skip for every type filter; a sweep over template
    gates beside a live one; ListGates putting `template=false` in the
    query under ExcludeTemplates, and gate check's listing setting it;
    the reword and its chain; the pass-through cases; the sighting patch
    matching its map form.
  - Embedded: a cooked proto's gh:run, bead and timer gates are
    untouched, and gh is never run. With --limit 1 behind that proto's
    gates, a live expired timer gate is checked and closed.
  - Proxied-server: template timer and bead gates stay open and
    unstamped. With --limit 1 behind two newer template timer gates, the
    live one is checked and closed.
  - Served: the template gates are untouched, and the gh:run error names
    the server. --dry-run --limit 3 behind three template gates checks
    three live gates.
  - Each --limit case first shows, through bd gate list, that a template
    leads the listing.
  - Against the PR-head bd, the embedded, proxied and served template
    cases fail. Against a bd that skips templates after the cut, the
    three --limit cases fail, printing "No open gates found." and
    exiting 0.
- bd_test gains the //internal/httpclient{,/encode} deps (gazelle-checked).
  The embedded shard manifest lists both TestEmbeddedGateCheck* tests.
  The CHANGELOG notes the template skip, that templates take no row of
  --limit, and the new wording.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
2026-10-16 15:14:53 +00:00

112 lines
4.4 KiB
Go

package issueops_test
import (
"context"
"errors"
"reflect"
"testing"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
func gateQuery(all bool, limit int) issueops.QueryRequest {
return issueops.QueryRequest{Expression: "type=gate", IncludeClosed: all, Limit: &limit}
}
// TestListGatesAsksTheQuerierTheRawQuestion is S20: `bd gate list` used to
// build a raw SearchIssues filter (gate type, closed excluded unless --all),
// which the http backend refuses. The entry asks the Querier role the same
// question, so every route answers it through a role every server serves.
func TestListGatesAsksTheQuerierTheRawQuestion(t *testing.T) {
cases := []struct {
name string
req issueops.GateListRequest
want issueops.QueryRequest
}{
{"open gates, default limit", issueops.GateListRequest{Limit: 50}, gateQuery(false, 50)},
{"unlimited", issueops.GateListRequest{Limit: 0}, gateQuery(false, 0)},
{"all gates", issueops.GateListRequest{All: true, Limit: 7}, gateQuery(true, 7)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
q := &currentQueryRecorder{responses: []currentQueryResponse{{ids: []string{"g-1", "g-2"}}}}
got, err := issueops.ListGates(context.Background(), q, tc.req)
if err != nil {
t.Fatalf("ListGates: %v", err)
}
if len(q.calls) != 1 || !reflect.DeepEqual(q.calls[0], tc.want) {
t.Fatalf("Query calls = %#v, want one %#v", q.calls, tc.want)
}
ids := make([]string, 0, len(got))
for _, g := range got {
ids = append(ids, g.ID)
}
if !reflect.DeepEqual(ids, []string{"g-1", "g-2"}) {
t.Fatalf("gates = %v, want [g-1 g-2] in the Querier's order", ids)
}
})
}
}
// TestListGatesExcludesTemplatesInTheQuery: ExcludeTemplates (bd gate check's
// listing) puts the template exclusion in the expression the Querier answers,
// so the cut at Limit counts only the gates that remain. Dropping templates
// from the returned page instead let a page of a persisted proto's gates fill
// the limit and hide every live gate behind it. Without it the listing names
// no template field: `bd gate list` lists templates.
func TestListGatesExcludesTemplatesInTheQuery(t *testing.T) {
nonTemplate := func(all bool, limit int) issueops.QueryRequest {
return issueops.QueryRequest{Expression: "type=gate AND template=false", IncludeClosed: all, Limit: &limit}
}
cases := []struct {
name string
req issueops.GateListRequest
want issueops.QueryRequest
}{
{"open gates, limit 1", issueops.GateListRequest{ExcludeTemplates: true, Limit: 1}, nonTemplate(false, 1)},
{"unlimited", issueops.GateListRequest{ExcludeTemplates: true}, nonTemplate(false, 0)},
{"all gates", issueops.GateListRequest{All: true, ExcludeTemplates: true, Limit: 7}, nonTemplate(true, 7)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
q := &currentQueryRecorder{responses: []currentQueryResponse{{ids: []string{"g-1"}}}}
if _, err := issueops.ListGates(context.Background(), q, tc.req); err != nil {
t.Fatalf("ListGates: %v", err)
}
if len(q.calls) != 1 || !reflect.DeepEqual(q.calls[0], tc.want) {
t.Fatalf("Query calls = %#v, want one %#v", q.calls, tc.want)
}
})
}
}
func TestListGatesNeverTakesAFailureForAnEmptyList(t *testing.T) {
refusal := &issueops.ErrUnsupported{Op: "Querier.Query", Backend: "http"}
q := &currentQueryRecorder{responses: []currentQueryResponse{{err: refusal}}}
got, err := issueops.ListGates(context.Background(), q, issueops.GateListRequest{Limit: 50})
if !errors.Is(err, refusal) || got != nil {
t.Fatalf("ListGates = (%v, %v), want (nil, the refusal)", got, err)
}
}
func TestListGatesDropsRowsWithoutAnIssue(t *testing.T) {
q := gateRows{items: []*types.IssueWithCounts{nil, {}, {Issue: &types.Issue{ID: "g-1"}}}}
got, err := issueops.ListGates(context.Background(), q, issueops.GateListRequest{})
if err != nil || len(got) != 1 || got[0].ID != "g-1" {
t.Fatalf("ListGates = (%v, %v), want ([g-1], nil)", got, err)
}
}
func TestListGatesRefusesANilQuerier(t *testing.T) {
var unsupported *issueops.ErrUnsupported
if _, err := issueops.ListGates(context.Background(), nil, issueops.GateListRequest{}); !errors.As(err, &unsupported) {
t.Fatalf("ListGates(nil) error = %v, want ErrUnsupported", err)
}
}
type gateRows struct{ items []*types.IssueWithCounts }
func (g gateRows) Query(context.Context, issueops.QueryRequest) (issueops.IssuePage, error) {
return issueops.IssuePage{Items: g.items}, nil
}