selfware 0.2.2

Your personal AI workshop — software you own, software that lasts
Documentation
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
//! Selfware TUI Application
//!
//! State machine for the terminal UI with multi-pane layouts.

// Feature-gated module - dead_code lint disabled at crate level

use super::{wrap_chat_message, CommandPalette, TuiPalette};
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::Style,
    text::Span,
    widgets::{Block, Borders, List, ListItem, Paragraph},
    Frame,
};

/// Application state
#[derive(Debug, Clone, PartialEq)]
pub enum AppState {
    /// Normal chat mode
    Chatting,
    /// Running a task with progress
    RunningTask,
    /// Command palette is open
    Palette,
    /// Browsing files
    FileBrowser,
    /// Viewing help
    Help,
    /// Confirming an action
    Confirming(String),
}

/// A chat message for display
#[derive(Debug, Clone)]
pub struct ChatMessage {
    pub role: MessageRole,
    pub content: String,
    pub timestamp: String,
}

#[derive(Debug, Clone, PartialEq)]
pub enum MessageRole {
    User,
    Assistant,
    System,
    Tool,
}

/// Task progress for display
#[derive(Debug, Clone)]
pub struct TaskProgress {
    pub description: String,
    pub current_step: usize,
    pub total_steps: Option<usize>,
    pub current_action: String,
    pub elapsed_secs: u64,
}

/// Animation speed settings
pub const ANIMATION_SPEED_MIN: f64 = 0.25;
pub const ANIMATION_SPEED_MAX: f64 = 4.0;
pub const ANIMATION_SPEED_STEP: f64 = 0.25;
pub const ANIMATION_SPEED_DEFAULT: f64 = 1.0;

/// The main TUI application
pub struct App {
    /// Current state
    pub state: AppState,
    /// Chat messages
    pub messages: Vec<ChatMessage>,
    /// Current input buffer
    pub input: String,
    /// Cursor position in input
    pub cursor: usize,
    /// Command palette
    pub palette: CommandPalette,
    /// Task progress (if running)
    pub task_progress: Option<TaskProgress>,
    /// Status bar message
    pub status: String,
    /// Model name
    pub model: String,
    /// Whether we're connected
    pub connected: bool,
    /// Scroll offset for messages
    pub scroll: usize,
    /// Selected item in lists
    pub selected: usize,
    /// Animation speed multiplier (1.0 = normal, 2.0 = faster, 0.5 = slower)
    pub animation_speed: f64,
    /// Verbose output mode (toggled by /verbose and /compact)
    pub verbose: bool,
}

impl App {
    /// Create a new app instance
    pub fn new(model: &str) -> Self {
        Self {
            state: AppState::Chatting,
            messages: vec![ChatMessage {
                role: MessageRole::System,
                content: "Welcome to your workshop. How can I help you tend your garden today?"
                    .into(),
                timestamp: chrono::Local::now().format("%H:%M").to_string(),
            }],
            input: String::new(),
            cursor: 0,
            palette: CommandPalette::new(),
            task_progress: None,
            status: "Ready".into(),
            model: model.into(),
            connected: true,
            scroll: 0,
            selected: 0,
            animation_speed: ANIMATION_SPEED_DEFAULT,
            verbose: false,
        }
    }

    /// Add a user message
    pub fn add_user_message(&mut self, content: &str) {
        self.messages.push(ChatMessage {
            role: MessageRole::User,
            content: content.into(),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
    }

    /// Add an assistant message
    pub fn add_assistant_message(&mut self, content: &str) {
        self.messages.push(ChatMessage {
            role: MessageRole::Assistant,
            content: content.into(),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
    }

    /// Add a system message
    pub fn add_system_message(&mut self, content: &str) {
        self.messages.push(ChatMessage {
            role: MessageRole::System,
            content: content.into(),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
    }

    /// Add a tool output message
    pub fn add_tool_message(&mut self, tool_name: &str, output: &str) {
        self.messages.push(ChatMessage {
            role: MessageRole::Tool,
            content: format!("[{}] {}", tool_name, output),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
    }

    /// Clear chat history (keeping a fresh system message)
    pub fn clear_chat(&mut self) {
        self.messages.clear();
        self.messages.push(ChatMessage {
            role: MessageRole::System,
            content: "Chat cleared.".into(),
            timestamp: chrono::Local::now().format("%H:%M").to_string(),
        });
        self.scroll = 0;
    }

    /// Set task progress
    pub fn set_progress(&mut self, progress: TaskProgress) {
        self.task_progress = Some(progress);
        self.state = AppState::RunningTask;
    }

    /// Clear task progress
    pub fn clear_progress(&mut self) {
        self.task_progress = None;
        self.state = AppState::Chatting;
    }

    /// Toggle command palette
    pub fn toggle_palette(&mut self) {
        self.state = if self.state == AppState::Palette {
            AppState::Chatting
        } else {
            AppState::Palette
        };
    }

    /// Render the application
    pub fn render(&self, frame: &mut Frame) {
        let area = frame.area();

        // Create main layout
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3), // Header
                Constraint::Min(10),   // Main content
                Constraint::Length(3), // Input area
                Constraint::Length(1), // Status bar
            ])
            .split(area);

        self.render_header(frame, chunks[0]);
        self.render_messages(frame, chunks[1]);
        self.render_input(frame, chunks[2]);
        self.render_status_bar(frame, chunks[3]);

        // Render overlay if palette is open
        if self.state == AppState::Palette {
            self.render_palette(frame, area);
        }
    }

    /// Render the header
    fn render_header(&self, frame: &mut Frame, area: Rect) {
        let title = format!(" 🦊 Selfware Workshop — {} ", self.model);
        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(TuiPalette::border_style())
            .title(Span::styled(title, TuiPalette::title_style()));

        frame.render_widget(block, area);
    }

    /// Render chat messages
    fn render_messages(&self, frame: &mut Frame, area: Rect) {
        let inner = Block::default()
            .borders(Borders::ALL)
            .border_style(TuiPalette::border_style())
            .title(" Messages ");

        let inner_area = inner.inner(area);
        frame.render_widget(inner, area);

        // Build message list with line wrapping
        let msg_width = inner_area.width as usize;
        let items: Vec<ListItem> = self
            .messages
            .iter()
            .rev()
            .skip(self.scroll)
            .map(|msg| {
                let style = match msg.role {
                    MessageRole::User => Style::default().fg(TuiPalette::AMBER),
                    MessageRole::Assistant => Style::default().fg(TuiPalette::GARDEN_GREEN),
                    MessageRole::System => TuiPalette::muted_style(),
                    MessageRole::Tool => Style::default().fg(TuiPalette::COPPER),
                };

                let prefix = match msg.role {
                    MessageRole::User => "You",
                    MessageRole::Assistant => "🦊",
                    MessageRole::System => "📋",
                    MessageRole::Tool => "🔧",
                };

                let prefix_str = format!("{} {} ", msg.timestamp, prefix);
                wrap_chat_message(&prefix_str, &msg.content, style, msg_width)
            })
            .collect();

        let messages = List::new(items);
        frame.render_widget(messages, inner_area);
    }

    /// Render input area
    fn render_input(&self, frame: &mut Frame, area: Rect) {
        let input_block = Block::default()
            .borders(Borders::ALL)
            .border_style(if self.state == AppState::Chatting {
                TuiPalette::title_style()
            } else {
                TuiPalette::muted_style()
            })
            .title(" Input (Ctrl+P for palette) ");

        let inner = input_block.inner(area);
        frame.render_widget(input_block, area);

        let input_text = Paragraph::new(format!("{}", self.input))
            .style(Style::default().fg(TuiPalette::PARCHMENT));
        frame.render_widget(input_text, inner);

        // Show cursor
        if self.state == AppState::Chatting {
            frame.set_cursor_position((inner.x + 2 + self.cursor as u16, inner.y));
        }
    }

    /// Render status bar
    fn render_status_bar(&self, frame: &mut Frame, area: Rect) {
        let status_style = if self.connected {
            TuiPalette::success_style()
        } else {
            TuiPalette::error_style()
        };

        let connection_status = if self.connected { "" } else { "" };

        let status_text = format!(
            " {} {}{}{} messages │ Ctrl+C to quit ",
            connection_status,
            self.model,
            self.status,
            self.messages.len()
        );

        let status = Paragraph::new(status_text).style(status_style);
        frame.render_widget(status, area);
    }

    /// Render command palette overlay
    fn render_palette(&self, frame: &mut Frame, area: Rect) {
        // Center the palette
        let palette_width = 60.min(area.width - 4);
        let palette_height = 15.min(area.height - 4);
        let x = (area.width - palette_width) / 2;
        let y = (area.height - palette_height) / 3;

        let palette_area = Rect::new(x, y, palette_width, palette_height);

        // Clear background
        let clear = Block::default().style(Style::default().bg(TuiPalette::INK));
        frame.render_widget(clear, palette_area);

        // Render palette
        self.palette.render(frame, palette_area, self.selected);
    }

    /// Handle character input
    pub fn on_char(&mut self, c: char) {
        if self.state == AppState::Chatting {
            self.input.insert(self.cursor, c);
            self.cursor += 1;
        } else if self.state == AppState::Palette {
            self.palette.on_char(c);
        }
    }

    /// Handle backspace
    pub fn on_backspace(&mut self) {
        if self.state == AppState::Chatting && self.cursor > 0 {
            self.cursor -= 1;
            self.input.remove(self.cursor);
        } else if self.state == AppState::Palette {
            self.palette.on_backspace();
        }
    }

    /// Handle left arrow
    pub fn on_left(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
        }
    }

    /// Handle right arrow
    pub fn on_right(&mut self) {
        if self.cursor < self.input.len() {
            self.cursor += 1;
        }
    }

    /// Handle up arrow
    pub fn on_up(&mut self) {
        if self.state == AppState::Palette {
            self.palette.previous();
        } else if self.scroll + 1 < self.messages.len() {
            self.scroll += 1;
        }
    }

    /// Handle down arrow
    pub fn on_down(&mut self) {
        if self.state == AppState::Palette {
            self.palette.next();
        } else if self.scroll > 0 {
            self.scroll -= 1;
        }
    }

    /// Handle enter key
    pub fn on_enter(&mut self) -> Option<String> {
        if self.state == AppState::Palette {
            if let Some(cmd) = self.palette.selected_command() {
                self.state = AppState::Chatting;
                return Some(cmd);
            }
            None
        } else if !self.input.is_empty() {
            let input = std::mem::take(&mut self.input);
            self.cursor = 0;
            Some(input)
        } else {
            None
        }
    }

    /// Handle escape key
    pub fn on_escape(&mut self) {
        match self.state {
            AppState::Palette => self.state = AppState::Chatting,
            AppState::Confirming(_) => self.state = AppState::Chatting,
            _ => {}
        }
    }

    /// Increase animation speed (+ key)
    pub fn on_plus(&mut self) {
        self.animation_speed =
            (self.animation_speed + ANIMATION_SPEED_STEP).min(ANIMATION_SPEED_MAX);
        self.status = format!("Animation speed: {:.0}%", self.animation_speed * 100.0);
    }

    /// Decrease animation speed (- key)
    pub fn on_minus(&mut self) {
        self.animation_speed =
            (self.animation_speed - ANIMATION_SPEED_STEP).max(ANIMATION_SPEED_MIN);
        self.status = format!("Animation speed: {:.0}%", self.animation_speed * 100.0);
    }

    /// Get animation delay based on current speed
    /// Returns the delay in milliseconds to use between animation frames
    pub fn animation_delay_ms(&self) -> u64 {
        // Base delay is 100ms, adjusted by speed (faster = shorter delay)
        let base_delay = 100.0;
        (base_delay / self.animation_speed) as u64
    }

    /// Get animation speed as percentage string
    pub fn animation_speed_display(&self) -> String {
        format!("{:.0}%", self.animation_speed * 100.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_app_creation() {
        let app = App::new("test-model");
        assert_eq!(app.model, "test-model");
        assert_eq!(app.state, AppState::Chatting);
        assert!(app.connected);
    }

    #[test]
    fn test_app_initial_state() {
        let app = App::new("test");
        assert!(app.input.is_empty());
        assert_eq!(app.cursor, 0);
        assert_eq!(app.scroll, 0);
        assert_eq!(app.selected, 0);
        assert_eq!(app.status, "Ready");
    }

    #[test]
    fn test_app_has_welcome_message() {
        let app = App::new("test");
        assert!(!app.messages.is_empty());
        assert_eq!(app.messages[0].role, MessageRole::System);
    }

    #[test]
    fn test_add_messages() {
        let mut app = App::new("test");
        app.add_user_message("Hello");
        app.add_assistant_message("Hi there!");

        assert_eq!(app.messages.len(), 3); // 1 system + 2 new
        assert_eq!(app.messages[1].role, MessageRole::User);
        assert_eq!(app.messages[2].role, MessageRole::Assistant);
    }

    #[test]
    fn test_add_user_message() {
        let mut app = App::new("test");
        app.add_user_message("Test message");

        assert_eq!(app.messages.last().unwrap().role, MessageRole::User);
        assert_eq!(app.messages.last().unwrap().content, "Test message");
    }

    #[test]
    fn test_add_assistant_message() {
        let mut app = App::new("test");
        app.add_assistant_message("Assistant response");

        assert_eq!(app.messages.last().unwrap().role, MessageRole::Assistant);
        assert_eq!(app.messages.last().unwrap().content, "Assistant response");
    }

    #[test]
    fn test_add_tool_message() {
        let mut app = App::new("test");
        app.add_tool_message("file_read", "File contents here");

        assert_eq!(app.messages.last().unwrap().role, MessageRole::Tool);
        assert!(app.messages.last().unwrap().content.contains("file_read"));
        assert!(app
            .messages
            .last()
            .unwrap()
            .content
            .contains("File contents here"));
    }

    #[test]
    fn test_message_has_timestamp() {
        let mut app = App::new("test");
        app.add_user_message("Test");

        assert!(!app.messages.last().unwrap().timestamp.is_empty());
    }

    #[test]
    fn test_input_handling() {
        let mut app = App::new("test");

        app.on_char('h');
        app.on_char('i');
        assert_eq!(app.input, "hi");
        assert_eq!(app.cursor, 2);

        app.on_backspace();
        assert_eq!(app.input, "h");
        assert_eq!(app.cursor, 1);
    }

    #[test]
    fn test_input_char_inserts_at_cursor() {
        let mut app = App::new("test");
        app.on_char('a');
        app.on_char('c');
        app.on_left();
        app.on_char('b');

        assert_eq!(app.input, "abc");
    }

    #[test]
    fn test_backspace_at_start() {
        let mut app = App::new("test");
        app.on_char('a');
        app.on_left();
        app.on_backspace();

        // Should not change anything
        assert_eq!(app.input, "a");
        assert_eq!(app.cursor, 0);
    }

    #[test]
    fn test_backspace_empty() {
        let mut app = App::new("test");
        app.on_backspace();

        // Should not panic
        assert!(app.input.is_empty());
        assert_eq!(app.cursor, 0);
    }

    #[test]
    fn test_on_left() {
        let mut app = App::new("test");
        app.on_char('a');
        app.on_char('b');
        assert_eq!(app.cursor, 2);

        app.on_left();
        assert_eq!(app.cursor, 1);

        app.on_left();
        assert_eq!(app.cursor, 0);

        app.on_left();
        assert_eq!(app.cursor, 0); // Can't go below 0
    }

    #[test]
    fn test_on_right() {
        let mut app = App::new("test");
        app.input = "abc".into();
        app.cursor = 0;

        app.on_right();
        assert_eq!(app.cursor, 1);

        app.on_right();
        app.on_right();
        assert_eq!(app.cursor, 3);

        app.on_right();
        assert_eq!(app.cursor, 3); // Can't go beyond length
    }

    #[test]
    fn test_on_up_scroll() {
        let mut app = App::new("test");
        // Add enough messages to scroll
        for i in 0..10 {
            app.add_user_message(&format!("Message {}", i));
        }

        assert_eq!(app.scroll, 0);
        app.on_up();
        assert_eq!(app.scroll, 1);
        app.on_up();
        assert_eq!(app.scroll, 2);
    }

    #[test]
    fn test_on_down_scroll() {
        let mut app = App::new("test");
        for i in 0..10 {
            app.add_user_message(&format!("Message {}", i));
        }

        app.scroll = 5;
        app.on_down();
        assert_eq!(app.scroll, 4);

        app.scroll = 0;
        app.on_down();
        assert_eq!(app.scroll, 0); // Can't go below 0
    }

    #[test]
    fn test_toggle_palette() {
        let mut app = App::new("test");
        assert_eq!(app.state, AppState::Chatting);

        app.toggle_palette();
        assert_eq!(app.state, AppState::Palette);

        app.toggle_palette();
        assert_eq!(app.state, AppState::Chatting);
    }

    #[test]
    fn test_on_enter() {
        let mut app = App::new("test");
        app.input = "hello world".into();
        app.cursor = 11;

        let result = app.on_enter();
        assert_eq!(result, Some("hello world".into()));
        assert!(app.input.is_empty());
        assert_eq!(app.cursor, 0);
    }

    #[test]
    fn test_on_enter_empty() {
        let mut app = App::new("test");
        let result = app.on_enter();
        assert!(result.is_none());
    }

    #[test]
    fn test_on_escape_from_palette() {
        let mut app = App::new("test");
        app.state = AppState::Palette;

        app.on_escape();
        assert_eq!(app.state, AppState::Chatting);
    }

    #[test]
    fn test_on_escape_from_confirming() {
        let mut app = App::new("test");
        app.state = AppState::Confirming("test action".into());

        app.on_escape();
        assert_eq!(app.state, AppState::Chatting);
    }

    #[test]
    fn test_on_escape_from_chatting() {
        let mut app = App::new("test");
        app.state = AppState::Chatting;

        app.on_escape();
        assert_eq!(app.state, AppState::Chatting); // No change
    }

    #[test]
    fn test_set_progress() {
        let mut app = App::new("test");
        let progress = TaskProgress {
            description: "Test task".into(),
            current_step: 3,
            total_steps: Some(10),
            current_action: "Testing".into(),
            elapsed_secs: 120,
        };

        app.set_progress(progress);
        assert_eq!(app.state, AppState::RunningTask);
        assert!(app.task_progress.is_some());
    }

    #[test]
    fn test_clear_progress() {
        let mut app = App::new("test");
        let progress = TaskProgress {
            description: "Test".into(),
            current_step: 1,
            total_steps: None,
            current_action: "".into(),
            elapsed_secs: 0,
        };
        app.set_progress(progress);

        app.clear_progress();
        assert_eq!(app.state, AppState::Chatting);
        assert!(app.task_progress.is_none());
    }

    #[test]
    fn test_input_in_palette_mode() {
        let mut app = App::new("test");
        app.state = AppState::Palette;

        app.on_char('a');
        // Input should go to palette, not main input
        assert!(app.input.is_empty());
    }

    #[test]
    fn test_up_down_in_palette_mode() {
        let mut app = App::new("test");
        app.toggle_palette();
        assert_eq!(app.state, AppState::Palette);

        // Navigation in palette mode should work without panic
        app.on_down();
        app.on_up();
        // Navigation is handled by palette internally
    }

    #[test]
    fn test_message_role_equality() {
        assert_eq!(MessageRole::User, MessageRole::User);
        assert_ne!(MessageRole::User, MessageRole::Assistant);
    }

    #[test]
    fn test_app_state_equality() {
        assert_eq!(AppState::Chatting, AppState::Chatting);
        assert_ne!(AppState::Chatting, AppState::Palette);
        assert_eq!(
            AppState::Confirming("a".into()),
            AppState::Confirming("a".into())
        );
    }

    #[test]
    fn test_animation_speed_default() {
        let app = App::new("test");
        assert!((app.animation_speed - ANIMATION_SPEED_DEFAULT).abs() < 0.001);
    }

    #[test]
    fn test_on_plus_increases_speed() {
        let mut app = App::new("test");
        let original = app.animation_speed;
        app.on_plus();
        assert!(app.animation_speed > original);
    }

    #[test]
    fn test_on_minus_decreases_speed() {
        let mut app = App::new("test");
        app.on_plus(); // First increase so we can decrease
        let speed_after_plus = app.animation_speed;
        app.on_minus();
        assert!(app.animation_speed < speed_after_plus);
    }

    #[test]
    fn test_animation_speed_max_cap() {
        let mut app = App::new("test");
        // Increase many times
        for _ in 0..20 {
            app.on_plus();
        }
        assert!(app.animation_speed <= ANIMATION_SPEED_MAX);
    }

    #[test]
    fn test_animation_speed_min_cap() {
        let mut app = App::new("test");
        // Decrease many times
        for _ in 0..20 {
            app.on_minus();
        }
        assert!(app.animation_speed >= ANIMATION_SPEED_MIN);
    }

    #[test]
    fn test_animation_delay_inversely_proportional() {
        let mut app = App::new("test");
        let normal_delay = app.animation_delay_ms();

        app.animation_speed = 2.0;
        let fast_delay = app.animation_delay_ms();

        // Faster speed should mean shorter delay
        assert!(fast_delay < normal_delay);
    }

    #[test]
    fn test_animation_speed_display() {
        let mut app = App::new("test");
        app.animation_speed = 1.5;
        assert_eq!(app.animation_speed_display(), "150%");
    }
}