1package tui
2
3import (
4 "encoding/json"
5 "strings"
6 "testing"
7
8 "github.com/patriceckhart/zot/packages/provider"
9)
10
11func TestLiveToolOverlayRemainsAfterAssistantToolUse(t *testing.T) {
12 args := json.RawMessage(`{"command":"sleep 1"}`)
13 v := View{
14 Theme: Dark,
15 Messages: []provider.Message{
16 {
17 Role: provider.RoleAssistant,
18 Content: []provider.Content{
19 provider.ToolCallBlock{ID: "toolu_1", Name: "bash", Arguments: args},
20 },
21 },
22 },
23 ToolCalls: []ToolCallView{
24 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", args), Done: false},
25 },
26 }
27
28 plain := stripANSI(strings.Join(v.Build(80), "\n"))
29 if !strings.Contains(plain, "bash sleep 1") {
30 t.Fatalf("live tool overlay disappeared after assistant tool_use was appended:\n%s", plain)
31 }
32}
33
34func TestLiveToolOverlayShowsFullBashCommandBeforeResult(t *testing.T) {
35 args := json.RawMessage(`{"command":"printf 'start' && sleep 60 && printf 'done with a command long enough to exceed the header truncation limit'"}`)
36 v := View{
37 Theme: Dark,
38 ToolCalls: []ToolCallView{
39 {
40 ID: "toolu_1",
41 Name: "bash",
42 Args: ShortArgs("bash", args),
43 RawJSONBuf: string(args),
44 },
45 },
46 }
47
48 plain := stripANSI(strings.Join(v.BuildLive(100), "\n"))
49 if !strings.Contains(plain, "$ printf 'start' && sleep 60") || !strings.Contains(plain, "header truncation limit") {
50 t.Fatalf("bash command was not visible before result arrived:\n%s", plain)
51 }
52}
53
54func TestLiveToolOverlayHeightDoesNotShrinkMidStream(t *testing.T) {
55 v := View{Theme: Dark}
56 tall := json.RawMessage(`{"command":"line1\nline2\nline3\nline4\nline5"}`)
57 v.ToolCalls = []ToolCallView{
58 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall)},
59 }
60 tallRows := len(v.Build(80))
61 if tallRows == 0 {
62 t.Fatal("expected rows for the tall command")
63 }
64
65 // Same call id, but the streamed command shrinks (a transient
66 // mid-stream state). The box must keep its high-water height so the
67 // editor/status band below doesn't bounce.
68 short := json.RawMessage(`{"command":"echo hi"}`)
69 v.ToolCalls = []ToolCallView{
70 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", short), RawJSONBuf: string(short)},
71 }
72 if got := len(v.Build(80)); got < tallRows {
73 t.Fatalf("live box shrank mid-stream from %d to %d rows", tallRows, got)
74 }
75}
76
77func TestLiveToolReservationResetsWhenExpansionChanges(t *testing.T) {
78 args := json.RawMessage(`{"path":"sample.ts","content":"line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\nline14\nline15\nline16\nline17\nline18\nline19\nline20"}`)
79 v := View{
80 Theme: Dark,
81 ToolCalls: []ToolCallView{
82 {ID: "toolu_1", Name: "write", RawJSONBuf: string(args), LivePath: "sample.ts"},
83 },
84 }
85
86 collapsedRows := len(v.Build(80))
87 v.ExpandAll = true
88 expandedRows := len(v.Build(80))
89 if expandedRows <= collapsedRows {
90 t.Fatalf("expanded live box did not grow: collapsed=%d expanded=%d", collapsedRows, expandedRows)
91 }
92
93 v.ExpandAll = false
94 if got := len(v.Build(80)); got != collapsedRows {
95 t.Fatalf("collapsed live box kept expanded height: got %d rows, want %d", got, collapsedRows)
96 }
97}
98
99func TestLiveToolReservationDoesNotLeakToNextCall(t *testing.T) {
100 v := View{Theme: Dark}
101 tall := json.RawMessage(`{"command":"line1\nline2\nline3\nline4\nline5"}`)
102 v.ToolCalls = []ToolCallView{
103 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall)},
104 }
105 tallRows := len(v.Build(80))
106
107 // A *different* tool call that is short must NOT inherit the tall
108 // call's reservation; it renders at its own natural height. (This
109 // is the regression that produced a giant empty box.)
110 short := json.RawMessage(`{"command":"echo hi"}`)
111 v.ToolCalls = []ToolCallView{
112 {ID: "toolu_2", Name: "bash", Args: ShortArgs("bash", short), RawJSONBuf: string(short)},
113 }
114 if got := len(v.Build(80)); got >= tallRows {
115 t.Fatalf("short call inherited prior call's height: %d rows (tall was %d)", got, tallRows)
116 }
117}
118
119func TestLiveToolReservationDropsOnResult(t *testing.T) {
120 v := View{Theme: Dark}
121 tall := json.RawMessage(`{"command":"line1\nline2\nline3\nline4\nline5"}`)
122 v.ToolCalls = []ToolCallView{
123 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall)},
124 }
125 tallRows := len(v.Build(80))
126
127 // Same id, now finalised with a tiny error result. The box must
128 // collapse to the natural result height instead of staying padded.
129 v.ToolCalls = []ToolCallView{
130 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall), Done: true, Error: true, Result: "boom"},
131 }
132 if got := len(v.Build(80)); got >= tallRows {
133 t.Fatalf("finalised box kept live reservation: %d rows (live was %d)", got, tallRows)
134 }
135}
136
137func TestLiveToolOverlayKeepsWritePreviewAfterArgsEnd(t *testing.T) {
138 args := json.RawMessage(`{"path":"/tmp/sample.ts","content":"export const n = 1\n"}`)
139 v := View{
140 Theme: Dark,
141 Messages: []provider.Message{
142 {
143 Role: provider.RoleAssistant,
144 Content: []provider.Content{
145 provider.ToolCallBlock{ID: "toolu_1", Name: "write", Arguments: args},
146 },
147 },
148 },
149 ToolCalls: []ToolCallView{
150 {
151 ID: "toolu_1",
152 Name: "write",
153 Args: ShortArgs("write", args),
154 Streaming: false,
155 RawJSONBuf: string(args),
156 LivePath: "/tmp/sample.ts",
157 },
158 },
159 }
160
161 plain := stripANSI(strings.Join(v.Build(80), "\n"))
162 if !strings.Contains(plain, "export const n = 1") {
163 t.Fatalf("write preview collapsed after tool args ended but before tool_result arrived:\n%s", plain)
164 }
165}
166
167func TestLiveToolOverlayHidesAfterToolResult(t *testing.T) {
168 args := json.RawMessage(`{"command":"sleep 1"}`)
169 v := View{
170 Theme: Dark,
171 Messages: []provider.Message{
172 {
173 Role: provider.RoleAssistant,
174 Content: []provider.Content{
175 provider.ToolCallBlock{ID: "toolu_1", Name: "bash", Arguments: args},
176 },
177 },
178 {
179 Role: provider.RoleTool,
180 Content: []provider.Content{
181 provider.ToolResultBlock{
182 CallID: "toolu_1",
183 Content: []provider.Content{provider.TextBlock{Text: "done"}},
184 },
185 },
186 },
187 },
188 ToolCalls: []ToolCallView{
189 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", args), Result: "done", Done: true},
190 },
191 }
192
193 plain := stripANSI(strings.Join(v.BuildLive(80), "\n"))
194 if strings.Contains(plain, "bash sleep 1") {
195 t.Fatalf("live tool overlay still rendered after tool_result was appended:\n%s", plain)
196 }
197}