Skip to main content

smugmug_cli/uploader/
queue.rs

1use std::collections::VecDeque;
2use std::path::PathBuf;
3
4pub struct UploadQueue {
5    queue: VecDeque<PathBuf>,
6}
7
8impl UploadQueue {
9    pub fn new() -> Self {
10        UploadQueue {
11            queue: VecDeque::new(),
12        }
13    }
14
15    pub fn add(&mut self, path: PathBuf) {
16        self.queue.push_back(path);
17    }
18
19    pub fn next(&mut self) -> Option<PathBuf> {
20        self.queue.pop_front()
21    }
22}
23
24impl Default for UploadQueue {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    // Tests for UploadQueue FIFO behavior and basic operations
35
36    #[test]
37    fn test_new_queue_is_empty() {
38        let mut queue = UploadQueue::new();
39        assert!(queue.next().is_none());
40    }
41
42    #[test]
43    fn test_default_queue_is_empty() {
44        let mut queue = UploadQueue::default();
45        assert!(queue.next().is_none());
46    }
47
48    #[test]
49    fn test_add_and_next() {
50        let mut queue = UploadQueue::new();
51        let path1 = PathBuf::from("/path/to/file1.jpg");
52        let path2 = PathBuf::from("/path/to/file2.jpg");
53
54        queue.add(path1.clone());
55        queue.add(path2.clone());
56
57        assert_eq!(queue.next(), Some(path1));
58        assert_eq!(queue.next(), Some(path2));
59        assert_eq!(queue.next(), None);
60    }
61
62    #[test]
63    fn test_fifo_order() {
64        let mut queue = UploadQueue::new();
65        let paths: Vec<PathBuf> = (0..5)
66            .map(|i| PathBuf::from(format!("/path/file{}.jpg", i)))
67            .collect();
68
69        for path in &paths {
70            queue.add(path.clone());
71        }
72
73        for path in &paths {
74            assert_eq!(queue.next().as_ref(), Some(path));
75        }
76
77        assert_eq!(queue.next(), None);
78    }
79
80    #[test]
81    fn test_single_item() {
82        let mut queue = UploadQueue::new();
83        let path = PathBuf::from("/path/to/single.jpg");
84
85        queue.add(path.clone());
86        assert_eq!(queue.next(), Some(path));
87        assert_eq!(queue.next(), None);
88    }
89
90    #[test]
91    fn test_multiple_add_and_next_cycles() {
92        let mut queue = UploadQueue::new();
93
94        let path1 = PathBuf::from("/path/file1.jpg");
95        queue.add(path1.clone());
96        assert_eq!(queue.next(), Some(path1));
97
98        let path2 = PathBuf::from("/path/file2.jpg");
99        queue.add(path2.clone());
100        assert_eq!(queue.next(), Some(path2));
101
102        assert_eq!(queue.next(), None);
103    }
104
105    #[test]
106    fn test_empty_path() {
107        let mut queue = UploadQueue::new();
108        let empty_path = PathBuf::from("");
109
110        queue.add(empty_path.clone());
111        assert_eq!(queue.next(), Some(empty_path));
112    }
113}