beads/cmd/bd/dolt_worktree_remote_test.go
Okano, Osamu 55625dbef2
Fix worktree-aware config.yaml resolution (#3265)
* Fix worktree-aware config.yaml resolution

* fix: honor shared worktree config resolution

* fix(doctor-fix): resolve shared-worktree beads paths

intent(doctor-fix): review-fix-loop found that doctor --fix should keep working when a worktree uses a shared .beads directory with no local path
constraint(doctor-fix): permission fixes must still skip chmod on local symlinked .beads directories when they exist
decision(doctor-fix): centralize resolved workspace lookup in a helper and route fix handlers through it instead of rebuilding path/.beads ad hoc

* test(doctor-fix): cover shared-worktree path resolution

intent(doctor-fix): review-fix-loop needed regressions that prove fix handlers operate on the shared workspace when a worktree has no local .beads
decision(doctor-fix): cover one config-regeneration path and two file-mutation paths so future regressions fail close to the real worktree behavior
learned(doctor-fix): metadata regeneration, stale lock cleanup, and permission repair all exercised separate path assumptions inside the fix package

* test(doctor-fix): strengthen workspace validation coverage

Why:
- shared-worktree fix coverage still left validateBeadsWorkspace edge cases loosely asserted
- fix package had no direct test for resolvedWorkspaceBeadsDir local vs shared precedence

What:
- add focused tests for resolvedWorkspaceBeadsDir local/shared selection
- make validateBeadsWorkspace reject file-backed and file-symlinked .beads explicitly

Refs:
- beads-xc4

* refactor(doctor-fix): share workspace beads dir resolution

Why:
- fix package resolved local and shared .beads paths in multiple steps even though they come from the same workspace root
- permissions handling was correct but harder to follow because local/resolved values were stitched together at the call site

What:
- extract a package-local helper that returns both local and resolved .beads paths from one workspace lookup
- make Permissions reuse that helper and add direct tests for local path behavior

Refs:
- beads-rim

* fix(config): remove shared-worktree import cycle

Why:
- shared-worktree config fallback in internal/config imported internal/beads
- that pulled internal/utils back into internal/utils test setup through storage/dolt and caused PR #37 CI import cycle failures

What:
- keep worktree fallback resolution inside internal/config
- reuse the repo-scoped git-dir fallback for config.yaml discovery without importing internal/beads

Refs:
- beads-3ix

* fix(doctor): resolve database-integrity against shared worktrees

intent(doctor): keep integrity recovery from reading a nonexistent worktree-local .beads during shared-worktree repairs
decision(database-integrity): resolve config and dolt paths from resolvedWorkspaceBeadsDir while keeping bd init --force anchored to the worktree repo root
learned(shared-worktree): the server-mode guard must inspect shared config or it can silently bypass the configured database safety check

* fix(doctor): honor shared worktrees in remote-consistency

intent(doctor): keep remote reconciliation pointed at the active shared workspace state during worktree repairs
decision(remote-consistency): resolve shared-worktree config and dolt paths once before comparing SQL and CLI remotes
constraint(remote-consistency): the fixer still runs from the worktree repo path but cannot derive paths from repoPath/.beads when that directory is absent

* fix(doctor): align shared worktree resolution (beads-u8z)

* test(config): cover shared worktree config precedence (beads-7kw)

intent(config): lock down shared worktree config resolution so future fallback fixes do not silently write or read the wrong file
decision(config-tests): cover both write precedence and Initialize() local override behavior in worktree-specific tests
constraint(shared-worktree): runtime resolution can legitimately target either worktree-local or shared .beads, so the regression coverage must assert both paths explicitly

* Add worktree path coverage for beads dir resolution

* Avoid creating .beads on normal Dolt store opens

* Prefer loaded worktree config when finding project config

* test(worktree): add shared config regression coverage (beads-z65b)

intent(worktree-config): keep shared-worktree sync.remote persistence covered at both the YAML helper and CLI command layers

decision(regression-tests): pair a loaded-state config test with a bd subprocess test because the original regression crossed initialization and command execution
constraint(cmd-bd): use the repo CGO test wrapper for command coverage because raw go test ./cmd/bd requires local ICU setup outside the supported path

* fix(doctor): sweep shared-worktree resolver into remaining checks (beads-3y9p)

intent: keep this PR's worktree bugfix coherent by removing the last redirect-only doctor resolution paths

decision: route standalone doctor checks through ResolveBeadsDirForRepo and add shared-worktree regressions for locks, mq cleanup, hook migrations, and remote consistency

constraint: leave local-only semantics alone and validate with targeted doctor tests instead of broad environment-sensitive full-suite expectations

* test(worktree): strengthen resolver regression coverage (beads-i9x1)

intent(worktree-coverage): raise patch coverage for the shared-worktree resolver PR on branches introduced by this change set
decision(config-tests): cover worktree config helpers directly in internal/config so fallback and loaded-state branches stay deterministic
decision(storage-tests): exercise lazy federation credential-key initialization with one server-backed peer lifecycle instead of broader end-to-end sync flows
constraint(doctor-tests): verify tracked runtime cleanup from the resolved main repo because shared .beads lives outside the worktree root

* fix(bootstrap): persist sync remote in resolved config

intent(bootstrap): fresh worktree bootstrap should finalize the shared workspace config instead of failing on a local .beads stub
decision(config-resolution): add a targeted yaml writer for an explicit beads directory so bootstrap can reuse the resolved workspace path
constraint(worktree-config): bare-parent and shared worktree setups can expose a local .beads directory that must not shadow the canonical config.yaml during finalize

* test(config): cover worktree resolution edge paths (beads-adan)

intent(codecov): PR #3265 needed patch coverage above the Codecov complaint on config/worktree resolution changes
decision(tests): add unit coverage in existing config, doctor/fix, and credentials suites instead of broader e2e coverage
constraint(tests): keep sanitizeDBName available to nocgo typecheck because init.go calls it outside cgo-only files

* test(embedded): allow concurrent dolt status

---------

Co-authored-by: matt wilkie <maphew@gmail.com>
2026-04-24 07:33:31 -07:00

52 lines
1.6 KiB
Go

//go:build cgo
package main
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestDoltRemoteAddPersistsSyncRemoteToSharedWorktreeConfig(t *testing.T) {
skipIfNoDolt(t)
if runtime.GOOS == "windows" {
t.Skip("Skipping worktree test on Windows")
}
bd := buildBDForInitTests(t)
bareDir, worktreeDir := setupBareParentInitWorktree(t)
bareBeadsDir := filepath.Join(bareDir, ".beads")
sharedEnv := append(os.Environ(), "BEADS_DOLT_SHARED_SERVER=1")
initCmd := exec.Command(bd, "init", "--prefix", "remote-sync", "--skip-hooks", "--quiet")
initCmd.Dir = worktreeDir
initCmd.Env = sharedEnv
if out, err := initCmd.CombinedOutput(); err != nil {
t.Fatalf("bd init from bare-parent worktree failed: %v\n%s", err, out)
}
remoteURL := "git+ssh://git@example.com/acme/beads.git"
addCmd := exec.Command(bd, "dolt", "remote", "add", "origin", remoteURL)
addCmd.Dir = worktreeDir
addCmd.Env = sharedEnv
if out, err := addCmd.CombinedOutput(); err != nil {
t.Fatalf("bd dolt remote add from bare-parent worktree failed: %v\n%s", err, out)
}
configPath := filepath.Join(bareBeadsDir, "config.yaml")
content, err := os.ReadFile(configPath)
if err != nil {
t.Fatalf("failed to read shared config.yaml: %v", err)
}
if !strings.Contains(string(content), `sync.remote: "`+remoteURL+`"`) {
t.Fatalf("expected shared config.yaml to contain sync.remote, got:\n%s", string(content))
}
if _, err := os.Stat(filepath.Join(worktreeDir, ".beads")); !os.IsNotExist(err) {
t.Fatalf("expected no worktree-local .beads directory after remote add, got err=%v", err)
}
}