use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use sonda_core::ScenarioHandle;
#[derive(Clone)]
pub struct AppState {
pub scenarios: Arc<RwLock<HashMap<String, ScenarioHandle>>>,
}
impl AppState {
pub fn new() -> Self {
Self {
scenarios: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl Default for AppState {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_state_has_empty_scenarios() {
let state = AppState::new();
let scenarios = state.scenarios.read().expect("RwLock must not be poisoned");
assert!(
scenarios.is_empty(),
"new AppState must have an empty scenarios map"
);
}
#[test]
fn default_produces_empty_state() {
let state = AppState::default();
let scenarios = state.scenarios.read().expect("RwLock must not be poisoned");
assert!(
scenarios.is_empty(),
"default AppState must have an empty scenarios map"
);
}
#[test]
fn clone_shares_same_arc() {
let state1 = AppState::new();
let state2 = state1.clone();
assert!(
Arc::ptr_eq(&state1.scenarios, &state2.scenarios),
"cloned AppState must share the same Arc<RwLock<...>>"
);
}
#[test]
fn app_state_is_send_and_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<AppState>();
}
#[test]
fn app_state_is_clone() {
fn assert_clone<T: Clone>() {}
assert_clone::<AppState>();
}
}