hex/src/rune_ui/keyboard_model_tests.rs
Evgenii Perminov 6a5519aae1
Switch the speech model and language with the macOS keyboard input source (#216)
Opt-in macOS setting: each enabled input source maps to Don't switch, or to a downloaded model and a language it supports. Changing the source applies the mapping after a 300 ms debounce through the existing installed-only preparation. Input sources are read only while the setting is on.

Fixes #183.

Co-authored-by: Evgenii Perminov <evperminov@ozon.ru>
Co-authored-by: Dax Raad <d@ironbay.co>
2026-10-28 16:20:24 -04:00

106 lines
3.7 KiB
Rust

use super::*;
use rune::text::TextSystem;
use rune::{Env, Runtime};
fn runtime(scene: &str) -> Runtime<Ui> {
let mut state = Ui::with(AppSettings::default(), Service::Fixture(Vec::new()));
SCENES.iter().find(|(name, _)| *name == scene).unwrap().1(&mut state);
if scene == "portal" {
state.platform.caps = platform::Capabilities::LINUX;
}
let text = TextSystem::new(rune_theme_opencode::FONTS, rune_theme_opencode::FAMILIES);
let mut rt = Runtime::with_text(
state,
view,
(980.0, 1720.0),
Env {
system_dark: true,
scale: 1.0,
},
text,
);
rt.settle_all();
rt.frame(0.0);
rt
}
fn tap(rt: &mut Runtime<Ui>, key: &str) {
let (x, y) = rt.find(key).unwrap_or_else(|| panic!("no element {key}"));
rt.pointer_down(x, y);
rt.pointer_up(x, y);
rt.settle_all();
rt.frame(0.0);
}
#[test]
fn keyboard_model_switching_is_a_macos_only_saved_opt_in() {
let mut rt = runtime("mac-settings");
assert!(!rt.state.settings.keyboard_model_switching.enabled);
tap(&mut rt, "keyboard-model-switching");
assert!(rt.state.settings.keyboard_model_switching.enabled);
assert_eq!(rt.state.saved, 1);
tap(&mut rt, "keyboard-model-switching");
assert!(!rt.state.settings.keyboard_model_switching.enabled);
for scene in ["portal", "windows-settings"] {
let rt = runtime(scene);
assert!(rt.find("keyboard-model-switching").is_none());
}
}
#[test]
fn keyboard_model_switching_maps_enabled_sources_to_an_installed_model_and_language() {
use crate::app_settings::KeyboardModelChoice;
let mut rt = runtime("mac-settings");
rt.state.platform.input_sources = vec![crate::keyboard_model_switching::InputSource {
id: "com.apple.keylayout.Spanish".into(),
name: "Spanish".into(),
languages: vec!["es".into()],
}];
rt.state.picker.installed = Some(vec![TranscriptionModelId::ParakeetV3]);
rt.invalidate();
rt.frame(0.0);
tap(&mut rt, "keyboard-model-switching");
assert!(rt.find("input-source-language0").is_none());
tap(&mut rt, "input-source-model0-trigger");
tap(&mut rt, "input-source-model0-item1");
let mapping = |rt: &Runtime<Ui>| {
rt.state
.settings
.keyboard_model_switching
.mappings
.get("com.apple.keylayout.Spanish")
.cloned()
};
// A new mapping starts in the source's own language.
assert_eq!(
mapping(&rt),
Some(KeyboardModelChoice {
model: TranscriptionModelId::ParakeetV3,
language: "es".into(),
})
);
let german = crate::transcription_models::definition(TranscriptionModelId::ParakeetV3)
.language_choices()
.iter()
.position(|code| *code == "de")
.unwrap();
tap(&mut rt, "input-source-language0-trigger");
tap(&mut rt, &format!("input-source-language0-item{german}"));
assert_eq!(mapping(&rt).unwrap().language, "de");
tap(&mut rt, "input-source-model0-trigger");
tap(&mut rt, "input-source-model0-item0");
assert_eq!(mapping(&rt), None);
assert!(rt.find("input-source-language0").is_none());
}
#[test]
fn a_one_language_model_shows_its_language_fixed() {
let rt = runtime("mac-input-source-models");
// ABC maps to Parakeet Unified English, which has only English.
assert!(rt.find("input-source-language0").is_some());
assert!(rt.find("input-source-language0-trigger").is_none());
// Russian maps to Parakeet v3, which offers a choice.
assert!(rt.find("input-source-language1-trigger").is_some());
// Spanish doesn't switch, so it has no language.
assert!(rt.find("input-source-language2").is_none());
}