konnektoren_core/controller/
debug_plugin.rs1use super::GameControllerTrait;
2use super::{ControllerPlugin, ControllerPluginError};
3use crate::commands::CommandType;
4use log;
5use std::sync::Arc;
6
7#[derive(Clone)]
8pub struct DebugPlugin {
9 logger: Arc<dyn log::Log>,
10}
11
12impl DebugPlugin {
13 pub fn new(logger: Arc<dyn log::Log>) -> Self {
14 Self { logger }
15 }
16}
17
18impl ControllerPlugin for DebugPlugin {
19 fn name(&self) -> &str {
20 "DebugPlugin"
21 }
22 fn init(&self) -> Result<(), ControllerPluginError> {
23 Ok(())
24 }
25 fn load(
26 &self,
27 game_controller: Arc<dyn GameControllerTrait>,
28 ) -> Result<(), ControllerPluginError> {
29 let logger = self.logger.clone();
30
31 game_controller
32 .command_bus()
33 .subscribe(CommandType::Game, move |command| {
34 logger.log(
35 &log::Record::builder()
36 .level(log::Level::Debug)
37 .target("GameCommand")
38 .args(format_args!("Command: {:?}", command))
39 .build(),
40 );
41 });
42
43 let logger = self.logger.clone();
44
45 game_controller
46 .command_bus()
47 .subscribe(CommandType::Challenge, move |command| {
48 logger.log(
49 &log::Record::builder()
50 .level(log::Level::Debug)
51 .target("ChallengeCommand")
52 .args(format_args!("Command: {:?}", command))
53 .build(),
54 );
55 });
56
57 Ok(())
58 }
59
60 fn unload(
61 &self,
62 _game_controller: Arc<dyn GameControllerTrait>,
63 ) -> Result<(), ControllerPluginError> {
64 Ok(())
65 }
66}
67
68#[cfg(test)]
69mod test {
70 use crate::commands::{Command, CommandBus, GameCommand};
71 use crate::controller::game_controller::MockGameControllerTrait;
72
73 use super::*;
74 use std::sync::Mutex;
75
76 #[derive(Default)]
77 pub struct MockLogger {
78 messages: Mutex<Vec<String>>,
79 }
80
81 impl MockLogger {
82 pub fn get_messages(&self) -> Vec<String> {
83 self.messages.lock().unwrap().clone()
84 }
85 }
86
87 impl log::Log for MockLogger {
88 fn enabled(&self, _metadata: &log::Metadata) -> bool {
89 true
90 }
91
92 fn log(&self, record: &log::Record) {
93 let mut messages = self.messages.lock().unwrap();
94 messages.push(format!("{}: {}", record.level(), record.args()));
95 }
96
97 fn flush(&self) {}
98 }
99
100 #[test]
101 fn test_debug_plugin() {
102 let mut mock_game_controller = MockGameControllerTrait::new();
103
104 let command_bus = CommandBus::new();
105
106 mock_game_controller
107 .expect_command_bus()
108 .return_const(command_bus.clone());
109
110 let mock_logger = Arc::new(MockLogger::default());
111 let debug_plugin = DebugPlugin::new(mock_logger.clone());
112
113 assert_eq!(debug_plugin.name(), "DebugPlugin");
114 assert_eq!(debug_plugin.init(), Ok(()));
115 assert_eq!(debug_plugin.load(Arc::new(mock_game_controller)), Ok(()));
116
117 command_bus.publish(Command::Game(GameCommand::NextChallenge));
118
119 let messages = mock_logger.get_messages();
120 assert_eq!(messages.len(), 1);
121 assert_eq!(messages[0], "DEBUG: Command: Game(NextChallenge)");
122 assert!(messages.contains(&"DEBUG: Command: Game(NextChallenge)".to_string()));
123 }
124}