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
#[cfg(test)]
mod tests {
    use crate::components::BoxRenderer;
    use crate::model::common::{Bounds, ExecutionMode, Stream, StreamSource, StreamType};
    use crate::tests::test_utils::TestDataFactory;
    use crate::{AppContext, Config};
    use indexmap::IndexMap;
    use std::collections::HashMap;

    fn create_test_context() -> AppContext {
        let app = TestDataFactory::create_test_app();
        AppContext::new(app, Config::default())
    }

    #[test]
    fn test_box_renderer_creation() {
        let muxbox = TestDataFactory::create_test_muxbox("test_box");
        let mut renderer = BoxRenderer::new(&muxbox, "test_renderer".to_string());

        // BoxRenderer created successfully - component encapsulates muxbox reference
        assert_eq!(muxbox.id, "test_box");
    }

    #[test]
    fn test_box_renderer_basic_rendering() {
        let mut muxbox = TestDataFactory::create_test_muxbox_with_parent("test_box", "test_layout");
        muxbox.title = Some("Test Title".to_string());

        let context = create_test_context();
        let mut app = TestDataFactory::create_test_app();
        let graph = app.generate_graph();

        // Create adjusted bounds
        let mut adjusted_bounds = HashMap::new();
        let mut layout_bounds = HashMap::new();
        layout_bounds.insert(
            "test_box".to_string(),
            Bounds {
                x1: 10,
                y1: 10,
                x2: 50,
                y2: 20,
            },
        );
        adjusted_bounds.insert("test_layout".to_string(), layout_bounds);

        let mut renderer = BoxRenderer::new(&muxbox, "test_renderer".to_string());
        let mut buffer = crate::ScreenBuffer::new();

        // Should render without errors
        let result = renderer.render(
            &context,
            &graph,
            &adjusted_bounds,
            &app.layouts[0],
            &mut buffer,
        );

        assert!(result, "BoxRenderer should render successfully");

        // Validate buffer dimensions were respected
        assert!(buffer.width > 0, "Buffer should have positive width");
        assert!(buffer.height > 0, "Buffer should have positive height");

        // Validate that rendering actually occurred by checking buffer cells
        let mut has_non_default_content = false;
        for row in &buffer.buffer {
            for cell in row {
                if cell.ch != ' ' {
                    has_non_default_content = true;
                    break;
                }
            }
        }
        assert!(
            has_non_default_content,
            "Buffer should contain rendered content (non-space characters)"
        );
    }

    #[test]
    fn test_box_renderer_with_content_stream() {
        let mut muxbox =
            TestDataFactory::create_test_muxbox_with_parent("content_box", "test_layout");

        // Add content stream with correct API
        let mut streams = IndexMap::new();
        let content_lines = vec!["Line 1".to_string(), "Line 2".to_string()];
        let mut stream = Stream::new(
            "content".to_string(),
            StreamType::Content,
            "Content".to_string(),
            content_lines,
            None,
            None,
        );
        // Stream active state is now managed by muxbox.selected_stream_id
        streams.insert("content".to_string(), stream);
        muxbox.streams = streams;

        let context = create_test_context();
        let mut app = TestDataFactory::create_test_app();
        let graph = app.generate_graph();

        let mut adjusted_bounds = HashMap::new();
        let mut layout_bounds = HashMap::new();
        layout_bounds.insert(
            "content_box".to_string(),
            Bounds {
                x1: 5,
                y1: 5,
                x2: 45,
                y2: 15,
            },
        );
        adjusted_bounds.insert("test_layout".to_string(), layout_bounds);

        let mut renderer = BoxRenderer::new(&muxbox, "content_renderer".to_string());
        let mut buffer = crate::ScreenBuffer::new();

        let result = renderer.render(
            &context,
            &graph,
            &adjusted_bounds,
            &app.layouts[0],
            &mut buffer,
        );

        assert!(result, "BoxRenderer should handle content streams");

        // Validate stream state
        assert_eq!(muxbox.streams.len(), 1, "Should have one content stream");
        let content_stream = muxbox.streams.get("content").unwrap();
        assert_eq!(
            content_stream.stream_type,
            crate::model::common::StreamType::Content,
            "Content stream should exist"
        );
        assert_eq!(
            content_stream.stream_type,
            StreamType::Content,
            "Stream should be content type"
        );

        // Validate stream content is available
        assert!(
            !content_stream.content.is_empty(),
            "Content stream should have content lines"
        );
        assert!(
            content_stream.content.contains(&"Line 1".to_string())
                || content_stream.content.contains(&"Line 2".to_string()),
            "Stream should contain expected content lines"
        );

        // Validate buffer has been written to
        assert!(
            buffer.width > 0 && buffer.height > 0,
            "Buffer should have valid dimensions"
        );
    }

    #[test]
    fn test_box_renderer_with_choices_stream() {
        let mut muxbox =
            TestDataFactory::create_test_muxbox_with_parent("choice_box", "test_layout");

        // Add choices stream with correct API
        let mut streams = IndexMap::new();
        let choice1 = crate::model::choice::Choice {
            id: "choice1".to_string(),
            content: Some("Option 1".to_string()),
            script: Some(vec!["echo option1".to_string()]),
            ..Default::default()
        };
        let choice2 = crate::model::choice::Choice {
            id: "choice2".to_string(),
            content: Some("Option 2".to_string()),
            script: Some(vec!["echo option2".to_string()]),
            ..Default::default()
        };
        let choices = vec![choice1, choice2];
        let mut stream = Stream::new(
            "choices".to_string(),
            StreamType::Choices,
            "Choices".to_string(),
            vec![],
            Some(choices),
            None,
        );
        // Stream active state is now managed by muxbox.selected_stream_id
        streams.insert("choices".to_string(), stream);
        muxbox.streams = streams;

        let context = create_test_context();
        let mut app = TestDataFactory::create_test_app();
        let graph = app.generate_graph();

        let mut adjusted_bounds = HashMap::new();
        let mut layout_bounds = HashMap::new();
        layout_bounds.insert(
            "choice_box".to_string(),
            Bounds {
                x1: 10,
                y1: 10,
                x2: 60,
                y2: 25,
            },
        );
        adjusted_bounds.insert("test_layout".to_string(), layout_bounds);

        let mut renderer = BoxRenderer::new(&muxbox, "choice_renderer".to_string());
        let mut buffer = crate::ScreenBuffer::new();

        let result = renderer.render(
            &context,
            &graph,
            &adjusted_bounds,
            &app.layouts[0],
            &mut buffer,
        );

        assert!(result, "BoxRenderer should handle choice streams");

        // Validate stream configuration
        assert_eq!(muxbox.streams.len(), 1, "Should have one choices stream");
        let choices_stream = muxbox.streams.get("choices").unwrap();
        assert!(
            choices_stream.choices.is_some(),
            "Choices stream should contain choices"
        );
        assert_eq!(
            choices_stream.stream_type,
            StreamType::Choices,
            "Stream should be choices type"
        );

        // Validate choices are available in the stream
        assert!(
            choices_stream.choices.is_some(),
            "Choices stream should have choices"
        );
        let choices = choices_stream.choices.as_ref().unwrap();
        assert_eq!(choices.len(), 2, "Should have two choices");
        assert_eq!(
            choices[0].id, "choice1",
            "First choice should have correct ID"
        );
        assert_eq!(
            choices[1].id, "choice2",
            "Second choice should have correct ID"
        );

        // Validate buffer dimensions
        assert!(
            buffer.width > 0 && buffer.height > 0,
            "Buffer should have valid dimensions"
        );
    }

    #[test]
    fn test_box_renderer_preserves_muxbox_state() {
        let muxbox = TestDataFactory::create_test_muxbox("state_box");
        let mut renderer = BoxRenderer::new(&muxbox, "state_renderer".to_string());

        // BoxRenderer preserves muxbox state - it's a read-only rendering component
        assert_eq!(muxbox.id, "state_box");
        assert_eq!(muxbox.streams.len(), 0); // Default test muxbox has no streams

        // Renderer doesn't modify the muxbox - it only renders based on its state
        // This validates the design principle that BoxRenderer is purely visual

        // Validate renderer is read-only and doesn't mutate the muxbox
        assert_eq!(
            muxbox.error_state, false,
            "Renderer should not change muxbox error state"
        );
        assert!(
            muxbox.title.is_some() || muxbox.title.is_none(),
            "Renderer should not affect title"
        );
        assert!(
            muxbox.content.is_some() || muxbox.content.is_none(),
            "Renderer should not affect content"
        );
    }

    #[test]
    fn test_box_renderer_with_missing_bounds() {
        let muxbox = TestDataFactory::create_test_muxbox("missing_bounds_box");
        let context = create_test_context();
        let mut app = TestDataFactory::create_test_app();
        let graph = app.generate_graph();

        // Empty adjusted bounds - should handle gracefully
        let adjusted_bounds = HashMap::new();

        let mut renderer = BoxRenderer::new(&muxbox, "missing_bounds_renderer".to_string());
        let mut buffer = crate::ScreenBuffer::new();

        let result = renderer.render(
            &context,
            &graph,
            &adjusted_bounds,
            &app.layouts[0],
            &mut buffer,
        );

        assert!(
            !result,
            "BoxRenderer should return false when bounds are missing"
        );
    }

    #[test]
    fn test_box_renderer_component_integration() {
        let mut muxbox =
            TestDataFactory::create_test_muxbox_with_parent("integration_box", "test_layout");

        // Configure for scrollable content to test scrollbar integration
        muxbox.overflow_behavior = Some("scroll".to_string());
        muxbox.next_focus_id = Some("next_box".to_string()); // Makes it focusable

        // Add large content stream to trigger scrollbars
        let mut streams = IndexMap::new();
        let content_lines: Vec<String> = (1..=20)
            .map(|i| {
                format!(
                    "Long content line {} with lots of text to trigger horizontal scrolling",
                    i
                )
            })
            .collect();
        let mut stream = Stream::new(
            "content".to_string(),
            StreamType::Content,
            "Large Content".to_string(),
            content_lines,
            None,
            None,
        );
        // Stream active state is now managed by muxbox.selected_stream_id
        streams.insert("content".to_string(), stream);
        muxbox.streams = streams;

        let context = create_test_context();
        let mut app = TestDataFactory::create_test_app();
        let graph = app.generate_graph();

        let mut adjusted_bounds = HashMap::new();
        let mut layout_bounds = HashMap::new();
        layout_bounds.insert(
            "integration_box".to_string(),
            Bounds {
                x1: 5,
                y1: 5,
                x2: 35,
                y2: 15, // Small box to force scrolling
            },
        );
        adjusted_bounds.insert("test_layout".to_string(), layout_bounds);

        let mut renderer = BoxRenderer::new(&muxbox, "integration_renderer".to_string());
        let mut buffer = crate::ScreenBuffer::new();

        let result = renderer.render(
            &context,
            &graph,
            &adjusted_bounds,
            &app.layouts[0],
            &mut buffer,
        );

        assert!(
            result,
            "BoxRenderer should integrate with scrollbar components"
        );
    }

    #[test]
    fn test_box_renderer_pty_indicator_integration() {
        let mut muxbox = TestDataFactory::create_test_muxbox_with_parent("pty_box", "test_layout");

        // Set PTY execution mode
        muxbox.execution_mode = ExecutionMode::Pty;

        let context = create_test_context();
        let mut app = TestDataFactory::create_test_app();
        let graph = app.generate_graph();

        let mut adjusted_bounds = HashMap::new();
        let mut layout_bounds = HashMap::new();
        layout_bounds.insert(
            "pty_box".to_string(),
            Bounds {
                x1: 10,
                y1: 10,
                x2: 50,
                y2: 20,
            },
        );
        adjusted_bounds.insert("test_layout".to_string(), layout_bounds);

        let mut renderer = BoxRenderer::new(&muxbox, "pty_renderer".to_string());
        let mut buffer = crate::ScreenBuffer::new();

        let result = renderer.render(
            &context,
            &graph,
            &adjusted_bounds,
            &app.layouts[0],
            &mut buffer,
        );

        assert!(
            result,
            "BoxRenderer should handle PTY muxboxes with proper indicators"
        );
    }
}