napali 0.1.1

TUI interface for Integrated Reasoning's accelerated solver API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
use crate::{action::scene::Scene, action::Action};
use color_eyre::eyre::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use derive_deref::{Deref, DerefMut};
use ratatui::style::{Color, Modifier, Style};
use serde::{de::Deserializer, Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};

const CONFIG: &str = "
{
  \"keybindings\": {
    \"Home\": {
      \"<H>\": \"ChangeScene(Home)\",
      \"<S>\": \"ChangeScene(Session)\",
      \"<I>\": \"ChangeScene(Internals)\",
      \"<A>\": \"ChangeView(A)\",
      \"<L>\": \"ChangeView(L)\",
      \"<R>\": \"ChangeView(R)\",
      \"<E>\": \"ChangeView(Prompt)\",
      \"<?>\": \"ToggleOverlay(UsageInfo)\",
      \"<q>\": \"Quit\",
      \"<Ctrl-d>\": \"Quit\",
      \"<Ctrl-c>\": \"Quit\",
      \"<Ctrl-z>\": \"Suspend\"
    },
    \"Session\": {
      \"<H>\": \"ChangeScene(Home)\",
      \"<S>\": \"ChangeScene(Session)\",
      \"<I>\": \"ChangeScene(Internals)\",
      \"<A>\": \"ChangeView(A)\",
      \"<L>\": \"ChangeView(L)\",
      \"<R>\": \"ChangeView(R)\",
      \"<.>\": \"ChangeView(Prompt)\",
      \"<?>\": \"ToggleOverlay(UsageInfo)\",
      \"<q>\": \"Quit\",
      \"<Ctrl-d>\": \"Quit\",
      \"<Ctrl-c>\": \"Quit\",
      \"<Ctrl-z>\": \"Suspend\"
    },
    \"Internals\": {
      \"<H>\": \"ChangeScene(Home)\",
      \"<S>\": \"ChangeScene(Session)\",
      \"<I>\": \"ChangeScene(Internals)\",
      \"<A>\": \"ChangeView(A)\",
      \"<L>\": \"ChangeView(L)\",
      \"<R>\": \"ChangeView(R)\",
      \"<?>\": \"ToggleOverlay(UsageInfo)\",
      \"<q>\": \"Quit\",
      \"<Ctrl-d>\": \"Quit\",
      \"<Ctrl-c>\": \"Quit\",
      \"<Ctrl-z>\": \"Suspend\"
    },
  }
}";

/// Defines the application configuration properties.
///
/// This structure holds paths for data and configuration directories.
#[derive(
  Default, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize,
)]
pub struct AppConfig {
  #[serde(default)]
  pub _data_dir: PathBuf,
  #[serde(default)]
  pub _config_dir: PathBuf,
}

/// Represents the main configuration for the application.
///
/// Includes application configuration, keybindings, and styles.
#[derive(Default, Debug, Clone, Deserialize)]
pub struct Config {
  #[serde(default, flatten)]
  pub config: AppConfig,
  #[serde(default)]
  pub keybindings: KeyBindings,
  #[serde(default)]
  pub styles: Styles,
}

impl Config {
  /// Constructs a new configuration instance.
  ///
  /// Attempts to load configuration from various file formats and merges with default config.
  ///
  /// # Returns
  ///
  /// `Result<Self, config::ConfigError>` - The configuration instance or an error.
  pub fn new() -> Result<Self, config::ConfigError> {
    let default_config: Config = json5::from_str(CONFIG).unwrap();
    let data_dir = crate::utils::get_data_dir();
    let config_dir = crate::utils::get_config_dir();
    let mut builder = config::Config::builder()
      .set_default("_data_dir", data_dir.to_str().unwrap())?
      .set_default("_config_dir", config_dir.to_str().unwrap())?;

    let config_files = [
      ("config.json5", config::FileFormat::Json5),
      ("config.json", config::FileFormat::Json),
      ("config.yaml", config::FileFormat::Yaml),
      ("config.toml", config::FileFormat::Toml),
      ("config.ini", config::FileFormat::Ini),
    ];
    let mut found_config = false;
    for (file, format) in &config_files {
      builder = builder.add_source(
        config::File::from(config_dir.join(file))
          .format(*format)
          .required(false),
      );
      if config_dir.join(file).exists() {
        found_config = true;
      }
    }
    if !found_config {
      log::error!(
        "No configuration file found. Application may not behave as expected"
      );
    }

    let mut cfg: Self = builder.build()?.try_deserialize()?;

    for (scene, default_bindings) in &*default_config.keybindings {
      let user_bindings = cfg.keybindings.entry(*scene).or_default();
      for (key, cmd) in default_bindings {
        user_bindings
          .entry(key.clone())
          .or_insert_with(|| cmd.clone());
      }
    }
    for (scene, default_styles) in &*default_config.styles {
      let user_styles = cfg.styles.entry(*scene).or_default();
      for (style_key, style) in default_styles {
        user_styles
          .entry(style_key.clone())
          .or_insert_with(|| *style);
      }
    }

    Ok(cfg)
  }
}

/// Custom key bindings for the application.
///
/// Maps scenes to their respective key bindings.
#[derive(Clone, Debug, Default, Deref, DerefMut)]
pub struct KeyBindings(pub HashMap<Scene, HashMap<Vec<KeyEvent>, Action>>);

impl<'de> Deserialize<'de> for KeyBindings {
  /// Custom deserialization for key bindings.
  ///
  /// Converts string representations of key sequences into actual `KeyEvent` sequences.
  ///
  /// # Parameters
  ///
  /// * `deserializer`: Deserializer to deserialize the key bindings.
  ///
  /// # Returns
  ///
  /// `Result<Self, D::Error>` - The key bindings instance or an error.
  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  where
    D: Deserializer<'de>,
  {
    let parsed_map =
      HashMap::<Scene, HashMap<String, Action>>::deserialize(deserializer)?;

    let keybindings = parsed_map
      .into_iter()
      .map(|(scene, inner_map)| {
        let converted_inner_map = inner_map
          .into_iter()
          .map(|(key_str, cmd)| (parse_key_sequence(&key_str).unwrap(), cmd))
          .collect();
        (scene, converted_inner_map)
      })
      .collect();

    Ok(KeyBindings(keybindings))
  }
}

/// Parses a string representation of a `KeyEvent`.
///
/// Converts raw string inputs to `KeyEvent` objects, handling modifiers.
///
/// # Parameters
///
/// * `raw`: The string representation of the key event.
///
/// # Returns
///
/// `Result<KeyEvent, String>` - The parsed `KeyEvent` or an error message.
fn parse_key_event(raw: &str) -> Result<KeyEvent, String> {
  let raw_lower = raw.to_ascii_lowercase();
  let (remaining, modifiers) = extract_modifiers(&raw_lower);
  parse_key_code_with_modifiers(remaining, modifiers)
}
/// Extracts key modifiers from a raw string.
///
/// Parses a raw string to identify and extract key modifiers like Ctrl, Alt, and Shift.
///
/// # Parameters
///
/// * `raw`: The raw string representing the key event with potential modifiers.
///
/// # Returns
///
/// Tuple (`&str`, `KeyModifiers`) - The remaining string after extracting modifiers,
/// and the extracted `KeyModifiers`.
fn extract_modifiers(raw: &str) -> (&str, KeyModifiers) {
  let mut modifiers = KeyModifiers::empty();
  let mut current = raw;

  loop {
    match current {
      rest if rest.starts_with("ctrl-") => {
        modifiers.insert(KeyModifiers::CONTROL);
        current = &rest[5..];
      }
      rest if rest.starts_with("alt-") => {
        modifiers.insert(KeyModifiers::ALT);
        current = &rest[4..];
      }
      rest if rest.starts_with("shift-") => {
        modifiers.insert(KeyModifiers::SHIFT);
        current = &rest[6..];
      }
      _ => break, // break out of the loop if no known prefix is detected
    };
  }

  (current, modifiers)
}

/// Parses a `KeyCode` with modifiers from a raw string.
///
/// Interprets the key code and combines it with any identified modifiers.
///
/// # Parameters
///
/// * `raw`: The string representing the key code.
/// * `modifiers`: The modifiers to be applied to the key code.
///
/// # Returns
///
/// `Result<KeyEvent, String>` - The parsed `KeyEvent` including modifiers, or an error message.
fn parse_key_code_with_modifiers(
  raw: &str,
  mut modifiers: KeyModifiers,
) -> Result<KeyEvent, String> {
  let c = match raw {
    "esc" => KeyCode::Esc,
    "enter" => KeyCode::Enter,
    "left" => KeyCode::Left,
    "right" => KeyCode::Right,
    "up" => KeyCode::Up,
    "down" => KeyCode::Down,
    "home" => KeyCode::Home,
    "end" => KeyCode::End,
    "pageup" => KeyCode::PageUp,
    "pagedown" => KeyCode::PageDown,
    "backtab" => {
      modifiers.insert(KeyModifiers::SHIFT);
      KeyCode::BackTab
    }
    "backspace" => KeyCode::Backspace,
    "delete" => KeyCode::Delete,
    "insert" => KeyCode::Insert,
    "f1" => KeyCode::F(1),
    "f2" => KeyCode::F(2),
    "f3" => KeyCode::F(3),
    "f4" => KeyCode::F(4),
    "f5" => KeyCode::F(5),
    "f6" => KeyCode::F(6),
    "f7" => KeyCode::F(7),
    "f8" => KeyCode::F(8),
    "f9" => KeyCode::F(9),
    "f10" => KeyCode::F(10),
    "f11" => KeyCode::F(11),
    "f12" => KeyCode::F(12),
    "space" => KeyCode::Char(' '),
    "hyphen" | "minus" => KeyCode::Char('-'),
    "tab" => KeyCode::Tab,
    c if c.len() == 1 => {
      let mut c = c.chars().next().unwrap();
      if modifiers.contains(KeyModifiers::SHIFT) {
        c = c.to_ascii_uppercase();
      }
      KeyCode::Char(c)
    }
    _ => return Err(format!("Unable to parse {raw}")),
  };
  Ok(KeyEvent::new(c, modifiers))
}

/// Converts a `KeyEvent` into a string representation.
///
/// This function is typically used for serializing or logging key events.
///
/// # Parameters
///
/// * `key_event`: The `KeyEvent` to convert.
///
/// # Returns
///
/// `String` - The string representation of the `KeyEvent`.
pub fn _key_event_to_string(key_event: &KeyEvent) -> String {
  let char;
  let key_code = match key_event.code {
    KeyCode::Backspace => "backspace",
    KeyCode::Enter => "enter",
    KeyCode::Left => "left",
    KeyCode::Right => "right",
    KeyCode::Up => "up",
    KeyCode::Down => "down",
    KeyCode::Home => "home",
    KeyCode::End => "end",
    KeyCode::PageUp => "pageup",
    KeyCode::PageDown => "pagedown",
    KeyCode::Tab => "tab",
    KeyCode::BackTab => "backtab",
    KeyCode::Delete => "delete",
    KeyCode::Insert => "insert",
    KeyCode::F(c) => {
      char = format!("f({c})");
      &char
    }
    KeyCode::Char(' ') => "space",
    KeyCode::Char(c) => {
      char = c.to_string();
      &char
    }
    KeyCode::Esc => "esc",
    KeyCode::Null
    | KeyCode::CapsLock
    | KeyCode::Menu
    | KeyCode::ScrollLock
    | KeyCode::Media(_)
    | KeyCode::NumLock
    | KeyCode::PrintScreen
    | KeyCode::Pause
    | KeyCode::KeypadBegin
    | KeyCode::Modifier(_) => "",
  };

  let mut modifiers = Vec::with_capacity(3);

  if key_event.modifiers.intersects(KeyModifiers::CONTROL) {
    modifiers.push("ctrl");
  }

  if key_event.modifiers.intersects(KeyModifiers::SHIFT) {
    modifiers.push("shift");
  }

  if key_event.modifiers.intersects(KeyModifiers::ALT) {
    modifiers.push("alt");
  }

  let mut key = modifiers.join("-");

  if !key.is_empty() {
    key.push('-');
  }
  key.push_str(key_code);

  key
}

/// Parses a raw string into a sequence of `KeyEvent`s.
///
/// Useful for converting user-defined key binding strings into actionable key events.
///
/// # Parameters
///
/// * `raw`: The raw string representing a sequence of key events.
///
/// # Returns
///
/// `Result<Vec<KeyEvent>, String>` - A vector of `KeyEvent`s if successful, or an error message.
pub fn parse_key_sequence(raw: &str) -> Result<Vec<KeyEvent>, String> {
  if raw.chars().filter(|c| *c == '>').count()
    != raw.chars().filter(|c| *c == '<').count()
  {
    return Err(format!("Unable to parse `{raw}`"));
  }
  let raw = if raw.contains("><") {
    raw
  } else {
    let raw = raw.strip_prefix('<').unwrap_or(raw);
    let raw = raw.strip_prefix('>').unwrap_or(raw);
    raw
  };
  let sequences = raw
    .split("><")
    .map(|seq| {
      if let Some(s) = seq.strip_prefix('<') {
        s
      } else if let Some(s) = seq.strip_suffix('>') {
        s
      } else {
        seq
      }
    })
    .collect::<Vec<_>>();

  sequences.into_iter().map(parse_key_event).collect()
}

/// Represents custom style configurations for the application.
///
/// Maps application scenes to their respective style configurations.
#[derive(Clone, Debug, Default, Deref, DerefMut)]
pub struct Styles(pub HashMap<Scene, HashMap<String, Style>>);

impl<'de> Deserialize<'de> for Styles {
  /// Custom deserialization for styles.
  ///
  /// Converts string representations of styles into `Style` objects.
  ///
  /// # Parameters
  ///
  /// * `deserializer`: Deserializer to deserialize the styles.
  ///
  /// # Returns
  ///
  /// `Result<Self, D::Error>` - The styles instance or an error.
  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  where
    D: Deserializer<'de>,
  {
    let parsed_map =
      HashMap::<Scene, HashMap<String, String>>::deserialize(deserializer)?;

    let styles = parsed_map
      .into_iter()
      .map(|(scene, inner_map)| {
        let converted_inner_map = inner_map
          .into_iter()
          .map(|(str, style)| (str, parse_style(&style)))
          .collect();
        (scene, converted_inner_map)
      })
      .collect();

    Ok(Styles(styles))
  }
}

/// Parses a style configuration line into a `Style` object.
///
/// # Parameters
///
/// * `line`: The style configuration line.
///
/// # Returns
///
/// `Style` - The parsed `Style` object.
pub fn parse_style(line: &str) -> Style {
  let (foreground, background) =
    line.split_at(line.to_lowercase().find("on ").unwrap_or(line.len()));
  let foreground = process_color_string(foreground);
  let background = process_color_string(&background.replace("on ", ""));

  let mut style = Style::default();
  if let Some(fg) = parse_color(&foreground.0) {
    style = style.fg(fg);
  }
  if let Some(bg) = parse_color(&background.0) {
    style = style.bg(bg);
  }
  style = style.add_modifier(foreground.1 | background.1);
  style
}

/// Processes a color string to extract the color and any text modifiers.
///
/// # Parameters
///
/// * `color_str`: The string representing the color and modifiers.
///
/// # Returns
///
/// Tuple (`String`, `Modifier`) - The color string and any identified modifiers.
fn process_color_string(color_str: &str) -> (String, Modifier) {
  let color = color_str
    .replace("grey", "gray")
    .replace("bright ", "")
    .replace("bold ", "")
    .replace("underline ", "")
    .replace("inverse ", "");

  let mut modifiers = Modifier::empty();
  if color_str.contains("underline") {
    modifiers |= Modifier::UNDERLINED;
  }
  if color_str.contains("bold") {
    modifiers |= Modifier::BOLD;
  }
  if color_str.contains("inverse") {
    modifiers |= Modifier::REVERSED;
  }

  (color, modifiers)
}

/// Parses a color string into a `Color`.
///
/// # Parameters
///
/// * `s`: The color string.
///
/// # Returns
///
/// `Option<Color>` - The parsed `Color` or `None` if the color cannot be parsed.
fn parse_color(s: &str) -> Option<Color> {
  let s = s.trim_start();
  let s = s.trim_end();
  if s.contains("bright color") {
    let s = s.trim_start_matches("bright ");
    let c = s
      .trim_start_matches("color")
      .parse::<u8>()
      .unwrap_or_default();
    Some(Color::Indexed(c.wrapping_shl(8)))
  } else if s.contains("color") {
    let c = s
      .trim_start_matches("color")
      .parse::<u8>()
      .unwrap_or_default();
    Some(Color::Indexed(c))
  } else if s.contains("gray") {
    let c = 232
      + s
        .trim_start_matches("gray")
        .parse::<u8>()
        .unwrap_or_default();
    Some(Color::Indexed(c))
  } else if s.contains("rgb") {
    let red = (s.as_bytes()[3] as char).to_digit(10).unwrap_or_default() as u8;
    let green =
      (s.as_bytes()[4] as char).to_digit(10).unwrap_or_default() as u8;
    let blue = (s.as_bytes()[5] as char).to_digit(10).unwrap_or_default() as u8;
    let c = 16 + red * 36 + green * 6 + blue;
    Some(Color::Indexed(c))
  } else if s == "bold black" {
    Some(Color::Indexed(8))
  } else if s == "bold red" {
    Some(Color::Indexed(9))
  } else if s == "bold green" {
    Some(Color::Indexed(10))
  } else if s == "bold yellow" {
    Some(Color::Indexed(11))
  } else if s == "bold blue" {
    Some(Color::Indexed(12))
  } else if s == "bold magenta" {
    Some(Color::Indexed(13))
  } else if s == "bold cyan" {
    Some(Color::Indexed(14))
  } else if s == "bold white" {
    Some(Color::Indexed(15))
  } else if s == "black" {
    Some(Color::Indexed(0))
  } else if s == "red" {
    Some(Color::Indexed(1))
  } else if s == "green" {
    Some(Color::Indexed(2))
  } else if s == "yellow" {
    Some(Color::Indexed(3))
  } else if s == "blue" {
    Some(Color::Indexed(4))
  } else if s == "magenta" {
    Some(Color::Indexed(5))
  } else if s == "cyan" {
    Some(Color::Indexed(6))
  } else if s == "white" {
    Some(Color::Indexed(7))
  } else {
    None
  }
}

/// Test module for `Config`, `KeyBindings`, `Styles`, and associated parsing functions.
#[cfg(test)]
mod tests {
  use super::*;
  use pretty_assertions::assert_eq;

  /// Tests the default configuration for style parsing.
  ///
  /// Ensures that an empty style string is parsed as a default `Style` object.
  #[test]
  fn test_parse_style_default() {
    let style = parse_style("");
    assert_eq!(style, Style::default());
  }

  /// Tests the parsing of a foreground color in a style string.
  ///
  /// Verifies that a simple color name is correctly parsed as the foreground color.
  #[test]
  fn test_parse_style_foreground() {
    let style = parse_style("red");
    assert_eq!(style.fg, Some(Color::Indexed(1)));
  }

  /// Tests the parsing of a background color in a style string.
  ///
  /// Checks if the background color is correctly interpreted from the style string.
  #[test]
  fn test_parse_style_background() {
    let style = parse_style("on blue");
    assert_eq!(style.bg, Some(Color::Indexed(4)));
  }

  /// Tests the parsing of style modifiers along with colors.
  ///
  /// Confirms that both foreground and background colors as well as text modifiers are correctly parsed.
  #[test]
  fn test_parse_style_modifiers() {
    let style = parse_style("underline red on blue");
    assert_eq!(style.fg, Some(Color::Indexed(1)));
    assert_eq!(style.bg, Some(Color::Indexed(4)));
  }

  /// Tests processing of a color string with modifiers.
  ///
  /// Validates correct extraction of color and modifiers from a given string.
  #[test]
  fn test_process_color_string() {
    let (color, modifiers) =
      process_color_string("underline bold inverse gray");
    assert_eq!(color, "gray");
    assert!(modifiers.contains(Modifier::UNDERLINED));
    assert!(modifiers.contains(Modifier::BOLD));
    assert!(modifiers.contains(Modifier::REVERSED));
  }

  /// Tests the parsing of RGB color values.
  ///
  /// Verifies that RGB color strings are correctly converted into their respective color values.
  #[test]
  fn test_parse_color_rgb() {
    let color = parse_color("rgb123");
    let expected = 16 + 36 + 2 * 6 + 3;
    assert_eq!(color, Some(Color::Indexed(expected)));
  }

  /// Tests parsing of unknown color values.
  ///
  /// Ensures that an unknown color string returns `None`.
  #[test]
  fn test_parse_color_unknown() {
    let color = parse_color("unknown");
    assert_eq!(color, None);
  }

  /// Tests loading configuration for a specific scene.
  ///
  /// Verifies that keybindings for a given scene (e.g., `Scene::Home`) are correctly loaded from the configuration.
  #[test]
  fn test_config_home() -> Result<()> {
    let c = Config::new()?;
    assert_eq!(
      c.keybindings
        .get(&Scene::Home)
        .unwrap()
        .get(&parse_key_sequence("<q>").unwrap_or_default())
        .unwrap(),
      &Action::Quit
    );
    Ok(())
  }

  /// Tests loading configuration for the 'Internals' scene.
  ///
  /// Ensures keybindings specific to the 'Internals' scene are correctly loaded and parsed.
  #[test]
  fn test_config_internals() -> Result<()> {
    let c = Config::new()?;
    assert_eq!(
      c.keybindings
        .get(&Scene::Internals)
        .unwrap()
        .get(&parse_key_sequence("<q>").unwrap_or_default())
        .unwrap(),
      &Action::Quit
    );
    Ok(())
  }

  /// Tests parsing of simple key events.
  ///
  /// Verifies that individual key events are correctly parsed from their string representations.
  #[test]
  fn test_simple_keys() {
    assert_eq!(
      parse_key_event("a").unwrap(),
      KeyEvent::new(KeyCode::Char('a'), KeyModifiers::empty())
    );

    assert_eq!(
      parse_key_event("enter").unwrap(),
      KeyEvent::new(KeyCode::Enter, KeyModifiers::empty())
    );

    assert_eq!(
      parse_key_event("esc").unwrap(),
      KeyEvent::new(KeyCode::Esc, KeyModifiers::empty())
    );
  }

  /// Tests parsing of key events with modifiers.
  ///
  /// Confirms that key events with modifiers like Ctrl and Alt are correctly interpreted.
  #[test]
  fn test_with_modifiers() {
    assert_eq!(
      parse_key_event("ctrl-a").unwrap(),
      KeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL)
    );

    assert_eq!(
      parse_key_event("alt-enter").unwrap(),
      KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT)
    );

    assert_eq!(
      parse_key_event("shift-esc").unwrap(),
      KeyEvent::new(KeyCode::Esc, KeyModifiers::SHIFT)
    );
  }

  /// Tests parsing of key events with multiple modifiers.
  ///
  /// Checks that combinations of multiple modifiers are correctly handled.
  #[test]
  fn test_multiple_modifiers() {
    assert_eq!(
      parse_key_event("ctrl-alt-a").unwrap(),
      KeyEvent::new(
        KeyCode::Char('a'),
        KeyModifiers::CONTROL | KeyModifiers::ALT
      )
    );

    assert_eq!(
      parse_key_event("ctrl-shift-enter").unwrap(),
      KeyEvent::new(
        KeyCode::Enter,
        KeyModifiers::CONTROL | KeyModifiers::SHIFT
      )
    );
  }

  /// Tests reverse parsing from `KeyEvent` to string.
  ///
  /// Verifies that a `KeyEvent` object can be accurately converted back into its string representation.
  #[test]
  fn test_reverse_multiple_modifiers() {
    assert_eq!(
      _key_event_to_string(&KeyEvent::new(
        KeyCode::Char('a'),
        KeyModifiers::CONTROL | KeyModifiers::ALT
      )),
      "ctrl-alt-a".to_string()
    );
  }

  /// Tests parsing of invalid key strings.
  ///
  /// Ensures that invalid key strings result in an error.
  #[test]
  fn test_invalid_keys() {
    assert!(parse_key_event("invalid-key").is_err());
    assert!(parse_key_event("ctrl-invalid-key").is_err());
  }

  /// Tests case insensitivity in key parsing.
  ///
  /// Confirms that the key parsing logic correctly handles case-insensitive input.
  #[test]
  fn test_case_insensitivity() {
    assert_eq!(
      parse_key_event("CTRL-a").unwrap(),
      KeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL)
    );

    assert_eq!(
      parse_key_event("AlT-eNtEr").unwrap(),
      KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT)
    );
  }
}