use super::*;
use crate::valuetree::Property;
fn node(name: &str) -> Node {
Node {
name: name.to_string(),
properties: vec![],
children: vec![],
}
}
fn node_with_children(name: &str, children: Vec<Node>) -> Node {
Node {
name: name.to_string(),
properties: vec![],
children,
}
}
fn synthetic_tree() -> Node {
let phys = node_with_children(
"PHYSICALINTERFACE",
vec![
node("HEADER"), node("FADER"), node("FADER"), node("FADER"), node("FOOTER"), ],
);
let mut children = vec![
node("OTHER1"), node("OTHER2"), phys, node("OTHER3"), node("CHANNEL"), node("CHANNEL"), node("CHANNEL"), node("OTHER4"), ];
for _ in 0..26 {
children.push(node("MIX"));
}
for _ in 0..4 {
children.push(node("INPUTSOURCE")); }
children.push(node("MASTERCHANNEL")); children.push(node("OUTPUT")); children.push(node("DUCKER")); children.push(node("RECORDER")); children.push(node("PLAYER")); children.push(node("HEADPHONE")); children.push(node("HEADPHONE")); children.push(node("EFFECTS_PARAMETERS")); children.push(node("EFFECTS_PARAMETERS")); children.push(node("EFFECTS_PARAMETERS")); children.push(node("GUI")); children.push(node_with_children(
"SOUNDPADS", vec![
node("PADHEADER"), node("PAD"), node("PAD"), node("PAD"), ],
));
children.push(node("SYSTEM")); node_with_children("DEVICE", children)
}
#[test]
fn discovers_bases_from_tree_positions_not_constants() {
let root = synthetic_tree();
let layout = Layout::from_full_sync(&root).expect("builds");
assert_eq!(layout.physical_interface_idx(), 2);
assert_eq!(layout.first_fader_in_phys(), 1);
assert_eq!(layout.fader_count(), 3);
assert_eq!(layout.first_channel(), 4);
assert_eq!(layout.channel_count(), 3);
assert_eq!(layout.first_mix(), 8);
assert_eq!(layout.source_count(), 2);
assert_eq!(layout.mix_count_per_source(), 13);
}
#[test]
fn channel_path_uses_discovered_first_channel() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.channel_path(0), Some(vec![4]));
assert_eq!(layout.channel_path(2), Some(vec![6]));
assert_eq!(layout.channel_path(3), None);
assert_eq!(layout.channel_index_from_path(&[4]), Some(0));
assert_eq!(layout.channel_index_from_path(&[6]), Some(2));
assert_eq!(layout.channel_index_from_path(&[7]), None);
assert_eq!(layout.channel_index_from_path(&[3]), None);
assert_eq!(layout.channel_index_from_path(&[4, 0]), None);
}
#[test]
fn fader_path_is_two_level_through_physical_interface() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.fader_path(0), Some(vec![2, 1]));
assert_eq!(layout.fader_path(2), Some(vec![2, 3]));
assert_eq!(layout.fader_path(3), None);
assert_eq!(layout.fader_index_from_path(&[2, 1]), Some(0));
assert_eq!(layout.fader_index_from_path(&[2, 3]), Some(2));
assert_eq!(layout.fader_index_from_path(&[2, 4]), None);
assert_eq!(layout.fader_index_from_path(&[0, 1]), None); assert_eq!(layout.fader_index_from_path(&[1]), None); }
#[test]
fn mix_cell_path_is_source_major() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.mix_cell_path(0, 0), Some(vec![8]));
assert_eq!(layout.mix_cell_path(0, 12), Some(vec![20]));
assert_eq!(layout.mix_cell_path(1, 0), Some(vec![21]));
assert_eq!(layout.mix_cell_path(1, 12), Some(vec![33]));
assert_eq!(layout.mix_cell_path(2, 0), None); assert_eq!(layout.mix_cell_path(0, 13), None); assert_eq!(layout.mix_cell_from_path(&[8]), Some((0, 0)));
assert_eq!(layout.mix_cell_from_path(&[20]), Some((0, 12)));
assert_eq!(layout.mix_cell_from_path(&[21]), Some((1, 0)));
assert_eq!(layout.mix_cell_from_path(&[33]), Some((1, 12)));
assert_eq!(layout.mix_cell_from_path(&[34]), None); assert_eq!(layout.mix_cell_from_path(&[7]), None); }
#[test]
fn input_source_path_uses_discovered_first_input_source() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.first_input_source(), Some(34));
assert_eq!(layout.input_source_count(), 4);
assert_eq!(layout.input_source_path(0), Some(vec![34]));
assert_eq!(layout.input_source_path(3), Some(vec![37]));
assert_eq!(layout.input_source_path(4), None); assert_eq!(layout.input_source_index_from_path(&[34]), Some(0));
assert_eq!(layout.input_source_index_from_path(&[37]), Some(3));
assert_eq!(layout.input_source_index_from_path(&[38]), None); assert_eq!(layout.input_source_index_from_path(&[33]), None); assert_eq!(layout.input_source_index_from_path(&[34, 0]), None); }
#[test]
fn master_and_output_paths_use_discovered_singletons() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.master_channel(), Some(38));
assert_eq!(layout.output(), Some(39));
assert_eq!(layout.master_channel_path(), Some(vec![38]));
assert_eq!(layout.output_path(), Some(vec![39]));
assert!(layout.is_master_channel_path(&[38]));
assert!(!layout.is_master_channel_path(&[39])); assert!(!layout.is_master_channel_path(&[38, 0])); assert!(layout.is_output_path(&[39]));
assert!(!layout.is_output_path(&[38])); }
#[test]
fn master_and_output_absent_yield_none_not_panic() {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("FADER")]);
let mut children = vec![phys, node("CHANNEL")];
for _ in 0..13 {
children.push(node("MIX"));
}
let layout = Layout::from_full_sync(&node_with_children("DEVICE", children)).unwrap();
assert_eq!(layout.master_channel(), None);
assert_eq!(layout.output(), None);
assert_eq!(layout.master_channel_path(), None);
assert_eq!(layout.output_path(), None);
assert!(!layout.is_master_channel_path(&[0]));
assert!(!layout.is_output_path(&[0]));
}
#[test]
fn ducker_recorder_player_paths_use_discovered_singletons() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.ducker(), Some(40));
assert_eq!(layout.recorder(), Some(41));
assert_eq!(layout.player(), Some(42));
assert_eq!(layout.ducker_path(), Some(vec![40]));
assert_eq!(layout.recorder_path(), Some(vec![41]));
assert_eq!(layout.player_path(), Some(vec![42]));
assert!(layout.is_ducker_path(&[40]));
assert!(!layout.is_ducker_path(&[41]));
assert!(layout.is_recorder_path(&[41]));
assert!(!layout.is_recorder_path(&[42]));
assert!(layout.is_player_path(&[42]));
assert!(!layout.is_player_path(&[40]));
assert!(!layout.is_player_path(&[42, 0]));
}
#[test]
fn headphone_paths_use_discovered_run() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.first_headphone(), Some(43));
assert_eq!(layout.headphone_count(), 2);
assert_eq!(layout.headphone_path(0), Some(vec![43]));
assert_eq!(layout.headphone_path(1), Some(vec![44]));
assert_eq!(layout.headphone_path(2), None); assert_eq!(layout.headphone_index_from_path(&[43]), Some(0));
assert_eq!(layout.headphone_index_from_path(&[44]), Some(1));
assert_eq!(layout.headphone_index_from_path(&[45]), None); assert_eq!(layout.headphone_index_from_path(&[42]), None); assert_eq!(layout.headphone_index_from_path(&[43, 0]), None); }
#[test]
fn effects_paths_use_discovered_run() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.first_effects(), Some(45));
assert_eq!(layout.effects_count(), 3);
assert_eq!(layout.effects_path(0), Some(vec![45]));
assert_eq!(layout.effects_path(1), Some(vec![46]));
assert_eq!(layout.effects_path(2), Some(vec![47]));
assert_eq!(layout.effects_path(3), None); assert_eq!(layout.effects_index_from_path(&[45]), Some(0));
assert_eq!(layout.effects_index_from_path(&[47]), Some(2));
assert_eq!(layout.effects_index_from_path(&[48]), None); assert_eq!(layout.effects_index_from_path(&[44]), None); assert_eq!(layout.effects_index_from_path(&[45, 0]), None); }
#[test]
fn gui_path_uses_discovered_singleton() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.gui(), Some(48));
assert_eq!(layout.gui_path(), Some(vec![48]));
assert!(layout.is_gui_path(&[48]));
assert!(!layout.is_gui_path(&[47])); assert!(!layout.is_gui_path(&[48, 0])); }
#[test]
fn system_path_uses_discovered_singleton() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.system(), Some(50));
assert_eq!(layout.system_path(), Some(vec![50]));
assert!(layout.is_system_path(&[50]));
assert!(!layout.is_system_path(&[49])); assert!(!layout.is_system_path(&[50, 0])); }
#[test]
fn pad_path_is_two_level_through_soundpads() {
let layout = Layout::from_full_sync(&synthetic_tree()).unwrap();
assert_eq!(layout.soundpads(), Some(49));
assert_eq!(layout.first_pad(), 1);
assert_eq!(layout.pad_count(), 3);
assert_eq!(layout.pad_path(0), Some(vec![49, 1]));
assert_eq!(layout.pad_path(2), Some(vec![49, 3]));
assert_eq!(layout.pad_path(3), None);
assert_eq!(layout.pad_index_from_path(&[49, 1]), Some(0));
assert_eq!(layout.pad_index_from_path(&[49, 3]), Some(2));
assert_eq!(layout.pad_index_from_path(&[49, 4]), None); assert_eq!(layout.pad_index_from_path(&[49, 0]), None); assert_eq!(layout.pad_index_from_path(&[2, 1]), None); assert_eq!(layout.pad_index_from_path(&[49]), None); }
#[test]
fn ducker_recorder_player_headphone_absent_yield_none_not_panic() {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("FADER")]);
let mut children = vec![phys, node("CHANNEL")];
for _ in 0..13 {
children.push(node("MIX"));
}
let layout = Layout::from_full_sync(&node_with_children("DEVICE", children)).unwrap();
assert_eq!(layout.ducker(), None);
assert_eq!(layout.recorder(), None);
assert_eq!(layout.player(), None);
assert_eq!(layout.ducker_path(), None);
assert_eq!(layout.recorder_path(), None);
assert_eq!(layout.player_path(), None);
assert!(!layout.is_ducker_path(&[0]));
assert!(!layout.is_recorder_path(&[0]));
assert!(!layout.is_player_path(&[0]));
assert_eq!(layout.first_headphone(), None);
assert_eq!(layout.headphone_count(), 0);
assert_eq!(layout.headphone_path(0), None);
assert_eq!(layout.headphone_index_from_path(&[0]), None);
assert_eq!(layout.first_effects(), None);
assert_eq!(layout.effects_count(), 0);
assert_eq!(layout.effects_path(0), None);
assert_eq!(layout.effects_index_from_path(&[0]), None);
assert_eq!(layout.gui(), None);
assert_eq!(layout.gui_path(), None);
assert!(!layout.is_gui_path(&[0]));
assert_eq!(layout.soundpads(), None);
assert_eq!(layout.pad_count(), 0);
assert_eq!(layout.pad_path(0), None);
assert_eq!(layout.pad_index_from_path(&[0, 0]), None);
assert_eq!(layout.system(), None);
assert_eq!(layout.system_path(), None);
assert!(!layout.is_system_path(&[0]));
}
#[test]
fn input_source_absent_yields_none_not_panic() {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("FADER")]);
let mut children = vec![phys, node("CHANNEL")];
for _ in 0..13 {
children.push(node("MIX"));
}
let layout = Layout::from_full_sync(&node_with_children("DEVICE", children)).unwrap();
assert_eq!(layout.first_input_source(), None);
assert_eq!(layout.input_source_count(), 0);
assert_eq!(layout.input_source_path(0), None);
assert_eq!(layout.input_source_index_from_path(&[0]), None);
}
#[test]
fn fails_when_physical_interface_missing() {
let root = node_with_children("DEVICE", vec![node("CHANNEL"), node("MIX")]);
let err = Layout::from_full_sync(&root).unwrap_err();
assert_eq!(err, BuildError::MissingNode("PHYSICALINTERFACE under root"));
}
#[test]
fn fails_when_fader_missing_inside_physical_interface() {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("HEADER")]);
let mut children = vec![phys, node("CHANNEL")];
for _ in 0..13 {
children.push(node("MIX"));
}
let root = node_with_children("DEVICE", children);
let err = Layout::from_full_sync(&root).unwrap_err();
assert_eq!(
err,
BuildError::MissingNode("FADER under PHYSICALINTERFACE")
);
}
#[test]
fn fails_when_channel_missing() {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("FADER")]);
let mut children = vec![phys];
for _ in 0..13 {
children.push(node("MIX"));
}
let root = node_with_children("DEVICE", children);
let err = Layout::from_full_sync(&root).unwrap_err();
assert_eq!(err, BuildError::MissingNode("CHANNEL under root"));
}
#[test]
fn fails_when_mix_missing() {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("FADER")]);
let root = node_with_children("DEVICE", vec![phys, node("CHANNEL")]);
let err = Layout::from_full_sync(&root).unwrap_err();
assert_eq!(err, BuildError::MissingNode("MIX under root"));
}
#[test]
fn fails_when_mix_count_not_divisible_by_matrix_width() {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("FADER")]);
let mut children = vec![phys, node("CHANNEL")];
for _ in 0..14 {
children.push(node("MIX"));
}
let root = node_with_children("DEVICE", children);
let err = Layout::from_full_sync(&root).unwrap_err();
match err {
BuildError::ShapeMismatch { what, .. } => assert_eq!(what, "MIX"),
other => panic!("expected ShapeMismatch, got {other:?}"),
}
}
fn tree_with_system(props: Vec<Property>) -> Node {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("FADER")]);
let system = Node {
name: "SYSTEM".to_string(),
properties: props,
children: vec![],
};
let mut children = vec![system, phys, node("CHANNEL")];
for _ in 0..13 {
children.push(node("MIX"));
}
node_with_children("DEVICE", children)
}
#[test]
fn model_defaults_to_pro2_without_system_node() {
let phys = node_with_children("PHYSICALINTERFACE", vec![node("FADER")]);
let mut children = vec![phys, node("CHANNEL")];
for _ in 0..13 {
children.push(node("MIX"));
}
let layout = Layout::from_full_sync(&node_with_children("DEVICE", children)).unwrap();
assert_eq!(layout.system(), None);
assert_eq!(layout.model(), DeviceModel::Pro2);
}
#[test]
fn model_reads_board_type_from_system() {
let pro2 = tree_with_system(vec![Property {
name: "boardType".to_string(),
value: Value::Int(0),
}]);
assert_eq!(
Layout::from_full_sync(&pro2).unwrap().model(),
DeviceModel::Pro2
);
let duo = tree_with_system(vec![Property {
name: "boardType".to_string(),
value: Value::Int(1),
}]);
assert_eq!(
Layout::from_full_sync(&duo).unwrap().model(),
DeviceModel::Duo
);
}
#[test]
fn model_falls_back_to_system_name() {
let duo = tree_with_system(vec![Property {
name: "systemName".to_string(),
value: Value::String("RODECaster Duo".to_string()),
}]);
assert_eq!(
Layout::from_full_sync(&duo).unwrap().model(),
DeviceModel::Duo
);
}