`bd create --file plan.md` had two implementations of one bulk create. The
direct route built types.Issue values in cmd/bd/markdown.go and handed them to
store.CreateIssues; the proxied route parsed the same templates again, opened a
unit of work, read a create context, checked the types itself and looped the
domain use case. Both now build ONE issueops.CreateBatchRequest from one shared
projection, and the difference between the two routes is which accessor answers.
THE ROLE. A batch's REQUEST is its transaction boundary, which is the same
reason BatchCloser exists and cannot be a loop over Lifecycle.Close: N creates
one call at a time are N transactions and N history entries, and the caller who
wants them to land together has no way to say so. It is its own role rather than
a Create overload because it is ALL OR NOTHING where the batch close keeps its
survivors, and one method cannot make both promises. The difference is what has
already happened: a close records work that is done, so a rollback destroys
information the batch was carrying; a create makes the work, so a refused item
leaves no survivors, and half a created plan cannot be re-run without
duplicating the half that landed.
It is born with Provenance, which CreateRequest does not have. Both --file
routes have always written `bd: create N issue(s) from <path>`, and a label
composed from the result cannot spell a file the result does not carry, so
without the field this commit would have caused the regression Q16 recorded
rather than inherited it.
USER-VISIBLE, and bigger than the bead described. The bead and the Q11 batch say
direct --file is non-atomic today - "a bad row can leave earlier rows created".
Reading the path does not support it: markdown.go built every issue first and
made ONE store.CreateIssues call, which both stores run in a single SQL
transaction. What was true is worse, and is recorded as Q25/Q26 in
AMBIGUITIES.md with A-bcr-2:
- a dependency naming an id that does not exist was SILENTLY DROPPED on the
direct route (the batch engine skips a dangling edge and nothing passed
OnSkippedDependency), while the proxied route already failed on it. Every
requested edge is now written or the batch refuses. An `external:` target
and an id belonging to another repository are still written, exactly as
`bd dep add` accepts them.
- --ephemeral, --no-history, --mol-type and --validate were accepted and
IGNORED on the direct route while the proxied route acted on all four, so
`--file --ephemeral` created durable issues locally and wisps on a team
server. All four now mean what they mean everywhere else, templates are
linted on both routes, and created_by/owner are set as the single-issue
create sets them.
- --file --json emits the stored post-create rows on both routes. Additive.
- proxied --file loses its config.yaml fallback for types.custom, the
convergence Q17 already recorded for single-issue create.
--graph is untouched, per Q10: an upsert over a declared shape is a different
contract and gets its own role when it gets one.
CONFORMANCE. backend/conformance/batch_creator_contract.go holds nine cases,
each citing the leaf doc by line, run at all three implementations with ZERO
parks: 27 subtests green. They are TWO independent bodies plus an engine check,
not three - dolt and embeddeddolt share issueops.ExecuteCreateBatch, which
prepares every item and hands the slice to one CreateIssuesInTxWithResult, while
the unit-of-work body creates item by item through the domain use case and keeps
"all or nothing" by returning early and letting the transaction roll back. That
is also why no case asks for a FORWARD reference inside a batch: the store body
could resolve one and the uow body provably cannot, so the role promises only an
edge onto an EARLIER item and the contract says so at the case site (A-bcr-4).
The store-backed body is an InTx function rather than an internal/workapi/store
package, the documented variant EdgeReader introduced: it must assign every id
before any row is written so an item's edge can name a sibling, which cannot be
expressed above storage.DoltStorage. It needs no depguard entry for the same
reason - the shared function takes a transaction, so no front door can call it.
HTTP. POST /v0/beads/issues:batchCreate is new canonical surface, spec written
first and the types generated from it. It is a collection-level custom method
spelled the way ready:count is, which leaves POST /v0/beads/issues free for the
single create that belongs there; it does not collide with the claim row's wide
POST wildcard, because that pattern requires the separating slash
(TestBatchCreateDoesNotShadowTheClaimRoute drives both). BatchCreateResponse
carries no allOf, so the oapi-codegen v2.6.0 pin-dropping trap does not apply,
and its Items are pinned to []types.Issue in pinning.go.
The wire publishes no `id` member, which is what keeps the role's
ErrAlreadyExists off a vocabulary that has no already_exists code (A-bcr-5): the
server assigns every id and `bd import` remains the upsert surface. The role's
ErrValidation is answered by the operation's own failure path rather than by
ClassifyError, because the dangling-edge refusal wraps BOTH ErrValidation and
ErrNotFound and the shared classifier reaches ErrNotFound first - a 404 for a
request that addressed no resource. Neither branch quotes the role's message: a
refused edge arrives as a driver error naming tables and constraints, and 4xx
details here reflect the caller's input.
httpapi.Config's roles source grows to eight roles. The half-set cases that
built a Config by hand now go through rolesConfig, so the next role does not
have to edit six literals that are not about the role set.
Verified: build, vet, gofmt clean; golangci-lint 0 issues; make api-check clean;
TestBatchCreatorContract green at dolt (9/9), embeddeddolt (9/9) and uow (9/9);
./internal/httpapi green including eleven new batch-create tests; TestCorpusGolden
green (the corpus has no --file capture); TestCreateFromMarkdownFile green; the
cmd/bd create suites green with BEADS_TEST_EMBEDDED_DOLT=1 and the proxied
integration create suites green with BEADS_TEST_PROXIED_SERVER=1;
TestProxiedLookup green.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
327 lines
14 KiB
Go
327 lines
14 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
// These cover the transport half of POST /v0/beads/issues:batchCreate — the
|
|
// body shape, the refusals this operation owns, and how a role refusal is named
|
|
// on the wire. Everything below the wire — what lands together, what an edge
|
|
// onto an absent target does, the id assignment — is the role's, and is pinned
|
|
// by backend/conformance/batch_creator_contract.go at all three backends.
|
|
|
|
const batchCreatePath = "/v0/beads/issues:batchCreate"
|
|
|
|
func newBatchCreateServer(t *testing.T, creator *roleBatchCreator) *testServer {
|
|
t.Helper()
|
|
return newTestServer(t, rolesConfig(Config{BatchCreator: creator}))
|
|
}
|
|
|
|
// TestBatchCreatePassesTheRequestToTheRoleAndAnswersWithWhatItCreated pins the
|
|
// projection in both directions: the wire members become the role's request,
|
|
// and the role's issues come back in request order carrying the ids only the
|
|
// server can assign.
|
|
func TestBatchCreatePassesTheRequestToTheRoleAndAnswersWithWhatItCreated(t *testing.T) {
|
|
creator := &roleBatchCreator{}
|
|
ts := newBatchCreateServer(t, creator)
|
|
|
|
resp := ts.claim(t, batchCreatePath, `{"actor":"alice","items":[
|
|
{"title":"first","priority":1,"issue_type":"bug","labels":["api"],
|
|
"dependencies":[{"target_id":"bd-9","type":"blocks"}]},
|
|
{"title":"second","description":"body"}
|
|
]}`)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
items, _ := body["items"].([]any)
|
|
if len(items) != 2 {
|
|
t.Fatalf("items = %v, want the two issues the role created", body["items"])
|
|
}
|
|
for i, want := range []string{"bd-batch-0", "bd-batch-1"} {
|
|
issue, _ := items[i].(map[string]any)
|
|
if issue["id"] != want {
|
|
t.Errorf("items[%d].id = %v, want %q: the generated ids come back in request order", i, issue["id"], want)
|
|
}
|
|
}
|
|
if _, present := body["has_more"]; present {
|
|
t.Error("the response carries has_more; this is not a page and a client must not look for a second one")
|
|
}
|
|
|
|
reqs := creator.createRequests()
|
|
if len(reqs) != 1 {
|
|
t.Fatalf("role calls = %d, want 1", len(reqs))
|
|
}
|
|
got := reqs[0]
|
|
if got.Actor != "alice" {
|
|
t.Errorf("actor = %q, want the one the wire named", got.Actor)
|
|
}
|
|
if len(got.Items) != 2 {
|
|
t.Fatalf("items = %d, want 2", len(got.Items))
|
|
}
|
|
first := got.Items[0]
|
|
if first.Issue.Title != "first" || first.Issue.Priority != 1 || first.Issue.IssueType != types.TypeBug {
|
|
t.Errorf("item 0 = %+v, want the wire's title, priority and issue_type", *first.Issue)
|
|
}
|
|
if first.Issue.Status != types.StatusOpen {
|
|
t.Errorf("item 0 status = %q, want open: a created issue starts open", first.Issue.Status)
|
|
}
|
|
if len(first.Issue.Labels) != 1 || first.Issue.Labels[0] != "api" {
|
|
t.Errorf("item 0 labels = %v, want the wire's", first.Issue.Labels)
|
|
}
|
|
if len(first.Dependencies) != 1 ||
|
|
first.Dependencies[0].TargetID != "bd-9" || first.Dependencies[0].Type != types.DepBlocks {
|
|
t.Errorf("item 0 dependencies = %+v, want the one edge the wire named", first.Dependencies)
|
|
}
|
|
// Absent members are absent, not zero-valued strings the role has to
|
|
// second-guess: an omitted priority is the workspace default.
|
|
if got.Items[1].Issue.Priority != 0 || got.Items[1].Issue.Description != "body" {
|
|
t.Errorf("item 1 = %+v, want the wire's description and no priority", *got.Items[1].Issue)
|
|
}
|
|
// No provenance: the file name a CLI batch spells has no analogue here, so
|
|
// the entry reads as the implementation's default rather than as a lie
|
|
// about where the request came from.
|
|
if got.Provenance != "" {
|
|
t.Errorf("provenance = %q, want empty: this surface names no source file", got.Provenance)
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateRefusesTheShapesTheDocumentRefuses walks the 400s this
|
|
// operation owns. Every one of them is answered BEFORE the role is reached, so
|
|
// a refusal here is also the proof that nothing was created.
|
|
func TestBatchCreateRefusesTheShapesTheDocumentRefuses(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
body string
|
|
wantParam string
|
|
}{
|
|
{"no actor", `{"items":[{"title":"t"}]}`, "actor"},
|
|
{"blank actor", `{"actor":" ","items":[{"title":"t"}]}`, "actor"},
|
|
{"actor with a newline", "{\"actor\":\"alice\\nbd: create\",\"items\":[{\"title\":\"t\"}]}", "actor"},
|
|
{"null actor", `{"actor":null,"items":[{"title":"t"}]}`, "actor"},
|
|
{"no items", `{"actor":"alice"}`, "items"},
|
|
{"empty items", `{"actor":"alice","items":[]}`, "items"},
|
|
{"items is not an array", `{"actor":"alice","items":{"title":"t"}}`, "items"},
|
|
{"unknown top-level member", `{"actor":"alice","items":[{"title":"t"}],"dry_run":true}`, "dry_run"},
|
|
{"unknown item member", `{"actor":"alice","items":[{"title":"t","id":"bd-1"}]}`, "items[0].id"},
|
|
{"unknown edge member", `{"actor":"alice","items":[{"title":"t","dependencies":[{"target_id":"bd-9","type":"blocks","reverse":true}]}]}`, "items[0].dependencies.reverse"},
|
|
{"no title", `{"actor":"alice","items":[{"description":"d"}]}`, "items[0].title"},
|
|
{"blank title", `{"actor":"alice","items":[{"title":" "}]}`, "items[0].title"},
|
|
{"priority out of range", `{"actor":"alice","items":[{"title":"t","priority":9}]}`, "items[0].priority"},
|
|
{"priority is not a number", `{"actor":"alice","items":[{"title":"t","priority":"high"}]}`, "items[0]"},
|
|
{"edge with no target", `{"actor":"alice","items":[{"title":"t","dependencies":[{"target_id":"","type":"blocks"}]}]}`, "items[0].dependencies.target_id"},
|
|
// A value at all, not membership of a list: the edge vocabulary is
|
|
// OPEN, so "nonsense" is a legal type here and only an unstorable one
|
|
// is refused.
|
|
{"edge with an unstorable type", `{"actor":"alice","items":[{"title":"t","dependencies":[{"target_id":"bd-9","type":"` + strings.Repeat("x", types.MaxDependencyTypeLen+1) + `"}]}]}`, "items[0].dependencies.type"},
|
|
{"the second item is the bad one", `{"actor":"alice","items":[{"title":"ok"},{"title":""}]}`, "items[1].title"},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
creator := &roleBatchCreator{}
|
|
ts := newBatchCreateServer(t, creator)
|
|
|
|
resp := ts.claim(t, batchCreatePath, test.body)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["code"] != string(CodeInvalidArgument) {
|
|
t.Errorf("code = %v, want %q", body["code"], CodeInvalidArgument)
|
|
}
|
|
if body["param"] != test.wantParam {
|
|
t.Errorf("param = %v, want %q — a client dispatches on this rather than on the detail", body["param"], test.wantParam)
|
|
}
|
|
if calls := creator.createRequests(); len(calls) != 0 {
|
|
t.Errorf("the role was called %d times for a refused request; nothing may be created", len(calls))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateRefusesAnOversizeBatch pins the one size bound this operation
|
|
// owns. It is a bound on how long a request may hold a write transaction, so it
|
|
// is refused before the role is reached rather than after.
|
|
func TestBatchCreateRefusesAnOversizeBatch(t *testing.T) {
|
|
creator := &roleBatchCreator{}
|
|
ts := newBatchCreateServer(t, creator)
|
|
|
|
items := make([]string, maxBatchCreateItems+1)
|
|
for i := range items {
|
|
items[i] = fmt.Sprintf(`{"title":"issue %d"}`, i)
|
|
}
|
|
resp := ts.claim(t, batchCreatePath, `{"actor":"alice","items":[`+strings.Join(items, ",")+`]}`)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
if body := decodeBody(t, resp); body["param"] != "items" {
|
|
t.Errorf("param = %v, want items", body["param"])
|
|
}
|
|
if calls := creator.createRequests(); len(calls) != 0 {
|
|
t.Errorf("the role was called for an oversize batch")
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateNamesARoleRefusalInTheServersOwnWords is the case
|
|
// failBatchCreate exists for.
|
|
//
|
|
// The absent-target refusal arrives as ErrValidation WRAPPING ErrNotFound, and
|
|
// the shared classifier reaches ErrNotFound first — so without the operation's
|
|
// own failure path this 400 would be a 404 for a request that addressed no
|
|
// resource at all. The detail is the server's own sentence: the role's carries
|
|
// a driver error naming tables and constraints, and 4xx details here reflect
|
|
// the caller's input rather than server internals.
|
|
func TestBatchCreateNamesARoleRefusalInTheServersOwnWords(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
err error
|
|
wantDetail string
|
|
}{
|
|
{
|
|
name: "an absent edge target is a 400, not a 404",
|
|
err: fmt.Errorf("create batch item 1: %w: create: dependency target does not exist: "+
|
|
"Error 1452 (23000): Cannot add or update a child row: a foreign key constraint fails "+
|
|
"(`beads`.`dependencies`): %w", storage.ErrValidation, storage.ErrNotFound),
|
|
wantDetail: "a dependency target names no issue in this workspace; nothing was created",
|
|
},
|
|
{
|
|
name: "a content refusal is a 400",
|
|
err: fmt.Errorf("create batch item 0: %w: create: invalid issue type", storage.ErrValidation),
|
|
wantDetail: "an item was refused by this workspace's own validation; nothing was created",
|
|
},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
ts := newBatchCreateServer(t, &roleBatchCreator{err: test.err})
|
|
|
|
resp := ts.claim(t, batchCreatePath, `{"actor":"alice","items":[{"title":"t"}]}`)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["code"] != string(CodeInvalidArgument) {
|
|
t.Errorf("code = %v, want %q", body["code"], CodeInvalidArgument)
|
|
}
|
|
if body["detail"] != test.wantDetail {
|
|
t.Errorf("detail = %v, want %q", body["detail"], test.wantDetail)
|
|
}
|
|
if detail, _ := body["detail"].(string); strings.Contains(detail, "foreign key") {
|
|
t.Error("the detail quotes the storage error; 4xx details reflect the caller's input, not server internals")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateReportsANonValidationRoleFailureAsTheGenericFailure pins the
|
|
// other half of failBatchCreate: an error that is not the role's own refusal
|
|
// still goes through the shared classifier, so contention and outages keep the
|
|
// codes every other route uses.
|
|
func TestBatchCreateReportsANonValidationRoleFailureAsTheGenericFailure(t *testing.T) {
|
|
ts := newBatchCreateServer(t, &roleBatchCreator{err: errors.New("connection reset by the void")})
|
|
|
|
resp := ts.claim(t, batchCreatePath, `{"actor":"alice","items":[{"title":"t"}]}`)
|
|
if resp.StatusCode != http.StatusInternalServerError {
|
|
t.Fatalf("status = %d, want 500: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
if body := decodeBody(t, resp); body["code"] != string(CodeInternal) {
|
|
t.Errorf("code = %v, want %q", body["code"], CodeInternal)
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateRefusesACreatorThatAnswersWithNothing pins checkedBatchCreator.
|
|
//
|
|
// The response body carries issue VALUES where the role answers with pointers,
|
|
// so a nil entry is a nil dereference in the handler. It is the generic 500
|
|
// with the fault in the log — not a 404, because nothing here knows an issue is
|
|
// missing — and above all not a panic.
|
|
func TestBatchCreateRefusesACreatorThatAnswersWithNothing(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
issues []*types.Issue
|
|
}{
|
|
{"a nil entry", []*types.Issue{nil}},
|
|
{"fewer issues than items", []*types.Issue{}},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
ts := newBatchCreateServer(t, &roleBatchCreator{issues: test.issues})
|
|
|
|
resp := ts.claim(t, batchCreatePath, `{"actor":"alice","items":[{"title":"t"}]}`)
|
|
if resp.StatusCode != http.StatusInternalServerError {
|
|
t.Fatalf("status = %d, want 500: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
assertNoPanic(t, ts)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateRefusesANonJSONContentType pins the CSRF control this
|
|
// operation inherits from the claim: a JSON content type is not CORS-"simple",
|
|
// so a cross-origin write always triggers a preflight this server never
|
|
// approves. It is the second write on this surface and the first one that
|
|
// creates rows, so accepting text/plain here would be strictly worse than
|
|
// accepting it there.
|
|
func TestBatchCreateRefusesANonJSONContentType(t *testing.T) {
|
|
creator := &roleBatchCreator{}
|
|
ts := newBatchCreateServer(t, creator)
|
|
|
|
resp := ts.claimRequest(t, batchCreatePath, "text/plain", `{"actor":"alice","items":[{"title":"t"}]}`)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
if body := decodeBody(t, resp); body["param"] != "Content-Type" {
|
|
t.Errorf("param = %v, want Content-Type", body["param"])
|
|
}
|
|
if calls := creator.createRequests(); len(calls) != 0 {
|
|
t.Error("the role was called for a request that skipped the preflight")
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateRefusesAQueryParameter pins that the document-level
|
|
// unknown-parameter rule reaches this operation too: it declares no parameters,
|
|
// so every query key is refused.
|
|
func TestBatchCreateRefusesAQueryParameter(t *testing.T) {
|
|
ts := newBatchCreateServer(t, &roleBatchCreator{})
|
|
|
|
resp := ts.claim(t, batchCreatePath+"?dry_run=true", `{"actor":"alice","items":[{"title":"t"}]}`)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["param"] != "dry_run" || body["reason"] != string(ReasonUnknownParameter) {
|
|
t.Errorf("param/reason = %v/%v, want dry_run/unknown_parameter", body["param"], body["reason"])
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateIsNotReachableByOtherMethods pins that the collection custom
|
|
// method is a POST and nothing else, so a GET of it is the catch-all's answer
|
|
// rather than a listing.
|
|
func TestBatchCreateIsNotReachableByOtherMethods(t *testing.T) {
|
|
ts := newBatchCreateServer(t, &roleBatchCreator{})
|
|
|
|
resp := ts.get(t, batchCreatePath)
|
|
if resp.StatusCode == http.StatusOK {
|
|
t.Fatalf("GET %s = 200; the custom method is a POST", batchCreatePath)
|
|
}
|
|
}
|
|
|
|
// TestBatchCreateDoesNotShadowTheClaimRoute pins the one routing risk the new
|
|
// path carries: both are POSTs under /v0/beads/issues, and the claim's pattern
|
|
// is a wildcard wide enough to have swallowed this one had the separating slash
|
|
// not been there.
|
|
func TestBatchCreateDoesNotShadowTheClaimRoute(t *testing.T) {
|
|
issues := &fakeIssues{issue: seededIssue("bd-1", "alice", types.StatusInProgress)}
|
|
ts, _ := newClaimServer(t, issues)
|
|
|
|
resp := ts.claim(t, "/v0/beads/issues/bd-1:claim", `{"actor":"alice"}`)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("claim status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
}
|
|
|
|
var _ issueops.BatchCreator = (*roleBatchCreator)(nil)
|