144 lines
3.8 KiB
Rust
144 lines
3.8 KiB
Rust
use midi::{
|
|
DeviceMatch, MatchBasis, PortIdentity, PortResolution, UsbDeviceIdentity, resolve_port,
|
|
};
|
|
|
|
fn usb(serial: Option<&str>, path: &str) -> UsbDeviceIdentity {
|
|
UsbDeviceIdentity {
|
|
vendor_id: "1234".into(),
|
|
product_id: "5678".into(),
|
|
serial: serial.map(str::to_owned),
|
|
path: Some(path.into()),
|
|
}
|
|
}
|
|
|
|
fn port(id: &str, serial: Option<&str>, path: &str) -> PortIdentity {
|
|
PortIdentity {
|
|
backend_id: id.into(),
|
|
name: "Controller MIDI 1".into(),
|
|
usb: Some(usb(serial, path)),
|
|
}
|
|
}
|
|
|
|
fn expected(serial: Option<&str>, path: Option<&str>) -> DeviceMatch {
|
|
DeviceMatch {
|
|
port_name: "Controller MIDI 1".into(),
|
|
vendor_id: Some("1234".into()),
|
|
product_id: Some("5678".into()),
|
|
serial: serial.map(str::to_owned),
|
|
path: path.map(str::to_owned),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn serial_distinguishes_identical_models_independent_of_port_order() {
|
|
let ports = vec![
|
|
port("28:0", Some("RIGHT"), "usb-0:2"),
|
|
port("24:0", Some("LEFT"), "usb-0:1"),
|
|
];
|
|
|
|
assert_eq!(
|
|
resolve_port(&expected(Some("LEFT"), None), &ports, None),
|
|
PortResolution::Matched {
|
|
index: 1,
|
|
basis: MatchBasis::Serial,
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn physical_path_distinguishes_identical_serialless_models() {
|
|
let ports = vec![
|
|
port("28:0", None, "pci-1-usb-0:2"),
|
|
port("24:0", None, "pci-1-usb-0:1"),
|
|
];
|
|
|
|
assert_eq!(
|
|
resolve_port(&expected(None, Some("pci-1-usb-0:2")), &ports, None),
|
|
PortResolution::Matched {
|
|
index: 0,
|
|
basis: MatchBasis::PhysicalPath,
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn serialless_same_model_stays_ambiguous_until_explicitly_selected() {
|
|
let ports = vec![port("24:0", None, "usb-0:1"), port("28:0", None, "usb-0:2")];
|
|
let hints = expected(None, None);
|
|
assert_eq!(
|
|
resolve_port(&hints, &ports, None),
|
|
PortResolution::Ambiguous {
|
|
candidate_indices: vec![0, 1],
|
|
}
|
|
);
|
|
assert_eq!(
|
|
resolve_port(&hints, &ports, Some("28:0")),
|
|
PortResolution::Matched {
|
|
index: 1,
|
|
basis: MatchBasis::Manual,
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn manual_selection_cannot_override_model_or_usb_mismatch() {
|
|
let mut wrong = port("30:0", None, "usb-0:3");
|
|
wrong.name = "Other Controller".into();
|
|
assert_eq!(
|
|
resolve_port(&expected(None, None), &[wrong], Some("30:0")),
|
|
PortResolution::Missing
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_unique_virtual_port_can_fall_back_to_its_name() {
|
|
let port = PortIdentity {
|
|
backend_id: "128:0".into(),
|
|
name: "DAW bridge".into(),
|
|
usb: None,
|
|
};
|
|
let hints = DeviceMatch {
|
|
port_name: "DAW bridge".into(),
|
|
vendor_id: None,
|
|
product_id: None,
|
|
serial: None,
|
|
path: None,
|
|
};
|
|
assert_eq!(
|
|
resolve_port(&hints, &[port], None),
|
|
PortResolution::Matched {
|
|
index: 0,
|
|
basis: MatchBasis::UniqueModel,
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn alsa_numeric_suffix_does_not_defeat_stable_usb_identity() {
|
|
let actual = PortIdentity {
|
|
backend_id: "36:0".into(),
|
|
name: "APC MINI:APC MINI MIDI 1 36:0".into(),
|
|
usb: Some(UsbDeviceIdentity {
|
|
vendor_id: "09e8".into(),
|
|
product_id: "0028".into(),
|
|
serial: Some("Ver00.10".into()),
|
|
path: Some("pci-usb-1".into()),
|
|
}),
|
|
};
|
|
let hints = DeviceMatch {
|
|
port_name: "APC MINI:APC MINI MIDI 1".into(),
|
|
vendor_id: Some("09e8".into()),
|
|
product_id: Some("0028".into()),
|
|
serial: Some("Ver00.10".into()),
|
|
path: None,
|
|
};
|
|
|
|
assert_eq!(
|
|
resolve_port(&hints, &[actual], None),
|
|
PortResolution::Matched {
|
|
index: 0,
|
|
basis: MatchBasis::Serial,
|
|
}
|
|
);
|
|
}
|