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 TestEditConfirmationPreviewRendersDiffInsteadOfNewText(t *testing.T) {
78 args := json.RawMessage(`{"path":"sample.go","edits":[{"oldText":"old value","newText":"new value"}]}`)
79 v := View{
80 Theme: Dark,
81 ToolCalls: []ToolCallView{{
82 ID: "toolu_1",
83 Name: "edit",
84 RawJSONBuf: string(args),
85 LivePath: "sample.go",
86 Preview: "-old value\n+new value\n",
87 }},
88 }
89
90 plain := stripANSI(strings.Join(v.Build(80), "\n"))
91 for _, want := range []string{"-old value", "+new value"} {
92 if !strings.Contains(plain, want) {
93 t.Fatalf("confirmation preview missing %q:\n%s", want, plain)
94 }
95 }
96 if strings.Contains(plain, "edit 1 (streaming)") {
97 t.Fatalf("confirmation still showed the newText streaming view:\n%s", plain)
98 }
99}
100
101func TestLiveToolReservationResetsWhenExpansionChanges(t *testing.T) {
102 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"}`)
103 v := View{
104 Theme: Dark,
105 ToolCalls: []ToolCallView{
106 {ID: "toolu_1", Name: "write", RawJSONBuf: string(args), LivePath: "sample.ts"},
107 },
108 }
109
110 collapsedRows := len(v.Build(80))
111 v.ExpandAll = true
112 expandedRows := len(v.Build(80))
113 if expandedRows <= collapsedRows {
114 t.Fatalf("expanded live box did not grow: collapsed=%d expanded=%d", collapsedRows, expandedRows)
115 }
116
117 v.ExpandAll = false
118 if got := len(v.Build(80)); got != collapsedRows {
119 t.Fatalf("collapsed live box kept expanded height: got %d rows, want %d", got, collapsedRows)
120 }
121}
122
123func TestLiveToolReservationDoesNotLeakToNextCall(t *testing.T) {
124 v := View{Theme: Dark}
125 tall := json.RawMessage(`{"command":"line1\nline2\nline3\nline4\nline5"}`)
126 v.ToolCalls = []ToolCallView{
127 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall)},
128 }
129 tallRows := len(v.Build(80))
130
131 // A *different* tool call that is short must NOT inherit the tall
132 // call's reservation; it renders at its own natural height. (This
133 // is the regression that produced a giant empty box.)
134 short := json.RawMessage(`{"command":"echo hi"}`)
135 v.ToolCalls = []ToolCallView{
136 {ID: "toolu_2", Name: "bash", Args: ShortArgs("bash", short), RawJSONBuf: string(short)},
137 }
138 if got := len(v.Build(80)); got >= tallRows {
139 t.Fatalf("short call inherited prior call's height: %d rows (tall was %d)", got, tallRows)
140 }
141}
142
143func TestLiveToolReservationDropsOnResult(t *testing.T) {
144 v := View{Theme: Dark}
145 tall := json.RawMessage(`{"command":"line1\nline2\nline3\nline4\nline5"}`)
146 v.ToolCalls = []ToolCallView{
147 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall)},
148 }
149 tallRows := len(v.Build(80))
150
151 // Same id, now finalised with a tiny error result. The box must
152 // collapse to the natural result height instead of staying padded.
153 v.ToolCalls = []ToolCallView{
154 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall), Done: true, Error: true, Result: "boom"},
155 }
156 if got := len(v.Build(80)); got >= tallRows {
157 t.Fatalf("finalised box kept live reservation: %d rows (live was %d)", got, tallRows)
158 }
159}
160
161func TestLiveToolOverlayKeepsWritePreviewAfterArgsEnd(t *testing.T) {
162 args := json.RawMessage(`{"path":"/tmp/sample.ts","content":"export const n = 1\n"}`)
163 v := View{
164 Theme: Dark,
165 Messages: []provider.Message{
166 {
167 Role: provider.RoleAssistant,
168 Content: []provider.Content{
169 provider.ToolCallBlock{ID: "toolu_1", Name: "write", Arguments: args},
170 },
171 },
172 },
173 ToolCalls: []ToolCallView{
174 {
175 ID: "toolu_1",
176 Name: "write",
177 Args: ShortArgs("write", args),
178 Streaming: false,
179 RawJSONBuf: string(args),
180 LivePath: "/tmp/sample.ts",
181 },
182 },
183 }
184
185 plain := stripANSI(strings.Join(v.Build(80), "\n"))
186 if !strings.Contains(plain, "export const n = 1") {
187 t.Fatalf("write preview collapsed after tool args ended but before tool_result arrived:\n%s", plain)
188 }
189}
190
191func TestLiveToolOverlayHidesAfterToolResult(t *testing.T) {
192 args := json.RawMessage(`{"command":"sleep 1"}`)
193 v := View{
194 Theme: Dark,
195 Messages: []provider.Message{
196 {
197 Role: provider.RoleAssistant,
198 Content: []provider.Content{
199 provider.ToolCallBlock{ID: "toolu_1", Name: "bash", Arguments: args},
200 },
201 },
202 {
203 Role: provider.RoleTool,
204 Content: []provider.Content{
205 provider.ToolResultBlock{
206 CallID: "toolu_1",
207 Content: []provider.Content{provider.TextBlock{Text: "done"}},
208 },
209 },
210 },
211 },
212 ToolCalls: []ToolCallView{
213 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", args), Result: "done", Done: true},
214 },
215 }
216
217 plain := stripANSI(strings.Join(v.BuildLive(80), "\n"))
218 if strings.Contains(plain, "bash sleep 1") {
219 t.Fatalf("live tool overlay still rendered after tool_result was appended:\n%s", plain)
220 }
221}