boxmux 0.240.3373

YAML-driven terminal UI framework for rich, interactive CLI applications and dashboards with PTY support
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
//! F0091 - Mouse Click Support Tests
//! Test the mouse click functionality for muxbox selection and choice activation

#[cfg(test)]
mod mouse_click_tests {
    use crate::model::choice::Choice;
    use crate::model::common::{Bounds, InputBounds};
    use crate::model::layout::Layout;
    use crate::model::muxbox::MuxBox;
    use crate::tests::test_utils::TestDataFactory;
    use crate::thread_manager::Message;
    use crate::{
        color_utils::get_bg_color, draw_loop::apply_calibration_cursor_overlay_at, ScreenBuffer,
    };

    /// Test that MouseClick message can be created
    #[test]
    fn test_mouse_click_message_creation() {
        let x = 10u16;
        let y = 20u16;
        let message = Message::MouseClick(x, y);

        match message {
            Message::MouseClick(click_x, click_y) => {
                assert_eq!(click_x, x);
                assert_eq!(click_y, y);
            }
            _ => panic!("Expected MouseClick message"),
        }
    }

    /// Test that MouseClick message hashes correctly
    #[test]
    fn test_mouse_click_message_hash() {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let msg1 = Message::MouseClick(10, 20);
        let msg2 = Message::MouseClick(10, 20);
        let msg3 = Message::MouseClick(10, 21);

        let mut hasher1 = DefaultHasher::new();
        let mut hasher2 = DefaultHasher::new();
        let mut hasher3 = DefaultHasher::new();

        msg1.hash(&mut hasher1);
        msg2.hash(&mut hasher2);
        msg3.hash(&mut hasher3);

        // Same coordinates should produce same hash
        assert_eq!(hasher1.finish(), hasher2.finish());

        // Different coordinates should produce different hash
        assert_ne!(hasher1.finish(), hasher3.finish());
    }

    /// Test finding muxbox at coordinates
    #[test]
    fn test_find_muxbox_at_coordinates() {
        let mut muxbox1 = TestDataFactory::create_test_muxbox("muxbox1");
        let mut muxbox2 = TestDataFactory::create_test_muxbox("muxbox2");

        // Create layout with muxboxes at different positions
        let layout =
            TestDataFactory::create_test_layout("test_layout", Some(vec![muxbox1, muxbox2]));

        // Test finding muxboxes at various coordinates
        // Note: The actual coordinates depend on bounds calculation which needs screen size
        // For this test, we're mainly testing the method exists and doesn't panic

        let found_muxbox = layout.find_muxbox_at_coordinates(50, 25);
        // The result depends on bounds calculation, but method should not panic

        let not_found = layout.find_muxbox_at_coordinates(1000, 1000);
        // Very large coordinates should not find any muxbox (unless screen is huge)
    }

    #[test]
    fn test_every_terminal_pixel_maps_to_topmost_muxbox() {
        let mut left = TestDataFactory::create_test_muxbox("left");
        left.position = InputBounds {
            x1: "1%".to_string(),
            y1: "8%".to_string(),
            x2: "24%".to_string(),
            y2: "91%".to_string(),
        };

        let mut center = TestDataFactory::create_test_muxbox("center");
        center.position = InputBounds {
            x1: "25%".to_string(),
            y1: "8%".to_string(),
            x2: "73%".to_string(),
            y2: "70%".to_string(),
        };

        let mut overlay = TestDataFactory::create_test_muxbox("overlay");
        overlay.position = InputBounds {
            x1: "39%".to_string(),
            y1: "11%".to_string(),
            x2: "52%".to_string(),
            y2: "18%".to_string(),
        };
        overlay.z_index = Some(10);

        let mut right = TestDataFactory::create_test_muxbox("right");
        right.position = InputBounds {
            x1: "74%".to_string(),
            y1: "8%".to_string(),
            x2: "99%".to_string(),
            y2: "91%".to_string(),
        };

        let mut far_corner = TestDataFactory::create_test_muxbox("far_corner");
        far_corner.position = InputBounds {
            x1: "82.5%".to_string(),
            y1: "74.5%".to_string(),
            x2: "99.6%".to_string(),
            y2: "99.2%".to_string(),
        };
        far_corner.z_index = Some(20);

        let layout = TestDataFactory::create_test_layout(
            "pixel_map",
            Some(vec![left, center, right, overlay, far_corner]),
        );

        for (width, height) in [
            (80usize, 24usize),
            (100, 30),
            (132, 43),
            (192, 54),
            (241, 67),
            (319, 91),
        ] {
            let root_bounds = Bounds {
                x1: 0,
                y1: 0,
                x2: width - 1,
                y2: height - 1,
            };
            let children = layout.children.as_ref().expect("pixel map children");
            let left_bounds = children[0].bounds_with_parent(&root_bounds);
            let center_bounds = children[1].bounds_with_parent(&root_bounds);
            let right_bounds = children[2].bounds_with_parent(&root_bounds);
            let overlay_bounds = children[3].bounds_with_parent(&root_bounds);
            let far_corner_bounds = children[4].bounds_with_parent(&root_bounds);

            for y in 0..height as u16 {
                for x in 0..width as u16 {
                    let ux = x as usize;
                    let uy = y as usize;
                    // Expected = the box that is visually on top, matching the
                    // renderer: highest z_index first (far_corner z20, overlay z10),
                    // then among equal z_index the box drawn LATER (later in the
                    // children list) wins. Children order is left, center, right, so
                    // where center and right share an edge column, right is on top.
                    let expected = if far_corner_bounds.contains_point(ux, uy) {
                        Some("far_corner")
                    } else if overlay_bounds.contains_point(ux, uy) {
                        Some("overlay")
                    } else if right_bounds.contains_point(ux, uy) {
                        Some("right")
                    } else if center_bounds.contains_point(ux, uy) {
                        Some("center")
                    } else if left_bounds.contains_point(ux, uy) {
                        Some("left")
                    } else {
                        None
                    };

                    let actual = layout
                        .find_muxbox_at_coordinates_with_bounds(x, y, &root_bounds)
                        .map(|muxbox| muxbox.id.as_str());

                    assert_eq!(
                        actual, expected,
                        "coordinate ({},{}) at terminal {}x{} mapped to the wrong muxbox",
                        x, y, width, height
                    );
                }
            }
        }
    }

    /// Regression: when a full-width box's bottom edge rounds onto the same row as
    /// the next box's TOP edge (fractional-percentage adjacency under inclusive
    /// bounds), a click on that shared row — which is where the lower box's tab bar
    /// is rendered — must resolve to the lower box (drawn on top), not the upper
    /// one. This was the cause of the central panel's tabs being unclickable at
    /// some terminal sizes until a resize/move shifted the rounding.
    #[test]
    fn test_shared_edge_row_resolves_to_box_rendered_on_top() {
        // Status spans full width, y 0%..7%; main is below at y 8%..70%.
        let mut status = TestDataFactory::create_test_muxbox("status");
        status.position = InputBounds {
            x1: "0%".to_string(),
            y1: "0%".to_string(),
            x2: "100%".to_string(),
            y2: "7%".to_string(),
        };
        let mut main = TestDataFactory::create_test_muxbox("main");
        main.position = InputBounds {
            x1: "25%".to_string(),
            y1: "8%".to_string(),
            x2: "73%".to_string(),
            y2: "70%".to_string(),
        };
        // status is listed BEFORE main, exactly like the real dashboard.
        let layout =
            TestDataFactory::create_test_layout("share", Some(vec![status, main]));

        // 100x30: 7% -> round(0.07*29)=2 and 8% -> round(0.08*29)=2 collide on row 2.
        let root_bounds = Bounds {
            x1: 0,
            y1: 0,
            x2: 99,
            y2: 29,
        };
        let children = layout.children.as_ref().unwrap();
        let status_bounds = children[0].bounds_with_parent(&root_bounds);
        let main_bounds = children[1].bounds_with_parent(&root_bounds);
        assert_eq!(
            status_bounds.y2, main_bounds.y1,
            "test requires the shared-edge condition (status.bottom == main.top)"
        );

        // A cell on the shared row within main's columns is main's tab-bar row.
        let shared_row = main_bounds.y1 as u16;
        let x = ((main_bounds.x1 + main_bounds.x2) / 2) as u16;
        assert!(status_bounds.contains_point(x as usize, shared_row as usize));
        assert!(main_bounds.contains_point(x as usize, shared_row as usize));

        let hit = layout
            .find_muxbox_at_coordinates_with_bounds(x, shared_row, &root_bounds)
            .map(|m| m.id.clone());
        assert_eq!(
            hit.as_deref(),
            Some("main"),
            "the shared tab-bar row must belong to the box rendered on top (main)"
        );
    }

    #[test]
    fn test_calibration_marks_only_cursor_cell_background_for_detected_muxbox() {
        let mut muxbox = TestDataFactory::create_test_muxbox("target");
        muxbox.position = InputBounds {
            x1: "25%".to_string(),
            y1: "8%".to_string(),
            x2: "73%".to_string(),
            y2: "70%".to_string(),
        };
        let layout = TestDataFactory::create_test_layout("calibration", Some(vec![muxbox]));
        let root_bounds = Bounds {
            x1: 0,
            y1: 0,
            x2: 240,
            y2: 66,
        };
        let mut buffer = ScreenBuffer::new_custom(241, 67);
        let before_cursor = buffer.get(90, 20).cloned().expect("cursor cell");
        let before_neighbor = buffer.get(91, 20).cloned().expect("neighbor cell");

        assert!(apply_calibration_cursor_overlay_at(
            &layout,
            &mut buffer,
            90,
            20,
            &root_bounds
        ));

        let cursor = buffer.get(90, 20).expect("calibrated cursor cell");
        let neighbor = buffer.get(91, 20).expect("neighbor cell after calibration");
        assert_eq!(cursor.ch, before_cursor.ch);
        assert_eq!(cursor.fg_color, before_cursor.fg_color);
        assert_eq!(cursor.bg_color, get_bg_color("red"));
        assert_eq!(neighbor, &before_neighbor);
    }

    #[test]
    fn test_calibration_does_not_mark_unowned_gap_cells() {
        let mut muxbox = TestDataFactory::create_test_muxbox("target");
        muxbox.position = InputBounds {
            x1: "25%".to_string(),
            y1: "8%".to_string(),
            x2: "73%".to_string(),
            y2: "70%".to_string(),
        };
        let layout = TestDataFactory::create_test_layout("calibration", Some(vec![muxbox]));
        let root_bounds = Bounds {
            x1: 0,
            y1: 0,
            x2: 240,
            y2: 66,
        };
        let mut buffer = ScreenBuffer::new_custom(241, 67);
        let before = buffer.get(1, 1).cloned().expect("gap cell");

        assert!(!apply_calibration_cursor_overlay_at(
            &layout,
            &mut buffer,
            1,
            1,
            &root_bounds
        ));
        assert_eq!(buffer.get(1, 1), Some(&before));
    }

    /// Test choice creation with mouse-activatable properties
    #[test]
    fn test_choice_mouse_activation_properties() {
        let choice = Choice {
            id: "test_choice".to_string(),
            content: Some("Click Me".to_string()),
            script: Some(vec!["echo clicked".to_string()]),
            redirect_output: Some("output_muxbox".to_string()),
            append_output: Some(false),
            execution_mode: crate::model::common::ExecutionMode::default(),
            selected: false,
            hovered: false,
            waiting: false,
        };

        // Verify the choice has all properties needed for mouse activation
        assert!(choice.script.is_some());
        assert_eq!(choice.redirect_output, Some("output_muxbox".to_string()));
    }

    /// Test muxbox selection properties for mouse clicks
    #[test]
    fn test_muxbox_selection_properties() {
        let mut muxbox = TestDataFactory::create_test_muxbox("selectable_muxbox");
        muxbox.tab_order = Some("1".to_string()); // Makes muxbox selectable

        // MuxBox should be selectable if it has tab_order
        assert!(muxbox.tab_order.is_some());
        assert_eq!(muxbox.tab_order, Some("1".to_string()));
    }

    /// Test muxbox with choices for menu activation
    #[test]
    fn test_muxbox_with_choices_for_menu() {
        let choice1 = Choice {
            id: "choice1".to_string(),
            content: Some("First Choice".to_string()),
            script: Some(vec!["echo first".to_string()]),
            redirect_output: None,
            append_output: None,
            execution_mode: crate::model::common::ExecutionMode::default(),
            selected: false,
            hovered: false,
            waiting: false,
        };

        let choice2 = Choice {
            id: "choice2".to_string(),
            content: Some("Second Choice".to_string()),
            script: Some(vec!["echo second".to_string()]),
            redirect_output: Some("output".to_string()),
            append_output: Some(true),
            execution_mode: crate::model::common::ExecutionMode::default(),
            selected: false,
            hovered: false,
            waiting: false,
        };

        let mut muxbox = TestDataFactory::create_test_muxbox("menu_muxbox");
        muxbox.choices = Some(vec![choice1, choice2]);

        // MuxBox should have choices for menu activation
        assert!(muxbox.choices.is_some());
        let choices = muxbox.choices.as_ref().unwrap();
        assert_eq!(choices.len(), 2);
        assert_eq!(choices[0].id, "choice1");
        assert_eq!(choices[1].id, "choice2");

        assert_eq!(choices[1].redirect_output, Some("output".to_string()));
    }

    /// Test mouse click coordinate validation
    #[test]
    fn test_mouse_click_coordinate_bounds() {
        // Test boundary values for mouse coordinates
        let min_coords = Message::MouseClick(0, 0);
        let max_coords = Message::MouseClick(u16::MAX, u16::MAX);

        match min_coords {
            Message::MouseClick(x, y) => {
                assert_eq!(x, 0);
                assert_eq!(y, 0);
            }
            _ => panic!("Expected MouseClick message"),
        }

        match max_coords {
            Message::MouseClick(x, y) => {
                assert_eq!(x, u16::MAX);
                assert_eq!(y, u16::MAX);
            }
            _ => panic!("Expected MouseClick message"),
        }
    }

    /// Test layout with nested muxboxes for coordinate detection
    #[test]
    fn test_nested_muxboxes_coordinate_detection() {
        let child_muxbox = TestDataFactory::create_test_muxbox("child");
        let mut parent_muxbox = TestDataFactory::create_test_muxbox("parent");
        parent_muxbox.children = Some(vec![child_muxbox]);

        let layout =
            TestDataFactory::create_test_layout("nested_layout", Some(vec![parent_muxbox]));

        // Test that coordinate detection works with nested structure
        // The method should handle nested muxboxes without panicking
        let _result = layout.find_muxbox_at_coordinates(10, 10);

        // Main test is that this doesn't panic with nested structure
        assert!(
            true,
            "Nested muxbox coordinate detection completed without panic"
        );
    }

    /// Test choice index calculation edge cases
    #[test]
    fn test_choice_index_calculation_logic() {
        // This tests the logic that would be used in calculate_clicked_choice_index
        let num_choices = 5;
        let muxbox_height = 20u16;
        let content_start_offset = 3u16;
        let content_height = muxbox_height - content_start_offset;

        if content_height > 0 && num_choices > 0 {
            let choice_height = content_height / num_choices as u16;

            // Test different click positions
            let click_positions = vec![
                0,
                choice_height / 2,
                choice_height,
                choice_height * 2,
                choice_height * 4,
            ];

            for click_pos in click_positions {
                let choice_index = (click_pos / choice_height.max(1)) as usize;
                assert!(
                    choice_index < num_choices || click_pos >= content_height,
                    "Choice index {} should be valid for click position {}",
                    choice_index,
                    click_pos
                );
            }
        }

        // Test zero cases
        assert_eq!(0u16 / 1u16.max(1), 0);
        assert_eq!(5u16 / 1u16.max(1), 5);
    }
}