agent-p 0.2.0

A fast podcast downloader with customizable ID3 tagging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Core application state, screen enum, and episode/download logic.

use std::time::Instant;

use crossterm::event::KeyCode;
use ratatui::widgets::ListState;

use crate::podcast::{Config, DownloadEvent, EpisodeInfo, Podcast};

use super::add_podcast_wizard::AddPodcastState;
use super::commands::{COMMANDS, CommandPaletteState, palette_command_visible};

/// Available screens in the application.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Screen {
    PodcastList,
    EpisodeSelect,
    Downloading,
    Config,
    EditPodcast,
    EditPodcastSelect,
    AddPodcast,
    About,
}

/// Episode list sort direction.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SortOrder {
    Desc,
    Asc,
}

impl SortOrder {
    /// Return the opposite sort order.
    pub fn toggle(&self) -> Self {
        match self {
            SortOrder::Desc => SortOrder::Asc,
            SortOrder::Asc => SortOrder::Desc,
        }
    }

    /// Human-readable label for the current sort order.
    pub fn label(&self) -> &'static str {
        match self {
            SortOrder::Desc => "Newest first",
            SortOrder::Asc => "Oldest first",
        }
    }
}

/// Tracks progress of an in-flight download batch.
#[derive(Debug, Clone)]
pub struct DownloadProgress {
    pub current_episode: String,
    pub current_index: usize,
    pub total: usize,
    pub completed: Vec<String>,
    pub tagged: Vec<String>,
    pub finished: bool,
    pub errors: Vec<String>,
}

impl DownloadProgress {
    /// Create an empty download progress tracker.
    pub fn new() -> Self {
        Self {
            current_episode: String::new(),
            current_index: 0,
            total: 0,
            completed: Vec::new(),
            tagged: Vec::new(),
            finished: false,
            errors: Vec::new(),
        }
    }

    /// True until the batch reports `Finished`.
    ///
    /// Per-episode errors do not end a batch: `download_selected_episodes`
    /// records the failure and carries on with the remaining selections, so
    /// only `Finished` marks the run complete.
    pub fn is_active(&self) -> bool {
        !self.finished
    }
}

/// Editing mode for config and podcast editor screens.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConfigEditMode {
    Navigate,
    EditingText,
}

/// Which confirmation dialog is open on the config screen.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConfigDialog {
    Closed,
    ConfirmDirChange,
    ConfirmDirDefault,
    ConfirmCreateDir,
}

/// Number of rows Page Up and Page Down travel in a list.
pub const LIST_PAGE_STEP: usize = 10;

/// Apply a jump or page key to a list selection.
///
/// Handles `Home`/`g`, `End`/`G`, `PageUp` and `PageDown`, clamping the result
/// to `floor..len`. Returns true when the key was consumed.
pub fn apply_list_jump(state: &mut ListState, len: usize, floor: usize, code: KeyCode) -> bool {
    if len == 0 || floor >= len {
        return false;
    }
    let last = len - 1;
    let current = state.selected().unwrap_or(floor).clamp(floor, last);
    let target = match code {
        KeyCode::Home | KeyCode::Char('g') => floor,
        KeyCode::End | KeyCode::Char('G') => last,
        KeyCode::PageUp => current.saturating_sub(LIST_PAGE_STEP).max(floor),
        KeyCode::PageDown => current.saturating_add(LIST_PAGE_STEP).min(last),
        _ => return false,
    };
    state.select(Some(target));
    true
}

/// Number of items in the config menu.
pub const CONFIG_MENU_ITEM_COUNT: usize = 11;
/// Total number of editable fields in the podcast editor.
pub const PODCAST_FIELD_COUNT: usize = 14;
/// Index of the first boolean field in the podcast editor.
pub const PODCAST_FIELD_BOOL_START: usize = 4;
/// Index of the leading-zeros usize field in the podcast editor.
pub const PODCAST_FIELD_USIZE: usize = 12;

/// UI state for the config menu screen.
pub struct ConfigEditorState {
    pub menu_state: ListState,
    pub edit_mode: ConfigEditMode,
    pub text_buffer: String,
    pub pending_dir_change: Option<String>,
    pub dialog: ConfigDialog,
}

/// Whether the podcast editor is editing an existing podcast or the default template.
#[derive(Debug)]
pub enum PodcastEditorTarget {
    Existing(usize),
    Template,
}

/// UI state for the single-podcast editor screen.
pub struct PodcastEditorState {
    pub target: PodcastEditorTarget,
    pub back_to: Screen,
    pub working_copy: Podcast,
    pub list_state: ListState,
    pub edit_mode: ConfigEditMode,
    pub text_buffer: String,
    pub dirty: bool,
    pub show_confirm_discard: bool,
    pub quit_pending: bool,
}

/// Application state for the TUI podcast manager.
pub struct App {
    pub screen: Screen,
    pub config: Config,
    pub podcast_list_state: ListState,
    pub latest_episodes: Vec<Option<(String, Option<String>)>>,
    pub download_progress: DownloadProgress,
    pub should_quit: bool,

    pub episodes: Vec<EpisodeInfo>,
    pub episode_selected: Vec<bool>,
    pub episode_list_state: ListState,
    pub loading_episodes: bool,
    pub episode_load_error: Option<String>,
    pub sort_order: SortOrder,
    pub config_notice: Option<String>,

    pub config_editor: ConfigEditorState,
    pub palette: CommandPaletteState,
    pub podcast_delete_pending: Option<usize>,
    pub podcast_editor: PodcastEditorState,
    pub edit_podcast_select_state: ListState,
    pub add_podcast: AddPodcastState,
    pub pending_open_folder: Option<String>,
    pub hint_bar_expanded: bool,
    pub banner_started: Instant,
    pub about_back_to: Screen,
    pub about_notice: Option<String>,
    /// Kept alive after a copy: on Linux the copied text lives only as long as this handle.
    pub clipboard: Option<arboard::Clipboard>,
}

impl App {
    /// Initialize the app with the given config and an optional startup notice.
    pub fn new(config: Config, config_notice: Option<String>) -> Self {
        let podcast_count = config.podcasts.len();
        let mut list_state = ListState::default();
        list_state.select(Some(0));

        let mut menu_state = ListState::default();
        menu_state.select(Some(0));

        let palette_matches: Vec<usize> = (0..COMMANDS.len())
            .filter(|&i| palette_command_visible(Screen::PodcastList, &COMMANDS[i]))
            .collect();
        let mut palette_list_state = ListState::default();
        if !palette_matches.is_empty() {
            palette_list_state.select(Some(0));
        }

        let mut ped_list_state = ListState::default();
        ped_list_state.select(Some(0));

        Self {
            screen: Screen::PodcastList,
            config,
            podcast_list_state: list_state,
            latest_episodes: vec![None; podcast_count],
            download_progress: DownloadProgress::new(),
            should_quit: false,
            episodes: Vec::new(),
            episode_selected: Vec::new(),
            episode_list_state: ListState::default(),
            loading_episodes: false,
            episode_load_error: None,
            sort_order: SortOrder::Desc,
            config_notice,
            config_editor: ConfigEditorState {
                menu_state,
                edit_mode: ConfigEditMode::Navigate,
                text_buffer: String::new(),
                pending_dir_change: None,
                dialog: ConfigDialog::Closed,
            },
            palette: CommandPaletteState {
                open: false,
                query: String::new(),
                list_state: palette_list_state,
                matches: palette_matches,
            },
            podcast_delete_pending: None,
            podcast_editor: PodcastEditorState {
                target: PodcastEditorTarget::Existing(0),
                back_to: Screen::PodcastList,
                working_copy: Podcast::default(),
                list_state: ped_list_state,
                edit_mode: ConfigEditMode::Navigate,
                text_buffer: String::new(),
                dirty: false,
                show_confirm_discard: false,
                quit_pending: false,
            },
            edit_podcast_select_state: ListState::default(),
            add_podcast: AddPodcastState {
                step: 0,
                podcast: Podcast::default(),
                text_buffer: String::new(),
                loading_feed_info: false,
                mode_select: false,
            },
            pending_open_folder: None,
            hint_bar_expanded: false,
            banner_started: Instant::now(),
            about_back_to: Screen::PodcastList,
            about_notice: None,
            clipboard: None,
        }
    }

    pub fn toggle_hint_bar(&mut self) {
        self.hint_bar_expanded = !self.hint_bar_expanded;
    }

    /// True while a download batch is still running.
    pub fn download_in_progress(&self) -> bool {
        self.screen == Screen::Downloading && self.download_progress.is_active()
    }

    /// Quit, unless a download is running or the editor has unsaved changes.
    ///
    /// The one path both `q` and the palette's quit entry take, so the guards
    /// cannot drift apart. Unsaved editor changes open the save/discard
    /// confirmation instead of quitting.
    pub fn request_quit(&mut self) {
        if self.download_in_progress() {
            return;
        }
        self.request_force_quit();
    }

    /// Quit even mid-download, still asking about unsaved editor changes.
    pub fn request_force_quit(&mut self) {
        self.palette_close();
        if self.screen == Screen::About
            && self.about_back_to == Screen::EditPodcast
            && self.podcast_editor.dirty
        {
            self.about_back();
        }
        if self.screen == Screen::EditPodcast && self.podcast_editor.dirty {
            self.podcast_editor.quit_pending = true;
            self.podcast_editor.show_confirm_discard = true;
        } else {
            self.should_quit = true;
        }
    }

    /// Index of the currently highlighted podcast in the list.
    pub fn selected_podcast_index(&self) -> usize {
        self.podcast_list_state.selected().unwrap_or(0)
    }

    /// Name of the currently highlighted podcast.
    pub fn selected_podcast_name(&self) -> &str {
        &self.config.podcasts[self.selected_podcast_index()].name
    }

    /// Navigate one podcast up in the list.
    pub fn move_up(&mut self) {
        let i = self.selected_podcast_index();
        if i > 0 {
            self.podcast_list_state.select(Some(i - 1));
        }
    }

    /// Navigate one podcast down in the list.
    pub fn move_down(&mut self) {
        let i = self.selected_podcast_index();
        if i < self.config.podcasts.len().saturating_sub(1) {
            self.podcast_list_state.select(Some(i + 1));
        }
    }

    /// Switch to the episode selection screen for the current podcast.
    pub fn enter_episode_select(&mut self) {
        self.podcast_delete_pending = None;
        self.screen = Screen::EpisodeSelect;
        self.episodes.clear();
        self.episode_selected.clear();
        self.episode_list_state = ListState::default();
        self.loading_episodes = true;
        self.episode_load_error = None;
    }

    /// Store a fetched episode list and reset selection state.
    pub fn set_episodes(&mut self, episodes: Vec<EpisodeInfo>) {
        let count = episodes.len();
        self.episodes = episodes;
        self.episode_selected = vec![false; count];
        if count > 0 {
            self.episode_list_state.select(Some(0));
        }
        self.loading_episodes = false;
    }

    /// Reverse the episode list sort order.
    pub fn toggle_sort_order(&mut self) {
        self.sort_order = self.sort_order.toggle();
        self.episodes.reverse();
        self.episode_selected.reverse();
        if !self.episodes.is_empty() {
            self.episode_list_state.select(Some(0));
        }
    }

    /// Record an error that occurred while loading episodes.
    pub fn set_episode_load_error(&mut self, err: String) {
        self.episode_load_error = Some(err);
        self.loading_episodes = false;
    }

    /// Move the episode cursor up by one.
    pub fn episode_move_up(&mut self) {
        if let Some(i) = self.episode_list_state.selected()
            && i > 0
        {
            self.episode_list_state.select(Some(i - 1));
        }
    }

    /// Move the episode cursor down by one.
    pub fn episode_move_down(&mut self) {
        if let Some(i) = self.episode_list_state.selected()
            && i < self.episodes.len().saturating_sub(1)
        {
            self.episode_list_state.select(Some(i + 1));
        }
    }

    /// Toggle the download checkbox on the current episode.
    pub fn toggle_current_episode(&mut self) {
        if let Some(i) = self.episode_list_state.selected()
            && i < self.episode_selected.len()
        {
            self.episode_selected[i] = !self.episode_selected[i];
        }
    }

    /// Toggle all episode checkboxes on or off.
    pub fn select_all_episodes(&mut self) {
        let all_selected = self.episode_selected.iter().all(|&s| s);
        self.episode_selected.fill(!all_selected);
    }

    /// Number of currently selected episodes.
    pub fn selected_count(&self) -> usize {
        self.episode_selected.iter().filter(|&&s| s).count()
    }

    /// Feed indices of all selected episodes for download.
    pub fn selected_feed_indices(&self) -> Vec<usize> {
        self.episodes
            .iter()
            .zip(self.episode_selected.iter())
            .filter(|&(_, &sel)| sel)
            .map(|(ep, _)| ep.feed_index)
            .collect()
    }

    /// Navigate back from the current screen.
    pub fn go_back(&mut self) {
        match self.screen {
            Screen::EpisodeSelect => self.screen = Screen::PodcastList,
            Screen::Downloading if !self.download_progress.is_active() => {
                self.screen = Screen::PodcastList;
                self.download_progress = DownloadProgress::new();
            }
            _ => {}
        }
    }

    /// Switch to the downloading screen and reset progress.
    pub fn start_download(&mut self) {
        self.screen = Screen::Downloading;
        self.download_progress = DownloadProgress::new();
    }

    /// Process a download progress event from the background task.
    pub fn handle_download_event(&mut self, event: DownloadEvent) {
        match event {
            DownloadEvent::Started {
                episode_name,
                index,
                total,
            } => {
                self.download_progress.current_episode = episode_name;
                self.download_progress.current_index = index;
                self.download_progress.total = total;
            }
            DownloadEvent::Completed { episode_name } => {
                self.download_progress.completed.push(episode_name);
            }
            DownloadEvent::Tagged { episode_name } => {
                self.download_progress.tagged.push(episode_name);
            }
            DownloadEvent::Error { message } => {
                self.download_progress.errors.push(message);
            }
            DownloadEvent::Finished => {
                self.download_progress.finished = true;
            }
        }
    }
}

#[cfg(test)]
pub(crate) fn test_app_one_podcast() -> App {
    use crate::podcast::{Config, Podcast};
    let config = Config {
        download_dir_location: "/tmp/test".to_string(),
        podcasts: vec![Podcast {
            name: "Pod1".into(),
            feed_url: "http://example.com/1".into(),
            ..Podcast::default()
        }],
        default_podcast: Podcast::default(),
        default_mode: crate::podcast::DefaultMode::default(),
        banner_style: crate::podcast::BannerStyle::default(),
    };
    App::new(config, None)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::podcast::{Config, Podcast};

    fn test_app() -> App {
        let config = Config {
            download_dir_location: "/tmp/test".to_string(),
            podcasts: vec![
                Podcast {
                    name: "Pod1".into(),
                    feed_url: "http://example.com/1".into(),
                    ..Podcast::default()
                },
                Podcast {
                    name: "Pod2".into(),
                    feed_url: "http://example.com/2".into(),
                    ..Podcast::default()
                },
            ],
            default_podcast: Podcast::default(),
            default_mode: crate::podcast::DefaultMode::default(),
            banner_style: crate::podcast::BannerStyle::default(),
        };
        App::new(config, None)
    }

    #[test]
    fn sort_order_toggle_desc_to_asc() {
        assert_eq!(SortOrder::Desc.toggle(), SortOrder::Asc);
    }

    #[test]
    fn sort_order_toggle_asc_to_desc() {
        assert_eq!(SortOrder::Asc.toggle(), SortOrder::Desc);
    }

    #[test]
    fn sort_order_desc_label() {
        assert_eq!(SortOrder::Desc.label(), "Newest first");
    }

    #[test]
    fn sort_order_asc_label() {
        assert_eq!(SortOrder::Asc.label(), "Oldest first");
    }

    #[test]
    fn download_progress_new_defaults() {
        let dp = DownloadProgress::new();
        assert!(!dp.finished);
        assert_eq!(dp.total, 0);
        assert_eq!(dp.current_index, 0);
        assert!(dp.current_episode.is_empty());
        assert!(dp.completed.is_empty());
        assert!(dp.errors.is_empty());
    }

    #[test]
    fn list_jump_home_and_end_reach_the_bounds() {
        let mut st = ListState::default();
        st.select(Some(3));
        assert!(apply_list_jump(&mut st, 8, 0, KeyCode::End));
        assert_eq!(st.selected(), Some(7));
        assert!(apply_list_jump(&mut st, 8, 0, KeyCode::Char('g')));
        assert_eq!(st.selected(), Some(0));
    }

    #[test]
    fn list_jump_pages_clamp_at_both_ends() {
        let mut st = ListState::default();
        st.select(Some(2));
        assert!(apply_list_jump(&mut st, 30, 0, KeyCode::PageUp));
        assert_eq!(st.selected(), Some(0));
        assert!(apply_list_jump(&mut st, 30, 0, KeyCode::PageDown));
        assert_eq!(st.selected(), Some(LIST_PAGE_STEP));
        st.select(Some(25));
        assert!(apply_list_jump(&mut st, 30, 0, KeyCode::PageDown));
        assert_eq!(st.selected(), Some(29));
    }

    #[test]
    fn list_jump_respects_the_floor() {
        let mut st = ListState::default();
        st.select(Some(9));
        assert!(apply_list_jump(&mut st, 14, 4, KeyCode::Home));
        assert_eq!(st.selected(), Some(4));
        assert!(apply_list_jump(&mut st, 14, 4, KeyCode::PageUp));
        assert_eq!(st.selected(), Some(4));
    }

    #[test]
    fn list_jump_handles_no_selection_and_empty_lists() {
        let mut st = ListState::default();
        assert!(apply_list_jump(&mut st, 5, 0, KeyCode::End));
        assert_eq!(st.selected(), Some(4));

        let mut empty = ListState::default();
        assert!(!apply_list_jump(&mut empty, 0, 0, KeyCode::End));
        assert_eq!(empty.selected(), None);

        let mut other = ListState::default();
        other.select(Some(1));
        assert!(!apply_list_jump(&mut other, 5, 0, KeyCode::Char('j')));
        assert_eq!(other.selected(), Some(1));
    }

    #[test]
    fn move_up_at_zero_stays() {
        let mut app = test_app();
        app.podcast_list_state.select(Some(0));
        app.move_up();
        assert_eq!(app.selected_podcast_index(), 0);
    }

    #[test]
    fn move_down_advances_index() {
        let mut app = test_app();
        app.podcast_list_state.select(Some(0));
        app.move_down();
        assert_eq!(app.selected_podcast_index(), 1);
    }

    #[test]
    fn toggle_sort_order_reverses_episodes() {
        let mut app = test_app();
        app.set_episodes(vec![
            EpisodeInfo {
                title: "Ep1".into(),
                feed_index: 1,
                pub_date: None,
            },
            EpisodeInfo {
                title: "Ep2".into(),
                feed_index: 2,
                pub_date: None,
            },
        ]);
        assert_eq!(app.sort_order, SortOrder::Desc);
        app.toggle_sort_order();
        assert_eq!(app.sort_order, SortOrder::Asc);
        assert_eq!(app.episodes[0].title, "Ep2");
        assert_eq!(app.episodes[1].title, "Ep1");
    }

    #[test]
    fn select_all_then_deselect_all() {
        let mut app = test_app();
        app.set_episodes(vec![
            EpisodeInfo {
                title: "Ep1".into(),
                feed_index: 1,
                pub_date: None,
            },
            EpisodeInfo {
                title: "Ep2".into(),
                feed_index: 2,
                pub_date: None,
            },
        ]);
        app.select_all_episodes();
        assert!(app.episode_selected.iter().all(|&s| s));
        app.select_all_episodes();
        assert!(app.episode_selected.iter().all(|&s| !s));
    }

    #[test]
    fn selected_feed_indices_returns_correct() {
        let mut app = test_app();
        app.set_episodes(vec![
            EpisodeInfo {
                title: "Ep1".into(),
                feed_index: 10,
                pub_date: None,
            },
            EpisodeInfo {
                title: "Ep2".into(),
                feed_index: 20,
                pub_date: None,
            },
            EpisodeInfo {
                title: "Ep3".into(),
                feed_index: 30,
                pub_date: None,
            },
        ]);
        app.episode_selected[0] = true;
        app.episode_selected[2] = true;
        assert_eq!(app.selected_feed_indices(), vec![10, 30]);
    }

    #[test]
    fn go_back_from_episode_select_to_podcast_list() {
        let mut app = test_app();
        app.screen = Screen::EpisodeSelect;
        app.go_back();
        assert_eq!(app.screen, Screen::PodcastList);
    }

    #[test]
    fn handle_download_event_started() {
        let mut app = test_app();
        app.handle_download_event(DownloadEvent::Started {
            episode_name: "Test Ep".into(),
            index: 1,
            total: 5,
        });
        assert_eq!(app.download_progress.current_episode, "Test Ep");
        assert_eq!(app.download_progress.current_index, 1);
        assert_eq!(app.download_progress.total, 5);
    }

    #[test]
    fn handle_download_event_completed() {
        let mut app = test_app();
        app.handle_download_event(DownloadEvent::Completed {
            episode_name: "Done Ep".into(),
        });
        assert_eq!(app.download_progress.completed, vec!["Done Ep"]);
    }

    #[test]
    fn an_episode_error_does_not_end_the_batch() {
        let mut app = test_app();
        app.start_download();
        app.handle_download_event(DownloadEvent::Started {
            episode_name: "Ep 1".into(),
            index: 1,
            total: 3,
        });
        app.handle_download_event(DownloadEvent::Error {
            message: "Episode index 9 is out of range — skipping".into(),
        });
        assert!(
            app.download_progress.is_active(),
            "a skipped episode must not end a batch that is still downloading"
        );
        app.handle_download_event(DownloadEvent::Finished);
        assert!(!app.download_progress.is_active());
    }

    #[test]
    fn handle_download_event_finished() {
        let mut app = test_app();
        assert!(!app.download_progress.finished);
        app.handle_download_event(DownloadEvent::Finished);
        assert!(app.download_progress.finished);
    }
}