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
//! Contains utility related to collecting stats about the input data.

/// Displays an error message if the config doesn't have the mute error flag set.
pub(crate) fn display_error(error: &str) {
    use crate::util::{config::CONFIG, lib::UtilOpt};
    let is_muting_errors = CONFIG.get().unwrap().mute_errors();
    if !is_muting_errors {
        log::error!("{error}");
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        stats::{collect_its_stats, collect_system_specific_stats, StatType, SystemId},
        words::lib::RDH_CRU,
    };
    use std::sync::OnceLock;

    #[test]
    fn test_collect_its_stats() {
        let (stats_sender, stats_receiver) = flume::unbounded::<StatType>();
        let rdh = crate::words::rdh_cru::test_data::CORRECT_RDH_CRU_V7;

        let expect_layer = crate::words::its::layer_from_feeid(rdh.fee_id());
        let expect_stave = crate::words::its::stave_number_from_feeid(rdh.fee_id());

        collect_its_stats(&rdh, &stats_sender);

        let stats = stats_receiver.recv().unwrap();

        match stats {
            StatType::LayerStaveSeen { layer, stave } => {
                assert_eq!(layer, expect_layer);
                assert_eq!(stave, expect_stave);
            }
            _ => panic!("Wrong stat type received"),
        }
    }

    #[test]
    fn test_collect_system_specific_stats() {
        let (stats_sender, stats_receiver) = flume::unbounded::<StatType>();
        let mut system_id = None;

        let rdh = crate::words::rdh_cru::test_data::CORRECT_RDH_CRU_V7;
        let expect_layer = crate::words::its::layer_from_feeid(rdh.fee_id());
        let expect_stave = crate::words::its::stave_number_from_feeid(rdh.fee_id());

        collect_system_specific_stats(&rdh, &mut system_id, &stats_sender).unwrap();

        let stats = stats_receiver.recv().unwrap();

        match stats {
            StatType::LayerStaveSeen { layer, stave } => {
                assert_eq!(layer, expect_layer);
                assert_eq!(stave, expect_stave);
            }
            _ => panic!("Wrong stat type received"),
        }
    }

    #[test]
    fn test_system_id_from_system_id() {
        let system_id = SystemId::from_system_id(32).unwrap();
        assert_eq!(system_id, SystemId::ITS);
        let as_string = format!("{system_id}");
        assert_eq!(as_string, "ITS");
    }

    use crate::util::lib::test_util::MockConfig;
    static CONFIG_TEST_INIT_STATS_CONTROLLER: OnceLock<MockConfig> = OnceLock::new();
    #[test]
    fn test_init_stats_controller() {
        let mock_config = MockConfig::default();
        CONFIG_TEST_INIT_STATS_CONTROLLER.set(mock_config).unwrap();

        let (handle, send_ch, stop_flag, _errors_flag) =
            crate::stats::init_stats_controller(CONFIG_TEST_INIT_STATS_CONTROLLER.get().unwrap());

        // Stop flag should be false
        assert!(!stop_flag.load(std::sync::atomic::Ordering::SeqCst));

        // Send RDH version seen
        send_ch.send(StatType::RdhVersion(7)).unwrap();

        // Send Data format seen
        send_ch.send(StatType::DataFormat(99)).unwrap();

        // Send Run Trigger Type
        send_ch
            .send(StatType::RunTriggerType((0xBEEF, "BEEF".to_owned())))
            .unwrap();

        // Send rdh seen stat
        send_ch.send(StatType::RDHSeen).unwrap();

        // Send a fatal error that should cause the stop flag to be set
        send_ch
            .send(StatType::Fatal("Test fatal error".to_string()))
            .unwrap();

        // Stop the controller by dropping the sender channel
        drop(send_ch);

        // Wait for the controller to stop
        handle.join().unwrap();

        // Stop flag should be true
        assert!(stop_flag.load(std::sync::atomic::Ordering::SeqCst));
    }

    #[test]
    fn test_system_id_enums_all() {
        let valid_system_ids: [u8; 20] = [
            3, 4, 5, 6, 7, 8, 10, 15, 17, 18, 19, 32, 33, 34, 35, 36, 37, 38, 39, 255,
        ];
        for id in 0..=255 {
            let system_id = SystemId::from_system_id(id);
            if valid_system_ids.contains(&id) {
                assert!(system_id.is_ok());
                let to_str = system_id.unwrap().to_string();
                assert!(!to_str.is_empty());
            } else {
                assert!(system_id.is_err());
                let to_str = system_id.unwrap_err().to_string();
                assert_eq!(to_str, format!("Unknown system ID {id}"));
            }
        }
    }

    #[test]
    fn test_all_stattype_enums() {
        let fatal = StatType::Fatal("Test fatal error".to_string());
        let error = StatType::Error("Test error".to_string());
        let run_trig_type = StatType::RunTriggerType((1, "Test run trigger type".to_string()));
        let sys_id = StatType::SystemId(SystemId::ITS);
        let rdh_seen = StatType::RDHSeen;
        let rdh_filtered = StatType::RDHFiltered;
        let layer_stave_seen = StatType::LayerStaveSeen { layer: 1, stave: 1 };
        let mut stat_type_vec = vec![
            fatal,
            error,
            run_trig_type,
            sys_id,
            rdh_seen,
            rdh_filtered,
            layer_stave_seen,
        ];

        stat_type_vec.push(StatType::PayloadSize(1));
        stat_type_vec.push(StatType::LinksObserved(0));
        stat_type_vec.push(StatType::RdhVersion(1));
        stat_type_vec.push(StatType::DataFormat(1));
        stat_type_vec.push(StatType::HBFSeen);

        for stat_type in stat_type_vec {
            // Test to_string() method
            let to_str = stat_type.to_string();
            assert!(!to_str.is_empty());
        }
    }
}