beads/cmd/bd/bootstrap_embedded_test.go
gascity-eddie[bot] 4a67d0d028
fix(bootstrap): cut the host part before stripping userinfo (mc-gcmer) (#7616)
* fix(bootstrap): cut the host part before stripping userinfo (mc-gcmer)

urlHostname stripped userinfo through the first "@" anywhere in the URL,
before cutting out the host part, so an "@" in a path, query or fragment
picked the host: https://my-dolt.example.com/x@github.com/path read as
github.com. isGitCodeRepoURL then called it a forge, and doltRemoteURL
rewrote what is really a Dolt remotesapi URL to git+https:// (the GH#3339
class). The SCP branch made the same split, so both
my-dolt.example.com:org/x@github.com:db and the local path
./backups/me@github.com:db read as github.com too.

Cut the host part first, then strip userinfo through its last "@":

- scheme URLs: the authority, ending at the first "/", "?" or "#". That is
  the host net/url dials, and Dolt's earl.Parse hands scheme URLs to
  net/url. Taking the last "@" matches net/url too, so
  https://a@b@github.com/org/db is now a forge, as Dolt would dial it,
  instead of going raw to the remotesapi client (the #4421 class).
- SCP: everything before the first ":", unless a "/" comes first (a local
  path) - the split doltremote.FromGitURL makes.

TestIsGitCodeRepoURL gains the path/query/fragment-@, SCP path-@,
local-path-@ and last-@ cases, 7 of them red before this change, plus
userinfo controls in both URL forms that stay green.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(bootstrap): scrub credentials from the Bootstrap failed message (mc-gcmer)

An unencoded "#" or "?" in a sync.remote password ends the URL's
authority early, so urlHostname no longer reads the remote as a forge and
bootstrap clones it as configured. The clone error then carries the URL,
password included, twice: sanitizeURL passes through a URL net/url cannot
parse, and Dolt's "invalid remote url:" repeats it. Scrub the error with
scrubURLCredentials where "Bootstrap failed:" prints it.
TestEmbeddedBootstrapCloneFailureScrubsCredentials pins this; it fails
against the pre-fix binary for both "pa#ss" and "pa?ss", and the new test
is registered in the embedded shard manifest.

Also add a forgeSyncRemoteForms row for multi-@ userinfo without a .git
suffix, so the routing pin covers a URL only its host can make a forge.
The row fails with the base urlHostname.

Review: PR #7616 synthesis F1 (minor) and F5 (nit).

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 18:07:50 +00:00

343 lines
12 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
)
// bdBootstrap runs "bd bootstrap" with the given args and returns stdout.
func bdBootstrap(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"bootstrap"}, 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 bootstrap %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
func bdBootstrapAllowError(t *testing.T, bd, dir string, args ...string) (string, error) {
t.Helper()
fullArgs := append([]string{"bootstrap"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
return string(out), err
}
func TestBootstrapNoWorkspace(t *testing.T) {
t.Parallel()
bd := buildEmbeddedBD(t)
dir := t.TempDir()
initGitRepoAt(t, dir)
t.Run("default_output", func(t *testing.T) {
out, err := bdBootstrapAllowError(t, bd, dir)
if err == nil {
t.Fatal("expected bd bootstrap to exit non-zero without a workspace")
}
if !strings.Contains(out, activeWorkspaceNotFoundMessage()) {
t.Fatalf("expected no-workspace message, got: %s", out)
}
if !strings.Contains(out, "bd where") {
t.Fatalf("expected bootstrap hint to mention bd where, got: %s", out)
}
})
t.Run("json_output", func(t *testing.T) {
cmd := exec.Command(bd, "bootstrap", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err == nil {
t.Fatal("expected bd bootstrap --json to exit non-zero without a workspace")
}
s := strings.TrimSpace(stdout.String())
start := strings.Index(s, "{")
if start < 0 {
t.Fatalf("expected JSON object in output:\nstdout: %s\nstderr: %s", s, stderr.String())
}
var payload map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &payload); err != nil {
t.Fatalf("parse bootstrap JSON: %v\nstdout: %s", err, s)
}
if action, _ := payload["action"].(string); action != "none" {
t.Fatalf("action = %q, want %q", action, "none")
}
if reason, _ := payload["reason"].(string); reason != activeWorkspaceNotFoundError() {
t.Fatalf("reason = %q, want %q", reason, activeWorkspaceNotFoundError())
}
if suggestion, _ := payload["suggestion"].(string); !strings.Contains(suggestion, "bd where") {
t.Fatalf("suggestion should mention bd where, got: %q", suggestion)
}
})
}
func TestEmbeddedBootstrapExistingDryRunInit(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// ===== Already Exists =====
t.Run("bootstrap_existing_db", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "tb")
out := bdBootstrap(t, bd, dir, "--dry-run")
if !strings.Contains(out, "already exists") && !strings.Contains(out, "Nothing to do") {
t.Errorf("expected 'already exists' for initialized db: %s", out)
}
})
// ===== Dry Run (fresh .beads with no db) =====
t.Run("bootstrap_dry_run_fresh", func(t *testing.T) {
dir := t.TempDir()
cmd := gitCommand("init", "-q")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = gitCommand("config", "user.name", "Test")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = gitCommand("config", "user.email", "test@test.com")
cmd.Dir = dir
cmd.CombinedOutput()
// Create .beads with metadata.json so FindBeadsDir detects it
beadsDir := filepath.Join(dir, ".beads")
os.MkdirAll(beadsDir, 0o750)
os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644)
out := bdBootstrap(t, bd, dir, "--dry-run")
if !strings.Contains(out, "create fresh") && !strings.Contains(out, "init") {
t.Errorf("expected fresh init plan: %s", out)
}
})
// ===== Full Bootstrap (init action) =====
t.Run("bootstrap_init", func(t *testing.T) {
dir := t.TempDir()
cmd := gitCommand("init", "-q")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = gitCommand("config", "user.name", "Test")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = gitCommand("config", "user.email", "test@test.com")
cmd.Dir = dir
cmd.CombinedOutput()
beadsDir := filepath.Join(dir, ".beads")
os.MkdirAll(beadsDir, 0o750)
os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644)
bcmd := exec.Command(bd, "bootstrap")
bcmd.Dir = dir
bcmd.Env = bdEnv(dir)
bcmd.Stdin = strings.NewReader("y\n")
stdout, stderr, err := runCommandBuffers(t, bcmd)
if err != nil {
t.Fatalf("bootstrap init failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
combined := stdout.String() + stderr.String()
if !strings.Contains(combined, "Created fresh database") {
t.Errorf("expected 'Created fresh database':\nstdout:\n%s\nstderr:\n%s", stdout.String(), stderr.String())
}
})
}
func TestEmbeddedBootstrapJSONLAndGitOrigin(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// ===== JSONL Import =====
t.Run("bootstrap_jsonl_import", func(t *testing.T) {
// First create a db and export
srcDir, _, _ := bdInit(t, bd, "--prefix", "bs")
bdCreate(t, bd, srcDir, "Export for bootstrap", "--type", "task")
cmd := exec.Command(bd, "export", "-o", filepath.Join(srcDir, ".beads", "issues.jsonl"))
cmd.Dir = srcDir
cmd.Env = bdEnv(srcDir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("export failed: %v\n%s", err, out)
}
// Create new dir with .beads + issues.jsonl but no database
dir := t.TempDir()
gitCmd := gitCommand("init", "-q")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
gitCmd = gitCommand("config", "user.name", "Test")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
gitCmd = gitCommand("config", "user.email", "test@test.com")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
destBeads := filepath.Join(dir, ".beads")
os.MkdirAll(destBeads, 0o750)
os.WriteFile(filepath.Join(destBeads, "metadata.json"), []byte("{}"), 0o644)
// Copy the JSONL file
data, _ := os.ReadFile(filepath.Join(srcDir, ".beads", "issues.jsonl"))
os.WriteFile(filepath.Join(dir, ".beads", "issues.jsonl"), data, 0o644)
// Bootstrap should detect and import JSONL
bcmd := exec.Command(bd, "bootstrap")
bcmd.Dir = dir
bcmd.Env = bdEnv(dir)
bcmd.Stdin = strings.NewReader("y\n")
stdout, stderr, err := runCommandBuffers(t, bcmd)
if err != nil {
t.Fatalf("bootstrap jsonl-import failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
combined := stdout.String() + stderr.String()
if !strings.Contains(combined, "Imported") {
t.Errorf("expected 'Imported' in output:\nstdout:\n%s\nstderr:\n%s", stdout.String(), stderr.String())
}
})
t.Run("bootstrap_from_git_origin_wires_remote", func(t *testing.T) {
bareDir := filepath.Join(t.TempDir(), "origin.git")
runGitForBootstrapTest(t, "", "init", "--bare", "--initial-branch=main", bareDir)
remoteURL := "file://" + bareDir
sourceDir := t.TempDir()
initGitRepoAt(t, sourceDir)
runGitForBootstrapTest(t, sourceDir, "branch", "-M", "main")
runGitForBootstrapTest(t, sourceDir, "remote", "add", "origin", remoteURL)
runGitForBootstrapTest(t, sourceDir, "commit", "--allow-empty", "-m", "init")
runGitForBootstrapTest(t, sourceDir, "push", "-u", "origin", "main")
runBDInit(t, bd, sourceDir, "--prefix", "beads", "--skip-hooks", "--skip-agents")
bdCreate(t, bd, sourceDir, "Seed remote data", "--type", "task")
bdDolt(t, bd, sourceDir, "push")
cloneDir := t.TempDir()
runGitForBootstrapTest(t, cloneDir, "init", "-b", "main")
runGitForBootstrapTest(t, cloneDir, "remote", "add", "origin", remoteURL)
out := bdBootstrap(t, bd, cloneDir, "--yes")
if !strings.Contains(out, "clone from remote") {
t.Fatalf("expected bootstrap sync plan, got:\n%s", out)
}
remotes := bdDolt(t, bd, cloneDir, "remote", "list")
if !strings.Contains(remotes, "origin") || !strings.Contains(remotes, remoteURL) {
t.Fatalf("bootstrap should leave origin configured as a Dolt remote %q; remote list:\n%s", remoteURL, remotes)
}
configYAML, err := os.ReadFile(filepath.Join(cloneDir, ".beads", "config.yaml"))
if err != nil {
t.Fatalf("read config.yaml: %v", err)
}
if !strings.Contains(string(configYAML), remoteURL) {
t.Fatalf("bootstrap should persist sync.remote; config.yaml:\n%s", configYAML)
}
})
}
// TestEmbeddedBootstrapConcurrent exercises bootstrap --dry-run concurrently.
func TestEmbeddedBootstrapConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "bx")
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
cmd := exec.Command(bd, "bootstrap", "--dry-run")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("bootstrap --dry-run (worker %d): %v\n%s", worker, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}
// TestEmbeddedBootstrapCloneFailureScrubsCredentials pins the credential scrub
// at the "Bootstrap failed:" print site (mc-gcmer). An unencoded "#" or "?" in
// the password ends the URL's authority early, so the remote is not a forge
// and is cloned as configured. The clone error then carries the password
// twice: sanitizeURL passes a URL net/url cannot parse through unchanged, and
// Dolt's "invalid remote url:" repeats it.
func TestEmbeddedBootstrapCloneFailureScrubsCredentials(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
for _, tc := range []struct{ name, password string }{
{"hash", "pa#ss"},
{"question_mark", "pa?ss"},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
initGitRepoAt(t, dir)
beadsDir := filepath.Join(dir, ".beads")
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
t.Fatalf("mkdir .beads: %v", err)
}
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644); err != nil {
t.Fatalf("write metadata.json: %v", err)
}
seedSyncRemote(t, beadsDir, "https://alice:"+tc.password+"@github.com/org/db")
cmd := exec.Command(bd, "bootstrap", "--yes")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
// A failure before the clone would pass the leak check below
// without reaching the print site.
if err == nil || !strings.Contains(stderr.String(), "Bootstrap failed: clone from remote:") {
t.Fatalf("bootstrap should fail at the clone, got err=%v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if strings.Contains(stdout.String()+stderr.String(), tc.password) {
t.Errorf("bootstrap printed the sync.remote password %q:\nstdout:\n%s\nstderr:\n%s", tc.password, stdout.String(), stderr.String())
}
})
}
}