beads/issueops/issueops_external_test.go
Julian Knutsen 4caeeffeec
feat(serve): name the blockers on a live-blocker not_closable (#6893)
* feat(issueops): type the blocked-close refusal as BlockedError

Every close-policy blocker refusal was fmt.Errorf("%w: %s is blocked by
%v", ErrCloseBlocked, ...), so the blocker list existed only as prose. A
caller reporting the refusal elsewhere (the HTTP surface) had to parse it
or drop it.

issueops.BlockedError carries IssueID, the typed []Blocker (id plus the
blocking edge type when known) and the wrapped sentinel. Its message is
byte-identical to the historical sentence, so the direct and proxied CLI
lines and failed[].error do not move, and errors.Is(ErrCloseBlocked) still
matches. The sentinel is a field rather than fixed so other "X is blocked
by Y" refusals (e.g. a blocked claim) can reuse the type.

The in-transaction guard keeps the edges typed (isBlockedByInTx, with
IsBlockedInTx formatting through Blocker.String), and the external guard
builds the error from IsBlocked's []string via ParseBlocker, the inverse
of that spelling.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(httpapi): name the blockers on a live-blocker not_closable

The served close refusal said only "issue is blocked", so an HTTP client
could not tell the user what held the close, while bd close on the direct
and proxied routes prints "cannot close blocked issue: X is blocked by
[Y]".

A live-blocker not_closable now carries a `blockers` extension member,
[{id, kind: local|external, type?}], read from *issueops.BlockedError's
typed list, and a detail that opens with the direct route's sentence
built from the same list. It covers issues/{id}:close, per-item
issues:batchClose outcomes, PATCH into a done status and
issues:batchApply. The code, status and open_children discriminator are
unchanged; an error that only mentions blockers in prose keeps the old
generic detail and no member.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(issueops): test the external: prefix before the type split in ParseBlocker

Maintainer review fixup for #6893.

ParseBlocker tried the " (" / ")" split before the external: prefix
branch, so it won for any external reference whose capability ends in
")". validateExternalRef (cmd/bd/dep.go) constrains a capability to
non-empty only, so `external:remote:pay (beta)` is a legal reference
created by the sanctioned `bd dep add` path -- and it parsed to a
truncated ID of "external:remote:pay" plus Type "beta", a value outside
the documented blocks|waits-for|conditional-blocks vocabulary that
falsifies the spec's own promise that an external blocker carries no
type.

Only the structured half was wrong: the mis-split round-trips through
String(), so the prose detail and the CLI sentence stayed byte-identical
and the failure was silent on exactly the new `blockers` member this
change exists to make trustworthy.

Hoisting the prefix test is loss-free -- Blocker.Type's doc comment
already states an external reference never carries a reported type.

Pin: the ParseBlocker/String table in
TestBlockedErrorKeepsTheHistoricalSentence gains the paren-suffixed
external row; every external case tabled there was paren-free, which is
why this got through. Restoring the old ordering reds the new row.
The DeepEqual failure now prints %#v, because Blocker implements
String() and the default verbs render got and want identically for
precisely this defect.

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
2026-10-01 00:51:06 -07:00

342 lines
13 KiB
Go

package issueops_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"testing"
"github.com/steveyegge/beads"
"github.com/steveyegge/beads/issueops"
)
func TestPublicDeclarationsHaveUsefulZeroValues(t *testing.T) {
var _ issueops.Lifecycle = operationsProbe{}
var create issueops.CreateRequest
var update issueops.UpdateRequest
var closeRequest issueops.CloseRequest
var reopen issueops.ReopenRequest
if create.Issue != nil || update.ExpectedVersion != nil || closeRequest.ExpectedVersion != nil || reopen.ExpectedVersion != nil {
t.Fatal("zero-value request unexpectedly carries a value")
}
}
func TestFieldDistinguishesOmittedFromExplicitZero(t *testing.T) {
var omitted issueops.Field[int]
explicit := issueops.Field[int]{Set: true, Value: 0}
if omitted.Set || !explicit.Set || explicit.Value != 0 {
t.Fatalf("field semantics lost: %#v %#v", omitted, explicit)
}
clear := issueops.Field[*string]{Set: true, Value: nil}
if !clear.Set || clear.Value != nil {
t.Fatalf("explicit pointer clear lost: %#v", clear)
}
}
func TestPatchesRepresentExplicitEmptyReplacements(t *testing.T) {
labels := issueops.LabelPatch{
Replace: issueops.Field[[]string]{Set: true, Value: []string{}},
}
if !labels.Replace.Set || labels.Replace.Value == nil || len(labels.Replace.Value) != 0 {
t.Fatalf("explicit empty label replacement lost: %#v", labels.Replace)
}
metadata := issueops.MetadataPatch{
Replace: issueops.Field[json.RawMessage]{Set: true, Value: json.RawMessage(`{}`)},
}
if !metadata.Replace.Set || string(metadata.Replace.Value) != `{}` {
t.Fatalf("explicit empty metadata replacement lost: %#v", metadata.Replace)
}
}
func TestPersistencePatchHasOneTypedMode(t *testing.T) {
patchType := reflect.TypeFor[issueops.IssuePatch]()
if _, found := patchType.FieldByName("Ephemeral"); found {
t.Fatal("IssuePatch still exposes independent Ephemeral")
}
if _, found := patchType.FieldByName("NoHistory"); found {
t.Fatal("IssuePatch still exposes independent NoHistory")
}
persistence, found := patchType.FieldByName("Persistence")
if !found {
t.Fatal("IssuePatch.Persistence is missing")
}
if persistence.Type != reflect.TypeFor[issueops.Field[issueops.PersistenceMode]]() {
t.Fatalf("IssuePatch.Persistence type = %v, want Field[PersistenceMode]", persistence.Type)
}
}
func TestPublicPersistenceModeValues(t *testing.T) {
cases := []struct {
got issueops.PersistenceMode
want string
}{
{issueops.PersistenceModePersistent, "persistent"},
{issueops.PersistenceModeEphemeral, "ephemeral"},
{issueops.PersistenceModeNoHistory, "no_history"},
}
for _, tc := range cases {
if string(tc.got) != tc.want {
t.Errorf("persistence mode = %q, want %q", tc.got, tc.want)
}
}
}
func TestClaimRequestSupportsPostClaimPatch(t *testing.T) {
version := int64(7)
request := issueops.UpdateRequest{
Actor: "worker",
IssueID: "bd-123",
Claim: true,
ExpectedVersion: &version,
Patch: issueops.IssuePatch{
Status: issueops.Field[issueops.Status]{Set: true, Value: issueops.StatusBlocked},
Assignee: issueops.Field[string]{Set: true, Value: "dispatcher"},
},
}
if !request.Claim || request.Patch.Status.Value != issueops.StatusBlocked || request.Patch.Assignee.Value != "dispatcher" || *request.ExpectedVersion != version {
t.Fatalf("claim request lost its explicit post-claim patch or version guard: %#v", request)
}
}
func TestForceAssigneeTransferHasSafeZeroValue(t *testing.T) {
var guarded issueops.UpdateRequest
forced := issueops.UpdateRequest{ForceAssigneeTransfer: true}
if guarded.ForceAssigneeTransfer || !forced.ForceAssigneeTransfer {
t.Fatalf("force-assignee-transfer declaration lost bool zero-value semantics: %#v %#v", guarded, forced)
}
}
func TestPublicConstantsKeepCanonicalValues(t *testing.T) {
statuses := []struct {
got issueops.Status
want beads.Status
}{
{issueops.StatusOpen, beads.StatusOpen},
{issueops.StatusInProgress, beads.StatusInProgress},
{issueops.StatusBlocked, beads.StatusBlocked},
{issueops.StatusDeferred, beads.StatusDeferred},
{issueops.StatusClosed, beads.StatusClosed},
}
for _, check := range statuses {
if check.got != check.want {
t.Fatalf("status constant = %q, want %q", check.got, check.want)
}
}
for name, got := range map[string]string{
"pinned": string(issueops.StatusPinned),
"hooked": string(issueops.StatusHooked),
"all-children": issueops.WaitsForAllChildren,
"any-children": issueops.WaitsForAnyChildren,
} {
if got != name {
t.Errorf("public constant = %q, want %q", got, name)
}
}
}
func TestPublicErrorsKeepCanonicalIdentity(t *testing.T) {
checks := []struct {
name string
got, want error
}{
{"not found", issueops.ErrNotFound, beads.ErrNotFound},
{"field too long", issueops.ErrFieldTooLong, beads.ErrFieldTooLong},
{"already claimed", issueops.ErrAlreadyClaimed, beads.ErrAlreadyClaimed},
{"not claimable", issueops.ErrNotClaimable, beads.ErrNotClaimable},
{"close blocked", issueops.ErrCloseBlocked, beads.ErrCloseBlocked},
{"version mismatch", issueops.ErrVersionMismatch, beads.ErrVersionMismatch},
{"self dependency", issueops.ErrSelfDependency, beads.ErrSelfDependency},
{"dependency cycle", issueops.ErrDependencyCycle, beads.ErrDependencyCycle},
}
for _, check := range checks {
if !errors.Is(check.got, check.want) {
t.Errorf("%s error identity lost: %v", check.name, check.got)
}
}
for name, err := range map[string]error{
"validation": issueops.ErrValidation,
"not initialized": issueops.ErrNotInitialized,
"prefix mismatch": issueops.ErrPrefixMismatch,
"assignee mismatch": issueops.ErrAssigneeMismatch,
"status mismatch": issueops.ErrStatusMismatch,
"already exists": issueops.ErrAlreadyExists,
} {
if err == nil {
t.Errorf("%s sentinel is nil", name)
}
}
classified := errors.Join(issueops.ErrValidation, issueops.ErrPrefixMismatch)
if !errors.Is(classified, issueops.ErrValidation) {
t.Error("deterministic validation failure lost ErrValidation classification")
}
if !errors.Is(classified, issueops.ErrPrefixMismatch) {
t.Error("deterministic validation failure lost its specific classification")
}
}
func TestOpenChildrenCloseContractShape(t *testing.T) {
err := &issueops.CloseOpenChildrenError{
IssueID: "bd-parent",
OpenChildren: 3,
}
if !errors.Is(err, issueops.ErrCloseOpenChildren) {
t.Fatalf("CloseOpenChildrenError does not match ErrCloseOpenChildren: %v", err)
}
if got := err.Error(); !strings.Contains(got, "bd-parent") || !strings.Contains(got, "3") {
t.Errorf("CloseOpenChildrenError = %q, want issue and count", got)
}
result := issueops.CloseResult{OpenChildren: 3}
if result.OpenChildren != 3 {
t.Fatalf("CloseResult.OpenChildren = %d, want 3", result.OpenChildren)
}
}
// TestBlockedErrorKeepsTheHistoricalSentence pins the compatibility half of the
// typed refusal: its message is byte-identical to the fmt.Errorf every producer
// used before it, so the direct and proxied CLI lines and `failed[].error` do
// not move, while the list it prints is now typed.
func TestBlockedErrorKeepsTheHistoricalSentence(t *testing.T) {
blockers := []string{"bd-2", "bd-3 (waits-for)", "bd-4 (conditional-blocks)", "external:remote:payments", "external:remote:pay (beta)"}
err := issueops.NewCloseBlockedError("bd-1", blockers)
legacy := fmt.Errorf("%w: %s is blocked by %v", issueops.ErrCloseBlocked, "bd-1", blockers)
if err.Error() != legacy.Error() {
t.Errorf("BlockedError = %q, want the historical %q", err.Error(), legacy.Error())
}
if !errors.Is(err, issueops.ErrCloseBlocked) {
t.Errorf("BlockedError does not match ErrCloseBlocked: %v", err)
}
var typed *issueops.BlockedError
if !errors.As(fmt.Errorf("close bd-1: %w", err), &typed) || typed.IssueID != "bd-1" {
t.Fatalf("errors.As through a wrap = %+v", typed)
}
want := []issueops.Blocker{
{ID: "bd-2", Type: "blocks"},
{ID: "bd-3", Type: "waits-for"},
{ID: "bd-4", Type: "conditional-blocks"},
{ID: "external:remote:payments"},
// A capability may end in a parenthesized suffix. The whole
// reference is the ID, and the external guard reports no type.
{ID: "external:remote:pay (beta)"},
}
// %#v, not %+v: Blocker implements String(), so the default verbs print
// the reassembled spelling — which round-trips even when the parse split
// an external reference wrongly, making got and want look identical.
if !reflect.DeepEqual(typed.Blockers, want) {
t.Errorf("Blockers = %#v, want %#v", typed.Blockers, want)
}
for i, blocker := range typed.Blockers {
if got := blocker.String(); got != blockers[i] {
t.Errorf("Blockers[%d].String() = %q, want the IsBlocked spelling %q", i, got, blockers[i])
}
if parsed := issueops.ParseBlocker(blocker.String()); parsed != blocker {
t.Errorf("ParseBlocker(String(%+v)) = %+v; the two must be inverses", blocker, parsed)
}
if got, wantExternal := blocker.External(), i >= 3; got != wantExternal {
t.Errorf("Blockers[%d].External() = %v, want %v", i, got, wantExternal)
}
}
}
func TestPublicDependencyConflictTypesRemainCanonical(t *testing.T) {
var typeConflict error = &issueops.DependencyTypeConflictError{}
var hierarchyConflict error = &issueops.DependencyHierarchyConflictError{}
if typeConflict == nil || hierarchyConflict == nil {
t.Fatal("dependency conflict aliases lost their error implementations")
}
if reflect.TypeFor[*issueops.DependencyTypeConflictError]() != reflect.TypeFor[*beads.DependencyTypeConflictError]() {
t.Error("DependencyTypeConflictError lost canonical type identity")
}
if reflect.TypeFor[*issueops.DependencyHierarchyConflictError]() != reflect.TypeFor[*beads.DependencyHierarchyConflictError]() {
t.Error("DependencyHierarchyConflictError lost canonical type identity")
}
if reflect.TypeFor[*issueops.DependencyEndpointNotFoundError]() != reflect.TypeFor[*beads.DependencyEndpointNotFoundError]() {
t.Error("DependencyEndpointNotFoundError lost canonical type identity")
}
}
// TestDependencyEndpointNotFoundCarriesTheAbsentEndpoint pins the value's own
// promises: the sentinel a caller branches on, the edge and the endpoint it
// names, and the message — which the two write plumbings render identically
// only because both build it from here.
func TestDependencyEndpointNotFoundCarriesTheAbsentEndpoint(t *testing.T) {
ghostSource := &issueops.DependencyEndpointNotFoundError{
IssueID: "bd-ghost", DependsOnID: "bd-real", MissingID: "bd-ghost",
Err: issueops.ErrDependencySourceNotFound,
}
if !errors.Is(ghostSource, issueops.ErrDependencySourceNotFound) {
t.Fatalf("ghost source does not match ErrDependencySourceNotFound: %v", ghostSource)
}
if errors.Is(ghostSource, issueops.ErrDependencyTargetNotFound) {
t.Error("ghost source also matches ErrDependencyTargetNotFound: the two refusals are not interchangeable")
}
if got := ghostSource.Error(); got != "issue bd-ghost not found" {
t.Errorf("ghost source message = %q, want the endpoint named once", got)
}
missingTarget := &issueops.DependencyEndpointNotFoundError{
IssueID: "bd-real", DependsOnID: "bd-ghost", MissingID: "bd-ghost",
Err: issueops.ErrDependencyTargetNotFound,
}
var recovered *issueops.DependencyEndpointNotFoundError
if !errors.As(fmt.Errorf("add deps[1]: %w", missingTarget), &recovered) {
t.Fatal("DependencyEndpointNotFoundError is not recoverable through a wrap")
}
if recovered.IssueID != "bd-real" || recovered.DependsOnID != "bd-ghost" || recovered.MissingID != "bd-ghost" {
t.Errorf("recovered = %+v, want the refused edge and the absent endpoint", recovered)
}
}
// TestClaimConflictCarriesTheStateThatBeatIt pins the whole point of the typed
// conflict: a caller reports who won by reading FIELDS, and still classifies
// the refusal by sentinel.
func TestClaimConflictCarriesTheStateThatBeatIt(t *testing.T) {
conflict := &issueops.ClaimConflictError{
IssueID: "bd-1",
Assignee: "bob",
Status: issueops.StatusInProgress,
Err: issueops.ErrAlreadyClaimed,
}
if !errors.Is(conflict, issueops.ErrAlreadyClaimed) {
t.Fatalf("ClaimConflictError does not match ErrAlreadyClaimed: %v", conflict)
}
var recovered *issueops.ClaimConflictError
if !errors.As(fmt.Errorf("claim bd-1: %w", conflict), &recovered) {
t.Fatal("ClaimConflictError is not recoverable through a wrap")
}
if recovered.Assignee != "bob" || recovered.Status != issueops.StatusInProgress {
t.Errorf("recovered = %+v, want the observed assignee and status", recovered)
}
// The zero observation is a documented state, not a panic: the refusal
// stands, it just carries nothing.
bare := &issueops.ClaimConflictError{IssueID: "bd-1", Err: issueops.ErrNotClaimable}
if !errors.Is(bare, issueops.ErrNotClaimable) || bare.Error() != issueops.ErrNotClaimable.Error() {
t.Errorf("unobserved conflict = %q, want the bare sentinel", bare.Error())
}
}
type operationsProbe struct{}
func (operationsProbe) Create(context.Context, issueops.CreateRequest) (issueops.CreateResult, error) {
return issueops.CreateResult{}, nil
}
func (operationsProbe) Update(context.Context, issueops.UpdateRequest) (issueops.UpdateResult, error) {
return issueops.UpdateResult{}, nil
}
func (operationsProbe) Close(context.Context, issueops.CloseRequest) (issueops.CloseResult, error) {
return issueops.CloseResult{}, nil
}
func (operationsProbe) Reopen(context.Context, issueops.ReopenRequest) (issueops.ReopenResult, error) {
return issueops.ReopenResult{}, nil
}