1package component
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7)
8
9func TestResolveFavoriteToggleTarget(t *testing.T) {
10 suggestions := []Suggestion{
11 {URL: "https://example.com", Title: "Example"},
12 {URL: "https://favorite.example.com", Title: "Favorite", IsFavorite: true},
13 }
14 favorites := []Favorite{
15 {ID: 42, URL: "https://saved.example.com", Title: "Saved"},
16 {ID: 0, URL: "https://invalid.example.com", Title: "Invalid"},
17 }
18
19 tests := []struct {
20 name string
21 mode ViewMode
22 index int
23 suggestions []Suggestion
24 favorites []Favorite
25 wantTarget favoriteToggleTarget
26 wantValidTarget bool
27 }{
28 {
29 name: "history add target uses selected suggestion URL and title",
30 mode: ViewModeHistory,
31 index: 0,
32 suggestions: suggestions,
33 favorites: favorites,
34 wantTarget: favoriteToggleTarget{
35 url: "https://example.com",
36 title: "Example",
37 },
38 wantValidTarget: true,
39 },
40 {
41 name: "history remove target preserves prior favorite state",
42 mode: ViewModeHistory,
43 index: 1,
44 suggestions: suggestions,
45 favorites: favorites,
46 wantTarget: favoriteToggleTarget{
47 url: "https://favorite.example.com",
48 title: "Favorite",
49 wasFavorite: true,
50 },
51 wantValidTarget: true,
52 },
53 {
54 name: "history invalid index has no target",
55 mode: ViewModeHistory,
56 index: 2,
57 suggestions: suggestions,
58 favorites: favorites,
59 wantValidTarget: false,
60 },
61 {
62 name: "history empty URL has no target",
63 mode: ViewModeHistory,
64 index: 0,
65 suggestions: []Suggestion{{Title: "Missing URL"}},
66 favorites: favorites,
67 wantValidTarget: false,
68 },
69 {
70 name: "favorites remove target uses favorite ID",
71 mode: ViewModeFavorites,
72 index: 0,
73 suggestions: suggestions,
74 favorites: favorites,
75 wantTarget: favoriteToggleTarget{
76 url: "https://saved.example.com",
77 title: "Saved",
78 favoriteID: 42,
79 },
80 wantValidTarget: true,
81 },
82 {
83 name: "favorites invalid index has no target",
84 mode: ViewModeFavorites,
85 index: -1,
86 suggestions: suggestions,
87 favorites: favorites,
88 wantValidTarget: false,
89 },
90 {
91 name: "favorites zero ID has no target",
92 mode: ViewModeFavorites,
93 index: 1,
94 suggestions: suggestions,
95 favorites: favorites,
96 wantValidTarget: false,
97 },
98 }
99
100 for _, tt := range tests {
101 t.Run(tt.name, func(t *testing.T) {
102 gotTarget, gotValidTarget := resolveFavoriteToggleTarget(tt.mode, tt.index, tt.suggestions, tt.favorites)
103 assert.Equal(t, tt.wantValidTarget, gotValidTarget)
104 assert.Equal(t, tt.wantTarget, gotTarget)
105 })
106 }
107}
108
109func TestResolveFavoriteToggleResultUpdate(t *testing.T) {
110 suggestions := []Suggestion{{URL: "https://example.com"}}
111
112 tests := []struct {
113 name string
114 index int
115 expectedURL string
116 added bool
117 want favoriteToggleResultUpdate
118 }{
119 {
120 name: "matching URL updates selected suggestion favorite state",
121 index: 0,
122 expectedURL: "https://example.com",
123 added: true,
124 want: favoriteToggleResultUpdate{Apply: true, Index: 0, IsFavorite: true},
125 },
126 {
127 name: "stale URL skips update",
128 index: 0,
129 expectedURL: "https://stale.example.com",
130 added: true,
131 want: favoriteToggleResultUpdate{},
132 },
133 {
134 name: "out of range index skips update",
135 index: 1,
136 expectedURL: "https://example.com",
137 added: true,
138 want: favoriteToggleResultUpdate{},
139 },
140 }
141
142 for _, tt := range tests {
143 t.Run(tt.name, func(t *testing.T) {
144 got := resolveFavoriteToggleResultUpdate(suggestions, tt.index, tt.expectedURL, tt.added)
145 assert.Equal(t, tt.want, got)
146 })
147 }
148}
149
150func TestFavoriteToggleErrorMessage(t *testing.T) {
151 assert.Equal(t, "Failed to add favorite", favoriteToggleErrorMessage(false))
152 assert.Equal(t, "Failed to remove favorite", favoriteToggleErrorMessage(true))
153}