(ns tui.input-test "Unit tests for input parsing and key matching." (:require [clojure.test :refer [deftest testing is]] [tui.input :as input])) ;; === Key Matching Tests === (deftest key-match-character-test (testing "matches single character keys" (is (input/key-match? [:key {:char \q}] "q")) (is (input/key-match? [:key {:char \a}] "a")) (is (input/key-match? [:key {:char \1}] "1"))) (testing "does not match different characters" (is (not (input/key-match? [:key {:char \q}] "a"))) (is (not (input/key-match? [:key {:char \x}] "y")))) (testing "does not match ctrl+char as plain char" (is (not (input/key-match? [:key {:ctrl true :char \c}] "c")))) (testing "does not match alt+char as plain char" (is (not (input/key-match? [:key {:alt true :char \x}] "x"))))) (deftest key-match-special-keys-test (testing "matches special keys by keyword" (is (input/key-match? [:key :enter] :enter)) (is (input/key-match? [:key :escape] :escape)) (is (input/key-match? [:key :backspace] :backspace)) (is (input/key-match? [:key :tab] :tab))) (testing "matches arrow keys" (is (input/key-match? [:key :up] :up)) (is (input/key-match? [:key :down] :down)) (is (input/key-match? [:key :left] :left)) (is (input/key-match? [:key :right] :right))) (testing "matches function keys" (is (input/key-match? [:key :f1] :f1)) (is (input/key-match? [:key :f12] :f12))) (testing "does not match wrong special keys" (is (not (input/key-match? [:key :up] :down))) (is (not (input/key-match? [:key :enter] :escape))))) (deftest key-match-ctrl-combo-test (testing "matches ctrl+char combinations" (is (input/key-match? [:key {:ctrl true :char \c}] [:ctrl \c])) (is (input/key-match? [:key {:ctrl true :char \x}] [:ctrl \x])) (is (input/key-match? [:key {:ctrl true :char \z}] [:ctrl \z]))) (testing "does not match wrong ctrl combinations" (is (not (input/key-match? [:key {:ctrl true :char \c}] [:ctrl \x]))) (is (not (input/key-match? [:key {:char \c}] [:ctrl \c]))))) (deftest key-match-alt-combo-test (testing "matches alt+char combinations" (is (input/key-match? [:key {:alt true :char \x}] [:alt \x])) (is (input/key-match? [:key {:alt true :char \a}] [:alt \a]))) (testing "does not match wrong alt combinations" (is (not (input/key-match? [:key {:alt true :char \x}] [:alt \y]))) (is (not (input/key-match? [:key {:char \x}] [:alt \x]))))) (deftest key-match-non-key-messages-test (testing "returns nil for non-key messages" (is (nil? (input/key-match? [:tick 123] "q"))) (is (nil? (input/key-match? [:quit] :enter))) (is (nil? (input/key-match? nil "a"))))) ;; === Key to String Tests === (deftest key->str-special-keys-test (testing "converts special keys to strings" (is (= "enter" (input/key->str [:key :enter]))) (is (= "escape" (input/key->str [:key :escape]))) (is (= "up" (input/key->str [:key :up]))) (is (= "f1" (input/key->str [:key :f1]))))) (deftest key->str-character-keys-test (testing "converts character keys to strings" (is (= "q" (input/key->str [:key {:char \q}]))) (is (= "a" (input/key->str [:key {:char \a}]))))) (deftest key->str-modifier-keys-test (testing "converts ctrl combinations to strings" (is (= "ctrl+c" (input/key->str [:key {:ctrl true :char \c}])))) (testing "converts alt combinations to strings" (is (= "alt+x" (input/key->str [:key {:alt true :char \x}]))))) (deftest key->str-non-key-messages-test (testing "returns nil for non-key messages" (is (nil? (input/key->str [:tick 123]))) (is (nil? (input/key->str nil)))))