beads/internal/httpapi/batch_create_test.go
Eddie the Engineer b32798d1bb fix(httpclient): address #7247 relaunch review iteration 1
Review iteration 1 of the relaunched adoption raised six majors. This
commit fixes all six. Five are the majors the first adoption's
iteration 1 raised. Its fixup, 696803f250 (tag
adopt-pr/bd-f5illd/stranded-fixup-696803f250), fixed them but never
reached the branch. That fix is ported here onto the contributor's
current head, with its reasoning unchanged; the sixth is new.

Creates refused what the CLI sends. Every `bd create` carries
CreatedBy, a workspace whose config.yaml names a prefix also carries
IDPrefix, and the client refused both.
- The server stamps created_by from the actor on all three create
  shapes: batch_create.go and batch_apply.go now match create.go.
- The client sends a CreatedBy that names the actor and refuses any
  other value, which the server's stamp would silently replace.
  createIssue and the apply create item share
  refuseUnwirableIssueMembers.
- IDPrefix refuses only where the role would act on it: an explicit,
  unforced id.
- `bd create --file` stays refused over http. Dropping its Status was
  safe but not enough: the batch create still refuses its Provenance
  and Owner. The markdown.go and batchcreator.go comments that called
  it servable now say so.

keysetFilter ignored AfterPriority. It now compares priority first,
mirroring sqlbuild's KeysetPriorityCreatedAtIDPredicate, and a new
served case resumes a priority keyset position. AfterID or AfterPriority
without AfterCreatedAt no longer reads as a zero-instant position that
discarded every row (the AfterID minor).

Claim with a patch was refused on a false ledger claim. `claim`,
`force_assignee_transfer` and `force_close_policy` now ride updateBody
like `force_notes_overwrite`, so a claim with a patch or a version guard
is one updateIssue call. A claim ALONE sent to a server that predates
the member (#6890) falls back to claimIssue. The three ledger rows are
retired, and the served cases parked on them now run.

OpenReadOnly returns a writable store. It stays writable, as a
documented exception: backends.Backend.OpenReadOnly also serves the
root pre-run's classified reads, and `bd ready --claim` writes through
that store. The exception is named in the backends.Backend contract,
the store_factory invariant and the store doc, and
TestReadOnlyOpenServesTheClaimBdReadyMakes pins it. An OpenNonMutating
backend hook is the follow-up.

Security surface.
- http_target.json is in the doctor gitignore template and its required
  patterns.
- BEADS_HTTP_TOKEN and BEADS_HTTP_TOKEN_COMMAND are host-scoped with
  BEADS_HTTP_CA_FILE's "host[:port]=value" syntax. A value scoped to
  another server falls through to the next rung, and a command scoped
  elsewhere never runs.
- LoadTarget and SaveTarget refuse a url that carries userinfo, and
  Target.String drops userinfo.

The served Tier 3 had no CI home. The new target
//internal/httpclient:httpclient_served_test is an `embedded`-tagged
sh_test. It reruns httpclient_test's binary through
tools/bazel/go_test_variant.sh with the script's
BEADS_TEST_EMBEDDED_DOLT=1 and BEADS_HTTP_TEST_REQUIRED=1, in 24 shards
on the embedded lane's race build. bazel.yml's bazel-embedded job
already runs `bazel test //... --config=embedded`, so no workflow
changes.
- -test.timeout=19m, not the script's 40m: Go's own timeout then fires,
  and dumps every goroutine, before the action's 1200s limit.
- TestBazelRetiredLanesCannotBeNarrowed pins its args and env. The new
  TestConformanceServedTierRunsUnderBazel ties the script's Tier 3
  switches to the rule.
- The new untagged TestServedTierIsLinkedWhenRequired fails a required
  run built without cgo, which drops every served file (the sentinel
  minor).
- A first 12-shard run found two test defects; both are fixed.
  TestServedBatchApplyBoundsTheItemCount's 1000-item apply outlasts the
  client's 60s per-request timeout under -race, so a
  race_detector_{on,off}_test.go pair scales its "at bound" half to 150
  there, as embeddeddolt's TestBatchApplyContract does. And
  TestTheCompositionCatchesAWrongParameter's dropped-filter case leaned
  on rows earlier tests had seeded: run alone, or first in its shard,
  it failed. It now seeds its own row outside the scope.
- conformance.sh, its script test and
  engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md name the target.

Deviations from the review's suggested fixes:
- A bare BEADS_HTTP_TOKEN(_COMMAND) fails closed rather than falling
  back, and the error does not echo the value: a bare token split at an
  "=" would put token text into the pattern half. Dropping the bare
  syntax is safe today: the backend has no Register call site and no
  docs/ page names the variables.
- Target.String drops userinfo instead of using url.URL.Redacted, which
  masks only the password, so a token used as the username would print.
- The pre-#6890 claimIssue fallback leaves RowVersion unset instead of
  borrowing the follow-up read's token. That read is a second snapshot,
  so its token could postdate a write this claim never saw and arm a
  guard that should miss; a guard built from the unset token fails
  closed. Against a current server the claim rides updateIssue, whose
  `revision` is stitched as for any update.

Also fixed: the nit about the completeness failure message, which now
says to regenerate the shell, and the offrole.go tombstone comment that
named a skip list which no longer exists. Left for follow-up (all
latent, since httpclient.Register has no production caller): vocabOnce
latching a failed fetch, snapshot holding s.mu across the handshake
dial, unchunked graph anchors, private tracker IDs in park owners, and
the bounded journal limit above 10000.

Validation:
- git diff --check; gofmt -l; go vet over internal/httpclient/... and
  scripts
- go test -tags gms_pure_go over internal/httpapi/...,
  internal/httpclient/..., internal/storage/backends/... and
  backend/conformance/...; the doctor gitignore tests; the cmd/bd
  events-journal construction guard; ./scripts
- bazel test --config=embedded
  //internal/httpclient:httpclient_served_test on the lane's remote
  executors: 24 shards, 702 top-level tests, 632 pass, 70 skip (69
  ledgered KNOWN DIVERGENCE, plus
  TestCAFileHygieneRefusesGroupWritableFile, which finds no
  supplementary group on the executor), 0 fail, 0 data races; slowest
  shard 283s. tools/bazel/check_testcases.py --bep passes.
- each of the 702 tests run alone (go test -run '^Name$', without
  -race): 0 fail
- the whole tier under go test without -race, with the script's
  switches and -timeout=40m, before the two test fixes: 633 pass, 69
  skip, 0 fail
- removing BEADS_HTTP_TEST_REQUIRED from the rule fails both policy
  tests
- bazel test --config=ci over //internal/httpclient/...,
  //internal/httpapi/..., //scripts/..., //test/docsync/...,
  //internal/storage/backends/..., //backend/conformance/... and
  //cmd/bd/doctor/...: 32 tests pass
- make ci-pr-lint: nogo native plus windows/darwin, passes

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 07:47:17 +00:00

325 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 is the role's, 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)
}
// created_by is stamped from the actor on EVERY item, createIssue's rule:
// the role copies the issue's and never stamps one itself.
for i, item := range got.Items {
if item.Issue.CreatedBy != "alice" {
t.Errorf("item %d created_by = %q, want the actor %q", i, item.Issue.CreatedBy, "alice")
}
}
// 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.
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.
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: a nil entry would be a nil dereference in the handler,
// so it is the generic 500 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.
func TestBatchCreateRefusesANonJSONContentType(t *testing.T) {
creator := &roleBatchCreator{}
ts := newBatchCreateServer(t, creator)
resp := ts.postBody(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)