beads/cmd/bd/query_limit_policy_test.go
Harry Miller 0e317c4fc0
fix(query): apply bd list's piped-output limit policy to bd query (#6229) (#6744)
* fix(query): apply bd list's piped-output limit policy to bd query (#6229)

Root cause: bd list resolves an unflagged --limit through a policy that
treats piped stdout as unlimited (GH#4094), but bd query read its --limit
flag straight through, so its default of workapi.DefaultQueryLimit (50)
applied to piped output too. `bd query '...' | consumer` silently dropped
every row past 50; the truncation hint did not fire either, because it only
prints when stderr is a terminal.

Fix: factor the unflagged tail of bd list's policy into unflaggedLimit /
resolveUnflaggedLimit in list_input.go and call it from both commands, so
they cannot drift. bd list's switch is unchanged in behavior; bd query's
change is a single localized branch in gatherQueryInput.

Precedence for bd query, mirroring bd list:
  1. explicit --limit (including 0) wins
  2. piped stdout -> 0 (unlimited)
  3. agent mode on a terminal -> 20
  4. otherwise the 50 default
bd list keeps its own --all and list.limit branches ahead of step 2; bd
query has neither (its --all means "include closed", and there is no
query.limit config key), so none was invented.

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* review: document the query path's absent defensive cap; unstale a reachability comment

Maintainer review fixes for PR #6744 (GH#6229). Both are comment-only; no
behavior changes.

issueops.QueryRequest.Limit now states that nothing backstops it. ListRequest
pairs its Limit with MaxRows, a circuit breaker for an oversized answer, and
neither knob that sets that one (--max-rows, BEADS_MAX_ROWS) is read anywhere
on the query path, so an unlimited query is unbounded in the plain sense. That
was already reachable via `--limit 0`; this PR makes a piped `bd query` reach
it without asking, which is what makes it worth writing down. Recording the
absence is the documented half of the reviewers' choice — threading a MaxRows
twin onto QueryRequest stays available as a follow-up.

unionCountsGoSideSortKeepsTheRightSubset's comment justified its case with
"DefaultQueryLimit means a limit is always set", which this PR falsified for
the CLI half: an unflagged piped `bd query '<expr>' --sort id` now arrives on
the IsGoSideSort path with Limit 0 and no bound. The HTTP surface still brings
a bound, because an absent `limit` parameter resolves to DefaultQueryLimit, so
the clause is requalified rather than dropped. The assertions were never
affected — they pass an explicit Limit: 5.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
2026-09-26 02:50:09 -07:00

95 lines
3.4 KiB
Go

package main
import (
"testing"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/ui"
"github.com/steveyegge/beads/internal/workapi"
)
// newQueryLimitCommand registers the flags gatherQueryInput reads, with
// queryCmd's own --limit default.
func newQueryLimitCommand(t *testing.T, args ...string) *cobra.Command {
t.Helper()
cmd := &cobra.Command{Use: "query"}
cmd.Flags().IntP("limit", "n", workapi.DefaultQueryLimit, "")
cmd.Flags().Int("offset", 0, "")
cmd.Flags().BoolP("all", "a", false, "")
cmd.Flags().Bool("long", false, "")
cmd.Flags().String("sort", "", "")
cmd.Flags().BoolP("reverse", "r", false, "")
cmd.Flags().Bool("parse-only", false, "")
if err := cmd.ParseFlags(args); err != nil {
t.Fatalf("parse %v: %v", args, err)
}
return cmd
}
// TestQueryLimitPolicyMatchesList pins GH#6229: `bd query` read --limit
// straight through, so `bd query ... | consumer` silently stopped at
// workapi.DefaultQueryLimit rows where `bd list | consumer` got every row
// (GH#4094). An unflagged query now resolves through the same policy as list.
func TestQueryLimitPolicyMatchesList(t *testing.T) {
if ui.IsTerminal() {
t.Skip("this test asserts the piped-stdout branch; go test's stdout is a pipe, but this run's is not")
}
for _, tc := range []struct {
name string
args []string
want int
}{
{"an explicit limit wins when piped", []string{"--limit", "7"}, 7},
{"an explicit default-sized limit is still honored when piped", []string{"--limit", "50"}, 50},
{"an explicit zero stays unlimited", []string{"--limit", "0"}, 0},
{"piped stdout is unlimited, not the query default", nil, 0},
// query's --all means "include closed", not "no limit"; piping is
// what lifts the cap here.
{"--all does not change the piped policy", []string{"--all"}, 0},
} {
t.Run(tc.name, func(t *testing.T) {
in, err := gatherQueryInput(newQueryLimitCommand(t, tc.args...), []string{"status=open"})
if err != nil {
t.Fatalf("gatherQueryInput(%v): %v", tc.args, err)
}
if in.limit != tc.want {
t.Errorf("limit = %d, want %d", in.limit, tc.want)
}
if in.request.Limit == nil {
t.Fatal("QueryRequest.Limit is nil; `bd query` resolves its own limit before the request exists")
}
if *in.request.Limit != tc.want {
t.Errorf("QueryRequest.Limit = %d, want %d", *in.request.Limit, tc.want)
}
})
}
}
// TestUnflaggedLimitBranches covers the terminal and agent-mode branches the
// piped `go test` process cannot reach through gatherQueryInput or
// gatherListInput, for both commands' defaults.
func TestUnflaggedLimitBranches(t *testing.T) {
for _, fallback := range []int{workapi.DefaultListLimit, workapi.DefaultQueryLimit} {
for _, tc := range []struct {
name string
terminal bool
agent bool
want int
}{
{"terminal gets the command default", true, false, fallback},
{"agent mode on a terminal is compact", true, true, agentModeLimit},
{"piped stdout is unlimited", false, false, 0},
// Piping outranks agent mode, as it always has for bd list.
{"piped stdout is unlimited in agent mode too", false, true, 0},
} {
if got := resolveUnflaggedLimit(fallback, tc.terminal, tc.agent); got != tc.want {
t.Errorf("%s: resolveUnflaggedLimit(%d, terminal=%v, agent=%v) = %d, want %d",
tc.name, fallback, tc.terminal, tc.agent, got, tc.want)
}
}
}
if agentModeLimit != 20 {
t.Errorf("agentModeLimit = %d, want 20 (bd list's historical agent-mode cap)", agentModeLimit)
}
}