139 lines
4.6 KiB
Rust
139 lines
4.6 KiB
Rust
use fader_core::SurfaceId;
|
|
use midi::{
|
|
DeviceMatch, MatchBasis, PortIdentity, SurfaceAction, SurfaceAssignment, SurfaceLifecycle,
|
|
SurfaceStatus, UsbDeviceIdentity,
|
|
};
|
|
|
|
fn surface(id: &str, serial: Option<&str>, manual: Option<&str>) -> SurfaceAssignment {
|
|
SurfaceAssignment {
|
|
surface: SurfaceId::new(id).unwrap(),
|
|
expected: DeviceMatch {
|
|
port_name: "APC MINI MIDI 1".into(),
|
|
vendor_id: Some("09e8".into()),
|
|
product_id: Some("0028".into()),
|
|
serial: serial.map(str::to_owned),
|
|
path: None,
|
|
},
|
|
manual_backend_id: manual.map(str::to_owned),
|
|
}
|
|
}
|
|
|
|
fn port(backend_id: &str, serial: Option<&str>, path: &str) -> PortIdentity {
|
|
PortIdentity {
|
|
backend_id: backend_id.into(),
|
|
name: "APC MINI MIDI 1".into(),
|
|
usb: Some(UsbDeviceIdentity {
|
|
vendor_id: "09e8".into(),
|
|
product_id: "0028".into(),
|
|
serial: serial.map(str::to_owned),
|
|
path: Some(path.into()),
|
|
}),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn unplug_replug_and_port_reorder_preserve_a_serial_assignment() {
|
|
let id = SurfaceId::new("left").unwrap();
|
|
let mut lifecycle = SurfaceLifecycle::new([surface("left", Some("SERIAL"), None)]).unwrap();
|
|
let first = vec![port("36:0", Some("SERIAL"), "usb-1")];
|
|
assert!(matches!(
|
|
lifecycle.reconcile(&first).as_slice(),
|
|
[SurfaceAction::Connect { backend_id, .. }] if backend_id == "36:0"
|
|
));
|
|
lifecycle.connection_succeeded(&id, "36:0").unwrap();
|
|
assert!(matches!(
|
|
lifecycle.status(&id),
|
|
Some(SurfaceStatus::Connected {
|
|
basis: MatchBasis::Serial,
|
|
..
|
|
})
|
|
));
|
|
|
|
assert!(matches!(
|
|
lifecycle.reconcile(&[]).as_slice(),
|
|
[SurfaceAction::Disconnect { backend_id, .. }] if backend_id == "36:0"
|
|
));
|
|
assert_eq!(lifecycle.status(&id), Some(&SurfaceStatus::Missing));
|
|
|
|
let reordered = vec![
|
|
port("40:0", Some("OTHER"), "usb-2"),
|
|
port("42:0", Some("SERIAL"), "usb-3"),
|
|
];
|
|
assert!(matches!(
|
|
lifecycle.reconcile(&reordered).as_slice(),
|
|
[SurfaceAction::Connect { backend_id, .. }] if backend_id == "42:0"
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn ambiguous_identical_devices_stay_unassigned_until_manually_selected() {
|
|
let id = SurfaceId::new("left").unwrap();
|
|
let ports = vec![port("36:0", None, "usb-1"), port("40:0", None, "usb-2")];
|
|
let mut unresolved = SurfaceLifecycle::new([surface("left", None, None)]).unwrap();
|
|
assert!(unresolved.reconcile(&ports).is_empty());
|
|
assert!(matches!(
|
|
unresolved.status(&id),
|
|
Some(SurfaceStatus::Ambiguous { backend_ids }) if backend_ids == &["36:0", "40:0"]
|
|
));
|
|
|
|
let mut manual = SurfaceLifecycle::new([surface("left", None, Some("40:0"))]).unwrap();
|
|
assert!(matches!(
|
|
manual.reconcile(&ports).as_slice(),
|
|
[SurfaceAction::Connect { backend_id, .. }] if backend_id == "40:0"
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn one_connection_failure_does_not_disconnect_another_surface() {
|
|
let left = SurfaceId::new("left").unwrap();
|
|
let right = SurfaceId::new("right").unwrap();
|
|
let ports = vec![
|
|
port("36:0", Some("LEFT"), "usb-1"),
|
|
port("40:0", Some("RIGHT"), "usb-2"),
|
|
];
|
|
let mut lifecycle = SurfaceLifecycle::new([
|
|
surface("left", Some("LEFT"), None),
|
|
surface("right", Some("RIGHT"), None),
|
|
])
|
|
.unwrap();
|
|
assert_eq!(lifecycle.reconcile(&ports).len(), 2);
|
|
lifecycle.connection_succeeded(&left, "36:0").unwrap();
|
|
lifecycle
|
|
.connection_failed(&right, "40:0", "device busy")
|
|
.unwrap();
|
|
|
|
assert!(matches!(
|
|
lifecycle.status(&left),
|
|
Some(SurfaceStatus::Connected { .. })
|
|
));
|
|
assert!(matches!(
|
|
lifecycle.status(&right),
|
|
Some(SurfaceStatus::Error { message, .. }) if message == "device busy"
|
|
));
|
|
assert!(matches!(
|
|
lifecycle.reconcile(&ports).as_slice(),
|
|
[SurfaceAction::Connect { surface, .. }] if surface == &right
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn two_configured_surfaces_cannot_claim_the_same_port() {
|
|
let left = SurfaceId::new("left").unwrap();
|
|
let right = SurfaceId::new("right").unwrap();
|
|
let ports = vec![port("36:0", Some("SHARED"), "usb-1")];
|
|
let assignment = surface("left", Some("SHARED"), None);
|
|
let mut duplicate = assignment.clone();
|
|
duplicate.surface = right.clone();
|
|
let mut lifecycle = SurfaceLifecycle::new([assignment, duplicate]).unwrap();
|
|
|
|
assert!(lifecycle.reconcile(&ports).is_empty());
|
|
assert!(matches!(
|
|
lifecycle.status(&left),
|
|
Some(SurfaceStatus::Ambiguous { .. })
|
|
));
|
|
assert!(matches!(
|
|
lifecycle.status(&right),
|
|
Some(SurfaceStatus::Ambiguous { .. })
|
|
));
|
|
}
|