33//! Test-Driven Development tests for the keybindings system.
44
55use keyring_cli:: tui:: keybindings:: { parse_shortcut, Action , KeyBinding , KeyBindingManager } ;
6+ use serial_test:: serial;
67
8+ #[ serial]
79#[ test]
810fn test_parse_ctrl_char ( ) {
911 // Test parsing "Ctrl+N" into KeyEvent
@@ -17,6 +19,7 @@ fn test_parse_ctrl_char() {
1719 . contains( crossterm:: event:: KeyModifiers :: CONTROL ) ) ;
1820}
1921
22+ #[ serial]
2023#[ test]
2124fn test_parse_function_key ( ) {
2225 let result = parse_shortcut ( "F5" ) ;
@@ -25,6 +28,7 @@ fn test_parse_function_key() {
2528 assert_eq ! ( event. code, crossterm:: event:: KeyCode :: F ( 5 ) ) ;
2629}
2730
31+ #[ serial]
2832#[ test]
2933fn test_parse_ctrl_shift_char ( ) {
3034 let result = parse_shortcut ( "Ctrl+Shift+N" ) ;
@@ -39,12 +43,14 @@ fn test_parse_ctrl_shift_char() {
3943 . contains( crossterm:: event:: KeyModifiers :: SHIFT ) ) ;
4044}
4145
46+ #[ serial]
4247#[ test]
4348fn test_parse_invalid_shortcut ( ) {
4449 let result = parse_shortcut ( "Invalid" ) ;
4550 assert ! ( result. is_err( ) ) ;
4651}
4752
53+ #[ serial]
4854#[ test]
4955fn test_action_display ( ) {
5056 // Test that actions can be displayed for help
@@ -53,6 +59,7 @@ fn test_action_display() {
5359 assert_eq ! ( format!( "{}" , Action :: Quit ) , "Quit" ) ;
5460}
5561
62+ #[ serial]
5663#[ test]
5764fn test_default_keybindings ( ) {
5865 use crossterm:: event:: { KeyCode , KeyEvent , KeyModifiers } ;
@@ -70,6 +77,7 @@ fn test_default_keybindings() {
7077 assert_eq ! ( manager. get_action( & ctrl_q) , Some ( Action :: Quit ) ) ;
7178}
7279
80+ #[ serial]
7381#[ test]
7482fn test_keybinding_from_yaml ( ) {
7583 use serde_yaml;
@@ -85,6 +93,7 @@ shortcuts:
8593 assert ! ( binding. is_ok( ) ) ;
8694}
8795
96+ #[ serial]
8897#[ test]
8998fn test_conflict_detection ( ) {
9099 use serde_yaml;
@@ -104,6 +113,7 @@ shortcuts:
104113
105114// Additional comprehensive tests
106115
116+ #[ serial]
107117#[ test]
108118fn test_all_default_actions_have_bindings ( ) {
109119 let manager = KeyBindingManager :: new ( ) ;
@@ -134,19 +144,32 @@ fn test_all_default_actions_have_bindings() {
134144 }
135145}
136146
147+ #[ serial]
137148#[ test]
138149fn test_manager_get_key_for_action ( ) {
139- use crossterm:: event:: KeyCode ;
150+ use crossterm:: event:: { KeyCode , KeyModifiers } ;
151+
152+ // Ensure clean state by removing any existing config file
153+ // that might have been created by other tests
154+ if let Some ( config_dir) = dirs:: config_dir ( ) {
155+ let config_path = config_dir. join ( "open-keyring" ) . join ( "keybindings.yaml" ) ;
156+ let _ = std:: fs:: remove_file ( & config_path) ;
157+ }
140158
141159 let manager = KeyBindingManager :: new ( ) ;
142160
161+ // Action::New is bound to Ctrl+N (Char('n') with CONTROL modifier)
143162 let new_key = manager. get_key ( Action :: New ) ;
144163 assert_eq ! ( new_key. unwrap( ) . code, KeyCode :: Char ( 'n' ) ) ;
164+ assert ! ( new_key. unwrap( ) . modifiers. contains( KeyModifiers :: CONTROL ) ) ;
145165
166+ // Action::Help is bound to F1 (not Ctrl+H)
146167 let help_key = manager. get_key ( Action :: Help ) ;
147- assert_eq ! ( help_key. unwrap( ) . code, KeyCode :: Char ( 'h' ) ) ;
168+ assert_eq ! ( help_key. unwrap( ) . code, KeyCode :: F ( 1 ) ) ;
169+ assert_eq ! ( help_key. unwrap( ) . modifiers, KeyModifiers :: empty( ) ) ;
148170}
149171
172+ #[ serial]
150173#[ test]
151174fn test_manager_format_key ( ) {
152175 use crossterm:: event:: { KeyCode , KeyEvent , KeyModifiers } ;
@@ -164,6 +187,7 @@ fn test_manager_format_key() {
164187 assert_eq ! ( KeyBindingManager :: format_key( & f5) , "F5" ) ;
165188}
166189
190+ #[ serial]
167191#[ test]
168192fn test_parse_alt_key ( ) {
169193 let result = parse_shortcut ( "Alt+T" ) ;
@@ -175,6 +199,7 @@ fn test_parse_alt_key() {
175199 . contains( crossterm:: event:: KeyModifiers :: ALT ) ) ;
176200}
177201
202+ #[ serial]
178203#[ test]
179204fn test_parse_ctrl_alt_key ( ) {
180205 let result = parse_shortcut ( "Ctrl+Alt+Delete" ) ;
@@ -188,18 +213,21 @@ fn test_parse_ctrl_alt_key() {
188213 . contains( crossterm:: event:: KeyModifiers :: ALT ) ) ;
189214}
190215
216+ #[ serial]
191217#[ test]
192218fn test_parse_empty_input ( ) {
193219 let result = parse_shortcut ( "" ) ;
194220 assert ! ( result. is_err( ) ) ;
195221}
196222
223+ #[ serial]
197224#[ test]
198225fn test_parse_whitespace_only ( ) {
199226 let result = parse_shortcut ( " " ) ;
200227 assert ! ( result. is_err( ) ) ;
201228}
202229
230+ #[ serial]
203231#[ test]
204232fn test_parse_special_keys ( ) {
205233 assert_eq ! (
@@ -224,6 +252,7 @@ fn test_parse_special_keys() {
224252 ) ;
225253}
226254
255+ #[ serial]
227256#[ test]
228257fn test_parse_navigation_keys ( ) {
229258 assert_eq ! (
@@ -244,6 +273,7 @@ fn test_parse_navigation_keys() {
244273 ) ;
245274}
246275
276+ #[ serial]
247277#[ test]
248278fn test_parse_function_keys_f1_to_f12 ( ) {
249279 for i in 1 ..=12 {
@@ -253,6 +283,7 @@ fn test_parse_function_keys_f1_to_f12() {
253283 }
254284}
255285
286+ #[ serial]
256287#[ test]
257288fn test_parse_case_insensitive_modifiers ( ) {
258289 let ctrl_lower = parse_shortcut ( "ctrl+n" ) ;
@@ -267,6 +298,7 @@ fn test_parse_case_insensitive_modifiers() {
267298 assert_eq ! ( ctrl_lower. unwrap( ) , ctrl_upper. unwrap( ) ) ;
268299}
269300
301+ #[ serial]
270302#[ test]
271303fn test_action_command_names ( ) {
272304 assert_eq ! ( Action :: New . command_name( ) , "/new" ) ;
@@ -275,13 +307,15 @@ fn test_action_command_names() {
275307 assert_eq ! ( Action :: Help . command_name( ) , "/help" ) ;
276308}
277309
310+ #[ serial]
278311#[ test]
279312fn test_action_descriptions ( ) {
280313 assert ! ( !Action :: New . description( ) . is_empty( ) ) ;
281314 assert ! ( !Action :: Quit . description( ) . is_empty( ) ) ;
282315 assert ! ( !Action :: Help . description( ) . is_empty( ) ) ;
283316}
284317
318+ #[ serial]
285319#[ test]
286320fn test_keybinding_default_creation ( ) {
287321 let binding = KeyBinding :: new ( ) ;
@@ -290,6 +324,7 @@ fn test_keybinding_default_creation() {
290324 assert_eq ! ( binding. shortcuts. get( "quit" ) , Some ( & "Ctrl+Q" . to_string( ) ) ) ;
291325}
292326
327+ #[ serial]
293328#[ test]
294329fn test_unknown_shortcut_returns_none ( ) {
295330 use crossterm:: event:: { KeyCode , KeyEvent , KeyModifiers } ;
@@ -299,6 +334,7 @@ fn test_unknown_shortcut_returns_none() {
299334 assert_eq ! ( manager. get_action( & unknown_key) , None ) ;
300335}
301336
337+ #[ serial]
302338#[ test]
303339fn test_all_bindings_coverage ( ) {
304340 let manager = KeyBindingManager :: new ( ) ;
0 commit comments