beads/cmd/bd/format_json_alias_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

102 lines
3.7 KiB
Go

//go:build cgo
package main
import (
"bytes"
"encoding/json"
"errors"
"os"
"os/exec"
"slices"
"strings"
"testing"
)
// bdRunStreams runs one bd process in dir with the hermetic test environment
// and returns stdout, stderr and the exit status separately. A nonzero exit is
// returned, not fatal, so a caller can pin the status itself; only a process
// that could not be started fails the test here.
func bdRunStreams(t *testing.T, bd, dir string, args ...string) (stdout, stderr string, exit int) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
var outBuf, errBuf bytes.Buffer
cmd.Stdout, cmd.Stderr = &outBuf, &errBuf
if err := cmd.Run(); err != nil {
var ee *exec.ExitError
if !errors.As(err, &ee) {
t.Fatalf("bd %s did not run: %v", strings.Join(args, " "), err)
}
return outBuf.String(), errBuf.String(), ee.ExitCode()
}
return outBuf.String(), errBuf.String(), 0
}
// TestEmbeddedFormatJSONAliasMatchesJSON pins the hidden root --format alias
// (GH#2612): on show, list, ready and memories, `--format json` prints exactly
// what `--json` prints, byte for byte, and exits 0 like it.
//
// The alias is a root persistent flag, so any subcommand that registers a
// --format of its own shadows it (the GH#6278 hazard). `bd list` already has
// its own --format and passes `json` through by hand (list_input.go). A
// local --format added to show, ready or memories for another output
// format would turn `bd show ID --format json` into a refusal or into a
// different document. Both are regressions this test catches: it compares
// the whole of stdout, not a parse of it.
//
// Each command is checked in three spellings: `--format json` and
// `--format=json` after the subcommand, and `--format json` before it, where
// the flag is plainly the root's.
func TestEmbeddedFormatJSONAliasMatchesJSON(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)
dir, _, _ := bdInit(t, bd, "--prefix", "fj")
id := bdCreateSilent(t, bd, dir, "Format alias parity", "--type", "task", "--description", "read through --format json")
bdRunOK(t, bd, dir, "remember", "the format alias is --json", "--key", "fj-alias")
for _, tc := range []struct {
name string
args []string
// mention is a value the --json document must contain, so two empty
// documents cannot match each other.
mention string
}{
{"show", []string{"show", id}, id},
{"list", []string{"list"}, id},
{"ready", []string{"ready"}, id},
{"memories", []string{"memories"}, "fj-alias"},
} {
t.Run(tc.name, func(t *testing.T) {
wantArgs := slices.Concat(tc.args, []string{"--json"})
want, wantErr, exit := bdRunStreams(t, bd, dir, wantArgs...)
if exit != 0 {
t.Fatalf("bd %s exited %d\nstdout:\n%s\nstderr:\n%s", strings.Join(wantArgs, " "), exit, want, wantErr)
}
if !json.Valid([]byte(want)) || !strings.Contains(want, tc.mention) {
t.Fatalf("bd %s did not print one JSON document naming %s:\n%s", strings.Join(wantArgs, " "), tc.mention, want)
}
for _, args := range [][]string{
slices.Concat(tc.args, []string{"--format", "json"}),
slices.Concat(tc.args, []string{"--format=json"}),
slices.Concat([]string{"--format", "json"}, tc.args),
} {
got, gotErr, exit := bdRunStreams(t, bd, dir, args...)
if exit != 0 {
t.Errorf("bd %s exited %d, want 0 like bd %s\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), exit, strings.Join(wantArgs, " "), got, gotErr)
continue
}
if got != want {
t.Errorf("bd %s printed a different document from bd %s\n--format:\n%s\n--json:\n%s", strings.Join(args, " "), strings.Join(wantArgs, " "), got, want)
}
}
})
}
}