beads/cmd/bd/init_external_message_test.go
Julian Knutsen 3488dc4536
fix(init): bd init --server --external starts no server; a failed bd init --server can be retried (#7674)
* fix(init): admit a retried `bd init --server` over an empty .beads/dolt

A first `bd init --server` that cannot reach its server fails after
creating the local Dolt root, leaving an empty .beads/dolt, no
.local_version witness, and no config.yaml/metadata.json recording the
server choice. The legacy-upgrade guard runs before init writes that
choice, so it saw an embedded-looking workspace with a local Dolt root
and refused every retry as a "legacy Dolt workspace"; no bd command could
repair it.

guardLegacyUpgradeWorkspaceForInit(beadsDir, initSelectsServer) carries
init's already-resolved server intent into the guard. It is consulted
for exactly one decision: an empty, witness-less .beads/dolt is admitted,
the same admission a workspace that records server mode already gets
(#6935). It is deliberately not folded into the workspace's own server
mode, so a non-empty root with an unreadable witness and an embedded
repository beside a non-empty root keep their current outcomes.
guardLegacyUpgradeWorkspace is now the initSelectsServer=false wrapper,
and only init's main guard call passes the flag; the proxied-server
init path keeps the plain wrapper.

Ported from the versioned-beads fork (donnabox/codex/preview3-source-
catchup-46aa), commits bc8d72182a (red) and 5bdfbfe5e2 (green). The
fork's graph_mode refusal in the same function is graph-preview-only
and is not carried here.

Tests: TestLegacyUpgradeGuardForInitAdmitsEmptyDoltRoot and
TestLegacyUpgradeGuardForInitAdmissionStaysNarrow pin the admission and
its narrowness at the guard; TestInitServerFlagAdmitsEmptyDoltRootWithoutConfig
and TestInitServerFlagRetryAfterFailedFirstAttempt run the real binary
against the test Dolt server and fail on main with the legacy refusal.

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(init): `bd init --server --external` must not auto-start a server

`--external` declares the Dolt server managed outside bd ("skip server
startup", per its help), but init builds its dolt.Config by hand with
`AutoStart: initServerMode && BEADS_DOLT_AUTO_START != "0"`, so the flag
never reached the auto-start decision. A `bd init --server --external`
that named no port therefore ran dolt and tried to start a repo-local
sql-server. A named port was already declared external by rule 2c
(#5934), but its failure still advised `bd dolt start`, the one thing
--external says bd must not do, and each route (host:port, socket,
shared server, auto-start pinned off) worded the failure differently.

- AutoStart is now `initServerMode && !externalServer && ...`.
- externalServerUnreachableMessage rewrites only a failed dial
  (*net.OpError with Op "dial") under --external: it names the endpoint
  that was dialed and says bd did not start a server. A dial to port 0
  means no port was named, so that message asks for --server-port or
  BEADS_DOLT_SERVER_PORT and offers no default (GH#2098, GH#2372). Any
  other open failure keeps its own text.

Ported from the versioned-beads fork (donnabox/codex/preview3-source-
catchup-46aa), commits 234ca3d502 and a601a80e77 (red/green), 03a64a1aba
(socket helper rename), and a6ca0536c9 and 397fc4c69e (red/green: no-port
arm and wording). The fork's graph-preview dispatch at the top of init
and its --skip-hooks help text change are graph-only and not carried.
The TestInitExternalProvisionerShape doc comment is corrected: on main
every provisioner cell is a control (a named port is already external),
not a reproduction.

Tests: TestInitServerExternalDeadPortStartsNoServer,
TestInitServerExternalNoPortStartsNoServer and
TestInitExternalUnreachableMessage run the real binary against a dolt
shim that records every launch and fail on main;
TestInitSharedServerExternalDeadPortStillFails,
TestInitServerWithoutNamedPortStillStartsServer and
TestInitExternalProvisionerShape are controls; and
TestExternalServerUnreachableMessage pins which errors are rewritten.

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-19 03:11:14 +00:00

97 lines
3.2 KiB
Go

package main
import (
"errors"
"fmt"
"net"
"strings"
"testing"
)
// TestExternalServerUnreachableMessage pins which failures get the --external
// wording. Only a failed dial may: the message claims the server could not be
// reached, so any other open failure (an identity mismatch, a handshake that
// was reset) must keep its own text instead of being reported as "externally
// managed".
func TestExternalServerUnreachableMessage(t *testing.T) {
refused := errors.New("connect: connection refused")
dial := &net.OpError{
Op: "dial",
Net: "tcp",
Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 3307},
Err: refused,
}
tests := []struct {
name string
err error
wantEndpoint string // "" means the error is not rewritten
}{
{"dial error", dial, "127.0.0.1:3307"},
{
// The store wraps the dial error with %w and its own advice.
"dial error wrapped like the store does",
fmt.Errorf("Dolt server unreachable at 127.0.0.1:3307: %w\n\nThe Dolt server may not be running.", dial),
"127.0.0.1:3307",
},
{
"unix socket dial error",
&net.OpError{Op: "dial", Net: "unix", Addr: &net.UnixAddr{Name: "/run/dolt.sock", Net: "unix"}, Err: refused},
"/run/dolt.sock",
},
{
// A name that does not resolve has no address to report.
"dial error without an address",
&net.OpError{Op: "dial", Net: "tcp", Err: errors.New("no such host")},
"the configured endpoint",
},
{"read error after connecting", &net.OpError{Op: "read", Net: "tcp", Err: errors.New("connection reset by peer")}, ""},
{"not a network error", errors.New("project identity mismatch"), ""},
}
// Every rewritten message says the server is declared external and that bd
// did not start one, and never advises `bd dolt start`.
requireDeclaredExternal := func(t *testing.T, got string, wants ...string) {
t.Helper()
for _, want := range append(wants, "externally managed", "did not start") {
if !strings.Contains(got, want) {
t.Errorf("message does not contain %q:\n%s", want, got)
}
}
if strings.Contains(got, "bd dolt start") {
t.Errorf("message advises `bd dolt start`:\n%s", got)
}
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := externalServerUnreachableMessage(tt.err)
if tt.wantEndpoint == "" {
if got != "" {
t.Fatalf("message = %q, want none for an error that is not a failed dial", got)
}
return
}
requireDeclaredExternal(t, got, tt.wantEndpoint)
})
}
// A dial to port 0 is an init that was never told a port: the address is a
// placeholder, not the server's endpoint, so the message must not offer it
// as one. The first line quotes the dialled address, so it is cut out
// before looking.
t.Run("dial error with no port named", func(t *testing.T) {
noPort := &net.OpError{
Op: "dial",
Net: "tcp",
Addr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0},
Err: refused,
}
got := externalServerUnreachableMessage(noPort)
if got == "" {
t.Fatal("message = none, want the --external wording for a failed dial")
}
requireDeclaredExternal(t, got)
if strings.Contains(strings.ReplaceAll(got, noPort.Error(), ""), ":0") {
t.Errorf("message offers port 0 as the endpoint:\n%s", got)
}
})
}