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
// F0327: Character-Exact Assertions - Precise coordinate validation
// Assert specific characters at exact terminal coordinates

use super::terminal_capture::{TerminalCell, TerminalFrame};
use std::fmt;

/// F0327: Visual assertion trait for precise validation
pub trait VisualAssertions {
    /// Assert character at specific coordinate
    fn assert_char_at(&self, x: u16, y: u16, expected: char) -> Result<(), AssertionError>;

    /// Assert line contains specific text
    fn assert_line_contains(&self, y: u16, expected: &str) -> Result<(), AssertionError>;

    /// Assert specific region matches expected content
    fn assert_screen_region(
        &self,
        x: u16,
        y: u16,
        width: u16,
        height: u16,
        expected: &str,
    ) -> Result<(), AssertionError>;

    /// Assert cursor is at expected position
    fn assert_cursor_at(&self, x: u16, y: u16) -> Result<(), AssertionError>;

    /// Assert cursor visibility state
    fn assert_cursor_visible(&self, visible: bool) -> Result<(), AssertionError>;

    /// Assert line is empty (all spaces)
    fn assert_line_empty(&self, y: u16) -> Result<(), AssertionError>;

    /// Assert character has specific attributes
    fn assert_char_attributes(
        &self,
        x: u16,
        y: u16,
        fg_color: Option<u8>,
        bg_color: Option<u8>,
    ) -> Result<(), AssertionError>;

    /// Assert screen contains specific text anywhere
    fn assert_contains_text(&self, expected: &str) -> Result<(), AssertionError>;

    /// Assert screen has a border (corners and edges)
    fn assert_has_border(&self) -> Result<(), AssertionError>;
}

/// F0327: Assertion error with detailed context
#[derive(Debug, Clone)]
pub struct AssertionError {
    pub message: String,
    pub expected: String,
    pub actual: String,
    pub position: Option<(u16, u16)>,
    pub context: Vec<String>,
}

impl fmt::Display for AssertionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Visual Assertion Failed: {}\nExpected: {}\nActual: {}",
            self.message, self.expected, self.actual
        )?;

        if let Some((x, y)) = self.position {
            write!(f, "\nPosition: ({}, {})", x, y)?;
        }

        if !self.context.is_empty() {
            write!(f, "\nContext:\n{}", self.context.join("\n"))?;
        }

        Ok(())
    }
}

impl std::error::Error for AssertionError {}

/// F0327: Implementation of visual assertions for TerminalFrame
impl VisualAssertions for TerminalFrame {
    fn assert_char_at(&self, x: u16, y: u16, expected: char) -> Result<(), AssertionError> {
        if y >= self.buffer.len() as u16 {
            return Err(AssertionError {
                message: "Y coordinate out of bounds".to_string(),
                expected: format!("char '{}' at ({}, {})", expected, x, y),
                actual: format!("y={} >= height={}", y, self.buffer.len()),
                position: Some((x, y)),
                context: vec![format!(
                    "Terminal dimensions: {}x{}",
                    self.dimensions.0, self.dimensions.1
                )],
            });
        }

        let row = &self.buffer[y as usize];
        if x >= row.len() as u16 {
            return Err(AssertionError {
                message: "X coordinate out of bounds".to_string(),
                expected: format!("char '{}' at ({}, {})", expected, x, y),
                actual: format!("x={} >= width={}", x, row.len()),
                position: Some((x, y)),
                context: vec![format!("Row {} length: {}", y, row.len())],
            });
        }

        let actual_char = row[x as usize].ch;
        if actual_char != expected {
            return Err(AssertionError {
                message: format!("Character mismatch at ({}, {})", x, y),
                expected: format!("'{}'", expected),
                actual: format!("'{}'", actual_char),
                position: Some((x, y)),
                context: vec![
                    format!("Expected: '{}' (U+{:04X})", expected, expected as u32),
                    format!("Actual:   '{}' (U+{:04X})", actual_char, actual_char as u32),
                    self.get_surrounding_context(x, y, 3),
                ],
            });
        }

        Ok(())
    }

    fn assert_line_contains(&self, y: u16, expected: &str) -> Result<(), AssertionError> {
        if y >= self.buffer.len() as u16 {
            return Err(AssertionError {
                message: "Y coordinate out of bounds".to_string(),
                expected: format!("line {} contains '{}'", y, expected),
                actual: format!("y={} >= height={}", y, self.buffer.len()),
                position: Some((0, y)),
                context: vec![format!("Terminal height: {}", self.buffer.len())],
            });
        }

        let row = &self.buffer[y as usize];
        let line_content: String = row.iter().map(|cell| cell.ch).collect();

        if !line_content.contains(expected) {
            return Err(AssertionError {
                message: format!("Line {} does not contain expected text", y),
                expected: format!("contains '{}'", expected),
                actual: format!("'{}'", line_content.trim()),
                position: Some((0, y)),
                context: vec![
                    format!("Full line content: '{}'", line_content),
                    format!("Looking for: '{}'", expected),
                ],
            });
        }

        Ok(())
    }

    fn assert_screen_region(
        &self,
        x: u16,
        y: u16,
        width: u16,
        height: u16,
        expected: &str,
    ) -> Result<(), AssertionError> {
        let mut actual_region = Vec::new();

        for row_offset in 0..height {
            let row_y = y + row_offset;
            if row_y >= self.buffer.len() as u16 {
                break;
            }

            let mut row_content = String::new();
            let row = &self.buffer[row_y as usize];

            for col_offset in 0..width {
                let col_x = x + col_offset;
                if col_x >= row.len() as u16 {
                    break;
                }
                row_content.push(row[col_x as usize].ch);
            }

            actual_region.push(row_content);
        }

        let actual = actual_region.join("\n");
        let expected_trimmed = expected.trim();

        if actual.trim() != expected_trimmed {
            return Err(AssertionError {
                message: format!("Region mismatch at ({}, {}) {}x{}", x, y, width, height),
                expected: format!("'{}'", expected_trimmed),
                actual: format!("'{}'", actual.trim()),
                position: Some((x, y)),
                context: vec![
                    format!("Region dimensions: {}x{}", width, height),
                    format!("Expected:\n{}", expected_trimmed),
                    format!("Actual:\n{}", actual.trim()),
                ],
            });
        }

        Ok(())
    }

    fn assert_cursor_at(&self, x: u16, y: u16) -> Result<(), AssertionError> {
        let (actual_x, actual_y) = self.cursor;

        if actual_x != x || actual_y != y {
            return Err(AssertionError {
                message: "Cursor position mismatch".to_string(),
                expected: format!("cursor at ({}, {})", x, y),
                actual: format!("cursor at ({}, {})", actual_x, actual_y),
                position: Some((actual_x, actual_y)),
                context: vec![format!("Cursor visible: {}", self.cursor_visible)],
            });
        }

        Ok(())
    }

    fn assert_cursor_visible(&self, visible: bool) -> Result<(), AssertionError> {
        if self.cursor_visible != visible {
            return Err(AssertionError {
                message: "Cursor visibility mismatch".to_string(),
                expected: format!("cursor visible: {}", visible),
                actual: format!("cursor visible: {}", self.cursor_visible),
                position: Some(self.cursor),
                context: vec![format!(
                    "Cursor position: ({}, {})",
                    self.cursor.0, self.cursor.1
                )],
            });
        }

        Ok(())
    }

    fn assert_line_empty(&self, y: u16) -> Result<(), AssertionError> {
        if y >= self.buffer.len() as u16 {
            return Err(AssertionError {
                message: "Y coordinate out of bounds".to_string(),
                expected: format!("empty line at y={}", y),
                actual: format!("y={} >= height={}", y, self.buffer.len()),
                position: Some((0, y)),
                context: vec![format!("Terminal height: {}", self.buffer.len())],
            });
        }

        let row = &self.buffer[y as usize];
        let line_content: String = row.iter().map(|cell| cell.ch).collect();
        let trimmed = line_content.trim();

        if !trimmed.is_empty() {
            return Err(AssertionError {
                message: format!("Line {} is not empty", y),
                expected: "empty line".to_string(),
                actual: format!("'{}'", trimmed),
                position: Some((0, y)),
                context: vec![
                    format!("Full line: '{}'", line_content),
                    format!("Non-whitespace characters: {}", trimmed.len()),
                ],
            });
        }

        Ok(())
    }

    fn assert_char_attributes(
        &self,
        x: u16,
        y: u16,
        fg_color: Option<u8>,
        bg_color: Option<u8>,
    ) -> Result<(), AssertionError> {
        if y >= self.buffer.len() as u16 || x >= self.buffer[y as usize].len() as u16 {
            return Err(AssertionError {
                message: "Coordinates out of bounds".to_string(),
                expected: format!("attributes at ({}, {})", x, y),
                actual: "out of bounds".to_string(),
                position: Some((x, y)),
                context: vec![format!(
                    "Terminal dimensions: {}x{}",
                    self.dimensions.0, self.dimensions.1
                )],
            });
        }

        let cell = &self.buffer[y as usize][x as usize];

        if cell.fg_color != fg_color || cell.bg_color != bg_color {
            return Err(AssertionError {
                message: format!("Color attributes mismatch at ({}, {})", x, y),
                expected: format!("fg={:?}, bg={:?}", fg_color, bg_color),
                actual: format!("fg={:?}, bg={:?}", cell.fg_color, cell.bg_color),
                position: Some((x, y)),
                context: vec![
                    format!("Character: '{}'", cell.ch),
                    format!("Attributes: {:?}", cell.attributes),
                ],
            });
        }

        Ok(())
    }

    fn assert_contains_text(&self, expected: &str) -> Result<(), AssertionError> {
        // Search through all screen content for the expected text
        for (y, row) in self.buffer.iter().enumerate() {
            let line: String = row.iter().map(|cell| cell.ch).collect();
            if line.contains(expected) {
                return Ok(());
            }
        }

        // If not found, create error with context
        let mut context = Vec::new();
        context.push("Screen content:".to_string());
        for (y, row) in self.buffer.iter().enumerate().take(10) {
            let line: String = row.iter().map(|cell| cell.ch).collect();
            context.push(format!("{:2}: '{}'", y, line.trim_end()));
        }

        Err(AssertionError {
            message: "Text not found in screen content".to_string(),
            expected: format!("text containing '{}'", expected),
            actual: "text not found".to_string(),
            position: None,
            context,
        })
    }

    fn assert_has_border(&self) -> Result<(), AssertionError> {
        // Check for basic border characters at expected positions
        // This is a simplified check for demonstration

        if self.buffer.is_empty() || self.buffer[0].is_empty() {
            return Err(AssertionError {
                message: "Empty screen buffer".to_string(),
                expected: "border characters".to_string(),
                actual: "empty buffer".to_string(),
                position: None,
                context: vec!["Screen is empty".to_string()],
            });
        }

        // Look for border-like characters in the first row
        let first_row: String = self.buffer[0].iter().map(|cell| cell.ch).collect();
        let has_border_chars = first_row.chars().any(|c| {
            matches!(
                c,
                '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | ''
            )
        });

        if has_border_chars {
            Ok(())
        } else {
            Err(AssertionError {
                message: "No border characters detected".to_string(),
                expected: "border characters (┌┐└┘─│)".to_string(),
                actual: format!("first row: '{}'", first_row.trim()),
                position: Some((0, 0)),
                context: vec!["Expected box drawing characters".to_string()],
            })
        }
    }
}

impl TerminalFrame {
    /// F0327: Get surrounding context for better error messages
    fn get_surrounding_context(&self, x: u16, y: u16, radius: u16) -> String {
        let mut context = Vec::new();

        let start_y = y.saturating_sub(radius);
        let end_y = (y + radius + 1).min(self.buffer.len() as u16);

        for row_idx in start_y..end_y {
            if row_idx >= self.buffer.len() as u16 {
                break;
            }

            let row = &self.buffer[row_idx as usize];
            let line: String = row.iter().map(|cell| cell.ch).collect();

            let marker = if row_idx == y {
                format!("{:2}", row_idx)
            } else {
                format!("{:2} ", row_idx)
            };

            context.push(format!("{} {}", marker, line));

            // Add cursor indicator for the target row
            if row_idx == y {
                let mut pointer = " ".repeat(4 + x as usize);
                pointer.push('^');
                context.push(pointer);
            }
        }

        context.join("\n")
    }
}

/// F0327: Convenience macros for visual assertions
#[macro_export]
macro_rules! assert_char {
    ($frame:expr, $x:expr, $y:expr, $expected:expr) => {
        use $crate::tests::visual_testing::visual_assertions::VisualAssertions;
        $frame.assert_char_at($x, $y, $expected).unwrap();
    };
}

#[macro_export]
macro_rules! assert_line_contains {
    ($frame:expr, $y:expr, $expected:expr) => {
        use $crate::tests::visual_testing::visual_assertions::VisualAssertions;
        $frame.assert_line_contains($y, $expected).unwrap();
    };
}

#[macro_export]
macro_rules! assert_region {
    ($frame:expr, $x:expr, $y:expr, $width:expr, $height:expr, $expected:expr) => {
        use $crate::tests::visual_testing::visual_assertions::VisualAssertions;
        $frame
            .assert_screen_region($x, $y, $width, $height, $expected)
            .unwrap();
    };
}