beads/internal/httpclient/encode/oracle_test.go
Julian Knutsen dbaa4631ca
feat(roles): BatchGetter role and POST issues:batchGet (#7248)
* feat(roles): BatchGetter role and issues:batchGet route (S7)

Adds BatchGetter.GetMany(ids) -> {Issues, Missing}, a one-snapshot,
order-preserving, deduped, 1000-id-capped batch read, wired through all
three library legs (dolt, embedded dolt, uow) behind the existing
hook/telemetry decorator chain, plus the POST /v0/beads/issues:batchGet
HTTP route (spec, apigen, wire-shape golden, issues.batchGet capability
token). Conformance contract (RunBatchGetter*, 12 cases) passes on all
three legs against real backends. No HTTP client leg yet (waits on the
generator / PR #7247); left as a documented seam on BatchGetter's doc
comment.

CLI GetIssuesByIDs call sites are left unmigrated this slice and marked
with TODO(batchgetter) comments: every call site reads an unbounded id
list today, and GetMany's 1000-id cap has no chunking/fallback in any
caller yet, so swapping them in now would add a new, unhandled failure
mode to already-shipped CLI paths (export, import, molecule port,
ready-explain, wisp sweep).

Verified via three live-mutate guards against
internal/storage/issueops/batchget.go (disabling the cap check, dropping
the Missing append, and replacing ordered iteration with map iteration):
each turns the embedded-dolt contract red, confirming the cap, Missing
reporting, and order-preservation assertions actually catch a break.

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

* fix(roles): close S7 review gaps — revision on wire, BUILD sync, handler tests

Addresses the S7 Opus review's FIX-FIRST items on BatchGetter/issues:batchGet:

HIGH
- batchGetResponse now projects apigen.BatchGetIssue (types.BatchGetIssue +
  NewBatchGetIssue), carrying `revision` per issue the way IssueDetails does;
  spec, apigen and the wire-shape golden regenerated (additive, no
  wire_revision bump).
- make bazel-sync: the 12 files missing from BUILD.bazel srcs are back in
  sync across 9 BUILD.bazel files.
- New internal/httpapi/batch_get_test.go: success incl. revision, missing
  ids, empty arrays not null, unknown member refused, missing/null ids
  refused, query parameter refused, 1001 ids refused with the full request
  reaching the role, 1000 ids accepted.

MED
- RunBatchGetterAcceptsExactlyTheCap pins the cap boundary (> vs >=).
- RunBatchGetterHydratesLabels pins Labels survive projection.
- RunBatchGetterReadsOneSnapshot renamed to
  RunBatchGetterSharesOneReadStructurally with a doc that no longer
  overclaims atomicity; a real interleaving test needs a hook on the frozen
  roleFixtureKit, out of scope here.
- Doc comments across ExecuteGetMany, GetManyResult.Issues and the
  BatchGetter interface now say hydration is labels only.

LOW
- failBatchGetErr's doc no longer claims blank entries are "already caught".
- PR #7247 references kept.

Re-ran build/vet/gofmt/lint, make api-check, make bazel-sync(-check), the
full internal/httpapi suite, and all three conformance legs (dolt via the
pinned local server, embeddeddolt, uow) plus targeted cmd/bd serve tests —
all green. Live-mutate-guard reverified: cap >=, dropping Missing from the
response, and clearing Labels each now fail the relevant test.

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

* fix(roles): sync client vocabulary and repo gates with issues:batchGet

The S7 route landed on the server without the sibling tables that hold
the rest of the tree set-equal with the server's surface, so five
tests and two manifest gates went red on the PR head:

- httpclient/wire: mirror batchGetIssues into the client vocabulary,
  CLASSIFIED, not wired, the way the wire wave and #7250's
  issues.count.scope landed: OpBatchGetIssues, its issues.batchGet
  token on opCapability, its identity row, and the first-slice skew
  dimension 35 -> 36 (TestCapabilityTableMatchesTheServerRouteTable).
- httpclient/wire: BatchGetIssue is the first golden.json schema with
  a `revision` member that needs no legacy-integer tolerance — the
  route postdates #6053 and types.BatchGetIssue declares Revision a
  string, and a pre-#6053 server never advertises issues.batchGet, so
  Preflight refuses before any body is decoded. Exempt it with that
  reason in a new revisionSchemaExempt table rather than adding an
  issues[] rewrite no legacy server can reach; the gate refuses an
  entry that is both covered and exempt, a stale exemption, and an
  empty reason.
- httpclient/encode: give the oracle's role set a BatchGetter stub —
  httpapi.Listen requires the whole set, so all three oracle tests
  failed to bind.
- storage/uow: exempt IssueUseCase.GetMany from the notifying-parity
  partition as a read, the reason hook_batch_getter.go gives.
- shard manifest: place TestBatchGetterContract in the 15-shard
  embedded storage block (generator --write, incremental).

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

* test(roles): pin the BatchGetter pure tier and the spec ids caps

Review fixes, iteration 1. The review approved with no blocker or major
finding and three minors, and this commit takes all three. It changes
tests and one comment only, and every new assertion was mutation-probed
with go test -overlay.

- storage/issueops: add batchget_test.go, the database-free tier that
  FinishGetMany's doc already claimed ("pinned without a database") but
  that no test supplied. It pins:
  - the cap is checked before the blank scan (an over-cap request with
    a blank at position 0 answers *TooManyIDsError);
  - the bound is inclusive;
  - dedupe keeps the first mention, does not trim, and never writes
    through the caller's slice;
  - Issues and Missing both follow request order;
  - both slices are never nil;
  - ExecuteGetMany refuses one id repeated past the cap before any
    query (a sqlmock with no expectations).
  Ten mutants were each killed by exactly their intended test, with the
  rest of the package green: last-mention, sort, trim, in-place dedupe,
  blank-before-cap, >=, nil Missing, nil Issues, dedupe-before-validate
  and validate-after-query.
- httpapi: TestSpecDefaultsMatchSharedConstants now checks ids.maxItems
  against the enforced bound on the two request bodies that carry an
  `ids` list: batchGetIssues against issueops.MaxGetManyIDs, and
  deleteIssues against maxDeleteIDs. Each body is reached through its
  operation's own requestBody $ref. Setting either constant to 999 turns
  only this test red. The spec's other list caps (batch
  create/close/apply items, dependency edge lists) stay ungated, as
  before.
- cmd/bd/serve.go: the batchGetter field comment said its accessor
  "does NOT recurse through the hook decorator". HookFiringStore.
  BatchGetter does recurse to the inner getter; it just doesn't wrap
  the result. The comment now says that.

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 Sonnet 5 <noreply@anthropic.com>
2026-10-05 12:09:18 -07:00

283 lines
9.8 KiB
Go

// Contributed by gascity from bd-enterprise (internal/enterprise/httpstore/encode/oracle_test.go@49d1df2f6)
// to OSS beads under the MIT license.
package encode
import (
"context"
"io"
"net/http"
"net/url"
"sync"
"testing"
"github.com/steveyegge/beads/internal/httpapi"
"github.com/steveyegge/beads/issueops"
"github.com/steveyegge/beads/memoryops"
)
// The oracle: a real bd serve, bound to loopback, answering from roles that
// record the request the PRODUCTION decoder built and nothing else.
//
// A real server rather than a call into the decoding helpers, because the
// helpers are unexported and — more to the point — because the properties this
// gate needs are the SERVER's, not a function's: the route table has to accept
// the path, the Host policy has to admit the request, and the
// unknown-parameter rule has to run. A parameter this client sends that the
// operation does not publish is a 400 from that rule, and the 400 is half the
// assertion.
//
// The WHOLE role set because httpapi.Listen requires it rather than a partial
// one — a Config missing one role would bind, answer every other route, and fail
// that one with a nil dereference on a live server. Four of them record and the
// rest are interface stubs this surface never dials, and a stub that IS dialed
// by accident panics rather than answering, which is the loud failure a nil
// would not have been.
type oracle struct {
base string
mu sync.Mutex
ready issueops.ReadyRequest
list issueops.ListRequest
get issueops.GetRequest
count issueops.ReadyRequest
query issueops.QueryRequest
issueCount issueops.CountRequest
issueGroup issueops.CountByGroupRequest
visited map[string]bool
}
func startOracle(t *testing.T) *oracle {
t.Helper()
o := &oracle{visited: map[string]bool{}}
srv, err := httpapi.Listen(httpapi.Config{
Addr: "127.0.0.1:0",
Stdout: io.Discard,
Stderr: io.Discard,
Reader: recordingReader{o},
ReadyCounter: recordingCounter{o},
Querier: recordingQuerier{o},
Counter: recordingIssueCounter{o},
Claimer: stubClaimer{},
ReadyClaimer: stubReadyClaimer{},
Releaser: stubReleaser{},
Lifecycle: stubLifecycle{},
Settings: stubSettings{},
Stats: stubStats{},
CycleDetector: stubCycles{},
EdgeReader: stubEdges{},
GraphCounter: stubGraphCounter{},
BatchGetter: stubBatchGetter{},
Relations: stubRelations{},
Commenter: stubCommenter{},
BlockingAnnotator: stubBlocking{},
TreeWalker: stubTree{},
Sweeper: stubSweeper{},
Deleter: stubDeleter{},
BatchCreator: stubBatchCreator{},
BatchCloser: stubBatchCloser{},
DependencyEditor: stubDependencyEditor{},
MetadataCAS: stubMetadataCAS{},
BatchApplier: stubBatchApplier{},
Memories: stubMemories{},
})
if err != nil {
t.Fatalf("bind the oracle: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { defer close(done); _ = srv.Serve(ctx) }()
t.Cleanup(func() { cancel(); <-done })
o.base = "http://" + srv.Addr()
return o
}
func (o *oracle) reset() {
o.mu.Lock()
defer o.mu.Unlock()
o.ready, o.list, o.get, o.count, o.query = issueops.ReadyRequest{}, issueops.ListRequest{}, issueops.GetRequest{}, issueops.ReadyRequest{}, issueops.QueryRequest{}
o.issueCount, o.issueGroup = issueops.CountRequest{}, issueops.CountByGroupRequest{}
o.visited = map[string]bool{}
}
// dial issues the encoded request against the operation's route and reports the
// status.
func (o *oracle) dial(t *testing.T, op Op, encoded Encoded) int {
t.Helper()
var path string
switch op {
case OpListReadyWork:
path = "/v0/beads/ready"
case OpCountReadyWork:
path = "/v0/beads/ready:count"
case OpListIssues:
path = "/v0/beads/issues"
case OpQueryIssues:
path = "/v0/beads/issues:query"
case OpCountIssues:
path = "/v0/beads/issues:count"
case OpGetIssue:
if len(encoded.PathIDs) != 1 {
t.Fatalf("getIssue needs exactly one path id, got %v", encoded.PathIDs)
}
path = "/v0/beads/issues/" + url.PathEscape(encoded.PathIDs[0])
default:
t.Fatalf("no route for operation %q", op)
}
target := o.base + path
if query := encoded.Params.Encode(); query != "" {
target += "?" + query
}
resp, err := http.Get(target) //nolint:gosec // a loopback oracle bound by this test
if err != nil {
t.Fatalf("dial %s: %v", target, err)
}
defer func() { _ = resp.Body.Close() }()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Logf("%s -> %d %s", target, resp.StatusCode, body)
}
return resp.StatusCode
}
// captured returns the request the server's decoder built for the operation.
//
// It keys on the SHAPE as well as the operation, because countIssues answers
// two of them on one route: `group_by` picks between the scalar method and the
// bucketed one, and the two roles are handed different request types. Reading
// the scalar one back for a grouped case would compare the caller's dimension
// against a request that has nowhere to hold it.
func (o *oracle) captured(t *testing.T, op Op, shape string) any {
t.Helper()
o.mu.Lock()
defer o.mu.Unlock()
if !o.visited[visitKey(op, shape)] {
t.Fatalf("the %s/%s handler never reached its role; the request was refused before decoding", op, shape)
}
switch {
case op == OpListReadyWork:
return o.ready
case op == OpCountReadyWork:
return o.count
case op == OpListIssues:
return o.list
case op == OpQueryIssues:
return o.query
case op == OpCountIssues && shape == "byGroup":
return o.issueGroup
case op == OpCountIssues:
return o.issueCount
case op == OpGetIssue:
return o.get
}
t.Fatalf("nothing captured for operation %q shape %q", op, shape)
return nil
}
// visitKey names the SERVER-side arm a client shape reaches.
//
// A client shape is usually invisible from here — the two bridges map a legacy
// filter onto an operation the server answers with one handler and one role
// method, so `workFilterBridge` and the primary listing are the same arm. The
// count is the exception the parameter carries: `group_by` picks between
// Counter.Count and Counter.CountByGroup, which are two methods handed two
// request types, so that one shape gets a key of its own.
func visitKey(op Op, shape string) string {
if op == OpCountIssues && shape == "byGroup" {
return string(op) + "/byGroup"
}
return string(op)
}
type recordingReader struct{ o *oracle }
func (r recordingReader) Ready(_ context.Context, req issueops.ReadyRequest) (issueops.IssuePage, error) {
r.o.mu.Lock()
defer r.o.mu.Unlock()
r.o.ready, r.o.visited[visitKey(OpListReadyWork, "")] = req, true
return issueops.IssuePage{Items: []*issueops.IssueWithCounts{}}, nil
}
func (r recordingReader) List(_ context.Context, req issueops.ListRequest) (issueops.IssuePage, error) {
r.o.mu.Lock()
defer r.o.mu.Unlock()
r.o.list, r.o.visited[visitKey(OpListIssues, "")] = req, true
return issueops.IssuePage{Items: []*issueops.IssueWithCounts{}}, nil
}
func (r recordingReader) Get(_ context.Context, req issueops.GetRequest) (*issueops.IssueDetails, error) {
r.o.mu.Lock()
defer r.o.mu.Unlock()
r.o.get, r.o.visited[visitKey(OpGetIssue, "")] = req, true
return &issueops.IssueDetails{}, nil
}
type recordingCounter struct{ o *oracle }
func (c recordingCounter) CountReady(_ context.Context, req issueops.ReadyRequest) (issueops.ReadyCountResult, error) {
c.o.mu.Lock()
defer c.o.mu.Unlock()
c.o.count, c.o.visited[visitKey(OpCountReadyWork, "")] = req, true
return issueops.ReadyCountResult{}, nil
}
type recordingQuerier struct{ o *oracle }
func (q recordingQuerier) Query(_ context.Context, req issueops.QueryRequest) (issueops.IssuePage, error) {
q.o.mu.Lock()
defer q.o.mu.Unlock()
q.o.query, q.o.visited[visitKey(OpQueryIssues, "")] = req, true
return issueops.IssuePage{Items: []*issueops.IssueWithCounts{}}, nil
}
type recordingIssueCounter struct{ o *oracle }
func (c recordingIssueCounter) Count(_ context.Context, req issueops.CountRequest) (issueops.CountResult, error) {
c.o.mu.Lock()
defer c.o.mu.Unlock()
c.o.issueCount, c.o.visited[visitKey(OpCountIssues, "")] = req, true
return issueops.CountResult{}, nil
}
func (c recordingIssueCounter) CountByGroup(_ context.Context, req issueops.CountByGroupRequest) (issueops.CountByGroupResult, error) {
c.o.mu.Lock()
defer c.o.mu.Unlock()
c.o.issueGroup, c.o.visited[visitKey(OpCountIssues, "byGroup")] = req, true
return issueops.CountByGroupResult{Groups: map[string]int{}}, nil
}
// The roles this gate never dials. Each embeds its interface, so the
// Config is complete and a call that should not happen panics on the nil
// embedded value instead of answering something plausible.
type (
stubClaimer struct{ issueops.Claimer }
stubReadyClaimer struct{ issueops.ReadyClaimer }
stubReleaser struct{ issueops.Releaser }
stubMetadataCAS struct{ issueops.MetadataCAS }
stubBatchApplier struct{ issueops.BatchApplier }
stubLifecycle struct{ issueops.Lifecycle }
stubSettings struct{ issueops.WorkspaceConfig }
stubStats struct{ issueops.StatsReporter }
stubCycles struct{ issueops.CycleDetector }
stubEdges struct{ issueops.EdgeReader }
stubGraphCounter struct{ issueops.GraphCounter }
stubBatchGetter struct{ issueops.BatchGetter }
stubRelations struct{ issueops.Relations }
stubCommenter struct{ issueops.Commenter }
stubBlocking struct{ issueops.BlockingAnnotator }
stubTree struct{ issueops.TreeWalker }
stubSweeper struct{ issueops.Sweeper }
stubDeleter struct{ issueops.Deleter }
stubBatchCreator struct{ issueops.BatchCreator }
stubBatchCloser struct{ issueops.BatchCloser }
stubDependencyEditor struct{ issueops.DependencyEditor }
stubMemories struct{ memoryops.Memories }
)