presentar-terminal 0.3.5

Terminal backend for Presentar UI framework with zero-allocation rendering
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
//! Time-series graph widget with multiple render modes.

use crate::theme::Gradient;
use presentar_core::{
    Brick, BrickAssertion, BrickBudget, BrickVerification, Canvas, Color, Constraints, Event,
    LayoutResult, Point, Rect, Size, TextStyle, TypeId, Widget,
};
use std::any::Any;
use std::time::Duration;

/// Render mode for the graph.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum GraphMode {
    /// Unicode braille characters (U+2800-28FF): 2x4 dots per cell.
    #[default]
    Braille,
    /// Half-block characters (▀▄█): 1x2 resolution per cell.
    Block,
    /// Pure ASCII characters: TTY compatible.
    Tty,
}

/// UX-117: Time axis display mode.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum TimeAxisMode {
    /// Show numeric indices (0, 1, 2, ...).
    #[default]
    Indices,
    /// Show relative time (1m, 2m, 5m ago).
    Relative {
        /// Seconds per data point.
        interval_secs: u64,
    },
    /// Show absolute time (HH:MM:SS).
    Absolute,
    /// Hide X-axis labels.
    Hidden,
}

impl TimeAxisMode {
    /// Format a time offset as a label.
    pub fn format_label(&self, index: usize, total: usize) -> Option<String> {
        match self {
            Self::Indices => Some(format!("{index}")),
            Self::Relative { interval_secs } => {
                let secs_ago = (total - index) as u64 * interval_secs;
                if secs_ago < 60 {
                    Some(format!("{secs_ago}s"))
                } else if secs_ago < 3600 {
                    Some(format!("{}m", secs_ago / 60))
                } else {
                    Some(format!("{}h", secs_ago / 3600))
                }
            }
            Self::Absolute | Self::Hidden => None, // Would need actual timestamp
        }
    }
}

/// UX-102: Axis margin configuration.
#[derive(Debug, Clone, Copy)]
pub struct AxisMargins {
    /// Width for Y-axis labels (in characters).
    pub y_axis_width: u16,
    /// Height for X-axis labels (in lines).
    pub x_axis_height: u16,
}

impl Default for AxisMargins {
    fn default() -> Self {
        Self {
            y_axis_width: 6,
            x_axis_height: 1,
        }
    }
}

impl AxisMargins {
    /// No margins (labels overlap content).
    pub const NONE: Self = Self {
        y_axis_width: 0,
        x_axis_height: 0,
    };

    /// Compact margins.
    pub const COMPACT: Self = Self {
        y_axis_width: 4,
        x_axis_height: 1,
    };

    /// Standard margins.
    pub const STANDARD: Self = Self {
        y_axis_width: 6,
        x_axis_height: 1,
    };

    /// Wide margins for large numbers.
    pub const WIDE: Self = Self {
        y_axis_width: 10,
        x_axis_height: 2,
    };
}

/// Time-series graph widget.
#[derive(Debug, Clone)]
pub struct BrailleGraph {
    data: Vec<f64>,
    color: Color,
    /// Optional gradient for per-column coloring based on value.
    gradient: Option<Gradient>,
    min: f64,
    max: f64,
    mode: GraphMode,
    label: Option<String>,
    /// UX-102: Axis margin configuration.
    margins: AxisMargins,
    /// UX-117: Time axis display mode.
    time_axis: TimeAxisMode,
    /// UX-104: Show legend for braille characters.
    show_legend: bool,
    bounds: Rect,
}

impl BrailleGraph {
    /// Create a new braille graph.
    #[must_use]
    pub fn new(data: Vec<f64>) -> Self {
        let (min, max) = Self::compute_range(&data);
        Self {
            data,
            color: Color::GREEN,
            gradient: None,
            min,
            max,
            mode: GraphMode::default(),
            label: None,
            margins: AxisMargins::default(),
            time_axis: TimeAxisMode::default(),
            show_legend: false,
            bounds: Rect::new(0.0, 0.0, 0.0, 0.0),
        }
    }

    /// Set the color.
    #[must_use]
    pub fn with_color(mut self, color: Color) -> Self {
        self.color = color;
        self
    }

    /// Set a gradient for per-column coloring based on value.
    /// When set, each column is colored based on its normalized value (0.0-1.0).
    #[must_use]
    pub fn with_gradient(mut self, gradient: Gradient) -> Self {
        self.gradient = Some(gradient);
        self
    }

    /// Set explicit min/max range.
    #[must_use]
    pub fn with_range(mut self, min: f64, max: f64) -> Self {
        debug_assert!(min.is_finite(), "min must be finite");
        debug_assert!(max.is_finite(), "max must be finite");
        self.min = min;
        self.max = max;
        self
    }

    /// Set the render mode.
    #[must_use]
    pub fn with_mode(mut self, mode: GraphMode) -> Self {
        self.mode = mode;
        self
    }

    /// Set the label.
    #[must_use]
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// UX-102: Set axis margins.
    #[must_use]
    pub fn with_margins(mut self, margins: AxisMargins) -> Self {
        self.margins = margins;
        self
    }

    /// UX-117: Set time axis display mode.
    #[must_use]
    pub fn with_time_axis(mut self, mode: TimeAxisMode) -> Self {
        self.time_axis = mode;
        self
    }

    /// UX-104: Enable/disable legend display.
    #[must_use]
    pub fn with_legend(mut self, show: bool) -> Self {
        self.show_legend = show;
        self
    }

    /// Get the effective graph area after accounting for margins.
    fn graph_area(&self) -> Rect {
        let y_offset = self.margins.y_axis_width as f32;
        let x_height = self.margins.x_axis_height as f32;
        Rect::new(
            self.bounds.x + y_offset,
            self.bounds.y,
            (self.bounds.width - y_offset).max(0.0),
            (self.bounds.height - x_height).max(0.0),
        )
    }

    /// Render Y-axis labels in the margin.
    fn render_y_axis(&self, canvas: &mut dyn Canvas) {
        if self.margins.y_axis_width == 0 {
            return;
        }

        let style = TextStyle {
            color: Color::WHITE,
            ..Default::default()
        };

        // Max value at top
        let max_str = format!("{:.0}", self.max);
        canvas.draw_text(&max_str, Point::new(self.bounds.x, self.bounds.y), &style);

        // Min value at bottom of graph area
        let graph_height = (self.bounds.height - self.margins.x_axis_height as f32).max(1.0);
        let min_str = format!("{:.0}", self.min);
        canvas.draw_text(
            &min_str,
            Point::new(self.bounds.x, self.bounds.y + graph_height - 1.0),
            &style,
        );
    }

    /// Render X-axis time labels.
    fn render_x_axis(&self, canvas: &mut dyn Canvas) {
        if self.margins.x_axis_height == 0 {
            return;
        }
        if matches!(self.time_axis, TimeAxisMode::Hidden) {
            return;
        }

        let graph = self.graph_area();
        let y_pos = self.bounds.y + self.bounds.height - 1.0;
        let total = self.data.len();

        let style = TextStyle {
            color: Color::WHITE,
            ..Default::default()
        };

        // Show labels at start, middle, and end
        let positions = [0, total / 2, total.saturating_sub(1)];
        for &idx in &positions {
            if let Some(label) = self.time_axis.format_label(idx, total) {
                let x_frac = if total > 1 {
                    idx as f32 / (total - 1) as f32
                } else {
                    0.5
                };
                let x_pos = graph.x + x_frac * (graph.width - 1.0).max(0.0);
                canvas.draw_text(&label, Point::new(x_pos, y_pos), &style);
            }
        }
    }

    /// Render legend explaining braille patterns.
    fn render_legend(&self, canvas: &mut dyn Canvas) {
        if !self.show_legend {
            return;
        }

        let style = TextStyle {
            color: Color::WHITE,
            ..Default::default()
        };

        // Simple legend showing value mapping
        let legend = format!("⣿={:.0} ⣀={:.0}", self.max, self.min);
        let x = self.bounds.x + self.bounds.width - legend.len() as f32;
        canvas.draw_text(&legend, Point::new(x.max(0.0), self.bounds.y), &style);
    }

    /// Update the data.
    pub fn set_data(&mut self, data: Vec<f64>) {
        let (min, max) = Self::compute_range(&data);
        self.data = data;
        self.min = min;
        self.max = max;
    }

    /// Push a new data point.
    pub fn push(&mut self, value: f64) {
        self.data.push(value);
        if value < self.min {
            self.min = value;
        }
        if value > self.max {
            self.max = value;
        }
    }

    fn compute_range(data: &[f64]) -> (f64, f64) {
        if data.is_empty() {
            return (0.0, 1.0);
        }
        let min = data.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let max = data.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        if (max - min).abs() < f64::EPSILON {
            (min - 0.5, max + 0.5)
        } else {
            (min, max)
        }
    }

    fn normalize(&self, value: f64) -> f64 {
        if (self.max - self.min).abs() < f64::EPSILON {
            0.5
        } else {
            (value - self.min) / (self.max - self.min)
        }
    }

    /// Get color for a normalized value (0.0-1.0).
    /// Uses gradient if set, otherwise returns the fixed color.
    fn color_for_value(&self, normalized: f64) -> Color {
        match &self.gradient {
            Some(gradient) => gradient.sample(normalized),
            None => self.color,
        }
    }

    fn render_braille(&self, canvas: &mut dyn Canvas) {
        let width = self.bounds.width as usize;
        let height = self.bounds.height as usize;
        if width == 0 || height == 0 || self.data.is_empty() {
            return;
        }

        let dots_per_col = 2;
        let dots_per_row = 4;
        let total_dots_x = width * dots_per_col;
        let total_dots_y = height * dots_per_row;

        let step = if self.data.len() > total_dots_x {
            self.data.len() as f64 / total_dots_x as f64
        } else {
            1.0
        };

        // Track dots and values per column for gradient coloring
        let mut dots = vec![vec![false; total_dots_x]; total_dots_y];
        let mut column_values: Vec<f64> = vec![0.0; width];

        for (i, x) in (0..total_dots_x).enumerate() {
            let data_idx = (i as f64 * step) as usize;
            if data_idx >= self.data.len() {
                break;
            }
            let value = self.normalize(self.data[data_idx]);
            let y = ((1.0 - value) * (total_dots_y - 1) as f64).round() as usize;
            if y < total_dots_y {
                dots[y][x] = true;
            }
            // Track max value for each character column
            let char_col = x / dots_per_col;
            if char_col < width && value > column_values[char_col] {
                column_values[char_col] = value;
            }
        }

        for cy in 0..height {
            for (cx, &col_value) in column_values.iter().enumerate().take(width) {
                let mut code_point = 0x2800u32;
                let dot_offsets = [
                    (0, 0, 0x01),
                    (0, 1, 0x02),
                    (0, 2, 0x04),
                    (1, 0, 0x08),
                    (1, 1, 0x10),
                    (1, 2, 0x20),
                    (0, 3, 0x40),
                    (1, 3, 0x80),
                ];

                for (dx, dy, bit) in dot_offsets {
                    let dot_x = cx * dots_per_col + dx;
                    let dot_y = cy * dots_per_row + dy;
                    if dot_y < total_dots_y && dot_x < total_dots_x && dots[dot_y][dot_x] {
                        code_point |= bit;
                    }
                }

                if let Some(c) = char::from_u32(code_point) {
                    // Use per-column color based on value
                    let color = self.color_for_value(col_value);
                    let style = TextStyle {
                        color,
                        ..Default::default()
                    };
                    canvas.draw_text(
                        &c.to_string(),
                        Point::new(self.bounds.x + cx as f32, self.bounds.y + cy as f32),
                        &style,
                    );
                }
            }
        }
    }

    fn render_block(&self, canvas: &mut dyn Canvas) {
        let width = self.bounds.width as usize;
        let height = self.bounds.height as usize;
        if width == 0 || height == 0 || self.data.is_empty() {
            return;
        }

        let total_rows = height * 2;

        let step = if self.data.len() > width {
            self.data.len() as f64 / width as f64
        } else {
            1.0
        };

        // Track both row position and normalized value for each column
        let mut column_data: Vec<(usize, f64)> = Vec::with_capacity(width);
        for x in 0..width {
            let data_idx = (x as f64 * step) as usize;
            if data_idx >= self.data.len() {
                column_data.push((total_rows, 0.0));
                continue;
            }
            let value = self.normalize(self.data[data_idx]);
            let row = ((1.0 - value) * (total_rows - 1) as f64).round() as usize;
            column_data.push((row.min(total_rows - 1), value));
        }

        for cy in 0..height {
            for cx in 0..width {
                let (value_row, normalized) =
                    column_data.get(cx).copied().unwrap_or((total_rows, 0.0));
                let top_row = cy * 2;
                let bottom_row = cy * 2 + 1;

                let top_filled = value_row <= top_row;
                let bottom_filled = value_row <= bottom_row;

                let ch = match (top_filled, bottom_filled) {
                    (true, true) => 'â–ˆ',
                    (true, false) => 'â–€',
                    (false, true) => 'â–„',
                    (false, false) => ' ',
                };

                // Use per-column color based on value
                let color = self.color_for_value(normalized);
                let style = TextStyle {
                    color,
                    ..Default::default()
                };
                canvas.draw_text(
                    &ch.to_string(),
                    Point::new(self.bounds.x + cx as f32, self.bounds.y + cy as f32),
                    &style,
                );
            }
        }
    }

    fn render_tty(&self, canvas: &mut dyn Canvas) {
        let width = self.bounds.width as usize;
        let height = self.bounds.height as usize;
        if width == 0 || height == 0 || self.data.is_empty() {
            return;
        }

        let step = if self.data.len() > width {
            self.data.len() as f64 / width as f64
        } else {
            1.0
        };

        // Track both row position and normalized value for each column
        let mut column_data: Vec<(usize, f64)> = Vec::with_capacity(width);
        for x in 0..width {
            let data_idx = (x as f64 * step) as usize;
            if data_idx >= self.data.len() {
                column_data.push((height, 0.0));
                continue;
            }
            let value = self.normalize(self.data[data_idx]);
            let row = ((1.0 - value) * (height - 1) as f64).round() as usize;
            column_data.push((row.min(height - 1), value));
        }

        for cy in 0..height {
            for cx in 0..width {
                let (value_row, normalized) = column_data.get(cx).copied().unwrap_or((height, 0.0));
                let ch = if value_row == cy { '*' } else { ' ' };

                // Use per-column color based on value
                let color = self.color_for_value(normalized);
                let style = TextStyle {
                    color,
                    ..Default::default()
                };
                canvas.draw_text(
                    &ch.to_string(),
                    Point::new(self.bounds.x + cx as f32, self.bounds.y + cy as f32),
                    &style,
                );
            }
        }
    }
}

impl Brick for BrailleGraph {
    fn brick_name(&self) -> &'static str {
        "braille_graph"
    }

    fn assertions(&self) -> &[BrickAssertion] {
        static ASSERTIONS: &[BrickAssertion] = &[BrickAssertion::max_latency_ms(16)];
        ASSERTIONS
    }

    fn budget(&self) -> BrickBudget {
        BrickBudget::uniform(16)
    }

    fn verify(&self) -> BrickVerification {
        BrickVerification {
            passed: vec![BrickAssertion::max_latency_ms(16)],
            failed: vec![],
            verification_time: Duration::from_micros(10),
        }
    }

    fn to_html(&self) -> String {
        String::new() // TUI-only widget
    }

    fn to_css(&self) -> String {
        String::new() // TUI-only widget
    }
}

impl Widget for BrailleGraph {
    fn type_id(&self) -> TypeId {
        TypeId::of::<Self>()
    }

    fn measure(&self, constraints: Constraints) -> Size {
        let width = constraints.max_width.max(10.0);
        let height = constraints.max_height.max(3.0);
        constraints.constrain(Size::new(width, height))
    }

    fn layout(&mut self, bounds: Rect) -> LayoutResult {
        self.bounds = bounds;
        LayoutResult {
            size: Size::new(bounds.width, bounds.height),
        }
    }

    fn paint(&self, canvas: &mut dyn Canvas) {
        // Early return if bounds are too small or data is empty
        if self.bounds.width < 1.0 || self.bounds.height < 1.0 || self.data.is_empty() {
            return;
        }

        // UX-102: Render Y-axis labels in margin
        self.render_y_axis(canvas);

        // UX-117: Render X-axis time labels
        self.render_x_axis(canvas);

        // UX-104: Render legend
        self.render_legend(canvas);

        // Render the graph data
        match self.mode {
            GraphMode::Braille => self.render_braille(canvas),
            GraphMode::Block => self.render_block(canvas),
            GraphMode::Tty => self.render_tty(canvas),
        }

        if let Some(ref label) = self.label {
            let style = TextStyle {
                color: self.color,
                ..Default::default()
            };
            canvas.draw_text(label, Point::new(self.bounds.x, self.bounds.y), &style);
        }
    }

    fn event(&mut self, _event: &Event) -> Option<Box<dyn Any + Send>> {
        None
    }

    fn children(&self) -> &[Box<dyn Widget>] {
        &[]
    }

    fn children_mut(&mut self) -> &mut [Box<dyn Widget>] {
        &mut []
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use presentar_core::{Canvas, TextStyle};

    struct MockCanvas {
        texts: Vec<(String, Point)>,
    }

    impl MockCanvas {
        fn new() -> Self {
            Self { texts: vec![] }
        }
    }

    impl Canvas for MockCanvas {
        fn fill_rect(&mut self, _rect: Rect, _color: Color) {}
        fn stroke_rect(&mut self, _rect: Rect, _color: Color, _width: f32) {}
        fn draw_text(&mut self, text: &str, position: Point, _style: &TextStyle) {
            self.texts.push((text.to_string(), position));
        }
        fn draw_line(&mut self, _from: Point, _to: Point, _color: Color, _width: f32) {}
        fn fill_circle(&mut self, _center: Point, _radius: f32, _color: Color) {}
        fn stroke_circle(&mut self, _center: Point, _radius: f32, _color: Color, _width: f32) {}
        fn fill_arc(
            &mut self,
            _center: Point,
            _radius: f32,
            _start: f32,
            _end: f32,
            _color: Color,
        ) {
        }
        fn draw_path(&mut self, _points: &[Point], _color: Color, _width: f32) {}
        fn fill_polygon(&mut self, _points: &[Point], _color: Color) {}
        fn push_clip(&mut self, _rect: Rect) {}
        fn pop_clip(&mut self) {}
        fn push_transform(&mut self, _transform: presentar_core::Transform2D) {}
        fn pop_transform(&mut self) {}
    }

    #[test]
    fn test_graph_creation() {
        let graph = BrailleGraph::new(vec![1.0, 2.0, 3.0]);
        assert_eq!(graph.data.len(), 3);
    }

    #[test]
    fn test_graph_assertions_not_empty() {
        let graph = BrailleGraph::new(vec![1.0, 2.0, 3.0]);
        assert!(!graph.assertions().is_empty());
    }

    #[test]
    fn test_graph_verify_pass() {
        let graph = BrailleGraph::new(vec![1.0, 2.0, 3.0]);
        assert!(graph.verify().is_valid());
    }

    #[test]
    fn test_graph_with_color() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_color(Color::RED);
        assert_eq!(graph.color, Color::RED);
    }

    #[test]
    fn test_graph_with_range() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_range(0.0, 100.0);
        assert_eq!(graph.min, 0.0);
        assert_eq!(graph.max, 100.0);
    }

    #[test]
    fn test_graph_with_mode() {
        let graph = BrailleGraph::new(vec![1.0]).with_mode(GraphMode::Block);
        assert_eq!(graph.mode, GraphMode::Block);

        let graph2 = BrailleGraph::new(vec![1.0]).with_mode(GraphMode::Tty);
        assert_eq!(graph2.mode, GraphMode::Tty);
    }

    #[test]
    fn test_graph_with_label() {
        let graph = BrailleGraph::new(vec![1.0]).with_label("CPU Usage");
        assert_eq!(graph.label, Some("CPU Usage".to_string()));
    }

    #[test]
    fn test_graph_set_data() {
        let mut graph = BrailleGraph::new(vec![1.0, 2.0]);
        graph.set_data(vec![10.0, 20.0, 30.0, 40.0]);
        assert_eq!(graph.data.len(), 4);
        assert_eq!(graph.min, 10.0);
        assert_eq!(graph.max, 40.0);
    }

    #[test]
    fn test_graph_push() {
        let mut graph = BrailleGraph::new(vec![5.0, 10.0]);
        graph.push(15.0);
        assert_eq!(graph.data.len(), 3);
        assert_eq!(graph.max, 15.0);

        graph.push(2.0);
        assert_eq!(graph.min, 2.0);
    }

    #[test]
    fn test_graph_empty_data_range() {
        let graph = BrailleGraph::new(vec![]);
        assert_eq!(graph.min, 0.0);
        assert_eq!(graph.max, 1.0);
    }

    #[test]
    fn test_graph_constant_data_range() {
        let graph = BrailleGraph::new(vec![5.0, 5.0, 5.0]);
        assert_eq!(graph.min, 4.5);
        assert_eq!(graph.max, 5.5);
    }

    #[test]
    fn test_graph_normalize() {
        let graph = BrailleGraph::new(vec![0.0, 100.0]);
        assert!((graph.normalize(50.0) - 0.5).abs() < f64::EPSILON);
        assert!((graph.normalize(0.0) - 0.0).abs() < f64::EPSILON);
        assert!((graph.normalize(100.0) - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_graph_normalize_constant() {
        let graph = BrailleGraph::new(vec![5.0, 5.0]);
        assert!((graph.normalize(5.0) - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn test_graph_measure() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]);
        let constraints = Constraints::new(0.0, 100.0, 0.0, 50.0);
        let size = graph.measure(constraints);
        assert!(size.width >= 10.0);
        assert!(size.height >= 3.0);
    }

    #[test]
    fn test_graph_layout() {
        let mut graph = BrailleGraph::new(vec![1.0, 2.0]);
        let bounds = Rect::new(10.0, 20.0, 80.0, 24.0);
        let result = graph.layout(bounds);
        assert_eq!(result.size.width, 80.0);
        assert_eq!(result.size.height, 24.0);
        assert_eq!(graph.bounds, bounds);
    }

    #[test]
    fn test_graph_paint_braille() {
        let mut graph = BrailleGraph::new(vec![0.0, 50.0, 100.0]).with_mode(GraphMode::Braille);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_block() {
        let mut graph = BrailleGraph::new(vec![0.0, 50.0, 100.0]).with_mode(GraphMode::Block);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_tty() {
        let mut graph = BrailleGraph::new(vec![0.0, 50.0, 100.0]).with_mode(GraphMode::Tty);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_with_label() {
        let mut graph = BrailleGraph::new(vec![1.0, 2.0]).with_label("Test");
        graph.bounds = Rect::new(0.0, 0.0, 20.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(canvas.texts.iter().any(|(t, _)| t.contains("Test")));
    }

    #[test]
    fn test_graph_paint_empty_bounds() {
        let mut graph = BrailleGraph::new(vec![1.0, 2.0]);
        graph.bounds = Rect::new(0.0, 0.0, 0.0, 0.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_empty_data() {
        let mut graph = BrailleGraph::new(vec![]);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_event() {
        let mut graph = BrailleGraph::new(vec![1.0]);
        let event = Event::KeyDown {
            key: presentar_core::Key::Enter,
        };
        assert!(graph.event(&event).is_none());
    }

    #[test]
    fn test_graph_children() {
        let graph = BrailleGraph::new(vec![1.0]);
        assert!(graph.children().is_empty());
    }

    #[test]
    fn test_graph_children_mut() {
        let mut graph = BrailleGraph::new(vec![1.0]);
        assert!(graph.children_mut().is_empty());
    }

    #[test]
    fn test_graph_type_id() {
        let graph = BrailleGraph::new(vec![1.0]);
        assert_eq!(Widget::type_id(&graph), TypeId::of::<BrailleGraph>());
    }

    #[test]
    fn test_graph_brick_name() {
        let graph = BrailleGraph::new(vec![1.0]);
        assert_eq!(graph.brick_name(), "braille_graph");
    }

    #[test]
    fn test_graph_budget() {
        let graph = BrailleGraph::new(vec![1.0]);
        let budget = graph.budget();
        assert!(budget.measure_ms > 0);
    }

    #[test]
    fn test_graph_to_html() {
        let graph = BrailleGraph::new(vec![1.0]);
        assert!(graph.to_html().is_empty());
    }

    #[test]
    fn test_graph_to_css() {
        let graph = BrailleGraph::new(vec![1.0]);
        assert!(graph.to_css().is_empty());
    }

    #[test]
    fn test_graph_mode_default() {
        assert_eq!(GraphMode::default(), GraphMode::Braille);
    }

    #[test]
    fn test_graph_large_dataset() {
        let data: Vec<f64> = (0..1000).map(|i| (i as f64).sin()).collect();
        let mut graph = BrailleGraph::new(data);
        graph.bounds = Rect::new(0.0, 0.0, 50.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_block_mode_various_values() {
        let mut graph =
            BrailleGraph::new(vec![0.0, 25.0, 50.0, 75.0, 100.0]).with_mode(GraphMode::Block);
        graph.bounds = Rect::new(0.0, 0.0, 5.0, 4.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_tty_mode_various_values() {
        let mut graph =
            BrailleGraph::new(vec![0.0, 25.0, 50.0, 75.0, 100.0]).with_mode(GraphMode::Tty);
        graph.bounds = Rect::new(0.0, 0.0, 5.0, 4.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    // ========================================================================
    // Additional tests for axis margins and time axis
    // ========================================================================

    #[test]
    fn test_graph_with_margins() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_margins(AxisMargins::WIDE);
        assert_eq!(graph.margins.y_axis_width, 10);
        assert_eq!(graph.margins.x_axis_height, 2);
    }

    #[test]
    fn test_graph_with_margins_none() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_margins(AxisMargins::NONE);
        assert_eq!(graph.margins.y_axis_width, 0);
        assert_eq!(graph.margins.x_axis_height, 0);
    }

    #[test]
    fn test_graph_with_margins_compact() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_margins(AxisMargins::COMPACT);
        assert_eq!(graph.margins.y_axis_width, 4);
        assert_eq!(graph.margins.x_axis_height, 1);
    }

    #[test]
    fn test_graph_with_margins_standard() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_margins(AxisMargins::STANDARD);
        assert_eq!(graph.margins.y_axis_width, 6);
        assert_eq!(graph.margins.x_axis_height, 1);
    }

    #[test]
    fn test_axis_margins_default() {
        let margins = AxisMargins::default();
        assert_eq!(margins.y_axis_width, 6);
        assert_eq!(margins.x_axis_height, 1);
    }

    #[test]
    fn test_graph_with_time_axis_indices() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_time_axis(TimeAxisMode::Indices);
        assert_eq!(graph.time_axis, TimeAxisMode::Indices);
    }

    #[test]
    fn test_graph_with_time_axis_relative() {
        let graph = BrailleGraph::new(vec![1.0, 2.0])
            .with_time_axis(TimeAxisMode::Relative { interval_secs: 5 });
        match graph.time_axis {
            TimeAxisMode::Relative { interval_secs } => assert_eq!(interval_secs, 5),
            _ => panic!("Expected Relative time axis mode"),
        }
    }

    #[test]
    fn test_graph_with_time_axis_absolute() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_time_axis(TimeAxisMode::Absolute);
        assert_eq!(graph.time_axis, TimeAxisMode::Absolute);
    }

    #[test]
    fn test_graph_with_time_axis_hidden() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_time_axis(TimeAxisMode::Hidden);
        assert_eq!(graph.time_axis, TimeAxisMode::Hidden);
    }

    #[test]
    fn test_time_axis_mode_default() {
        assert_eq!(TimeAxisMode::default(), TimeAxisMode::Indices);
    }

    #[test]
    fn test_time_axis_format_label_indices() {
        let mode = TimeAxisMode::Indices;
        assert_eq!(mode.format_label(0, 10), Some("0".to_string()));
        assert_eq!(mode.format_label(5, 10), Some("5".to_string()));
        assert_eq!(mode.format_label(9, 10), Some("9".to_string()));
    }

    #[test]
    fn test_time_axis_format_label_relative_seconds() {
        let mode = TimeAxisMode::Relative { interval_secs: 1 };
        // At index 0 with total 60, that's 60 seconds ago
        assert_eq!(mode.format_label(0, 60), Some("1m".to_string()));
        // At index 59 with total 60, that's 1 second ago
        assert_eq!(mode.format_label(59, 60), Some("1s".to_string()));
        // At index 30 with total 60, that's 30 seconds ago
        assert_eq!(mode.format_label(30, 60), Some("30s".to_string()));
    }

    #[test]
    fn test_time_axis_format_label_relative_minutes() {
        let mode = TimeAxisMode::Relative { interval_secs: 60 };
        // At index 0 with total 10, that's 600 seconds (10 minutes) ago
        assert_eq!(mode.format_label(0, 10), Some("10m".to_string()));
        // At index 5 with total 10, that's 300 seconds (5 minutes) ago
        assert_eq!(mode.format_label(5, 10), Some("5m".to_string()));
    }

    #[test]
    fn test_time_axis_format_label_relative_hours() {
        let mode = TimeAxisMode::Relative {
            interval_secs: 3600,
        };
        // At index 0 with total 5, that's 18000 seconds (5 hours) ago
        assert_eq!(mode.format_label(0, 5), Some("5h".to_string()));
        // At index 3 with total 5, that's 7200 seconds (2 hours) ago
        assert_eq!(mode.format_label(3, 5), Some("2h".to_string()));
    }

    #[test]
    fn test_time_axis_format_label_absolute() {
        let mode = TimeAxisMode::Absolute;
        assert_eq!(mode.format_label(0, 10), None);
    }

    #[test]
    fn test_time_axis_format_label_hidden() {
        let mode = TimeAxisMode::Hidden;
        assert_eq!(mode.format_label(0, 10), None);
    }

    #[test]
    fn test_graph_with_legend() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_legend(true);
        assert!(graph.show_legend);
    }

    #[test]
    fn test_graph_with_legend_disabled() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_legend(false);
        assert!(!graph.show_legend);
    }

    #[test]
    fn test_graph_with_gradient() {
        let gradient = Gradient::two(Color::BLUE, Color::RED);
        let graph = BrailleGraph::new(vec![1.0, 2.0]).with_gradient(gradient);
        assert!(graph.gradient.is_some());
    }

    #[test]
    fn test_graph_color_for_value_without_gradient() {
        let graph = BrailleGraph::new(vec![0.0, 100.0]).with_color(Color::GREEN);
        let color = graph.color_for_value(0.5);
        assert_eq!(color, Color::GREEN);
    }

    #[test]
    fn test_graph_color_for_value_with_gradient() {
        let gradient = Gradient::two(Color::BLUE, Color::RED);
        let graph = BrailleGraph::new(vec![0.0, 100.0]).with_gradient(gradient);
        // Should get different colors at different positions
        let color_low = graph.color_for_value(0.0);
        let color_high = graph.color_for_value(1.0);
        // Colors should differ (one is blue, one is red)
        assert_ne!(color_low, color_high);
    }

    #[test]
    fn test_graph_area_with_margins() {
        let mut graph = BrailleGraph::new(vec![1.0, 2.0]).with_margins(AxisMargins::STANDARD);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 24.0);
        let area = graph.graph_area();
        // y_axis_width = 6, so x starts at 6
        assert_eq!(area.x, 6.0);
        // x_axis_height = 1, so height reduced by 1
        assert_eq!(area.height, 23.0);
        assert_eq!(area.width, 74.0);
    }

    #[test]
    fn test_graph_area_with_no_margins() {
        let mut graph = BrailleGraph::new(vec![1.0, 2.0]).with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 24.0);
        let area = graph.graph_area();
        assert_eq!(area.x, 0.0);
        assert_eq!(area.y, 0.0);
        assert_eq!(area.width, 80.0);
        assert_eq!(area.height, 24.0);
    }

    #[test]
    fn test_graph_paint_with_y_axis() {
        let mut graph =
            BrailleGraph::new(vec![0.0, 50.0, 100.0]).with_margins(AxisMargins::STANDARD);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        // Should render y-axis labels (min and max values)
        let has_max_label = canvas.texts.iter().any(|(t, _)| t.contains("100"));
        let has_min_label = canvas.texts.iter().any(|(t, _)| t.contains("0"));
        assert!(has_max_label || has_min_label);
    }

    #[test]
    fn test_graph_paint_with_x_axis_indices() {
        let mut graph = BrailleGraph::new(vec![0.0, 50.0, 100.0])
            .with_margins(AxisMargins::STANDARD)
            .with_time_axis(TimeAxisMode::Indices);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        // Should render x-axis labels with indices
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_with_x_axis_hidden() {
        let mut graph = BrailleGraph::new(vec![0.0, 50.0, 100.0])
            .with_margins(AxisMargins::STANDARD)
            .with_time_axis(TimeAxisMode::Hidden);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        // Should still render but without x-axis labels
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_with_legend() {
        let mut graph = BrailleGraph::new(vec![0.0, 100.0])
            .with_legend(true)
            .with_margins(AxisMargins::STANDARD);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        // Should render legend with braille characters
        let has_legend = canvas
            .texts
            .iter()
            .any(|(t, _)| t.contains("⣿") || t.contains("⣀"));
        assert!(has_legend);
    }

    #[test]
    fn test_graph_paint_without_legend() {
        let mut graph = BrailleGraph::new(vec![0.0, 100.0])
            .with_legend(false)
            .with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        // Should not render legend
        let has_legend = canvas
            .texts
            .iter()
            .any(|(t, _)| t.contains("⣿=") || t.contains("⣀="));
        assert!(!has_legend);
    }

    #[test]
    fn test_graph_paint_with_no_y_axis_margin() {
        let mut graph = BrailleGraph::new(vec![0.0, 100.0]).with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        // Should render the graph but not y-axis labels at position 0
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_with_gradient_braille() {
        let gradient = Gradient::two(Color::BLUE, Color::RED);
        let mut graph = BrailleGraph::new(vec![0.0, 50.0, 100.0])
            .with_gradient(gradient)
            .with_mode(GraphMode::Braille)
            .with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_with_gradient_block() {
        let gradient = Gradient::two(Color::BLUE, Color::RED);
        let mut graph = BrailleGraph::new(vec![0.0, 50.0, 100.0])
            .with_gradient(gradient)
            .with_mode(GraphMode::Block)
            .with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_paint_with_gradient_tty() {
        let gradient = Gradient::two(Color::BLUE, Color::RED);
        let mut graph = BrailleGraph::new(vec![0.0, 50.0, 100.0])
            .with_gradient(gradient)
            .with_mode(GraphMode::Tty)
            .with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_block_mode_single_point() {
        let mut graph = BrailleGraph::new(vec![50.0]).with_mode(GraphMode::Block);
        graph.bounds = Rect::new(0.0, 0.0, 5.0, 4.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_tty_mode_single_point() {
        let mut graph = BrailleGraph::new(vec![50.0]).with_mode(GraphMode::Tty);
        graph.bounds = Rect::new(0.0, 0.0, 5.0, 4.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_braille_more_data_than_width() {
        let data: Vec<f64> = (0..100).map(|i| i as f64).collect();
        let mut graph = BrailleGraph::new(data)
            .with_mode(GraphMode::Braille)
            .with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_block_more_data_than_width() {
        let data: Vec<f64> = (0..100).map(|i| i as f64).collect();
        let mut graph = BrailleGraph::new(data)
            .with_mode(GraphMode::Block)
            .with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_tty_more_data_than_width() {
        let data: Vec<f64> = (0..100).map(|i| i as f64).collect();
        let mut graph = BrailleGraph::new(data)
            .with_mode(GraphMode::Tty)
            .with_margins(AxisMargins::NONE);
        graph.bounds = Rect::new(0.0, 0.0, 10.0, 5.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_small_bounds_clipping() {
        // Test with bounds smaller than margins would require
        let mut graph = BrailleGraph::new(vec![0.0, 100.0]).with_margins(AxisMargins::WIDE);
        graph.bounds = Rect::new(0.0, 0.0, 5.0, 2.0);
        let area = graph.graph_area();
        // Width should be clamped to 0 since bounds.width (5) - y_axis_width (10) < 0
        assert!(area.width >= 0.0);
        assert!(area.height >= 0.0);
    }

    #[test]
    fn test_graph_x_axis_single_data_point() {
        let mut graph = BrailleGraph::new(vec![50.0])
            .with_margins(AxisMargins::STANDARD)
            .with_time_axis(TimeAxisMode::Indices);
        graph.bounds = Rect::new(0.0, 0.0, 80.0, 10.0);
        let mut canvas = MockCanvas::new();
        graph.paint(&mut canvas);
        // Should handle single data point gracefully
        assert!(!canvas.texts.is_empty());
    }

    #[test]
    fn test_graph_mode_debug() {
        // Test Debug impl for GraphMode
        let mode = GraphMode::Braille;
        let debug_str = format!("{:?}", mode);
        assert!(debug_str.contains("Braille"));
    }

    #[test]
    fn test_time_axis_mode_debug() {
        // Test Debug impl for TimeAxisMode
        let mode = TimeAxisMode::Relative { interval_secs: 60 };
        let debug_str = format!("{:?}", mode);
        assert!(debug_str.contains("Relative"));
        assert!(debug_str.contains("60"));
    }

    #[test]
    fn test_axis_margins_debug() {
        // Test Debug impl for AxisMargins
        let margins = AxisMargins::WIDE;
        let debug_str = format!("{:?}", margins);
        assert!(debug_str.contains("10")); // y_axis_width
        assert!(debug_str.contains("2")); // x_axis_height
    }

    #[test]
    fn test_graph_clone() {
        let graph = BrailleGraph::new(vec![1.0, 2.0, 3.0])
            .with_color(Color::RED)
            .with_label("Test")
            .with_range(0.0, 100.0);
        let cloned = graph.clone();
        assert_eq!(cloned.data, graph.data);
        assert_eq!(cloned.color, graph.color);
        assert_eq!(cloned.label, graph.label);
        assert_eq!(cloned.min, graph.min);
        assert_eq!(cloned.max, graph.max);
    }

    #[test]
    fn test_graph_debug() {
        let graph = BrailleGraph::new(vec![1.0, 2.0]);
        let debug_str = format!("{:?}", graph);
        assert!(debug_str.contains("BrailleGraph"));
    }
}