charmed-bubbles 0.2.0

Common TUI components for bubbletea applications
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
//! Integration tests for bubbles components within the bubbletea event loop.
//!
//! These tests verify that components work correctly when composed in a parent App
//! and driven by the bubbletea runtime (simulated).

#![forbid(unsafe_code)]
#![expect(
    clippy::items_after_statements,
    clippy::float_cmp,
    clippy::uninlined_format_args,
    clippy::single_char_pattern
)]

use bubbles::spinner::{SpinnerModel, spinners};
use bubbles::textarea::TextArea;
use bubbles::textinput::TextInput;
use bubbles::timer::Timer;
use bubbles::viewport::Viewport;
use bubbletea::simulator::ProgramSimulator;
use bubbletea::{Cmd, KeyMsg, KeyType, Message, Model, batch};
use std::time::Duration;

// ============================================================================
// Scenario 1: Form with Focus Management
// Tests: TextInput + TextArea, Tab navigation, Key event routing
// ============================================================================

struct FormApp {
    name_input: TextInput,
    bio_input: TextArea,
    focus_index: usize,
}

impl FormApp {
    fn new() -> Self {
        let mut name = TextInput::new();
        name.set_placeholder("Name");
        name.focus(); // Initial focus

        let bio = TextArea::new();

        Self {
            name_input: name,
            bio_input: bio,
            focus_index: 0,
        }
    }
}

impl Model for FormApp {
    fn init(&self) -> Option<Cmd> {
        // Return batch of init commands from children
        batch(vec![self.name_input.init(), self.bio_input.init()])
    }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        // Handle global navigation
        if let Some(key) = msg.downcast_ref::<KeyMsg>()
            && key.key_type == KeyType::Tab
        {
            self.focus_index = (self.focus_index + 1) % 2;

            if self.focus_index == 0 {
                self.name_input.focus();
                self.bio_input.blur();
            } else {
                self.name_input.blur();
                self.bio_input.focus();
            }
            return None;
        }

        // Route messages to focused component (only update the focused one)
        if self.focus_index == 0 {
            self.name_input.update(msg)
        } else {
            self.bio_input.update(msg)
        }
    }

    fn view(&self) -> String {
        format!("{}\n{}", self.name_input.view(), self.bio_input.view())
    }
}

#[test]
fn test_form_focus_and_input_routing() {
    let mut sim = ProgramSimulator::new(FormApp::new());
    sim.init();

    // 1. Initial state: Name focused
    assert!(sim.model().name_input.focused());
    assert!(!sim.model().bio_input.focused());

    // 2. Type "Alice" into Name (step once per key to avoid cursor blink loop)
    for c in "Alice".chars() {
        sim.sim_key(c);
        sim.step(); // Process single key event
    }

    assert_eq!(sim.model().name_input.value(), "Alice");
    assert_eq!(sim.model().bio_input.value(), "");

    // 3. Tab to switch focus
    sim.sim_key_type(KeyType::Tab);
    sim.step();

    assert!(!sim.model().name_input.focused());
    assert!(sim.model().bio_input.focused());

    // 4. Type "Dev" into Bio
    for c in "Dev".chars() {
        sim.sim_key(c);
        sim.step();
    }

    assert_eq!(sim.model().name_input.value(), "Alice"); // Should be unchanged
    assert_eq!(sim.model().bio_input.value(), "Dev");
}

// ============================================================================
// Scenario 2: Async Command Integration
// Tests: Spinner + Timer, Tick propagation, Cmd composition
// ============================================================================

struct AsyncApp {
    spinner: SpinnerModel,
    timer: Timer,
    finished: bool,
    last_msg_for_spinner: bool,
}

impl AsyncApp {
    fn new() -> Self {
        Self {
            spinner: SpinnerModel::with_spinner(spinners::dot()),
            timer: Timer::new(Duration::from_mins(1)), // Long timer to avoid early finish
            finished: false,
            last_msg_for_spinner: true, // Alternate between spinner and timer
        }
    }
}

impl Model for AsyncApp {
    fn init(&self) -> Option<Cmd> {
        // Start both
        batch(vec![self.spinner.init(), self.timer.init()])
    }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        // Route alternately to prevent both consuming the same message
        // In a real app, messages are typically specific to each component (by ID)
        if self.last_msg_for_spinner {
            self.last_msg_for_spinner = false;
            self.spinner.update(msg)
        } else {
            self.last_msg_for_spinner = true;
            let cmd = self.timer.update(msg);
            // Check if timer finished
            if !self.timer.running() && !self.finished {
                self.finished = true;
            }
            cmd
        }
    }

    fn view(&self) -> String {
        if self.finished {
            "Done!".to_string()
        } else {
            format!("{} {}", self.spinner.view(), self.timer.view())
        }
    }
}

#[test]
fn test_async_component_integration() {
    let mut sim = ProgramSimulator::new(AsyncApp::new());

    // 1. Init should trigger commands for both components
    let init_cmd = sim.init();
    assert!(init_cmd.is_some(), "Init should return batch command");

    // Execute init batch (spinner tick + timer tick)
    if let Some(cmd) = init_cmd
        && let Some(batch_msg) = cmd.execute()
    {
        sim.send(batch_msg);
    }

    // 2. Step a few times to process commands (avoid run_until_empty due to timer tick loops)
    for _ in 0..5 {
        sim.step();
    }

    // 3. Verify view renders (spinner and timer both produce output)
    let view = sim.model().view();

    // If timer finished, view will be "Done!", otherwise it will contain spinner and timer
    // Either state is valid for this test - we're testing that the components can be composed
    assert!(!view.is_empty(), "View should not be empty");

    // The view should either be "Done!" or contain spinner characters
    let is_done = view.contains("Done!");
    let has_spinner = view.contains('⣾')
        || view.contains('⣽')
        || view.contains('⣻')
        || view.contains('⢿')
        || view.contains('â¡¿')
        || view.contains('⣟')
        || view.contains('⣯')
        || view.contains('⣷');

    assert!(
        is_done || has_spinner,
        "View should show either 'Done!' or spinner, got: {view}"
    );
}

// ============================================================================
// Scenario 3: Batch Commands & Viewport Scrolling
// Tests: Viewport + Key handling, Batch execution order
// ============================================================================

struct LogViewer {
    viewport: Viewport,
    content: String, // Store content separately since Viewport doesn't expose it
    auto_scroll: bool,
}

#[derive(Clone)]
struct AddLogMsg(String);

impl LogViewer {
    fn new() -> Self {
        let mut vp = Viewport::new(20, 5);
        let initial_content = "Log started...".to_string();
        vp.set_content(&initial_content);
        Self {
            viewport: vp,
            content: initial_content,
            auto_scroll: true,
        }
    }
}

impl Model for LogViewer {
    fn init(&self) -> Option<Cmd> {
        None
    }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        if let Some(AddLogMsg(line)) = msg.downcast_ref::<AddLogMsg>() {
            // Append log line
            self.content.push('\n');
            self.content.push_str(line);
            self.viewport.set_content(&self.content);

            if self.auto_scroll {
                self.viewport.goto_bottom();
            }
            return None;
        }

        // Handle viewport navigation (returns () not Option<Cmd>)
        self.viewport.update(&msg);
        None
    }

    fn view(&self) -> String {
        self.viewport.view()
    }
}

#[test]
fn test_viewport_batch_updates() {
    let mut sim = ProgramSimulator::new(LogViewer::new());
    sim.init();

    // 1. Send a batch of log messages
    use bubbletea::message::BatchMsg;

    let batch = BatchMsg(vec![
        Cmd::new(|| Message::new(AddLogMsg("Line 1".into()))),
        Cmd::new(|| Message::new(AddLogMsg("Line 2".into()))),
        Cmd::new(|| Message::new(AddLogMsg("Line 3".into()))),
        Cmd::new(|| Message::new(AddLogMsg("Line 4".into()))),
        Cmd::new(|| Message::new(AddLogMsg("Line 5".into()))),
    ]);

    sim.send(Message::new(batch));
    sim.run_until_empty();

    // 2. Verify content added and scrolled
    let model = sim.model();
    assert!(model.content.contains("Line 5"));

    // Viewport height is 5. We added 5 lines + 1 initial = 6 lines.
    // With auto-scroll, we should be at the bottom.
    assert!(model.viewport.at_bottom());

    // 3. Test manual scrolling (simulating keys)
    sim.sim_key_type(KeyType::Up); // Scroll up
    sim.run_until_empty();

    assert!(!sim.model().viewport.at_bottom(), "Should scroll up");
}

// ============================================================================
// Scenario 4: Viewport Mouse Wheel Scrolling
// Tests: Mouse events routing to viewport
// ============================================================================

struct MouseScrollApp {
    viewport: Viewport,
}

impl MouseScrollApp {
    fn new() -> Self {
        let mut vp = Viewport::new(40, 5);
        // mouse_wheel_enabled is true by default
        // Add enough content to scroll
        vp.set_content(
            "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10",
        );
        Self { viewport: vp }
    }
}

impl Model for MouseScrollApp {
    fn init(&self) -> Option<Cmd> {
        None
    }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        self.viewport.update(&msg);
        None
    }

    fn view(&self) -> String {
        self.viewport.view()
    }
}

#[test]
fn test_viewport_mouse_wheel_scrolling() {
    use bubbletea::mouse::{MouseAction, MouseButton};

    let mut sim = ProgramSimulator::new(MouseScrollApp::new());
    sim.init();

    // 1. Initial state: at top
    assert!(sim.model().viewport.at_top());
    assert_eq!(sim.model().viewport.y_offset(), 0);

    // 2. Scroll down with mouse wheel
    sim.sim_mouse(5, 2, MouseButton::WheelDown, MouseAction::Press);
    sim.run_until_empty();

    assert!(
        !sim.model().viewport.at_top(),
        "Should scroll down on wheel"
    );

    // 3. Scroll back up with mouse wheel
    sim.sim_mouse(5, 2, MouseButton::WheelUp, MouseAction::Press);
    sim.run_until_empty();

    assert!(sim.model().viewport.at_top(), "Should scroll back to top");
}

// ============================================================================
// Scenario 5: Progress Updates from Async Commands
// Tests: Progress component with animated updates
// ============================================================================

use bubbles::progress::Progress;

struct ProgressApp {
    progress: Progress,
    percent: f64,
}

#[derive(Clone)]
struct SetProgressMsg(f64);

impl ProgressApp {
    fn new() -> Self {
        Self {
            progress: Progress::new().width(20),
            percent: 0.0,
        }
    }
}

impl Model for ProgressApp {
    fn init(&self) -> Option<Cmd> {
        self.progress.init()
    }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        // Handle custom progress messages
        if let Some(SetProgressMsg(p)) = msg.downcast_ref::<SetProgressMsg>() {
            self.percent = *p;
            self.progress.set_percent(*p);
        }

        // Forward to progress for animation frames
        self.progress.update(msg)
    }

    fn view(&self) -> String {
        self.progress.view()
    }
}

#[test]
fn test_progress_async_updates() {
    let mut sim = ProgramSimulator::new(ProgressApp::new());

    // 1. Init triggers animation frame command
    let _init_cmd = sim.init();
    // Progress may or may not return an init command depending on implementation

    // 2. Set progress to 50%
    sim.send(Message::new(SetProgressMsg(0.5)));
    sim.run_until_empty();

    assert_eq!(sim.model().percent, 0.5);

    // 3. Set progress to 100%
    sim.send(Message::new(SetProgressMsg(1.0)));
    sim.run_until_empty();

    assert_eq!(sim.model().percent, 1.0);

    // 4. Verify view renders progress bar
    let view = sim.model().view();
    assert!(!view.is_empty(), "Progress view should render");
}

// ============================================================================
// Scenario 6: Multi-Component with Mouse Event Routing
// Tests: Mouse events route to correct component based on position
// ============================================================================

struct MultiPanelApp {
    left_viewport: Viewport,
    right_viewport: Viewport,
    // Layout: left panel is columns 0-19, right panel is columns 20-39
    left_clicks: usize,
    right_clicks: usize,
}

impl MultiPanelApp {
    fn new() -> Self {
        let mut left = Viewport::new(20, 10);
        left.set_content("Left panel\nClick here");
        // mouse_wheel_enabled is true by default

        let mut right = Viewport::new(20, 10);
        right.set_content("Right panel\nClick here");
        // mouse_wheel_enabled is true by default

        Self {
            left_viewport: left,
            right_viewport: right,
            left_clicks: 0,
            right_clicks: 0,
        }
    }
}

impl Model for MultiPanelApp {
    fn init(&self) -> Option<Cmd> {
        None
    }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        use bubbletea::mouse::MouseMsg;

        if let Some(mouse) = msg.downcast_ref::<MouseMsg>() {
            // Route based on X position
            if mouse.x < 20 {
                self.left_clicks += 1;
                self.left_viewport.update(&msg);
            } else {
                self.right_clicks += 1;
                self.right_viewport.update(&msg);
            }
        }

        None
    }

    fn view(&self) -> String {
        format!(
            "{} | {}",
            self.left_viewport.view(),
            self.right_viewport.view()
        )
    }
}

#[test]
fn test_mouse_event_routing_to_components() {
    use bubbletea::mouse::{MouseAction, MouseButton};

    let mut sim = ProgramSimulator::new(MultiPanelApp::new());
    sim.init();

    // 1. Click in left panel (x < 20)
    sim.sim_mouse(5, 2, MouseButton::Left, MouseAction::Press);
    sim.run_until_empty();

    assert_eq!(sim.model().left_clicks, 1);
    assert_eq!(sim.model().right_clicks, 0);

    // 2. Click in right panel (x >= 20)
    sim.sim_mouse(25, 2, MouseButton::Left, MouseAction::Press);
    sim.run_until_empty();

    assert_eq!(sim.model().left_clicks, 1);
    assert_eq!(sim.model().right_clicks, 1);

    // 3. Multiple clicks in each panel
    sim.sim_mouse(10, 5, MouseButton::Left, MouseAction::Press);
    sim.sim_mouse(30, 5, MouseButton::Left, MouseAction::Press);
    sim.run_until_empty();

    assert_eq!(sim.model().left_clicks, 2);
    assert_eq!(sim.model().right_clicks, 2);
}

// ============================================================================
// Scenario 7: Nested Component Rendering & Style Composition
// Tests: Component view() in parent view(), styles don't corrupt
// ============================================================================

use lipgloss::Style as LipStyle;

struct StyledPanelApp {
    input: TextInput,
    viewport: Viewport,
    panel_style: LipStyle,
    input_wrapper_style: LipStyle,
}

impl StyledPanelApp {
    fn new() -> Self {
        let mut input = TextInput::new();
        input.set_placeholder("Enter text...");
        input.set_prompt(">> ");

        let mut vp = Viewport::new(30, 5);
        vp.set_content("Content line 1\nContent line 2\nContent line 3");

        Self {
            input,
            viewport: vp,
            panel_style: LipStyle::new()
                .padding(1)
                .border(lipgloss::Border::rounded()),
            input_wrapper_style: LipStyle::new().foreground("205"),
        }
    }
}

impl Model for StyledPanelApp {
    fn init(&self) -> Option<Cmd> {
        self.input.init()
    }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        // Route to input
        self.input.update(msg)
    }

    fn view(&self) -> String {
        // Nested rendering: input wrapped in style, then viewport, all in panel
        let input_view = self.input_wrapper_style.render(&self.input.view());
        let viewport_view = self.viewport.view();

        let content = format!("{}\n{}", input_view, viewport_view);
        self.panel_style.render(&content)
    }
}

#[test]
fn test_nested_component_rendering() {
    let mut sim = ProgramSimulator::new(StyledPanelApp::new());
    sim.init();

    // 1. Get initial view
    let view = sim.model().view();

    // View should contain input prompt and viewport content
    assert!(
        view.contains(">>") || view.contains(">"),
        "Should contain input prompt"
    );
    assert!(
        view.contains("Content line"),
        "Should contain viewport content"
    );

    // 2. Type some text (step once per key to avoid cursor blink loop)
    sim.model_mut().input.focus();
    for c in "hello".chars() {
        sim.sim_key(c);
        sim.step();
    }

    // 3. Verify view still renders correctly with input value
    let view_after = sim.model().view();
    assert!(view_after.contains("hello"), "Should contain typed text");

    // 4. Verify styles are applied (view should have border characters from rounded border)
    // Rounded borders use: ╭ ╮ ╰ ╯ │ ─
    let has_border =
        view_after.contains('╭') || view_after.contains('│') || view_after.contains('─');
    assert!(has_border, "Should have border styling applied");
}

#[test]
fn test_style_composition_no_corruption() {
    let mut sim = ProgramSimulator::new(StyledPanelApp::new());
    sim.init();

    // 1. Get multiple renders
    let view1 = sim.model().view();
    let view2 = sim.model().view();
    let view3 = sim.model().view();

    // Views should be identical (no state corruption from rendering)
    assert_eq!(view1, view2, "Consecutive views should be identical");
    assert_eq!(view2, view3, "Consecutive views should be identical");

    // 2. Modify state and verify no corruption
    sim.model_mut().input.focus();
    sim.model_mut().input.set_value("test");

    let view_modified1 = sim.model().view();
    let view_modified2 = sim.model().view();

    assert_eq!(
        view_modified1, view_modified2,
        "Modified views should be identical"
    );
    assert_ne!(
        view1, view_modified1,
        "Modified view should differ from original"
    );
}

// ============================================================================
// Scenario 8: Focus Styling Changes
// Tests: Visual feedback for focus/blur state
// ============================================================================

struct FocusStyleApp {
    input1: TextInput,
    input2: TextInput,
    focused_idx: usize,
}

impl FocusStyleApp {
    fn new() -> Self {
        let mut i1 = TextInput::new();
        i1.set_prompt("[1] ");
        i1.focus();

        let mut i2 = TextInput::new();
        i2.set_prompt("[2] ");

        Self {
            input1: i1,
            input2: i2,
            focused_idx: 0,
        }
    }

    fn switch_focus(&mut self) {
        if self.focused_idx == 0 {
            self.input1.blur();
            self.input2.focus();
            self.focused_idx = 1;
        } else {
            self.input2.blur();
            self.input1.focus();
            self.focused_idx = 0;
        }
    }
}

impl Model for FocusStyleApp {
    fn init(&self) -> Option<Cmd> {
        batch(vec![self.input1.init(), self.input2.init()])
    }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        if let Some(key) = msg.downcast_ref::<KeyMsg>()
            && key.key_type == KeyType::Tab
        {
            self.switch_focus();
            return None;
        }

        // Route to focused input
        if self.focused_idx == 0 {
            self.input1.update(msg)
        } else {
            self.input2.update(msg)
        }
    }

    fn view(&self) -> String {
        format!("{}\n{}", self.input1.view(), self.input2.view())
    }
}

#[test]
fn test_focus_styling_changes() {
    let mut sim = ProgramSimulator::new(FocusStyleApp::new());
    sim.init();

    // 1. Initial state: input1 focused
    assert!(sim.model().input1.focused());
    assert!(!sim.model().input2.focused());

    let view_initial = sim.model().view();

    // 2. Switch focus via Tab (step once to avoid cursor blink loop)
    sim.sim_key_type(KeyType::Tab);
    sim.step();

    assert!(!sim.model().input1.focused());
    assert!(sim.model().input2.focused());

    let view_after_tab = sim.model().view();

    // Views might differ due to cursor blink state, but structure should be same
    // Both should contain the prompts
    assert!(view_initial.contains("[1]"));
    assert!(view_initial.contains("[2]"));
    assert!(view_after_tab.contains("[1]"));
    assert!(view_after_tab.contains("[2]"));

    // 3. Type in focused input (step once per key)
    for c in "focused".chars() {
        sim.sim_key(c);
        sim.step();
    }

    // Text should appear in input2 (now focused)
    assert_eq!(sim.model().input1.value(), "");
    assert_eq!(sim.model().input2.value(), "focused");

    // 4. Switch back and type
    sim.sim_key_type(KeyType::Tab);
    sim.step();

    for c in "also".chars() {
        sim.sim_key(c);
        sim.step();
    }

    assert_eq!(sim.model().input1.value(), "also");
    assert_eq!(sim.model().input2.value(), "focused");
}