beads/cmd/bd/dolt_remote_add_test.go
Steve Yegge e294c00742
fix(dolt): recover persisted remotes in the GH#2118 cold-start window instead of trusting an empty listing (wy-6k7f7) (#5077)
A freshly (auto-)started sql-server can report an EMPTY dolt_remotes
while the remote is persisted on disk in .dolt/repo_state.json
(GH#2118). wy-xtv17/wy-82hc5 taught the push/sync no-remote gate to
consult the on-disk probe, but three MUTATING siblings still decided
'no remote' from len(ListRemotes) alone (wy-6k7f7, found by the
wy-82hc5 adversarial pass):

- bd config apply blind-added the federation remote over the invisible
  one. currentOriginRemoteURL now resolves origin from evidence — SQL
  listing first, persisted enumeration as the cold-start fallback, a
  failed listing stays an error — routing apply into the same
  consistent/update legs it takes after the window.
- bd dolt remote add skipped the URL-disagreement confirmation (an
  empty listing made existingURL "") and silently clobbered the
  persisted remote. ensureDoltRemote now recovers the persisted URL, so
  the idempotent re-add stays a no-op and a real disagreement gets the
  same confirm flow as a visible remote; the confirmed overwrite
  tolerates the cold server refusing to remove a remote it cannot see.
- prepareCLIRouteForGitProtocol fell back to SQL DOLT_PUSH, which the
  cold server refuses with a bare 'remote origin not found'. A
  persisted git-protocol remote now recovers the CLI route outright
  (the CLI transport never needed the SQL listing — the push
  proceeds); a persisted non-git remote fails with the cold-start
  explanation instead of the bare error.

Recovery beats detection here because the evidence carries URLs, not
just existence: new DoltStore.PersistedRemoteInfos() exposes the
name+URL enumeration with HasPersistedRemote's exact directory-search
semantics (HasPersistedRemote is now derived from it), and the cmd
layer peels decorators to reach it via persistedRemoteInfosFor, the
recovery-grade sibling of persistedRemoteProberFor. Embedded rigs
return nil — GH#2118 is a sql-server phenomenon.

Read-only doctor/drift diagnostics still judge from the listing alone
(they misreport, they don't mutate) — noted in hasConfiguredRemote's
doc; the bead tracks them as lower priority.


Claude-Session: https://claude.ai/code/session_01EF8QbCbF6JnTuoCKvu9xiG

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 15:10:30 -07:00

209 lines
6.9 KiB
Go

package main
import (
"context"
"errors"
"reflect"
"strings"
"testing"
"github.com/steveyegge/beads/internal/storage"
)
type fakeDoltRemoteAddStore struct {
remotes []storage.RemoteInfo
calls []string
}
func (f *fakeDoltRemoteAddStore) ListRemotes(ctx context.Context) ([]storage.RemoteInfo, error) {
f.calls = append(f.calls, "list")
return append([]storage.RemoteInfo(nil), f.remotes...), nil
}
func (f *fakeDoltRemoteAddStore) AddRemote(ctx context.Context, name, url string) error {
f.calls = append(f.calls, "add "+name+" "+url)
f.remotes = append(f.remotes, storage.RemoteInfo{Name: name, URL: url})
return nil
}
func (f *fakeDoltRemoteAddStore) RemoveRemote(ctx context.Context, name string) error {
f.calls = append(f.calls, "remove "+name)
filtered := f.remotes[:0]
for _, remote := range f.remotes {
if remote.Name != name {
filtered = append(filtered, remote)
}
}
f.remotes = filtered
return nil
}
func TestEnsureDoltRemoteSameNormalizedURLIsNoop(t *testing.T) {
store := &fakeDoltRemoteAddStore{
remotes: []storage.RemoteInfo{
{Name: "origin", URL: "https://github.com/org/repo.git"},
},
}
prompted := false
result, err := ensureDoltRemote(context.Background(), store, "origin", "git+https://github.com/org/repo.git", func(surface, name, existingURL, newURL string) bool {
prompted = true
return true
})
if err != nil {
t.Fatalf("ensureDoltRemote: %v", err)
}
if result.Canceled {
t.Fatal("same normalized URL should not cancel")
}
if prompted {
t.Fatal("same normalized URL should not prompt for overwrite")
}
if want := []string{"list"}; !reflect.DeepEqual(store.calls, want) {
t.Fatalf("calls = %v, want %v", store.calls, want)
}
}
func TestEnsureDoltRemoteDifferentURLReplacesExisting(t *testing.T) {
store := &fakeDoltRemoteAddStore{
remotes: []storage.RemoteInfo{
{Name: "origin", URL: "git+https://github.com/org/old.git"},
},
}
confirmed := false
result, err := ensureDoltRemote(context.Background(), store, "origin", "git+https://github.com/org/new.git", func(surface, name, existingURL, newURL string) bool {
confirmed = true
if surface != "SQL server" || name != "origin" || existingURL != "git+https://github.com/org/old.git" || newURL != "git+https://github.com/org/new.git" {
t.Fatalf("confirm args = %q %q %q %q", surface, name, existingURL, newURL)
}
return true
})
if err != nil {
t.Fatalf("ensureDoltRemote: %v", err)
}
if result.Canceled {
t.Fatal("confirmed replacement should not cancel")
}
if !confirmed {
t.Fatal("different URL should prompt before replacement")
}
want := []string{
"list",
"remove origin",
"add origin git+https://github.com/org/new.git",
}
if !reflect.DeepEqual(store.calls, want) {
t.Fatalf("calls = %v, want %v", store.calls, want)
}
}
// fakeDoltRemoteAddStoreWithDisk simulates the GH#2118 cold-start window: the
// SQL listing (embedded fake) can be empty while remotes are persisted on
// disk. It implements persistedRemoteInfoLister directly, which
// persistedRemoteInfosFor honors before any decorator peeling.
type fakeDoltRemoteAddStoreWithDisk struct {
fakeDoltRemoteAddStore
persisted []storage.RemoteInfo
removeErr error
}
func (f *fakeDoltRemoteAddStoreWithDisk) PersistedRemoteInfos() []storage.RemoteInfo {
f.calls = append(f.calls, "persisted")
return append([]storage.RemoteInfo(nil), f.persisted...)
}
func (f *fakeDoltRemoteAddStoreWithDisk) RemoveRemote(ctx context.Context, name string) error {
if f.removeErr != nil {
f.calls = append(f.calls, "remove-fail "+name)
return f.removeErr
}
return f.fakeDoltRemoteAddStore.RemoveRemote(ctx, name)
}
// TestEnsureDoltRemoteColdStartSameURLIsNoop pins the wy-6k7f7 fence: an
// empty dolt_remotes listing with the same remote persisted on disk (a
// freshly started sql-server, GH#2118) must be treated as the existing
// remote it is — an idempotent re-add writes nothing and asks nothing.
func TestEnsureDoltRemoteColdStartSameURLIsNoop(t *testing.T) {
store := &fakeDoltRemoteAddStoreWithDisk{
persisted: []storage.RemoteInfo{
{Name: "origin", URL: "https://github.com/org/repo.git"},
},
}
prompted := false
result, err := ensureDoltRemote(context.Background(), store, "origin", "git+https://github.com/org/repo.git", func(surface, name, existingURL, newURL string) bool {
prompted = true
return true
})
if err != nil {
t.Fatalf("ensureDoltRemote: %v", err)
}
if result.Canceled || prompted {
t.Fatalf("cold-start same-URL re-add should be a silent no-op (canceled=%v prompted=%v)", result.Canceled, prompted)
}
if want := []string{"list", "persisted"}; !reflect.DeepEqual(store.calls, want) {
t.Fatalf("calls = %v, want %v (no write may reach the store)", store.calls, want)
}
}
// TestEnsureDoltRemoteColdStartDifferentURLPrompts pins that the invisible
// persisted remote gets the SAME overwrite confirmation the visible one
// would — before wy-6k7f7 an empty listing skipped the confirmation and the
// add silently clobbered the persisted remote. A declined confirmation
// writes nothing; a confirmed one proceeds even though the cold server
// cannot remove a remote it does not see yet.
func TestEnsureDoltRemoteColdStartDifferentURLPrompts(t *testing.T) {
t.Run("declined_writes_nothing", func(t *testing.T) {
store := &fakeDoltRemoteAddStoreWithDisk{
persisted: []storage.RemoteInfo{
{Name: "origin", URL: "git+https://github.com/org/old.git"},
},
}
confirmed := false
result, err := ensureDoltRemote(context.Background(), store, "origin", "git+https://github.com/org/new.git", func(surface, name, existingURL, newURL string) bool {
confirmed = true
if existingURL != "git+https://github.com/org/old.git" {
t.Fatalf("confirm existingURL = %q, want the persisted on-disk URL", existingURL)
}
return false
})
if err != nil {
t.Fatalf("ensureDoltRemote: %v", err)
}
if !confirmed {
t.Fatal("cold-start URL disagreement should prompt for overwrite")
}
if !result.Canceled {
t.Fatal("declined overwrite should cancel")
}
for _, call := range store.calls {
if strings.HasPrefix(call, "add") || strings.HasPrefix(call, "remove") {
t.Fatalf("declined overwrite wrote to the store: %v", store.calls)
}
}
})
t.Run("confirmed_tolerates_unremovable_invisible_remote", func(t *testing.T) {
store := &fakeDoltRemoteAddStoreWithDisk{
persisted: []storage.RemoteInfo{
{Name: "origin", URL: "git+https://github.com/org/old.git"},
},
removeErr: errors.New("unknown remote: origin"),
}
result, err := ensureDoltRemote(context.Background(), store, "origin", "git+https://github.com/org/new.git", func(surface, name, existingURL, newURL string) bool {
return true
})
if err != nil {
t.Fatalf("ensureDoltRemote: %v", err)
}
if result.Canceled {
t.Fatal("confirmed overwrite should not cancel")
}
want := []string{"list", "persisted", "remove-fail origin", "add origin git+https://github.com/org/new.git"}
if !reflect.DeepEqual(store.calls, want) {
t.Fatalf("calls = %v, want %v", store.calls, want)
}
})
}