beads/cmd/bd/restore_embedded_test.go
Bùi Phan Viết Cường 39aa86fec5
fix(cli): stop eleven subcommands shadowing the root --json flag (#6245)
* fix(cli): stop eleven subcommands shadowing the root --json flag

--json is a persistent flag on rootCmd bound to jsonOutput. Ten
subcommands (restore, admin compact, repo add/remove/list/sync,
migrate, migrate sync/hooks/schema) also registered a local --json bound
to the same variable. pflag's AddFlagSet keeps the local flag and drops
the inherited one, so rootCmd.PersistentFlags().Changed("json") stayed
false for those commands; PersistentPreRunE and
refreshBoundCommandConfig then treated the flag as unset and reset
jsonOutput from config. `bd restore <id> --json` and friends printed text
unless config said json: true. restore_embedded_test.go recorded the
symptom as a "known issue" and skipped its JSON assertion.

bd preflight registered an unbound local Bool("json") instead; it read
its own flag and was not affected by the reset, but it shadowed the root
flag the same way and ignored a configured json default. It now inherits
the root flag like status, update and the proxied close/reopen routes.

Remove the eleven local registrations. Add
TestNoSubcommandShadowsRootJSONFlag, which walks the command tree and
fails on any command that registers its own --json (local or
persistent). Restore the restore_json assertion (parse the output, check
ID and description). Flip the compact and migrate schema registration
tests to assert the flag is inherited, matching stale_test.go.

Visible change: --json moves from each command's Flags section to Global
Flags in --help, with the root description. docs/cli-reference is pinned
to the release tag and regenerates at release time.

* fix(cli): make the --json shadow guard side-effect free

TestNoSubcommandShadowsRootJSONFlag walked every command through
LocalNonPersistentFlags(), and the --json assertions in compact_test.go and
migrate_test.go used InheritedFlags(). Both call cobra's
mergePersistentFlags(), which copies every root persistent flag into the
command's Flags() for the rest of the test process. TestServeFlags pins the
exact flag set of `bd serve`, so `go test ./cmd/bd` failed whenever it ran
after the guard; CI only stayed green because the two tests land in
different name-hash shards.

Compare the command's Flags().Lookup("json") against the root flag pointer
instead. A local registration is a different *pflag.Flag; an inherited one
is the same pointer, and a command that has not merged yet returns nil.
Verified: the four tests pass together, and re-adding a local --json to
`bd restore` still fails the guard.

bd preflight also drops its `jsonOutput, _ := cmd.Flags().GetBool("json")`
shadow of the package-level variable. The root flag is bound to that
variable and PersistentPreRun already resolves the flag/config precedence,
so the local copy was redundant and swallowed the lookup error.

* review: re-key the preflight --json test to the root flag

Base #6293 added TestPreflightJSONFlagBindsGlobal, which opens by requiring
preflightCmd to own a local --json. This PR's guard forbids exactly that, so
the two tests were mutually exclusive and the adopted head was red. The base
test arrived with a base advance after this PR was written, so nothing in the
contributed work is wrong.

Adapt the test rather than restore the local flag: every property it pins
survives re-keyed to the root persistent flag, which is BoolVar'd to the same
package global. The opening assertion becomes the shadow-aware compare this PR
already introduced in compact_test.go, the Changed==true branch is driven
through rootCmd.PersistentFlags().Set (flag.Value.Set never sets Changed), and
the config half is unchanged. Renamed to TestPreflightJSONInheritsRootFlag so
the name stops asserting the very thing the PR removes.

Also correct three CHANGELOG clauses that were true against the contributor's
older base and false at this one: #6293's commandJSONFlagChanged already
consults a subcommand's own Changed bit before the root's, preflight's local
flag was bound rather than unbound, and #6293 already announced that preflight
honors the json config default. The bullet now claims what this PR delivers
here -- deleting the shadowing registrations instead of compensating for them.

Compile the new guard into the Bazel test target. cmd/bd/json_flag_shadow_test.go
was absent from the explicit srcs of go_test(name = "bd_test"), so the invariant
this PR exists to create never ran in the BAZEL* lanes -- and those lanes are
required for merge: CI_GATE_REQUIRED on origin/main lists BAZEL, BAZEL_TEST,
BAZEL_PURE, BAZEL_EMBEDDED, BAZEL_DOLTSERVER, BAZEL_PROXIED and
BAZEL_SERVER_STORAGE. Added in sorted position between jira_test.go and
kv_embedded_test.go; the file is untagged package main, so bd_test is the right
target under the root gazelle:build_tags gms_pure_go. make bazel-sync-check
cannot catch this -- it maintains only the go_srcs filegroup globs, not go_test
srcs.

Finally, reconcile the neighbouring #6293 bullet, which this PR invalidates. It
still described preflight's --json as "bound to the same global every sibling
command binds" and closed on "because an unbound flag reports as unset" -- both
describing the local registration deleted here, fourteen lines from the bullet
announcing the deletion and shipping in the same [Unreleased] section. Trimmed
to the user-visible outcome #6293 still owns; the new bullet carries the
registration story.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-10-01 00:53:08 -07:00

258 lines
7.5 KiB
Go

//go:build cgo
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
"github.com/steveyegge/beads/internal/types"
)
// bdRestore runs "bd restore" with extra args. Returns combined output.
func bdRestore(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"restore"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd restore %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdRestoreFail runs "bd restore" expecting failure.
func bdRestoreFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"restore"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd restore %s should have failed, got: %s", strings.Join(args, " "), out)
}
return string(out)
}
// simulateCompaction creates an issue with content, commits, then marks it as
// compacted by updating compaction_level directly via the embedded store.
// Returns the issue ID.
func simulateCompaction(t *testing.T, bd, dir, beadsDir, database string) string {
t.Helper()
// Create an issue with substantial content (auto-committed by bd create)
id := bdCreateSilent(t, bd, dir, "Compactable issue",
"--description", "This is a long description that will be preserved in history.",
"--design", "Design notes for the compactable issue.")
// Directly update the issue to simulate compaction:
// set compaction_level=1 and truncate description.
// Use a retry loop because the bd subprocess may still hold the lock briefly.
var store *embeddeddolt.EmbeddedDoltStore
var err error
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for i := 0; i < 5; i++ {
store, err = embeddeddolt.Open(ctx, beadsDir, database, "main")
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
if err != nil {
t.Fatalf("open store after retries: %v", err)
}
issue, err := store.GetIssue(ctx, id)
if err != nil {
store.Close()
t.Fatalf("get issue: %v", err)
}
originalSize := len(issue.Description) + len(issue.Design)
if err := store.ApplyCompaction(ctx, id, 1, originalSize, 0, "simulated"); err != nil {
store.Close()
t.Fatalf("apply compaction: %v", err)
}
if err := store.UpdateIssue(ctx, id, map[string]interface{}{
"description": "[compacted]",
"design": "",
}, "test"); err != nil {
store.Close()
t.Fatalf("update issue: %v", err)
}
if err := store.Commit(ctx, "simulate compaction"); err != nil {
store.Close()
t.Fatalf("commit compaction: %v", err)
}
// Close explicitly so the lock is released before the next bd subprocess.
store.Close()
return id
}
func TestEmbeddedRestore(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt restore tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("not_found", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "rstnf")
out := bdRestoreFail(t, bd, dir, "rstnf-nonexistent")
if !strings.Contains(out, "not found") {
t.Errorf("expected 'not found' error, got: %s", out)
}
})
t.Run("not_compacted", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "rstnc")
id := bdCreateSilent(t, bd, dir, "Not compacted issue")
out := bdRestoreFail(t, bd, dir, id)
if !strings.Contains(out, "not compacted") {
t.Errorf("expected 'not compacted' error, got: %s", out)
}
})
t.Run("restore_compacted", func(t *testing.T) {
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "rstok")
id := simulateCompaction(t, bd, dir, beadsDir, "rstok")
out := bdRestore(t, bd, dir, id)
// Should show the restored content from history
if !strings.Contains(out, "long description") {
t.Errorf("expected restored description in output, got: %s", out)
}
})
t.Run("restore_json", func(t *testing.T) {
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "rstjs")
id := simulateCompaction(t, bd, dir, beadsDir, "rstjs")
out := bdRestore(t, bd, dir, id, "--json")
var restored types.Issue
if err := json.Unmarshal([]byte(out), &restored); err != nil {
t.Fatalf("bd restore --json must print a JSON issue, got error %v:\n%s", err, out)
}
if restored.ID != id {
t.Errorf("expected restored issue %s, got %q", id, restored.ID)
}
if !strings.Contains(restored.Description, "long description") {
t.Errorf("expected restored description in JSON, got: %q", restored.Description)
}
})
}
func TestEmbeddedRestoreConcurrent(t *testing.T) {
t.Skip("TODO: simulateCompaction fails under CI concurrency — needs investigation")
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt restore tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "rstcon")
// Create multiple compacted issues
const numIssues = 5
var ids []string
for i := 0; i < numIssues; i++ {
id := bdCreateSilent(t, bd, dir, fmt.Sprintf("Concurrent restore issue %d", i),
"--description", fmt.Sprintf("Content for issue %d that should be in history.", i))
ids = append(ids, id)
}
// Issues are auto-committed by bd create. Simulate compaction on all of them.
// Retry opening the store in case the last bd subprocess still holds the lock.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var store *embeddeddolt.EmbeddedDoltStore
var err error
for i := 0; i < 5; i++ {
store, err = embeddeddolt.Open(ctx, beadsDir, "rstcon", "main")
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
if err != nil {
t.Fatalf("open store after retries: %v", err)
}
for _, id := range ids {
issue, err := store.GetIssue(ctx, id)
if err != nil {
store.Close()
t.Fatalf("get issue %s: %v", id, err)
}
if err := store.ApplyCompaction(ctx, id, 1, len(issue.Description), 0, "sim"); err != nil {
store.Close()
t.Fatalf("apply compaction %s: %v", id, err)
}
if err := store.UpdateIssue(ctx, id, map[string]interface{}{"description": "[compacted]"}, "test"); err != nil {
store.Close()
t.Fatalf("update issue %s: %v", id, err)
}
}
if err := store.Commit(ctx, "compact all"); err != nil {
store.Close()
t.Fatalf("commit compaction: %v", err)
}
store.Close()
// Concurrent restore reads
type result struct {
worker int
out string
err error
}
results := make([]result, numIssues)
var wg sync.WaitGroup
wg.Add(numIssues)
for w := 0; w < numIssues; w++ {
go func(worker int) {
defer wg.Done()
cmd := exec.Command(bd, "restore", ids[worker])
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
results[worker] = result{worker: worker, out: string(out), err: err}
}(w)
}
wg.Wait()
successes := 0
for _, r := range results {
if strings.Contains(r.out, "panic") {
t.Errorf("worker %d panicked:\n%s", r.worker, r.out)
}
if r.err == nil {
successes++
} else if !strings.Contains(r.out, "one writer at a time") {
t.Errorf("worker %d failed with unexpected error: %v\n%s", r.worker, r.err, r.out)
}
}
if successes < 1 {
t.Errorf("expected at least 1 successful restore, got %d", successes)
}
t.Logf("%d/%d restore workers succeeded", successes, numIssues)
}