login_dialog_test.go RawHistoryFolder
1package modes
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/bnema/zut/packages/tui"
8)
9
10func TestLoginDialogProviderPickerFiltersLikeModelPicker(t *testing.T) {
11 d := newLoginDialog()
12 d.step = loginStepProvider
13 d.method = "apikey"
14 d.status = map[string]string{}
15 for _, r := range "llamacpp" {
16 d.HandleKey(tui.Key{Kind: tui.KeyRune, Rune: r})
17 }
18 options := d.providerOptions()
19 if len(options) != 1 || options[0] != "llama.cpp" {
20 t.Fatalf("options = %v", options)
21 }
22 d.HandleKey(tui.Key{Kind: tui.KeyEnter})
23 if d.step != loginStepLlamaURL {
24 t.Fatalf("step = %v", d.step)
25 }
26}
27
28func TestLoginDialogProviderPickerShowsNoMatches(t *testing.T) {
29 d := newLoginDialog()
30 d.step = loginStepProvider
31 d.method = "apikey"
32 d.status = map[string]string{}
33 d.providerQuery = "not-a-provider"
34 if options := d.providerOptions(); len(options) != 0 {
35 t.Fatalf("options = %v", options)
36 }
37 d.HandleKey(tui.Key{Kind: tui.KeyPageDown})
38 if d.cursor != 0 {
39 t.Fatalf("cursor = %d", d.cursor)
40 }
41 if action := d.HandleKey(tui.Key{Kind: tui.KeyEnter}); action != (loginDialogAction{}) {
42 t.Fatalf("action = %+v", action)
43 }
44}
45
46func TestLoginDialogLlamaCPPValidatesURLAndAcceptsOptionalKey(t *testing.T) {
47 d := newLoginDialog()
48 d.step = loginStepLlamaURL
49 d.provider = "llama.cpp"
50 d.method = "apikey"
51 d.llamaEd = tui.NewEditor("")
52
53 d.llamaEd.SetValue("file:///tmp/router")
54 if action := d.HandleKey(tui.Key{Kind: tui.KeyEnter}); action.SaveLlama || d.step != loginStepLlamaURL {
55 t.Fatalf("invalid URL advanced dialog: action=%+v step=%v", action, d.step)
56 }
57 if !strings.Contains(d.message, "http or https") {
58 t.Fatalf("validation message = %q", d.message)
59 }
60
61 d.llamaEd.SetValue("http://127.0.0.1:8080/v1/")
62 d.HandleKey(tui.Key{Kind: tui.KeyEnter})
63 if d.step != loginStepLlamaKey || d.llamaURL != "http://127.0.0.1:8080" {
64 t.Fatalf("step=%v URL=%q", d.step, d.llamaURL)
65 }
66 action := d.HandleKey(tui.Key{Kind: tui.KeyEnter})
67 if !action.SaveLlama || action.LlamaURL != "http://127.0.0.1:8080" || action.LlamaAPIKey != "" {
68 t.Fatalf("action = %+v", action)
69 }
70}
71
72func TestLoginDialogCursorPosMatchesPaddedInputRow(t *testing.T) {
73 d := newLoginDialog()
74 d.Open(t.TempDir())
75 d.method = "oauth"
76 d.provider = "anthropic"
77 d.ShowWaiting("https://example.com/oauth/authorize?code_challenge=abc&state=xyz")
78
79 lines := padDialogFrame(d.Render(tui.Theme{}, 80))
80 row, _ := d.CursorPos(80)
81 if row < 0 || row >= len(lines) {
82 t.Fatalf("CursorPos row = %d outside rendered lines %d", row, len(lines))
83 }
84 if got := stripANSIBytes(lines[row]); !strings.Contains(got, "▌") {
85 t.Fatalf("CursorPos row %d = %q; want editor input row", row, got)
86 }
87}