beads/internal/storage/embeddeddolt/reader_contract_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

168 lines
6.2 KiB
Go

//go:build cgo
package embeddeddolt_test
import (
"context"
"testing"
"github.com/steveyegge/beads/backend/conformance"
)
// The Reader contract, wired at the embedded store. This accessor returns the
// same workapi/storereader body the server-backed store's does, so what this
// wiring buys is the ENGINE and the connection wrapper underneath it, not a
// second opinion on the body. It is still worth running: engine-level
// disagreement is the class the embedded wirings have caught before.
func TestEmbeddedReaderReadyDefaultTypeExclusionsYieldToAnExplicitType(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderReadyDefaultTypeExclusionsYieldToAnExplicitType(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderReadyDeferredAndEphemeralGates(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderReadyDeferredAndEphemeralGates(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderReadyLimitBoundary(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderReadyLimitBoundary(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderOffsetIsHonoredOrRefused(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderOffsetIsHonoredOrRefused(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderReadySortPoliciesOrderTheSameRows(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderReadySortPoliciesOrderTheSameRows(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListDefaultExclusionsAndTheirOverrides(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListDefaultExclusionsAndTheirOverrides(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListRejectsATypeOutsideTheWorkspaceVocabulary(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListRejectsATypeOutsideTheWorkspaceVocabulary(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListNaturalNumericIDSortTrimsAfterTheFetch(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListNaturalNumericIDSortTrimsAfterTheFetch(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListLimitBoundaryUnderASortTheDatabaseCanExpress(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListLimitBoundaryUnderASortTheDatabaseCanExpress(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderReadySetOwnsItsStatusPinnedAndTemplateDecisions(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderReadySetOwnsItsStatusPinnedAndTemplateDecisions(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListReadyFlagCarriesTheAssigneeAndPriorityFilters(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListReadyFlagCarriesTheAssigneeAndPriorityFilters(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListStatusAcceptsACommaSeparatedORSet(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListStatusAcceptsACommaSeparatedORSet(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListKeysetPositionResumesTheCreatedDescIDAscOrder(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListKeysetPositionResumesTheCreatedDescIDAscOrder(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListReadyFlagAnswersTheBlockerAwareSet(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListReadyFlagAnswersTheBlockerAwareSet(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListReadyFlagRefusesAFilterItCannotCarry(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListReadyFlagRefusesAFilterItCannotCarry(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderListEmptyPageIsWellFormed(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderListEmptyPageIsWellFormed(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderGetResolvesTheExactIDAcrossBothPlanes(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderGetResolvesTheExactIDAcrossBothPlanes(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderGetMissIsNotFoundAndBackendFailureDoesNotDecay(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderGetMissIsNotFoundAndBackendFailureDoesNotDecay(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderGetOptionalRowListsAreOffByDefault(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderGetOptionalRowListsAreOffByDefault(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderGetDetailShapeMatchesTheSeededIssue(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderGetDetailShapeMatchesTheSeededIssue(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
func TestEmbeddedReaderDoesNotMutateTheCallerRequest(t *testing.T) {
skipUnlessEmbeddedDolt(t)
ctx := t.Context()
conformance.RunReaderDoesNotMutateTheCallerRequest(t, ctx, newEmbeddedReaderFixture(t, "rdr"))
}
// newEmbeddedReaderFixture composes the shared role kit with the reader
// accessor. One environment per case: newTestEnv clones a pristine template
// into the test's own temp dir, which costs a fraction of a second once the
// template exists.
func newEmbeddedReaderFixture(t *testing.T, prefix string) conformance.ReaderFixture {
t.Helper()
te := newTestEnv(t, prefix)
reader, err := te.store.IssueReader()
if err != nil {
t.Fatalf("IssueReader(): %v", err)
}
kit := newEmbeddedRoleFixtureKit(te, prefix)
return conformance.ReaderFixture{
IssuePrefix: kit.IssuePrefix,
Reader: reader,
CreateIssue: kit.CreateIssue,
CreateWisp: kit.CreateWisp,
AddDependency: kit.AddDependency,
// Not on the kit; see the same note in the server-backed wiring.
AddComment: func(ctx context.Context, issueID, author, text string) error {
_, err := te.store.AddIssueComment(ctx, issueID, author, text)
return err
},
}
}