beads/cmd/bd/version_embedded_test.go
Julian Knutsen f142cc2ffd
test(cli): before-parity pins for --format json, ordinary --if-revision, and version/config/history/as-of shapes (P7, P8, P10) (#7676)
* test(cli): pin --format json == --json on show, list, ready and memories

Before-parity pin P7 (gap analysis 2026-10-17, section 5a). The hidden
root --format flag is an alias for --json (GH#2612). It is a root
persistent flag, so a subcommand that registers a --format of its own
shadows it (the GH#6278 hazard): `bd list` already does and passes `json`
through by hand, and a graph-preview fork that added a local --format to
`bd show` made `bd show ID --format json` exit nonzero.

TestEmbeddedFormatJSONAliasMatchesJSON runs show, list, ready and memories
with --json and then with `--format json`, `--format=json` and a leading
`bd --format json <verb>`, and requires exit 0 and byte-identical stdout.
It adds bdRunStreams, a small helper returning stdout, stderr and the exit
status separately, which the following pins reuse.

The new TestEmbedded* function is listed in the bd_test srcs and placed in
the 100-shard embedded cmd manifest by
gen_embedded_cmd_shard_manifest.py --write (incremental).

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(cli): pin the ordinary --if-revision compare-and-swap contract

Before-parity pin P8 (gap analysis 2026-10-17, section 5a). --if-revision
is the decimal int64 compare-and-swap merged in #7203, and gascity's
bdstore_conditional.go classifier reads both its exit status and its
machine body. A second, opaque-token meaning for the same flag in another
kind of workspace must leave the ordinary one unchanged.

TestEmbeddedIfRevisionOrdinaryCAS pins, through the real binary:
  - a stale decimal token on update, close, assign and delete (preview
    and --force) exits 13, prints nothing on stdout, writes nothing, and
    ends stderr with a precondition_failed body naming the id and the
    expected and current revisions as exact JSON integers;
  - a non-decimal token exits 1 with the "invalid --if-revision" refusal
    in text and --json modes, never 13, and writes nothing;
  - the current token applies on update, close, assign and delete.

It is the ordinary-workspace half of
TestGraphPreviewIfRevisionOutsideLinkModeIsUpstreamCAS from the
donnabox/codex/preview3-source-catchup-46aa branch (added in 3d16d826be,
revised in 1640940fa9, 3ff2fe73ce, 55dbb61156, 653369305a and
97446d1f08), ported onto this package's embedded helpers. The port drops
the graph-only forget refusal, extends the stale and non-decimal cases
from update/delete to close and assign, and compares revisions as
json.Number rather than float64, since they are int64 values past 2^53.

It is named TestEmbedded* so the bazel-embedded lane, which discovers
`^func TestEmbedded` in *_embedded_test.go, runs it; the existing
TestIfRevisionMatchAndMismatch is not discovered there. The file is
listed in the bd_test srcs and placed in the 100-shard embedded cmd
manifest by gen_embedded_cmd_shard_manifest.py --write (incremental).

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Donna Box <dbox@boxenllc.com>
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(cli): pin version, config set, history and show --as-of shapes

Before-parity pin P10 (gap analysis 2026-10-17, section 5a): the
store-less and history-adjacent commands a later versioned-history or
graph slice could change by accident.

  - TestEmbeddedVersionOutputShape: `bd version` with no arguments exits
    0 and prints one "bd version <version> (<build>...)" line, in a
    workspace and outside one; --json is one object with version, build
    and schema_version (plus commit and branch when known) agreeing with
    the text line, and --format json prints the same document; an
    argument exits 1 and points at `bd versions <arg>` (#6661), on stderr
    or as the --json error, through the real binary rather than RunE.
  - TestEmbeddedConfigSetOtherKeysSkipTheVersionedHistoryScan: in a store
    holding a row the pre-enable scan refuses over, `bd config set` and
    `bd config set-many` of another key with the value "true" exit 0,
    store it, print no refusal and leave the switch off; the switch
    itself is still refused afterwards, as a control.
  - TestEmbeddedHistoryAndShowAsOfReadDoltCommits: with versioned history
    recording, `bd history <id> --json` is still the Dolt-commit history
    (an array of CommitHash/Committer/CommitDate/Issue entries with full
    32-character Dolt hashes, no recorded-version fields, and the same
    hashes in the text output), and `bd show <id> --as-of <hash>` with a
    hash from it is the Dolt AS OF read in show's JSON shape, not the
    R7.1 as-of read over issue_versions; `--as-of main` reads the branch
    head.

The three TestEmbedded* functions are placed in the 100-shard embedded
cmd manifest by gen_embedded_cmd_shard_manifest.py --write (incremental).

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Donna Box <dbox@boxenllc.com>
2026-10-19 03:41:08 +00:00

169 lines
5.9 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"testing"
)
func TestEmbeddedVersion(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("version_output", func(t *testing.T) {
cmd := exec.Command(bd, "version")
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd version failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
s := stdout.String()
if len(strings.TrimSpace(s)) == 0 {
t.Error("expected non-empty version output")
}
// Should contain a version-like string
if !strings.Contains(s, "v") && !strings.Contains(s, "0.") {
t.Errorf("expected version string in output: %s", s)
}
})
}
func TestEmbeddedVersionConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
cmd := exec.Command(bd, "version")
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("version (worker %d): %v\n%s", worker, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}
// versionLinePattern is the whole of `bd version`'s stdout:
// "bd version <version> (<build>)", with ": <commit>" or
// ": <branch>@<commit>" inside the parentheses when the binary knows them.
var versionLinePattern = regexp.MustCompile(`^bd version (\S+) \(([^):]+)(?:: (?:\S+@)?\S+)?\)\n$`)
// TestEmbeddedVersionOutputShape pins `bd version` the way scripts and
// plugins call it, in an ordinary workspace and in a directory that is not
// one:
// - with no arguments it exits 0 and prints exactly one line on stdout,
// "bd version <version> (<build>...)";
// - --json prints one object with version, build and schema_version, plus
// commit and branch when known, and no other key; its version and build
// agree with the text line, and --format json prints the same document;
// - an argument is refused with exit 1 and points at `bd versions <arg>`
// (#6661): on stderr in text mode, as the JSON error on stdout with
// --json, and without the version line either way.
//
// TestVersionWithAnArgumentPointsAtVersions pins the refusal on RunE; this
// pins it through the real binary, where the root pre-run and workspace
// discovery run first.
func TestEmbeddedVersionOutputShape(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
workspace, _, _ := bdInit(t, bd, "--prefix", "vo")
for name, dir := range map[string]string{"workspace": workspace, "no workspace": t.TempDir()} {
t.Run(name, func(t *testing.T) {
text, stderr, exit := bdRunStreams(t, bd, dir, "version")
if exit != 0 {
t.Fatalf("bd version exited %d\nstdout:\n%s\nstderr:\n%s", exit, text, stderr)
}
line := versionLinePattern.FindStringSubmatch(text)
if line == nil {
t.Fatalf("bd version stdout is not one %q line:\n%q", versionLinePattern, text)
}
doc, stderr, exit := bdRunStreams(t, bd, dir, "version", "--json")
if exit != 0 {
t.Fatalf("bd version --json exited %d\nstdout:\n%s\nstderr:\n%s", exit, doc, stderr)
}
var fields map[string]any
if err := json.Unmarshal([]byte(doc), &fields); err != nil {
t.Fatalf("bd version --json is not one JSON object: %v\n%s", err, doc)
}
for key := range fields {
switch key {
case "version", "build", "schema_version", "commit", "branch":
default:
t.Errorf("bd version --json has an unexpected key %q:\n%s", key, doc)
}
}
if _, ok := fields["schema_version"].(float64); !ok {
t.Errorf("bd version --json has no numeric schema_version:\n%s", doc)
}
if fields["version"] != line[1] || fields["build"] != line[2] {
t.Errorf("bd version --json says version %v build %v, the text line says %s and %s:\n%s", fields["version"], fields["build"], line[1], line[2], doc)
}
for _, key := range []string{"commit", "branch"} {
if v, ok := fields[key]; ok {
if s, isString := v.(string); !isString || s == "" {
t.Errorf("bd version --json %s = %#v, want a non-empty string when present", key, v)
}
}
}
if alias, _, exit := bdRunStreams(t, bd, dir, "version", "--format", "json"); exit != 0 || alias != doc {
t.Errorf("bd version --format json exited %d and printed a different document from --json:\n%s\nwant:\n%s", exit, alias, doc)
}
stdout, stderr, exit := bdRunStreams(t, bd, dir, "version", "vo-123")
if exit != 1 || stdout != "" {
t.Errorf("bd version vo-123 exited %d with stdout %q, want exit 1 and nothing on stdout", exit, stdout)
}
for _, want := range []string{"Did you mean", "bd versions vo-123"} {
if !strings.Contains(stderr, want) {
t.Errorf("bd version vo-123 stderr lacks %q:\n%s", want, stderr)
}
}
stdout, stderr, exit = bdRunStreams(t, bd, dir, "version", "vo-123", "--json")
var refusal struct {
Error string `json:"error"`
}
if exit != 1 || json.Unmarshal([]byte(stdout), &refusal) != nil || !strings.Contains(refusal.Error, "bd versions vo-123") {
t.Errorf("bd version vo-123 --json exited %d, want exit 1 and a JSON error naming `bd versions vo-123`\nstdout:\n%s\nstderr:\n%s", exit, stdout, stderr)
}
})
}
}