use fader_core::{ BackendEpoch, ChannelGains, Gain, StreamHandle, VolumeMode, plan_volume, volume_view, }; use std::collections::BTreeMap; fn gain(value: f64) -> Gain { Gain::new(value).unwrap() } fn channels(values: &[f64]) -> ChannelGains { ChannelGains::new(values.iter().copied().map(gain).collect()).unwrap() } fn handle(id: u64) -> StreamHandle { StreamHandle::new(BackendEpoch::new(1), id, 1) } #[test] fn absolute_and_relative_plans_preserve_channel_balance_without_mutation() { let live = BTreeMap::from([ (handle(1), channels(&[0.8, 0.4])), (handle(2), channels(&[0.2])), ]); let before = live.clone(); let absolute = plan_volume(&live, VolumeMode::Absolute, gain(0.4), Gain::UNITY).unwrap(); let relative = plan_volume(&live, VolumeMode::Relative, gain(0.4), Gain::UNITY).unwrap(); assert_eq!(absolute[&handle(1)], channels(&[0.4, 0.2])); assert_eq!(absolute[&handle(2)], channels(&[0.4])); assert_eq!(relative[&handle(2)], channels(&[0.1])); assert_eq!(live, before); assert!(volume_view(&live).unwrap().mixed); assert_eq!(volume_view(&live).unwrap().maximum, gain(0.8)); } #[test] fn silent_and_empty_groups_have_explicit_behavior() { let empty = BTreeMap::new(); assert_eq!(volume_view(&empty), None); assert!( plan_volume(&empty, VolumeMode::Relative, Gain::UNITY, Gain::UNITY) .unwrap() .is_empty() ); let silent = BTreeMap::from([(handle(1), channels(&[0.0, 0.0]))]); assert_eq!( plan_volume(&silent, VolumeMode::Relative, gain(0.5), Gain::UNITY).unwrap()[&handle(1)], channels(&[0.5, 0.5]) ); } #[test] fn relative_scaling_handles_extreme_finite_observations_and_clips_requests() { let live = BTreeMap::from([ (handle(1), channels(&[f64::MAX])), (handle(2), channels(&[f64::MAX / 2.0])), ]); let plan = plan_volume(&live, VolumeMode::Relative, gain(2.0), Gain::UNITY).unwrap(); assert_eq!(plan[&handle(1)], channels(&[1.0])); assert_eq!(plan[&handle(2)], channels(&[1.0])); } #[test] fn partial_confirmation_and_new_members_rebase_the_next_plan() { let mut live = BTreeMap::from([(handle(1), channels(&[0.8])), (handle(2), channels(&[0.2]))]); let plan = plan_volume(&live, VolumeMode::Absolute, gain(0.4), Gain::UNITY).unwrap(); // Only the first stream confirms; the failed second stream stays truthful. live.insert(handle(1), plan[&handle(1)].clone()); assert!(volume_view(&live).unwrap().mixed); assert_eq!(live[&handle(2)], channels(&[0.2])); // New streams are adopted without replaying the previous absolute intent. live.insert(handle(3), channels(&[1.0])); let next = plan_volume(&live, VolumeMode::Relative, gain(0.5), Gain::UNITY).unwrap(); assert_eq!(next[&handle(1)], channels(&[0.2])); assert_eq!(next[&handle(2)], channels(&[0.1])); assert_eq!(next[&handle(3)], channels(&[0.5])); }