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
use std::{collections::VecDeque, sync::Mutex};

use async_trait::async_trait;

use crate::cardano_block_scanner::RawCardanoPoint;
use crate::StdResult;

use super::{ChainBlockNextAction, ChainBlockReader};

/// [FakeChainReader] is a fake implementation of [ChainBlockReader] for testing purposes.
pub struct FakeChainReader {
    chain_block_next_actions: Mutex<VecDeque<ChainBlockNextAction>>,
}

impl FakeChainReader {
    /// Creates a new [FakeChainReader] instance.
    pub fn new(chain_block_next_actions: Vec<ChainBlockNextAction>) -> Self {
        Self {
            chain_block_next_actions: Mutex::new(chain_block_next_actions.into()),
        }
    }

    /// Total remaining next actions
    pub fn get_total_remaining_next_actions(&self) -> usize {
        self.chain_block_next_actions.lock().unwrap().len()
    }
}

#[async_trait]
impl ChainBlockReader for FakeChainReader {
    async fn set_chain_point(&mut self, _point: &RawCardanoPoint) -> StdResult<()> {
        Ok(())
    }

    async fn get_next_chain_block(&mut self) -> StdResult<Option<ChainBlockNextAction>> {
        Ok(self.chain_block_next_actions.lock().unwrap().pop_front())
    }
}

#[cfg(test)]
mod tests {
    use crate::cardano_block_scanner::ScannedBlock;
    use crate::entities::{BlockNumber, SlotNumber};

    use super::*;

    #[tokio::test]
    async fn test_get_next_chain_block() {
        let expected_chain_block_next_actions = vec![
            ChainBlockNextAction::RollForward {
                parsed_block: ScannedBlock::new(
                    "hash-1",
                    BlockNumber(1),
                    SlotNumber(10),
                    Vec::<&str>::new(),
                ),
            },
            ChainBlockNextAction::RollForward {
                parsed_block: ScannedBlock::new(
                    "hash-2",
                    BlockNumber(2),
                    SlotNumber(11),
                    Vec::<&str>::new(),
                ),
            },
            ChainBlockNextAction::RollBackward {
                rollback_point: RawCardanoPoint::new(SlotNumber(1), "point-hash-1".as_bytes()),
            },
        ];

        let mut chain_reader = FakeChainReader::new(expected_chain_block_next_actions.clone());

        let mut chain_block_next_actions = vec![];
        while let Some(chain_block_next_action) = chain_reader.get_next_chain_block().await.unwrap()
        {
            chain_block_next_actions.push(chain_block_next_action);
        }

        assert_eq!(expected_chain_block_next_actions, chain_block_next_actions);
    }
}