beads/internal/httpapi/reads_test.go
Julian Knutsen 601be8a6bf
feat(list): bd list's remaining filters over http behind issues.list.filters (#7647)
* feat(list): bd list's remaining filters over http behind issues.list.filters

Over http every `bd list` filter beyond the created bounds and the title,
id and no-assignee scope refused (time bounds, priority range, text and external-ref
matches, spec, label pattern/regex, exclude-type, no-parent, no-labels,
empty-description, pinned/no-pinned, deferred, overdue, mol/wisp type).
The library entry already applied all of them: issueops.Reader.List via
workapi.BuildListFilter, shared by the CLI and the handler. This adds
only the wire.

- Server: GET /v0/beads/issues decodes 29 new optional parameters
  (listFilterParams, decode only; countIssues' names where it already
  spells the filter) into the existing ListRequest, behind one new
  behavior token, issues.list.filters. mol_type/wisp_type are closed
  enums refused by name (an empty value too). wire_revision stays 2;
  apigen and the wireshape golden are regenerated (additive only).
- Every RFC 3339 bound the listing and count decode is normalized to UTC,
  so an offset-bearing bound selects what its Z twin does on a zone-less
  timestamp column.
- workapi.BuildListFilter refuses PinnedFlag with NoPinnedFlag
  (ErrValidation, the CLI's own sentence) instead of letting PinnedFlag
  win; over http that is a 400 on no_pinned.
- Client: the encoder sends the members (encode.ListFiltersMembers, one
  list read by the encoder, the pre-dial gate and a spec-parity test).
  Against a server not advertising the token, httpReader.List refuses
  before dialing with ErrUnsupported naming issues.list.filters. The 29
  E-ListRequest ledger refusal rows retire; the ledger doc is updated;
  skew flag mappings added.
- Tests: handler decode/parity/enum/UTC/malformed/pinned-pair tests;
  encode member/gate/spec parity and bound encoding; per-member
  ride-behind-token and pre-dial skew refusal; two new Reader contract
  cases on every leg (dolt, embedded, uow, served): the eight time
  bounds at exact boundaries (strict, whole-second truncation, NULL never
  matches, +02:00 equals UTC) and one in/out row per remaining family;
  three served Reader cases unparked; TestE2E_ListFilterParityOverHTTP
  (served == embedded, masked server refuses naming the token, no dial);
  five corpus shapes marked pending until the reference server profile
  advertises the token.

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

* feat(list): exclude_status on listIssues, the 30th issues.list.filters member

issueops.ListRequest gains ExcludeStatus: statuses to leave out of a
listing, appended to the default exclusions (never re-admitting what they
hide), comma-separated or repeated, and validated against the workspace's
statuses by the same rule CountRequest.ExcludeStatus uses (an unknown name
is ErrValidation). The rule moves into one shared function,
workapi.ParseExcludeStatus, called by both builders. With AllFlag it reads
the active set without enumerating every status, so an http-backed caller
can list O(active) rows instead of the whole history.

- Server: listIssues decodes a repeatable `exclude_status` under the same
  issues.list.filters token (no new token); an unknown name is a 400 on
  exclude_status. apigen and the wireshape golden are regenerated
  (additive).
- Client: the encoder sends it and it joins encode.ListFiltersMembers, so
  the pre-dial gate asks for the token.
- It is refused together with ReadyFlag, like the other members the ready
  query cannot carry.
- No `bd list --exclude-status` flag yet (it would need the CLI docs
  regenerated); the member is reachable through the library and the wire.

Tests: builder unit test (append, AllFlag, comma split, unknown name);
handler decode and 400; encoder, round-trip and gate; Reader contract
cases on every leg (active set under AllFlag, repeated and comma values,
adds to the defaults, narrows an explicit Status, unknown name refused).

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

* fix(list): label_regex compile errors are 400s; review polish for list filters

- An invalid `label_regex` on GET /v0/beads/issues, /v0/beads/ready and
  /v0/beads/ready:count is now `400 invalid_argument` (`invalid_value` on
  `label_regex`) instead of `500 internal`. The database still compiles
  the pattern in its own dialect; a request that sent a regex and earned
  the engine's "regular expression is invalid" refusal is attributed to
  the parameter (failRegexReadErr). The same text on a request with no
  regex stays a server fault.
- mol_type and wisp_type refuse each other's vocabulary (tests pin
  `mol_type=heartbeat` and `wisp_type=swarm`), so the call-site
  allowlist, not just the spec enum, is covered.
- exclude_status with a workspace custom status: leg tests on the
  embedded store and over the served wire (accepted and applied; a name in
  no vocabulary refused). ReaderFixture cannot configure custom statuses,
  so these sit beside the count's twin rather than in the contract.
- The search e2e gains a positive arm: --desc-contains returns exactly the
  row whose description carries the token.
- Docs: `all` plus `exclude_status` keeps every status not named (pinned,
  hooked and custom done/frozen included unless named), not "the active
  set"; label_regex documents its 400. A corpus source line is made
  neutral; a test message says 30.

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

* fix(list): compose issues.list.filters with the --title-contains fold

#7631, now on main, folds `bd list --title-contains` onto listIssues
`title` and refuses the TitleContains left over from a --title and
--title-contains pair that differs, because the listing then had one
title parameter. This branch publishes `title_contains` behind
issues.list.filters, so that leftover now rides it beside `title` and
the server applies both. Neither half is dropped, and against a server
without the token the pre-dial gate refuses, naming issues.list.filters.

- encode.FoldTitleContains still runs first; its doc now says where a
  differing pair goes.
- Tests: the fold table expects `title` plus `title_contains` for a
  differing pair, including the ſ/s pair strings.EqualFold would match;
  the differing-pair refusal names issues.list.filters; the
  list_filters_test sweep pairs its TitleContains probe with a differing
  TitleSearch, since a lone one asks only for issues.list.title; the e2e
  differing-pair refusal becomes a listFilterShapes row that answers
  exactly child-a over http and embedded, and refuses before dialing
  without the token.
- The gc-polecat-closed-after corpus shape's pending reason also names
  issues.list.title. The CHANGELOG --title-contains entry no longer
  lists the differing pair and --closed-after as refused.

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

* build(bazel): auto-sync BUILD files

Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/39013611268
(`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy).
Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see
scripts/bazel-autofix-push.sh.

* fix(list): serve --priority-max in search parity, refuse the parent pair

The proxied tier's search parity table still expected a remote
`bd search --priority-max 1` to refuse, because listIssues could not
carry it. This branch serves priority_max behind issues.list.filters,
so the remote route answered and the dolt-race shard went red in
TestProxiedServerSearchRoutesAgree. The row now expects the answer on
every route. It was the last wireCan=false row; the branch stays, with
a comment, for the next flag listIssues cannot carry.

- ParentID with NoParent: a served listing now carries no_parent beside
  parent, and ANDing the two answered only the edge-less rows under the
  parent's dotted id. BuildListFilter refuses the pair with
  ErrValidation, in the sentence bd list and BuildCountFilter already
  use, so issueops.Reader.List refuses it on every backend and the
  listing answers 400 on no_parent. Pinned by a builder test, a reader
  conformance case (dolt, embedded, uow, served http) and two httpapi
  tests.
- POST /v0/beads/issues:claimNext shares the ready listing's
  label_regex but answered a pattern the database cannot compile with
  500. It now gives the listing's 400 invalid_value on label_regex,
  through the helper the listing's mapping is split into.
- The descendant walk's ledger rows said listIssues publishes no
  parameter for members it now serves behind issues.list.filters. They
  now say the walk does not send them yet, and that --flat or --json
  asks listIssues directly. The ExcludeStatus, Pinned and ExcludeTypes
  rows, the parentwalk.go header, the walk table's doc and two walk test
  comments are corrected the same way, and the ledger doc is
  regenerated. parentWalkRefusals notes that a member moved off the list
  must check issues.list.filters itself, because the walk dials past
  the listing's pre-dial gate.
- Spec: the listing's no_parent documents the pair's 400, the count's
  exclude_status no longer says it has no listing counterpart, and
  claimNext's label_regex documents its 400. types.gen.go is
  regenerated. Comment-only: countFilters, and readyScopeFields'
  ExcludeStatus entry.
- CHANGELOG and README say the default `bd list --parent` tree view
  does not carry the new filters yet; the label_regex entry names
  claimNext, and a Changed entry records the parent-pair refusal.

Second review pass: docs and comments only, no behavior change.

- The prose copies of the issues.list.filters member set now match
  ListFiltersMembers: CapIssuesListFilters and wire.CapListFilters name
  the status exclusion, and the spec's capabilities text names
  label-regex.
- Every time bound on the listing and the count goes through
  q.timestamp, which normalizes it to UTC, and sqlbuild compares it
  strictly at whole-second precision. listIssues' created_before and
  created_after, and countIssues' created, updated and closed bounds,
  now say so in the words the new listing bounds use. The count's
  updated bounds also say what they filter, and its closed bounds say
  that a NULL closed_at never matches. types.gen.go is regenerated.
- README and CHANGELOG: `--flat` or `--json` applies the filters to the
  parent's direct children, not its whole subtree.
- The 29 listRefusal rows this branch serves stay deleted rather than
  kept as retired rows, as #7550 deleted its TitleSearch, IDFilter and
  NoAssignee rows when it served them. The CHANGELOG now says the rows
  are removed, not retired.

Agent-Signature: claude-code-claude-opus-5-5-max 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 <338826946+gascity-eddie[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-10-17 11:21:18 +00:00

563 lines
25 KiB
Go

package httpapi
import (
"context"
"net/http"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/steveyegge/beads/internal/storage/domain"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/workapi"
)
// recordingIssues captures the filter the reader built, which is the only way
// to see what a handler actually asked storage for. Everything else about a
// read is observable from the response; the FILTER is not, and the filter is
// where the drift this design exists to prevent would live.
type recordingIssues struct {
domain.IssueUseCase
mu sync.Mutex
ready []types.WorkFilter
search []types.IssueFilter
// queries is the free-text argument of each SearchIssuesWithCounts call,
// index-aligned with search.
queries []string
items []*types.IssueWithCounts
// err, when set, is what both reads answer with after recording the
// filter: the storage-side failure a handler has to classify.
err error
}
func (f *recordingIssues) GetReadyWorkWithCounts(_ context.Context, filter types.WorkFilter) (domain.SearchCountsPage, error) {
f.mu.Lock()
f.ready = append(f.ready, filter)
f.mu.Unlock()
if f.err != nil {
return domain.SearchCountsPage{}, f.err
}
return domain.SearchCountsPage{Items: f.items}, nil
}
func (f *recordingIssues) SearchIssuesWithCounts(_ context.Context, query string, filter types.IssueFilter) (domain.SearchCountsPage, error) {
f.mu.Lock()
f.search = append(f.search, filter)
f.queries = append(f.queries, query)
f.mu.Unlock()
if f.err != nil {
return domain.SearchCountsPage{}, f.err
}
return domain.SearchCountsPage{Items: f.items}, nil
}
// The ready surface's defer-wake sweep reaches this before the read; nothing
// is deferred in the fixture, so it reports a no-op sweep.
func (f *recordingIssues) WakeExpiredDefers(context.Context) (issues, wisps int, err error) {
return 0, 0, nil
}
func (f *recordingIssues) readyFilters() []types.WorkFilter {
f.mu.Lock()
defer f.mu.Unlock()
return append([]types.WorkFilter(nil), f.ready...)
}
func (f *recordingIssues) searchQueries() []string {
f.mu.Lock()
defer f.mu.Unlock()
return append([]string(nil), f.queries...)
}
func (f *recordingIssues) searchFilters() []types.IssueFilter {
f.mu.Lock()
defer f.mu.Unlock()
return append([]types.IssueFilter(nil), f.search...)
}
// emptyConfig is the workspace vocabulary a bare fixture has: no custom
// statuses, no custom types, no infra overrides. The list reader loads it
// through the unit of work before it builds a filter, so a read test that
// drives the list path has to answer it.
type emptyConfig struct{ domain.ConfigUseCase }
func (emptyConfig) GetCustomStatuses(context.Context) ([]types.CustomStatus, error) { return nil, nil }
func (emptyConfig) GetCustomTypes(context.Context) ([]string, error) { return nil, nil }
func (emptyConfig) GetInfraTypes(context.Context) (map[string]bool, error) { return nil, nil }
func newReadServer(t *testing.T, cfg Config) (*testServer, *recordingIssues) {
t.Helper()
rec := &recordingIssues{}
cfg.Provider = &fakeProvider{issues: &fakeIssues{}, readIssues: rec, readConfig: emptyConfig{}}
return newTestServer(t, cfg), rec
}
// TestReadyForwardsAnExplicitSortPolicy is the guard on the one default that
// changes the item SET rather than just its order.
//
// The storage layer maps an EMPTY sort policy to hybrid. A handler that
// forwarded an absent `sort` as "" would therefore serve hybrid while the
// frozen document still read `default: priority` — and hybrid demotes older
// high-priority work, so as soon as `limit` truncates, the page contains
// DIFFERENT ISSUES from the ones `bd ready` shows. The document tests pin the
// document; only this pins the handler.
//
// The wanted value is the LITERAL, not readySortDefault. Comparing the filter
// against the same constant the handler read would pass for every value that
// constant could take, including hybrid — the assertion would say only that
// the handler forwards its own default, which is not the property at risk.
// TestDefaultsMatchCLIFlags ties that literal to `bd ready --sort`'s
// registered default and to the frozen document, so all three move together or
// one of them fails.
func TestReadyForwardsAnExplicitSortPolicy(t *testing.T) {
ts, rec := newReadServer(t, Config{})
if resp := ts.get(t, "/v0/beads/ready"); resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200", resp.StatusCode)
}
filters := rec.readyFilters()
if len(filters) != 1 {
t.Fatalf("%d ready queries, want 1", len(filters))
}
if got := filters[0].SortPolicy; got != types.SortPolicy("priority") {
t.Errorf("SortPolicy = %q, want \"priority\" — an empty policy is the storage layer's hybrid fallback, and hybrid re-SELECTS the page once the limit truncates",
got)
}
// The shared limit default reaches storage too: the document states the
// number, the CLI flag registers the same constant, and this is where the
// server proves it uses that constant rather than a literal of its own.
if got, want := filters[0].Limit, workapi.DefaultReadyLimit; got != want {
t.Errorf("Limit = %d, want the shared default %d", got, want)
}
}
// TestReadySortIsValidatedAgainstTheDocumentedEnum: an unrecognized policy is a
// 400 rather than a silent fallback, because a silent fallback would answer a
// question the client did not ask.
func TestReadySortIsValidatedAgainstTheDocumentedEnum(t *testing.T) {
ts, rec := newReadServer(t, Config{})
resp := ts.get(t, "/v0/beads/ready?sort=newest")
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", resp.StatusCode)
}
body := decodeBody(t, resp)
if body["code"] != string(CodeInvalidArgument) || body["param"] != "sort" || body["reason"] != string(ReasonInvalidValue) {
t.Errorf("body = %v, want invalid_argument on param sort with reason invalid_value", body)
}
if n := len(rec.readyFilters()); n != 0 {
t.Errorf("%d ready queries ran; a refused request must not reach storage", n)
}
}
// TestABuilderRefusalIsTheDocumentedBadRequest drives every row of
// invalidFilterParam end to end, through the real builders.
//
// The mapping is prose matching on this repository's own error strings, which
// is only safe while something fails when one of those strings is reworded.
// Nothing did: the builders' golden files record successful filters and never
// see a message, so `invalid issue type ` in particular was pinned nowhere and
// a reworded builder would have quietly demoted `?type=bogus` from 400
// invalid_value to 500 internal.
//
// metadata_field and has_metadata_key are here for a second reason: their keys
// used to be checked only in the SQL builder, whose error arrives wrapped in
// the storage method's name and therefore cannot be classified at all. The
// frozen document says an invalid key is a 400; it was a 500.
func TestABuilderRefusalIsTheDocumentedBadRequest(t *testing.T) {
for _, tc := range []struct {
name string
path string
param string
}{
{"an issue type outside the workspace vocabulary", "/v0/beads/issues?type=bogus", "type"},
{"a status that is not a status", "/v0/beads/issues?status=bogus", "status"},
{"a metadata field key the query layer cannot spell", "/v0/beads/issues?metadata_field=1bad=x", "metadata_field"},
{"a has-metadata key the query layer cannot spell", "/v0/beads/issues?has_metadata_key=1bad", "has_metadata_key"},
{"a metadata field key on the ready surface", "/v0/beads/ready?metadata_field=1bad=x", "metadata_field"},
{"a has-metadata key on the ready surface", "/v0/beads/ready?has_metadata_key=1bad", "has_metadata_key"},
{"a metadata field key on the count surface", "/v0/beads/issues:count?metadata_field=1bad=x", "metadata_field"},
{"a has-metadata key on the count surface", "/v0/beads/issues:count?has_metadata_key=1bad", "has_metadata_key"},
{"an unknown exclude-status name on the count surface", "/v0/beads/issues:count?exclude_status=bogus", "exclude_status"},
{"parent and no_parent together on the count surface", "/v0/beads/issues:count?parent=bd-1&no_parent=true", "no_parent"},
{"pinned and no_pinned together on the listing", "/v0/beads/issues?pinned=true&no_pinned=true", "no_pinned"},
{"parent and no_parent together on the listing", "/v0/beads/issues?parent=bd-1&no_parent=true", "no_parent"},
{"an unknown exclude-status name on the listing", "/v0/beads/issues?exclude_status=bogus", "exclude_status"},
} {
t.Run(tc.name, func(t *testing.T) {
ts, _ := newReadServer(t, Config{})
resp := ts.get(t, tc.path)
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("GET %s: status = %d, want 400", tc.path, resp.StatusCode)
}
body := decodeBody(t, resp)
if body["code"] != string(CodeInvalidArgument) || body["param"] != tc.param || body["reason"] != string(ReasonInvalidValue) {
t.Errorf("GET %s: body = %v, want invalid_argument on param %s with reason invalid_value", tc.path, body, tc.param)
}
// The detail reflects the caller's own input back, which is what a
// 4xx detail is for — and is the half that stops being true if the
// builder's message moves.
if detail, _ := body["detail"].(string); detail == "" {
t.Errorf("GET %s: no detail; the builder's own message is the detail", tc.path)
}
})
}
}
// TestAnInvalidMetadataKeyNeverReachesStorage: the refusal is worth having only
// if it happens before the query runs. A key that reached the SQL builder came
// back as a 500 with the storage method's name wrapped around it.
func TestAnInvalidMetadataKeyNeverReachesStorage(t *testing.T) {
ts, rec := newReadServer(t, Config{})
if resp := ts.get(t, "/v0/beads/issues?metadata_field=1bad=x"); resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", resp.StatusCode)
}
if n := len(rec.searchFilters()); n != 0 {
t.Errorf("%d list queries ran; the refusal must happen before the database is touched", n)
}
if n := len(rec.readyFilters()); n != 0 {
t.Errorf("%d ready queries ran; the refusal must happen before the database is touched", n)
}
}
// TestUnknownReadParameterIsRefusedByName: silently ignoring an unrecognized
// FILTER parameter WIDENS the result set, so a client one version ahead
// receives rows it believes it filtered out.
func TestUnknownReadParameterIsRefusedByName(t *testing.T) {
for _, path := range []string{"/v0/beads/ready?bogus=1", "/v0/beads/issues?bogus=1", "/v0/beads/issues/bd-1?bogus=1"} {
ts, _ := newReadServer(t, Config{})
resp := ts.get(t, path)
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("GET %s: status = %d, want 400", path, resp.StatusCode)
continue
}
body := decodeBody(t, resp)
if body["param"] != "bogus" || body["reason"] != string(ReasonUnknownParameter) {
t.Errorf("GET %s: body = %v, want param=bogus reason=unknown_parameter", path, body)
}
}
}
// TestAMalformedKnownParameterIsNotReportedAsVersionSkew: the two 400 reasons
// carry opposite client recoveries — unknown_parameter says "this server is
// older than you think, degrade", invalid_value says "send something else" —
// so a bad value on a parameter the server DOES know must not be reported as
// the former just because the request also has to be checked for the latter.
func TestAMalformedKnownParameterIsNotReportedAsVersionSkew(t *testing.T) {
ts, _ := newReadServer(t, Config{})
resp := ts.get(t, "/v0/beads/ready?limit=-1&bogus=1")
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", resp.StatusCode)
}
body := decodeBody(t, resp)
if body["param"] != "limit" || body["reason"] != string(ReasonInvalidValue) {
t.Errorf("body = %v, want the malformed known parameter reported first", body)
}
}
// TestUnlimitedReadsAreLoopbackOnly pins the one mode-dependent refusal: an
// unlimited read buffers the whole active set and its JSON encoding inside one
// shared process, which must not be reachable by arbitrary network peers.
func TestUnlimitedReadsAreLoopbackOnly(t *testing.T) {
t.Run("loopback allows it", func(t *testing.T) {
ts, rec := newReadServer(t, Config{})
if resp := ts.get(t, "/v0/beads/ready?limit=0"); resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200", resp.StatusCode)
}
filters := rec.readyFilters()
if len(filters) != 1 || filters[0].Limit != 0 {
t.Errorf("filters = %v, want one query with Limit 0 (unlimited passes through untouched)", filters)
}
})
t.Run("a non-loopback bind refuses it", func(t *testing.T) {
// InsecureNoAuth is what --allow-non-loopback now requires when no
// token file is configured. It is the posture under test here — the
// refusal being pinned is about the BIND, not about the credential.
ts, rec := newReadServer(t, Config{Addr: "127.0.0.1:0", AllowNonLoopback: true, InsecureNoAuth: true})
resp := ts.get(t, "/v0/beads/ready?limit=0")
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", resp.StatusCode)
}
body := decodeBody(t, resp)
if body["param"] != "limit" || body["reason"] != string(ReasonInvalidValue) {
t.Errorf("body = %v, want invalid_argument on param limit", body)
}
if n := len(rec.readyFilters()); n != 0 {
t.Errorf("%d queries ran; the refusal must happen before the database is touched", n)
}
})
}
// TestGetIssueRefusesAnImpossibleIDFromTheEdge: an id longer than the column,
// or one carrying a control character a percent-escape decoded to, names no row
// that can exist. Answering it from the edge costs nothing and tells the caller
// exactly what a read would have — and the SAME 404 a real miss gets, so a
// caller cannot map the server's notion of a well-formed id.
func TestGetIssueRefusesAnImpossibleIDFromTheEdge(t *testing.T) {
long := ""
for range types.MaxFieldLen + 1 {
long += "x"
}
for _, id := range []string{long, "bd-%01"} {
ts, rec := newReadServer(t, Config{})
resp := ts.get(t, "/v0/beads/issues/"+id)
if resp.StatusCode != http.StatusNotFound {
t.Errorf("GET issue %q: status = %d, want 404", id, resp.StatusCode)
continue
}
if body := decodeBody(t, resp); body["code"] != string(CodeNotFound) {
t.Errorf("GET issue %q: code = %v, want not_found", id, body["code"])
}
if n := len(rec.readyFilters()); n != 0 {
t.Errorf("GET issue %q reached storage", id)
}
}
}
// TestCursorRoundTrips: the token is opaque and server-private, and the only
// thing that invalidates one is a change to the encoding.
//
// The fixture's CreatedAt has to be NON-ZERO for this test to test anything.
// decodeCursor rejects a zero instant — an empty position is one of its
// documented failure modes — so a fixture left at the zero time made the
// happy-path assertions unreachable and the test passed while asserting
// nothing, encodeCursor broken outright included.
func TestCursorRoundTrips(t *testing.T) {
created := time.Now().UTC().Truncate(time.Second)
items := []*types.IssueWithCounts{{Issue: &types.Issue{ID: "bd-7", CreatedAt: created, Priority: 2}}}
// Both served orders, because a round trip that held for one and lost a
// member of the other's position would still page — into the wrong rows.
for _, order := range []listOrder{orderCreated, orderPriority} {
token := cursorFor(items, order)
if token == "" {
t.Fatalf("%s: cursorFor returned no token for a nonempty page", order)
}
pos, ok := decodeCursor(token, order)
if !ok {
t.Fatalf("%s: a token this server minted did not decode: %q", order, token)
}
if pos.ID != "bd-7" {
t.Errorf("%s: decoded id = %q, want bd-7", order, pos.ID)
}
if !pos.CreatedAt.Equal(created) {
t.Errorf("%s: decoded created_at = %s, want %s — the position is a keyset predicate, so a lossy instant skips or repeats rows", order, pos.CreatedAt, created)
}
if order == orderPriority {
if pos.Priority == nil {
t.Errorf("%s: decoded no priority; this order's position is (priority, created_at, id)", order)
} else if *pos.Priority != 2 {
t.Errorf("%s: decoded priority = %d, want 2", order, *pos.Priority)
}
} else if pos.Priority != nil {
t.Errorf("%s: decoded a priority (%d) it has no place for", order, *pos.Priority)
}
}
for _, bad := range []string{"", "v0.abc", "v1.!!!", "v1.", "v2.!!!", "v2.", "v3.abc", "not-a-cursor"} {
if _, ok := decodeCursor(bad, orderCreated); ok {
t.Errorf("decodeCursor(%q) succeeded; every unreadable token is the same client situation", bad)
}
}
}
// TestAReadRouteTimesTheUnitsOfWorkItsReaderOpens pins the one property of
// timedProvider.IssueReader that is invisible from reading it, and it is here
// because a reviewer proposed removing it: build the reader OVER THE WRAPPER,
// so every unit of work it opens goes through the wrapper's NewUOW and lands in
// this request's uow_ms.
//
// The tempting edit is `p.inner.IssueReader()` — "add the layer by recursion,
// the way every other decorator does". Recursion is right for a decorator whose
// layer is on the RESULT: telemetry's accessor recurses because it wraps the
// reader it gets back. This decorator's layer is on NewUOW, which only a reader
// holding THIS wrapper can reach, so recursion would hand back a reader bound
// to the untimed provider. It compiles, every other test still passes, and
// every read route reports uow_ms=0.000 from then on. So this asserts the
// NUMBER on the request line, which is the only place the difference shows.
func TestAReadRouteTimesTheUnitsOfWorkItsReaderOpens(t *testing.T) {
// A provider that takes a measurable moment to hand out a unit of work, so
// the field is checkable rather than a rounded zero either way.
provider := &fakeProvider{
issues: &fakeIssues{},
readIssues: &recordingIssues{},
readConfig: emptyConfig{},
delay: 5 * time.Millisecond,
}
ts := newTestServer(t, Config{Provider: provider})
if resp := ts.get(t, "/v0/beads/ready"); resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200", resp.StatusCode)
}
// Two units of work, both through the wrapper: the ready surface's
// defer-wake sweep, then the read span. A reader bound to the untimed
// provider would report zero.
if n := len(provider.openedUOWs()); n != 2 {
t.Fatalf("opened %d units of work, want 2 (defer-wake sweep + read span)", n)
}
line := findLogLine(t, ts.stderr.String(), "op="+OpListReadyWork)
if !strings.Contains(line, "uow_ms=") {
t.Fatalf("read request line has no uow_ms field:\n%s", line)
}
if strings.Contains(line, "uow_ms=0.000") {
t.Errorf("read request line reports no unit-of-work time though the provider took 5ms; the reader is bound to the untimed provider:\n%s", line)
}
}
// TestListForwardsTheEphemeralPlaneParameter is the handler half of
// ListRequest.IncludeEphemeral. The parameter decides which TABLES the query
// reads, and nothing in the response distinguishes a merged page from a durable
// one when the fixture holds no wisps — so the recorded filter is the only place
// a dropped plumb-through is visible, which is exactly why the filter is
// recorded at all.
//
// The third case is the one that stops the parameter from being wired to the
// wrong knob: `include_infra` also admits the plane, so a handler that mapped
// `include_ephemeral` onto IncludeInfra would pass the first two and silently
// widen the answer by four issue types.
func TestListForwardsTheEphemeralPlaneParameter(t *testing.T) {
for _, tc := range []struct {
name string
path string
wantSkipWisps bool
wantInfraType bool
}{
{"absent leaves the durable listing alone", "/v0/beads/issues", true, false},
{"include_ephemeral admits the plane and takes no type exclusion off", "/v0/beads/issues?include_ephemeral=true", false, false},
{"include_infra is the wider one", "/v0/beads/issues?include_infra=true", false, true},
} {
t.Run(tc.name, func(t *testing.T) {
ts, rec := newReadServer(t, Config{})
if resp := ts.get(t, tc.path); resp.StatusCode != http.StatusOK {
t.Fatalf("GET %s: status = %d, want 200", tc.path, resp.StatusCode)
}
filters := rec.searchFilters()
if len(filters) != 1 {
t.Fatalf("%d list queries, want 1", len(filters))
}
if got := filters[0].SkipWisps; got != tc.wantSkipWisps {
t.Errorf("SkipWisps = %v, want %v", got, tc.wantSkipWisps)
}
infraExcluded := false
for _, excluded := range filters[0].ExcludeTypes {
if excluded == types.IssueType("message") {
infraExcluded = true
}
}
if infraExcluded == tc.wantInfraType {
t.Errorf("ExcludeTypes = %v; the infra types are a TYPE exclusion and only include_infra takes it off", filters[0].ExcludeTypes)
}
})
}
}
// TestListForwardsTheSearchText is the handler half of ListRequest.Query
// (issues.list.search): `q` reaches the storage seam as SearchIssues' free-text
// argument — not as TitleSearch, which matches titles only — and its absence
// sends no text at all.
func TestListForwardsTheSearchText(t *testing.T) {
for _, tc := range []struct {
name, path, want string
}{
{"absent sends no text", "/v0/beads/issues", ""},
{"q is the search text", "/v0/beads/issues?q=login+bug&all=true", "login bug"},
} {
t.Run(tc.name, func(t *testing.T) {
ts, rec := newReadServer(t, Config{})
if resp := ts.get(t, tc.path); resp.StatusCode != http.StatusOK {
t.Fatalf("GET %s: status = %d, want 200", tc.path, resp.StatusCode)
}
queries := rec.searchQueries()
if len(queries) != 1 || queries[0] != tc.want {
t.Fatalf("search texts = %q, want [%q]", queries, tc.want)
}
if f := rec.searchFilters()[0]; f.TitleSearch != "" || f.TitleContains != "" {
t.Errorf("q leaked into a title-only filter: TitleSearch=%q TitleContains=%q", f.TitleSearch, f.TitleContains)
}
})
}
}
// TestTheEphemeralPlaneParameterIsTheSkewSignal pins the version-skew half.
//
// A client that sends `include_ephemeral` to a server built before this
// parameter existed gets 400 `unknown_parameter` — the designed signal that the
// server is older than the client thinks, whose recovery is to degrade rather
// than retry. That behavior is the shared decoder's, so what has to hold HERE
// is the other side of it: this build must CONSUME the parameter, because a
// handler that stopped reading it would answer the durable set and report the
// request as unknown-parameter-free, which is indistinguishable from success.
func TestTheEphemeralPlaneParameterIsTheSkewSignal(t *testing.T) {
ts, _ := newReadServer(t, Config{})
resp := ts.get(t, "/v0/beads/issues?include_ephemeral=true")
if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200: a parameter this build serves must not read as skew", resp.StatusCode)
}
// The neighbouring spelling a client might guess is NOT quietly accepted:
// an unknown parameter is refused by name, which is what makes the 200
// above mean something.
resp = ts.get(t, "/v0/beads/issues?include_ephemerals=true")
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", resp.StatusCode)
}
body := decodeBody(t, resp)
if body["param"] != "include_ephemerals" || body["reason"] != string(ReasonUnknownParameter) {
t.Errorf("body = %v, want param=include_ephemerals reason=unknown_parameter", body)
}
}
// TestListForwardsTheScopeFilters is the handler half of S15b: listIssues'
// `no_assignee` (issues.list.noAssignee), `title` (issues.list.title) and `id`
// (issues.list.ids, repeatable) reach the storage seam through the Reader role as the
// ListRequest members `bd list --no-assignee`, `--title` and `--id` set, and
// their absence narrows nothing.
func TestListForwardsTheScopeFilters(t *testing.T) {
for _, tc := range []struct {
name, path string
check func(t *testing.T, f types.IssueFilter)
}{
{"absent narrows nothing", "/v0/beads/issues", func(t *testing.T, f types.IssueFilter) {
if f.NoAssignee || f.TitleSearch != "" || len(f.IDs) != 0 {
t.Errorf("filter = {NoAssignee:%v TitleSearch:%q IDs:%v}, want none set", f.NoAssignee, f.TitleSearch, f.IDs)
}
}},
{"no_assignee", "/v0/beads/issues?no_assignee=true&status=in_progress", func(t *testing.T, f types.IssueFilter) {
if !f.NoAssignee {
t.Error("no_assignee=true did not reach the filter")
}
}},
{"title", "/v0/beads/issues?title=Routed+open+task", func(t *testing.T, f types.IssueFilter) {
if f.TitleSearch != "Routed open task" {
t.Errorf("TitleSearch = %q, want %q", f.TitleSearch, "Routed open task")
}
if f.TitleContains != "" {
t.Errorf("title leaked into TitleContains = %q", f.TitleContains)
}
}},
{"id is repeatable, one id per value", "/v0/beads/issues?id=e2e-r1&id=e2e-a1&all=true", func(t *testing.T, f types.IssueFilter) {
if !slices.Equal(f.IDs, []string{"e2e-r1", "e2e-a1"}) {
t.Errorf("IDs = %v, want [e2e-r1 e2e-a1]", f.IDs)
}
}},
} {
t.Run(tc.name, func(t *testing.T) {
ts, rec := newReadServer(t, Config{})
if resp := ts.get(t, tc.path); resp.StatusCode != http.StatusOK {
t.Fatalf("GET %s: status = %d, want 200", tc.path, resp.StatusCode)
}
filters := rec.searchFilters()
if len(filters) != 1 {
t.Fatalf("search calls = %d, want 1", len(filters))
}
tc.check(t, filters[0])
})
}
}