1package modes
2
3import (
4 "context"
5 "encoding/json"
6 "strings"
7 "testing"
8
9 "github.com/patriceckhart/zot/packages/core"
10 "github.com/patriceckhart/zot/packages/tui"
11)
12
13func TestConfirmDialogAllowsToolExpansion(t *testing.T) {
14 resp := make(chan core.ConfirmDecision, 1)
15 dialog := newConfirmDialog()
16 dialog.Enqueue(&confirmRequest{
17 toolName: "edit",
18 preview: "large edit",
19 resp: resp,
20 })
21 args, err := json.Marshal(map[string]any{
22 "path": "sample.ts",
23 "edits": []map[string]string{{
24 "oldText": "old",
25 "newText": strings.Repeat("const value = 1\n", 20),
26 }},
27 })
28 if err != nil {
29 t.Fatal(err)
30 }
31 view := &tui.View{
32 ToolCalls: []tui.ToolCallView{{
33 ID: "call-1",
34 Name: "edit",
35 RawJSONBuf: string(args),
36 LivePath: "sample.ts",
37 }},
38 }
39 i := &Interactive{
40 view: view,
41 confirmDialog: dialog,
42 dirty: make(chan struct{}, 1),
43 }
44 if rendered := strings.Join(view.Build(80), "\n"); !strings.Contains(rendered, "ctrl+o to expand") {
45 t.Fatalf("test preview was not initially collapsed:\n%s", rendered)
46 }
47
48 i.handleKey(context.Background(), tui.Key{Kind: tui.KeyCtrlO})
49
50 if !i.view.ExpandAll {
51 t.Fatal("ctrl+o did not expand tool previews while confirmation was active")
52 }
53 if rendered := strings.Join(view.Build(80), "\n"); strings.Contains(rendered, "ctrl+o to expand") {
54 t.Fatalf("live edit preview remained collapsed after ctrl+o:\n%s", rendered)
55 }
56 if !dialog.Active() {
57 t.Fatal("ctrl+o closed the confirmation dialog")
58 }
59 select {
60 case decision := <-resp:
61 t.Fatalf("ctrl+o unexpectedly answered confirmation: %+v", decision)
62 default:
63 }
64
65 i.handleKey(context.Background(), tui.Key{Kind: tui.KeyCtrlO})
66 if i.view.ExpandAll {
67 t.Fatal("second ctrl+o did not collapse tool previews during confirmation")
68 }
69 if rendered := strings.Join(view.Build(80), "\n"); !strings.Contains(rendered, "ctrl+o to expand") {
70 t.Fatalf("live edit preview remained expanded after second ctrl+o:\n%s", rendered)
71 }
72 if !dialog.Active() {
73 t.Fatal("second ctrl+o closed the confirmation dialog")
74 }
75}