250 lines
8.1 KiB
Rust
250 lines
8.1 KiB
Rust
use fader_config::{DeviceProfile, PersistedState, Profile, StartupPolicy};
|
|
use fader_core::{StreamMetadata, VolumeMode};
|
|
|
|
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 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 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");
|
|
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(), 4);
|
|
assert_eq!(profile.bindings().len(), 8);
|
|
assert_eq!(profile.bindings()[0].number(), 48);
|
|
assert_eq!(profile.bindings()[7].number(), 98);
|
|
}
|
|
|
|
#[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 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:?}"
|
|
);
|
|
}
|