use fader_config::{ Action, MessageKind, MixerLayoutEntry, ProfileDocument, SelectorDraft, SelectorField, SelectorPattern, TargetKind, }; const PROFILE: &str = include_str!("../../examples/profiles/desktop.toml"); #[test] fn draft_creates_an_application_group_and_fader_as_valid_toml() { let mut draft = ProfileDocument::parse(PROFILE).unwrap(); draft .add_application( "chat", "Chat", &[SelectorDraft { field: SelectorField::ProcessBinary, pattern: SelectorPattern::Exact("signal-desktop".to_owned()), }], ) .unwrap(); draft .add_group("communications", "Communications", &["chat", "browser"]) .unwrap(); draft .add_fader( "communications-fader", TargetKind::Group, "communications", true, 0.02, ) .unwrap(); let profile = draft.preview().unwrap(); assert!( profile .applications() .iter() .any(|item| item.id() == "chat") ); assert!( profile .groups() .iter() .any(|item| item.id() == "communications") ); assert!( profile .faders() .iter() .any(|item| item.id() == "communications-fader") ); ProfileDocument::parse(&draft.finish().unwrap()).unwrap(); } #[test] fn invalid_edits_and_referenced_deletes_roll_back_the_draft() { let mut draft = ProfileDocument::parse(PROFILE).unwrap(); let original = draft.source(); let invalid = draft .add_application( "broken", "Broken", &[SelectorDraft { field: SelectorField::ApplicationName, pattern: SelectorPattern::Regex("(".to_owned()), }], ) .unwrap_err(); assert!(invalid.to_string().contains("regex")); assert_eq!(draft.source(), original); let referenced = draft.remove_application("browser").unwrap_err(); assert!(referenced.to_string().contains("unknown application")); assert_eq!(draft.source(), original); } #[test] fn existing_applications_groups_and_faders_can_be_edited_and_deleted() { let mut draft = ProfileDocument::parse(PROFILE).unwrap(); draft .set_application( "browser", "Web", &[SelectorDraft { field: SelectorField::ProcessBinary, pattern: SelectorPattern::Exact("waterfox-bin".to_owned()), }], ) .unwrap(); draft.set_group("media", "Players", &["browser"]).unwrap(); draft .set_fader( "media-fader", TargetKind::Application, "browser", false, 0.05, ) .unwrap(); let profile = draft.preview().unwrap(); assert_eq!(profile.applications()[1].label(), "Web"); assert_eq!(profile.groups()[0].members(), &["browser"]); assert_eq!(profile.faders()[0].target_kind(), TargetKind::Application); assert!(!profile.faders()[0].pickup()); draft.remove_group("media").unwrap(); draft.remove_fader("media-fader").unwrap_err(); assert!(draft.source().contains("media-fader")); } #[test] fn draft_adds_bindings_and_rejects_duplicate_physical_controls() { let mut draft = ProfileDocument::parse(PROFILE).unwrap(); draft .add_binding( "desk-left", "media-fader", Action::Volume, MessageKind::Cc, 1, 49, ) .unwrap(); let before_duplicate = draft.source(); let error = draft .add_binding( "desk-left", "media-fader", Action::ToggleMute, MessageKind::Cc, 1, 49, ) .unwrap_err(); assert!(error.to_string().contains("duplicate physical binding")); assert_eq!(draft.source(), before_duplicate); } #[test] fn mixer_layout_draft_reorders_separators_and_retains_hidden_faders() { let source = PROFILE.replacen( "[[bindings]]", "# separator comment must move with its entry\n[[mixer_layout]]\nkind = \"separator\"\nid = \"outputs\"\nlabel = \"Outputs\"\n\n[[bindings]]", 1, ); let mut draft = ProfileDocument::parse(&source).unwrap(); draft .set_fader_hidden("media-fader", true) .expect("fader must be hidden without deletion"); draft .move_layout_entry("media-fader", 0) .expect("hidden fader must remain reorderable"); draft .set_separator_label("outputs", "Main outputs") .expect("separator label must be editable"); let profile = draft.preview().unwrap(); assert_eq!( profile.mixer_layout(), [ MixerLayoutEntry::Fader { fader: "media-fader".to_owned(), hidden: true, }, MixerLayoutEntry::Separator { id: "outputs".to_owned(), label: "Main outputs".to_owned(), }, ] ); assert_eq!(profile.faders().len(), 1, "hiding must not delete routing"); assert!( draft .source() .contains("# separator comment must move with its entry") ); draft.remove_separator("outputs").unwrap(); draft.set_fader_hidden("media-fader", false).unwrap(); assert_eq!( draft.preview().unwrap().mixer_layout(), [MixerLayoutEntry::Fader { fader: "media-fader".to_owned(), hidden: false, }] ); }