mirror of
https://github.com/gastownhall/beads.git
synced 2026-09-14 20:17:24 +08:00
927f943572
DependencyEditor.AddDependencies refuses an edge whose SOURCE names no row, and one whose TARGET names no row this database can see the absence of. Every other refusal the role can raise hands a caller something to branch on — ErrSelfDependency, ErrDependencyCycle, *DependencyTypeConflictError, *DependencyHierarchyConflictError — and these two handed back "an error", with different text on each backend. A programmatic consumer could not tell a ghost endpoint from an infrastructure failure, and the role's own doc said so rather than promising the anonymity would last. Both now carry an identity: ErrDependencySourceNotFound and ErrDependencyTargetNotFound, wrapped by a *DependencyEndpointNotFoundError that names the refused edge and which of its endpoints was absent. Two sentinels rather than one because the two are separate answers — a ghost source is always a bad id, while a target is refused only where this database would have held it. What is NOT refused is unchanged: an "external:" reference and another repository's id are still stored as external targets. The store-backed body and the cross-tier target precheck mint the refusal where they already read the row. The domain repository has no such read: it learns of the absence from a foreign-key violation, so it reads both endpoints back on the transaction that refused, only on the refusal path, and never downgrades the refusal to a probe's failure. Taking the identity out of the driver's constraint name would have been the thing a typed refusal exists to avoid. One user-visible consequence: the proxied-server path now renders "issue <id> not found" where it used to render a raw foreign-key violation, which is what the embedded path has always said. The guarded create classifies the new type where it classified the raw foreign-key signal, so a missing dependency, parent or waits-for target is still ErrValidation wrapping ErrNotFound on every backend. RemoveDependency is deliberately untouched. A removal that finds no edge is Removed false with a nil error, so there is no refusal there to name. The shared contract asserts the sentinel and the typed fields for each endpoint in TWO POSITIONS: alone in a request, and mid-batch where the refusal competes with the rollback of an edge already written, the half that also reads the graph back at zero edges. Both cases were verified red on all three backends before the production change — "issue <id> not found" untyped on the two store paths, "Error 1452 ... Foreign key violation" on the unit-of-work path. Agent-Signature: claude-code-claude-opus-5-unknown-reasoning on behalf of CI Bot Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
254 lines
11 KiB
Go
254 lines
11 KiB
Go
package beads_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/dolt"
|
|
"github.com/steveyegge/beads/internal/storage/domain"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
func TestReExportCommitIndeterminate(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if beads.ErrCommitIndeterminate != storage.ErrCommitIndeterminate {
|
|
t.Error("beads.ErrCommitIndeterminate is not the shared storage sentinel value (identity broken)")
|
|
}
|
|
if dolt.ErrCommitIndeterminate != storage.ErrCommitIndeterminate {
|
|
t.Error("dolt.ErrCommitIndeterminate is not the shared storage sentinel value (identity broken)")
|
|
}
|
|
wrapped := fmt.Errorf("update issue: %w", storage.ErrCommitIndeterminate)
|
|
if !errors.Is(wrapped, beads.ErrCommitIndeterminate) {
|
|
t.Errorf("errors.Is(wrapped, beads.ErrCommitIndeterminate) = false; err = %v", wrapped)
|
|
}
|
|
}
|
|
|
|
// TestReExportCloseBlocked proves the public beads.ErrCloseBlocked alias is the
|
|
// same value as the internal sentinel and composes through errors.Is when
|
|
// wrapped — the property CloseIssueChecked callers rely on to detect a guard
|
|
// refusal without importing internal/storage.
|
|
func TestReExportCloseBlocked(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if beads.ErrCloseBlocked != storage.ErrCloseBlocked {
|
|
t.Error("beads.ErrCloseBlocked is not the internal sentinel value (identity broken)")
|
|
}
|
|
wrapped := fmt.Errorf("x: %w", beads.ErrCloseBlocked)
|
|
if !errors.Is(wrapped, beads.ErrCloseBlocked) {
|
|
t.Errorf("errors.Is(wrapped, beads.ErrCloseBlocked) = false; err = %v", wrapped)
|
|
}
|
|
}
|
|
|
|
// TestReExportVersionMismatch proves the public beads.ErrVersionMismatch alias
|
|
// is the same value as the internal sentinel and composes through errors.Is when
|
|
// wrapped — the property a CloseIssueChecked caller relies on to detect an
|
|
// optimistic-concurrency refusal without importing internal/storage.
|
|
func TestReExportVersionMismatch(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if beads.ErrVersionMismatch != storage.ErrVersionMismatch {
|
|
t.Error("beads.ErrVersionMismatch is not the internal sentinel value (identity broken)")
|
|
}
|
|
wrapped := fmt.Errorf("x: %w", beads.ErrVersionMismatch)
|
|
if !errors.Is(wrapped, beads.ErrVersionMismatch) {
|
|
t.Errorf("errors.Is(wrapped, beads.ErrVersionMismatch) = false; err = %v", wrapped)
|
|
}
|
|
}
|
|
|
|
// TestUpdateIssueOptionsIsExported proves the public beads.UpdateIssueOptions
|
|
// alias is usable from outside the module and its ExpectedVersion compare-and-
|
|
// swap field round-trips — the type a caller names to opt a
|
|
// Storage.UpdateIssueChecked into optimistic concurrency without importing
|
|
// internal/storage. The zero value must leave ExpectedVersion nil (no check).
|
|
func TestUpdateIssueOptionsIsExported(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
v := int64(7)
|
|
opts := beads.UpdateIssueOptions{ExpectedVersion: &v}
|
|
if opts.ExpectedVersion == nil || *opts.ExpectedVersion != 7 {
|
|
t.Fatalf("ExpectedVersion did not round-trip through the exported alias: %+v", opts)
|
|
}
|
|
if (beads.UpdateIssueOptions{}).ExpectedVersion != nil {
|
|
t.Fatal("zero-value UpdateIssueOptions must have a nil ExpectedVersion (no check)")
|
|
}
|
|
}
|
|
|
|
// 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},
|
|
{"ErrVersionMismatch", beads.ErrVersionMismatch, storage.ErrVersionMismatch},
|
|
{"ErrSelfDependency", beads.ErrSelfDependency, domain.ErrSelfDependency},
|
|
{"ErrDependencyCycle", beads.ErrDependencyCycle, domain.ErrDependencyCycle},
|
|
{"ErrDependencySourceNotFound", beads.ErrDependencySourceNotFound, domain.ErrDependencySourceNotFound},
|
|
{"ErrDependencyTargetNotFound", beads.ErrDependencyTargetNotFound, domain.ErrDependencyTargetNotFound},
|
|
{"ErrFieldTooLong", beads.ErrFieldTooLong, types.ErrFieldTooLong},
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
// insertErrDepRepo returns preset errors from the two methods the domain
|
|
// dependency use-case consults before its typed-conflict passthrough branches:
|
|
// ValidateBlockingHierarchy (hierarchy conflict) and Insert (type conflict). Any
|
|
// other call nil-panics through the embedded interface, keeping the stub honest
|
|
// about the surface these branches touch.
|
|
type insertErrDepRepo struct {
|
|
domain.DependencySQLRepository
|
|
hierarchyErr error
|
|
insertErr error
|
|
}
|
|
|
|
func (r insertErrDepRepo) ValidateBlockingHierarchy(context.Context, *types.Dependency) error {
|
|
return r.hierarchyErr
|
|
}
|
|
|
|
func (r insertErrDepRepo) HasCycle(context.Context, string, string) (bool, error) {
|
|
return false, nil
|
|
}
|
|
|
|
func (r insertErrDepRepo) Insert(context.Context, *types.Dependency, string, domain.DepInsertOpts) error {
|
|
return r.insertErr
|
|
}
|
|
|
|
// TestReExportedDependencyConflictTypes proves the public
|
|
// beads.DependencyTypeConflictError and beads.DependencyHierarchyConflictError
|
|
// aliases are the SAME struct types the engine returns: driving ACTUAL domain
|
|
// use-case passthrough returns (the conflict is passed through unwrapped, the
|
|
// property both write stacks now share), errors.As classifies each through the
|
|
// public alias and reads its fields — no message parsing.
|
|
func TestReExportedDependencyConflictTypes(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Type conflict: a different-type edge already exists between the pair.
|
|
typeConflict := &domain.DependencyTypeConflictError{
|
|
IssueID: "a", DependsOnID: "b", ExistingType: "blocks", RequestedType: "related",
|
|
}
|
|
uc := domain.NewDependencyUseCase(insertErrDepRepo{insertErr: typeConflict})
|
|
err := uc.AddDependency(context.Background(),
|
|
&types.Dependency{IssueID: "a", DependsOnID: "b", Type: types.DepRelated}, "tester")
|
|
var gotType *beads.DependencyTypeConflictError
|
|
if !errors.As(err, &gotType) {
|
|
t.Fatalf("errors.As(real type-conflict err, *beads.DependencyTypeConflictError) = false; err = %v", err)
|
|
}
|
|
if gotType.IssueID != "a" || gotType.DependsOnID != "b" ||
|
|
gotType.ExistingType != "blocks" || gotType.RequestedType != "related" {
|
|
t.Errorf("extracted type-conflict fields = %+v, want {a b blocks related}", gotType)
|
|
}
|
|
|
|
// Hierarchy conflict: a blocking edge would gate an issue on its ancestor.
|
|
hierConflict := &domain.DependencyHierarchyConflictError{
|
|
IssueID: "child", BlockerID: "ancestor", BlockerIsAncestor: true,
|
|
}
|
|
uc = domain.NewDependencyUseCase(insertErrDepRepo{hierarchyErr: hierConflict})
|
|
err = uc.AddDependency(context.Background(),
|
|
&types.Dependency{IssueID: "child", DependsOnID: "ancestor", Type: types.DepBlocks}, "tester")
|
|
var gotHier *beads.DependencyHierarchyConflictError
|
|
if !errors.As(err, &gotHier) {
|
|
t.Fatalf("errors.As(real hierarchy-conflict err, *beads.DependencyHierarchyConflictError) = false; err = %v", err)
|
|
}
|
|
if gotHier.IssueID != "child" || gotHier.BlockerID != "ancestor" || !gotHier.BlockerIsAncestor {
|
|
t.Errorf("extracted hierarchy-conflict fields = %+v, want {child ancestor true}", gotHier)
|
|
}
|
|
|
|
// Endpoint miss: the target names no row this database holds. The domain
|
|
// use case passes it through unwrapped for the same reason the two
|
|
// conflicts above are passed through.
|
|
missing := &domain.DependencyEndpointNotFoundError{
|
|
IssueID: "a", DependsOnID: "ghost", MissingID: "ghost",
|
|
Err: domain.ErrDependencyTargetNotFound,
|
|
}
|
|
uc = domain.NewDependencyUseCase(insertErrDepRepo{insertErr: missing})
|
|
err = uc.AddDependency(context.Background(),
|
|
&types.Dependency{IssueID: "a", DependsOnID: "ghost", Type: types.DepBlocks}, "tester")
|
|
var gotMissing *beads.DependencyEndpointNotFoundError
|
|
if !errors.As(err, &gotMissing) {
|
|
t.Fatalf("errors.As(real endpoint-miss err, *beads.DependencyEndpointNotFoundError) = false; err = %v", err)
|
|
}
|
|
if !errors.Is(err, beads.ErrDependencyTargetNotFound) {
|
|
t.Errorf("errors.Is(real endpoint-miss err, beads.ErrDependencyTargetNotFound) = false; err = %v", err)
|
|
}
|
|
if gotMissing.IssueID != "a" || gotMissing.DependsOnID != "ghost" || gotMissing.MissingID != "ghost" {
|
|
t.Errorf("extracted endpoint-miss fields = %+v, want {a ghost ghost}", gotMissing)
|
|
}
|
|
}
|
|
|
|
// TestReExportFieldTooLong proves the public beads.ErrFieldTooLong alias is the
|
|
// same value as the internal types sentinel and composes through errors.Is when
|
|
// wrapped — the property length-validation callers rely on to detect an
|
|
// over-length assignee/owner/label without importing internal/types. It also
|
|
// checks the MaxFieldLen constant re-export tracks the source of truth.
|
|
func TestReExportFieldTooLong(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if beads.ErrFieldTooLong != types.ErrFieldTooLong {
|
|
t.Error("beads.ErrFieldTooLong is not the internal sentinel value (identity broken)")
|
|
}
|
|
wrapped := fmt.Errorf("x: %w", beads.ErrFieldTooLong)
|
|
if !errors.Is(wrapped, beads.ErrFieldTooLong) {
|
|
t.Errorf("errors.Is(wrapped, beads.ErrFieldTooLong) = false; err = %v", wrapped)
|
|
}
|
|
if beads.MaxFieldLen != types.MaxFieldLen {
|
|
t.Errorf("beads.MaxFieldLen = %d, want types.MaxFieldLen = %d", beads.MaxFieldLen, types.MaxFieldLen)
|
|
}
|
|
}
|