beads/cmd/bd/show_proxied_reader_test.go
Athos Bernardes d871b5c54a
fix(show): explain that "not found" may mean deleted, not never-existed (#5802)
* fix(show): explain that "not found" may mean deleted, not never-existed (ga-m6inyb)

bd delete and wisp purge both remove a row (and its events) from the
live tables with no trace left behind, so `bd show` on a deleted or
purged id is byte-for-byte identical to one that never existed —
confirmed live (Gas Town HQ Dolt server) against two real archived-wisp
ids, and against dolt_history_wisps (always empty for wisps by design,
per the dolt_ignore comments throughout internal/storage/dolt).

An earlier draft of this fix tried to auto-detect the distinction via
bd's own commit history (store.History), but that scan has a
documented multi-second-to-timeout cost even on a healthy production
database (reproduced live: `bd history` timed out with "row read wait
bigger than connection timeout" on both a real archived wisp and a
made-up id on the same server) — wiring it into show's default
not-found path would regress the common typo case to fix a rarer one.
Wisps never have history at all regardless, so it wouldn't have
resolved the reported case either way.

Instead, the not-found message now says plainly that the id may have
existed and been removed, and points at `bd history <id>` as the
(separate, opt-in) way to check further — additive only: the JSON
"error" field text is unchanged, the new context lands in a "hint"
field; the direct-mode text path keeps "Issue %s not found" as its
first line and adds the hint as a second line.

Also found, filed separately, not fixed here: `make ci-pr-lint`
surfaces 2 pre-existing gosec findings in darwin/nonlinux-only files
that the Linux CI runner never lints (no darwin cross-lint pass,
unlike the existing Windows one) — confirmed present on a clean
origin/main checkout, unrelated to this change.

* fix(show): update contract-pinning tests for the new not-found hint (CI)

CI on PR #5802 caught 4 test corpora this change needed to update but I
missed locally (my earlier local run only matched tests named
TestShow*; these pin the SAME code path — reportIssueLookupFailure and
show.go's JSON not-found branch — under different names):

- cmd/bd/proxied_lookup_failure_test.go: TestProxiedLookupReportsMissingIssue
  (show / show --refs / show --children / reopen rows) and
  TestReportIssueLookupFailure both assert exact stderr text by design
  ("Their lines here are unchanged, and that is the point" — the
  file's own doc comment). Updated the 4 affected wantNotFound values
  to include the new hint, computed via showNotFoundHint() rather than
  duplicated as a literal, so the two can't drift independently.

- cmd/bd/protocol/testdata/corpus/{flat,envelope}/error.json and their
  manifest.json SHA-256 entries: the committed golden-file corpus for
  `bd show <missing> --json`. `make corpus-regen` needs Docker
  (unavailable here) to regenerate for real, so I independently
  reimplemented corpus.go's exact marshalCanonical encoding (same
  json.Encoder settings: SetEscapeHTML(false), 2-space indent) in a
  standalone program and confirmed both edited files are byte-for-byte
  identical to what that encoder produces, before hand-computing and
  writing the new SHA-256 manifest entries.

Two more matches from a repo-wide grep for the old exact string
(linear_test.go, update_claim_proxied_test.go) needed no change:
linear_test.go's is an unrelated subsystem's own literal message, and
update_claim_proxied_test.go asserts via strings.Contains, which the
purely-additive hint doesn't break.

Local verification (same commands as before, still Docker-less):
go build ./..., go vet ./cmd/bd/..., go vet -tags=integration, make
ci-pr-lint (2 pre-existing unrelated darwin-only gosec findings only),
and the full previously-failing test names plus a broader NotFound|
LookupFailure|Claim sweep — all green.

* docs(changelog): add [Unreleased] entry for the show not-found hint (ga-m6inyb)

Addresses reviewer bee-ghosttrack's second ask on PR #5802: "this adds a
line to one of bd's most common outputs and a new JSON field" needs a
CHANGELOG entry. Filed under Fixed, since the underlying bead is a bug
(archived and never-existed issues were indistinguishable via bd show).

Also documents, per the reviewer's first ask, that the direct-mode
single-ID not-found path's first line is not purely additive: it
changes from "Error fetching <id>: ..." to "Issue <id> not found" to
match the wording the other not-found paths already used.

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

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Steve Yegge <steve.yegge@gmail.com>
2026-08-20 08:11:39 -07:00

111 lines
4.1 KiB
Go

package main
import (
"context"
"errors"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/storage/uow"
"github.com/steveyegge/beads/issueops"
)
// readerProvider is lookupOnlyProvider plus the capability accessor, which is
// the door `bd show --json` now reaches the detail view through on this route.
// The reader it hands back is the REAL one, over the same failing lookup, so
// what these tests observe is the role's own error vocabulary reaching the
// command — not a stub's imitation of it.
type readerProvider struct{ lookupOnlyProvider }
func (p readerProvider) IssueReader() (issueops.Reader, error) {
return uow.NewIssueReader(p)
}
var _ uow.IssueReaderSource = readerProvider{}
func withStubbedProxiedReader(t *testing.T, hardErr error) {
t.Helper()
oldProvider, oldJSON := uowProvider, jsonOutput
uowProvider = readerProvider{lookupOnlyProvider{issues: stubLookupIssueUC{hardErr: hardErr}}}
jsonOutput = true
t.Cleanup(func() {
uowProvider = oldProvider
jsonOutput = oldJSON
})
}
// TestShowProxiedJSONMissingIDKeepsItsContract is the regression guard for
// moving the proxied detail view onto issueops.Reader. The corpus pins a PAIR
// of outputs for a missing id under --json — the human line on stderr and the
// error envelope on stdout, exit 1 — and the role reports the same miss as a
// storage.ErrNotFound rather than as a resolve failure the command reported
// itself. If that mapping is ever lost, this fails instead of the corpus.
func TestShowProxiedJSONMissingIDKeepsItsContract(t *testing.T) {
withStubbedProxiedReader(t, nil)
var err error
stderr := captureStderrDuring(t, func() {
err = runShowProxiedServer(&cobra.Command{}, context.Background(), []string{stubMissingID})
})
if err == nil {
t.Error("a batch that found nothing exited zero")
}
if got, want := strings.SplitN(stderr, "\n", 2)[0], "Issue "+stubMissingID+" not found"; got != want {
t.Errorf("stderr first line = %q, want %q", got, want)
}
if !strings.Contains(stderr, "bd history "+stubMissingID) {
t.Errorf("expected stderr to hint at checking 'bd history %s', got: %q", stubMissingID, stderr)
}
if strings.Contains(stderr, stubRawNoRows) {
t.Errorf("stderr leaks the raw driver sentinel: %q", stderr)
}
}
// TestShowProxiedJSONBackendFailureAborts pins the one behavior this move
// changed on purpose. Resolution and assembly used to be two calls, and a
// backend failure was reported per id and skipped past when it surfaced in the
// first, aborted on when it surfaced in the second. They are one call now, so
// there is one answer, and abort is the one worth keeping: a JSON array
// missing the rows a database error swallowed is indistinguishable from a
// complete one.
func TestShowProxiedJSONBackendFailureAborts(t *testing.T) {
withStubbedProxiedReader(t, errors.New(stubBackendError))
var err error
_ = captureStderrDuring(t, func() {
err = runShowProxiedServer(&cobra.Command{}, context.Background(), []string{stubMissingID})
})
if err == nil {
t.Fatal("a backend failure exited zero")
}
if strings.Contains(strings.ToLower(err.Error()), "not found") {
t.Errorf("backend failure reported as a missing issue: %v", err)
}
}
// TestShowProxiedTextRouteDoesNotOpenAReader pins the other half of the split:
// the terminal rendering needs the raw issue and the wisp flag, which the
// detail contract does not carry, so it stays on the CLI's own resolution and
// must not pay for a reader it cannot use.
func TestShowProxiedTextRouteDoesNotOpenAReader(t *testing.T) {
withStubbedProxiedLookup(t, nil) // a provider with NO capability accessor
var err error
stderr := captureStderrDuring(t, func() {
err = runShowProxiedServer(&cobra.Command{}, context.Background(), []string{stubMissingID})
})
if err == nil {
t.Error("a batch that found nothing exited zero")
}
if got, want := strings.SplitN(stderr, "\n", 2)[0], "Issue "+stubMissingID+" not found"; got != want {
t.Errorf("stderr first line = %q, want %q", got, want)
}
if !strings.Contains(stderr, "bd history "+stubMissingID) {
t.Errorf("expected stderr to hint at checking 'bd history %s', got: %q", stubMissingID, stderr)
}
}