use rodecaster_protocol::test_fixtures::{n, nc, np, prop};
use rodecaster_protocol::{
change_frame::{decode as decode_change_frame, encode_property_changed, ChangeFrame},
decode_event, ChannelParam, Command, DeviceEvent, DuckerParam, EffectsParam, Fader, GuiParam,
HeadphoneParam, InputSourceParam, Layout, MasterParam, MixLinkDirection, MixOutput, Node,
OutputParam, PadParam, PlayerParam, RecorderParam, Source, SystemParam, TriggerPhase, Value,
};
fn synthetic_root() -> Node {
let phys = nc(
"PHYSICALINTERFACE",
vec![
n("HEADER"),
np("FADER", vec![prop("faderLevel", Value::Int(50))]),
np("FADER", vec![prop("faderLevel", Value::Int(60))]),
np("FADER", vec![prop("faderLevel", Value::Int(70))]),
n("FOOTER"),
],
);
let mut children = vec![
n("OTHER1"),
n("OTHER2"),
phys, n("OTHER3"),
np(
"CHANNEL",
vec![
prop("channelOutputMute", Value::Bool(false)),
prop("channelCueEnable", Value::Bool(false)),
],
),
np(
"CHANNEL",
vec![prop("channelOutputMute", Value::Bool(true))],
),
np(
"CHANNEL",
vec![prop("channelOutputMute", Value::Bool(false))],
),
n("OTHER4"),
];
for i in 0..26 {
children.push(np(
"MIX",
vec![prop(
"mixLevelWithAnchor",
Value::String(format!("0.5|{}", 0.1 * (i % 13) as f32)),
)],
));
}
for _ in 0..19 {
children.push(n("INPUTSOURCE"));
}
children.push(n("MASTERCHANNEL"));
children.push(n("OUTPUT"));
children.push(n("DUCKER"));
children.push(n("RECORDER"));
children.push(n("PLAYER"));
children.push(n("HEADPHONE"));
children.push(n("HEADPHONE"));
children.push(n("EFFECTS_PARAMETERS"));
children.push(n("EFFECTS_PARAMETERS"));
children.push(n("EFFECTS_PARAMETERS"));
children.push(n("GUI"));
children.push(nc("SOUNDPADS", vec![n("PAD"), n("PAD")]));
children.push(n("SYSTEM"));
nc("DEVICE", children)
}
fn layout() -> Layout {
Layout::from_full_sync(&synthetic_root()).unwrap()
}
#[test]
fn round_trip_set_fader_mute() {
let l = layout();
let cmd = Command::SetFaderMute {
fader: Fader::Physical2,
mute: true,
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(payloads.len(), 1);
let event = decode_event(&payloads[0], &l).unwrap();
assert_eq!(
event,
DeviceEvent::FaderMuteChanged {
fader: Fader::Physical2,
muted: true,
}
);
}
#[test]
fn round_trip_set_fader_cue() {
let l = layout();
let cmd = Command::SetFaderCue {
fader: Fader::Physical1,
enable: true,
};
let payloads = cmd.encode(&l).unwrap();
let event = decode_event(&payloads[0], &l).unwrap();
assert_eq!(
event,
DeviceEvent::FaderCueChanged {
fader: Fader::Physical1,
enabled: true,
}
);
}
#[test]
fn round_trip_set_fader_level_through_two_level_path() {
let l = layout();
let cmd = Command::SetFaderLevel {
fader: Fader::Physical3,
level: 100,
};
let payloads = cmd.encode(&l).unwrap();
let event = decode_event(&payloads[0], &l).unwrap();
assert_eq!(
event,
DeviceEvent::FaderLevelChanged {
fader: Fader::Physical3,
level: 100,
}
);
}
#[test]
fn round_trip_set_mix_disabled() {
let l = layout();
let cmd = Command::SetMixDisabled {
source: Source::Combo2,
mix: MixOutput::Usb1,
disabled: true,
};
let payloads = cmd.encode(&l).unwrap();
let event = decode_event(&payloads[0], &l).unwrap();
assert_eq!(
event,
DeviceEvent::MixDisabledChanged {
source: Source::Combo2,
mix: MixOutput::Usb1,
disabled: true,
}
);
}
#[test]
fn round_trip_set_mix_level() {
let l = layout();
let cmd = Command::SetMixLevel {
source: Source::Combo2,
mix: MixOutput::Usb1,
anchor: 0.5,
value: 0.8,
};
let payloads = cmd.encode(&l).unwrap();
let event = decode_event(&payloads[0], &l).unwrap();
match event {
DeviceEvent::MixLevelChanged {
source,
mix,
anchor,
value,
} => {
assert_eq!(source, Source::Combo2);
assert_eq!(mix, MixOutput::Usb1);
assert!((anchor - 0.5).abs() < 1e-4);
assert!((value - 0.8).abs() < 1e-4);
}
other => panic!("expected MixLevelChanged, got {other:?}"),
}
}
#[test]
fn round_trip_set_channel_param() {
let l = layout();
let cmd = Command::SetChannelParam {
fader: Fader::Physical2,
param: ChannelParam::EqHighGain,
value: Value::Double(6.0),
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(payloads.len(), 1);
let event = decode_event(&payloads[0], &l).unwrap();
assert_eq!(
event,
DeviceEvent::ChannelParamChanged {
fader: Fader::Physical2,
param: ChannelParam::EqHighGain,
value: Value::Double(6.0),
}
);
}
#[test]
fn round_trip_set_channel_param_preserves_every_value_shape() {
let l = layout();
let cases = [
(ChannelParam::EqOn, Value::Bool(true)),
(ChannelParam::CompressorRatio, Value::Int(4)),
(ChannelParam::HpfFrequency, Value::Double(80.0)),
(
ChannelParam::Other("channelCustomLabel".to_string()),
Value::String("xlr".to_string()),
),
(
ChannelParam::Other("channelMysteryKnob".to_string()),
Value::Int(3),
),
];
for (param, value) in cases {
let cmd = Command::SetChannelParam {
fader: Fader::Physical1,
param: param.clone(),
value: value.clone(),
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::ChannelParamChanged {
fader: Fader::Physical1,
param,
value,
}
);
}
}
#[test]
fn round_trip_set_input_source_param() {
let l = layout();
let cmd = Command::SetInputSourceParam {
source: Source::Combo2,
param: InputSourceParam::InputMicrophoneGain,
value: Value::Int(50),
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(payloads.len(), 1);
let event = decode_event(&payloads[0], &l).unwrap();
assert_eq!(
event,
DeviceEvent::InputSourceParamChanged {
source: Source::Combo2,
param: InputSourceParam::InputMicrophoneGain,
value: Value::Int(50),
}
);
}
#[test]
fn round_trip_set_input_source_param_preserves_every_value_shape() {
let l = layout();
let cases = [
(InputSourceParam::InputPower, Value::Int(1)),
(InputSourceParam::InputPhaseFlip, Value::Bool(true)),
(
InputSourceParam::InputWirelessSn,
Value::String("SN12345".to_string()),
),
(
InputSourceParam::Other("inputMysteryFlag".to_string()),
Value::Int(7),
),
];
for (param, value) in cases {
let cmd = Command::SetInputSourceParam {
source: Source::Combo1,
param: param.clone(),
value: value.clone(),
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::InputSourceParamChanged {
source: Source::Combo1,
param,
value,
}
);
}
}
#[test]
fn round_trip_set_master_param() {
let l = layout();
let cmd = Command::SetMasterParam {
param: MasterParam::CompellorThreshold,
value: Value::Double(0.67),
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(payloads.len(), 1);
let event = decode_event(&payloads[0], &l).unwrap();
assert_eq!(
event,
DeviceEvent::MasterParamChanged {
param: MasterParam::CompellorThreshold,
value: Value::Double(0.67),
}
);
}
#[test]
fn round_trip_set_output_param() {
let l = layout();
let cmd = Command::SetOutputParam {
param: OutputParam::MonLevel,
value: Value::Double(0.0),
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(payloads.len(), 1);
let event = decode_event(&payloads[0], &l).unwrap();
assert_eq!(
event,
DeviceEvent::OutputParamChanged {
param: OutputParam::MonLevel,
value: Value::Double(0.0),
}
);
}
#[test]
fn round_trip_set_master_and_output_params_preserve_every_value_shape() {
let l = layout();
let master_cases = [
(MasterParam::CompellorOn, Value::Bool(true)),
(MasterParam::DelaySeconds, Value::Double(0.25)),
(
MasterParam::Other("masterMysteryKnob".to_string()),
Value::Int(3),
),
];
for (param, value) in master_cases {
let payloads = Command::SetMasterParam {
param: param.clone(),
value: value.clone(),
}
.encode(&l)
.unwrap();
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::MasterParamChanged { param, value }
);
}
let output_cases = [
(OutputParam::MonMute, Value::Bool(false)),
(OutputParam::MultiMode, Value::Int(5)),
(OutputParam::BtLevel, Value::Double(0.5)),
(
OutputParam::Other("outputMysteryFlag".to_string()),
Value::Int(9),
),
];
for (param, value) in output_cases {
let payloads = Command::SetOutputParam {
param: param.clone(),
value: value.clone(),
}
.encode(&l)
.unwrap();
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::OutputParamChanged { param, value }
);
}
}
#[test]
fn round_trip_set_ducker_recorder_player_params() {
let l = layout();
let payloads = Command::SetDuckerParam {
param: DuckerParam::Depth,
value: Value::Double(-9.0),
}
.encode(&l)
.unwrap();
assert_eq!(payloads.len(), 1);
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::DuckerParamChanged {
param: DuckerParam::Depth,
value: Value::Double(-9.0),
}
);
let payloads = Command::SetRecorderParam {
param: RecorderParam::RequestRecordState,
value: Value::Int(1),
}
.encode(&l)
.unwrap();
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::RecorderParamChanged {
param: RecorderParam::RequestRecordState,
value: Value::Int(1),
}
);
let payloads = Command::SetPlayerParam {
param: PlayerParam::State,
value: Value::Int(0),
}
.encode(&l)
.unwrap();
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::PlayerParamChanged {
param: PlayerParam::State,
value: Value::Int(0),
}
);
}
#[test]
fn round_trip_set_headphone_param_preserves_index_and_value_shape() {
let l = layout();
let cases = [
(0u8, HeadphoneParam::Type, Value::Int(0)),
(
1u8,
HeadphoneParam::Colour,
Value::String("ffd43580".into()),
),
(
1u8,
HeadphoneParam::Other("headphoneMystery".to_string()),
Value::Bool(true),
),
];
for (headphone, param, value) in cases {
let payloads = Command::SetHeadphoneParam {
headphone,
param: param.clone(),
value: value.clone(),
}
.encode(&l)
.unwrap();
assert_eq!(payloads.len(), 1);
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::HeadphoneParamChanged {
headphone,
param,
value,
}
);
}
}
#[test]
fn round_trip_set_effects_param_preserves_index_and_value_shape() {
let l = layout();
let cases = [
(0u8, EffectsParam::ReverbOn, Value::Bool(true)),
(1u8, EffectsParam::EchoMix, Value::Double(0.25)),
(2u8, EffectsParam::PitchShiftSemitones, Value::Int(-3)),
(
2u8,
EffectsParam::Other("flangerDepth".to_string()),
Value::Double(0.8),
),
];
for (effects, param, value) in cases {
let payloads = Command::SetEffectsParam {
effects,
param: param.clone(),
value: value.clone(),
}
.encode(&l)
.unwrap();
assert_eq!(payloads.len(), 1);
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::EffectsParamChanged {
effects,
param,
value,
}
);
}
}
#[test]
fn round_trip_set_gui_param_preserves_value_shape() {
let l = layout();
let cases = [
(GuiParam::Lang, Value::String("en".to_string())),
(GuiParam::ScreenBrightness, Value::Int(250)),
(GuiParam::BroadcastMeters, Value::Bool(false)),
(GuiParam::Other("mysteryGuiKnob".to_string()), Value::Int(7)),
];
for (param, value) in cases {
let payloads = Command::SetGuiParam {
param: param.clone(),
value: value.clone(),
}
.encode(&l)
.unwrap();
assert_eq!(payloads.len(), 1);
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::GuiParamChanged { param, value }
);
}
}
#[test]
fn round_trip_set_pad_param_preserves_index_and_value_shape() {
let l = layout();
let cases = [
(0u8, PadParam::Name, Value::String("Applause".to_string())),
(1, PadParam::ColourIndex, Value::Int(7)),
(1, PadParam::Gain, Value::Double(0.5)),
(0, PadParam::Loop, Value::Bool(true)),
(
1,
PadParam::Other("padMysteryKnob".to_string()),
Value::Int(3),
),
];
for (pad, param, value) in cases {
let payloads = Command::SetPadParam {
pad,
param: param.clone(),
value: value.clone(),
}
.encode(&l)
.unwrap();
assert_eq!(payloads.len(), 1);
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::PadParamChanged { pad, param, value }
);
}
}
#[test]
fn round_trip_set_system_param_preserves_value_shape() {
let l = layout();
let cases = [
(
SystemParam::FirmwareVersion,
Value::String("1.7.3".to_string()),
),
(SystemParam::BoardType, Value::Int(0)),
(SystemParam::PowerOffRequest, Value::Bool(true)),
(SystemParam::UpdateDownloadProgress, Value::Double(0.5)),
(SystemParam::UpdateViaUsb, Value::Bool(true)),
(
SystemParam::Other("systemMysteryKnob".to_string()),
Value::Int(7),
),
];
for (param, value) in cases {
let payloads = Command::SetSystemParam {
param: param.clone(),
value: value.clone(),
}
.encode(&l)
.unwrap();
assert_eq!(payloads.len(), 1);
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::SystemParamChanged { param, value }
);
}
}
#[test]
fn round_trip_link_mix_emits_device_exact_sequence() {
let l = layout();
let cmd = Command::LinkMix {
source: Source::Combo1,
mix: MixOutput::Speaker,
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(
payloads.len(),
3,
"enable + unmute + mixLinkRequest trigger (the device's ack is its own write, \
not something the client emits)"
);
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::MixDisabledChanged {
source: Source::Combo1,
mix: MixOutput::Speaker,
disabled: false,
}
);
assert_eq!(
decode_event(&payloads[1], &l).unwrap(),
DeviceEvent::MixMuteChanged {
source: Source::Combo1,
mix: MixOutput::Speaker,
muted: false,
}
);
assert_eq!(
decode_event(&payloads[2], &l).unwrap(),
DeviceEvent::MixLinkRequested {
source: Source::Combo1,
mix: MixOutput::Speaker,
direction: MixLinkDirection::Link,
origin: TriggerPhase::Press,
}
);
}
#[test]
fn round_trip_unlink_mix_is_single_trigger_frame() {
let l = layout();
let cmd = Command::UnlinkMix {
source: Source::Combo1,
mix: MixOutput::Speaker,
};
let payloads = cmd.encode(&l).unwrap();
assert_eq!(
payloads.len(),
1,
"mixUnlinkRequest trigger; the cell's prior mixDisabled / mixMute are retained"
);
assert_eq!(
decode_event(&payloads[0], &l).unwrap(),
DeviceEvent::MixLinkRequested {
source: Source::Combo1,
mix: MixOutput::Speaker,
direction: MixLinkDirection::Unlink,
origin: TriggerPhase::Press,
}
);
}
#[test]
fn channel_input_source_is_asymmetric_write_stride1_echo_stride6() {
let l = layout();
let write = Command::AssignFaderSource {
fader: Fader::Physical1,
source: Some(Source::Combo2_3),
}
.encode(&l)
.unwrap();
assert_eq!(write.len(), 1);
let echo = encode_property_changed(
&[l.first_channel() + 6 * 2],
"channelInputSource",
&Value::Int(5),
);
let event = decode_event(&echo, &l).unwrap();
assert_eq!(
event,
DeviceEvent::FaderAssignmentChanged {
fader: Fader::Physical3,
source: Some(Source::Combo2_3),
}
);
}
#[test]
fn full_sync_path_yields_initial_state_for_synthetic_tree() {
let root = synthetic_root();
let l = Layout::from_full_sync(&root).unwrap();
let mut payload = vec![0x02];
write_value_tree(&root, &mut payload);
let event = decode_event(&payload, &l).unwrap();
let initial = match event {
DeviceEvent::InitialState(events) => events,
other => panic!("expected InitialState, got {other:?}"),
};
assert_eq!(initial.len(), 33);
assert_eq!(
initial[0],
DeviceEvent::FaderLevelChanged {
fader: Fader::Physical1,
level: 50,
}
);
assert_eq!(
initial[1],
DeviceEvent::FaderLevelChanged {
fader: Fader::Physical2,
level: 60,
}
);
assert_eq!(
initial[2],
DeviceEvent::FaderLevelChanged {
fader: Fader::Physical3,
level: 70,
}
);
}
#[test]
fn structural_change_invalidates_layout() {
let l = layout();
let payload = [0x04, 0x01, 0x01, 0x01, 0x02, 0x01, 0x03];
let event = decode_event(&payload, &l).unwrap();
assert_eq!(event, DeviceEvent::LayoutInvalidated);
}
#[test]
fn full_round_trip_through_transport_packet() {
use rodecaster_protocol::Packet;
let l = layout();
let cmd = Command::SetFaderMute {
fader: Fader::Physical1,
mute: true,
};
let payloads = cmd.encode(&l).unwrap();
let pkt = Packet::new(payloads[0].clone());
let wire = pkt.to_bytes();
let (received_pkt, used) = Packet::from_bytes(&wire).unwrap();
assert_eq!(used, wire.len());
let event = decode_event(&received_pkt.payload, &l).unwrap();
assert_eq!(
event,
DeviceEvent::FaderMuteChanged {
fader: Fader::Physical1,
muted: true,
}
);
}
#[test]
fn round_trip_addresses_track_layout_not_constants() {
let phys = nc("PHYSICALINTERFACE", vec![n("FADER"), n("FADER")]);
let mut a_children = vec![
phys.clone(),
n("X"),
n("Y"),
n("Z"),
n("CHANNEL"),
n("CHANNEL"),
];
for _ in 0..13 {
a_children.push(n("MIX"));
}
let layout_a = Layout::from_full_sync(&nc("DEVICE", a_children)).unwrap();
let mut b_children = vec![
phys,
n("X"),
n("Y"),
n("Z"),
n("W1"),
n("W2"),
n("W3"),
n("CHANNEL"),
n("CHANNEL"),
];
for _ in 0..13 {
b_children.push(n("MIX"));
}
let layout_b = Layout::from_full_sync(&nc("DEVICE", b_children)).unwrap();
let cmd = Command::SetFaderMute {
fader: Fader::Physical1,
mute: true,
};
let a = cmd.encode(&layout_a).unwrap();
let b = cmd.encode(&layout_b).unwrap();
assert_ne!(a, b, "wire bytes must differ when Layout differs");
assert_eq!(
decode_event(&a[0], &layout_a).unwrap(),
DeviceEvent::FaderMuteChanged {
fader: Fader::Physical1,
muted: true,
}
);
assert_eq!(
decode_event(&b[0], &layout_b).unwrap(),
DeviceEvent::FaderMuteChanged {
fader: Fader::Physical1,
muted: true,
}
);
let cross = decode_event(&a[0], &layout_b).unwrap();
assert_ne!(
cross,
DeviceEvent::FaderMuteChanged {
fader: Fader::Physical1,
muted: true,
},
"different Layout must NOT resolve A's path the same way"
);
}
#[test]
fn change_frame_codec_round_trip_via_encoder() {
use rodecaster_protocol::change_frame::encode_property_changed;
let cases = vec![
(vec![28u32], "channelOutputMute", Value::Bool(true)),
(
vec![217],
"mixLevelWithAnchor",
Value::String("0.5|0.5".to_string()),
),
(vec![0, 7], "faderLevel", Value::Int(75)),
(vec![0], "rootProperty", Value::Undefined),
];
for (path, name, value) in cases {
let bytes = encode_property_changed(&path, name, &value);
let frame = decode_change_frame(&bytes).expect("decodes");
match frame {
ChangeFrame::PropertyChanged {
path: p,
name: n,
value: v,
} => {
assert_eq!(p, path);
assert_eq!(n, name);
assert_eq!(v, value);
}
other => panic!("expected PropertyChanged, got {other:?}"),
}
}
}
fn write_value_tree(node: &Node, out: &mut Vec<u8>) {
out.extend_from_slice(node.name.as_bytes());
out.push(0);
write_compressed_int(out, node.properties.len() as i64);
for p in &node.properties {
out.extend_from_slice(p.name.as_bytes());
out.push(0);
p.value.write_to_stream(out);
}
write_compressed_int(out, node.children.len() as i64);
for c in &node.children {
write_value_tree(c, out);
}
}
fn write_compressed_int(out: &mut Vec<u8>, value: i64) {
let negative = value < 0;
let mut magnitude = value.unsigned_abs();
let mut bytes = [0u8; 4];
let mut num_bytes = 0usize;
while magnitude != 0 && num_bytes < 4 {
bytes[num_bytes] = (magnitude & 0xff) as u8;
magnitude >>= 8;
num_bytes += 1;
}
let size_byte = num_bytes as u8 | if negative { 0x80 } else { 0 };
out.push(size_byte);
out.extend_from_slice(&bytes[..num_bytes]);
}