beads/cmd/bd/graph_visual_test.go
ecuthiell 3402679901
fix(output): route graph renderers and ratchet stdout writes (#5624)
* fix(graph): route render modes through command writer (#5622)

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell

* test(output): ratchet list and graph stdout routing (#5617)

Replace the rejected source-shape census with typed forbidigo rules for process-stdout writes across list and graph files. Existing out-of-scope paths carry visible line-local exceptions; new sites fail lint by default.

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell
Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>

* docs(lint): link graph diagnostics and exclusions (saa-cgh.75)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

---------

Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>
2026-09-30 15:55:33 -07:00

313 lines
8.7 KiB
Go

package main
import (
"bytes"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
func TestRenderGraphVisual(t *testing.T) {
t.Parallel()
subgraph, layout := makeTestSubgraph()
var output bytes.Buffer
if err := renderGraphVisual(&output, layout, subgraph); err != nil {
t.Fatalf("renderGraphVisual: %v", err)
}
got := output.String()
// Verify layer headers
if !strings.Contains(got, "LAYER 0") {
t.Error("Visual output should contain LAYER 0 header")
}
if !strings.Contains(got, "ready") {
t.Error("Visual output should mark layer 0 as ready")
}
// Verify all node IDs are present
for _, id := range []string{"test-a", "test-b", "test-c", "test-d"} {
if !strings.Contains(got, id) {
t.Errorf("Visual output should contain node %q", id)
}
}
// Verify node titles are present
for _, title := range []string{"Root issue", "Child task", "Blocked task", "Done task"} {
if !strings.Contains(got, title) {
t.Errorf("Visual output should contain title %q", title)
}
}
// Verify box-drawing characters are used
if !strings.Contains(got, "┌") || !strings.Contains(got, "┘") {
t.Error("Visual output should use box-drawing characters for node borders")
}
// Verify edge arrows exist (edges between layers)
if !strings.Contains(got, "▶") {
t.Error("Visual output should contain arrow characters for edges")
}
// Verify summary
if !strings.Contains(got, "layers") {
t.Error("Visual output should contain summary with layer count")
}
}
func TestRenderGraphVisual_Empty(t *testing.T) {
t.Parallel()
emptySubgraph := &TemplateSubgraph{
Root: &types.Issue{ID: "empty"},
Issues: []*types.Issue{},
IssueMap: map[string]*types.Issue{},
}
layout := &GraphLayout{
Nodes: map[string]*GraphNode{},
Layers: [][]string{},
RootID: "empty",
}
var output bytes.Buffer
if err := renderGraphVisual(&output, layout, emptySubgraph); err != nil {
t.Fatalf("renderGraphVisual: %v", err)
}
if !strings.Contains(output.String(), "Empty graph") {
t.Error("Empty visual output should say 'Empty graph'")
}
}
func TestRenderGraphVisual_SingleNode(t *testing.T) {
t.Parallel()
issue := &types.Issue{
ID: "solo-1", Title: "Solo issue", Status: types.StatusOpen,
Priority: 0, IssueType: types.TypeTask,
}
subgraph := &TemplateSubgraph{
Root: issue,
Issues: []*types.Issue{issue},
IssueMap: map[string]*types.Issue{"solo-1": issue},
}
layout := computeLayout(subgraph)
var output bytes.Buffer
if err := renderGraphVisual(&output, layout, subgraph); err != nil {
t.Fatalf("renderGraphVisual: %v", err)
}
got := output.String()
if !strings.Contains(got, "solo-1") {
t.Error("Single-node visual should contain the issue ID")
}
if !strings.Contains(got, "Solo issue") {
t.Error("Single-node visual should contain the title")
}
// No edges for single node
if strings.Contains(got, "▶") {
t.Error("Single-node visual should not contain edge arrows")
}
}
func TestDAGNodeLine(t *testing.T) {
t.Parallel()
node := &GraphNode{
Issue: &types.Issue{
ID: "test-1", Title: "Test Node", Status: types.StatusOpen,
Priority: 2, IssueType: types.TypeTask,
},
}
nodeW := 20
// Top border
top := dagNodeLine(node, nodeW, 0)
if !strings.HasPrefix(top, "┌") || !strings.HasSuffix(top, "┐") {
t.Errorf("Top border should be ┌───┐, got: %s", top)
}
// Title line
title := dagNodeLine(node, nodeW, 1)
if !strings.HasPrefix(title, "│") || !strings.HasSuffix(title, "│") {
t.Error("Title line should be bordered with │")
}
if !strings.Contains(title, "Test Node") {
t.Error("Title line should contain the issue title")
}
// ID line
idLine := dagNodeLine(node, nodeW, 2)
if !strings.Contains(idLine, "test-1") {
t.Error("ID line should contain the issue ID")
}
if !strings.Contains(idLine, "P2") {
t.Error("ID line should contain priority")
}
// Bottom border
bottom := dagNodeLine(node, nodeW, 3)
if !strings.HasPrefix(bottom, "└") || !strings.HasSuffix(bottom, "┘") {
t.Errorf("Bottom border should be └───┘, got: %s", bottom)
}
}
func TestComputeDAGNodeWidth(t *testing.T) {
t.Parallel()
_, layout := makeTestSubgraph()
w := computeDAGNodeWidth(layout)
if w < 18 {
t.Errorf("DAG node width should be at least 18, got %d", w)
}
}
func TestCollectGutterEdges(t *testing.T) {
t.Parallel()
subgraph, layout := makeTestSubgraph()
numLayers := len(layout.Layers)
edges := collectGutterEdges(layout, subgraph, numLayers)
// Should have gutter entries for each layer gap
if len(edges) != numLayers-1 {
t.Errorf("Expected %d gutter edge lists, got %d", numLayers-1, len(edges))
}
// Should have at least one edge (test-a -> test-b in gutter 0)
totalEdges := 0
for _, ge := range edges {
totalEdges += len(ge)
}
if totalEdges == 0 {
t.Error("Should have at least one edge in gutters")
}
}
func TestBuildDAGGutterGrid_SameRow(t *testing.T) {
t.Parallel()
edges := []dagEdgeInfo{{sourceRow: 0, targetRow: 0}}
gutterW := 6
bandH := 5
totalLines := 5
lines := buildDAGGutterGrid(edges, gutterW, totalLines, bandH)
if len(lines) != totalLines {
t.Errorf("Expected %d gutter lines, got %d", totalLines, len(lines))
}
// Content line (offset 1) should have horizontal arrow
contentLine := lines[1]
if !strings.Contains(contentLine, "▶") {
t.Errorf("Same-row edge should have arrow, got: %q", contentLine)
}
if !strings.Contains(contentLine, "─") {
t.Errorf("Same-row edge should have horizontal line, got: %q", contentLine)
}
}
func TestBuildDAGGutterGrid_DifferentRow(t *testing.T) {
t.Parallel()
edges := []dagEdgeInfo{{sourceRow: 0, targetRow: 1}}
gutterW := 6
bandH := 5
totalLines := 10
lines := buildDAGGutterGrid(edges, gutterW, totalLines, bandH)
// Source content line (row 0, subline 1) should have outgoing edge
sourceLine := lines[1]
if !strings.Contains(sourceLine, "─") {
t.Errorf("Source row should have horizontal connector, got: %q", sourceLine)
}
if !strings.Contains(sourceLine, "╮") {
t.Errorf("Source row should have corner character, got: %q", sourceLine)
}
// Target content line (row 1, subline 1 = line 6) should have incoming edge
targetLine := lines[6]
if !strings.Contains(targetLine, "▶") {
t.Errorf("Target row should have arrow, got: %q", targetLine)
}
if !strings.Contains(targetLine, "╰") {
t.Errorf("Target row should have corner character, got: %q", targetLine)
}
// Intermediate lines should have vertical connector
hasVertical := false
for y := 2; y < 6; y++ {
if strings.Contains(lines[y], "│") {
hasVertical = true
break
}
}
if !hasVertical {
t.Error("Intermediate lines should have vertical connector")
}
}
func TestDAGMergeRune(t *testing.T) {
t.Parallel()
tests := []struct {
existing, incoming rune
want rune
}{
{' ', '─', '─'},
{' ', '│', '│'},
{'│', '─', '┼'},
{'─', '│', '┼'},
{'─', '▶', '▶'},
{'─', '─', '─'},
{'│', '│', '│'},
{'│', '╮', '┤'},
{'│', '╰', '├'},
}
for _, tt := range tests {
got := dagMergeRune(tt.existing, tt.incoming)
if got != tt.want {
t.Errorf("dagMergeRune(%c, %c) = %c, want %c", tt.existing, tt.incoming, got, tt.want)
}
}
}
func TestCollectGutterEdges_SkipLayers(t *testing.T) {
t.Parallel()
// Create a graph where an edge skips a layer and goes to a different row.
// A(L0,row0) -> B(L1,row0) -> D(L2)
// A(L0,row0) -> C(L1,row1) -> D(L2)
// The edge routing should handle multiple layers correctly.
issueA := &types.Issue{ID: "a", Title: "A", Status: types.StatusOpen, IssueType: types.TypeTask}
issueB := &types.Issue{ID: "b", Title: "B", Status: types.StatusOpen, IssueType: types.TypeTask}
issueC := &types.Issue{ID: "c", Title: "C", Status: types.StatusOpen, IssueType: types.TypeTask}
issueD := &types.Issue{ID: "d", Title: "D", Status: types.StatusOpen, IssueType: types.TypeTask}
subgraph := &TemplateSubgraph{
Root: issueA,
Issues: []*types.Issue{issueA, issueB, issueC, issueD},
IssueMap: map[string]*types.Issue{
"a": issueA, "b": issueB, "c": issueC, "d": issueD,
},
Dependencies: []*types.Dependency{
{IssueID: "b", DependsOnID: "a", Type: types.DepBlocks},
{IssueID: "c", DependsOnID: "a", Type: types.DepBlocks},
{IssueID: "d", DependsOnID: "b", Type: types.DepBlocks},
{IssueID: "d", DependsOnID: "c", Type: types.DepBlocks},
},
}
layout := computeLayout(subgraph)
numLayers := len(layout.Layers)
edges := collectGutterEdges(layout, subgraph, numLayers)
// Should have gutter entries
if numLayers < 2 {
t.Fatalf("Expected at least 2 layers, got %d", numLayers)
}
// Gutter 0 should have edges for a->b and a->c
if len(edges[0]) < 2 {
t.Errorf("Gutter 0 should have at least 2 edges (a->b and a->c), got %d", len(edges[0]))
}
}