1package tui
2
3import (
4 "encoding/json"
5 "strings"
6 "testing"
7
8 "github.com/patriceckhart/zot/packages/provider"
9)
10
11func TestStartupResourcesRenderAsCompactSections(t *testing.T) {
12 v := View{
13 Theme: Dark,
14 StartupAgentName: "zot-maintenance",
15 StartupContextPaths: []string{
16 "/home/user/AGENTS.md",
17 "/repo/AGENTS.md",
18 },
19 StartupExtensionNames: []string{"todo", "workspaces"},
20 StartupSkillNames: []string{"review", "test"},
21 }
22
23 plain := stripANSI(strings.Join(v.Build(80), "\n"))
24 for _, want := range []string{
25 "[Agent]", "zot-maintenance",
26 "[Context]", "/home/user/AGENTS.md, /repo/AGENTS.md",
27 "[Extensions]", "todo, workspaces",
28 "[Skills]", "review, test",
29 } {
30 if !strings.Contains(plain, want) {
31 t.Fatalf("startup resources missing %q:\n%s", want, plain)
32 }
33 }
34 if strings.ContainsAny(plain, boxGlyphs) || strings.Contains(plain, "read ") {
35 t.Fatalf("startup resources rendered as a tool call:\n%s", plain)
36 }
37 if len(v.Messages) != 0 {
38 t.Fatalf("startup resources added %d transcript messages", len(v.Messages))
39 }
40}
41
42func TestLiveToolOverlayRemainsAfterAssistantToolUse(t *testing.T) {
43 args := json.RawMessage(`{"command":"sleep 1"}`)
44 v := View{
45 Theme: Dark,
46 Messages: []provider.Message{
47 {
48 Role: provider.RoleAssistant,
49 Content: []provider.Content{
50 provider.ToolCallBlock{ID: "toolu_1", Name: "bash", Arguments: args},
51 },
52 },
53 },
54 ToolCalls: []ToolCallView{
55 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", args), Done: false},
56 },
57 }
58
59 plain := stripANSI(strings.Join(v.Build(80), "\n"))
60 if !strings.Contains(plain, "bash sleep 1") {
61 t.Fatalf("live tool overlay disappeared after assistant tool_use was appended:\n%s", plain)
62 }
63}
64
65func TestLiveToolOverlayShowsFullBashCommandBeforeResult(t *testing.T) {
66 args := json.RawMessage(`{"command":"printf 'start' && sleep 60 && printf 'done with a command long enough to exceed the header truncation limit'"}`)
67 v := View{
68 Theme: Dark,
69 ToolCalls: []ToolCallView{
70 {
71 ID: "toolu_1",
72 Name: "bash",
73 Args: ShortArgs("bash", args),
74 RawJSONBuf: string(args),
75 },
76 },
77 }
78
79 plain := stripANSI(strings.Join(v.BuildLive(100), "\n"))
80 if !strings.Contains(plain, "$ printf 'start' && sleep 60") || !strings.Contains(plain, "header truncation limit") {
81 t.Fatalf("bash command was not visible before result arrived:\n%s", plain)
82 }
83}
84
85func TestLiveToolOverlayHeightDoesNotShrinkMidStream(t *testing.T) {
86 v := View{Theme: Dark}
87 tall := json.RawMessage(`{"command":"line1\nline2\nline3\nline4\nline5"}`)
88 v.ToolCalls = []ToolCallView{
89 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall)},
90 }
91 tallRows := len(v.Build(80))
92 if tallRows == 0 {
93 t.Fatal("expected rows for the tall command")
94 }
95
96 // Same call id, but the streamed command shrinks (a transient
97 // mid-stream state). The box must keep its high-water height so the
98 // editor/status band below doesn't bounce.
99 short := json.RawMessage(`{"command":"echo hi"}`)
100 v.ToolCalls = []ToolCallView{
101 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", short), RawJSONBuf: string(short)},
102 }
103 if got := len(v.Build(80)); got < tallRows {
104 t.Fatalf("live box shrank mid-stream from %d to %d rows", tallRows, got)
105 }
106}
107
108func TestEditConfirmationPreviewRendersDiffInsteadOfNewText(t *testing.T) {
109 args := json.RawMessage(`{"path":"sample.go","edits":[{"oldText":"old value","newText":"new value"}]}`)
110 v := View{
111 Theme: Dark,
112 ToolCalls: []ToolCallView{{
113 ID: "toolu_1",
114 Name: "edit",
115 RawJSONBuf: string(args),
116 LivePath: "sample.go",
117 Preview: "-old value\n+new value\n",
118 }},
119 }
120
121 rendered := strings.Join(v.Build(80), "\n")
122 plain := stripANSI(rendered)
123 for _, want := range []string{"- 1 old value", "+ 1 new value"} {
124 if !strings.Contains(plain, want) {
125 t.Fatalf("confirmation preview missing numbered diff row %q:\n%s", want, plain)
126 }
127 }
128 for _, want := range []string{
129 Dark.FG256(Dark.Error, "- 1 "),
130 Dark.FG256(Dark.Tool, "+ 1 "),
131 } {
132 if !strings.Contains(rendered, want) {
133 t.Fatalf("confirmation preview missing diff color %q:\n%s", want, rendered)
134 }
135 }
136 if strings.Contains(plain, "edit 1 (streaming)") {
137 t.Fatalf("confirmation still showed the newText streaming view:\n%s", plain)
138 }
139}
140
141func TestLiveToolReservationResetsWhenExpansionChanges(t *testing.T) {
142 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"}`)
143 v := View{
144 Theme: Dark,
145 ToolCalls: []ToolCallView{
146 {ID: "toolu_1", Name: "write", RawJSONBuf: string(args), LivePath: "sample.ts"},
147 },
148 }
149
150 collapsedRows := len(v.Build(80))
151 v.ExpandAll = true
152 expandedRows := len(v.Build(80))
153 if expandedRows <= collapsedRows {
154 t.Fatalf("expanded live box did not grow: collapsed=%d expanded=%d", collapsedRows, expandedRows)
155 }
156
157 v.ExpandAll = false
158 if got := len(v.Build(80)); got != collapsedRows {
159 t.Fatalf("collapsed live box kept expanded height: got %d rows, want %d", got, collapsedRows)
160 }
161}
162
163func TestLiveToolReservationDoesNotLeakToNextCall(t *testing.T) {
164 v := View{Theme: Dark}
165 tall := json.RawMessage(`{"command":"line1\nline2\nline3\nline4\nline5"}`)
166 v.ToolCalls = []ToolCallView{
167 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall)},
168 }
169 tallRows := len(v.Build(80))
170
171 // A *different* tool call that is short must NOT inherit the tall
172 // call's reservation; it renders at its own natural height. (This
173 // is the regression that produced a giant empty box.)
174 short := json.RawMessage(`{"command":"echo hi"}`)
175 v.ToolCalls = []ToolCallView{
176 {ID: "toolu_2", Name: "bash", Args: ShortArgs("bash", short), RawJSONBuf: string(short)},
177 }
178 if got := len(v.Build(80)); got >= tallRows {
179 t.Fatalf("short call inherited prior call's height: %d rows (tall was %d)", got, tallRows)
180 }
181}
182
183func TestLiveToolReservationDropsOnResult(t *testing.T) {
184 v := View{Theme: Dark}
185 tall := json.RawMessage(`{"command":"line1\nline2\nline3\nline4\nline5"}`)
186 v.ToolCalls = []ToolCallView{
187 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall)},
188 }
189 tallRows := len(v.Build(80))
190
191 // Same id, now finalised with a tiny error result. The box must
192 // collapse to the natural result height instead of staying padded.
193 v.ToolCalls = []ToolCallView{
194 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", tall), RawJSONBuf: string(tall), Done: true, Error: true, Result: "boom"},
195 }
196 if got := len(v.Build(80)); got >= tallRows {
197 t.Fatalf("finalised box kept live reservation: %d rows (live was %d)", got, tallRows)
198 }
199}
200
201func TestLiveToolOverlayKeepsWritePreviewAfterArgsEnd(t *testing.T) {
202 args := json.RawMessage(`{"path":"/tmp/sample.ts","content":"export const n = 1\n"}`)
203 v := View{
204 Theme: Dark,
205 Messages: []provider.Message{
206 {
207 Role: provider.RoleAssistant,
208 Content: []provider.Content{
209 provider.ToolCallBlock{ID: "toolu_1", Name: "write", Arguments: args},
210 },
211 },
212 },
213 ToolCalls: []ToolCallView{
214 {
215 ID: "toolu_1",
216 Name: "write",
217 Args: ShortArgs("write", args),
218 Streaming: false,
219 RawJSONBuf: string(args),
220 LivePath: "/tmp/sample.ts",
221 },
222 },
223 }
224
225 plain := stripANSI(strings.Join(v.Build(80), "\n"))
226 if !strings.Contains(plain, "export const n = 1") {
227 t.Fatalf("write preview collapsed after tool args ended but before tool_result arrived:\n%s", plain)
228 }
229}
230
231func TestLiveToolOverlayHidesAfterToolResult(t *testing.T) {
232 args := json.RawMessage(`{"command":"sleep 1"}`)
233 v := View{
234 Theme: Dark,
235 Messages: []provider.Message{
236 {
237 Role: provider.RoleAssistant,
238 Content: []provider.Content{
239 provider.ToolCallBlock{ID: "toolu_1", Name: "bash", Arguments: args},
240 },
241 },
242 {
243 Role: provider.RoleTool,
244 Content: []provider.Content{
245 provider.ToolResultBlock{
246 CallID: "toolu_1",
247 Content: []provider.Content{provider.TextBlock{Text: "done"}},
248 },
249 },
250 },
251 },
252 ToolCalls: []ToolCallView{
253 {ID: "toolu_1", Name: "bash", Args: ShortArgs("bash", args), Result: "done", Done: true},
254 },
255 }
256
257 plain := stripANSI(strings.Join(v.BuildLive(80), "\n"))
258 if strings.Contains(plain, "bash sleep 1") {
259 t.Fatalf("live tool overlay still rendered after tool_result was appended:\n%s", plain)
260 }
261}