visual_cortex_capture/
fake.rs1use std::collections::VecDeque;
2use std::sync::Arc;
3
4use crate::error::CaptureError;
5use crate::frame::Frame;
6use crate::source::FrameSource;
7
8pub struct FakeSource {
11 script: VecDeque<Arc<Frame>>,
12 last: Option<Arc<Frame>>,
13}
14
15impl FakeSource {
16 pub fn new(frames: impl IntoIterator<Item = Frame>) -> Self {
17 Self {
18 script: frames.into_iter().map(Arc::new).collect(),
19 last: None,
20 }
21 }
22}
23
24#[async_trait::async_trait]
25impl FrameSource for FakeSource {
26 async fn capture_frame(&mut self) -> Result<Arc<Frame>, CaptureError> {
27 if let Some(frame) = self.script.pop_front() {
28 self.last = Some(frame.clone());
29 return Ok(frame);
30 }
31 self.last.clone().ok_or(CaptureError::Exhausted)
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[tokio::test]
40 async fn replays_script_then_repeats_last_frame() {
41 let mut src = FakeSource::new([
42 Frame::solid(2, 2, [1, 1, 1, 255]),
43 Frame::solid(2, 2, [2, 2, 2, 255]),
44 ]);
45 assert_eq!(src.capture_frame().await.unwrap().data()[0], 1);
46 assert_eq!(src.capture_frame().await.unwrap().data()[0], 2);
47 assert_eq!(src.capture_frame().await.unwrap().data()[0], 2);
49 assert_eq!(src.capture_frame().await.unwrap().data()[0], 2);
50 }
51
52 #[tokio::test]
53 async fn empty_source_is_exhausted() {
54 let mut src = FakeSource::new([]);
55 assert!(matches!(
56 src.capture_frame().await,
57 Err(CaptureError::Exhausted)
58 ));
59 }
60}