beads/internal/workapi/storereader/reader_test.go
CI Bot de9fc833e2 refactor(workapi): move the store reader where a front door cannot reach it
`workapi.NewStoreReader` was exported and lived in internal/workapi, which 22
cmd/bd files already import for the builders. It takes storage.DoltStorage — an
interface a command already holds — so it was a one-line drop-in replacement for
`store.IssueReader()` from any front door, and the wrong one: every storage
decorator adds its layer in its OWN accessor (telemetry's reader-level spans, the
hook store's deliberate pass-through), so a command that constructed a reader
directly would get a bare unspanned one and the code would look entirely
ordinary. The old doc comment said "nothing does; the accessor is the door",
which is a promise, not a guard.

It has real callers, so it cannot simply be unexported: dolt.DoltStore and
embeddeddolt.EmbeddedDoltStore both need one shared body for their accessors,
and they are in different packages. So the body moves down to
internal/workapi/storereader instead, where those two are the only importers,
and the cmd-bd-reader-constructor depguard rule keeps cmd/bd out. Nothing about
internal/httpapi changes: depguard matches pkg by prefix, so
httpapi-transport-boundary already denied the subpackage along with its parent.

The two store packages' own NewIssueReader are unexported at the same time. They
had no caller but their own accessor, they are in internal/ so no out-of-tree
consumer can reach them, and leaving them exported would have left the same door
open one package over.

SEEN TO FAIL, on the golangci-lint version CI installs. `bd show --json` switched
from the accessor to the constructor — the exact bypass — compiles and vets
clean:

  $ golangci-lint run --config=.golangci.yml --build-tags=gms_pure_go ./...
  cmd/bd/show.go:15:2: import
  'github.com/steveyegge/beads/internal/workapi/storereader' is not allowed from
  list 'cmd-bd-reader-constructor': a command reaches the reader role through
  store.IssueReader(), never through the constructor ... (depguard)
  exit 1

ErrUnsupported.Op moves with the function, "NewStoreReader" -> "storereader.New",
so the Op still names the function that refused; its test moves with it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-31 20:04:09 +00:00

216 lines
7.8 KiB
Go

package storereader
import (
"context"
"errors"
"slices"
"testing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// The store-backed reader answers `bd show --json` today and nothing else: the
// CLI's paging commands still reach storage through the builders directly (see
// reader.go's boundary note), so Ready and List here run in production
// only once a front door moves onto the role. That is precisely why they need
// tests — the over-fetch and trim they inherited from cmd/bd/list.go would
// otherwise be unexercised until the day something depends on them.
// fakeReaderStore is a DoltStorage whose only real methods are the five the
// reader calls. It records the filter it was handed, which is the only way to
// see the over-fetch: everything else about a page is visible in the result,
// and the +1 probe row is not.
type fakeReaderStore struct {
storage.DoltStorage
rows []*types.IssueWithCounts
readyFilters []types.WorkFilter
searchFilters []types.IssueFilter
}
func (s *fakeReaderStore) GetReadyWorkWithCounts(_ context.Context, f types.WorkFilter) ([]*types.IssueWithCounts, error) {
s.readyFilters = append(s.readyFilters, f)
return s.serve(f.Limit), nil
}
func (s *fakeReaderStore) SearchIssuesWithCounts(_ context.Context, _ string, f types.IssueFilter) ([]*types.IssueWithCounts, error) {
s.searchFilters = append(s.searchFilters, f)
return s.serve(f.Limit), nil
}
// serve honors the filter's row limit the way a real query would, so the
// over-fetch is what actually produces the extra row the trim removes.
func (s *fakeReaderStore) serve(limit int) []*types.IssueWithCounts {
if limit > 0 && len(s.rows) > limit {
return slices.Clone(s.rows[:limit])
}
return slices.Clone(s.rows)
}
func (s *fakeReaderStore) GetCustomStatusesDetailed(context.Context) ([]types.CustomStatus, error) {
return nil, nil
}
func (s *fakeReaderStore) GetCustomTypes(context.Context) ([]string, error) { return nil, nil }
func (s *fakeReaderStore) GetInfraTypes(context.Context) map[string]bool { return nil }
func readerFixture(n int) []*types.IssueWithCounts {
out := make([]*types.IssueWithCounts, 0, n)
for i := 1; i <= n; i++ {
out = append(out, &types.IssueWithCounts{Issue: &types.Issue{ID: "bd-" + string(rune('0'+i))}})
}
return out
}
func storeReaderFor(t *testing.T, rows []*types.IssueWithCounts) (issueops.Reader, *fakeReaderStore) {
t.Helper()
store := &fakeReaderStore{rows: rows}
rd, err := New(store)
if err != nil {
t.Fatalf("storereader.New: %v", err)
}
return rd, store
}
func ids(page issueops.IssuePage) []string {
out := make([]string, 0, len(page.Items))
for _, item := range page.Items {
out = append(out, item.ID)
}
return out
}
// TestStoreReaderReadyDetectsTruncationWithAProbeRow: the store seam has no
// HasMore of its own, so the reader asks for one row past the page and lets its
// presence be the answer. Both halves matter — asking for limit+1 and then
// cutting back to limit — because a reader that only did the first would hand
// the caller a page one row longer than it asked for.
func TestStoreReaderReadyDetectsTruncationWithAProbeRow(t *testing.T) {
limit := 2
rd, store := storeReaderFor(t, readerFixture(5))
page, err := rd.Ready(context.Background(), issueops.ReadyRequest{Sort: "priority", Limit: &limit})
if err != nil {
t.Fatalf("Ready: %v", err)
}
if len(store.readyFilters) != 1 {
t.Fatalf("%d queries, want 1", len(store.readyFilters))
}
if got := store.readyFilters[0].Limit; got != limit+1 {
t.Errorf("query ran with Limit %d, want %d — the probe row is how this seam learns there is more", got, limit+1)
}
if got := ids(page); !slices.Equal(got, []string{"bd-1", "bd-2"}) {
t.Errorf("page = %v, want the first two rows; the probe row must not be delivered", got)
}
if !page.HasMore {
t.Error("HasMore = false, want true")
}
}
// TestStoreReaderReadyReportsAnExactPageAsComplete: the probe row is the whole
// signal, so a result that exactly fills the page must not claim more.
func TestStoreReaderReadyReportsAnExactPageAsComplete(t *testing.T) {
limit := 3
rd, _ := storeReaderFor(t, readerFixture(3))
page, err := rd.Ready(context.Background(), issueops.ReadyRequest{Sort: "priority", Limit: &limit})
if err != nil {
t.Fatalf("Ready: %v", err)
}
if len(page.Items) != 3 || page.HasMore {
t.Errorf("page = %v has_more = %v, want all three rows and no more", ids(page), page.HasMore)
}
}
// TestStoreReaderReadyLeavesAnUnlimitedQueryUnlimited: 0 means unlimited at
// both storage seams, and bumping it to 1 for a probe row would turn the one
// request that asks for everything into a request for one row.
func TestStoreReaderReadyLeavesAnUnlimitedQueryUnlimited(t *testing.T) {
unlimited := 0
rd, store := storeReaderFor(t, readerFixture(4))
page, err := rd.Ready(context.Background(), issueops.ReadyRequest{Sort: "priority", Limit: &unlimited})
if err != nil {
t.Fatalf("Ready: %v", err)
}
if got := store.readyFilters[0].Limit; got != 0 {
t.Errorf("query ran with Limit %d, want 0", got)
}
if len(page.Items) != 4 || page.HasMore {
t.Errorf("page = %v has_more = %v, want every row and no more", ids(page), page.HasMore)
}
}
// TestStoreReaderPagesAreNeverNil: an empty page is an empty array on every
// surface that serializes one, so no caller has to tell null from empty to
// learn that nothing matched.
func TestStoreReaderPagesAreNeverNil(t *testing.T) {
rd, _ := storeReaderFor(t, nil)
ready, err := rd.Ready(context.Background(), issueops.ReadyRequest{Sort: "priority"})
if err != nil {
t.Fatalf("Ready: %v", err)
}
if ready.Items == nil {
t.Error("Ready returned a nil Items on an empty result")
}
list, err := rd.List(context.Background(), issueops.ListRequest{})
if err != nil {
t.Fatalf("List: %v", err)
}
if list.Items == nil {
t.Error("List returned a nil Items on an empty result")
}
}
// TestStoreReaderListOverFetchesForAPushdownSort pins the other half of
// SQLLimit's decision. A sort the database CAN express keeps its row limit, so
// the reader has to add the probe row itself; the sort it cannot express
// already fetches everything, and adding one there would be meaningless.
func TestStoreReaderListOverFetchesForAPushdownSort(t *testing.T) {
limit := 2
for _, tc := range []struct {
sortBy string
wantLimit int
}{
{"created", 3}, // pushdown: SQLLimit is the page limit, plus the probe row
{"id", 0}, // no pushdown: SQLLimit is already 0, and 0 means unlimited
} {
t.Run(tc.sortBy, func(t *testing.T) {
rd, store := storeReaderFor(t, readerFixture(5))
page, err := rd.List(context.Background(), issueops.ListRequest{SortBy: tc.sortBy, Limit: &limit})
if err != nil {
t.Fatalf("List: %v", err)
}
if got := store.searchFilters[0].Limit; got != tc.wantLimit {
t.Errorf("--sort %s ran with Limit %d, want %d", tc.sortBy, got, tc.wantLimit)
}
if len(page.Items) != limit || !page.HasMore {
t.Errorf("--sort %s: page = %v has_more = %v, want %d rows and more",
tc.sortBy, ids(page), page.HasMore, limit)
}
})
}
}
// TestStoreReaderRefusesANilStore: the accessor is the only door, and a door
// that hands back a reader over nothing would fail on the first query with a
// nil dereference instead of at the seam that knows what is missing.
func TestStoreReaderRefusesANilStore(t *testing.T) {
rd, err := New(nil)
if err == nil {
t.Fatalf("New(nil) = %v, want an error", rd)
}
if rd != nil {
t.Errorf("New(nil) returned %T alongside its error", rd)
}
var unsupported *storage.ErrUnsupported
if !errors.As(err, &unsupported) {
t.Fatalf("New(nil) error = %v, want *storage.ErrUnsupported", err)
}
if unsupported.Op != "storereader.New" {
t.Errorf("ErrUnsupported.Op = %q, want storereader.New — the Op names the function that refused", unsupported.Op)
}
}