use crate::context::Context;
use async_trait::async_trait;
use std::any::Any;
#[async_trait]
pub trait System: Send + Sync + 'static {
fn name(&self) -> &'static str;
async fn initialize(&mut self, _ctx: &mut Context) {}
async fn shutdown(&mut self, _ctx: &mut Context) {}
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
#[cfg(test)]
mod tests {
use super::*;
struct TurnManagerSystem {
turn_count: u32,
log: Vec<String>,
}
impl TurnManagerSystem {
fn new() -> Self {
Self {
turn_count: 0,
log: Vec::new(),
}
}
fn next_turn(&mut self) {
self.turn_count += 1;
self.log.push(format!("Turn {}", self.turn_count));
}
fn get_turn_count(&self) -> u32 {
self.turn_count
}
}
#[async_trait]
impl System for TurnManagerSystem {
fn name(&self) -> &'static str {
"turn_manager"
}
async fn initialize(&mut self, _ctx: &mut Context) {
self.log.push("Turn manager initialized".to_string());
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
#[tokio::test]
async fn test_system_creation() {
let system = TurnManagerSystem::new();
assert_eq!(system.name(), "turn_manager");
assert_eq!(system.get_turn_count(), 0);
}
#[tokio::test]
async fn test_system_turn_management() {
let mut system = TurnManagerSystem::new();
system.next_turn();
system.next_turn();
assert_eq!(system.get_turn_count(), 2);
assert_eq!(system.log.len(), 2);
}
#[tokio::test]
async fn test_system_initialize() {
let mut system = TurnManagerSystem::new();
let mut ctx = Context::new();
system.initialize(&mut ctx).await;
assert!(system.log.contains(&"Turn manager initialized".to_string()));
}
}