beads/cmd/bd/store_reopen_test.go
matt wilkie 64678bcb34
fix: show diverged-history guidance on auto-push failure (#3138)
* fix: show diverged-history guidance on auto-push failure

When dolt auto-push fails with a 'no common ancestor' error, the user
only sees a cryptic warning. The explicit 'bd dolt push' and 'bd dolt
pull' commands already detect this and print recovery guidance via
printDivergedHistoryGuidance(), but the auto-push path did not.

Now auto-push also calls isDivergedHistoryErr() and prints the same
actionable recovery options (bootstrap, force-push, or re-init).

Amp-Thread-ID: https://ampcode.com/threads/T-019d6ee7-e46d-74f8-8b6e-2a0fc1b2d4d7
Co-authored-by: Amp <amp@ampcode.com>

* fix: store secret config keys in config.yaml, not Dolt DB

Keys like github.token and linear.api_key were stored in the Dolt
database's config table. When auto-push sends that data to a GitHub
remote, GitHub's push protection blocks the push because it detects
embedded secrets in the Dolt data files.

Fix: add github.token and linear.api_key to YamlOnlyKeys so they're
written to .beads/config.yaml (local-only, gitignored) instead of
the Dolt database. Update the read paths in getGitHubConfigValue()
and getLinearConfig() to check YAML config first for these keys.

Amp-Thread-ID: https://ampcode.com/threads/T-019d6ee7-e46d-74f8-8b6e-2a0fc1b2d4d7
Co-authored-by: Amp <amp@ampcode.com>

---------

Co-authored-by: Amp <amp@ampcode.com>
2026-04-08 15:55:37 -07:00

173 lines
5.1 KiB
Go

//go:build cgo
package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
)
func TestWithStorage_ReopensUsingMetadata(t *testing.T) {
ctx := context.Background()
testDBPath := filepath.Join(t.TempDir(), "dolt")
newTestStoreIsolatedDB(t, testDBPath, "cfg")
var gotPrefix string
err := withStorage(ctx, nil, testDBPath, func(s storage.DoltStorage) error {
var err error
gotPrefix, err = s.GetConfig(ctx, "issue_prefix")
return err
})
if err != nil {
t.Fatalf("withStorage() error = %v", err)
}
if gotPrefix != "cfg" {
t.Fatalf("issue_prefix = %q, want %q", gotPrefix, "cfg")
}
}
func TestResolveBeadsDirForDBPath_UsesRawBeadsDirForSymlinkedDBPath(t *testing.T) {
repoDir := t.TempDir()
beadsDir := filepath.Join(repoDir, ".beads")
actualDBPath := filepath.Join(repoDir, "external-dolt")
linkDBPath := filepath.Join(beadsDir, "dolt")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatalf("mkdir beads dir: %v", err)
}
if err := os.MkdirAll(actualDBPath, 0o755); err != nil {
t.Fatalf("mkdir external dolt dir: %v", err)
}
if err := os.Symlink(actualDBPath, linkDBPath); err != nil {
t.Fatalf("symlink db path: %v", err)
}
cfg := &configfile.Config{
Database: "dolt",
Backend: configfile.BackendDolt,
}
if err := cfg.Save(beadsDir); err != nil {
t.Fatalf("save metadata: %v", err)
}
if got := resolveBeadsDirForDBPath(linkDBPath); !utils.PathsEqual(got, beadsDir) {
t.Fatalf("resolveBeadsDirForDBPath(%q) = %q, want %q", linkDBPath, got, beadsDir)
}
}
func TestIssueIDCompletion_UsesMetadataWhenStoreNil(t *testing.T) {
originalStore := store
originalDBPath := dbPath
originalRootCtx := rootCtx
defer func() {
store = originalStore
dbPath = originalDBPath
rootCtx = originalRootCtx
}()
ctx := context.Background()
rootCtx = ctx
testDBPath := filepath.Join(t.TempDir(), "dolt")
testStore := newTestStoreIsolatedDB(t, testDBPath, "cfg")
if err := testStore.CreateIssue(ctx, &types.Issue{
ID: "cfg-abc1",
Title: "Completion target",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
}, "test"); err != nil {
t.Fatalf("CreateIssue: %v", err)
}
store = nil
dbPath = testDBPath
completions, directive := issueIDCompletion(&cobra.Command{}, nil, "cfg-a")
if directive != cobra.ShellCompDirectiveNoFileComp {
t.Fatalf("directive = %d, want %d", directive, cobra.ShellCompDirectiveNoFileComp)
}
if len(completions) != 1 {
t.Fatalf("len(completions) = %d, want 1 (%v)", len(completions), completions)
}
if len(completions[0]) < len("cfg-abc1") || completions[0][:len("cfg-abc1")] != "cfg-abc1" {
t.Fatalf("completion = %q, want prefix %q", completions[0], "cfg-abc1")
}
}
func TestResolveCommandBeadsDir_NoCWDFallbackForExplicitPath(t *testing.T) {
// Set up project A with metadata so FindBeadsDir() discovers it from CWD.
projectA := t.TempDir()
beadsDirA := filepath.Join(projectA, ".beads")
if err := os.MkdirAll(filepath.Join(beadsDirA, "dolt"), 0o755); err != nil {
t.Fatalf("mkdir beads dir A: %v", err)
}
cfgA := &configfile.Config{
Database: "dolt",
Backend: configfile.BackendDolt,
DoltDatabase: "project_a_db",
}
if err := cfgA.Save(beadsDirA); err != nil {
t.Fatalf("save metadata A: %v", err)
}
// Project B: .beads/dolt exists but metadata.json is missing.
// This triggers the bug: filepath.Dir(dbPath) gives the correct
// .beads dir but configfile.Load returns nil, so the old code falls
// through to FindBeadsDir() which discovers project A instead.
projectB := t.TempDir()
beadsDirB := filepath.Join(projectB, ".beads")
if err := os.MkdirAll(filepath.Join(beadsDirB, "dolt"), 0o755); err != nil {
t.Fatalf("mkdir beads dir B: %v", err)
}
// CWD is inside project A so FindBeadsDir() discovers A
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
if err := os.Chdir(projectA); err != nil {
t.Fatalf("chdir to project A: %v", err)
}
t.Cleanup(func() { os.Chdir(origDir) })
// Simulate --db pointing to project B's database path
dbPathB := filepath.Join(beadsDirB, "dolt")
got := resolveCommandBeadsDir(dbPathB)
// Must resolve to project B's .beads, NOT project A's.
// The old code falls back to FindBeadsDir() and returns beadsDirA.
if !utils.PathsEqual(got, beadsDirB) {
t.Fatalf("resolveCommandBeadsDir(%q) = %q, want %q", dbPathB, got, beadsDirB)
}
}
func TestGetGitHubConfigValue_UsesMetadataWhenStoreNil(t *testing.T) {
// github.token is now a YAML-only key (not stored in Dolt DB) to avoid
// leaking secrets when pushing to remotes. Test that the env-var fallback
// still works when the store is nil.
originalStore := store
originalDBPath := dbPath
defer func() {
store = originalStore
dbPath = originalDBPath
}()
ctx := context.Background()
store = nil
dbPath = ""
t.Setenv("GITHUB_TOKEN", "ghp_test_token")
if got := getGitHubConfigValue(ctx, "github.token"); got != "ghp_test_token" {
t.Fatalf("getGitHubConfigValue() = %q, want %q", got, "ghp_test_token")
}
}