beads/cmd/bd/conflicts_test.go
Steve Yegge 144876baab feat(conflicts): bd conflicts list|show|resolve — issue-oriented merge conflicts (wy-jpd3.5)
Federation ask #3. Resolving a halted merge meant dropping into the raw dolt
CLI inside .beads/dolt/<db> — dolt conflicts cat issues, dolt conflicts resolve
--ours|--theirs issues, dolt add -A && dolt commit — dolt arcana whose flag
surface differs from git's just enough to bite. This wraps it in bd, presented
per issue FIELD rather than as raw conflict rows.

  bd conflicts list                     tables + conflicted issue IDs
  bd conflicts show [<id>]              base/ours/theirs per diverged field
  bd conflicts resolve <id> --ours      one issue, leaving the rest conflicted
  bd conflicts resolve --all --theirs   whole tables (dolt's own resolution)

Storage: storage.ConflictInspector is a new optional interface (GetConflictRows
/ ResolveConflictRows) implemented by both Dolt-backed stores over
versioncontrolops/conflicts.go, which folds dolt_conflicts_<table>'s
base_/our_/their_ columns into one row per conflict — generically, from the
result-set column names, so it does not rot on the next migration.

Per-row resolution follows dolt's manual-resolution protocol, the one
resolveIssuesLWWConflicts already proves: --ours deletes the conflict row (the
working set already holds our values), --theirs writes their values first and
then deletes it. It is restricted to modify/modify rows — when one side deleted
the row, "ours"/"theirs" do not describe what the operator wants, so it is
refused by name instead of guessed at.

Safety rails, several from the adversarial review of this diff:
- The CLI reaches the store through storage.UnwrapStore: store is always
  decorated (telemetry + hooks), and a decorator embedding the DoltStorage
  interface promotes only that interface's methods, so asserting on the wrapper
  would silently report "No merge conflicts" over a wedged merge.
- The merge is committed only when this invocation actually resolved something
  AND no conflict remains, via CommitMergeResolution (plain Commit excludes
  config, GH#2455/GH#2474). A partial resolution leaves the merge open.
- Server-mode resolution pins a connection and resets
  @@dolt_allow_commit_conflicts/@@dolt_force_transaction_commit before
  returning it to the pool, discarding the connection if the reset fails.
- A --theirs UPDATE that matches no row (concurrent delete on a shared server)
  aborts instead of clearing the conflict and dropping their side.
- Interpolated identifiers — table AND column — are validated; values are bound.

Follow-ups filed as wy-36ilm: a dolt-backed integration test (every test here
is a pure unit test, which is why the review's blocker was invisible to the
suite), a first-class way to conclude an already-resolved merge, and
schema-conflict/constraint-violation visibility in the commit gate.
2026-07-24 19:47:16 -07:00

128 lines
4 KiB
Go

package main
import (
"testing"
"github.com/steveyegge/beads/internal/storage"
)
func strp(s string) *string { return &s }
func resetConflictResolveFlags() {
conflictsResolveOurs = false
conflictsResolveTheirs = false
conflictsResolveStrat = ""
}
func TestResolveStrategy(t *testing.T) {
t.Cleanup(resetConflictResolveFlags)
cases := []struct {
name string
ours bool
theirs bool
strat string
want string
wantErr bool
}{
{name: "ours flag", ours: true, want: "ours"},
{name: "theirs flag", theirs: true, want: "theirs"},
{name: "strategy flag", strat: "theirs", want: "theirs"},
{name: "agreeing flags", ours: true, strat: "ours", want: "ours"},
{name: "both sides", ours: true, theirs: true, wantErr: true},
{name: "contradiction", ours: true, strat: "theirs", wantErr: true},
{name: "unset", wantErr: true},
{name: "garbage strategy", strat: "mine", wantErr: true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
resetConflictResolveFlags()
conflictsResolveOurs = c.ours
conflictsResolveTheirs = c.theirs
conflictsResolveStrat = c.strat
got, err := resolveStrategy()
if c.wantErr {
if err == nil {
t.Fatalf("resolveStrategy() = %q, want an error", got)
}
return
}
if err != nil {
t.Fatalf("resolveStrategy() error = %v", err)
}
if got != c.want {
t.Errorf("resolveStrategy() = %q, want %q", got, c.want)
}
})
}
}
func TestDifferingFieldsDropsAgreement(t *testing.T) {
fields := []storage.ConflictFieldValue{
{Name: "id", Base: strp("bd-1"), Ours: strp("bd-1"), Theirs: strp("bd-1")},
{Name: "status", Base: strp("open"), Ours: strp("closed"), Theirs: strp("open")},
{Name: "assignee", Ours: strp("zelda"), Theirs: nil},
}
got := differingFields(fields)
if len(got) != 2 {
t.Fatalf("want status and assignee, got %d fields: %+v", len(got), got)
}
if got[0].Name != "status" || got[1].Name != "assignee" {
t.Errorf("differing fields = %q,%q", got[0].Name, got[1].Name)
}
}
func TestFilterShownFieldsHonorsAllFields(t *testing.T) {
rows := []storage.ConflictRow{{
Table: "issues",
Key: "bd-1",
Fields: []storage.ConflictFieldValue{
{Name: "id", Ours: strp("bd-1"), Theirs: strp("bd-1")},
{Name: "status", Ours: strp("open"), Theirs: strp("closed")},
},
}}
if got := filterShownFields(rows, true); len(got[0].Fields) != 2 {
t.Errorf("--all-fields must keep every column, got %d", len(got[0].Fields))
}
got := filterShownFields(rows, false)
if len(got[0].Fields) != 1 || got[0].Fields[0].Name != "status" {
t.Errorf("default view = %+v, want only the diverged field", got[0].Fields)
}
// The caller's slice must not be mutated: `bd conflicts show` renders the
// same rows again after filtering for JSON.
if len(rows[0].Fields) != 2 {
t.Error("filterShownFields mutated its input")
}
}
// The conflict class is what tells an operator what ours/theirs will DO, so
// each combination has to be named correctly.
func TestConflictKind(t *testing.T) {
cases := []struct {
row storage.ConflictRow
want string
}{
{storage.ConflictRow{BaseExists: true, OurExists: true, TheirExists: true}, "both sides modified"},
{storage.ConflictRow{OurExists: true, TheirExists: true}, "both sides added"},
{storage.ConflictRow{BaseExists: true, OurExists: true}, "we modified / they deleted"},
{storage.ConflictRow{BaseExists: true, TheirExists: true}, "we deleted / they modified"},
{storage.ConflictRow{BaseExists: true}, "both sides deleted"},
}
for _, c := range cases {
if got := conflictKind(c.row); got != c.want {
t.Errorf("conflictKind(%+v) = %q, want %q", c.row, got, c.want)
}
}
}
func TestConflictValueDistinguishesNullFromEmpty(t *testing.T) {
if got := conflictValue(nil); got != "(null)" {
t.Errorf("nil renders as %q, want (null)", got)
}
if got := conflictValue(strp("")); got != `""` {
t.Errorf("empty renders as %q, want \"\"", got)
}
if got := conflictValue(strp("a\nb")); got != `a\nb` {
t.Errorf("newlines must not break the field layout, got %q", got)
}
}