Key handling is state aware

By forwarding all `Key`s to the agent and allow it to decide what should
be done, instead of converting the `Key` to a `Control` and then decide
what should be done
This commit is contained in:
Alexander Heldt
2025-12-25 17:49:34 +01:00
parent 9d54495a4b
commit 0ef94d7c89
5 changed files with 107 additions and 127 deletions

View File

@@ -1,7 +1,7 @@
import gleam/list
import gleeunit
import musicplayer/control.{type Control}
import musicplayer/control.{type Control, type Mode}
import musicplayer/input/key.{type Key, Char}
pub fn main() -> Nil {
@@ -9,16 +9,24 @@ pub fn main() -> Nil {
}
type TestCase {
TestCase(key: Key, expected: Result(Control, Nil))
TestCase(key: Key, mode: Mode, expected: Result(Control, Nil))
}
pub fn control_from_key_test() {
let test_cases = [
TestCase(Char(" "), Ok(control.TogglePlayPause)),
TestCase(Char("q"), Ok(control.Exit)),
let idle_tests = [
TestCase(Char(" "), control.Idle, Ok(control.TogglePlayPause)),
TestCase(Char("/"), control.Idle, Ok(control.Search("", True))),
TestCase(Char("q"), control.Idle, Ok(control.Exit)),
]
list.each(test_cases, fn(tc) {
assert tc.expected == control.from_key(tc.key)
let search_tests = [
TestCase(Char("a"), control.Searching(""), Ok(control.Search("a", True))),
TestCase(Char("b"), control.Searching("a"), Ok(control.Search("ab", True))),
]
let test_cases = [idle_tests, search_tests]
list.each(list.flatten(test_cases), fn(tc) {
assert tc.expected == control.from_key(tc.key, tc.mode)
})
}