beads/internal/storage/hook_dependency_editor_test.go
Julian Knutsen d801ec43dc
feat(issueops): put every issue capability behind one library surface
Beads had two implementations of most issue verbs: one for the direct route
(the CLI talking to a store it opened itself) and one for the proxied-server
route (the CLI talking through a unit of work over a running Dolt server).
They drifted — different commit messages, different transaction boundaries,
different refusals, occasionally different answers.

Seven role interfaces now live in the leaf package issueops: Lifecycle,
Reader, ReadyClaimer, BatchCloser, DependencyEditor, Commenter and Relations.
Each is reached only through a store accessor, never a constructor, because
the accessor is where the hook and telemetry decorators add their layers.
Three implementations back every role — dolt, embeddeddolt, and the
unit-of-work provider the proxied route uses. Both routes call the same role
for close, dep add/remove/list, comment, ready --claim and update.

A depguard rule (httpapi-domain-boundary) denies internal/storage/domain from
internal/httpapi, so a handler cannot reach uw.IssueUseCase() past the roles
and open a second front door one lint-clean commit at a time.

Behaviour users will notice, all in the CHANGELOG:
  - bd close a b c is one Dolt commit, not three, and --claim-next moved
    inside that transaction so it fires only when something landed
  - the open-children guard moved into the transaction and its hand-rolled
    cmd/bd copy is gone, removing a read-then-write window that made
    bd close <child> <parent> depend on argument order
  - bd dep add --file honours deferred version commit
  - embedded bd comment writes the role's message and author
  - dep verb commit messages are the role's on the direct route

Wisp-sourced dependency edges are restored. Moving bd dep add onto the role
had pinned an edge's SOURCE to the issues plane, and the two routes did not
even fail alike: direct refused with "issue <id> not found", proxied threw an
fk_dep_issue violation. The comment justifying the pin claimed the issues
plane was "the only one its unit-of-work sibling can reach" — that was false;
domain.DependencyUseCase declared a full set of wisp twins with no production
caller. All three implementations now route per edge by the edge's own
source. A mixed request stays one transaction: parent-child-first ordering
and the whole-graph cycle gate span both planes, and a refusal rolls back
both. Behind the role, three bulk add verbs collapsed into one source-routed
AddDependencies, so no plane-pinned variant is left to reach for by mistake.

Six Provenance fields with no production caller were deleted rather than
wired — CloseRequest, CloseBatchRequest, ClaimNextRequest, AddCommentRequest,
AddDependenciesRequest, RemoveDependencyRequest. Each role already composes an
id-bearing message, so a caller could only restate the default and make it
unreachable from production. Two are not caller-settable even in principle:
CloseBatchCommitMessage names what LANDED and ClaimNextCommitMessage names the
id the claim WON. UpdateRequest and ReopenRequest keep theirs; commit messages
users see are unchanged.

Verified: build, vet and gofmt clean; golangci-lint 0 issues; protocol corpus
byte-identical under BEADS_PROTOCOL_REQUIRE_DOLT=1; all twelve
DependencyEditor conformance wirings green (four contract functions against
each implementation); TestEmbeddedDepAddWispSource runs bd dep add <wisp-id>
at the front door and was demonstrated RED against the pinned routing first.
Three internal/storage/dolt failures reproduce on pristine origin/main.

Known gaps are enumerated in the PR description rather than left to be found:
comment listing is on no role, bd dep tree and bd dep cycles have no role,
bd create and bd reopen are on Lifecycle only on the direct route, per-edge
line attribution is lost in bd dep add --file, multi-store bd close
--claim-next rides only the first store's batch, and the is_blocked staging gap
(bd-2y9ke, predates this change). Seven of the eight role interfaces now have a
shared conformance contract in backend/conformance, run at all three
implementations; main's Claimer, added in parallel by #5334, is the eighth and
has none yet.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-04 12:27:56 -07:00

162 lines
5.2 KiB
Go

package storage
import (
"context"
"errors"
"reflect"
"testing"
"github.com/steveyegge/beads/issueops"
)
// fakeDependencyEditor answers with a fixed result so the firing rules can be
// checked without a database.
type fakeDependencyEditor struct {
added []issueops.DependencyEdge
removed bool
err error
}
func (f *fakeDependencyEditor) AddDependencies(context.Context, issueops.AddDependenciesRequest) (issueops.AddDependenciesResult, error) {
return issueops.AddDependenciesResult{Added: f.added}, f.err
}
func (f *fakeDependencyEditor) RemoveDependency(context.Context, issueops.RemoveDependencyRequest) (issueops.RemoveDependencyResult, error) {
return issueops.RemoveDependencyResult{Removed: f.removed}, f.err
}
// dependencyEditorStore is a DoltStorage whose only real method is
// DependencyEditor.
type dependencyEditorStore struct {
DoltStorage
editor issueops.DependencyEditor
err error
}
func (s dependencyEditorStore) DependencyEditor() (issueops.DependencyEditor, error) {
return s.editor, s.err
}
// TestHookFiringStoreDependencyEditorLayersHooksOverInner pins the recursion.
// Delegating to the inner store instead would still compile and still satisfy
// Storage, and every guarded edge edit would silently stop firing the update
// hook that AddDependency and RemoveDependency fire.
func TestHookFiringStoreDependencyEditorLayersHooksOverInner(t *testing.T) {
inner := &fakeDependencyEditor{}
store := &HookFiringStore{inner: dependencyEditorStore{editor: inner}}
editor, err := store.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor() error = %v", err)
}
hooked, ok := editor.(*hookDependencyEditor)
if !ok {
t.Fatalf("DependencyEditor() = %T, want *hookDependencyEditor", editor)
}
if hooked.inner != issueops.DependencyEditor(inner) {
t.Fatalf("hook layer wraps %#v, want the inner store's editor", hooked.inner)
}
if hooked.hooks != issueOperationHooks(store) {
t.Fatalf("hook layer fires into %#v, want the decorator itself", hooked.hooks)
}
}
func TestHookFiringStoreDependencyEditorPropagatesInnerError(t *testing.T) {
want := errors.New("inner refused")
store := &HookFiringStore{inner: dependencyEditorStore{err: want}}
editor, err := store.DependencyEditor()
if !errors.Is(err, want) {
t.Fatalf("DependencyEditor() error = %v, want %v", err, want)
}
if editor != nil {
t.Fatalf("DependencyEditor() = %T, want nil", editor)
}
}
// TestHookDependencyEditorFiresOncePerSource pins the de-duplication. A hook
// script is written against one issue, so two edges leaving the same issue are
// one change to it; firing twice would have a script react to a graph it
// already saw.
func TestHookDependencyEditorFiresOncePerSource(t *testing.T) {
edge := func(from, to string) issueops.DependencyEdge {
return issueops.DependencyEdge{IssueID: from, DependsOnID: to, Type: issueops.DepBlocks}
}
for _, tc := range []struct {
name string
editor *fakeDependencyEditor
want []string
}{
{
name: "one edge fires its source",
editor: &fakeDependencyEditor{added: []issueops.DependencyEdge{edge("bd-1", "bd-2")}},
want: []string{"dependency:bd-1"},
},
{
name: "two edges from one source fire once, in request order",
editor: &fakeDependencyEditor{added: []issueops.DependencyEdge{
edge("bd-1", "bd-2"), edge("bd-3", "bd-4"), edge("bd-1", "bd-5"),
}},
want: []string{"dependency:bd-1", "dependency:bd-3"},
},
{
name: "a refused request fires nothing",
editor: &fakeDependencyEditor{
added: []issueops.DependencyEdge{edge("bd-1", "bd-2")},
err: errors.New("boom"),
},
want: nil,
},
} {
t.Run(tc.name, func(t *testing.T) {
recorder := &recordingIssueOperationHooks{}
editor := &hookDependencyEditor{inner: tc.editor, hooks: recorder}
_, _ = editor.AddDependencies(context.Background(), issueops.AddDependenciesRequest{Actor: "worker"})
if !reflect.DeepEqual(recorder.completions, tc.want) {
t.Fatalf("hooks fired = %v, want %v", recorder.completions, tc.want)
}
})
}
}
// TestHookDependencyEditorRemoveFiresOnlyForARemovedEdge pins the idempotent
// case. A removal that found nothing changed no graph, and a hook fired for it
// is a hook a replayed teardown runs on every pass.
func TestHookDependencyEditorRemoveFiresOnlyForARemovedEdge(t *testing.T) {
for _, tc := range []struct {
name string
editor *fakeDependencyEditor
want []string
}{
{
name: "removed edge fires",
editor: &fakeDependencyEditor{removed: true},
want: []string{"dependency:bd-1"},
},
{
name: "no such edge fires nothing",
editor: &fakeDependencyEditor{},
want: nil,
},
{
name: "error fires nothing",
editor: &fakeDependencyEditor{removed: true, err: errors.New("boom")},
want: nil,
},
} {
t.Run(tc.name, func(t *testing.T) {
recorder := &recordingIssueOperationHooks{}
editor := &hookDependencyEditor{inner: tc.editor, hooks: recorder}
_, _ = editor.RemoveDependency(context.Background(), issueops.RemoveDependencyRequest{
Actor: "worker", IssueID: "bd-1", DependsOnID: "bd-2",
})
if !reflect.DeepEqual(recorder.completions, tc.want) {
t.Fatalf("hooks fired = %v, want %v", recorder.completions, tc.want)
}
})
}
}