beads/cmd/bd/export_auto_test.go
Jim Wordelman c4e51986b0
feat(prune): reference-aware bd prune skips closed beads cited by open beads (be-jewoem, be-u2mw2x) (#4023)
* pm: decompose be-zej8mz → 3 builder beads for reference-aware prune

Child beads:
- be-jewoem: core implementation in cmd/bd/purge.go
- be-u2mw2x: --help and README docs update
- be-0wt833: bench asserting <5s on 10K-open-bead fixture (blocked on core)

Plan at docs/plans/reference-aware-prune.md.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(prune): reference-aware bd prune skips closed beads cited by open beads (be-jewoem, be-u2mw2x)

Implements be-5sn FR-01 through FR-06:

- buildReferencedSet scans description/notes/comments of every non-closed
  bead for literal candidate IDs using word-boundary regexp alternation.
  Uses Statuses filter (not ExcludeStatus) to avoid the PG coverage gap
  (be-jdeief guardrail per be-5sn §15).

- runPurgeOrPrune filters referenced beads after pinned-filtering when
  scope.cmdName == "prune" && !scope.ignoreReferences.

- All 4 human output paths updated: preview, dry-run, force, nothing-to-do.
  referenced_skipped/referenced_count always in prune JSON; absent in purge.
  referenced_ids_sample omitted when 0 (per be-zej8mz §5).

- --ignore-references flag registered in pruneCmd.Flags() only; purge
  init() unchanged so bd purge --ignore-references gives "unknown flag".

- pruneCmd.Long gains reference-protection paragraph per be-zej8mz §3.

- README.md adds Maintenance section documenting prune, purge, and the
  reference-aware skip with --ignore-references escape hatch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test(prune): NFR-02 bench test for buildReferencedSet on 10K-bead fixture (be-0wt833)

TestPruneLargeFixture generates 10K open beads × ~5KB bodies + 100 closed
candidates with 20 seeded references; asserts buildReferencedSet completes
<5s and returns exactly the seeded IDs.

Passes in ~1.2s on a standard dev machine. Skips under -short.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(prune): regen CLI reference + release gate PASS (be-jewoem)

Regenerate CLI docs with CGO_ENABLED=0 -tags gms_pure_go binary built
from PR HEAD. Adds --ignore-references flag to prune command docs in
CLI_REFERENCE.md and three website prune.md pages.

Gate evaluation: all 6 criteria PASS (AC-6 integration tests deferred
to be-zkzw per reviewer).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(contributor): bd-umbf contributor namespace isolation (Children 1-3)

Child 1 (be-c7696f): Auto-configure contributor routing in bd init
when upstream remote is detected. Non-interactive, idempotent — skips if
routing.contributor is already set. Creates ~/.beads-planning, sets
routing.mode=auto, routing.contributor, sync.remote=upstream, and pins
git beads.role=contributor.

Child 2 (be-9d2b67): bd export --exclude-owner flag and config-driven
export.exclude_owners / export.exclude_owner config. Filters issues by
created_by before writing JSONL. --verbose emits filtered count.

Child 3 (be-3c5a0f): New bd migrate-personal command. Finds project DB
issues whose created_by matches the current git identity, prompts for
confirmation, then moves them to the planning repo (routing.contributor),
copying labels, dependencies, and comments atomically.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(init): match autoConfigureForkContributor output to be-0ccf34 design spec

Three variants were missing; the one present used wrong symbol (✓ instead of ▶).

Happy path: ▶ header, upstream URL echoed, three ✓ action lines, opt-out hint last.
Opt-out path (--role=maintainer on a fork): ⚠ with explanation and recovery hint.
Re-init path (routing already configured): ⚠ with existing path and reconfigure hint.
Non-interactive/quiet: silent (unchanged).

Pass roleFlag through to autoConfigureForkContributor so the opt-out variant fires
when --role=maintainer is given explicitly on a fork.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(contributor): separate insert/delete phases in migrate-personal; suppress nonInteractive output

migrate-personal now copies all issues to planning DB first, then
deletes them from project DB in a single transaction via DeleteIssues.
A mid-loop failure before the delete phase leaves no issues deleted,
making re-runs safe.

Also pass quiet || nonInteractive to autoConfigureForkContributor so
bd init --non-interactive does not emit fork-detection output.

Fixes B4 and the nonInteractive gap from be-72b53f review.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(prune): regen CLI reference — add bd migrate-personal, update bd export flags

Regenerated from bd 1.0.4 dev (9ed33b68d) with CGO_ENABLED=0 -tags gms_pure_go.
Fixes B1: bd migrate-personal was missing from docs/CLI_REFERENCE.md and
all website cli-reference pages.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(gate): update be-jewoem release gate to cover bd-umbf scope

Expands gate scope from be-jewoem/be-u2mw2x only to include bd-umbf
Children 1-3 commits added to the branch. Documents blocker resolution
status (B1/B2/B4 resolved, B3 pending validator), updates CI results to
latest all-green run 26009209774, and marks overall gate ON HOLD pending
validator tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(gate): update be-jewoem gate — PR description refreshed, B3 dependency chain clarified (2026-05-19)

* test(contributor): be-f9a104 — bd-umbf export+migrate-personal tests

6 tests for bd-umbf Children 2-3 per be-f9a104 scope:

cmd/bd/export_exclude_owner_test.go (be-9d2b67):
- TestExportExcludeOwner_flag: --exclude-owner=alice filters alice's issues
- TestExportExcludeOwner_config: export.exclude_owners config drives filter
- TestExportExcludeOwner_verbose: --verbose reports filtered count

cmd/bd/migrate_personal_test.go (be-3c5a0f):
- TestMigratePersonal_noopWhenEmpty: no matching issues → 0/success
- TestMigratePersonal_movesIssues: personal issues moved to planning repo
- TestMigratePersonal_abortOnNoConfirm: EOF stdin → abort, issues stay

All gate on BEADS_TEST_EMBEDDED_DOLT=1. Fork-detection init tests
covered by be-de99a6 (tests/be-de99a6-fork-detect) per PM scope.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(test): be-36oa — skip TestMigratePersonal_movesIssues on Dolt unavailable

Broaden the skip condition in TestMigratePersonal_movesIssues to also skip
when the planning dir's Dolt server is unavailable (BEADS_DOLT_AUTO_START=0
leaves port=0, causing dolt.New() to fail). Adds "dolt", "cannot connect",
and "server" to the skip-trigger strings alongside the existing routing
and actor-mismatch conditions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test(init): be-de99a6 — fork-detection UX output format tests

4 embedded tests for autoConfigureForkContributor output per be-de99a6:

- TestBdInit_ForkAutoContributor: upstream remote → 'Fork detected' in output,
  routing.mode=auto set
- TestBdInit_ForkAutoContributor_Idempotent: second init run → routing.mode
  still 'auto' (no double-write)
- TestBdInit_ForkAutoContributor_MaintainerFlag: --role=maintainer → routing.mode
  NOT set to auto
- TestBdInit_ForkAutoContributor_NonInteractive: --quiet → no '▶' block, but
  routing still configured

All gate on BEADS_TEST_EMBEDDED_DOLT=1. Uses initForkRepo helper to
add upstream remote for fork detection.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(bd-umbf): be-jewoem B3 — fix yaml/DB store mismatch in export owner-filter and fork routing

Two real bugs found while landing the B3 validator tests:

1. buildOwnerExcludeSet (export.go) was only reading export.exclude_owners
   from the database. The export.* prefix is yaml-only (config.yaml), so
   config set via 'bd config set export.exclude_owners' was invisible to the
   filter. Now reads yaml first, falls back to database.

2. autoConfigureForkContributor (init_contributor.go) was writing
   routing.mode, routing.contributor, and sync.remote via store.SetConfig
   (database). Those keys carry the routing.*/sync.* prefixes which are
   yaml-only. Reading back via 'bd config get routing.mode' always returned
   empty. Now uses config.SetYamlConfig for these keys; idempotency check
   updated to read from yaml too.

Also: update TestBdInit_ForkAutoContributor to verify routing config state
rather than output text (suppressed in non-TTY test environments); remove
temporary debug_fork_test.go; mark B3 RESOLVED in release gate.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(lint): drop unused ctx+store params from autoConfigureForkContributor

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore: release gate PASS for bd-3852 (orphaned children doctor check)

* fix(rebase): update second autoConfigureForkContributor call to 2-arg signature

Later commits in this branch dropped ctx and store params from
autoConfigureForkContributor. The second call site added by be787e4b5
still used the old 4-arg form; update to match the final 2-arg form.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(rebase): drop #4028-superseded fork-detect; take main init.go/init_contributor.go (be-15y28)

PR #4028 landed autoConfigureForkContributor on main. PR #4023 had its own
version of init.go and init_contributor.go plus a fork_detect_embedded_test.go
that collided with TestEmbeddedInit/fork_auto_contributor, causing CI to fail.

Take origin/main's init.go and init_contributor.go verbatim; remove
fork_detect_embedded_test.go. The unique features — reference-aware prune,
export --exclude-owner, bd migrate-personal — are untouched.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(prune): scan active custom statuses in reference-aware skip

buildReferencedSet scanned only the built-in non-closed statuses, so a closed bead cited by a bead in an active *custom* status (status.custom) was unprotected and could be deleted by 'bd prune --pattern ... --force'. Now include every configured custom status whose category != done (via GetCustomStatusesDetailed); the read is required, not best-effort, so a destructive prune never silently under-scans. Adds TestPrune_protectsBeadCitedByCustomStatusBead. Addresses maphew's review of PR #4023 (be-e2nb).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(export): apply export.exclude_owners to auto-export

The export.exclude_owners safety net was applied only by manual 'bd export'; the git-committed auto-export (.beads/issues.jsonl) did not filter, so contributor/personal issues could still leak into git history and PRs. exportToFile now applies the same owner filter (config-sourced owners). Adds TestAutoExportExcludeOwner_config. Addresses maphew's review of PR #4023 (be-e2nb).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(migrate-personal): all-or-error copy preserving comments before delete

migrate-personal copied labels/deps/comments best-effort (warnings) and used event-style AddComment, then deleted the source -- so a partial copy could silently drop comments and dependencies while reporting success. Make the copy all-or-error in two passes (create all issue rows, then copy relations so same-prefix intra-set deps resolve), roll back the planning-DB rows and abort before touching the project DB on any failure, and copy comments via ImportIssueComment (structured row + original timestamp). Adds TestCopyIssueRelations_preservesCommentWithTimestamp and TestMigratePersonal_preservesComments. Addresses maphew's review of PR #4023 (be-e2nb).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(ci): register TestPruneLargeFixture in check-testing-short allowlist

Large-fixture bench introduced in this PR was blocked by pr-policy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(docs): restore version-1.0.5 migrate-personal CLI reference

The 2026-06-03 main merge (72a2a2521) pulled in main's wholesale website
doc regeneration, which dropped
website/versioned_docs/version-1.0.5/cli-reference/migrate-personal.md
(migrate-personal is new in this PR and absent on main). check-doc-flags'
generate-cli-docs.sh --check then failed: the live bd emits the command but
the 1.0.5 snapshot lacked the page, turning Build Artifacts, PR Policy,
Check doc flags freshness, and the CI Gate rollup red.

Regenerated with scripts/generate-cli-docs.sh ./bd — only this file
changed (index.md and llms-full.txt already reference the command).
Verified the full pr-policy sequence passes locally: build-tags,
go-install-guidance, versions, doc-flags (incl. llms-full), doc-freshness,
testing-short, and the jsonl guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(gate): finalize be-jewoem gate — re-review PASS, CI green 2026-06-04

Re-review PASS confirmed (gm-wisp-765hi, 2026-05-22, commit 9cfe6206f).
Post-review CI/docs commits (merge, CI allowlist, version-1.0.5 docs)
are non-behavioral. CI run 26924427657: 51/51 checks SUCCESS.
PR #4023 mergeStateStatus: CLEAN. Gate: definitive PASS.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(docs): true up CLI reference after upstream rebase

Mechanical doc-conflict resolution during the rebase onto upstream/main
(cb33b566a) picked a stale --include-infra description and live
top-level command count. Re-run scripts/generate-cli-docs.sh to
resync docs/CLI_REFERENCE.md and website/docs/cli-reference/* with the
post-rebase bd binary; scripts/check-doc-flags.sh passes clean.

Agent-Signature: claude-fable-5-high on behalf of matt wilkie

* test(export): teach fakeStateHashStore GetConfig for auto-export owner-exclude path (fixes PR Core panic)

buildOwnerExcludeSet reads export.exclude_owners / export.exclude_owner
from the store as a database fallback after the YAML-only lookup. The
fake store used by the auto-export tests embeds a nil storage.DoltStorage
and never implemented GetConfig, so any exercised auto-export path
(TestMaybeAutoExportExportsOnStateHashChange et al.) panicked on the
embedded nil interface. Add a GetConfig stub returning ("", nil), the
same pattern used by other embedded-nil-interface fakes in this package
(captureOptsStore, mockRefStore).

Agent-Signature: claude-code-sonnet-5-medium on behalf of matt wilkie

---------

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-07-05 15:57:27 -07:00

1157 lines
36 KiB
Go

package main
import (
"context"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
)
// TestGitAddFile_InWorktreeHook_StagesCorrectPath is a regression test for
// GH#3311: when bd's pre-commit hook calls git add with GIT_DIR inherited
// from the parent hook invocation, git defaults the work-tree to cwd and
// mis-stages the file at the root of the repo instead of under .beads/.
//
// This test verifies the file ends up staged at .beads/issues.jsonl, not
// at repo-root "issues.jsonl".
func TestGitAddFile_InWorktreeHook_StagesCorrectPath(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
tmpDir, err := os.MkdirTemp("", "bd-gh3311-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
// Resolve symlinks so toplevel comparisons below match git's canonical view
// (on macOS /var -> /private/var).
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
mainRepo := filepath.Join(tmpDir, "main")
if err := os.MkdirAll(mainRepo, 0o755); err != nil {
t.Fatal(err)
}
runGit := func(dir string, args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = dir
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v in %s failed: %v\n%s", args, dir, err, out)
}
}
runGit(mainRepo, "init", "-q")
runGit(mainRepo, "config", "user.email", "t@t")
runGit(mainRepo, "config", "user.name", "t")
if err := os.WriteFile(filepath.Join(mainRepo, "README.md"), []byte("x\n"), 0o644); err != nil {
t.Fatal(err)
}
runGit(mainRepo, "add", "README.md")
runGit(mainRepo, "commit", "-qm", "init")
worktree := filepath.Join(tmpDir, "wt")
runGit(mainRepo, "worktree", "add", worktree, "-b", "feat")
t.Cleanup(func() {
c := exec.Command("git", "worktree", "remove", "--force", worktree)
c.Dir = mainRepo
_ = c.Run()
})
beadsDir := filepath.Join(worktree, ".beads")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatal(err)
}
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
if err := os.WriteFile(jsonlPath, []byte(`{"id":"x"}`+"\n"), 0o644); err != nil {
t.Fatal(err)
}
// Simulate the environment inside a git pre-commit hook: GIT_DIR points
// at the worktree's per-worktree gitdir.
out, err := exec.Command("git", "-C", worktree, "rev-parse", "--git-dir").Output()
if err != nil {
t.Fatal(err)
}
gitDir := strings.TrimSpace(string(out))
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(worktree, gitDir)
}
if gitDir, err = filepath.EvalSymlinks(gitDir); err != nil {
t.Fatal(err)
}
t.Setenv("GIT_DIR", gitDir)
// Call the function under test from a state that matches the hook
// subprocess: cwd not particularly interesting here, but gitAddFile sets
// cmd.Dir = filepath.Dir(path) internally.
t.Chdir(worktree)
if err := gitAddFile(jsonlPath); err != nil {
t.Fatalf("gitAddFile: %v", err)
}
// Inspect the worktree's index: the staged path must be ".beads/issues.jsonl",
// NOT bare "issues.jsonl" at repo root.
lsFiles := exec.Command("git", "ls-files", "--stage")
lsFiles.Dir = worktree
data, err := lsFiles.CombinedOutput()
if err != nil {
t.Fatalf("git ls-files: %v\n%s", err, data)
}
staged := string(data)
if !strings.Contains(staged, ".beads/issues.jsonl") {
t.Errorf("expected .beads/issues.jsonl to be staged, got:\n%s", staged)
}
// Regression guard: the pre-fix bug stages bare "issues.jsonl" at the root.
for _, line := range strings.Split(strings.TrimSpace(staged), "\n") {
// Each line is "<mode> <sha> <stage>\t<path>"
parts := strings.SplitN(line, "\t", 2)
if len(parts) != 2 {
continue
}
if parts[1] == "issues.jsonl" {
t.Errorf("regression: issues.jsonl staged at repo root (GH#3311):\n%s", staged)
}
}
}
// TestScrubGitHookEnv verifies that the env-scrubbing helper drops exactly
// the git-hook-injected variables that would otherwise poison `git add`'s
// repo auto-discovery (or divert its object writes / config).
func TestScrubGitHookEnv(t *testing.T) {
in := []string{
"PATH=/usr/bin",
"GIT_DIR=/some/.git",
"GIT_WORK_TREE=/some",
"GIT_INDEX_FILE=/some/.git/index",
"GIT_COMMON_DIR=/some/.git",
"GIT_PREFIX=sub/",
"GIT_OBJECT_DIRECTORY=/some/.git/objects",
"GIT_ALTERNATE_OBJECT_DIRECTORIES=/elsewhere/.git/objects",
"GIT_CEILING_DIRECTORIES=/home",
"GIT_DISCOVERY_ACROSS_FILESYSTEM=1",
"GIT_CONFIG=/etc/some.conf",
"GIT_CONFIG_COUNT=1",
"GIT_CONFIG_KEY_0=core.worktree",
"GIT_CONFIG_VALUE_0=/elsewhere",
"GIT_CONFIG_PARAMETERS='core.worktree=/elsewhere'",
"GIT_CONFIG_GLOBAL=/tmp/gcfg",
"GIT_CONFIG_SYSTEM=/tmp/scfg",
"GIT_CONFIG_NOSYSTEM=1",
"HOME=/home/u",
// Non-discovery vars that must pass through.
"GIT_AUTHOR_NAME=kept",
"GIT_COMMITTER_EMAIL=kept@example.com",
"GIT_EDITOR=vim",
"GIT_PAGER=less",
}
out := scrubGitHookEnv(in)
joined := strings.Join(out, "\n")
banned := []string{
"GIT_DIR=", "GIT_WORK_TREE=", "GIT_INDEX_FILE=", "GIT_COMMON_DIR=",
"GIT_PREFIX=", "GIT_OBJECT_DIRECTORY=", "GIT_ALTERNATE_OBJECT_DIRECTORIES=",
"GIT_CEILING_DIRECTORIES=", "GIT_DISCOVERY_ACROSS_FILESYSTEM=",
"GIT_CONFIG=", "GIT_CONFIG_COUNT=", "GIT_CONFIG_KEY_0=", "GIT_CONFIG_VALUE_0=",
"GIT_CONFIG_PARAMETERS=", "GIT_CONFIG_GLOBAL=", "GIT_CONFIG_SYSTEM=", "GIT_CONFIG_NOSYSTEM=",
}
for _, b := range banned {
if strings.Contains(joined, b) {
t.Errorf("scrubGitHookEnv leaked %s\nresult:\n%s", b, joined)
}
}
kept := []string{
"PATH=/usr/bin", "HOME=/home/u",
"GIT_AUTHOR_NAME=kept", "GIT_COMMITTER_EMAIL=kept@example.com",
"GIT_EDITOR=vim", "GIT_PAGER=less",
}
for _, k := range kept {
if !strings.Contains(joined, k) {
t.Errorf("scrubGitHookEnv dropped %s\nresult:\n%s", k, joined)
}
}
}
func TestShouldRunPostCommandAutoExportSkipsReadOnlyCommands(t *testing.T) {
if shouldRunPostCommandAutoExport(&cobra.Command{Use: "search"}) {
t.Fatal("search is read-only and must not trigger post-command auto-export")
}
if !shouldRunPostCommandAutoExport(&cobra.Command{Use: "create"}) {
t.Fatal("write commands should still trigger post-command auto-export")
}
}
// fakeStateHashStore implements the storage.StateHasher optional interface
// plus the minimal DoltStorage surface maybeAutoExport touches. Any
// non-overridden method panics via the embedded nil interface.
type fakeStateHashStore struct {
storage.DoltStorage
stateHash string
stateHashCalls int
currentCommitCalls int
issues []*types.Issue
}
func (f *fakeStateHashStore) GetStateHash(_ context.Context) (string, error) {
f.stateHashCalls++
return f.stateHash, nil
}
func (f *fakeStateHashStore) GetCurrentCommit(_ context.Context) (string, error) {
f.currentCommitCalls++
return "head-commit-hash", nil
}
func (f *fakeStateHashStore) GetInfraTypes(_ context.Context) map[string]bool { return nil }
// GetConfig lets buildOwnerExcludeSet's database fallback lookup (for
// export.exclude_owners / export.exclude_owner) run without panicking;
// this fake has no config store, so every key is unset.
func (f *fakeStateHashStore) GetConfig(_ context.Context, _ string) (string, error) {
return "", nil
}
func (f *fakeStateHashStore) SearchIssues(_ context.Context, _ string, _ types.IssueFilter) ([]*types.Issue, error) {
return f.issues, nil
}
// fakeHeadOnlyStore does NOT implement storage.StateHasher, forcing the
// GetCurrentCommit fallback.
type fakeHeadOnlyStore struct {
storage.DoltStorage
currentCommitCalls int
}
func (f *fakeHeadOnlyStore) GetCurrentCommit(_ context.Context) (string, error) {
f.currentCommitCalls++
return "head-commit-hash", nil
}
func autoExportTestDir(t *testing.T) string {
t.Helper()
dir := t.TempDir()
beadsDir := filepath.Join(dir, ".beads")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"database":"beads","backend":"dolt"}`), 0o600); err != nil {
t.Fatal(err)
}
t.Chdir(dir)
t.Setenv("BEADS_DIR", beadsDir)
return dir
}
// Regression test for wy-4ope: server-mode clients used to skip auto-export
// entirely, so `git push` published stale JSONL. maybeAutoExport must reach
// change detection and consult the working-set-aware state hash, not HEAD —
// server mode runs with dolt auto-commit off, so HEAD does not advance on
// writes.
func TestMaybeAutoExportUsesStateHashForChangeDetection(t *testing.T) {
initConfigForTest(t)
config.Set("export.auto", true)
saveAndRestoreGlobals(t)
fake := &fakeStateHashStore{stateHash: "working-set-hash"}
store = fake
dir := autoExportTestDir(t)
saveExportAutoState(filepath.Join(dir, ".beads"), &exportAutoState{
LastDoltCommit: "working-set-hash",
Timestamp: time.Now(),
})
if err := maybeAutoExport(context.Background(), false); err != nil {
t.Fatalf("maybeAutoExport: %v", err)
}
if fake.stateHashCalls != 1 {
t.Fatalf("GetStateHash calls = %d, want 1", fake.stateHashCalls)
}
if fake.currentCommitCalls != 0 {
t.Fatalf("GetCurrentCommit calls = %d, want 0 (StateHasher must take precedence)", fake.currentCommitCalls)
}
if _, err := os.Stat(filepath.Join(dir, ".beads", "issues.jsonl")); !os.IsNotExist(err) {
t.Fatalf("unchanged state hash must not export, stat err=%v", err)
}
}
func TestMaybeAutoExportExportsOnStateHashChange(t *testing.T) {
initConfigForTest(t)
config.Set("export.auto", true)
saveAndRestoreGlobals(t)
fake := &fakeStateHashStore{stateHash: "hash-after-write"}
store = fake
dir := autoExportTestDir(t)
saveExportAutoState(filepath.Join(dir, ".beads"), &exportAutoState{
LastDoltCommit: "hash-before-write",
Timestamp: time.Time{}, // zero: throttle window open
})
if err := maybeAutoExport(context.Background(), false); err != nil {
t.Fatalf("maybeAutoExport: %v", err)
}
state := loadExportAutoState(filepath.Join(dir, ".beads"))
if state.LastDoltCommit != "hash-after-write" {
t.Fatalf("state LastDoltCommit = %q, want %q (export must run when the state hash moves)",
state.LastDoltCommit, "hash-after-write")
}
}
func TestMaybeAutoExportFallsBackToHeadCommit(t *testing.T) {
initConfigForTest(t)
config.Set("export.auto", true)
saveAndRestoreGlobals(t)
fake := &fakeHeadOnlyStore{}
store = fake
dir := autoExportTestDir(t)
saveExportAutoState(filepath.Join(dir, ".beads"), &exportAutoState{
LastDoltCommit: "head-commit-hash",
Timestamp: time.Now(),
})
if err := maybeAutoExport(context.Background(), false); err != nil {
t.Fatalf("maybeAutoExport: %v", err)
}
if fake.currentCommitCalls != 1 {
t.Fatalf("GetCurrentCommit calls = %d, want 1 (fallback when StateHasher is absent)", fake.currentCommitCalls)
}
}
func TestGuardAutoExportOverwriteAllowsViewerScopedJSONL(t *testing.T) {
path := filepath.Join(t.TempDir(), "issues.jsonl")
writeJSONLLines(t, path,
map[string]any{"_type": "issue", "id": "bd-1", "issue_type": "task", "title": "kept"},
map[string]any{"id": "bd-legacy", "issue_type": "bug", "title": "legacy issue record"},
)
if err := guardAutoExportOverwrite(path, map[string]bool{"agent": true}, false); err != nil {
t.Fatalf("guardAutoExportOverwrite: %v", err)
}
}
func TestGuardAutoExportOverwriteBlocksRicherJSONL(t *testing.T) {
path := filepath.Join(t.TempDir(), "issues.jsonl")
writeJSONLLines(t, path,
map[string]any{"_type": "issue", "id": "bd-1", "issue_type": "task", "title": "kept"},
map[string]any{"_type": "memory", "key": "keep-me", "value": "private context"},
map[string]any{"_type": "issue", "id": "bd-agent", "issue_type": "agent", "title": "infra"},
map[string]any{"_type": "issue", "id": "bd-template", "issue_type": "task", "is_template": true},
map[string]any{"_type": "issue", "id": "bd-wisp", "issue_type": "task", "ephemeral": true},
map[string]any{"_type": "event", "id": "bd-event"},
)
err := guardAutoExportOverwrite(path, map[string]bool{"agent": true}, false)
if err == nil {
t.Fatal("expected guardAutoExportOverwrite to reject richer JSONL, got nil")
}
msg := err.Error()
for _, want := range []string{
"refusing to overwrite",
"5 record(s) outside auto-export scope",
"1 memories",
"3 infra/template/ephemeral issues",
"1 unknown",
} {
if !strings.Contains(msg, want) {
t.Fatalf("guard error %q does not contain %q", msg, want)
}
}
}
func TestGuardAutoExportOverwriteAllowsMemoriesWhenIncluded(t *testing.T) {
path := filepath.Join(t.TempDir(), "issues.jsonl")
writeJSONLLines(t, path,
map[string]any{"_type": "memory", "key": "keep-me", "value": "private context"},
)
if err := guardAutoExportOverwrite(path, nil, true); err != nil {
t.Fatalf("guardAutoExportOverwrite with memories included: %v", err)
}
}
func writeJSONLLines(t *testing.T, path string, records ...map[string]any) {
t.Helper()
var b strings.Builder
for _, rec := range records {
data, err := json.Marshal(rec)
if err != nil {
t.Fatal(err)
}
b.Write(data)
b.WriteByte('\n')
}
if err := os.WriteFile(path, []byte(b.String()), 0o644); err != nil {
t.Fatal(err)
}
}
// TestPathInsideDir covers the common structural cases plus the
// fresh-file + symlinked-parent case that tripped the initial fix
// (macOS /tmp -> /private/tmp asymmetry when the target file doesn't
// yet exist).
func TestPathInsideDir(t *testing.T) {
tmpRaw, err := os.MkdirTemp("", "bd-pathinside-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpRaw) })
// Provoke a symlinked-parent asymmetry: keep `raw` as the un-resolved
// tmp form (/tmp/...) and derive `real` as the canonical form
// (/private/tmp/...) so tests can compare across the boundary.
real, err := filepath.EvalSymlinks(tmpRaw)
if err != nil {
t.Fatal(err)
}
wt := filepath.Join(real, "wt")
if err := os.MkdirAll(wt, 0o755); err != nil {
t.Fatal(err)
}
wtRaw := filepath.Join(tmpRaw, "wt") // un-resolved view of same dir
existing := filepath.Join(wt, "existing.txt")
if err := os.WriteFile(existing, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
cases := []struct {
name string
path string
dir string
want bool
}{
{"identical paths", wt, wt, true},
{"existing descendant", existing, wt, true},
{"fresh nonexistent descendant", filepath.Join(wt, "not-yet.txt"), wt, true},
{"sibling path with shared prefix", filepath.Join(real, "wt-other/x"), wt, false},
{"outside dir", filepath.Join(real, "elsewhere/x"), wt, false},
// The regression: fresh path expressed via /tmp symlink vs dir
// expressed via /private/tmp canonical. Must still say "inside".
{"fresh path with symlinked parent form", filepath.Join(wtRaw, "fresh.txt"), wt, true},
{"existing path with symlinked parent form", filepath.Join(wtRaw, "existing.txt"), wt, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := pathInsideDir(tc.path, tc.dir)
if got != tc.want {
t.Errorf("pathInsideDir(%q, %q) = %v, want %v", tc.path, tc.dir, got, tc.want)
}
})
}
}
// TestHookWorkTreeRoot covers the documented GIT_DIR shapes and the
// not-a-hook case.
func TestHookWorkTreeRoot(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "bd-hwt-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
// Case 1: GIT_DIR not set → "" (normal non-hook context).
if err := os.Unsetenv("GIT_DIR"); err != nil {
t.Fatal(err)
}
if got := hookWorkTreeRoot(); got != "" {
t.Errorf("with GIT_DIR unset: hookWorkTreeRoot = %q, want \"\"", got)
}
// Case 2: linked-worktree style — GIT_DIR = main/.git/worktrees/<n>,
// and that dir contains a `gitdir` file pointing at the worktree's
// .git file. Worktree root = parent of that .git file.
wtDotGit := filepath.Join(tmpDir, "wt", ".git")
if err := os.MkdirAll(filepath.Dir(wtDotGit), 0o755); err != nil {
t.Fatal(err)
}
linkedGitDir := filepath.Join(tmpDir, "main", ".git", "worktrees", "wt")
if err := os.MkdirAll(linkedGitDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(linkedGitDir, "gitdir"), []byte(wtDotGit+"\n"), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("GIT_DIR", linkedGitDir)
if got, want := hookWorkTreeRoot(), filepath.Dir(wtDotGit); got != want {
t.Errorf("linked worktree: hookWorkTreeRoot = %q, want %q", got, want)
}
// Case 3: plain repo — GIT_DIR = <repo>/.git. Worktree root is its parent.
plainGitDir := filepath.Join(tmpDir, "plain", ".git")
if err := os.MkdirAll(plainGitDir, 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("GIT_DIR", plainGitDir)
if got, want := hookWorkTreeRoot(), filepath.Dir(plainGitDir); got != want {
t.Errorf("plain repo: hookWorkTreeRoot = %q, want %q", got, want)
}
// Case 4: unrecognized shape (no gitdir file, basename != .git) → "".
// Bare-repo-ish; we conservatively decline to identify a worktree.
bare := filepath.Join(tmpDir, "bare.git")
if err := os.MkdirAll(bare, 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("GIT_DIR", bare)
if got := hookWorkTreeRoot(); got != "" {
t.Errorf("bare/unrecognized GIT_DIR: hookWorkTreeRoot = %q, want \"\"", got)
}
}
// TestGitAddFile_NonHookContext_GuardDoesNotFire verifies the worktree
// guard is a no-op when GIT_DIR is not set (normal bd invocation, not
// inside a git hook). Regression guard so a future tightening of
// hookWorkTreeRoot does not silently break the common path.
func TestGitAddFile_NonHookContext_GuardDoesNotFire(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
tmpDir, err := os.MkdirTemp("", "bd-nonhook-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
repo := filepath.Join(tmpDir, "repo")
if err := os.MkdirAll(repo, 0o755); err != nil {
t.Fatal(err)
}
runGit := func(args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = repo
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
runGit("init", "-q")
runGit("config", "user.email", "t@t")
runGit("config", "user.name", "t")
target := filepath.Join(repo, ".beads", "issues.jsonl")
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(target, []byte(`{"id":"x"}`+"\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Unsetenv("GIT_DIR"); err != nil {
t.Fatal(err)
}
t.Chdir(repo)
if err := gitAddFile(target); err != nil {
t.Fatalf("gitAddFile: %v", err)
}
c := exec.Command("git", "ls-files", "--stage")
c.Dir = repo
data, err := c.CombinedOutput()
if err != nil {
t.Fatalf("ls-files: %v\n%s", err, data)
}
if !strings.Contains(string(data), ".beads/issues.jsonl") {
t.Errorf("non-hook path did not stage .beads/issues.jsonl:\n%s", data)
}
}
// TestGitAddFile_CapturesStderrOnFailure verifies that when `git add` fails,
// the returned error wraps git's stderr text instead of just the bare exit
// status. Regression guard for the silent "Warning: auto-export: git add
// failed: exit status 1" noise where the user has no signal as to why.
func TestGitAddFile_CapturesStderrOnFailure(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
tmpDir, err := os.MkdirTemp("", "bd-stderr-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
repo := filepath.Join(tmpDir, "repo")
if err := os.MkdirAll(repo, 0o755); err != nil {
t.Fatal(err)
}
runGit := func(args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = repo
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
runGit("init", "-q")
runGit("config", "user.email", "t@t")
runGit("config", "user.name", "t")
// Force git add to fail by gitignoring the target. Common real-world
// trigger: a parent .gitignore excluding .beads/ that the user is
// unaware of.
if err := os.WriteFile(filepath.Join(repo, ".gitignore"), []byte(".beads/\n"), 0o644); err != nil {
t.Fatal(err)
}
target := filepath.Join(repo, ".beads", "issues.jsonl")
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(target, []byte(`{"id":"x"}`+"\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Unsetenv("GIT_DIR"); err != nil {
t.Fatal(err)
}
t.Chdir(repo)
err = gitAddFile(target)
if err == nil {
t.Fatal("expected gitAddFile to fail on gitignored target, got nil")
}
msg := err.Error()
// Bare-exit-status regression guard: pre-fix message was just "exit
// status 1" with nothing else. Post-fix must include git's stderr.
if !strings.Contains(strings.ToLower(msg), "ignored") {
t.Errorf("expected error to surface git's stderr (containing 'ignored'), got: %q", msg)
}
}
// TestGitAddFile_CapturesLockedIndexFailure verifies that a locked git index
// is surfaced as a rich, caller-visible error rather than a bare exit status.
func TestGitAddFile_CapturesLockedIndexFailure(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
tmpDir, err := os.MkdirTemp("", "bd-index-lock-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
repo := filepath.Join(tmpDir, "repo")
if err := os.MkdirAll(repo, 0o755); err != nil {
t.Fatal(err)
}
runGit := func(args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = repo
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
runGit("init", "-q")
runGit("config", "user.email", "t@t")
runGit("config", "user.name", "t")
target := filepath.Join(repo, ".beads", "issues.jsonl")
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(target, []byte(`{"id":"x"}`+"\n"), 0o644); err != nil {
t.Fatal(err)
}
lockPath := filepath.Join(repo, ".git", "index.lock")
if err := os.WriteFile(lockPath, []byte("held by another git process"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Unsetenv("GIT_DIR"); err != nil {
t.Fatal(err)
}
t.Chdir(repo)
err = gitAddFile(target)
if err == nil {
t.Fatal("expected gitAddFile to fail while index.lock exists, got nil")
}
if msg := err.Error(); !strings.Contains(msg, "index is locked") || !strings.Contains(msg, "index.lock") {
t.Fatalf("expected index.lock error, got: %q", msg)
}
c := exec.Command("git", "ls-files", "--stage")
c.Dir = repo
data, err := c.CombinedOutput()
if err != nil {
t.Fatalf("ls-files: %v\n%s", err, data)
}
if strings.Contains(string(data), ".beads/issues.jsonl") {
t.Fatalf("gitAddFile staged target despite index.lock:\n%s", data)
}
}
func TestAutoExportGitAddFailureExitsNonZero(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
bd := buildBDForInitTests(t)
dir := t.TempDir()
env := append(autoExportDataLossTestEnv(dir), "BD_NON_INTERACTIVE=1")
runGit := func(args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = dir
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
runGit("init", "-q")
run := func(args ...string) string {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd %v failed: %v\n%s", args, err, out)
}
return string(out)
}
run("init", "--prefix", "agf", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents")
if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte(".beads/\n"), 0o644); err != nil {
t.Fatal(err)
}
run("config", "set", "export.interval", "1ms")
run("config", "set", "export.auto", "true")
run("config", "set", "export.git-add", "true")
if err := os.Remove(filepath.Join(dir, ".beads", exportAutoStateFile)); err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
cmd := exec.Command(bd, "create", "caller visible git add failure", "-p", "2")
cmd.Dir = dir
cmd.Env = env
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd create succeeded despite auto-export git add failure:\n%s", out)
}
output := string(out)
if !strings.Contains(output, "Error: auto-export: git add failed") {
t.Fatalf("expected caller-visible auto-export git add error, got:\n%s", output)
}
if !strings.Contains(strings.ToLower(output), "ignored") {
t.Fatalf("expected git add stderr to explain ignored path, got:\n%s", output)
}
if _, err := os.Stat(filepath.Join(dir, ".beads", exportAutoStateFile)); !os.IsNotExist(err) {
t.Fatalf("git-add failure should not save export state, stat err=%v", err)
}
}
// TestGitAddFile_RedirectCase_DoesNotStageInMainRepo regresses the
// silent-stage-in-main follow-up from the GH#3311 review: when a worktree
// has .beads/redirect -> main/.beads, the worktree's pre-commit hook must
// NOT stage the redirected path into main's index. That would silently
// pollute a repo the user did not tell us to touch. Expected behavior is
// to skip staging entirely (the file content on disk is still correct).
func TestGitAddFile_RedirectCase_DoesNotStageInMainRepo(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
tmpDir, err := os.MkdirTemp("", "bd-gh3311-redirect-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
mainRepo := filepath.Join(tmpDir, "main")
if err := os.MkdirAll(mainRepo, 0o755); err != nil {
t.Fatal(err)
}
runGit := func(dir string, args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = dir
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v in %s failed: %v\n%s", args, dir, err, out)
}
}
runGit(mainRepo, "init", "-q")
runGit(mainRepo, "config", "user.email", "t@t")
runGit(mainRepo, "config", "user.name", "t")
if err := os.WriteFile(filepath.Join(mainRepo, "README.md"), []byte("x\n"), 0o644); err != nil {
t.Fatal(err)
}
runGit(mainRepo, "add", "README.md")
runGit(mainRepo, "commit", "-qm", "init")
// Create main's .beads directory with an issues.jsonl the hook would
// target via the redirect.
mainBeads := filepath.Join(mainRepo, ".beads")
if err := os.MkdirAll(mainBeads, 0o755); err != nil {
t.Fatal(err)
}
mainJSONL := filepath.Join(mainBeads, "issues.jsonl")
if err := os.WriteFile(mainJSONL, []byte(`{"id":"from-redirect"}`+"\n"), 0o644); err != nil {
t.Fatal(err)
}
// Create worktree; GIT_DIR env var simulation captures the hook context.
worktree := filepath.Join(tmpDir, "wt")
runGit(mainRepo, "worktree", "add", worktree, "-b", "feat")
t.Cleanup(func() {
c := exec.Command("git", "worktree", "remove", "--force", worktree)
c.Dir = mainRepo
_ = c.Run()
})
out, err := exec.Command("git", "-C", worktree, "rev-parse", "--git-dir").Output()
if err != nil {
t.Fatal(err)
}
gitDir := strings.TrimSpace(string(out))
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(worktree, gitDir)
}
if gitDir, err = filepath.EvalSymlinks(gitDir); err != nil {
t.Fatal(err)
}
t.Setenv("GIT_DIR", gitDir)
// Act: stage the main-repo-resident path from inside the worktree hook.
t.Chdir(worktree)
if err := gitAddFile(mainJSONL); err != nil {
t.Fatalf("gitAddFile: %v", err)
}
// Assert: neither the worktree's index nor main's index got a bogus
// staging entry from the worktree's hook firing.
checkNoStage := func(label, repoDir string) {
t.Helper()
c := exec.Command("git", "ls-files", "--stage")
c.Dir = repoDir
data, err := c.CombinedOutput()
if err != nil {
t.Fatalf("%s: ls-files: %v\n%s", label, err, data)
}
for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {
parts := strings.SplitN(line, "\t", 2)
if len(parts) != 2 {
continue
}
if strings.Contains(parts[1], "issues.jsonl") {
t.Errorf("%s staged issues.jsonl when it should not have; ls-files output:\n%s", label, data)
}
}
}
// Both checks use env with GIT_DIR unset so we observe each repo's
// own index rather than routing through the inherited hook gitdir.
// t.Setenv can only set (not unset); the outer Setenv of GIT_DIR has
// a Cleanup that restores it, so unsetting here is safe for the rest
// of this test and the outer cleanup will re-set if another test
// relies on the parent env.
if err := os.Unsetenv("GIT_DIR"); err != nil {
t.Fatal(err)
}
checkNoStage("worktree", worktree)
checkNoStage("main", mainRepo)
}
func TestPreCommitHasStagedBeadsFiles(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
tmpDir, err := os.MkdirTemp("", "bd-staged-beads-*")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
repo := filepath.Join(tmpDir, "repo")
if err := os.MkdirAll(filepath.Join(repo, ".beads"), 0o755); err != nil {
t.Fatal(err)
}
runGit := func(args ...string) {
t.Helper()
c := exec.Command("git", args...)
c.Dir = repo
if out, err := c.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
runGit("init", "-q")
runGit("config", "user.email", "t@t")
runGit("config", "user.name", "t")
readme := filepath.Join(repo, "README.md")
if err := os.WriteFile(readme, []byte("code\n"), 0o644); err != nil {
t.Fatal(err)
}
runGit("add", "README.md")
if preCommitHasStagedBeadsFiles(filepath.Join(repo, ".beads")) {
t.Fatal("staged non-.beads file should not trigger pre-commit JSONL export")
}
configPath := filepath.Join(repo, ".beads", "config.yaml")
if err := os.WriteFile(configPath, []byte("export:\n auto: true\n"), 0o644); err != nil {
t.Fatal(err)
}
runGit("add", ".beads/config.yaml")
if !preCommitHasStagedBeadsFiles(filepath.Join(repo, ".beads")) {
t.Fatal("staged .beads file should trigger pre-commit JSONL export")
}
}
func TestCommandAllowsEmptyAutoExport(t *testing.T) {
commandMayEmptyJSONLExport.Store(false)
t.Cleanup(func() { commandMayEmptyJSONLExport.Store(false) })
if commandAllowsEmptyAutoExport(&cobra.Command{Use: "prune"}) {
t.Fatal("prune should not allow an empty auto-export before deleting rows")
}
commandMayEmptyJSONLExport.Store(true)
if !commandAllowsEmptyAutoExport(&cobra.Command{Use: "prune"}) {
t.Fatal("prune should allow an intentional empty auto-export")
}
if !commandAllowsEmptyAutoExport(&cobra.Command{Use: "purge"}) {
t.Fatal("purge should allow an intentional empty auto-export")
}
if commandAllowsEmptyAutoExport(&cobra.Command{Use: "create"}) {
t.Fatal("create should not bypass empty auto-export protection")
}
}
// TestShouldExport covers the pure throttle-window decision used by
// maybeAutoExport. Adapted from Jeremy Longshore's GH#4061 refactor.
func TestShouldExport(t *testing.T) {
now := time.Now()
cases := []struct {
name string
state *exportAutoState
interval time.Duration
want bool
}{
{
name: "first run always exports",
state: &exportAutoState{},
interval: time.Minute,
want: true,
},
{
name: "throttle window active blocks",
state: &exportAutoState{Timestamp: now.Add(-10 * time.Second)},
interval: time.Minute,
want: false,
},
{
name: "throttle window elapsed allows",
state: &exportAutoState{Timestamp: now.Add(-2 * time.Minute)},
interval: time.Minute,
want: true,
},
{
name: "at interval boundary allows",
state: &exportAutoState{Timestamp: now.Add(-time.Minute)},
interval: time.Minute,
want: true,
},
{
name: "zero interval allows",
state: &exportAutoState{Timestamp: now.Add(-time.Microsecond)},
interval: 0,
want: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := shouldExport(tc.state, tc.interval); got != tc.want {
t.Errorf("shouldExport(%+v, %s) = %v, want %v", tc.state, tc.interval, got, tc.want)
}
})
}
}
func TestCountIssueRecordsInJSONL(t *testing.T) {
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "issues.jsonl")
data := strings.Join([]string{
`{"_type":"issue","id":"bd-1","status":"open"}`,
`{"id":"bd-2","status":"closed"}`,
`{"_type":"issue","id":"bd-2","status":"closed"}`,
`{"_type":"memory","key":"note","value":"private"}`,
`{"_type":"issue","id":"bd-3","status":"tombstone"}`,
`{"_type":"issue","title":"missing id"}`,
``,
}, "\n")
if err := os.WriteFile(path, []byte(data), 0o644); err != nil {
t.Fatal(err)
}
got, err := countIssueRecordsInJSONL(path)
if err != nil {
t.Fatalf("countIssueRecordsInJSONL: %v", err)
}
if got != 2 {
t.Fatalf("countIssueRecordsInJSONL = %d, want 2", got)
}
ids, err := issueIDsInJSONL(path)
if err != nil {
t.Fatalf("issueIDsInJSONL: %v", err)
}
if got := strings.Join(ids, ","); got != "bd-1,bd-2" {
t.Fatalf("issueIDsInJSONL = %q, want bd-1,bd-2", got)
}
}
func TestAutoExportSkipsEmptyExportOverPopulatedJSONL(t *testing.T) {
bd := buildBDForInitTests(t)
dir := t.TempDir()
env := autoExportDataLossTestEnv(dir)
run := func(args ...string) string {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd %v failed: %v\n%s", args, err, out)
}
return string(out)
}
run("init", "--prefix", "dl", "--non-interactive")
run("config", "set", "export.path", "custom.jsonl")
jsonlPath := filepath.Join(dir, ".beads", "custom.jsonl")
original := []byte(`{"_type":"issue","id":"dl-1","title":"Recovered issue","priority":1,"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-01T00:00:00Z"}` + "\n")
if err := os.WriteFile(jsonlPath, original, 0o644); err != nil {
t.Fatal(err)
}
run("config", "set", "export.auto", "true")
out := run("remember", "private context that should not be auto-exported")
if !strings.Contains(out, "refusing to overwrite") {
t.Fatalf("expected auto-export refusal warning, got:\n%s", out)
}
got, err := os.ReadFile(jsonlPath)
if err != nil {
t.Fatalf("expected populated JSONL to remain: %v", err)
}
if string(got) != string(original) {
t.Fatalf("populated JSONL was modified:\n%s", got)
}
if _, err := os.Stat(filepath.Join(dir, ".beads", exportAutoStateFile)); !os.IsNotExist(err) {
t.Fatalf("empty skipped auto-export should not save export state, stat err=%v", err)
}
}
func TestAutoExportSkipsWhenExistingJSONLHasIDsMissingFromStore(t *testing.T) {
bd := buildBDForInitTests(t)
dir := t.TempDir()
env := autoExportDataLossTestEnv(dir)
run := func(args ...string) string {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd %v failed: %v\n%s", args, err, out)
}
return string(out)
}
run("init", "--prefix", "dl", "--non-interactive")
run("config", "set", "export.path", "custom.jsonl")
run("create", "local issue", "-p", "2")
jsonlPath := filepath.Join(dir, ".beads", "custom.jsonl")
original := []byte(strings.Join([]string{
`{"_type":"issue","id":"dl-1","title":"Local issue","priority":2,"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-01T00:00:00Z"}`,
`{"_type":"issue","id":"dl-jsonl-only","title":"Only in JSONL","priority":1,"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-01T00:00:00Z"}`,
``,
}, "\n"))
if err := os.WriteFile(jsonlPath, original, 0o644); err != nil {
t.Fatal(err)
}
run("config", "set", "export.interval", "1ms")
run("config", "set", "export.auto", "true")
out := run("create", "another local issue", "-p", "2")
if !strings.Contains(out, "JSONL-only issue record") || !strings.Contains(out, "dl-jsonl-only") {
t.Fatalf("expected JSONL-only refusal warning, got:\n%s", out)
}
got, err := os.ReadFile(jsonlPath)
if err != nil {
t.Fatalf("expected JSONL to remain: %v", err)
}
if string(got) != string(original) {
t.Fatalf("JSONL-only records were overwritten:\n%s", got)
}
if _, err := os.Stat(filepath.Join(dir, ".beads", exportAutoStateFile)); !os.IsNotExist(err) {
t.Fatalf("skipped auto-export should not save export state, stat err=%v", err)
}
}
func autoExportDataLossTestEnv(home string) []string {
env := make([]string, 0, len(os.Environ())+3)
for _, e := range os.Environ() {
if strings.HasPrefix(e, "BEADS_") {
continue
}
env = append(env, e)
}
return append(env, "HOME="+home, "BEADS_DOLT_AUTO_START=0", "BEADS_NO_DAEMON=1", "BD_DISABLE_METRICS=1", "BD_DISABLE_EVENT_FLUSH=1")
}