1package component
2
3import "testing"
4
5func TestGhostCompletion(t *testing.T) {
6 history := []Suggestion{
7 {URL: "https://www.google.com/url?q=https://x"},
8 {URL: "https://github.com/bnema/dumber"},
9 }
10 favorites := []Favorite{{URL: "https://gitlab.com/"}}
11
12 tests := []struct {
13 name string
14 req ghostCompletionRequest
15 wantFull string
16 wantSuffix string
17 wantOK bool
18 }{
19 {
20 name: "host-like input completes to the domain, not the redirect URL",
21 req: ghostCompletionRequest{EntryText: "goo", Mode: ViewModeHistory, MaxResults: 5, Suggestions: history},
22 wantFull: "google.com",
23 wantSuffix: "gle.com",
24 wantOK: true,
25 },
26 {
27 name: "leading whitespace is ignored",
28 req: ghostCompletionRequest{EntryText: " gith", Mode: ViewModeHistory, MaxResults: 5, Suggestions: history},
29 wantFull: "github.com",
30 wantSuffix: "ub.com",
31 wantOK: true,
32 },
33 {
34 name: "hidden rows never complete",
35 req: ghostCompletionRequest{EntryText: "gith", Mode: ViewModeHistory, MaxResults: 1, Suggestions: history},
36 wantOK: false,
37 },
38 {
39 name: "favorites mode uses favorites",
40 req: ghostCompletionRequest{EntryText: "gitl", Mode: ViewModeFavorites, MaxResults: 5, Favorites: favorites},
41 wantFull: "gitlab.com",
42 wantSuffix: "ab.com",
43 wantOK: true,
44 },
45 {
46 name: "explicit selection completes from the selected URL, not the best match",
47 req: ghostCompletionRequest{
48 EntryText: "gi", SelectedURL: "https://gitlab.com/", HasExplicitSelection: true,
49 Mode: ViewModeHistory, MaxResults: 5, Suggestions: history,
50 },
51 wantFull: "gitlab.com",
52 wantSuffix: "tlab.com",
53 wantOK: true,
54 },
55 {
56 name: "blank input shows nothing",
57 req: ghostCompletionRequest{EntryText: " ", Mode: ViewModeHistory, MaxResults: 5, Suggestions: history},
58 wantOK: false,
59 },
60 }
61
62 for _, tt := range tests {
63 t.Run(tt.name, func(t *testing.T) {
64 full, suffix, ok := ghostCompletion(tt.req)
65 if ok != tt.wantOK || full != tt.wantFull || suffix != tt.wantSuffix {
66 t.Fatalf("ghostCompletion() = (%q, %q, %v), want (%q, %q, %v)",
67 full, suffix, ok, tt.wantFull, tt.wantSuffix, tt.wantOK)
68 }
69 })
70 }
71}
72
73func TestNavigableCount(t *testing.T) {
74 limit := OmniboxListDefaults.MaxResults
75 tests := []struct {
76 name string
77 bangMode bool
78 mode ViewMode
79 bangs, suggestions, favorites int
80 want int
81 }{
82 {"history capped at results limit", false, ViewModeHistory, 0, limit + 5, 30, limit},
83 {"favorites capped at results limit", false, ViewModeFavorites, 0, 3, 30, limit},
84 {"bangs win over view mode", true, ViewModeHistory, 2, 8, 0, 2},
85 {"empty list", false, ViewModeFavorites, 4, 4, 0, 0},
86 }
87 for _, tt := range tests {
88 t.Run(tt.name, func(t *testing.T) {
89 got := navigableCount(tt.bangMode, tt.mode, tt.bangs, tt.suggestions, tt.favorites)
90 if got != tt.want {
91 t.Fatalf("navigableCount() = %d, want %d", got, tt.want)
92 }
93 })
94 }
95}