158 lines
4.8 KiB
Rust
158 lines
4.8 KiB
Rust
use std::{cell::Cell, rc::Rc, time::Duration};
|
|
|
|
use fader_config::{
|
|
CheckpointCoordinator, CheckpointSink, Clock, PersistedState, PersistenceError, StartupPolicy,
|
|
UiView,
|
|
};
|
|
|
|
#[test]
|
|
fn sustained_movement_has_bounded_writes_and_shutdown_persists_the_final_value() {
|
|
let clock = FakeClock::default();
|
|
let sink = RecordingSink::default();
|
|
let mut checkpoints = CheckpointCoordinator::new(clock.clone(), sink);
|
|
let mut state = state_with_observation();
|
|
|
|
for step in 0..50 {
|
|
state
|
|
.set_desired("desktop", "music", Some(f64::from(step) / 50.0), None)
|
|
.unwrap();
|
|
checkpoints.mark_dirty(state.clone());
|
|
checkpoints.poll().unwrap();
|
|
clock.advance(Duration::from_millis(100));
|
|
}
|
|
|
|
assert_eq!(
|
|
checkpoints.sink().documents.len(),
|
|
2,
|
|
"2s maximum bounds writes"
|
|
);
|
|
checkpoints.shutdown().unwrap();
|
|
assert_eq!(checkpoints.sink().documents.len(), 3);
|
|
let final_state = PersistedState::parse(checkpoints.sink().documents.last().unwrap()).unwrap();
|
|
let startup = final_state
|
|
.startup("desktop", StartupPolicy::Restore, 1.0)
|
|
.unwrap();
|
|
let writes = startup.application_joined("music");
|
|
assert_eq!(writes.len(), 1);
|
|
assert_eq!(writes[0].gain(), Some(0.98));
|
|
}
|
|
|
|
#[test]
|
|
fn a_quiet_period_flushes_after_debounce_not_before() {
|
|
let clock = FakeClock::default();
|
|
let mut checkpoints = CheckpointCoordinator::new(clock.clone(), RecordingSink::default());
|
|
checkpoints.mark_dirty(state_with_observation());
|
|
|
|
clock.advance(Duration::from_millis(499));
|
|
checkpoints.poll().unwrap();
|
|
assert!(checkpoints.sink().documents.is_empty());
|
|
clock.advance(Duration::from_millis(1));
|
|
checkpoints.poll().unwrap();
|
|
assert_eq!(checkpoints.sink().documents.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn restart_marks_cached_observations_stale_and_has_no_ephemeral_state() {
|
|
let state = state_with_observation();
|
|
let encoded = state.to_toml().unwrap();
|
|
assert!(!encoded.contains("backend_id"));
|
|
assert!(!encoded.contains("pending"));
|
|
assert!(!encoded.contains("operation"));
|
|
|
|
let restarted = PersistedState::parse(&encoded).unwrap();
|
|
let startup = restarted
|
|
.startup("desktop", StartupPolicy::Adopt, 1.0)
|
|
.unwrap();
|
|
assert_eq!(startup.cached_observations().len(), 1);
|
|
assert!(startup.cached_observations()[0].is_stale());
|
|
}
|
|
|
|
#[test]
|
|
fn offline_intent_survives_but_only_explicit_restore_emits_a_join_write() {
|
|
let mut state = state_with_observation();
|
|
state
|
|
.set_desired("desktop", "music", Some(0.7), Some(true))
|
|
.unwrap();
|
|
let restarted = PersistedState::parse(&state.to_toml().unwrap()).unwrap();
|
|
|
|
let adopt = restarted
|
|
.startup("desktop", StartupPolicy::Adopt, 1.0)
|
|
.unwrap();
|
|
assert!(adopt.application_joined("music").is_empty());
|
|
assert_eq!(adopt.desired_intents().len(), 1, "intent remains visible");
|
|
|
|
let restore = restarted
|
|
.startup("desktop", StartupPolicy::Restore, 1.0)
|
|
.unwrap();
|
|
let writes = restore.application_joined("music");
|
|
assert_eq!(writes.len(), 1);
|
|
assert_eq!(writes[0].gain(), Some(0.7));
|
|
assert_eq!(writes[0].muted(), Some(true));
|
|
}
|
|
|
|
#[test]
|
|
fn checkpoint_mutations_validate_observations_assignments_views_and_restore_bounds() {
|
|
let mut state = state_with_observation();
|
|
state
|
|
.set_observed("desktop", "music", &[0.8, 0.4], true)
|
|
.unwrap();
|
|
state
|
|
.set_surface_assignment("desktop", "desk-left", "input:1", Some("output:1"))
|
|
.unwrap();
|
|
state.set_last_view("desktop", UiView::Devices).unwrap();
|
|
state
|
|
.set_desired("desktop", "music", Some(1.5), None)
|
|
.unwrap();
|
|
|
|
let encoded = state.to_toml().unwrap();
|
|
assert!(encoded.contains("last_view = \"devices\""));
|
|
assert!(encoded.contains("surface = \"desk-left\""));
|
|
let error = state
|
|
.startup("desktop", StartupPolicy::Restore, 1.0)
|
|
.expect_err("restore must enforce the profile write ceiling");
|
|
assert!(error.to_string().contains("write ceiling"));
|
|
}
|
|
|
|
fn state_with_observation() -> PersistedState {
|
|
PersistedState::parse(
|
|
r#"
|
|
schema_version = 1
|
|
[[profiles]]
|
|
id = "desktop"
|
|
last_view = "mixer"
|
|
[[profiles.observed]]
|
|
object = "music"
|
|
gain = [0.5, 0.25]
|
|
muted = false
|
|
"#,
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
struct FakeClock(Rc<Cell<Duration>>);
|
|
|
|
impl FakeClock {
|
|
fn advance(&self, duration: Duration) {
|
|
self.0.set(self.0.get() + duration);
|
|
}
|
|
}
|
|
|
|
impl Clock for FakeClock {
|
|
fn now(&self) -> Duration {
|
|
self.0.get()
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct RecordingSink {
|
|
documents: Vec<String>,
|
|
}
|
|
|
|
impl CheckpointSink for RecordingSink {
|
|
fn write(&mut self, state: &PersistedState) -> Result<(), PersistenceError> {
|
|
self.documents.push(state.to_toml().expect("valid state"));
|
|
Ok(())
|
|
}
|
|
}
|