beads/cmd/bd/global_identity_integration_test.go
matt wilkie 3d0031aadf
fix(create): validate global IDs against global prefix (#4957)
* fix(create): validate global IDs against global prefix

Select the opened global store's issue prefix for explicit global IDs while retaining YAML-first validation for ordinary project creates. Add precedence and shared-server integration regressions.

Agent-Signature: codex-gpt-5.6-sol-high on behalf of matt wilkie

* test(create): make global-prefix regression test actually exercise #4646

Write issue-prefix=proj0 into config.yaml explicitly after `bd init`,
since non-no-db init stores the prefix in the DB, not config.yaml (the
generated file only carries a commented `# issue-prefix: ""`). Without
this, the config.yaml assertion always failed when a Dolt container is
available, and the create/show regression steps passed on both patched
and unpatched code because config.GetString("issue-prefix") was empty
in the --global path either way. With config.yaml populated, unpatched
selectCreateIDPrefix would incorrectly prefer the project's YAML prefix
over the global store's prefix even in --global mode, and ID validation
would reject the global ID - so the test now actually guards #4646.

Also replace the hardcoded "global-explicit" prefix literal with
doltserver.GlobalIssuePrefix + "-explicit" to stay coupled to the
constant.

Agent-Signature: claude-fable-5-high on behalf of matt wilkie
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-22 21:48:49 -07:00

149 lines
4.7 KiB
Go

//go:build cgo
package main
import (
"context"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/doltserver"
"github.com/steveyegge/beads/internal/testutil"
)
func TestGlobalDBIdentityCheck(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("not supported on Windows")
}
bdBinary := buildSharedServerTestBinary(t)
cp, err := testutil.NewContainerProvider()
if err != nil {
t.Skipf("skipping: Dolt container not available: %v", err)
}
t.Cleanup(func() { _ = cp.Stop() })
containerPort := cp.Port()
sharedDir := t.TempDir()
if err := cp.WritePortFile(sharedDir); err != nil {
t.Fatalf("write port file: %v", err)
}
projectDir := filepath.Join(t.TempDir(), "proj0")
if err := os.MkdirAll(projectDir, 0o755); err != nil {
t.Fatalf("mkdir project dir: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
if err := gitInit(ctx, projectDir); err != nil {
t.Fatalf("git init: %v", err)
}
env := []string{
"PATH=" + os.Getenv("PATH"),
"HOME=" + t.TempDir(),
"GOPATH=" + os.Getenv("GOPATH"),
"GOROOT=" + os.Getenv("GOROOT"),
"BEADS_SHARED_SERVER_DIR=" + sharedDir,
"BEADS_DOLT_SHARED_SERVER=1",
"BEADS_DOLT_SERVER_PORT=" + strconv.Itoa(containerPort),
"BEADS_DOLT_AUTO_START=0",
"BEADS_TEST_MODE=1",
"BD_DISABLE_METRICS=1",
"BD_DISABLE_EVENT_FLUSH=1",
"GIT_TERMINAL_PROMPT=0",
"GIT_ASKPASS=",
"SSH_ASKPASS=",
"GT_ROOT=",
}
initArgs := []string{
"init",
"--shared-server",
"--global",
"--external",
"--prefix", "proj0",
"--quiet",
"--non-interactive",
}
if out, err := ssExec(ctx, bdBinary, projectDir, env, initArgs...); err != nil {
t.Fatalf("bd %s failed: %v\noutput:\n%s",
strings.Join(initArgs, " "), err, out)
}
beadsDir := filepath.Join(projectDir, ".beads")
cfg, err := configfile.Load(beadsDir)
if err != nil {
t.Fatalf("load metadata.json from %s: %v", beadsDir, err)
}
if cfg == nil {
t.Fatalf("metadata.json missing in %s", beadsDir)
}
if cfg.GlobalDoltDatabase != doltserver.GlobalDatabaseName {
t.Errorf("metadata.json global_dolt_database = %q, want %q",
cfg.GlobalDoltDatabase, doltserver.GlobalDatabaseName)
}
if cfg.ProjectID == "" {
t.Error("metadata.json project_id is empty; expected a generated UUID")
}
if cfg.ProjectID == doltserver.GlobalProjectID {
t.Errorf("metadata.json project_id = %q (the global sentinel); "+
"a per-project UUID was expected", cfg.ProjectID)
}
if cfg.GlobalProjectID != doltserver.GlobalProjectID {
t.Errorf("metadata.json global_project_id = %q, want %q",
cfg.GlobalProjectID, doltserver.GlobalProjectID)
}
// bd init (non-no-db) stores the prefix in the DB, not config.yaml
// (the generated file only carries a commented `# issue-prefix: ""`).
// Write the project prefix into config.yaml explicitly so that the
// create/show steps below exercise the YAML-first prefix-selection
// path exactly as bd-4646 regressed it: unpatched selectCreateIDPrefix
// would then incorrectly prefer this "proj0" YAML prefix over the
// global store's prefix even in --global mode, and the ID validation
// below would fail. See cmd/bd/init_embedded_test.go for the same
// pattern.
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("issue-prefix: proj0\n"), 0o644); err != nil {
t.Fatalf("write config.yaml issue-prefix: %v", err)
}
if yamlPrefix := config.GetStringFromDir(beadsDir, "issue-prefix"); yamlPrefix != "proj0" {
t.Fatalf("project YAML issue-prefix = %q, want %q", yamlPrefix, "proj0")
}
explicitGlobalID := doltserver.GlobalIssuePrefix + "-explicit"
out, err := ssExec(ctx, bdBinary, projectDir, env,
"create", "Global explicit ID", "--global", "--id", explicitGlobalID, "--silent")
if err != nil {
t.Fatalf("bd create --global --id %s failed: %v\noutput:\n%s", explicitGlobalID, err, out)
}
if got := strings.TrimSpace(out); got != explicitGlobalID {
t.Fatalf("bd create --global --id returned %q, want %q", got, explicitGlobalID)
}
out, err = ssExec(ctx, bdBinary, projectDir, env, "show", "--global", explicitGlobalID)
if err != nil {
t.Fatalf("bd show --global %s failed: %v\noutput:\n%s", explicitGlobalID, err, out)
}
if !strings.Contains(out, explicitGlobalID) {
t.Fatalf("bd show --global did not return %s from %s:\n%s",
explicitGlobalID, doltserver.GlobalDatabaseName, out)
}
out, err = ssExec(ctx, bdBinary, projectDir, env, "list", "--global")
if err != nil {
t.Fatalf("bd list --global failed: %v\noutput:\n%s", err, out)
}
if strings.Contains(out, "PROJECT IDENTITY MISMATCH") {
t.Fatalf("bd list --global returned identity mismatch error against the global DB:\n%s", out)
}
}