|
| 1 | +package sidebar |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/charmbracelet/x/ansi" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/docker/docker-agent/pkg/runtime" |
| 12 | + "github.com/docker/docker-agent/pkg/session" |
| 13 | + "github.com/docker/docker-agent/pkg/tui/messages" |
| 14 | + "github.com/docker/docker-agent/pkg/tui/service" |
| 15 | +) |
| 16 | + |
| 17 | +func newBreadcrumbSidebar(t *testing.T) *model { |
| 18 | + t.Helper() |
| 19 | + sess := session.New() |
| 20 | + sessionState := service.NewSessionState(sess) |
| 21 | + sessionState.SetCurrentAgentName("root") |
| 22 | + |
| 23 | + m := New(sessionState).(*model) |
| 24 | + // Driving the real Update handlers starts the model's spinner, which registers |
| 25 | + // with the process-global animation coordinator. Release it on cleanup so a |
| 26 | + // leaked registration can't make HasActive() true for other sidebar tests |
| 27 | + // (e.g. TestSidebar_TitleRegenerating asserts the first animation is active). |
| 28 | + t.Cleanup(func() { m.spinner.Stop() }) |
| 29 | + m.sessionHasContent = true |
| 30 | + m.titleGenerated = true |
| 31 | + m.sessionTitle = "Test" |
| 32 | + m.currentAgent = "root" |
| 33 | + m.availableAgents = []runtime.AgentDetails{ |
| 34 | + {Name: "root", Provider: "openai", Model: "gpt-4o", Description: "Orchestrator"}, |
| 35 | + {Name: "librarian", Provider: "openai", Model: "gpt-4o", Description: "Finds documents"}, |
| 36 | + } |
| 37 | + m.width = 60 |
| 38 | + m.height = 60 |
| 39 | + return m |
| 40 | +} |
| 41 | + |
| 42 | +func streamStarted(agent, sessionID string) *runtime.StreamStartedEvent { |
| 43 | + return &runtime.StreamStartedEvent{ |
| 44 | + AgentContext: runtime.AgentContext{AgentName: agent}, |
| 45 | + SessionID: sessionID, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// TestAgentChainTracksSessionStack enforces the invariant |
| 50 | +// len(agentChain) == len(sessionStack): the chain is pushed on StreamStarted and |
| 51 | +// popped on StreamStopped, in lockstep with the session stack. |
| 52 | +// |
| 53 | +// Not parallel: it drives the real Update handlers, which start the spinner and |
| 54 | +// touch the process-global animation coordinator shared across tests. |
| 55 | +func TestAgentChainTracksSessionStack(t *testing.T) { |
| 56 | + m := newBreadcrumbSidebar(t) |
| 57 | + require.Len(t, m.agentChain, len(m.sessionStack)) |
| 58 | + |
| 59 | + m.Update(streamStarted("root", "s-root")) |
| 60 | + assert.Equal(t, []string{"root"}, m.agentChain) |
| 61 | + assert.Len(t, m.agentChain, len(m.sessionStack)) |
| 62 | + |
| 63 | + m.Update(streamStarted("librarian", "s-lib")) |
| 64 | + assert.Equal(t, []string{"root", "librarian"}, m.agentChain) |
| 65 | + assert.Len(t, m.agentChain, len(m.sessionStack)) |
| 66 | + |
| 67 | + m.Update(&runtime.StreamStoppedEvent{SessionID: "s-lib"}) |
| 68 | + assert.Equal(t, []string{"root"}, m.agentChain) |
| 69 | + assert.Len(t, m.agentChain, len(m.sessionStack)) |
| 70 | + |
| 71 | + m.Update(&runtime.StreamStoppedEvent{SessionID: "s-root"}) |
| 72 | + assert.Empty(t, m.agentChain) |
| 73 | + assert.Len(t, m.agentChain, len(m.sessionStack)) |
| 74 | +} |
| 75 | + |
| 76 | +// Not parallel: drives the real Update handlers, which touch the process-global |
| 77 | +// animation coordinator shared across tests. |
| 78 | +func TestAgentChainResets(t *testing.T) { |
| 79 | + t.Run("StreamCancelledMsg clears the chain", func(t *testing.T) { |
| 80 | + m := newBreadcrumbSidebar(t) |
| 81 | + m.Update(streamStarted("root", "s-root")) |
| 82 | + m.Update(streamStarted("librarian", "s-lib")) |
| 83 | + |
| 84 | + m.Update(messages.StreamCancelledMsg{}) |
| 85 | + |
| 86 | + assert.Empty(t, m.agentChain) |
| 87 | + assert.Empty(t, m.sessionStack) |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("ResetStreamTracking clears the chain", func(t *testing.T) { |
| 91 | + m := newBreadcrumbSidebar(t) |
| 92 | + m.Update(streamStarted("root", "s-root")) |
| 93 | + m.Update(streamStarted("librarian", "s-lib")) |
| 94 | + |
| 95 | + m.ResetStreamTracking() |
| 96 | + |
| 97 | + assert.Empty(t, m.agentChain) |
| 98 | + assert.Empty(t, m.sessionStack) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +// TestDelegationBreadcrumbRendersOnlyWhenNested verifies the breadcrumb shows the |
| 103 | +// active chain (e.g. "root ⏵ librarian") only once delegation depth exceeds 1. |
| 104 | +func TestDelegationBreadcrumbRendersOnlyWhenNested(t *testing.T) { |
| 105 | + t.Parallel() |
| 106 | + |
| 107 | + t.Run("hidden at depth <= 1", func(t *testing.T) { |
| 108 | + t.Parallel() |
| 109 | + m := newBreadcrumbSidebar(t) |
| 110 | + m.agentChain = []string{"root"} |
| 111 | + assert.NotContains(t, ansi.Strip(m.View()), "⏵") |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("shown at depth > 1", func(t *testing.T) { |
| 115 | + t.Parallel() |
| 116 | + m := newBreadcrumbSidebar(t) |
| 117 | + m.agentChain = []string{"root", "librarian"} |
| 118 | + assert.Contains(t, ansi.Strip(m.View()), "root ⏵ librarian") |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +// TestDelegationBreadcrumbPreservesAgentClickZones guards the buildAgentClickZones |
| 123 | +// skip logic: the breadcrumb block agentInfo prepends must not become a click |
| 124 | +// zone or shift the per-agent rows. Without the skip, the breadcrumb would claim |
| 125 | +// the first agent slot and the current-agent row would mis-map. |
| 126 | +func TestDelegationBreadcrumbPreservesAgentClickZones(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + m := newBreadcrumbSidebar(t) |
| 130 | + m.agentChain = []string{"root", "librarian"} |
| 131 | + |
| 132 | + _ = m.View() // populate agentClickZones + cachedLines |
| 133 | + |
| 134 | + for i, line := range m.cachedLines { |
| 135 | + stripped := ansi.Strip(line) |
| 136 | + // The breadcrumb (joined by ⏵) must never be an agent click target. |
| 137 | + if strings.Contains(stripped, "⏵") { |
| 138 | + _, isZone := m.agentClickZones[i] |
| 139 | + assert.False(t, isZone, "breadcrumb line %d must not be a click zone", i) |
| 140 | + } |
| 141 | + // The current-agent roster row (prefixed with ▶) must map to root. |
| 142 | + if strings.Contains(stripped, "▶") { |
| 143 | + assert.Equal(t, "root", m.agentClickZones[i], "current-agent row %d should map to root", i) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + var clickable []string |
| 148 | + for _, name := range m.agentClickZones { |
| 149 | + clickable = append(clickable, name) |
| 150 | + } |
| 151 | + assert.Contains(t, clickable, "root") |
| 152 | + assert.Contains(t, clickable, "librarian") |
| 153 | +} |
0 commit comments