beads/cmd/bd/gate_check_listing_test.go
Julian Knutsen b126a7d1d2
fix(http): bd gate check over HTTP through issueops.ListGates (#7578)
* fix(http): bd gate check over http through issueops.ListGates

gate check listed its open gates with a raw SearchIssues filter (gate type,
closed excluded, --limit), which the HTTP backend refuses, so the command
failed before reading a gate on a remote scope and gc's core gate-sweep order
(bd gate check --type=timer --escalate) failed every tick there.

The listing now goes through issueops.ListGates over openQuerier on the
direct and proxied routes, the same entry bd gate list took in S20 (#7549):
the same question, served as queryIssues by every deployed server. The close,
the bead-gate target read and sighting, the pure timer arm and the gh/gt execs
are unchanged. The gh:run discovery's await_id persist still refuses on the
wire and counts as that gate's error.

The served corpus gains mc-gate-check-timer, mc-gate-check-gh (gate-sweep's
argv) and gate-check-all. No seeded gate carries an await type, since no wire
create accepts await_*, so these prove the listing is served.

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

* docs(gate): name both refused gate check writes; close gate's filter hole

Review fixes for #7578 (apply-fixes, iteration 1).

CHANGELOG, minor (taken): the entry said a bead gate's sighting was
already served over HTTP. It is not. evaluateBeadGate records the first
sighting (await_seen) through recordSeen, which calls the raw
store.UpdateIssue (cmd/bd/gate.go:675). The HTTP Store has no UpdateIssue
override, so its embedded unsupportedDoltStorage refuses the call in the
client before any request is sent (internal/httpclient/unsupported_gen.go),
exactly as it refuses the gh:run discovery's await_id persist. "Refuses on
the wire" was wrong for the same reason. The entry now names both writes
and keeps "counts as that gate's error" (applyGateCheckResults counts
r.err). The sighting is written only when the check finds the awaited
bead, not yet closed, in the gate's own store; inspectBeadGate sets
seenHere only there.

CHANGELOG, nit (taken): `bd gate check --limit -1` used to list every
open gate on the direct and proxied routes, because the raw search
emitted no LIMIT clause for a negative limit. The listing now goes
through issueops.ListGates, whose Querier rejects a negative limit as
ErrValidation, and the HTTP server rejects it too
(internal/httpapi/query.go). The entry says so.

.golangci.yml, minor (taken): at this head cmd/bd/gate.go and
gate_proxied_server.go name no filter type (one each at the base), so
their slot in the cmd/bd forbidigo family only kept a hole open. `gate`
leaves the alternation, with a "LEFT this list" note like the ones for
search, status and count. gate_discover keeps its own hole:
findPendingGates still builds the raw filter.

Declined:
- bd gate discover still refuses over HTTP in findPendingGates (minor):
  the synthesis' first remedy is no change in this PR, and the file is
  not in this PR's diff. Finalize files the follow-up to move it onto
  ListGates.
- the proxied listing is not pinned on its own (nit): it calls the same
  listOpenGatesForCheck as the direct route (gate_proxied_server.go:106),
  and the synthesis calls accepting the gap defensible.
- listOpenGatesForCheck/listOpenGatesWith naming (nit): the remedy is
  "leave as is".

Verified:
- git diff --check: clean.
- bazel build --config=nogo -- //cmd/bd:all on the edited config: 16
  targets, exit 0.
- Probe, then reverted: a types.IssueFilter literal in gate.go and a
  types.WorkFilter literal in gate_proxied_server.go each failed nogo
  with forbidigo's "do not write a filter here", so the hole is shut.
- git merge-tree against the heads of all 102 other open PRs that touch
  CHANGELOG.md or .golangci.yml (today's census): this commit changes
  no conflict count in either file. 43 PR/file pairs already conflict
  with or without it.

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>
2026-10-16 09:20:24 +00:00

100 lines
3.9 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, 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", 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)
}
}