174 lines
5.2 KiB
Rust
174 lines
5.2 KiB
Rust
use fader_config::{DeviceProfile, PersistedState, Profile, StartupPolicy};
|
|
|
|
const EXAMPLE: &str = include_str!("../../examples/profiles/desktop.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 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 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 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:?}"
|
|
);
|
|
}
|