beads/internal/validation/issue_test.go
Jim Wordelman f1db5a9fa5
fix(close): refuse silent close on actor/assignee mismatch (#3734)
* fix(close): be-035 refuse silent close on actor/assignee mismatch

bd close now requires the actor to match the bead's assignee (or have
--force) before persisting. Without this, an actor whose claim was
overwritten by a concurrent re-claim could call bd close, see "✓ Closed"
on stdout, and leave the bead open in the store — silent data loss.

The storage UPDATE was already id-only with proper rowcount checking,
so this is purely a CLI-layer authority guard implemented as a new
validation.AssigneeMatches validator wired into validateIssueClosable.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore: release gate PASS for be-07y (be-035 close authority guard)

Cherry-pick of 7a925e304 onto a clean branch off origin/main. Verified
delta byte-identical to reviewed commit. All four be-035 acceptance
criteria covered; reviewer-recorded PASS verdict in bead notes; tests
pass on the touched surfaces (TestAssigneeMatches 6/6, TestEmbeddedClose
35/35 incl. four new authority-guard cases, TestEmbeddedCloseConcurrent).
Three full-suite flakes observed are pre-existing baseline (reproduce
on plain origin/main).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(count): close alice-assigned fixture with --force under be-035 guard

TestEmbeddedCount's setup created a fixture assigned to alice and closed it
as the test actor ("Test"). With this PR's be-035 close-authority guard, that
cross-actor close is now correctly refused (exit 1), failing the test setup at
count_embedded_test.go:84.

The fixture must remain assigned to alice for the --assignee alice count
assertion below (expects >=3), so close it with --force rather than dropping
the assignee. Fixes the Test (Embedded Dolt Cmd 5/20) shard.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs(validation): note identity-by-string semantics of AssigneeMatches

Addresses review question: two principals sharing one actor name both pass
the check, because the actor string is compared verbatim and bd has no
identity layer. Document this as intentional — the guard removes silent
cross-actor closes without adding new identity guarantees.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(close): update proxied-server call site for validateIssueClosable actor param

Maintainer branch-update for #3734: upstream added cmd/bd/close_proxied_server.go
after this PR's base; align its call with the PR's widened signature.

Agent-Signature: claude-fable-5-high on behalf of matt wilkie

---------

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-07-05 23:51:45 -07:00

489 lines
11 KiB
Go

package validation
import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
func TestExists(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
wantErr bool
}{
{
name: "nil issue returns error",
issue: nil,
wantErr: true,
},
{
name: "non-nil issue passes",
issue: &types.Issue{ID: "bd-test"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Exists()("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("Exists() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestNotTemplate(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
wantErr bool
}{
{
name: "nil issue passes (delegated check)",
issue: nil,
wantErr: false,
},
{
name: "non-template passes",
issue: &types.Issue{ID: "bd-test", IsTemplate: false},
wantErr: false,
},
{
name: "template returns error",
issue: &types.Issue{ID: "bd-test", IsTemplate: true},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NotTemplate()("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("NotTemplate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestNotPinned(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
force bool
wantErr bool
}{
{
name: "nil issue passes",
issue: nil,
force: false,
wantErr: false,
},
{
name: "open issue passes",
issue: &types.Issue{ID: "bd-test", Status: types.StatusOpen},
force: false,
wantErr: false,
},
{
name: "pinned issue without force fails",
issue: &types.Issue{ID: "bd-test", Status: types.StatusPinned},
force: false,
wantErr: true,
},
{
name: "pinned issue with force passes",
issue: &types.Issue{ID: "bd-test", Status: types.StatusPinned},
force: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NotPinned(tt.force)("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("NotPinned() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestAssigneeMatches(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
actor string
force bool
wantErr bool
wantErrFrag string
}{
{
name: "nil issue passes (delegated check)",
issue: nil,
actor: "alice",
wantErr: false,
},
{
name: "unassigned issue passes for any actor",
issue: &types.Issue{ID: "bd-test", Assignee: ""},
actor: "alice",
wantErr: false,
},
{
name: "matching assignee passes",
issue: &types.Issue{ID: "bd-test", Assignee: "alice"},
actor: "alice",
wantErr: false,
},
{
name: "mismatched assignee without force fails",
issue: &types.Issue{ID: "bd-test", Assignee: "bob"},
actor: "alice",
force: false,
wantErr: true,
wantErrFrag: "assignee is",
},
{
name: "mismatched assignee with force passes",
issue: &types.Issue{ID: "bd-test", Assignee: "bob"},
actor: "alice",
force: true,
wantErr: false,
},
{
name: "empty actor with assigned bead fails",
issue: &types.Issue{ID: "bd-test", Assignee: "bob"},
actor: "",
wantErr: true,
wantErrFrag: "assignee is",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := AssigneeMatches(tt.actor, tt.force)("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("AssigneeMatches() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErrFrag != "" && err != nil {
if !strings.Contains(err.Error(), tt.wantErrFrag) {
t.Errorf("AssigneeMatches() error %q should contain %q", err.Error(), tt.wantErrFrag)
}
}
})
}
}
func TestNotClosed(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
wantErr bool
}{
{
name: "nil issue passes",
issue: nil,
wantErr: false,
},
{
name: "open issue passes",
issue: &types.Issue{ID: "bd-test", Status: types.StatusOpen},
wantErr: false,
},
{
name: "closed issue fails",
issue: &types.Issue{ID: "bd-test", Status: types.StatusClosed},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NotClosed()("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("NotClosed() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestChain(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
validators []IssueValidator
wantErr bool
}{
{
name: "empty chain passes",
issue: &types.Issue{ID: "bd-test"},
validators: []IssueValidator{},
wantErr: false,
},
{
name: "all validators pass",
issue: &types.Issue{ID: "bd-test", Status: types.StatusOpen},
validators: []IssueValidator{
Exists(),
NotTemplate(),
NotPinned(false),
},
wantErr: false,
},
{
name: "first validator fails stops chain",
issue: nil,
validators: []IssueValidator{
Exists(),
NotTemplate(),
},
wantErr: true,
},
{
name: "middle validator fails",
issue: &types.Issue{ID: "bd-test", IsTemplate: true},
validators: []IssueValidator{
Exists(),
NotTemplate(),
NotPinned(false),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
chain := Chain(tt.validators...)
err := chain("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("Chain() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestHasStatus(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
allowed []types.Status
wantErr bool
}{
{
name: "nil issue passes",
issue: nil,
allowed: []types.Status{types.StatusOpen},
wantErr: false,
},
{
name: "matching status passes",
issue: &types.Issue{ID: "bd-test", Status: types.StatusOpen},
allowed: []types.Status{types.StatusOpen},
wantErr: false,
},
{
name: "one of multiple allowed passes",
issue: &types.Issue{ID: "bd-test", Status: types.StatusClosed},
allowed: []types.Status{types.StatusOpen, types.StatusClosed},
wantErr: false,
},
{
name: "non-matching status fails",
issue: &types.Issue{ID: "bd-test", Status: types.StatusPinned},
allowed: []types.Status{types.StatusOpen, types.StatusClosed},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := HasStatus(tt.allowed...)("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("HasStatus() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestHasType(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
allowed []types.IssueType
wantErr bool
}{
{
name: "nil issue passes",
issue: nil,
allowed: []types.IssueType{types.TypeTask},
wantErr: false,
},
{
name: "matching type passes",
issue: &types.Issue{ID: "bd-test", IssueType: types.TypeTask},
allowed: []types.IssueType{types.TypeTask},
wantErr: false,
},
{
name: "one of multiple allowed passes",
issue: &types.Issue{ID: "bd-test", IssueType: types.TypeBug},
allowed: []types.IssueType{types.TypeTask, types.TypeBug},
wantErr: false,
},
{
name: "non-matching type fails",
issue: &types.Issue{ID: "bd-test", IssueType: types.TypeEpic},
allowed: []types.IssueType{types.TypeTask, types.TypeBug},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := HasType(tt.allowed...)("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("HasType() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestEpicHasOpenChildren(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
force bool
openChildCount int
wantErr bool
}{
{
name: "nil issue passes",
issue: nil,
force: false,
openChildCount: 5,
wantErr: false,
},
{
name: "non-epic passes regardless of children",
issue: &types.Issue{ID: "bd-test", IssueType: types.TypeTask},
force: false,
openChildCount: 10,
wantErr: false,
},
{
name: "epic with no open children passes",
issue: &types.Issue{ID: "bd-test", IssueType: types.TypeEpic},
force: false,
openChildCount: 0,
wantErr: false,
},
{
name: "epic with open children fails",
issue: &types.Issue{ID: "bd-test", IssueType: types.TypeEpic},
force: false,
openChildCount: 3,
wantErr: true,
},
{
name: "epic with open children passes with force",
issue: &types.Issue{ID: "bd-test", IssueType: types.TypeEpic},
force: true,
openChildCount: 3,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := EpicHasOpenChildren(tt.force, tt.openChildCount)("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("EpicHasOpenChildren() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestForUpdate(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
wantErr bool
}{
{
name: "nil issue fails",
issue: nil,
wantErr: true,
},
{
name: "template fails",
issue: &types.Issue{ID: "bd-test", IsTemplate: true},
wantErr: true,
},
{
name: "regular issue passes",
issue: &types.Issue{ID: "bd-test", IsTemplate: false},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := forUpdate()("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("forUpdate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestForClose(t *testing.T) {
tests := []struct {
name string
issue *types.Issue
force bool
wantErr bool
}{
{
name: "nil issue fails",
issue: nil,
force: false,
wantErr: true,
},
{
name: "template fails",
issue: &types.Issue{ID: "bd-test", IsTemplate: true},
force: false,
wantErr: true,
},
{
name: "pinned without force fails",
issue: &types.Issue{ID: "bd-test", Status: types.StatusPinned},
force: false,
wantErr: true,
},
{
name: "pinned with force passes",
issue: &types.Issue{ID: "bd-test", Status: types.StatusPinned},
force: true,
wantErr: false,
},
{
name: "regular open issue passes",
issue: &types.Issue{ID: "bd-test", Status: types.StatusOpen},
force: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := forClose(tt.force)("bd-test", tt.issue)
if (err != nil) != tt.wantErr {
t.Errorf("forClose() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}