Files
beads/cmd/bd/relate_test.go
Aarya Reddy bd5fc214c3 feat(ready,blocked): Add --parent flag for scoping by epic/bead desce… (#743)
* feat(ready,blocked): Add --parent flag for scoping by epic/bead descendants

Add --parent flag to `bd ready` and `bd blocked` CLI commands and MCP tools
to filter results to all descendants of a specific epic or bead.

## Backward Compatibility

- CLI: New optional --parent flag; existing usage unchanged
- RPC: New `blocked` operation added (was missing); existing operations unchanged
- MCP: New optional `parent` parameter; existing calls work as before
- Storage interface: GetBlockedIssues signature changed to accept WorkFilter
  - All callers updated to pass empty filter for existing behavior
  - Empty WorkFilter{} returns identical results to previous implementation

## Implementation Details

SQLite uses recursive CTE to traverse parent-child hierarchy:

    WITH RECURSIVE descendants AS (
        SELECT issue_id FROM dependencies
        WHERE type = 'parent-child' AND depends_on_id = ?
        UNION ALL
        SELECT d.issue_id FROM dependencies d
        JOIN descendants dt ON d.depends_on_id = dt.issue_id
        WHERE d.type = 'parent-child'
    )
    SELECT issue_id FROM descendants

MemoryStorage implements equivalent recursive traversal with visited-set
cycle protection via collectDescendants helper.

Parent filter composes with existing filters (priority, labels, assignee, etc.)
as an additional WHERE clause - all filters are AND'd together.

## RPC Blocked Support

MCP beads_blocked() existed but daemon client raised NotImplementedError.
Added OpBlocked and handleBlocked to enable daemon RPC path, which was
previously broken. Now both CLI and daemon clients work for blocked queries.

## Changes

- internal/types/types.go: Add ParentID *string to WorkFilter
- internal/storage/sqlite/ready.go: Add recursive CTE for parent filtering
- internal/storage/memory/memory.go: Add getAllDescendants/collectDescendants
- internal/storage/storage.go: Update GetBlockedIssues interface signature
- cmd/bd/ready.go: Add --parent flag to ready and blocked commands
- internal/rpc/protocol.go: Add OpBlocked constant and BlockedArgs type
- internal/rpc/server_issues_epics.go: Add handleBlocked RPC handler
- internal/rpc/client.go: Add Blocked client method
- integrations/beads-mcp/: Add BlockedParams model and parent parameter

## Usage

  bd ready --parent bd-abc              # All ready descendants
  bd ready --parent bd-abc --priority 1 # Combined with other filters
  bd blocked --parent bd-abc            # All blocked descendants

## Testing

Added 4 test cases for parent filtering:
- TestParentIDFilterDescendants: Verifies recursive traversal (grandchildren)
- TestParentIDWithOtherFilters: Verifies composition with priority filter
- TestParentIDWithBlockedDescendants: Verifies blocked issues excluded from ready
- TestParentIDEmptyParent: Verifies empty result for childless parent

* fix: Correct blockedCmd indentation and suppress gosec false positive

- Fix syntax error from incorrect indentation in blockedCmd Run function
- Add nolint:gosec comment for GetBlockedIssues SQL formatting (G201)
  The filterSQL variable contains only parameterized WHERE clauses with
  ? placeholders, not user input
2025-12-25 21:11:58 -08:00

235 lines
6.4 KiB
Go

package main
import (
"context"
"path/filepath"
"testing"
"time"
"github.com/steveyegge/beads/internal/types"
)
// TestRelateCommand tests the bd relate command functionality.
// This is a regression test for Decision 004 Phase 4 - relates-to links
// are now stored in the dependencies table.
func TestRelateCommand(t *testing.T) {
tmpDir := t.TempDir()
testDB := filepath.Join(tmpDir, ".beads", "beads.db")
s := newTestStore(t, testDB)
ctx := context.Background()
t.Run("relate creates bidirectional link", func(t *testing.T) {
// Create two issues
issue1 := &types.Issue{
ID: "test-relate-1",
Title: "Issue 1",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
CreatedAt: time.Now(),
}
issue2 := &types.Issue{
ID: "test-relate-2",
Title: "Issue 2",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
CreatedAt: time.Now(),
}
if err := s.CreateIssue(ctx, issue1, "test"); err != nil {
t.Fatalf("CreateIssue failed: %v", err)
}
if err := s.CreateIssue(ctx, issue2, "test"); err != nil {
t.Fatalf("CreateIssue failed: %v", err)
}
// Simulate what bd relate does: add bidirectional relates-to
dep1 := &types.Dependency{
IssueID: issue1.ID,
DependsOnID: issue2.ID,
Type: types.DepRelatesTo,
}
if err := s.AddDependency(ctx, dep1, "test"); err != nil {
t.Fatalf("AddDependency (1->2) failed: %v", err)
}
dep2 := &types.Dependency{
IssueID: issue2.ID,
DependsOnID: issue1.ID,
Type: types.DepRelatesTo,
}
if err := s.AddDependency(ctx, dep2, "test"); err != nil {
t.Fatalf("AddDependency (2->1) failed: %v", err)
}
// Verify bidirectional link exists
deps1, err := s.GetDependenciesWithMetadata(ctx, issue1.ID)
if err != nil {
t.Fatalf("GetDependenciesWithMetadata failed: %v", err)
}
found1to2 := false
for _, d := range deps1 {
if d.ID == issue2.ID && d.DependencyType == types.DepRelatesTo {
found1to2 = true
}
}
if !found1to2 {
t.Errorf("issue1 should have relates-to link to issue2")
}
deps2, err := s.GetDependenciesWithMetadata(ctx, issue2.ID)
if err != nil {
t.Fatalf("GetDependenciesWithMetadata failed: %v", err)
}
found2to1 := false
for _, d := range deps2 {
if d.ID == issue1.ID && d.DependencyType == types.DepRelatesTo {
found2to1 = true
}
}
if !found2to1 {
t.Errorf("issue2 should have relates-to link to issue1")
}
})
t.Run("unrelate removes bidirectional link", func(t *testing.T) {
// Create two issues
issue1 := &types.Issue{
ID: "test-unrelate-1",
Title: "Issue 1",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
CreatedAt: time.Now(),
}
issue2 := &types.Issue{
ID: "test-unrelate-2",
Title: "Issue 2",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
CreatedAt: time.Now(),
}
if err := s.CreateIssue(ctx, issue1, "test"); err != nil {
t.Fatalf("CreateIssue failed: %v", err)
}
if err := s.CreateIssue(ctx, issue2, "test"); err != nil {
t.Fatalf("CreateIssue failed: %v", err)
}
// Add bidirectional relates-to
dep1 := &types.Dependency{
IssueID: issue1.ID,
DependsOnID: issue2.ID,
Type: types.DepRelatesTo,
}
if err := s.AddDependency(ctx, dep1, "test"); err != nil {
t.Fatalf("AddDependency (1->2) failed: %v", err)
}
dep2 := &types.Dependency{
IssueID: issue2.ID,
DependsOnID: issue1.ID,
Type: types.DepRelatesTo,
}
if err := s.AddDependency(ctx, dep2, "test"); err != nil {
t.Fatalf("AddDependency (2->1) failed: %v", err)
}
// Simulate what bd unrelate does: remove both directions
if err := s.RemoveDependency(ctx, issue1.ID, issue2.ID, "test"); err != nil {
t.Fatalf("RemoveDependency (1->2) failed: %v", err)
}
if err := s.RemoveDependency(ctx, issue2.ID, issue1.ID, "test"); err != nil {
t.Fatalf("RemoveDependency (2->1) failed: %v", err)
}
// Verify links are gone
deps1, err := s.GetDependenciesWithMetadata(ctx, issue1.ID)
if err != nil {
t.Fatalf("GetDependenciesWithMetadata failed: %v", err)
}
for _, d := range deps1 {
if d.ID == issue2.ID && d.DependencyType == types.DepRelatesTo {
t.Errorf("issue1 should NOT have relates-to link to issue2 after unrelate")
}
}
deps2, err := s.GetDependenciesWithMetadata(ctx, issue2.ID)
if err != nil {
t.Fatalf("GetDependenciesWithMetadata failed: %v", err)
}
for _, d := range deps2 {
if d.ID == issue1.ID && d.DependencyType == types.DepRelatesTo {
t.Errorf("issue2 should NOT have relates-to link to issue1 after unrelate")
}
}
})
t.Run("relates-to does not block", func(t *testing.T) {
// Create two issues
issue1 := &types.Issue{
ID: "test-noblock-1",
Title: "Issue 1",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
CreatedAt: time.Now(),
}
issue2 := &types.Issue{
ID: "test-noblock-2",
Title: "Issue 2",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
CreatedAt: time.Now(),
}
if err := s.CreateIssue(ctx, issue1, "test"); err != nil {
t.Fatalf("CreateIssue failed: %v", err)
}
if err := s.CreateIssue(ctx, issue2, "test"); err != nil {
t.Fatalf("CreateIssue failed: %v", err)
}
// Add relates-to (not blocks)
dep := &types.Dependency{
IssueID: issue1.ID,
DependsOnID: issue2.ID,
Type: types.DepRelatesTo,
}
if err := s.AddDependency(ctx, dep, "test"); err != nil {
t.Fatalf("AddDependency failed: %v", err)
}
// Issue1 should NOT be blocked (relates-to doesn't block)
blocked, err := s.GetBlockedIssues(ctx, types.WorkFilter{})
if err != nil {
t.Fatalf("GetBlockedIssues failed: %v", err)
}
for _, b := range blocked {
if b.ID == issue1.ID {
t.Errorf("issue1 should NOT be blocked by relates-to dependency")
}
}
})
}
// TestRelateCommandInit tests that the relate and unrelate commands are properly initialized.
func TestRelateCommandInit(t *testing.T) {
if relateCmd == nil {
t.Fatal("relateCmd should be initialized")
}
if relateCmd.Use != "relate <id1> <id2>" {
t.Errorf("Expected Use='relate <id1> <id2>', got %q", relateCmd.Use)
}
if unrelateCmd == nil {
t.Fatal("unrelateCmd should be initialized")
}
if unrelateCmd.Use != "unrelate <id1> <id2>" {
t.Errorf("Expected Use='unrelate <id1> <id2>', got %q", unrelateCmd.Use)
}
}