chat_expand_test.go

 1package model
 2
 3import (
 4	"testing"
 5
 6	"github.com/charmbracelet/crush/internal/message"
 7	"github.com/charmbracelet/crush/internal/ui/chat"
 8	"github.com/stretchr/testify/require"
 9)
10
11// TestChatToggleExpandedSelectedItem_AssistantMessage is the regression test
12// for ยง4.8.1: before the fix, AssistantMessageItem.ToggleExpanded returned no
13// value so the type did not satisfy chat.Expandable, and the keyboard-driven
14// ToggleExpandedSelectedItem path silently skipped thinking blocks. This
15// wires a real Chat with an AssistantMessageItem, selects it, invokes
16// ToggleExpandedSelectedItem, and asserts the thinking block actually
17// flipped.
18func TestChatToggleExpandedSelectedItem_AssistantMessage(t *testing.T) {
19	t.Parallel()
20
21	u := newTestUI()
22
23	msg := &message.Message{
24		ID:   "m-assist",
25		Role: message.Assistant,
26		Parts: []message.ContentPart{
27			message.ReasoningContent{Thinking: "thinking about it"},
28		},
29	}
30	item := chat.NewAssistantMessageItem(u.com.Styles, msg)
31
32	// The keyboard expand path uses the generic Expandable interface;
33	// verifying satisfaction at runtime guards the contract.
34	exp, ok := item.(chat.Expandable)
35	require.True(t, ok, "AssistantMessageItem must satisfy chat.Expandable")
36
37	u.chat.SetMessages(item)
38	u.chat.SetSelected(0)
39
40	// First keyboard toggle should expand. Immediately follow with a
41	// direct ToggleExpanded: it flips the now-expanded item back to
42	// collapsed and returns false. If the keyboard path had silently
43	// skipped the item (the bug), the item would still be collapsed and
44	// the direct toggle would return true.
45	u.chat.ToggleExpandedSelectedItem()
46	require.False(t, exp.ToggleExpanded(),
47		"keyboard toggle did not expand the assistant thinking block")
48
49	// Second keyboard toggle against the re-collapsed item should expand
50	// it again. Direct ToggleExpanded then returns false because it
51	// re-collapses.
52	u.chat.ToggleExpandedSelectedItem()
53	require.False(t, exp.ToggleExpanded(),
54		"second keyboard toggle did not re-expand the assistant thinking block")
55}