556 lines
19 KiB
Rust
556 lines
19 KiB
Rust
use fader_config::{DeviceProfile, MixerLayoutEntry, PersistedState, Profile, StartupPolicy};
|
|
use fader_core::{StreamMetadata, VolumeMode};
|
|
use std::time::Duration;
|
|
|
|
const EXAMPLE: &str = include_str!("../../examples/profiles/desktop.toml");
|
|
const ARCH_PIPEWIRE: &str = include_str!("../../examples/profiles/arch-pipewire.toml");
|
|
const ARCHITECTURE: &str = include_str!("../../architecture.md");
|
|
|
|
#[test]
|
|
fn documented_media_profile_validates_and_round_trips_all_action_kinds() {
|
|
let source = include_str!("../../examples/profiles/media-qbz.toml");
|
|
let profile = Profile::parse(source).unwrap();
|
|
assert_eq!(profile.media()[0].controls.len(), 14);
|
|
assert_eq!(
|
|
profile,
|
|
Profile::parse(&profile.to_toml().unwrap()).unwrap()
|
|
);
|
|
let mut document = fader_config::ProfileDocument::parse(source).unwrap();
|
|
document.set_media_entry(&profile.media()[0]).unwrap();
|
|
assert_eq!(document.preview().unwrap().media(), profile.media());
|
|
}
|
|
|
|
#[test]
|
|
fn media_configuration_has_a_bounded_ipc_budget() {
|
|
let mut source = EXAMPLE.to_owned();
|
|
for n in 0..8 {
|
|
source.push_str(&format!("\n[[media]]\nid = 'entry-{n}'\nlabel = 'Player'\nselector = {{ field = 'identity', pattern = 'Player' }}\n"));
|
|
for c in 0..64 {
|
|
source.push_str(&format!("[[media.controls]]\nid = 'control-{c}'\naction = {{ kind = 'playlist', id = '/{}' }}\n", "x".repeat(1000)));
|
|
}
|
|
}
|
|
assert!(
|
|
Profile::parse(&source)
|
|
.unwrap_err()
|
|
.to_string()
|
|
.contains("256 KiB")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn media_entries_round_trip_and_validate_selectors_and_midi_collisions() {
|
|
let extra = r#"
|
|
[[media]]
|
|
id = "qbz"
|
|
label = "QBZ"
|
|
feedback_enabled = false
|
|
selector = { field = "bus_name", pattern = "org.mpris.MediaPlayer2.com.blitzfc.qbz" }
|
|
controls = [{ id = "play", action = { kind = "play" }, binding = { surface = "desk-left", message = { kind = "note", channel = 1, number = 40 } } }]
|
|
"#;
|
|
let source = format!("{EXAMPLE}\n{extra}");
|
|
let profile = Profile::parse(&source).unwrap();
|
|
let mut document = fader_config::ProfileDocument::parse(&source).unwrap();
|
|
let mut entry = profile.media()[0].clone();
|
|
entry.label = "Renamed QBZ".into();
|
|
document.set_media_entry(&entry).unwrap();
|
|
assert_eq!(document.preview().unwrap().media()[0].label, "Renamed QBZ");
|
|
document.remove_surface("desk-left").unwrap();
|
|
assert!(
|
|
document.preview().unwrap().media()[0].controls[0]
|
|
.binding
|
|
.is_none()
|
|
);
|
|
document.remove_media_entry("qbz").unwrap();
|
|
assert!(document.preview().unwrap().media().is_empty());
|
|
assert_eq!(
|
|
profile,
|
|
Profile::parse(&profile.to_toml().unwrap()).unwrap()
|
|
);
|
|
assert!(Profile::parse(&source.replace("number = 40", "number = 32")).is_err());
|
|
assert!(
|
|
Profile::parse(&source.replace("field = \"bus_name\"", "field = \"unknown\"")).is_err()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn feedback_switches_override_explicit_and_default_output() {
|
|
for source in [
|
|
format!("feedback_enabled = false\n{EXAMPLE}"),
|
|
EXAMPLE.replace("pickup = true", "pickup = true\nfeedback_enabled = false"),
|
|
] {
|
|
let profile = Profile::parse(&source).unwrap();
|
|
assert!(
|
|
profile
|
|
.bindings()
|
|
.iter()
|
|
.all(|binding| profile.effective_feedback(binding).is_none())
|
|
);
|
|
}
|
|
let profile = Profile::parse(EXAMPLE).unwrap();
|
|
assert!(profile.effective_feedback(&profile.bindings()[1]).is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn enriched_process_fields_match_only_available_values() {
|
|
let metadata = StreamMetadata::new().with_process_metadata(
|
|
Some("/games/Game/game.exe".into()),
|
|
Some("/games/Game".into()),
|
|
Some("123".into()),
|
|
Some("456".into()),
|
|
);
|
|
for (field, pattern) in [
|
|
("application.process.executable", "/games/Game/game.exe"),
|
|
("application.process.working_directory", "/games/Game"),
|
|
("steam.app_id", "123"),
|
|
("steam.game_id", "456"),
|
|
] {
|
|
let source = EXAMPLE.replace(
|
|
"field = \"application.name\", exact = \"Music Player\"",
|
|
&format!("field = {field:?}, exact = {pattern:?}"),
|
|
);
|
|
let profile = Profile::parse(&source).unwrap();
|
|
let matcher = profile.applications()[0].matcher().unwrap();
|
|
assert!(matcher.matches(&metadata));
|
|
assert!(!matcher.matches(&StreamMetadata::new()));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn selector_composition_defaults_to_any_and_all_requires_every_field() {
|
|
let source = EXAMPLE.replace(
|
|
"selectors = [{ field = \"application.name\", exact = \"Music Player\" }]",
|
|
"selectors = [{ field = \"application.name\", exact = \"Music Player\" }, { field = \"media.name\", regex = '(?i) - YouTube$' }]",
|
|
);
|
|
let partial = StreamMetadata::new().with_application_name("Music Player");
|
|
let complete = partial.clone().with_media_name("A song - YouTube");
|
|
let legacy = Profile::parse(&source).unwrap();
|
|
assert!(
|
|
legacy.applications()[0]
|
|
.matcher()
|
|
.unwrap()
|
|
.matches(&partial)
|
|
);
|
|
let source = source.replacen(
|
|
"label = \"Music\"",
|
|
"label = \"Music\"\nselector_mode = \"all\"",
|
|
1,
|
|
);
|
|
let profile = Profile::parse(&source).unwrap();
|
|
let matcher = profile.applications()[0].matcher().unwrap();
|
|
assert!(!matcher.matches(&partial));
|
|
assert!(matcher.matches(&complete));
|
|
assert!(!matcher.matches(&complete.with_application_name("Other Player")));
|
|
assert_eq!(
|
|
profile,
|
|
Profile::parse(&profile.to_toml().unwrap()).unwrap()
|
|
);
|
|
assert!(
|
|
Profile::parse(&source.replace("selector_mode = \"all\"", "selector_mode = \"xor\""))
|
|
.is_err()
|
|
);
|
|
assert!(Profile::parse(&source.replace("regex = '(?i) - YouTube$'", "regex = '['")).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn architecture_profile_parses_and_round_trips_semantically() {
|
|
let marker = "The following profile";
|
|
let documented = ARCHITECTURE
|
|
.split_once(marker)
|
|
.and_then(|(_, remainder)| remainder.split_once("```toml"))
|
|
.and_then(|(_, remainder)| remainder.split_once("```"))
|
|
.map(|(toml, _)| toml.trim())
|
|
.expect("architecture must contain its profile example");
|
|
assert_eq!(documented, EXAMPLE.trim(), "checked-in example drifted");
|
|
|
|
let profile = Profile::parse(documented).expect("architecture profile must validate");
|
|
assert_eq!(profile.id().as_str(), "desktop");
|
|
assert_eq!(profile.startup(), StartupPolicy::Adopt);
|
|
assert_eq!(profile.applications().len(), 2);
|
|
assert_eq!(profile.groups().len(), 1);
|
|
assert_eq!(profile.bindings().len(), 2);
|
|
|
|
let encoded = profile.to_toml().expect("validated profile must serialize");
|
|
let reparsed = Profile::parse(&encoded).expect("serialized profile must validate");
|
|
assert_eq!(profile, reparsed);
|
|
}
|
|
|
|
#[test]
|
|
fn validated_profile_exposes_compiled_runtime_application_and_group_views() {
|
|
let profile = Profile::parse(EXAMPLE).expect("example profile must validate");
|
|
let music = &profile.applications()[0];
|
|
assert_eq!(music.id(), "music");
|
|
assert_eq!(music.label(), "Music");
|
|
assert!(
|
|
music
|
|
.matcher()
|
|
.expect("validated selector compiles")
|
|
.matches(&StreamMetadata::new().with_application_name("Music Player"))
|
|
);
|
|
|
|
let media = &profile.groups()[0];
|
|
assert_eq!(media.id(), "media");
|
|
assert_eq!(media.label(), "Media");
|
|
assert_eq!(media.members(), ["music", "browser"]);
|
|
assert_eq!(media.volume_mode(), VolumeMode::Absolute);
|
|
}
|
|
|
|
#[test]
|
|
fn mixer_layout_defaults_to_declared_faders_and_validates_explicit_entries() {
|
|
let profile = Profile::parse(EXAMPLE).expect("legacy profile must synthesize its layout");
|
|
assert_eq!(
|
|
profile.mixer_orientation(),
|
|
fader_config::MixerOrientation::Vertical
|
|
);
|
|
assert_eq!(
|
|
profile.mixer_layout(),
|
|
[MixerLayoutEntry::Fader {
|
|
fader: "media-fader".to_owned(),
|
|
hidden: false,
|
|
density: fader_config::FaderDensity::Standard,
|
|
}]
|
|
);
|
|
|
|
let explicit = EXAMPLE.replacen(
|
|
"[[bindings]]",
|
|
"[[mixer_layout]]\nkind = \"separator\"\nid = \"media\"\nlabel = \"Media\"\n\n[[mixer_layout]]\nkind = \"fader\"\nfader = \"media-fader\"\nhidden = true\n\n[[bindings]]",
|
|
1,
|
|
);
|
|
let profile = Profile::parse(&explicit).expect("explicit layout must validate");
|
|
assert_eq!(profile.mixer_layout().len(), 2);
|
|
assert!(matches!(
|
|
&profile.mixer_layout()[1],
|
|
MixerLayoutEntry::Fader { fader, hidden, density }
|
|
if fader == "media-fader"
|
|
&& *hidden
|
|
&& *density == fader_config::FaderDensity::Standard
|
|
));
|
|
|
|
let slim = explicit.replace("hidden = true", "hidden = true\ndensity = \"slim\"");
|
|
assert!(matches!(
|
|
&Profile::parse(&slim).unwrap().mixer_layout()[1],
|
|
MixerLayoutEntry::Fader {
|
|
density: fader_config::FaderDensity::Slim,
|
|
..
|
|
}
|
|
));
|
|
|
|
let bordered_slim = explicit.replace(
|
|
"hidden = true",
|
|
"hidden = true\ndensity = \"bordered_slim\"",
|
|
);
|
|
assert!(matches!(
|
|
&Profile::parse(&bordered_slim).unwrap().mixer_layout()[1],
|
|
MixerLayoutEntry::Fader {
|
|
density: fader_config::FaderDensity::BorderedSlim,
|
|
..
|
|
}
|
|
));
|
|
|
|
let duplicate = explicit.replace(
|
|
"kind = \"fader\"\nfader = \"media-fader\"\nhidden = true",
|
|
"kind = \"fader\"\nfader = \"media-fader\"\nhidden = true\n\n[[mixer_layout]]\nkind = \"fader\"\nfader = \"media-fader\"",
|
|
);
|
|
assert_error_contains(Profile::parse(&duplicate), "duplicate fader");
|
|
|
|
let dangling = explicit.replace("fader = \"media-fader\"", "fader = \"missing\"");
|
|
assert_error_contains(Profile::parse(&dangling), "unknown fader");
|
|
|
|
let horizontal = EXAMPLE.replace(
|
|
"[ui]\ndefault_controller = \"desk-left\"",
|
|
"[ui]\ndefault_controller = \"desk-left\"\nmixer_orientation = \"horizontal\"",
|
|
);
|
|
assert_eq!(
|
|
Profile::parse(&horizontal).unwrap().mixer_orientation(),
|
|
fader_config::MixerOrientation::Horizontal
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn default_controller_is_optional_and_must_reference_a_surface() {
|
|
let profile = Profile::parse(EXAMPLE).expect("configured default controller must validate");
|
|
assert_eq!(profile.default_controller(), Some("desk-left"));
|
|
|
|
let missing = EXAMPLE.replace(
|
|
"default_controller = \"desk-left\"",
|
|
"default_controller = \"missing\"",
|
|
);
|
|
assert_error_contains(Profile::parse(&missing), "unknown surface \"missing\"");
|
|
|
|
let without_default = EXAMPLE.replace("default_controller = \"desk-left\"\n", "");
|
|
let profile =
|
|
Profile::parse(&without_default).expect("the default controller remains optional");
|
|
assert_eq!(profile.default_controller(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn theme_is_profile_backed_and_rejects_unknown_values() {
|
|
let configured = EXAMPLE.replace("theme = \"default\"", "theme = \"dracula\"");
|
|
assert_eq!(
|
|
Profile::parse(&configured).unwrap().theme(),
|
|
fader_config::UiTheme::Dracula
|
|
);
|
|
|
|
let invalid = configured.replace("theme = \"dracula\"", "theme = \"hot_dog\"");
|
|
assert!(Profile::parse(&invalid).is_err());
|
|
assert_eq!(
|
|
Profile::parse(EXAMPLE).unwrap().theme(),
|
|
fader_config::UiTheme::Default
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn pickup_status_display_defaults_and_validates_named_modes() {
|
|
assert_eq!(
|
|
Profile::parse(&EXAMPLE.replace("status_display = \"detailed\"\n", ""))
|
|
.unwrap()
|
|
.status_display(),
|
|
fader_config::StatusDisplayMode::Detailed
|
|
);
|
|
let configured = EXAMPLE.replace(
|
|
"status_display = \"detailed\"",
|
|
"status_display = \"symbol\"",
|
|
);
|
|
assert_eq!(
|
|
Profile::parse(&configured).unwrap().status_display(),
|
|
fader_config::StatusDisplayMode::Symbol
|
|
);
|
|
assert!(
|
|
Profile::parse(
|
|
&configured.replace("status_display = \"symbol\"", "status_display = \"raw\"")
|
|
)
|
|
.is_err()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn runtime_poll_interval_defaults_and_validates_safe_overrides() {
|
|
let without_runtime = EXAMPLE.replace("[runtime]\npoll_interval_ms = 10\n\n", "");
|
|
let default = Profile::parse(&without_runtime).expect("omitted runtime settings must validate");
|
|
assert_eq!(default.poll_interval(), Duration::from_millis(10));
|
|
|
|
let configured = EXAMPLE.replace("poll_interval_ms = 10", "poll_interval_ms = 4");
|
|
let configured = Profile::parse(&configured).expect("poll override must validate");
|
|
assert_eq!(configured.poll_interval(), Duration::from_millis(4));
|
|
assert_eq!(
|
|
Profile::parse(&configured.to_toml().unwrap())
|
|
.unwrap()
|
|
.poll_interval(),
|
|
Duration::from_millis(4)
|
|
);
|
|
|
|
for value in [0, 101] {
|
|
let invalid = EXAMPLE.replace(
|
|
"poll_interval_ms = 10",
|
|
&format!("poll_interval_ms = {value}"),
|
|
);
|
|
assert_error_contains(Profile::parse(&invalid), "runtime.poll_interval_ms");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn arch_pipewire_profile_matches_reported_qobuz_metadata() {
|
|
let profile = Profile::parse(ARCH_PIPEWIRE).expect("Arch profile must validate");
|
|
let music = profile.applications()[0]
|
|
.matcher()
|
|
.expect("validated music selector compiles");
|
|
assert!(
|
|
music.matches(
|
|
&StreamMetadata::new()
|
|
.with_application_name("PipeWire ALSA [qbz]")
|
|
.with_media_name("ALSA Playback")
|
|
)
|
|
);
|
|
assert!(!music.matches(&StreamMetadata::new().with_application_name("Firefox")));
|
|
|
|
let browser = profile.applications()[1]
|
|
.matcher()
|
|
.expect("validated browser selector compiles");
|
|
assert!(
|
|
browser.matches(
|
|
&StreamMetadata::new()
|
|
.with_application_name("Waterfox")
|
|
.with_process_binary("waterfox-bin")
|
|
)
|
|
);
|
|
|
|
let surface = &profile.surfaces()[0];
|
|
assert_eq!(surface.id(), "apc-mini-apc-mini-midi-1");
|
|
assert_eq!(surface.input_port(), "APC MINI:APC MINI MIDI 1");
|
|
assert_eq!(surface.usb_vendor_id(), Some("09e8"));
|
|
assert_eq!(surface.usb_product_id(), Some("0028"));
|
|
assert_eq!(surface.usb_serial(), Some("Ver00.10"));
|
|
assert_eq!(profile.faders().len(), 5);
|
|
assert_eq!(profile.bindings().len(), 4);
|
|
assert_eq!(profile.bindings()[0].number(), 48);
|
|
assert_eq!(profile.bindings()[3].number(), 89);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_unknown_fields_versions_and_bad_ids_with_context() {
|
|
let unknown = EXAMPLE.replace("schema_version = 1", "schema_version = 1\nsurprise = true");
|
|
assert_error_contains(Profile::parse(&unknown), "surprise");
|
|
|
|
let version = EXAMPLE.replace("schema_version = 1", "schema_version = 7");
|
|
assert_error_contains(Profile::parse(&version), "schema_version");
|
|
|
|
let bad_id = EXAMPLE.replace("id = \"desktop\"", "id = \"bad/profile\"");
|
|
assert_error_contains(Profile::parse(&bad_id), "profile.id");
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_dangling_references_and_duplicate_physical_bindings() {
|
|
let missing_member = EXAMPLE.replace(
|
|
"members = [\"music\", \"browser\"]",
|
|
"members = [\"music\", \"missing\"]",
|
|
);
|
|
assert_error_contains(Profile::parse(&missing_member), "groups[0].members[1]");
|
|
|
|
let missing_target = EXAMPLE.replace("id = \"media\" }", "id = \"missing\" }");
|
|
assert_error_contains(Profile::parse(&missing_target), "faders[0].target.id");
|
|
|
|
let unknown_output = ARCH_PIPEWIRE.replace(
|
|
"target = { kind = \"output\", id = \"default\" }",
|
|
"target = { kind = \"output\", id = \"headphones\" }",
|
|
);
|
|
assert_error_contains(Profile::parse(&unknown_output), "faders[3].target.id");
|
|
|
|
let duplicate = EXAMPLE.replace(
|
|
"kind = \"note\", channel = 1, number = 32",
|
|
"kind = \"cc\", channel = 1, number = 0",
|
|
);
|
|
assert_error_contains(Profile::parse(&duplicate), "bindings[1].message");
|
|
}
|
|
|
|
#[test]
|
|
fn relative_groups_are_reserved_but_not_runtime_selectable() {
|
|
let relative = EXAMPLE.replace("volume_mode = \"absolute\"", "volume_mode = \"relative\"");
|
|
assert_error_contains(Profile::parse(&relative), "groups[0].volume_mode");
|
|
assert_error_contains(Profile::parse(&relative), "use absolute");
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_incomplete_or_malformed_usb_surface_hints() {
|
|
let missing_product = ARCH_PIPEWIRE.replace("usb_product_id = \"0028\"\n", "");
|
|
assert_error_contains(Profile::parse(&missing_product), "provided together");
|
|
|
|
let malformed_vendor =
|
|
ARCH_PIPEWIRE.replace("usb_vendor_id = \"09e8\"", "usb_vendor_id = \"9e8\"");
|
|
assert_error_contains(Profile::parse(&malformed_vendor), "four hexadecimal digits");
|
|
}
|
|
|
|
#[test]
|
|
fn validates_all_midi_data_ranges_and_action_capabilities() {
|
|
for (needle, replacement, path) in [
|
|
(
|
|
"channel = 1, number = 0",
|
|
"channel = 0, number = 0",
|
|
"channel",
|
|
),
|
|
(
|
|
"channel = 1, number = 0",
|
|
"channel = 17, number = 0",
|
|
"channel",
|
|
),
|
|
(
|
|
"channel = 1, number = 32",
|
|
"channel = 1, number = 128",
|
|
"number",
|
|
),
|
|
("on = 127, off = 0", "on = 128, off = 0", "feedback.on"),
|
|
("on = 127, off = 0", "on = 127, off = 128", "feedback.off"),
|
|
] {
|
|
assert_error_contains(
|
|
Profile::parse(&EXAMPLE.replacen(needle, replacement, 1)),
|
|
path,
|
|
);
|
|
}
|
|
|
|
let volume_on_note = EXAMPLE.replace(
|
|
"action = \"volume\"\nmessage = { kind = \"cc\"",
|
|
"action = \"volume\"\nmessage = { kind = \"note\"",
|
|
);
|
|
assert_error_contains(Profile::parse(&volume_on_note), "requires a cc message");
|
|
|
|
let feedback_on_input_only =
|
|
EXAMPLE.replace("output_port = \"Example Controller MIDI 1\"\n", "");
|
|
assert_error_contains(Profile::parse(&feedback_on_input_only), "output_port");
|
|
}
|
|
|
|
#[test]
|
|
fn device_and_state_documents_are_strict_and_validated() {
|
|
let device = DeviceProfile::parse(
|
|
r#"
|
|
schema_version = 1
|
|
id = "nano"
|
|
label = "Nano controller"
|
|
[capabilities]
|
|
mute_led = true
|
|
value_display = false
|
|
motorized_fader = false
|
|
"#,
|
|
)
|
|
.expect("device profile must validate");
|
|
assert_eq!(device.id().as_str(), "nano");
|
|
assert_eq!(
|
|
device
|
|
.to_toml()
|
|
.and_then(|text| DeviceProfile::parse(&text))
|
|
.unwrap(),
|
|
device
|
|
);
|
|
|
|
let state = PersistedState::parse(
|
|
r#"
|
|
schema_version = 1
|
|
|
|
[[profiles]]
|
|
id = "desktop"
|
|
last_view = "mixer"
|
|
|
|
[[profiles.observed]]
|
|
object = "music"
|
|
gain = [0.5, 0.25]
|
|
muted = false
|
|
|
|
[[profiles.desired]]
|
|
object = "music"
|
|
gain = 0.7
|
|
muted = true
|
|
|
|
[[profiles.assignments]]
|
|
surface = "desk-left"
|
|
input_port = "hw:1"
|
|
output_port = "hw:1"
|
|
"#,
|
|
)
|
|
.expect("state must validate");
|
|
assert_eq!(state.profiles().len(), 1);
|
|
assert_eq!(
|
|
state
|
|
.to_toml()
|
|
.and_then(|text| PersistedState::parse(&text))
|
|
.unwrap(),
|
|
state
|
|
);
|
|
|
|
assert_error_contains(
|
|
PersistedState::parse("schema_version = 1\npending_operation = 4"),
|
|
"pending_operation",
|
|
);
|
|
assert_error_contains(
|
|
PersistedState::parse("schema_version = 2\nprofiles = []"),
|
|
"schema_version",
|
|
);
|
|
}
|
|
|
|
fn assert_error_contains<T>(result: Result<T, fader_config::ConfigError>, needle: &str) {
|
|
let error = result
|
|
.err()
|
|
.expect("document should be rejected")
|
|
.to_string();
|
|
assert!(
|
|
error.contains(needle),
|
|
"expected {error:?} to contain {needle:?}"
|
|
);
|
|
}
|