beads/errors_test.go
Julian Knutsen 1823f47ae4
feat(validate): typed length bounds for assignee, owner, and label (#4894)
The assignee, owner, and label columns are VARCHAR(255), but the engine never
validated their length: an over-255 assignee/owner made the row INSERT/UPDATE
fail with a raw backend "data too long" error, and an over-255 label went
through INSERT IGNORE and was SILENTLY TRUNCATED — storing a label the caller
never sent. Validate these three fields up front and return a typed
ErrFieldTooLong so `bd` (and the library's own call sites) get a clean, typed
rejection instead of a raw SQL error or silent corruption.

- types.MaxFieldLen (255), types.ErrFieldTooLong (re-exported as
  beads.ErrFieldTooLong), and types.CheckFieldLen using utf8.RuneCountInString
  — rune count, not bytes, so a multibyte value up to 255 characters fits the
  VARCHAR(255) column and passes while a 256-rune value fails.
- Guard every raw assignee/owner/label write on BOTH engine write stacks:
  the embedded issueops/DoltStore path (ValidateWithCustom + ValidateForImport
  for assignee/owner, UpdateIssueInTx pre-pass, AddLabelInTx and PersistLabels
  for labels, ClaimIssueInTx for the actor written as assignee) AND the
  proxied-server domain/db (uow) path (issue insertIssueRow + Update + Claim,
  label Insert) — which a first pass missed, so the silent-truncation bug
  persisted there.
- Tests on both stacks: over-length create/update/label/claim reject with
  ErrFieldTooLong and persist nothing (single and bulk); a 255-rune multibyte
  value round-trips unchanged and a 256-rune one is rejected (rune-count proof
  end to end); a 255-char value stores intact.

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

115 lines
4.4 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/domain"
"github.com/steveyegge/beads/internal/types"
)
// 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)
}
}
// 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},
{"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)
}
}
// 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)
}
}