beads/cmd/bd/gate_check_listing_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

102 lines
4 KiB
Go

package main
import (
"context"
"errors"
"reflect"
"testing"
"time"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// recordingGateQuerier records every Query and answers with fixed rows.
type recordingGateQuerier struct {
calls []issueops.QueryRequest
items []*types.IssueWithCounts
err error
}
func (q *recordingGateQuerier) Query(_ context.Context, req issueops.QueryRequest) (issueops.IssuePage, error) {
q.calls = append(q.calls, req)
if q.err != nil {
return issueops.IssuePage{}, q.err
}
return issueops.IssuePage{Items: q.items}, nil
}
// TestGateCheckListsOpenGatesThroughListGates pins the gate check listing to
// the Querier role (queryIssues on a remote backend) instead of the raw
// SearchIssues filter the http store refused before reading a gate: the gate
// type, closed excluded, templates excluded in the query itself (so a
// persisted proto's gates take no row of --limit), the --limit carried as
// given (0 = unlimited).
func TestGateCheckListsOpenGatesThroughListGates(t *testing.T) {
for _, limit := range []int{0, 5, 100} {
q := &recordingGateQuerier{items: []*types.IssueWithCounts{
{Issue: &types.Issue{ID: "g-timer", IssueType: "gate", AwaitType: "timer"}},
{Issue: &types.Issue{ID: "g-pr", IssueType: "gate", AwaitType: "gh:pr"}},
}}
got, err := listOpenGatesWith(context.Background(), q, limit)
if err != nil {
t.Fatalf("limit %d: listOpenGatesWith: %v", limit, err)
}
l := limit
want := issueops.QueryRequest{Expression: "type=gate AND template=false", IncludeClosed: false, Limit: &l}
if len(q.calls) != 1 || !reflect.DeepEqual(q.calls[0], want) {
t.Fatalf("limit %d: Query calls = %#v, want one %#v", limit, q.calls, want)
}
if len(got) != 2 || got[0].ID != "g-timer" || got[1].ID != "g-pr" {
t.Fatalf("limit %d: gates = %v, want [g-timer g-pr] in the Querier's order", limit, got)
}
// The per-type selection still happens after the listing.
timers := filterCheckableGates(got, "timer")
if len(timers) != 1 || timers[0].ID != "g-timer" {
t.Fatalf("limit %d: timer gates = %v, want [g-timer]", limit, timers)
}
gh := filterCheckableGates(got, "gh")
if len(gh) != 1 || gh[0].ID != "g-pr" {
t.Fatalf("limit %d: gh gates = %v, want [g-pr]", limit, gh)
}
}
}
// TestGateCheckListingFailureIsAnError keeps a refused or failed listing an
// error: gate-sweep must fail loudly, never report "No open gates found."
func TestGateCheckListingFailureIsAnError(t *testing.T) {
refusal := &issueops.ErrUnsupported{Op: "Querier.Query", Backend: "http"}
q := &recordingGateQuerier{err: refusal}
got, err := listOpenGatesWith(context.Background(), q, 0)
if !errors.Is(err, refusal) || got != nil {
t.Fatalf("listOpenGatesWith = (%v, %v), want (nil, the refusal)", got, err)
}
}
// TestGateCheckTimerArmOverListedGates runs the evaluation over the listed
// rows: an expired timer gate resolves, an unexpired one stays pending, and a
// gate with no await type is not evaluated (the corpus seed's case).
func TestGateCheckTimerArmOverListedGates(t *testing.T) {
now := time.Date(2026, 10, 15, 12, 0, 0, 0, time.UTC)
q := &recordingGateQuerier{items: []*types.IssueWithCounts{
{Issue: &types.Issue{ID: "g-expired", IssueType: "gate", AwaitType: "timer", CreatedAt: now.Add(-2 * time.Hour), Timeout: time.Hour}},
{Issue: &types.Issue{ID: "g-pending", IssueType: "gate", AwaitType: "timer", CreatedAt: now.Add(-time.Minute), Timeout: time.Hour}},
{Issue: &types.Issue{ID: "g-untyped", IssueType: "gate"}},
}}
gates, err := listOpenGatesWith(context.Background(), q, 0)
if err != nil {
t.Fatalf("listOpenGatesWith: %v", err)
}
results := evaluateGates(context.Background(), filterCheckableGates(gates, ""), now, nil, nil, nil)
got := map[string]bool{}
for _, r := range results {
if r.err != nil {
t.Fatalf("%s: unexpected error %v", r.gate.ID, r.err)
}
got[r.gate.ID] = r.resolved
}
want := map[string]bool{"g-expired": true, "g-pending": false}
if !reflect.DeepEqual(got, want) {
t.Fatalf("evaluated = %v, want %v (g-untyped skipped)", got, want)
}
}