1package modes
2
3import (
4 "testing"
5
6 "github.com/patriceckhart/zot/packages/core"
7 "github.com/patriceckhart/zot/packages/provider"
8 "github.com/patriceckhart/zot/packages/tui"
9)
10
11func TestInputHistoryUsesUpDown(t *testing.T) {
12 ag := core.NewAgent(nil, "", "", nil)
13 ag.SetMessages([]provider.Message{
14 {Role: provider.RoleUser, Content: []provider.Content{provider.TextBlock{Text: "first"}}},
15 {Role: provider.RoleAssistant, Content: []provider.Content{provider.TextBlock{Text: "reply"}}},
16 {Role: provider.RoleUser, Content: []provider.Content{provider.TextBlock{Text: "second"}}},
17 })
18 i := &Interactive{
19 agent: ag,
20 ed: tui.NewEditor(""),
21 inputHistoryIndex: -1,
22 }
23
24 if !i.handleInputHistoryKey(tui.Key{Kind: tui.KeyUp}) {
25 t.Fatal("expected Up to enter history")
26 }
27 if got := i.ed.Value(); got != "second" {
28 t.Fatalf("Up loaded %q, want newest history item", got)
29 }
30
31 if !i.handleInputHistoryKey(tui.Key{Kind: tui.KeyUp}) {
32 t.Fatal("expected repeated Up to stay in history")
33 }
34 if got := i.ed.Value(); got != "first" {
35 t.Fatalf("second Up loaded %q, want older history item", got)
36 }
37
38 if !i.handleInputHistoryKey(tui.Key{Kind: tui.KeyDown}) {
39 t.Fatal("expected Down to move forward in history")
40 }
41 if got := i.ed.Value(); got != "second" {
42 t.Fatalf("Down loaded %q, want newer history item", got)
43 }
44
45 if !i.handleInputHistoryKey(tui.Key{Kind: tui.KeyDown}) {
46 t.Fatal("expected Down at newest item to clear editor")
47 }
48 if got := i.ed.Value(); got != "" {
49 t.Fatalf("Down past newest loaded %q, want empty editor", got)
50 }
51}
52
53func TestInputHistoryNoLongerUsesLeftRight(t *testing.T) {
54 ag := core.NewAgent(nil, "", "", nil)
55 ag.SetMessages([]provider.Message{
56 {Role: provider.RoleUser, Content: []provider.Content{provider.TextBlock{Text: "prompt"}}},
57 })
58 i := &Interactive{
59 agent: ag,
60 ed: tui.NewEditor(""),
61 inputHistoryIndex: -1,
62 }
63
64 if i.handleInputHistoryKey(tui.Key{Kind: tui.KeyLeft}) {
65 t.Fatal("Left should not browse input history")
66 }
67 if i.handleInputHistoryKey(tui.Key{Kind: tui.KeyRight}) {
68 t.Fatal("Right should not browse input history")
69 }
70 if got := i.ed.Value(); got != "" {
71 t.Fatalf("left/right changed editor to %q", got)
72 }
73}
74
75func TestInputHistoryDoesNotStealNonEmptyEditor(t *testing.T) {
76 ag := core.NewAgent(nil, "", "", nil)
77 ag.SetMessages([]provider.Message{
78 {Role: provider.RoleUser, Content: []provider.Content{provider.TextBlock{Text: "prompt"}}},
79 })
80 i := &Interactive{
81 agent: ag,
82 ed: tui.NewEditor(""),
83 inputHistoryIndex: -1,
84 }
85 i.ed.SetValue("draft")
86
87 if i.handleInputHistoryKey(tui.Key{Kind: tui.KeyUp}) {
88 t.Fatal("Up should not browse history while editing a draft")
89 }
90 if got := i.ed.Value(); got != "draft" {
91 t.Fatalf("editor changed to %q, want draft", got)
92 }
93}