Skip to main content

strata_device/
mock.rs

1//! DeviceMockController: Thread-safe hardware simulation controller for tests, CI, and emulators.
2
3use crate::signals::{notify_memory_pressure, sync_signals_from_mock};
4use crate::types::*;
5use std::sync::{Arc, Mutex, OnceLock};
6
7/// Snapshot of simulated hardware telemetry.
8#[derive(Debug, Clone, PartialEq, Default)]
9pub struct DeviceTelemetrySnapshot {
10    pub battery: BatteryInfo,
11    pub thermal: ThermalState,
12    pub network: NetworkStatus,
13    pub screen: ScreenGeometry,
14    pub display_refresh: DisplayRefreshInfo,
15    pub gpu: GpuInfo,
16}
17
18static GLOBAL_MOCK: OnceLock<Arc<Mutex<DeviceTelemetrySnapshot>>> = OnceLock::new();
19
20fn get_global_mock() -> &'static Arc<Mutex<DeviceTelemetrySnapshot>> {
21    GLOBAL_MOCK.get_or_init(|| Arc::new(Mutex::new(DeviceTelemetrySnapshot::default())))
22}
23
24/// Controller allowing programmatic mutation of simulated device telemetry.
25#[derive(Debug, Clone)]
26pub struct DeviceMockController {
27    state: Arc<Mutex<DeviceTelemetrySnapshot>>,
28}
29
30impl Default for DeviceMockController {
31    fn default() -> Self {
32        Self::new()
33    }
34}
35
36impl DeviceMockController {
37    /// Obtains a controller handle connected to the global hardware telemetry simulation.
38    pub fn new() -> Self {
39        Self {
40            state: Arc::clone(get_global_mock()),
41        }
42    }
43
44    /// Resets all simulated device telemetry to standard factory defaults.
45    pub fn reset_to_defaults(&self) {
46        let mut guard = self.state.lock().unwrap();
47        *guard = DeviceTelemetrySnapshot::default();
48        drop(guard);
49        sync_signals_from_mock(self);
50    }
51
52    /// Gets a copy of current battery info.
53    pub fn battery(&self) -> BatteryInfo {
54        self.state.lock().unwrap().battery
55    }
56
57    /// Updates battery info and notifies reactive signals.
58    pub fn set_battery(&self, info: BatteryInfo) {
59        self.state.lock().unwrap().battery = info;
60        sync_signals_from_mock(self);
61    }
62
63    /// Updates battery level in range [0.0, 1.0].
64    pub fn set_battery_level(&self, level: f32) {
65        let mut guard = self.state.lock().unwrap();
66        guard.battery.level = level.clamp(0.0, 1.0);
67        drop(guard);
68        sync_signals_from_mock(self);
69    }
70
71    /// Gets current thermal state.
72    pub fn thermal(&self) -> ThermalState {
73        self.state.lock().unwrap().thermal
74    }
75
76    /// Updates thermal state and notifies reactive signals.
77    pub fn set_thermal(&self, state: ThermalState) {
78        self.state.lock().unwrap().thermal = state;
79        sync_signals_from_mock(self);
80    }
81
82    /// Gets current network status.
83    pub fn network(&self) -> NetworkStatus {
84        self.state.lock().unwrap().network
85    }
86
87    /// Updates network status and notifies reactive signals.
88    pub fn set_network(&self, status: NetworkStatus) {
89        self.state.lock().unwrap().network = status;
90        sync_signals_from_mock(self);
91    }
92
93    /// Sets online status.
94    pub fn set_online(&self, is_online: bool) {
95        let mut guard = self.state.lock().unwrap();
96        guard.network.is_online = is_online;
97        drop(guard);
98        sync_signals_from_mock(self);
99    }
100
101    /// Gets current screen geometry.
102    pub fn screen(&self) -> ScreenGeometry {
103        self.state.lock().unwrap().screen
104    }
105
106    /// Updates screen geometry and notifies reactive signals.
107    pub fn set_screen(&self, screen: ScreenGeometry) {
108        self.state.lock().unwrap().screen = screen;
109        sync_signals_from_mock(self);
110    }
111
112    /// Sets screen safe area insets (notches, dynamic island).
113    pub fn set_safe_area(&self, insets: SafeAreaInsets) {
114        let mut guard = self.state.lock().unwrap();
115        guard.screen.safe_area = insets;
116        drop(guard);
117        sync_signals_from_mock(self);
118    }
119
120    /// Gets current display refresh info.
121    pub fn display_refresh(&self) -> DisplayRefreshInfo {
122        self.state.lock().unwrap().display_refresh
123    }
124
125    /// Updates display refresh characteristics and capabilities.
126    pub fn set_display_refresh(&self, info: DisplayRefreshInfo) {
127        self.state.lock().unwrap().display_refresh = info;
128        sync_signals_from_mock(self);
129    }
130
131    /// Gets current GPU info.
132    pub fn gpu(&self) -> GpuInfo {
133        self.state.lock().unwrap().gpu.clone()
134    }
135
136    /// Updates GPU introspection info.
137    pub fn set_gpu(&self, gpu: GpuInfo) {
138        self.state.lock().unwrap().gpu = gpu;
139    }
140
141    /// Triggers a system memory pressure notification across all registered callbacks.
142    pub fn trigger_memory_pressure(&self, level: MemoryPressure) {
143        notify_memory_pressure(level);
144    }
145}