beads/errors_test.go
Julian Knutsen d0603697dc
feat(errors): typed dependency error taxonomy on the public beads package (#4892)
* feat(errors): typed dependency error taxonomy on the public beads package

S1 of the guarded-ops initiative: let the bd CLI (and the library's own call
sites) classify dependency-write failures with errors.Is instead of
string-matching message text, without changing any message (wrap-preserving),
so existing string matchers keep working during the migration.

- Add ErrSelfDependency / ErrDependencyCycle sentinels in
  internal/storage/domain and route EVERY production self-dep/cycle emitter
  through them: issueops.CheckDependencyCycleInTx, the dolt cross-tier
  scheduling-cycle check, the domain use-case add + bulk-add paths, and the
  db SQL repository. Canonical-message sites stay byte-identical (sentinel as
  the static prefix via %w, or a bare sentinel return); already-divergent
  sites (bulk, db) append the sentinel with %w.
- Re-export ErrNotFound, ErrAlreadyClaimed, ErrNotClaimable, ErrSelfDependency,
  ErrDependencyCycle as var aliases on the root beads package, so beads.ErrX is
  identity-equal to the internal sentinel and errors.Is composes across the
  boundary.
- Contract tests: wrap-preserving assertions with EXACT message byte-identity;
  a real-production-return test through the domain use case; errors.Is
  regression assertions on the dolt cross-tier cycle path and the db self-dep
  path. The pre-existing self-dependency test passes unchanged, proving the
  canonical messages stayed byte-identical.

A red-team review caught that an earlier draft converted only the same-tier
issueops path, leaving the dolt cross-tier check (which sets SkipCycleCheck and
bypasses issueops) returning an untyped string — errors.Is was false for
cross-tier cycles on the same public DoltStore. All plumbings are now uniform.

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

* fix(errors): type bulk dependency self-dep and cycle rejections without changing their text

The dependency-add bulk path (proxied `bd dep add` / `bd link`, and the embedded
bulk CLI) surfaces its error verbatim via HandleErrorRespectJSON("%v", err).
Wrapping a rejection with a plain "%w" appends the sentinel's own text to an
already-complete message and changes user-facing output, so the bulk family was
left partially untyped and a scheduling self-edge was misreported as a cycle.

Type every dependency-add bulk rejection so callers can errors.Is it while the
rendered message stays byte-for-byte identical to the pre-taxonomy text:

- Route the per-edge and final domain cycle rejections through a small
  string-preserving cycleError wrapper (Error() returns the legacy text,
  Unwrap() returns ErrDependencyCycle).
- Guard IssueID==DependsOnID in domain addBulk for all dep types before the
  cycle probe, so a scheduling self-edge is ErrSelfDependency instead of
  tripping HasCycle (or the final CycleThroughEdges gate) and surfacing as a
  cycle; the message matches every other self-dep site.
- Type the embedded bulk CLI final gate (cmd/bd addBulkDependenciesInTx) via the
  exported domain.NewCycleError helper, so errors.Is(ErrDependencyCycle) holds
  regardless of backend/plumbing.
- Lead the defensive repo-layer self-dep guard with the sentinel like every
  other self-dep site instead of appending it.

Add exact-message + errors.Is assertions for the bulk cycle paths (per-edge,
final SkipPerEdgeCycleCheck, embedded final gate) and new bulk self-edge tests
(default, SkipPerEdgeCycleCheck, non-scheduling). Scope ErrDependencyCycle's doc
to the dependency-add family.

---------

Co-authored-by: Test User <test@test.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-18 18:29:07 -07:00

77 lines
2.8 KiB
Go

package beads_test
import (
"context"
"errors"
"testing"
"github.com/steveyegge/beads"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/domain"
"github.com/steveyegge/beads/internal/types"
)
// TestReExportedSentinelIdentity proves each public sentinel is the SAME value
// as the internal one it aliases, so errors.Is composes across the package
// boundary without any bridging.
func TestReExportedSentinelIdentity(t *testing.T) {
t.Parallel()
cases := []struct {
name string
exported error
internal error
}{
{"ErrNotFound", beads.ErrNotFound, storage.ErrNotFound},
{"ErrAlreadyClaimed", beads.ErrAlreadyClaimed, storage.ErrAlreadyClaimed},
{"ErrNotClaimable", beads.ErrNotClaimable, storage.ErrNotClaimable},
{"ErrSelfDependency", beads.ErrSelfDependency, domain.ErrSelfDependency},
{"ErrDependencyCycle", beads.ErrDependencyCycle, domain.ErrDependencyCycle},
}
for _, tc := range cases {
if tc.exported != tc.internal {
t.Errorf("beads.%s is not the internal sentinel value (identity broken)", tc.name)
}
}
}
// stubDepRepo satisfies domain.DependencySQLRepository via the embedded
// interface; only the two methods dependencyUseCaseImpl.add consults before
// returning a self-dep/cycle sentinel are implemented. Any other call would
// nil-panic, which keeps the stub honest about the exact surface these
// branches touch.
type stubDepRepo struct {
domain.DependencySQLRepository
hasCycle bool
}
func (s stubDepRepo) ValidateBlockingHierarchy(context.Context, *types.Dependency) error {
return nil
}
func (s stubDepRepo) HasCycle(context.Context, string, string) (bool, error) {
return s.hasCycle, nil
}
// TestReExportedSentinelCatchesRealProductionError drives ACTUAL converted
// production returns — the domain dependency use case's self-dep and cycle
// branches — and asserts the PUBLIC aliases match them via errors.Is. This is
// the property the re-export exists to provide, verified end to end through
// real code rather than by wrapping a sentinel with itself.
func TestReExportedSentinelCatchesRealProductionError(t *testing.T) {
t.Parallel()
uc := domain.NewDependencyUseCase(stubDepRepo{})
selfErr := uc.AddDependency(context.Background(),
&types.Dependency{IssueID: "a", DependsOnID: "a", Type: types.DepBlocks}, "tester")
if !errors.Is(selfErr, beads.ErrSelfDependency) {
t.Errorf("errors.Is(real self-dep err, beads.ErrSelfDependency) = false; err = %v", selfErr)
}
uc = domain.NewDependencyUseCase(stubDepRepo{hasCycle: true})
cycleErr := uc.AddDependency(context.Background(),
&types.Dependency{IssueID: "a", DependsOnID: "b", Type: types.DepBlocks}, "tester")
if !errors.Is(cycleErr, beads.ErrDependencyCycle) {
t.Errorf("errors.Is(real cycle err, beads.ErrDependencyCycle) = false; err = %v", cycleErr)
}
}