* feat(backend/http): add the public HTTP backend door, cmd/bd registration, and bd connect internal/httpclient (S1-S5) had no production caller for the per-open backends.OpenOptions.Credential seam and no way for a workspace to actually select it. This slice (S6) closes both gaps in OSS's own idiom, without carrying over anything from bd-enterprise's gateway/box-login/storage-profile machinery: - backend/http (bdhttp) is the public, out-of-tree extension door: Register adds "http" to the backend registry and installs its transport, Open/ Handshake dial a server directly with no workspace on disk, and the registered OpenWith hook threads a per-call credential through beads.OpenBestAvailableWith for a multi-tenant embedder. - internal/httpclient/open_with.go is the backends.Backend.OpenWith hook itself: a fresh dial per open (opts.Credential/HTTPClient/UserAgent), distinct from the process-wide registered dialer Open/OpenReadOnly use. - cmd/bd/http_backend.go registers the backend for the OSS binary, so a workspace whose metadata.json names "http" opens through the ordinary OpenBestAvailable path; any other unregistered backend name still refuses. - cmd/bd/connect.go adds the minimal `bd connect <url>` command: a handshake that must succeed before anything is written, a per-user activation sidecar (.beads/http_target.json, never git-tracked), a plaintext-to- non-loopback refusal without --allow-plaintext, and a backend-switch refusal without --force. No credential is ever accepted on the command line or written to disk. - cmd/bd/doctor/gitignore.go requires the new http_target.json / http_local_metadata.json sidecars be git-ignored, the same way the proxied-server client info already is. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(storage): keep external-dependency blocking over http unless the server enforces it bd ready / bd close / claim hard-failed against the new http backend with "GetAllDependencyRecords not supported by the http backend": cmd/bd's storage chain wraps every opened store in the externaldeps policy decorator, which needs either the narrow ExternalDependencyQueryStore interface or the broad GetAllDependencyRecords fallback to resolve blocking state, and the http client implemented neither. The decorator now stays on for a remote store. It is skipped only when the server's handshake advertises policy.external_dependencies (the server enforces the policy before answering). Without that capability the client resolves blocking itself from the listing and chunked dependency reads, so an issue with an unresolvable external: dependency stays out of bd ready exactly as it does on a local backend; it is never silently skipped (DESIGN 3.6). Server-side enforcement is slice S10. storage.RemoteBackendStore marks a DoltStorage that is a pure network client of a remote bd serve; the http backend's Store satisfies it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(cmd/bd): suppress the CreatedBy stamp on create against a remote backend bd create failed against the http backend with "Issue.CreatedBy: ... is not expressible on the v0 wire": the http client's create role deliberately refuses any POPULATED CreatedBy/CreatedAt (the creation stamp belongs to the server's own journal entry, not a value the caller supplies), but cmd/bd/create.go unconditionally stamped getActorWithGit() onto every created issue regardless of backend. The refusal only fires on a non-zero field, so suppressing the stamp for a registered remote backend is wire- compatible by construction, not a workaround. Add isRemoteBackendWorkspace()/creationActorStamp() to backend_support.go, reusing the existing resolveCommandBeadsDir(dbPath) + backendNameForErrorFraming(beadsDir) + backends.IsRemote(name) pattern already used elsewhere in that file, and use creationActorStamp() at both create.go call sites (the dry-run preview and the real create). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * test(backend/http): add an embedder linkage test over public packages only Prove the one promise S6 makes to an out-of-tree embedder (gc is the motivating case): importing nothing but github.com/steveyegge/beads, .../backend, and .../backend/http is enough to register the http backend, open a workspace through it with a per-call credential (beads.OpenBestAvailableWith), and read through the role-accessor surface (IssueReader().Ready) against a stand-in server — with no internal/... import in the test file, which is itself the thing under test. Written fresh for OSS beads S6; no bd-enterprise source copied. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * docs: document the HTTP backend, bd connect, and the public backend/http door Add a README section with bd connect usage, the credential ladder, and the plaintext/force refusals, plus a CHANGELOG entry for the new cmd/bd registration, bd connect command, and the public backend/http package. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(httpclient): address bee-ghosttrack review on #7288 (connect --clear restore, persisted --allow-plaintext) Closes the CHANGES_REQUESTED items on the HTTP backend door PR: - Must-fix: `bd connect --force` over a workspace with its own local (dolt) identity now has writes succeed instead of tripping "workspace identity mismatch detected" — validateWorkspaceIdentity (cmd/bd/main.go) and the sidecar (internal/httpclient/metadata.go) now compare against the http backend's own pinned project id. New e2e regression test TestE2E_ForceConnectWritesSucceedDespiteDifferingLocalProjectID (backend/http/e2e_cli_test.go), confirmed to fail without the fix. - Should-fix 1: `bd connect --clear` now restores metadata.json's backend selection to whatever it was before this workspace connected, instead of leaving it pinned to "http" with no sidecar to dial. Target.PreviousBackend (internal/httpclient/target.go) is recorded by Attach (backend/http/attach.go) and round-tripped through the sidecar; connect.go reads it back on --clear. New tests in backend/http/attach_test.go and internal/httpclient/target_test.go. - Should-fix 2: `bd connect --allow-plaintext` is now remembered in the sidecar (Target.AllowInsecureCredential), scoped to the target it was granted for, so every later ordinary command's dial honors the grant without also needing BEADS_HTTP_ALLOW_INSECURE=1 set (internal/httpclient/dial.go). Fixed BearerProvider.warnInsecure's wording (internal/httpclient/credential.go), which used to assert a token was being sent immediately above a refusal saying the opposite. New test TestDialWithWiresTargetAllowInsecureCredentialThroughToTheGuard. - Should-fix 3: README no longer claims HTTP mode works "exactly as with a local database" — it lists what round-trips and points at the divergence ledger (engdocs/design/http-divergence-ledger.md) for what differs. - Verified, no code change needed: Attach already handles an existing metadata.json project_id coherently (only cfg.Backend is touched); bd show's isNotFoundErr already matches a http 404 (issueops.ErrNotFound is storage.ErrNotFound, the same sentinel a local miss uses), so it reads as "not found" rather than an unsupported-operation error. - Doc fix: guardInsecureCredential's doc comment and refusal message were stale against should-fix 2's own change — they named only DialOptions.AllowInsecureCredential and the env var as ways to get past the guard, not the newly-persisted target.AllowInsecureCredential or `bd connect --allow-plaintext` itself. Gates run clean on this branch: go build/vet, go test over internal/httpclient, backend/http (including the full e2e suite under BEADS_TEST_EMBEDDED_DOLT=1) and cmd/bd, make api-check, make ci-pr-lint, make bazel-sync-check, bazel run //:gazelle -- -mode=diff, and internal/storage's TestEveryLegWiresEveryRoleContract. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * test(backend/http): give the e2e server the BatchGetter role #7248 added to the required set Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix: exact http ready totals, connect capability row, doc corrections Ready totals over http (review major). externaldeps drops externally blocked rows client-side for a store that cannot express ExcludeIDs (today httpclient.Store). dropBlockedIssuesWithCounts compacted the fetched rows in place (`kept := rows[:0]`), and readyWorkWithCountsAndTotal then re-scanned `rows` to count the blocked rows it had dropped. By then the compaction had overwritten them, so whenever a blocked row came back ahead of a kept one the re-scan undercounted the drops: `bd ready --json`'s total and text-mode `bd ready`'s count (CountReadyWork) overstated ready work against any server that does not advertise policy.external_dependencies. Both drop helpers now copy into a fresh slice, and dropBlockedIssuesWithCounts reports how many rows it dropped, so nothing re-scans `rows`. fakeStore gains an excludeIDsUnsupported switch that, like httpclient.Store, refuses any ExcludeIDs. The new TestReadyTotalsWhenStoreCannotExpressExcludeIDs puts the blocked issue first and checks the page and total for an unlimited filter, a Limit whose bumped window holds the whole set, and one the set outgrows, then CountReadyWork. TestServedReadyExcludesUnsatisfiedExternalDependencyWithoutServerCapability now also checks the total and count against a real in-process bd serve. Against the previous decorator the unit test's unlimited case reports total and count 3 beside a 2-row page, and the served test 2 beside a 1-row page. Review minors, all wording: - `bd connect --clear` pointed at `bd config set backend dolt`, which writes the database's config table rather than metadata.json and, in the state that prints the advice (metadata.json says http, sidecar gone), fails the pre-open identity check. It now says to reconnect, or to set "backend" in metadata.json. - CHANGELOG described the superseded skip-at-wrap design; it now describes the always-wrap decorator with its capability probe and client-side drop. - RemoteBackendStore's docs no longer name a consumer; nothing in-tree checks it. - creationActorStamp's comment pointed at backend/http/lifecycle.go; the refusal lives in internal/httpclient/lifecycle.go. Capability row. TestProxyCapabilityRegistryCoversCommandTree fails at 60b7128204: the command tree walk finds `connect`, which this PR adds, with no row in cmd/bd/capability_registry.go ("1 command(s) have no proxied-server capability row: connect"). The test predates the PR (#6582 is an ancestor of its base) and passes at the base dabf20ef07. connect opts out of store init (skipStoreAnnotation) and never reaches the store factory, so it belongs in proxyPermittedPaths beside init and setup. The row is runtime-neutral: validateProxyRegistryBeforeProvider returns nil both for a permitted row and for a path with no row, so this only records the policy decision the coverage test asks for. 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> * docs(httpclient): regenerate the divergence ledger doc after the S5 rebase The rebase onto feat/http-client-batch-apply's rebased head (S3/S4 merged to main) left the ledger doc's summary line at a conflict-resolution placeholder count. Regenerate it from internal/httpclient/encode.Ledger() so TestDivergenceLedgerDocMatchesLedger passes against the merged ledger.go. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(backend/http): host-scope S6's test BEADS_HTTP_TOKEN values Rebasing onto feat/http-client-batch-apply's rebased head pulled in main's #7247 review-fix round, which made BEADS_HTTP_TOKEN/BEADS_HTTP_TOKEN_COMMAND host-scoped (host[:port]=value, scopedEnvValue/parseScopedEnv in internal/httpclient/credential.go) after S6's own tests were written against the unscoped form. Five S6 tests across backend/http/open_test.go, backend/http/register_test.go and internal/httpclient/insecure_credential_guard_test.go set a bare token and now hit the new ladder's "an unscoped token would be used for whatever server..." refusal. Scope each to the test server's actual host (127.0.0.1 for the httptest.Server-backed recording servers, nonLoopbackTestHost for the insecure-credential-guard integration tests) so they exercise the same ladder rung as before. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * build(bazel): resync go_srcs/repo_files for the new backend/http and test/embedder packages make bazel-sync-check failed after the rebase: tools/bazel/go_srcs.py's managed repo_files filegroups for backend/http and test/embedder (new in this slice) hadn't been generated, and the root BUILD.bazel / tools/bazel's release_cross_build target list hadn't picked up the new package either. Regenerate with `python3 tools/bazel/go_srcs.py`; `bazel run //:gazelle -- -mode=diff` reports no further drift. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(cmd/bd): route swarm's create paths through creationActorStamp after main's currentActor change Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix: police http's served roles, refuse close --claim-next, exact totals External-dependency policy over http (review blocker). cmd/bd wraps every store, http included, in the externaldeps decorator, and its role accessors rebuilt each role over legacy methods httpclient.Store refuses (SearchIssuesWithCounts, GetDependencyTree, ClaimReadyIssue, IsBlocked, GetDependencyRecordsForIssues, BatchCloserWithPolicy). Over http `bd list`, `bd dep tree`, `bd ready --claim` and a policed `bd close` therefore exited 1, even in a workspace with no external dependency. Over a store that reports storage.RemoteBackendStore, each accessor now starts from the inner store's served role and layers the policy around it (remote_roles.go): - IssueReader serves List and Get from the served reader; Ready and a ready-flagged List go through the policy reader. - IssueClaimer refuses an externally blocked claim before the served one. - ReadyClaimer uses the served claim-next while no issue, closed ones included, holds an unsatisfied external ref. Otherwise it claims down the policy's filtered ready page through the served Claimer, because claimNext cannot exclude ids. - BlockingAnnotator and TreeWalker merge external blockers into, or add external leaves to, the served answer. They read only the named issues' edges through the served EdgeReader, 100 anchors per call. - BatchCloser refuses externally blocked open items and sends the rest to the served closer as one request. --force keeps the base path, and a next claim is refused whole (below). RemoteBackendStore now only chooses this composition. Whether the policy runs is still ServerEnforcesExternalDependencyPolicy's answer (design 3.6), and the storage and httpclient docs say so. remote_roles_test.go drives each role over a store that refuses the legacy methods. TestServedRolesKeepTheExternalDependencyPolicy runs every role over a real http server. TestE2E_BDOverHTTP adds `bd list` (JSON, text and --deps), `bd dep tree`, `bd ready`, `bd list --ready`, `bd ready --claim`, a refused `bd close --claim-next`, and a refused then forced close of an externally blocked issue. At the fixup before the composition, the role tests then written (bar one pin of the next-claim refusal, since replaced) and the e2e's list, dep tree, claim and close steps of that round were red. Close with a next claim over http (review major). batchCloseIssues has no member for a next claim, so httpclient refuses any ClaimNext before the dial (ledger W-CloseBatchRequest.ClaimNext). Over http `bd close --claim-next` therefore always exited 1 and closed nothing. With an external ref anywhere in the workspace (a closed holder or --force included) the decorator's base path failed first, with an opaque `BatchCloserWithPolicy` unsupported. remoteBatchCloser now refuses a next claim whole, before it reads or closes anything, and the error says nothing closed: close without --claim-next, then run `bd ready --claim`. A request that is invalid on every backend is still ErrValidation first, and a server that enforces the policy itself gets the request unchanged. Composing close-then-claim was not taken. The role closes and claims in one transaction and answers with an error or with outcomes, never both, so a claim that failed after the closes committed could not report them; httpclient refuses the same request for that reason. A CLI-level close then claim that can report a partial result is the follow-up. The review also suggested leaving closed holders out of the blocking state. They stay in, as on every backend: a closed issue is never a claim candidate, so they change no claim, and leaving them out would cost a status read per holder because the wire has no batch read. TestRemoteBatchCloserRefusesANextClaim covers an open holder, a closed one, none, --force, and a server that enforces the policy; with the previous remote_roles.go the first four are red. TestRemoteBatchCloserValidatesANextClaimFirst keeps ErrValidation ahead of the refusal. Dropping the filter check, or loading the blocking state before refusing, is each caught. TestE2E_BDOverHTTP runs `bd close --claim-next` while the holder is open and again after it is closed; with the previous remote_roles.go both steps are red and print `operation "BatchCloserWithPolicy" not supported`. Ledger F-close no longer says --claim-next is served, and no longer names a renderEscapedHTTPRefusal that does not exist. L14 says the decorator's claim leg applies while any issue, closed or not, holds an unsatisfied ref. The README and CHANGELOG mention the refusal, and the decorator's IsBlocked, IsBlockedBatch and CloseIssueChecked say they refuse over a store that serves its roles. Ready totals with external blockers (review majors, both rounds). CountReadyWork asks for Limit=0, and readyWorkWithCountsAndTotal took any Limit<=0 to mean "the window held the whole ready set". Over http a zero limit never reaches the wire: the server answers with its default page (workapi.DefaultReadyLimit, 100 rows) while the bridge reports the full total, so past 100 ready issues text-mode `bd ready` kept counting externally blocked issues beyond the first page. Exactness is now judged from the rows that came back (total <= len(rows)). A window that holds only part of the set no longer subtracts every externally blocked id in the workspace either: that set includes closed issues and ones the filter excludes, none of them in the total, so the count came out low. It refetches the filtered ready set, ids only, and subtracts the blocked rows in it; the old bound is left as the fallback for a refetch that comes back truncated too. fakeStore gains the bridge's shape: a defaultPage for a zero Limit, with GetReadyWorkWithCountsAndTotal still totalling the whole set. TestReadyTotalsWhenAnUnlimitedFetchComesBackAsOnePage (a blocked issue past the first page) and TestReadyTotalsIgnoreExternallyBlockedIssuesOutsideTheReadySet (a blocked issue outside the ready set) have served counterparts, TestServedReadyCountExcludesAnExternallyBlockedIssuePastTheDefaultPage and TestServedReadyTotalIgnoresAnExternallyBlockedIssueOutsideTheFilter (an assignee filter). The first pair is red on the Limit<=0 condition, the second on the subtract-every-blocked-id branch. Docs: ledger L8 now says which decorator paths take the chunked GetAllDependencyRecords scan and which read only named edges, and L14 records the decorator's composed claim leg; http-divergence-ledger.md is regenerated. The README names the verified commands, and the README and CHANGELOG say how cross-repo dependencies behave over http. Minors from the reviews: - Drop creationActorStamp/isRemoteBackendWorkspace and restore the base stamps at all 11 sites. The arm guarded a refusal the http create role does not make: since #7247 (actorStampedCreateMember) it accepts a CreatedBy naming the actor. Because the arm keyed on the CWD workspace, a create routed from an http workspace into a local one (--repo) stored an empty created_by. TestE2E_BDOverHTTP checks created_by for a direct and a routed create; the routed check is red with the arm restored. The CHANGELOG sentence describing the arm goes with it. - bdhttp.Attach decides a reconnect from the raw metadata.json backend. GetBackend() reads an unregistered "http" as the dolt default, so in an embedder that never called Register a reconnect recorded dolt as the backend --clear restores. The reconnect test now seeds postgres (TestAttachOnAReconnectKeepsTheEarlierPreviousBackend) and is red with the old check. - README, CHANGELOG and `bd connect --help` document the host-scoped BEADS_HTTP_TOKEN=host[:port]=<token> form (and _COMMAND), and say why a bare value is refused. - bdhttp.Attach also gitignores http_local_metadata.json, the per-user file the store writes on first use, so an embedder activating through Attach alone gets the coverage `bd connect` already had. TestAttachGitignoresBothPerUserFiles pins it. - ServerSnapshot.WireRevision's doc names the handshake's actual check instead of a comparison `bd connect` never makes. 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> * test(backend/http): give the e2e server the LeaseReclaimer role #7412 added to the required set main's #7412 made LeaseReclaimer part of httpapi's required role set and updated main's own binders, but backend/http's e2eServeConfig predates it, so every TestE2E_* that binds the in-process server failed with "httpapi: no database source". Same shape as e63f7da36a for BatchGetter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(externaldeps): lead the --claim-next refusal with the wire fact; pin the L14 leg and connect --clear Review i4 minors/nits on the stranded fixup, carried as a separate commit: - remoteBatchCloser's --claim-next refusal now says the served batch close cannot carry a next claim (the real cause), rather than blaming externally blocked work a workspace may not have. The test asserts the reason, that nothing closed, and the remedy. - GetDependencyTree's doc says a served store refuses its reads and that bd dep tree goes through remoteTreeWalker. - README no longer gives `bd ready --claim` local semantics everywhere; it names when ledger row L14's two-step claim applies. - TestE2E_BDOverHTTP claims once more after the external-ref holder is closed, pinning L14's composed leg (a mutant whose composed leg claims nothing turns only that step red). - New TestE2E_ConnectClearRestoresThePreviousBackend: --clear restores the prior dolt backend with its identity intact, removes the sidecar, and a second --clear changes nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend/http): run the served e2e under bazel //backend/http:http_test declared no bd binary, so under bazel the e2e files (bazeltest.PrebuiltBD) failed on BEADS_TEST_BD_BINARY with BEADS_HTTP_TEST_REQUIRED=1 and only "passed" by skipping otherwise. It now takes //cmd/bd:bd_for_tests (visibility added), size large / timeout long: the served cases run 4-5 minutes on the farm under BEADS_TEST_EMBEDDED_DOLT=1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(backend/http): run the e2e tier in the embedded lane as http_served_test The previous commit gives //backend/http:http_test the bd binary, but no lane sets BEADS_TEST_EMBEDDED_DOLT for it, so its TestE2E_* cases still skipped in CI. http_served_test runs the same test binary through //tools/bazel:go_test_variant.sh with BEADS_TEST_EMBEDDED_DOLT=1 and BEADS_HTTP_TEST_REQUIRED=1 (a skip fails it). It is tagged embedded, so the --config=embedded lane runs it and prcore/ci keep their current shape, the same split as //internal/httpclient:httpclient_served_test. Under plain --config=embedded all 8 TestE2E_* cases run, none skipped. TestBazelRetiredLanesCannotBeNarrowed pins every embedded-lane target's args and env; the new target is added to that pin list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(externaldeps): cap the ready rows kept, not the bumped http window An http store cannot carry ExcludeIDs on listReadyWork, so readyExclusion fetches the unfiltered ready set, widened by len(blocked) under a limit, and drops the externally blocked rows client-side. The http ready bridge enforced MaxRows on the rows it fetched rather than the rows kept. So `bd ready --limit N --max-rows N` exited 2 whenever more than N issues were ready and any workspace issue held an unsatisfied external blocker (a closed one counts), even with no blocked row in the window, and a blocked row in the page counted against the cap although the caller never sees it. Locally neither can happen: issueops enforces the cap on the filtered page it delivers, after the Limit trim (finishReadyWorkWithCounts). The client-side path now takes MaxRows off the inner query and enforces the caller's cap on the rows it keeps, after the trim, in all three listings. The inner query keeps the cap whenever nothing is dropped client-side. MaxRows never rode the wire, so the fetch is the size it was; refetchedReadyTotal already clears the cap on its own over-fetch. TestServedReadyMaxRowsCountsTheRowsKept runs the decorator over a served http store holding three free issues and one externally blocked one: a cap equal to the limit, a cap under a wider limit, an unlimited listing, and a kept page wider than the cap, which must still return ErrTooManyRows with the caller's cap and source. Each case runs through GetReadyWork, GetReadyWorkWithCounts and GetReadyWorkWithCountsAndTotal. Leaving the cap on the inner query fails the first three cases in all three methods, dropping the re-applied cap fails the fourth, and enforcing it before the Limit trim fails the first. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
1059 lines
46 KiB
Go
1059 lines
46 KiB
Go
//go:build cgo
|
|
|
|
// Written fresh for OSS beads S6 (no bd-enterprise source copied). The
|
|
// in-process server wiring mirrors internal/httpclient/served_harness_test.go
|
|
// (itself under provenance there), reused here as a black-box harness: this
|
|
// file never imports the wire client, it shells out to a real `bd` binary so
|
|
// the CLI dispatch (cmd/bd/http_backend.go's Register call, `bd connect`,
|
|
// `bd update --if-revision`'s exit code contract) is what gets exercised, not
|
|
// the internal client package.
|
|
package bdhttp_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/httpapi"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/domain"
|
|
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
|
|
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
|
)
|
|
|
|
// e2eProjectID is the identity the in-process server publishes; `bd connect`
|
|
// reads it back off the handshake and pins the workspace to it.
|
|
const e2eProjectID = "proj-e2e-s6"
|
|
|
|
const e2eDatabase = "httpe2e"
|
|
|
|
// TestE2E_BDOverHTTP drives the real `bd` binary against an in-process `bd
|
|
// serve`, end to end: connect, create, an update guarded by --if-revision
|
|
// (both the matching case and the stale case, which must exit 13 with the
|
|
// precondition_failed body), close, dep add, ready, count, list (JSON, text
|
|
// and --deps), an external dependency through dep tree, ready, list --ready,
|
|
// ready --claim, a refused close --claim-next and a refused then forced close,
|
|
// a claim after that close, and delete.
|
|
func TestE2E_BDOverHTTP(t *testing.T) {
|
|
skipUnlessEmbeddedDolt(t)
|
|
bin := buildBD(t)
|
|
|
|
addr := startE2EServer(t)
|
|
workspace := t.TempDir()
|
|
|
|
connect := runBD(t, bin, workspace, nil,
|
|
"connect", "http://"+addr, "--expect-project-id", e2eProjectID, "--json")
|
|
if connect.code != 0 {
|
|
t.Fatalf("bd connect failed (exit %d): stdout=%s stderr=%s", connect.code, connect.stdout, connect.stderr)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(workspace, ".beads", "http_target.json")); err != nil {
|
|
t.Fatalf("bd connect did not write the activation sidecar: %v", err)
|
|
}
|
|
gitignore, err := os.ReadFile(filepath.Join(workspace, ".beads", ".gitignore"))
|
|
if err != nil {
|
|
t.Fatalf("bd connect did not ensure a .beads/.gitignore: %v", err)
|
|
}
|
|
if !strings.Contains(string(gitignore), "http_target.json") {
|
|
t.Errorf(".beads/.gitignore does not cover http_target.json: %s", gitignore)
|
|
}
|
|
|
|
// create, under a named actor: bd stamps CreatedBy with the same actor it
|
|
// sends, which the http create role accepts and the server records.
|
|
const creator = "e2e-creator"
|
|
asCreator := []string{"BEADS_ACTOR=" + creator}
|
|
create := runBD(t, bin, workspace, asCreator, "create", "A title", "-d", "a description", "--json")
|
|
if create.code != 0 {
|
|
t.Fatalf("bd create failed (exit %d): stdout=%s stderr=%s", create.code, create.stdout, create.stderr)
|
|
}
|
|
id := firstJSONField(t, create.stdout, "id")
|
|
if id == "" {
|
|
t.Fatalf("bd create --json produced no id: %s", create.stdout)
|
|
}
|
|
|
|
// read back the revision bd show reports, so the update guard below
|
|
// names a token the row actually carries.
|
|
show := runBD(t, bin, workspace, nil, "show", id, "--json")
|
|
if show.code != 0 {
|
|
t.Fatalf("bd show failed (exit %d): stderr=%s", show.code, show.stderr)
|
|
}
|
|
revision := firstJSONField(t, show.stdout, "revision")
|
|
if revision == "" {
|
|
t.Fatalf("bd show --json reported no revision: %s", show.stdout)
|
|
}
|
|
if got := firstJSONField(t, show.stdout, "created_by"); got != creator {
|
|
t.Errorf("created_by = %q after an http create, want the creating actor %q: %s", got, creator, show.stdout)
|
|
}
|
|
|
|
// The same create routed from this http workspace into a local one
|
|
// (--repo) is written by the local store, which records whatever stamp
|
|
// the command built: it must still be the actor, since nothing about the
|
|
// workspace a command runs in changes who created the issue.
|
|
local := t.TempDir()
|
|
routed := runBD(t, bin, workspace, asCreator, "create", "Routed", "--repo", local, "--json")
|
|
if routed.code != 0 {
|
|
t.Fatalf("bd create --repo %s failed (exit %d): stdout=%s stderr=%s", local, routed.code, routed.stdout, routed.stderr)
|
|
}
|
|
routedID := firstJSONField(t, routed.stdout, "id")
|
|
if routedID == "" {
|
|
t.Fatalf("bd create --repo --json produced no id: %s", routed.stdout)
|
|
}
|
|
routedShow := runBD(t, bin, local, nil, "show", routedID, "--json")
|
|
if routedShow.code != 0 {
|
|
t.Fatalf("bd show %s in the routed-to workspace failed (exit %d): stderr=%s", routedID, routedShow.code, routedShow.stderr)
|
|
}
|
|
if got := firstJSONField(t, routedShow.stdout, "created_by"); got != creator {
|
|
t.Errorf("created_by = %q on a create routed from an http workspace, want the creating actor %q: %s", got, creator, routedShow.stdout)
|
|
}
|
|
|
|
// update with a MATCHING --if-revision guard: must succeed.
|
|
okUpdate := runBD(t, bin, workspace, nil, "update", id, "-a", "alice", "--if-revision", revision, "--json")
|
|
if okUpdate.code != 0 {
|
|
t.Fatalf("bd update --if-revision %s (matching) failed (exit %d): stdout=%s stderr=%s",
|
|
revision, okUpdate.code, okUpdate.stdout, okUpdate.stderr)
|
|
}
|
|
|
|
// update again with the SAME (now stale) token: must refuse with exit 13
|
|
// and the precondition_failed machine body on the last stderr line.
|
|
staleUpdate := runBD(t, bin, workspace, nil, "update", id, "-a", "bob", "--if-revision", revision, "--json")
|
|
if staleUpdate.code != 13 {
|
|
t.Fatalf("bd update --if-revision %s (stale) exit = %d, want 13 (ExitGuardMismatch): stdout=%s stderr=%s",
|
|
revision, staleUpdate.code, staleUpdate.stdout, staleUpdate.stderr)
|
|
}
|
|
body := lastJSONLine(t, staleUpdate.stderr)
|
|
if body["code"] != "precondition_failed" {
|
|
t.Errorf("stale --if-revision body code = %v, want \"precondition_failed\": %s", body["code"], staleUpdate.stderr)
|
|
}
|
|
if body["id"] != id {
|
|
t.Errorf("stale --if-revision body id = %v, want %q: %s", body["id"], id, staleUpdate.stderr)
|
|
}
|
|
|
|
// dep add + a second issue to depend on, then ready/count/close/delete.
|
|
// id depends on (is blocked by) depID (dep.go's own doc: "bd dep add
|
|
// issue-123 issue-456" means issue-123 depends on issue-456), so depID
|
|
// is the one issue the ready set must show and id is the one it must not.
|
|
dep := runBD(t, bin, workspace, nil, "create", "A dependency", "--json")
|
|
if dep.code != 0 {
|
|
t.Fatalf("bd create (dependency) failed (exit %d): stderr=%s", dep.code, dep.stderr)
|
|
}
|
|
depID := firstJSONField(t, dep.stdout, "id")
|
|
|
|
if r := runBD(t, bin, workspace, nil, "dep", "add", id, depID, "--json"); r.code != 0 {
|
|
t.Fatalf("bd dep add failed (exit %d): stdout=%s stderr=%s", r.code, r.stdout, r.stderr)
|
|
}
|
|
|
|
ready := runBD(t, bin, workspace, nil, "ready", "--json")
|
|
if ready.code != 0 {
|
|
t.Fatalf("bd ready failed (exit %d): stdout=%s stderr=%s", ready.code, ready.stdout, ready.stderr)
|
|
}
|
|
readyIDs := jsonIDSet(t, ready.stdout)
|
|
if !readyIDs[depID] {
|
|
t.Errorf("bd ready did not list %s (no deps of its own, open): %s", depID, ready.stdout)
|
|
}
|
|
if readyIDs[id] {
|
|
t.Errorf("bd ready listed %s, which depends on the still-open %s: %s", id, depID, ready.stdout)
|
|
}
|
|
|
|
count := runBD(t, bin, workspace, nil, "count", "--json")
|
|
if count.code != 0 {
|
|
t.Fatalf("bd count failed (exit %d): stdout=%s stderr=%s", count.code, count.stdout, count.stderr)
|
|
}
|
|
if n := jsonIntField(t, count.stdout, "count"); n != 2 {
|
|
t.Errorf("bd count = %d, want 2 (id and depID, the only issues this workspace has): %s", n, count.stdout)
|
|
}
|
|
|
|
// The http backend serves listing, trees, claims and closes only as roles,
|
|
// and refuses the legacy store methods the external-dependency decorator
|
|
// once rebuilt those roles over, so each command below exited 1 here, even
|
|
// with nothing externally blocked.
|
|
list := runBD(t, bin, workspace, nil, "list", "--json")
|
|
if list.code != 0 {
|
|
t.Fatalf("bd list --json failed (exit %d): stdout=%s stderr=%s", list.code, list.stdout, list.stderr)
|
|
}
|
|
if listIDs := jsonIDSet(t, list.stdout); !listIDs[id] || !listIDs[depID] {
|
|
t.Errorf("bd list --json did not list both %s and %s: %s", id, depID, list.stdout)
|
|
}
|
|
textList := runBD(t, bin, workspace, nil, "list")
|
|
if textList.code != 0 || !strings.Contains(textList.stdout, id) || !strings.Contains(textList.stdout, depID) {
|
|
t.Errorf("bd list (exit %d) did not list both %s and %s: stdout=%s stderr=%s", textList.code, id, depID, textList.stdout, textList.stderr)
|
|
}
|
|
// --deps turns a failed whole-workspace edge scan behind that tree (the
|
|
// divergence ledger's L8) from a silent omission into an error.
|
|
depsList := runBD(t, bin, workspace, nil, "list", "--deps")
|
|
if depsList.code != 0 || !strings.Contains(depsList.stdout, id) || !strings.Contains(depsList.stdout, depID) {
|
|
t.Errorf("bd list --deps (exit %d) did not list both %s and %s: stdout=%s stderr=%s", depsList.code, id, depID, depsList.stdout, depsList.stderr)
|
|
}
|
|
|
|
// An external dependency on a project this workspace has not configured
|
|
// stays unsatisfied, so extID is blocked for as long as it is open. At
|
|
// priority 0 it sorts ahead of depID, where a claim blind to that would
|
|
// take it.
|
|
const externalRef = "external:otherproj:cap"
|
|
ext := runBD(t, bin, workspace, nil, "create", "Externally blocked", "-p", "0", "--json")
|
|
if ext.code != 0 {
|
|
t.Fatalf("bd create (externally blocked) failed (exit %d): stderr=%s", ext.code, ext.stderr)
|
|
}
|
|
extID := firstJSONField(t, ext.stdout, "id")
|
|
if r := runBD(t, bin, workspace, nil, "dep", "add", extID, externalRef, "--json"); r.code != 0 {
|
|
t.Fatalf("bd dep add %s %s failed (exit %d): stdout=%s stderr=%s", extID, externalRef, r.code, r.stdout, r.stderr)
|
|
}
|
|
tree := runBD(t, bin, workspace, nil, "dep", "tree", extID, "--json")
|
|
if tree.code != 0 {
|
|
t.Fatalf("bd dep tree %s failed (exit %d): stdout=%s stderr=%s", extID, tree.code, tree.stdout, tree.stderr)
|
|
}
|
|
if !jsonIDSet(t, tree.stdout)[externalRef] {
|
|
t.Errorf("bd dep tree %s did not show its %s leaf: %s", extID, externalRef, tree.stdout)
|
|
}
|
|
for _, args := range [][]string{{"ready", "--json"}, {"list", "--ready", "--json"}} {
|
|
r := runBD(t, bin, workspace, nil, args...)
|
|
if r.code != 0 {
|
|
t.Fatalf("bd %s failed (exit %d): stdout=%s stderr=%s", strings.Join(args, " "), r.code, r.stdout, r.stderr)
|
|
}
|
|
if readyIDs := jsonIDSet(t, r.stdout); !readyIDs[depID] || readyIDs[extID] || readyIDs[id] {
|
|
t.Errorf("bd %s = %s, want %s and neither %s nor %s", strings.Join(args, " "), r.stdout, depID, extID, id)
|
|
}
|
|
}
|
|
claim := runBD(t, bin, workspace, nil, "ready", "--claim", "--json")
|
|
if claim.code != 0 {
|
|
t.Fatalf("bd ready --claim failed (exit %d): stdout=%s stderr=%s", claim.code, claim.stdout, claim.stderr)
|
|
}
|
|
if got := firstJSONField(t, claim.stdout, "id"); got != depID {
|
|
t.Errorf("bd ready --claim claimed %q, want %s (%s is externally blocked): %s", got, depID, extID, claim.stdout)
|
|
}
|
|
// --claim-next refuses whole over http, before anything closes, and names
|
|
// the commands that do the same work. It is asked again once extID is
|
|
// closed, because a closed holder of an external ref changes nothing.
|
|
refuseClaimNext := func(when string) {
|
|
t.Helper()
|
|
r := runBD(t, bin, workspace, nil, "close", depID, "--claim-next")
|
|
if r.code == 0 || !strings.Contains(r.stderr, "nothing was closed") || !strings.Contains(r.stderr, "bd ready --claim") {
|
|
t.Errorf("bd close %s --claim-next %s (exit %d) did not refuse with the bd ready --claim hint: stdout=%s stderr=%s", depID, when, r.code, r.stdout, r.stderr)
|
|
}
|
|
show := runBD(t, bin, workspace, nil, "show", depID, "--json")
|
|
if status := firstJSONField(t, show.stdout, "status"); show.code != 0 || status == "closed" {
|
|
t.Errorf("bd show %s (exit %d) status = %q after a refused bd close --claim-next %s, want it not closed: stderr=%s", depID, show.code, status, when, show.stderr)
|
|
}
|
|
}
|
|
refuseClaimNext("while " + extID + " is open")
|
|
refused := runBD(t, bin, workspace, nil, "close", extID)
|
|
if refused.code == 0 || !strings.Contains(refused.stderr, externalRef) || !strings.Contains(refused.stderr, "use --force") {
|
|
t.Errorf("bd close %s (exit %d) did not refuse it as blocked by %s: stdout=%s stderr=%s", extID, refused.code, externalRef, refused.stdout, refused.stderr)
|
|
}
|
|
if r := runBD(t, bin, workspace, nil, "close", extID, "--force", "--json"); r.code != 0 {
|
|
t.Fatalf("bd close %s --force failed (exit %d): stdout=%s stderr=%s", extID, r.code, r.stdout, r.stderr)
|
|
}
|
|
forcedShow := runBD(t, bin, workspace, nil, "show", extID, "--json")
|
|
if status := firstJSONField(t, forcedShow.stdout, "status"); forcedShow.code != 0 || status != "closed" {
|
|
t.Errorf("bd show %s (exit %d) status = %q after bd close --force, want %q: stderr=%s", extID, forcedShow.code, status, "closed", forcedShow.stderr)
|
|
}
|
|
refuseClaimNext("after " + extID + " was closed")
|
|
|
|
// The closed extID still holds an unsatisfied external ref, so the claim
|
|
// stays on the decorator's composed leg (ledger row L14). It must still
|
|
// take what the served claim would: the one open, unassigned issue, since
|
|
// depID is claimed and id is assigned.
|
|
next := runBD(t, bin, workspace, nil, "create", "Claimable after the holder closed", "--json")
|
|
if next.code != 0 {
|
|
t.Fatalf("bd create (claimable) failed (exit %d): stdout=%s stderr=%s", next.code, next.stdout, next.stderr)
|
|
}
|
|
nextID := firstJSONField(t, next.stdout, "id")
|
|
reclaim := runBD(t, bin, workspace, nil, "ready", "--claim", "--json")
|
|
if reclaim.code != 0 {
|
|
t.Fatalf("bd ready --claim after %s was closed failed (exit %d): stdout=%s stderr=%s", extID, reclaim.code, reclaim.stdout, reclaim.stderr)
|
|
}
|
|
if got := firstJSONField(t, reclaim.stdout, "id"); got != nextID {
|
|
t.Errorf("bd ready --claim after %s was closed claimed %q, want %s: %s", extID, got, nextID, reclaim.stdout)
|
|
}
|
|
|
|
if r := runBD(t, bin, workspace, nil, "close", depID, "--json"); r.code != 0 {
|
|
t.Fatalf("bd close failed (exit %d): stdout=%s stderr=%s", r.code, r.stdout, r.stderr)
|
|
}
|
|
closedShow := runBD(t, bin, workspace, nil, "show", depID, "--json")
|
|
if closedShow.code != 0 {
|
|
t.Fatalf("bd show %s (after close) failed (exit %d): stderr=%s", depID, closedShow.code, closedShow.stderr)
|
|
}
|
|
if status := firstJSONField(t, closedShow.stdout, "status"); status != "closed" {
|
|
t.Errorf("bd show %s status = %q after bd close, want %q: %s", depID, status, "closed", closedShow.stdout)
|
|
}
|
|
|
|
if r := runBD(t, bin, workspace, nil, "delete", id, "--force", "--json"); r.code != 0 {
|
|
t.Fatalf("bd delete failed (exit %d): stdout=%s stderr=%s", r.code, r.stdout, r.stderr)
|
|
}
|
|
deletedShow := runBD(t, bin, workspace, nil, "show", id, "--json")
|
|
if deletedShow.code == 0 {
|
|
t.Errorf("bd show %s succeeded (exit 0) after bd delete --force; want a not-found refusal: stdout=%s", id, deletedShow.stdout)
|
|
}
|
|
}
|
|
|
|
// TestE2E_ConnectRefusesPlainHTTPToNonLoopback kills the mutation that
|
|
// disables connect.go's plaintext-host refusal (`&& !connectAllowPlaintext`
|
|
// widened to `&& !connectAllowPlaintext && false`). A bearer credential this
|
|
// workspace would send to a non-loopback http:// server travels the network
|
|
// unencrypted, so connect must refuse before ever dialing — which this test
|
|
// also verifies directly: 198.51.100.1 is RFC 5737 TEST-NET-2, reserved for
|
|
// documentation and globally unroutable, so even a mutated build that skipped
|
|
// the refusal could not reach a real server here, keeping this test on the
|
|
// loopback-only network the rest of this suite requires.
|
|
func TestE2E_ConnectRefusesPlainHTTPToNonLoopback(t *testing.T) {
|
|
skipUnlessEmbeddedDolt(t)
|
|
bin := buildBD(t)
|
|
workspace := t.TempDir()
|
|
|
|
const nonLoopbackURL = "http://198.51.100.1:1/"
|
|
refused := runBD(t, bin, workspace, nil, "connect", nonLoopbackURL, "--json")
|
|
if refused.code == 0 {
|
|
t.Fatalf("bd connect %s succeeded; want a refusal (not loopback, plain http, no --allow-plaintext): stdout=%s", nonLoopbackURL, refused.stdout)
|
|
}
|
|
if !strings.Contains(refused.stderr, "not loopback") {
|
|
t.Errorf("bd connect %s stderr does not name the loopback refusal; want the host-safety gate, not some other failure: exit=%d stdout=%s stderr=%s",
|
|
nonLoopbackURL, refused.code, refused.stdout, refused.stderr)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(workspace, ".beads", "http_target.json")); err == nil {
|
|
t.Error("bd connect wrote the activation sidecar despite refusing the connection")
|
|
}
|
|
}
|
|
|
|
// TestE2E_ConnectRefusesSwitchingBackendWithoutForce kills the mutation that
|
|
// disables connect.go's backend-switch force gate (`&& !connectForce`
|
|
// widened to `&& !connectForce && false`). A workspace that already selects
|
|
// a different backend must refuse to switch to http without --force; with
|
|
// --force it must proceed PAST that gate, so the test tells the two
|
|
// refusals apart by what they say, not just that they both fail: a port
|
|
// nothing listens on (still loopback, so it clears the plaintext-host gate
|
|
// above) turns "the force gate let this through" into its own distinct
|
|
// dial failure rather than a false pass.
|
|
func TestE2E_ConnectRefusesSwitchingBackendWithoutForce(t *testing.T) {
|
|
skipUnlessEmbeddedDolt(t)
|
|
bin := buildBD(t)
|
|
workspace := t.TempDir()
|
|
beadsDir := filepath.Join(workspace, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
|
t.Fatalf("mkdir .beads: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o600); err != nil {
|
|
t.Fatalf("write metadata.json: %v", err)
|
|
}
|
|
|
|
const target = "http://127.0.0.1:1/"
|
|
|
|
withoutForce := runBD(t, bin, workspace, nil, "connect", target, "--json")
|
|
if withoutForce.code == 0 {
|
|
t.Fatalf("bd connect %s without --force succeeded; workspace already selects backend %q", target, "dolt")
|
|
}
|
|
if !strings.Contains(withoutForce.stderr, "--force") {
|
|
t.Errorf("bd connect without --force did not mention --force in its refusal; want the backend-switch gate, not some other failure: exit=%d stdout=%s stderr=%s",
|
|
withoutForce.code, withoutForce.stdout, withoutForce.stderr)
|
|
}
|
|
|
|
withForce := runBD(t, bin, workspace, nil, "connect", target, "--force", "--json")
|
|
if withForce.code == 0 {
|
|
t.Fatalf("bd connect %s --force succeeded against a port nothing listens on", target)
|
|
}
|
|
if strings.Contains(withForce.stderr, "--force") {
|
|
t.Errorf("bd connect --force still hit the force-gate refusal; the gate was not bypassed: stderr=%s", withForce.stderr)
|
|
}
|
|
|
|
data, err := os.ReadFile(filepath.Join(beadsDir, "metadata.json"))
|
|
if err != nil {
|
|
t.Fatalf("read metadata.json: %v", err)
|
|
}
|
|
if !strings.Contains(string(data), `"dolt"`) {
|
|
t.Errorf("metadata.json no longer names backend dolt after the refused connect attempt: %s", data)
|
|
}
|
|
}
|
|
|
|
// TestE2E_ConnectRedactsCredentialsEverywhereItPrintsTheURL is MED-3's
|
|
// regression test: a URL pasted with a userinfo password must never have
|
|
// that password echoed back by connect, in any output stream, on any path —
|
|
// success or failure. The target here fails at the handshake dial (loopback,
|
|
// nothing listening on port 1), which is exactly the path that used to wrap
|
|
// the raw argument into "connecting to %s: %v".
|
|
func TestE2E_ConnectRedactsCredentialsEverywhereItPrintsTheURL(t *testing.T) {
|
|
skipUnlessEmbeddedDolt(t)
|
|
bin := buildBD(t)
|
|
workspace := t.TempDir()
|
|
|
|
const password = "s3cr3t-password-998" //nolint — a fixture value, not a real credential.
|
|
target := "http://alice:" + password + "@127.0.0.1:1/"
|
|
|
|
r := runBD(t, bin, workspace, nil, "connect", target, "--json")
|
|
if r.code == 0 {
|
|
t.Fatalf("bd connect %s succeeded against a port nothing listens on", target)
|
|
}
|
|
if strings.Contains(r.stdout, password) || strings.Contains(r.stderr, password) {
|
|
t.Errorf("bd connect's output carries the literal userinfo password %q: stdout=%s stderr=%s", password, r.stdout, r.stderr)
|
|
}
|
|
}
|
|
|
|
// TestE2E_ReconnectRefusesADifferentProjectUnlessForced is MED-5's test: a
|
|
// bare re-connect (no --expect-project-id, no --force) against a server that
|
|
// answers with a DIFFERENT project id than the one the workspace's sidecar
|
|
// already pinned must refuse, not silently re-pin — connect.go used to build
|
|
// the new Target from --expect-project-id alone, so a plain `bd connect
|
|
// <other-server>` with nothing on the command line would adopt whatever the
|
|
// new server said with no refusal at all. --force must still get past it.
|
|
func TestE2E_ReconnectRefusesADifferentProjectUnlessForced(t *testing.T) {
|
|
skipUnlessEmbeddedDolt(t)
|
|
bin := buildBD(t)
|
|
|
|
firstAddr := startE2EServer(t) // publishes e2eProjectID
|
|
secondAddr := startE2EServerWithIdentity(t, "proj-e2e-s6-OTHER", "httpe2e_other")
|
|
workspace := t.TempDir()
|
|
|
|
first := runBD(t, bin, workspace, nil, "connect", "http://"+firstAddr, "--json")
|
|
if first.code != 0 {
|
|
t.Fatalf("first bd connect failed (exit %d): stdout=%s stderr=%s", first.code, first.stdout, first.stderr)
|
|
}
|
|
pinned, err := os.ReadFile(filepath.Join(workspace, ".beads", "http_target.json"))
|
|
if err != nil {
|
|
t.Fatalf("read sidecar after first connect: %v", err)
|
|
}
|
|
if !strings.Contains(string(pinned), e2eProjectID) {
|
|
t.Fatalf("sidecar after first connect does not carry %q: %s", e2eProjectID, pinned)
|
|
}
|
|
|
|
// A bare re-connect to the DIFFERENT server, no --expect-project-id, no
|
|
// --force: must refuse, carrying the prior pin forward rather than
|
|
// adopting the new server's answer.
|
|
again := runBD(t, bin, workspace, nil, "connect", "http://"+secondAddr, "--json")
|
|
if again.code == 0 {
|
|
t.Fatalf("bare re-connect to a different project's server succeeded; want a refusal: stdout=%s", again.stdout)
|
|
}
|
|
if !strings.Contains(again.stderr, e2eProjectID) {
|
|
t.Errorf("re-connect refusal does not name the prior pin %q: stderr=%s", e2eProjectID, again.stderr)
|
|
}
|
|
stillPinned, err := os.ReadFile(filepath.Join(workspace, ".beads", "http_target.json"))
|
|
if err != nil {
|
|
t.Fatalf("read sidecar after refused re-connect: %v", err)
|
|
}
|
|
if string(stillPinned) != string(pinned) {
|
|
t.Errorf("sidecar changed after a REFUSED re-connect: before=%s after=%s", pinned, stillPinned)
|
|
}
|
|
|
|
// --force gets past it and re-pins to the new server's project.
|
|
forced := runBD(t, bin, workspace, nil, "connect", "http://"+secondAddr, "--force", "--json")
|
|
if forced.code != 0 {
|
|
t.Fatalf("bd connect --force to the other project failed (exit %d): stdout=%s stderr=%s", forced.code, forced.stdout, forced.stderr)
|
|
}
|
|
refetched, err := os.ReadFile(filepath.Join(workspace, ".beads", "http_target.json"))
|
|
if err != nil {
|
|
t.Fatalf("read sidecar after forced re-connect: %v", err)
|
|
}
|
|
if !strings.Contains(string(refetched), "proj-e2e-s6-OTHER") {
|
|
t.Errorf("sidecar after --force re-connect does not carry the new project id: %s", refetched)
|
|
}
|
|
}
|
|
|
|
// TestE2E_SwitchingBackToDoltLeavesOriginalIdentityIntact is MED-6's test:
|
|
// `bd connect --force` on a workspace that already selects dolt must not
|
|
// rewrite that workspace's git-tracked metadata.json project_id/database —
|
|
// the http identity belongs in the gitignored sidecar alone. This proves it
|
|
// by round-tripping: a dolt workspace with its own original project_id and
|
|
// database connects to http, and metadata.json's original dolt fields must
|
|
// still read back unchanged afterward — exactly what "switching back to
|
|
// dolt" would see, since nothing ever touched them to switch back FROM.
|
|
func TestE2E_SwitchingBackToDoltLeavesOriginalIdentityIntact(t *testing.T) {
|
|
skipUnlessEmbeddedDolt(t)
|
|
bin := buildBD(t)
|
|
addr := startE2EServer(t) // publishes e2eProjectID, unrelated to the dolt identity below.
|
|
workspace := t.TempDir()
|
|
beadsDir := filepath.Join(workspace, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
|
t.Fatalf("mkdir .beads: %v", err)
|
|
}
|
|
const (
|
|
originalProjectID = "dolt-original-project"
|
|
originalDatabase = "dolt-original-db"
|
|
)
|
|
metadataPath := filepath.Join(beadsDir, "metadata.json")
|
|
original := fmt.Sprintf(`{"backend":"dolt","database":%q,"project_id":%q}`, originalDatabase, originalProjectID)
|
|
if err := os.WriteFile(metadataPath, []byte(original), 0o600); err != nil {
|
|
t.Fatalf("write metadata.json: %v", err)
|
|
}
|
|
|
|
forced := runBD(t, bin, workspace, nil, "connect", "http://"+addr, "--force", "--json")
|
|
if forced.code != 0 {
|
|
t.Fatalf("bd connect --force failed (exit %d): stdout=%s stderr=%s", forced.code, forced.stdout, forced.stderr)
|
|
}
|
|
|
|
data, err := os.ReadFile(metadataPath)
|
|
if err != nil {
|
|
t.Fatalf("read metadata.json after connect: %v", err)
|
|
}
|
|
var after struct {
|
|
Backend string `json:"backend"`
|
|
Database string `json:"database"`
|
|
ProjectID string `json:"project_id"`
|
|
}
|
|
if err := json.Unmarshal(data, &after); err != nil {
|
|
t.Fatalf("parse metadata.json after connect: %v\n%s", err, data)
|
|
}
|
|
if after.Backend != "http" {
|
|
t.Errorf("metadata.json backend = %q after connect, want %q: %s", after.Backend, "http", data)
|
|
}
|
|
// The MED-6 assertion: connect must NOT have overwritten these with the
|
|
// http server's own database/project_id (e2eDatabase/e2eProjectID) — a
|
|
// future switch back to dolt reads these same, untouched fields.
|
|
if after.Database != originalDatabase {
|
|
t.Errorf("metadata.json database = %q after bd connect --force, want the original %q untouched (MED-6: connect must not rewrite it): %s",
|
|
after.Database, originalDatabase, data)
|
|
}
|
|
if after.ProjectID != originalProjectID {
|
|
t.Errorf("metadata.json project_id = %q after bd connect --force, want the original %q untouched (MED-6: connect must not rewrite it): %s",
|
|
after.ProjectID, originalProjectID, data)
|
|
}
|
|
|
|
// The http identity itself lives in the sidecar, not metadata.json.
|
|
sidecar, err := os.ReadFile(filepath.Join(beadsDir, "http_target.json"))
|
|
if err != nil {
|
|
t.Fatalf("read sidecar after connect: %v", err)
|
|
}
|
|
if !strings.Contains(string(sidecar), e2eProjectID) {
|
|
t.Errorf("sidecar does not carry the http server's project id %q: %s", e2eProjectID, sidecar)
|
|
}
|
|
}
|
|
|
|
// TestE2E_ConnectClearRestoresThePreviousBackend pins `bd connect --clear`
|
|
// (bee-ghosttrack CHANGES_REQUESTED on #7288, should-fix 1): a dolt workspace
|
|
// forced onto http and then cleared must select dolt again, with the sidecar
|
|
// gone and metadata.json's own dolt identity untouched. A second --clear finds
|
|
// nothing to clear and leaves metadata.json alone.
|
|
func TestE2E_ConnectClearRestoresThePreviousBackend(t *testing.T) {
|
|
skipUnlessEmbeddedDolt(t)
|
|
bin := buildBD(t)
|
|
addr := startE2EServer(t)
|
|
workspace := t.TempDir()
|
|
beadsDir := filepath.Join(workspace, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
|
t.Fatalf("mkdir .beads: %v", err)
|
|
}
|
|
const (
|
|
originalProjectID = "dolt-original-project-for-clear"
|
|
originalDatabase = "dolt-original-db-for-clear"
|
|
)
|
|
metadataPath := filepath.Join(beadsDir, "metadata.json")
|
|
original := fmt.Sprintf(`{"backend":"dolt","database":%q,"project_id":%q}`, originalDatabase, originalProjectID)
|
|
if err := os.WriteFile(metadataPath, []byte(original), 0o600); err != nil {
|
|
t.Fatalf("write metadata.json: %v", err)
|
|
}
|
|
sidecarPath := filepath.Join(beadsDir, "http_target.json")
|
|
|
|
forced := runBD(t, bin, workspace, nil, "connect", "http://"+addr, "--force", "--json")
|
|
if forced.code != 0 {
|
|
t.Fatalf("bd connect --force failed (exit %d): stdout=%s stderr=%s", forced.code, forced.stdout, forced.stderr)
|
|
}
|
|
if _, err := os.Stat(sidecarPath); err != nil {
|
|
t.Fatalf("bd connect --force did not write the sidecar: %v", err)
|
|
}
|
|
|
|
type clearResult struct {
|
|
Cleared bool `json:"cleared"`
|
|
RestoredBackend string `json:"restored_backend"`
|
|
}
|
|
type identity struct {
|
|
Backend string `json:"backend"`
|
|
Database string `json:"database"`
|
|
ProjectID string `json:"project_id"`
|
|
}
|
|
readMetadata := func() identity {
|
|
t.Helper()
|
|
data, err := os.ReadFile(metadataPath)
|
|
if err != nil {
|
|
t.Fatalf("read metadata.json: %v", err)
|
|
}
|
|
var got identity
|
|
if err := json.Unmarshal(data, &got); err != nil {
|
|
t.Fatalf("parse metadata.json: %v\n%s", err, data)
|
|
}
|
|
return got
|
|
}
|
|
want := identity{Backend: "dolt", Database: originalDatabase, ProjectID: originalProjectID}
|
|
|
|
cleared := runBD(t, bin, workspace, nil, "connect", "--clear", "--json")
|
|
if cleared.code != 0 {
|
|
t.Fatalf("bd connect --clear failed (exit %d): stdout=%s stderr=%s", cleared.code, cleared.stdout, cleared.stderr)
|
|
}
|
|
var first clearResult
|
|
if err := json.Unmarshal([]byte(cleared.stdout), &first); err != nil {
|
|
t.Fatalf("parse bd connect --clear --json: %v\n%s", err, cleared.stdout)
|
|
}
|
|
if !first.Cleared || first.RestoredBackend != "dolt" {
|
|
t.Errorf("bd connect --clear = %+v, want cleared and dolt restored: %s", first, cleared.stdout)
|
|
}
|
|
if _, err := os.Stat(sidecarPath); !os.IsNotExist(err) {
|
|
t.Errorf("sidecar still present after bd connect --clear (stat err %v)", err)
|
|
}
|
|
if got := readMetadata(); got != want {
|
|
t.Errorf("metadata.json after bd connect --clear = %+v, want %+v", got, want)
|
|
}
|
|
|
|
again := runBD(t, bin, workspace, nil, "connect", "--clear", "--json")
|
|
if again.code != 0 {
|
|
t.Fatalf("second bd connect --clear failed (exit %d): stdout=%s stderr=%s", again.code, again.stdout, again.stderr)
|
|
}
|
|
var second clearResult
|
|
if err := json.Unmarshal([]byte(again.stdout), &second); err != nil {
|
|
t.Fatalf("parse second bd connect --clear --json: %v\n%s", err, again.stdout)
|
|
}
|
|
if second != (clearResult{}) {
|
|
t.Errorf("second bd connect --clear = %+v, want nothing cleared or restored: %s", second, again.stdout)
|
|
}
|
|
if got := readMetadata(); got != want {
|
|
t.Errorf("metadata.json after a second bd connect --clear = %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
// TestE2E_ForceConnectWritesSucceedDespiteDifferingLocalProjectID pins the
|
|
// bee-ghosttrack CHANGES_REQUESTED finding on #7288: after `bd connect
|
|
// --force` over a workspace that already had a LOCAL (dolt) identity, every
|
|
// write used to fail with "workspace identity mismatch detected". That
|
|
// check compared metadata.json's project_id (the original dolt workspace's
|
|
// identity, left untouched by connect per MED-6 — see
|
|
// TestE2E_SwitchingBackToDoltLeavesOriginalIdentityIntact above) against the
|
|
// live server's _project_id, and those two are legitimately different
|
|
// projects; the check needs the http backend's OWN pin (the sidecar's
|
|
// ExpectProjectID, set by this same connect) instead.
|
|
//
|
|
// This reuses that test's exact setup — a workspace with its own original
|
|
// dolt project_id, forced onto an unrelated server's project — but goes
|
|
// further: it actually writes (create, update, comments add) over the new
|
|
// http connection, which is what the finding says used to fail outright.
|
|
func TestE2E_ForceConnectWritesSucceedDespiteDifferingLocalProjectID(t *testing.T) {
|
|
skipUnlessEmbeddedDolt(t)
|
|
bin := buildBD(t)
|
|
addr := startE2EServer(t) // publishes e2eProjectID, unrelated to the dolt identity below.
|
|
workspace := t.TempDir()
|
|
beadsDir := filepath.Join(workspace, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
|
t.Fatalf("mkdir .beads: %v", err)
|
|
}
|
|
const (
|
|
originalProjectID = "dolt-original-project-for-writes"
|
|
originalDatabase = "dolt-original-db-for-writes"
|
|
)
|
|
metadataPath := filepath.Join(beadsDir, "metadata.json")
|
|
original := fmt.Sprintf(`{"backend":"dolt","database":%q,"project_id":%q}`, originalDatabase, originalProjectID)
|
|
if err := os.WriteFile(metadataPath, []byte(original), 0o600); err != nil {
|
|
t.Fatalf("write metadata.json: %v", err)
|
|
}
|
|
|
|
forced := runBD(t, bin, workspace, nil, "connect", "http://"+addr, "--force", "--json")
|
|
if forced.code != 0 {
|
|
t.Fatalf("bd connect --force failed (exit %d): stdout=%s stderr=%s", forced.code, forced.stdout, forced.stderr)
|
|
}
|
|
|
|
create := runBD(t, bin, workspace, nil, "create", "A title over http after forced connect", "--json")
|
|
if create.code != 0 {
|
|
t.Fatalf("bd create after forced connect failed (exit %d): stdout=%s stderr=%s", create.code, create.stdout, create.stderr)
|
|
}
|
|
if strings.Contains(create.stderr, "workspace identity mismatch") {
|
|
t.Fatalf("bd create tripped the workspace-identity-mismatch refusal despite connect having just pinned this project: stderr=%s", create.stderr)
|
|
}
|
|
id := firstJSONField(t, create.stdout, "id")
|
|
if id == "" {
|
|
t.Fatalf("bd create --json produced no id: %s", create.stdout)
|
|
}
|
|
|
|
update := runBD(t, bin, workspace, nil, "update", id, "-a", "alice", "--json")
|
|
if update.code != 0 {
|
|
t.Fatalf("bd update after forced connect failed (exit %d): stdout=%s stderr=%s", update.code, update.stdout, update.stderr)
|
|
}
|
|
if strings.Contains(update.stderr, "workspace identity mismatch") {
|
|
t.Fatalf("bd update tripped the workspace-identity-mismatch refusal despite connect having just pinned this project: stderr=%s", update.stderr)
|
|
}
|
|
|
|
comment := runBD(t, bin, workspace, nil, "comments", "add", id, "a comment over http after forced connect", "--json")
|
|
if comment.code != 0 {
|
|
t.Fatalf("bd comments add after forced connect failed (exit %d): stdout=%s stderr=%s", comment.code, comment.stdout, comment.stderr)
|
|
}
|
|
if strings.Contains(comment.stderr, "workspace identity mismatch") {
|
|
t.Fatalf("bd comments add tripped the workspace-identity-mismatch refusal despite connect having just pinned this project: stderr=%s", comment.stderr)
|
|
}
|
|
}
|
|
|
|
// startE2EServer boots an in-process `bd serve` over embedded Dolt and
|
|
// returns its bound 127.0.0.1 address. It is the same role-binding shape as
|
|
// internal/httpclient's servedEnv harness, kept separate because that one is
|
|
// unexported in a different package: this test drives the CLI, not the
|
|
// internal wire client, so it owns its own (smaller) copy rather than
|
|
// reaching across the package boundary.
|
|
//
|
|
// This stays the one server topology this file uses — a real `bd serve`
|
|
// SUBPROCESS, gated on BEADS_TEST_DOLT_SERVER=local the way
|
|
// internal/testutil's local Dolt backend is elsewhere, was considered and
|
|
// rejected:
|
|
//
|
|
// 1. bd serve permanently refuses an embedded-Dolt workspace
|
|
// (cmd/bd/serve.go's serveDatabaseSource, "EMBEDDED DOLT IS PERMANENT").
|
|
// A real `bd` subprocess pointed at a `bd init`-created workspace with no
|
|
// registered backend would therefore refuse to start at all — there is no
|
|
// bd-init-then-bd-serve shape for this backend to drive as a subprocess.
|
|
// 2. The one way around that refusal, a registered backend whose Open hands
|
|
// back a real embedded store (what cmd/bd/serve_registered_backend_test.go
|
|
// does), only exists as Go wiring injected into the SAME test process —
|
|
// registration happens at init() time, so a spawned `bd` subprocess has
|
|
// nothing registered and cannot reproduce it. That file's own comment
|
|
// reaches the identical conclusion for the identical reason.
|
|
// 3. The other way around it is a real, non-embedded Dolt SQL server behind
|
|
// "shared"/proxied mode (BEADS_DOLT_SHARED_SERVER=1, internal/testutil's
|
|
// local-or-container harness, cmd/bd/serve_proxied_integration_test.go's
|
|
// startServe). That subprocess `bd serve` lifecycle contract — the bound
|
|
// address on stdout's first line, signal-driven shutdown, the auth
|
|
// posture warnings — is already pinned end to end there, against exactly
|
|
// that topology. Reproducing it a second time in this package would
|
|
// stand up a second real Dolt SQL server fixture per run to re-test the
|
|
// same `bd serve` CLI plumbing that package already owns, not anything
|
|
// specific to the public backend/http door this package exists to prove.
|
|
//
|
|
// What this e2e tier specifically has to show — bd connect, then full CRUD,
|
|
// actually working end to end over the wire against a real listening server
|
|
// — is exactly what the in-process httpapi.Listen harness below proves: it
|
|
// is the identical httpapi.Config / Listen / Serve call bd serve's own
|
|
// serveListen makes, bound to a real TCP socket a real `bd` binary dials
|
|
// unmodified. A real `bd serve` subprocess on top of that would add only
|
|
// coverage of serve's own flag parsing and workspace-source resolution,
|
|
// which belongs to (and is already owned by) cmd/bd/serve_test.go and
|
|
// cmd/bd/serve_proxied_integration_test.go.
|
|
func startE2EServer(t *testing.T) string {
|
|
t.Helper()
|
|
return startE2EServerWithIdentity(t, e2eProjectID, e2eDatabase)
|
|
}
|
|
|
|
// startE2EServerWithIdentity is startE2EServer, parameterized on the identity
|
|
// the in-process server publishes at handshake. MED-5's reconnect-mismatch
|
|
// test needs a SECOND server that answers with a DIFFERENT project id than
|
|
// the one a prior `bd connect` already pinned, to prove the pin is honored
|
|
// rather than silently replaced.
|
|
func startE2EServerWithIdentity(t *testing.T, projectID, database string) string {
|
|
t.Helper()
|
|
beadsDir := t.TempDir()
|
|
ctx := context.Background()
|
|
reference, err := embeddeddolt.Open(ctx, beadsDir, database, "main")
|
|
if err != nil {
|
|
t.Fatalf("open the reference store: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = reference.Close() })
|
|
if err := reference.SetConfig(ctx, "issue_prefix", "e2e"); err != nil {
|
|
t.Fatalf("set the issue prefix: %v", err)
|
|
}
|
|
|
|
cfg := e2eServeConfig(t, reference, projectID, database)
|
|
srv, err := httpapi.Listen(cfg)
|
|
if err != nil {
|
|
t.Fatalf("bind the in-process server: %v", err)
|
|
}
|
|
serveCtx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() { defer close(done); _ = srv.Serve(serveCtx) }()
|
|
t.Cleanup(func() {
|
|
cancel()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(30 * time.Second):
|
|
t.Error("the in-process server did not shut down")
|
|
}
|
|
})
|
|
return srv.Addr()
|
|
}
|
|
|
|
func e2eServeConfig(t *testing.T, s storage.DoltStorage, projectID, database string) httpapi.Config {
|
|
t.Helper()
|
|
cfg := httpapi.Config{
|
|
Addr: "127.0.0.1:0",
|
|
Stdout: io.Discard,
|
|
Stderr: io.Discard,
|
|
Workspace: domain.ContextInfo{ProjectID: projectID, Database: database},
|
|
}
|
|
var err error
|
|
fail := func(name string) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatalf("%s(): %v", name, err)
|
|
}
|
|
}
|
|
cfg.Reader, err = s.IssueReader()
|
|
fail("IssueReader")
|
|
cfg.Claimer, err = s.IssueClaimer()
|
|
fail("IssueClaimer")
|
|
cfg.ReadyClaimer, err = s.ReadyClaimer()
|
|
fail("ReadyClaimer")
|
|
cfg.Releaser, err = s.Releaser()
|
|
fail("Releaser")
|
|
cfg.Lifecycle, err = s.IssueLifecycle()
|
|
fail("IssueLifecycle")
|
|
cfg.Settings, err = s.WorkspaceConfig()
|
|
fail("WorkspaceConfig")
|
|
cfg.Stats, err = s.StatsReporter()
|
|
fail("StatsReporter")
|
|
cfg.CycleDetector, err = s.CycleDetector()
|
|
fail("CycleDetector")
|
|
cfg.EdgeReader, err = s.EdgeReader()
|
|
fail("EdgeReader")
|
|
cfg.GraphCounter, err = s.GraphCounter()
|
|
fail("GraphCounter")
|
|
cfg.BatchGetter, err = s.BatchGetter()
|
|
fail("BatchGetter")
|
|
cfg.LeaseReclaimer, err = s.LeaseReclaimer()
|
|
fail("LeaseReclaimer")
|
|
cfg.Relations, err = s.IssueRelations()
|
|
fail("IssueRelations")
|
|
cfg.Commenter, err = s.Commenter()
|
|
fail("Commenter")
|
|
cfg.BlockingAnnotator, err = s.BlockingAnnotator()
|
|
fail("BlockingAnnotator")
|
|
cfg.TreeWalker, err = s.TreeWalker()
|
|
fail("TreeWalker")
|
|
cfg.ReadyCounter, err = s.ReadyCounter()
|
|
fail("ReadyCounter")
|
|
cfg.Counter, err = s.Counter()
|
|
fail("Counter")
|
|
cfg.Querier, err = s.Querier()
|
|
fail("Querier")
|
|
cfg.Sweeper, err = s.Sweeper()
|
|
fail("Sweeper")
|
|
cfg.Deleter, err = s.Deleter()
|
|
fail("Deleter")
|
|
cfg.BatchCreator, err = s.BatchCreator()
|
|
fail("BatchCreator")
|
|
cfg.BatchCloser, err = s.BatchCloser()
|
|
fail("BatchCloser")
|
|
cfg.DependencyEditor, err = s.DependencyEditor()
|
|
fail("DependencyEditor")
|
|
cfg.MetadataCAS, err = s.MetadataCAS()
|
|
fail("MetadataCAS")
|
|
cfg.BatchApplier, err = s.BatchApplier()
|
|
fail("BatchApplier")
|
|
cfg.Memories, err = s.Memories()
|
|
fail("Memories")
|
|
return cfg
|
|
}
|
|
|
|
// skipUnlessEmbeddedDolt mirrors internal/httpclient's gate: this tier needs
|
|
// the cgo-linked embedded engine, and BEADS_HTTP_TEST_REQUIRED=1 is how the
|
|
// served-surface lane says that tier is mandatory rather than best-effort.
|
|
func skipUnlessEmbeddedDolt(t *testing.T) {
|
|
t.Helper()
|
|
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") == "1" {
|
|
return
|
|
}
|
|
if os.Getenv("BEADS_HTTP_TEST_REQUIRED") == "1" {
|
|
t.Fatal("BEADS_HTTP_TEST_REQUIRED=1 but BEADS_TEST_EMBEDDED_DOLT is not 1; the http e2e tier is not enforced")
|
|
}
|
|
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run the http backend e2e tier")
|
|
}
|
|
|
|
type bdResult struct {
|
|
stdout, stderr string
|
|
code int
|
|
}
|
|
|
|
// runBD shells the built bd binary out in dir, the same subprocess pattern
|
|
// test/conformance's e2e runner uses, kept local here so this tier has no
|
|
// dependency on that package's unexported helpers.
|
|
func runBD(t *testing.T, bin, dir string, env []string, args ...string) bdResult {
|
|
t.Helper()
|
|
cmd := exec.Command(bin, args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = minimalBDEnv(dir, env...)
|
|
var o, e strings.Builder
|
|
cmd.Stdout, cmd.Stderr = &o, &e
|
|
err := cmd.Run()
|
|
code := 0
|
|
if err != nil {
|
|
if ee, ok := err.(*exec.ExitError); ok {
|
|
code = ee.ExitCode()
|
|
} else {
|
|
t.Fatalf("running bd %s: %v", strings.Join(args, " "), err)
|
|
}
|
|
}
|
|
return bdResult{stdout: o.String(), stderr: e.String(), code: code}
|
|
}
|
|
|
|
// minimalBDEnv builds a hermetic environment for a bd subprocess, the same
|
|
// sanitizing shape cmd/bd's own proxied-integration harness uses
|
|
// (bdProxiedEnv): PATH plus the few variables a Go binary needs to start,
|
|
// HOME repointed at dir rather than the real one, and nothing BEADS_*
|
|
// carried over from whatever invoked `go test`. A stray BEADS_DIR or
|
|
// BEADS_HTTP_TOKEN* exported in this process's own environment must never
|
|
// leak into a workspace this test means to fully control — this harness
|
|
// ran the full os.Environ() through unfiltered before, which a credential
|
|
// or workspace-override variable set in the ambient shell could silently
|
|
// have reached. extra layers on top (a test case's own deliberate override),
|
|
// so it is applied last and wins any collision.
|
|
func minimalBDEnv(dir string, extra ...string) []string {
|
|
env := []string{
|
|
"HOME=" + dir,
|
|
"BD_DISABLE_METRICS=1",
|
|
"BD_DISABLE_EVENT_FLUSH=1",
|
|
}
|
|
for _, key := range []string{"PATH", "TMPDIR", "SYSTEMROOT", "WINDIR"} {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
env = append(env, key+"="+v)
|
|
}
|
|
}
|
|
return append(env, extra...)
|
|
}
|
|
|
|
// jsonIDSet collects every top-level "id" field out of a bd --json array
|
|
// response (bd ready among others), for the containment checks a real
|
|
// assertion needs rather than firstJSONField's single-match convenience.
|
|
func jsonIDSet(t *testing.T, out string) map[string]bool {
|
|
t.Helper()
|
|
var rows []map[string]any
|
|
if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &rows); err != nil {
|
|
t.Fatalf("parse JSON array output: %v\n%s", err, out)
|
|
}
|
|
ids := make(map[string]bool, len(rows))
|
|
for _, row := range rows {
|
|
if id, ok := row["id"].(string); ok {
|
|
ids[id] = true
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// jsonIntField pulls one numeric top-level field out of a bd --json object
|
|
// response (bd count's {"count": N}).
|
|
func jsonIntField(t *testing.T, out, field string) int64 {
|
|
t.Helper()
|
|
var generic map[string]any
|
|
if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &generic); err != nil {
|
|
t.Fatalf("parse JSON output for field %q: %v\n%s", field, err, out)
|
|
}
|
|
v, ok := generic[field].(float64)
|
|
if !ok {
|
|
t.Fatalf("field %q missing or not numeric in: %s", field, out)
|
|
}
|
|
return int64(v)
|
|
}
|
|
|
|
// firstJSONField pulls one string-valued top-level field out of a bd --json
|
|
// response, tolerating the schema_version envelope some commands wrap their
|
|
// payload in and the bare-object shape others return directly.
|
|
func firstJSONField(t *testing.T, out, field string) string {
|
|
t.Helper()
|
|
var generic any
|
|
if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &generic); err != nil {
|
|
t.Fatalf("parse JSON output for field %q: %v\n%s", field, err, out)
|
|
}
|
|
return findField(generic, field)
|
|
}
|
|
|
|
func findField(v any, field string) string {
|
|
switch m := v.(type) {
|
|
case map[string]any:
|
|
if s, ok := m[field].(string); ok {
|
|
return s
|
|
}
|
|
for _, nested := range []string{"data", "issue"} {
|
|
if inner, ok := m[nested]; ok {
|
|
if s := findField(inner, field); s != "" {
|
|
return s
|
|
}
|
|
}
|
|
}
|
|
case []any:
|
|
for _, item := range m {
|
|
if s := findField(item, field); s != "" {
|
|
return s
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// lastJSONLine parses the final non-blank line of stderr as JSON, matching
|
|
// reportIfRevisionFailure's own documented contract ("the JSON body is the
|
|
// LAST line on stderr").
|
|
func lastJSONLine(t *testing.T, stderr string) map[string]any {
|
|
t.Helper()
|
|
lines := strings.Split(strings.TrimRight(stderr, "\n"), "\n")
|
|
for i := len(lines) - 1; i >= 0; i-- {
|
|
line := strings.TrimSpace(lines[i])
|
|
if line == "" {
|
|
continue
|
|
}
|
|
var body map[string]any
|
|
if err := json.Unmarshal([]byte(line), &body); err == nil {
|
|
return body
|
|
}
|
|
t.Fatalf("last stderr line is not JSON: %q\nfull stderr:\n%s", line, stderr)
|
|
}
|
|
t.Fatalf("stderr had no non-blank line to parse:\n%s", stderr)
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
buildBDOnce sync.Once
|
|
bdBinPath string
|
|
bdBuildErr error
|
|
bdBinOwnBuild bool // true only on the go-build-from-source path; false for Bazel's prebuilt binary.
|
|
)
|
|
|
|
// buildBD builds the bd binary once per test process, matching the
|
|
// gms_pure_go embedded-engine tag used throughout cmd/bd's own CLI tests
|
|
// (e.g. cmd/bd/create_deps_atomic_test.go). Under Bazel there is no module
|
|
// tree to build from, so the prebuilt test binary is used instead.
|
|
//
|
|
// The binary is shared, via buildBDOnce, across every test in this package
|
|
// that calls buildBD — that sharing is the whole point, one build instead of
|
|
// one per test. Cleanup therefore cannot be a per-call t.Cleanup: the first
|
|
// caller to win the Once race would be the first test to finish, and its
|
|
// Cleanup would delete the binary every LATER test in the same process still
|
|
// needs (exactly what happened before this comment existed — the second and
|
|
// third test to call buildBD got "no such file or directory" because the
|
|
// first test's Cleanup had already removed it). TestMain below owns deleting
|
|
// it exactly once, after every test in the package has finished with it.
|
|
func buildBD(t *testing.T) string {
|
|
t.Helper()
|
|
buildBDOnce.Do(func() {
|
|
if bazeltest.IsBazel() {
|
|
bdBinPath, bdBuildErr = bazeltest.PrebuiltBD()
|
|
return
|
|
}
|
|
dir, err := os.MkdirTemp(os.Getenv("TMPDIR"), "bd-http-e2e")
|
|
if err != nil {
|
|
bdBuildErr = fmt.Errorf("mkdir temp for bd binary: %w", err)
|
|
return
|
|
}
|
|
bin := filepath.Join(dir, "bd-http-e2e")
|
|
cmd := exec.Command("go", "build", "-tags", "gms_pure_go", "-o", bin, "./cmd/bd")
|
|
cmd.Dir = e2eRepoRoot()
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
bdBuildErr = fmt.Errorf("build bd: %w\n%s", err, out)
|
|
_ = os.RemoveAll(dir)
|
|
return
|
|
}
|
|
bdBinPath = bin
|
|
bdBinOwnBuild = true
|
|
})
|
|
if bdBuildErr != nil {
|
|
t.Fatal(bdBuildErr)
|
|
}
|
|
return bdBinPath
|
|
}
|
|
|
|
// TestMain exists for exactly one reason: to delete the temp directory
|
|
// buildBD's go-build-from-source path creates, once, after every test in
|
|
// this package has had its turn with the shared binary inside it. See
|
|
// buildBD's doc comment for why a per-test t.Cleanup cannot do this job.
|
|
func TestMain(m *testing.M) {
|
|
code := m.Run()
|
|
if bdBinOwnBuild && bdBinPath != "" {
|
|
_ = os.RemoveAll(filepath.Dir(bdBinPath))
|
|
}
|
|
os.Exit(code)
|
|
}
|
|
|
|
func e2eRepoRoot() string {
|
|
_, file, _, _ := runtime.Caller(0) // <repo>/backend/http/e2e_cli_test.go
|
|
return filepath.Dir(filepath.Dir(filepath.Dir(file)))
|
|
}
|