1
use super::*;
2

            
3
27
fn make_opts(n: usize) -> Vec<SelectOption> {
4
27
    (0..n)
5
        .map(|i| SelectOption {
6
93
            id: format!("id-{i}"),
7
93
            label: format!("Label {i}"),
8
93
        })
9
        .collect()
10
27
}
11

            
12
#[test]
13
1
fn empty_widget_returns_empty_strings() {
14
1
    let w = SelectWidget::new();
15
1
    assert_eq!(w.value(), "");
16
1
    assert_eq!(w.display(), "");
17
1
}
18

            
19
#[test]
20
1
fn zero_match_confirm_is_noop_and_keeps_query() {
21
1
    let mut w = SelectWidget::new();
22
1
    w.set_options(make_opts(3));
23
1
    for c in "zzz".chars() {
24
3
        w.filter_push(c);
25
    }
26
1
    assert!(w.matches().is_empty(), "query matches nothing");
27
1
    w.confirm();
28
1
    assert_eq!(
29
1
        w.value(),
30
1
        "",
31
        "confirm with no match must not silently select option 0"
32
    );
33
1
    assert_eq!(
34
1
        w.query(),
35
1
        "zzz",
36
        "query kept so the user can fix the filter"
37
    );
38
1
}
39

            
40
#[test]
41
1
fn value_returns_focused_id() {
42
1
    let mut w = SelectWidget::new();
43
1
    w.set_options(make_opts(3));
44
1
    assert_eq!(w.value(), "id-0");
45
1
    w.next();
46
1
    assert_eq!(w.value(), "id-1");
47
1
}
48

            
49
#[test]
50
1
fn display_returns_focused_label() {
51
1
    let mut w = SelectWidget::new();
52
1
    w.set_options(make_opts(3));
53
1
    assert_eq!(w.display(), "Label 0");
54
1
    w.next();
55
1
    assert_eq!(w.display(), "Label 1");
56
1
}
57

            
58
#[test]
59
1
fn next_wraps_around() {
60
1
    let mut w = SelectWidget::new();
61
1
    w.set_options(make_opts(3));
62
1
    w.next();
63
1
    w.next(); // 0 → 1 → 2
64
1
    assert_eq!(w.value(), "id-2");
65
1
    w.next(); // wraps to 0
66
1
    assert_eq!(w.value(), "id-0");
67
1
}
68

            
69
#[test]
70
1
fn prev_wraps_around() {
71
1
    let mut w = SelectWidget::new();
72
1
    w.set_options(make_opts(3));
73
1
    w.prev(); // 0 wraps to 2
74
1
    assert_eq!(w.value(), "id-2");
75
1
    w.prev(); // 2 → 1
76
1
    assert_eq!(w.value(), "id-1");
77
1
}
78

            
79
#[test]
80
1
fn next_and_prev_noop_on_empty() {
81
1
    let mut w = SelectWidget::new();
82
1
    w.next();
83
1
    w.prev();
84
1
    assert_eq!(w.value(), "");
85
1
}
86

            
87
#[test]
88
1
fn an_id_chosen_before_the_options_exist_is_selected_when_they_arrive() {
89
1
    let mut w = SelectWidget::new();
90
1
    w.select_id("id-2");
91
1
    w.set_options(make_opts(3));
92
1
    assert_eq!(w.value(), "id-2");
93
1
    w.set_options(make_opts(3));
94
1
    assert_eq!(w.value(), "id-0", "the remembered id is used once");
95
1
}
96

            
97
#[test]
98
1
fn value_keeps_a_pending_id_before_the_options_arrive() {
99
1
    let mut w = SelectWidget::new();
100
1
    w.select_id("id-2");
101
1
    assert_eq!(w.value(), "id-2");
102
1
    w.set_options(make_opts(3));
103
1
    assert_eq!(w.value(), "id-2");
104
1
}
105

            
106
#[test]
107
1
fn set_options_resets_focus() {
108
1
    let mut w = SelectWidget::new();
109
1
    w.set_options(make_opts(3));
110
1
    w.next();
111
1
    w.next();
112
1
    w.set_options(make_opts(2));
113
1
    assert_eq!(w.value(), "id-0");
114
1
}
115

            
116
#[test]
117
1
fn set_options_preserving_id_keeps_selected_uuid() {
118
1
    let mut w = SelectWidget::new();
119
1
    w.set_options(vec![SelectOption {
120
1
        id: "uuid-42".to_string(),
121
1
        label: "uuid-42".to_string(),
122
1
    }]);
123
1
    assert_eq!(w.value(), "uuid-42");
124
1
    let full_list = vec![
125
1
        SelectOption {
126
1
            id: "uuid-10".to_string(),
127
1
            label: "First".to_string(),
128
1
        },
129
1
        SelectOption {
130
1
            id: "uuid-42".to_string(),
131
1
            label: "Second (proper label)".to_string(),
132
1
        },
133
1
        SelectOption {
134
1
            id: "uuid-99".to_string(),
135
1
            label: "Third".to_string(),
136
1
        },
137
    ];
138
1
    w.set_options_preserving_id(full_list);
139
1
    assert_eq!(
140
1
        w.value(),
141
1
        "uuid-42",
142
        "uuid preserved after set_options_preserving_id"
143
    );
144
1
    assert_eq!(
145
1
        w.display(),
146
1
        "Second (proper label)",
147
        "label updated to full label"
148
    );
149
1
}
150

            
151
#[test]
152
1
fn set_options_preserving_id_falls_back_to_zero_when_not_found() {
153
1
    let mut w = SelectWidget::new();
154
1
    w.set_options(vec![SelectOption {
155
1
        id: "old".to_string(),
156
1
        label: "Old".to_string(),
157
1
    }]);
158
1
    let new_list = vec![
159
1
        SelectOption {
160
1
            id: "new-a".to_string(),
161
1
            label: "A".to_string(),
162
1
        },
163
1
        SelectOption {
164
1
            id: "new-b".to_string(),
165
1
            label: "B".to_string(),
166
1
        },
167
    ];
168
1
    w.set_options_preserving_id(new_list);
169
1
    assert_eq!(w.value(), "new-a");
170
1
}
171

            
172
#[test]
173
1
fn confirm_closes_open_dropdown() {
174
1
    let mut w = SelectWidget::new();
175
1
    w.set_options(make_opts(2));
176
1
    w.open = true;
177
1
    w.confirm();
178
1
    assert!(!w.open);
179
1
}
180

            
181
#[test]
182
1
fn filter_push_narrows_matches() {
183
1
    let mut w = SelectWidget::new();
184
1
    w.set_options(vec![
185
1
        SelectOption {
186
1
            id: "a".to_string(),
187
1
            label: "Apple".to_string(),
188
1
        },
189
1
        SelectOption {
190
1
            id: "b".to_string(),
191
1
            label: "Grape".to_string(),
192
1
        },
193
1
        SelectOption {
194
1
            id: "c".to_string(),
195
1
            label: "Apricot".to_string(),
196
1
        },
197
1
    ]);
198
    // 'p' matches "Apple" (has 'p') and "Apricot" (has 'p'), not "Grape" (has 'p' too!)
199
    // Use 'i' instead: "Apricot" has 'i', "Apple" has no 'i', "Grape" has no 'i'
200
1
    w.filter_push('i');
201
1
    let m = w.matches();
202
1
    assert_eq!(m.len(), 1, "only Apricot matches 'i'");
203
1
    assert_eq!(w.value(), "c", "first (only) match is Apricot");
204
1
}
205

            
206
#[test]
207
1
fn filter_next_wraps_within_filtered_set() {
208
1
    let mut w = SelectWidget::new();
209
1
    w.set_options(vec![
210
1
        SelectOption {
211
1
            id: "id-0".to_string(),
212
1
            label: "Sword".to_string(),
213
1
        },
214
1
        SelectOption {
215
1
            id: "id-1".to_string(),
216
1
            label: "Tuna".to_string(),
217
1
        },
218
1
        SelectOption {
219
1
            id: "id-2".to_string(),
220
1
            label: "Squid".to_string(),
221
1
        },
222
1
    ]);
223
    // 's' matches "Sword" (idx 0) and "Squid" (idx 2), not "Tuna"
224
1
    w.filter_push('s');
225
1
    assert_eq!(w.value(), "id-0", "first match is Sword");
226
1
    w.next();
227
1
    assert_eq!(w.value(), "id-2", "second match is Squid");
228
1
    w.next();
229
1
    assert_eq!(w.value(), "id-0", "wraps back to first match");
230
1
}
231

            
232
#[test]
233
1
fn filter_pop_widens_matches() {
234
1
    let mut w = SelectWidget::new();
235
1
    w.set_options(make_opts(3));
236
1
    w.filter_push('1'); // only "Label 1" matches
237
1
    assert_eq!(w.matches().len(), 1);
238
1
    w.filter_pop();
239
1
    assert_eq!(w.matches().len(), 3, "empty query matches all");
240
1
}
241

            
242
#[test]
243
1
fn set_options_clears_query() {
244
1
    let mut w = SelectWidget::new();
245
1
    w.set_options(make_opts(3));
246
1
    w.filter_push('x');
247
1
    assert_eq!(w.query(), "x");
248
1
    w.set_options(make_opts(2));
249
1
    assert_eq!(w.query(), "", "set_options must clear query");
250
1
}
251

            
252
#[test]
253
1
fn set_options_preserving_id_clears_query() {
254
1
    let mut w = SelectWidget::new();
255
1
    w.set_options(make_opts(3));
256
1
    w.filter_push('x');
257
1
    assert_eq!(w.query(), "x");
258
1
    w.set_options_preserving_id(make_opts(2));
259
1
    assert_eq!(w.query(), "", "set_options_preserving_id must clear query");
260
1
}
261

            
262
#[test]
263
1
fn zero_match_value_and_display_empty() {
264
1
    let mut w = SelectWidget::new();
265
1
    w.set_options(make_opts(3));
266
1
    w.filter_push('z'); // no "Label N" contains 'z'
267
1
    assert_eq!(w.matches().len(), 0);
268
1
    assert_eq!(w.value(), "", "zero match → value is empty string");
269
1
    assert_eq!(w.display(), "", "zero match → display is empty string");
270
1
}
271

            
272
#[test]
273
1
fn zero_match_next_and_prev_no_panic() {
274
1
    let mut w = SelectWidget::new();
275
1
    w.set_options(make_opts(3));
276
1
    w.filter_push('z'); // zero matches
277
1
    w.next(); // must not panic or index out of bounds
278
1
    w.prev(); // must not panic or index out of bounds
279
1
    assert_eq!(w.value(), "");
280
1
    assert_eq!(w.display(), "");
281
1
}
282

            
283
#[test]
284
1
fn confirm_resolves_filtered_selection_to_raw_index() {
285
1
    let mut w = SelectWidget::new();
286
1
    w.set_options(vec![
287
1
        SelectOption {
288
1
            id: "id-x".to_string(),
289
1
            label: "Xenon".to_string(),
290
1
        },
291
1
        SelectOption {
292
1
            id: "id-a".to_string(),
293
1
            label: "Apple".to_string(),
294
1
        },
295
1
        SelectOption {
296
1
            id: "id-b".to_string(),
297
1
            label: "Banana".to_string(),
298
1
        },
299
1
    ]);
300
    // 'a' matches "Apple" (idx 1) and "Banana" (idx 2); focused=0 → "Apple"
301
1
    w.filter_push('a');
302
1
    assert_eq!(w.value(), "id-a");
303
1
    w.confirm();
304
1
    assert!(!w.open);
305
1
    assert_eq!(w.query(), "");
306
    // After confirm, focused resolved to raw index of "Apple" (idx 1)
307
    // With empty query, matches=[0,1,2], focused=1 → options[1] = "Apple"
308
1
    assert_eq!(
309
1
        w.value(),
310
1
        "id-a",
311
        "selection preserved after confirm+query clear"
312
    );
313
1
}
314

            
315
#[test]
316
1
fn a_step_before_the_options_keeps_the_pending_value() {
317
1
    let mut w = SelectWidget::new();
318
1
    w.select_id("id-2");
319
1
    w.next();
320
1
    w.prev();
321
1
    w.set_options_preserving_id(make_opts(3));
322
1
    assert_eq!(w.value(), "id-2");
323
1
}
324

            
325
#[test]
326
1
fn a_step_over_options_drops_the_pending_value() {
327
1
    let mut w = SelectWidget::new();
328
1
    w.set_options_preserving_id(make_opts(1));
329
1
    w.select_id("id-2");
330
1
    w.next();
331
1
    w.set_options_preserving_id(make_opts(3));
332
1
    assert_eq!(w.value(), "id-0");
333
1
}
334

            
335
#[test]
336
1
fn backspacing_a_query_to_empty_keeps_the_chosen_value() {
337
1
    let mut w = SelectWidget::new();
338
1
    w.set_options(make_opts(3));
339
1
    w.select_id("id-2");
340
1
    w.filter_push('1');
341
1
    assert_eq!(w.value(), "id-1");
342
1
    w.filter_pop();
343
1
    assert_eq!(w.value(), "id-2");
344
1
    assert_eq!(w.query(), "");
345
1
}
346

            
347
#[test]
348
1
fn cancel_goes_back_to_the_option_the_list_opened_on_and_drops_the_query() {
349
1
    let mut w = SelectWidget::new();
350
1
    w.set_options(make_opts(3));
351
1
    w.select_id("id-2");
352
1
    w.open_list();
353
1
    w.filter_push('0');
354
1
    assert_eq!(w.value(), "id-0");
355
1
    w.cancel();
356
1
    assert_eq!((w.value(), w.query(), w.open), ("id-2", "", false));
357
1
}
358

            
359
#[test]
360
1
fn a_longer_query_popped_once_keeps_filtering() {
361
1
    let mut w = SelectWidget::new();
362
1
    w.set_options(make_opts(12));
363
1
    w.select_id("id-5");
364
1
    w.filter_push('1');
365
1
    w.filter_push('1');
366
1
    w.filter_pop();
367
1
    assert_eq!(w.query(), "1");
368
1
    assert_eq!(w.value(), "id-1");
369
1
    w.filter_pop();
370
1
    assert_eq!(w.value(), "id-5");
371
1
}
372

            
373
#[test]
374
1
fn pick_closes_the_list_on_the_filtered_option() {
375
1
    let mut w = SelectWidget::new();
376
1
    w.set_options(make_opts(5));
377
1
    w.open_list();
378
1
    w.filter_push('4');
379
1
    w.pick(0);
380
1
    assert!(!w.open);
381
1
    assert_eq!(w.value(), "id-4");
382
1
}
383

            
384
#[test]
385
1
fn pick_past_the_options_changes_nothing() {
386
1
    let mut w = SelectWidget::new();
387
1
    w.set_options(make_opts(3));
388
1
    w.open_list();
389
1
    w.pick(3);
390
1
    assert!(w.open);
391
1
    assert_eq!(w.value(), "id-0");
392
1
}
393

            
394
#[test]
395
1
fn page_moves_a_window_of_rows_and_stops_at_the_ends() {
396
1
    let mut w = SelectWidget::new();
397
1
    w.set_options(make_opts(10));
398
1
    w.open_list();
399
1
    let _ = w.window().fit(10, 4);
400
1
    w.page(true);
401
1
    assert_eq!(w.value(), "id-4");
402
1
    w.page(true);
403
1
    w.page(true);
404
1
    assert_eq!(w.value(), "id-9");
405
1
    w.page(false);
406
1
    assert_eq!(w.value(), "id-5");
407
3
    (0..3).for_each(|_| w.page(false));
408
1
    assert_eq!(w.value(), "id-0");
409
1
}