beads/internal/telemetry/issue_reader_test.go
CI Bot e1de64a784 test(telemetry,storage): pin the Reader decorators the way Lifecycle is pinned
The write side of the facade guards its decorator recursion with a named test
at both sites, on the reasoning that blind delegation "would still compile and
still satisfy Storage". The read side got the same code and none of the
enforcement: `return s.Unwrap().IssueReader()` in telemetry compiled, passed
every existing test, and silently unspanned every read while writes stayed
instrumented — no test file in internal/telemetry, internal/storage or
internal/storage/uow referenced IssueReader at all.

Telemetry gets the recursion pin, the error propagation pin, and a
forwards-once-on-both-outcomes table mirroring its Lifecycle sibling.

The hook store's pin asserts the OPPOSITE property, which is why it needed
writing down: reads fire no completion hooks, so its accessor returns the inner
reader UNWRAPPED, and a future edit "making the two accessors consistent" now
fails a test instead of adding a hook layer with nothing to fire.

Both regressions were reintroduced to confirm the tests see them.
2026-07-31 18:37:26 +00:00

113 lines
3.6 KiB
Go

package telemetry
import (
"context"
"errors"
"testing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/issueops"
)
type fakeIssueReader struct {
calls int
err error
}
func (f *fakeIssueReader) Ready(context.Context, issueops.ReadyRequest) (issueops.IssuePage, error) {
f.calls++
return issueops.IssuePage{}, f.err
}
func (f *fakeIssueReader) List(context.Context, issueops.ListRequest) (issueops.IssuePage, error) {
f.calls++
return issueops.IssuePage{}, f.err
}
func (f *fakeIssueReader) Get(context.Context, issueops.GetRequest) (*issueops.IssueDetails, error) {
f.calls++
return nil, f.err
}
// readerDoltStore is a DoltStorage whose only real method is IssueReader, so a
// test can see which reader the instrumented layer recursed into.
type readerDoltStore struct {
storage.DoltStorage
reader issueops.Reader
err error
}
func (s *readerDoltStore) IssueReader() (issueops.Reader, error) { return s.reader, s.err }
// TestInstrumentedStorageIssueReaderInstrumentsInner pins the recursion, the
// same way TestInstrumentedStorageIssueLifecycleInstrumentsInner pins the write
// side. `return s.Unwrap().IssueReader()` is a one-line edit that compiles,
// satisfies Storage, and passes every other test in the tree — and every read
// goes unspanned while writes stay instrumented.
func TestInstrumentedStorageIssueReaderInstrumentsInner(t *testing.T) {
t.Setenv("BD_OTEL_STDOUT", "true")
inner := &fakeIssueReader{}
wrapped := WrapStorage(&readerDoltStore{reader: inner}).(*InstrumentedStorage)
reader, err := wrapped.IssueReader()
if err != nil {
t.Fatalf("IssueReader() error = %v", err)
}
instrumented, ok := reader.(*instrumentedIssueReader)
if !ok {
t.Fatalf("IssueReader() = %T, want *instrumentedIssueReader", reader)
}
if instrumented.inner != issueops.Reader(inner) {
t.Fatalf("telemetry layer wraps %#v, want the inner store's reader", instrumented.inner)
}
}
func TestInstrumentedStorageIssueReaderPropagatesInnerError(t *testing.T) {
t.Setenv("BD_OTEL_STDOUT", "true")
want := errors.New("inner refused")
wrapped := WrapStorage(&readerDoltStore{err: want}).(*InstrumentedStorage)
reader, err := wrapped.IssueReader()
if !errors.Is(err, want) {
t.Fatalf("IssueReader() error = %v, want %v", err, want)
}
if reader != nil {
t.Fatalf("IssueReader() = %T, want nil", reader)
}
}
// TestInstrumentedIssueReaderForwardsEveryCallOnce: the span wrapper must not
// swallow, duplicate or reorder a call, on either outcome.
func TestInstrumentedIssueReaderForwardsEveryCallOnce(t *testing.T) {
t.Setenv("BD_OTEL_STDOUT", "true")
base := WrapStorage(&fakeDoltStore{}).(*InstrumentedStorage)
for _, test := range []struct {
name string
call func(issueops.Reader) error
}{
{"ready", func(r issueops.Reader) error {
_, e := r.Ready(context.Background(), issueops.ReadyRequest{})
return e
}},
{"list", func(r issueops.Reader) error {
_, e := r.List(context.Background(), issueops.ListRequest{})
return e
}},
{"get", func(r issueops.Reader) error {
_, e := r.Get(context.Background(), issueops.GetRequest{})
return e
}},
} {
t.Run(test.name+" success", func(t *testing.T) {
fake := &fakeIssueReader{}
if err := test.call(base.WrapIssueReader(fake)); err != nil || fake.calls != 1 {
t.Fatalf("err=%v calls=%d", err, fake.calls)
}
})
t.Run(test.name+" failure", func(t *testing.T) {
want := errors.New("inner refused")
fake := &fakeIssueReader{err: want}
if err := test.call(base.WrapIssueReader(fake)); !errors.Is(err, want) || fake.calls != 1 {
t.Fatalf("err=%v calls=%d", err, fake.calls)
}
})
}
}