beads/cmd/bd/setup_test.go
Okano, Osamu ef6c6e3e48
Follow up remaining runtime .beads fallbacks after GH#3124 (#3183)
* fix(workspace): align resolved workspace messaging and config lookup

intent(workspace): pure worktrees and external BEADS_DIR setups should work without implying a local .beads directory is required
decision(config): derive config repo roots from FindBeadsDir-backed workspace resolution instead of a local-only .beads walk
constraint(workspace-detection): keep FindBeadsDir ordering and storage behavior unchanged while making bd where the primary diagnostic hint
learned(testing): shared-server env and global .beads ignore rules can distort CGO suites, so regression coverage now normalizes those test paths

* fix(bootstrap): align no-workspace JSON wording

intent(bootstrap): machine-readable no-workspace output should match the resolved-workspace guidance used elsewhere on this branch
decision(bootstrap-json): reuse the shared workspace error helpers through a dedicated payload builder so tests can lock the JSON contract
constraint(review-loop): keep the bootstrap CLI flow unchanged outside the no-workspace JSON branch while adding a focused regression test

* fix(where): allow no-workspace diagnostics without db (beads-9f74-5l1)

intent(where): no-workspace guidance should point to a command that actually runs when workspace resolution fails
decision(startup): treat top-level where as a no-db command so it reaches its own handler before database preflight
constraint(where-tests): subprocess mode can change the exact hint text, so the regression test locks the JSON contract and stable guidance fragments instead

* fix(bootstrap): restore bd where guidance for no-workspace output (beads-9f74-3c3)

intent(bootstrap): no-workspace bootstrap guidance should match the resolved-workspace diagnostics introduced on this branch
decision(no-workspace-hints): reuse the shared hint that mentions bd where instead of the bootstrap-specific fallback text
constraint(bootstrap-tests): cover both the payload helper and subprocess CLI path so wording regressions are caught before release

* fix(workspace): remove no-workspace .beads fallbacks (beads-9f74-gr1)

intent(workspace-resolution): finish the GH#3124 follow-up for runtime paths that still fabricated local .beads state
rejected(no-workspace-fallback): inventing ./ .beads paths outside an active workspace hid shared-worktree resolution and created stray files
decision(setup-recipes): keep built-in bd setup recipes usable without a workspace but require an active workspace for custom recipe state
constraint(worktree-support): completion and backup helpers still need to resolve shared main-repo workspaces from a git worktree

* fix(config): skip no-workspace store init for store-free commands

intent(config): review-fix-loop found no-workspace config commands should reach their own graceful paths instead of failing in PersistentPreRun
decision(config-preflight): detect store-free config subcommands before DB synthesis so config.yaml, git-backed, and diagnostic flows can run without an opened store
constraint(json-contract): keep bd config validate --json machine-readable on stdout once pre-run stops short-circuiting into low-level DB errors
learned(config-preflight): the parent config command not being in noDbCommands leaked store init into show, validate, drift, apply, and yaml-only config helpers

* fix(ci): restore init prefix normalization and formatting

intent(ci): clear the current PR failures instead of leaving the branch blocked behind known red checks

decision(init-prefix): normalize dotted prefixes before persisting issue_prefix so config, metadata.json, and Dolt database naming stay aligned for Julia-style repos
constraint(fmt-check): CI runs gofmt over the whole repo, so the existing spacing drift in internal/jira/tracker_test.go had to be cleaned up on this branch to get a green run
learned(ci): the latest main CI was already red on the same formatting file and the same embedded shard, so rebasing surfaced pre-existing blockers rather than introducing a new shard-only regression
2026-04-10 21:23:14 -07:00

187 lines
5.1 KiB
Go

//go:build cgo
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/git"
"github.com/steveyegge/beads/internal/recipes"
)
func resetSetupResolutionCaches(t *testing.T) {
t.Helper()
beads.ResetCaches()
git.ResetCaches()
t.Cleanup(func() {
beads.ResetCaches()
git.ResetCaches()
})
}
func resetSetupGlobals(t *testing.T) {
t.Helper()
originalProject := setupProject
originalGlobal := setupGlobal
originalCheck := setupCheck
originalRemove := setupRemove
originalStealth := setupStealth
originalPrint := setupPrint
originalOutput := setupOutput
originalList := setupList
originalAdd := setupAdd
setupProject = false
setupGlobal = false
setupCheck = false
setupRemove = false
setupStealth = false
setupPrint = false
setupOutput = ""
setupList = false
setupAdd = ""
t.Cleanup(func() {
setupProject = originalProject
setupGlobal = originalGlobal
setupCheck = originalCheck
setupRemove = originalRemove
setupStealth = originalStealth
setupPrint = originalPrint
setupOutput = originalOutput
setupList = originalList
setupAdd = originalAdd
})
}
func TestLoadSetupRecipes_NoWorkspaceUsesBuiltinsOnly(t *testing.T) {
tmpDir := t.TempDir()
orphanBeadsDir := filepath.Join(tmpDir, ".beads")
if err := recipes.SaveUserRecipe(orphanBeadsDir, "myeditor", ".myeditor/rules.md"); err != nil {
t.Fatalf("SaveUserRecipe: %v", err)
}
t.Chdir(tmpDir)
t.Setenv("BEADS_DIR", "")
resetSetupResolutionCaches(t)
allRecipes, usingWorkspaceRecipes, err := loadSetupRecipes()
if err != nil {
t.Fatalf("loadSetupRecipes: %v", err)
}
if usingWorkspaceRecipes {
t.Fatal("expected built-in-only recipe set without an active workspace")
}
if _, ok := allRecipes["cursor"]; !ok {
t.Fatal("expected built-in cursor recipe to be available")
}
if _, ok := allRecipes["myeditor"]; ok {
t.Fatal("unexpected orphan custom recipe outside active workspace")
}
}
func TestListRecipes_NoWorkspaceShowsBuiltinOnlyNote(t *testing.T) {
tmpDir := t.TempDir()
orphanBeadsDir := filepath.Join(tmpDir, ".beads")
if err := recipes.SaveUserRecipe(orphanBeadsDir, "myeditor", ".myeditor/rules.md"); err != nil {
t.Fatalf("SaveUserRecipe: %v", err)
}
t.Chdir(tmpDir)
t.Setenv("BEADS_DIR", "")
resetSetupResolutionCaches(t)
out := captureStdout(t, func() error {
listRecipes()
return nil
})
if !strings.Contains(out, "cursor") {
t.Fatalf("expected built-in recipes in output, got:\n%s", out)
}
if strings.Contains(out, "myeditor") {
t.Fatalf("unexpected orphan custom recipe in output:\n%s", out)
}
if !strings.Contains(out, "Note: No active beads workspace found. Showing built-in recipes only.") {
t.Fatalf("expected built-in-only note in output, got:\n%s", out)
}
if !strings.Contains(out, "Hint: "+diagHint()) {
t.Fatalf("expected diagnostic hint in output, got:\n%s", out)
}
}
func TestAddRecipe_NoWorkspaceReturnsActiveWorkspaceError(t *testing.T) {
tmpDir := t.TempDir()
t.Chdir(tmpDir)
t.Setenv("BEADS_DIR", "")
resetSetupResolutionCaches(t)
err := addRecipe("myeditor", ".myeditor/rules.md")
if err == nil {
t.Fatal("expected addRecipe to fail without active workspace")
}
if !strings.Contains(err.Error(), activeWorkspaceNotFoundError()) {
t.Fatalf("expected active-workspace error, got: %v", err)
}
if !strings.Contains(err.Error(), diagHint()) {
t.Fatalf("expected diagnostic hint, got: %v", err)
}
if _, statErr := os.Stat(filepath.Join(tmpDir, ".beads", "recipes.toml")); !os.IsNotExist(statErr) {
t.Fatalf("expected no local recipes.toml, got err=%v", statErr)
}
}
func TestLookupSetupRecipe_NoWorkspaceIgnoresOrphanCustomRecipes(t *testing.T) {
tmpDir := t.TempDir()
orphanBeadsDir := filepath.Join(tmpDir, ".beads")
if err := recipes.SaveUserRecipe(orphanBeadsDir, "myeditor", ".myeditor/rules.md"); err != nil {
t.Fatalf("SaveUserRecipe: %v", err)
}
t.Chdir(tmpDir)
t.Setenv("BEADS_DIR", "")
resetSetupResolutionCaches(t)
_, err := lookupSetupRecipe("myeditor")
if err == nil {
t.Fatal("expected custom recipe lookup to fail without active workspace")
}
if !strings.Contains(err.Error(), "workspace-local custom recipes require an active beads workspace") {
t.Fatalf("expected explicit no-workspace message, got: %v", err)
}
}
func TestRunRecipe_BuiltinFileRecipeWorksWithoutWorkspace(t *testing.T) {
tmpDir := t.TempDir()
t.Chdir(tmpDir)
t.Setenv("BEADS_DIR", "")
resetSetupResolutionCaches(t)
resetSetupGlobals(t)
out := captureStdout(t, func() error {
runRecipe("windsurf")
return nil
})
if !strings.Contains(out, "Installing Windsurf integration") {
t.Fatalf("expected install output, got:\n%s", out)
}
installedPath := filepath.Join(tmpDir, ".windsurf", "rules", "beads.md")
data, err := os.ReadFile(installedPath)
if err != nil {
t.Fatalf("ReadFile(%q): %v", installedPath, err)
}
if string(data) != recipes.Template {
t.Fatalf("installed recipe contents did not match template")
}
if _, err := os.Stat(filepath.Join(tmpDir, ".beads")); !os.IsNotExist(err) {
t.Fatalf("expected no local .beads directory to be created, got err=%v", err)
}
}