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
//! Loss curve widget for ML training visualization.
//!
//! Implements P204 from SPEC-024 Section 15.2.
//! Supports EMA smoothing for noisy training curves.

use crate::widgets::symbols::BRAILLE_UP;
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;

/// EMA (Exponential Moving Average) configuration.
#[derive(Debug, Clone, Copy)]
pub struct EmaConfig {
    /// Smoothing factor (0.0 = no smoothing, 0.99 = heavy smoothing).
    pub alpha: f64,
}

impl Default for EmaConfig {
    fn default() -> Self {
        Self { alpha: 0.6 }
    }
}

/// A single training series (e.g., train loss, val loss).
#[derive(Debug, Clone)]
pub struct LossSeries {
    /// Series name.
    pub name: String,
    /// Raw loss values per epoch/step.
    pub values: Vec<f64>,
    /// Series color.
    pub color: Color,
    /// Show smoothed line.
    pub smoothed: bool,
}

/// Loss curve widget for visualizing ML training progress.
#[derive(Debug, Clone)]
pub struct LossCurve {
    series: Vec<LossSeries>,
    ema_config: EmaConfig,
    show_raw: bool,
    x_label: Option<String>,
    y_label: Option<String>,
    y_log_scale: bool,
    bounds: Rect,
    // Cached smoothed values (computed once, reused)
    smoothed_cache: Vec<Vec<f64>>,
}

impl LossCurve {
    /// Create a new loss curve widget.
    #[must_use]
    pub fn new() -> Self {
        Self {
            series: Vec::new(),
            ema_config: EmaConfig::default(),
            show_raw: true,
            x_label: Some("Epoch".to_string()),
            y_label: Some("Loss".to_string()),
            y_log_scale: false,
            bounds: Rect::default(),
            smoothed_cache: Vec::new(),
        }
    }

    /// Add a loss series.
    #[must_use]
    pub fn add_series(mut self, name: &str, values: Vec<f64>, color: Color) -> Self {
        self.series.push(LossSeries {
            name: name.to_string(),
            values,
            color,
            smoothed: true,
        });
        self.invalidate_cache();
        self
    }

    /// Set EMA smoothing configuration.
    #[must_use]
    pub fn with_ema(mut self, config: EmaConfig) -> Self {
        self.ema_config = config;
        self.invalidate_cache();
        self
    }

    /// Toggle raw line visibility.
    #[must_use]
    pub fn with_raw_visible(mut self, show: bool) -> Self {
        self.show_raw = show;
        self
    }

    /// Set log scale for Y axis.
    #[must_use]
    pub fn with_log_scale(mut self, log: bool) -> Self {
        self.y_log_scale = log;
        self
    }

    /// Set X axis label.
    #[must_use]
    pub fn with_x_label(mut self, label: &str) -> Self {
        self.x_label = Some(label.to_string());
        self
    }

    /// Set Y axis label.
    #[must_use]
    pub fn with_y_label(mut self, label: &str) -> Self {
        self.y_label = Some(label.to_string());
        self
    }

    /// Update series data.
    pub fn update_series(&mut self, index: usize, values: Vec<f64>) {
        if let Some(series) = self.series.get_mut(index) {
            series.values = values;
            self.invalidate_cache();
        }
    }

    /// Invalidate smoothed value cache.
    fn invalidate_cache(&mut self) {
        self.smoothed_cache.clear();
    }

    /// Compute EMA smoothing for a series.
    /// Uses SIMD-friendly batch operations for large datasets.
    fn compute_ema(&self, values: &[f64]) -> Vec<f64> {
        if values.is_empty() {
            return Vec::new();
        }

        let alpha = self.ema_config.alpha;
        let mut smoothed = Vec::with_capacity(values.len());

        // EMA: S_t = α * X_t + (1-α) * S_{t-1}
        // Sequential processing due to recurrence relation.

        let mut prev = values[0];
        smoothed.push(prev);

        for &val in values.iter().skip(1) {
            if val.is_finite() {
                prev = alpha * val + (1.0 - alpha) * prev;
            }
            // Keep previous value for NaN/Inf
            smoothed.push(prev);
        }

        smoothed
    }

    /// Ensure smoothed cache is populated.
    fn ensure_cache(&mut self) {
        if self.smoothed_cache.len() != self.series.len() {
            self.smoothed_cache = self
                .series
                .iter()
                .map(|s| {
                    if s.smoothed {
                        self.compute_ema(&s.values)
                    } else {
                        s.values.clone()
                    }
                })
                .collect();
        }
    }

    /// Get Y range across all series.
    fn y_range(&self) -> (f64, f64) {
        let mut y_min = f64::INFINITY;
        let mut y_max = f64::NEG_INFINITY;

        for series in &self.series {
            for &v in &series.values {
                if v.is_finite() && v > 0.0 {
                    // Filter non-positive for log scale
                    y_min = y_min.min(v);
                    y_max = y_max.max(v);
                }
            }
        }

        if y_min == f64::INFINITY {
            (0.001, 1.0)
        } else {
            // Add padding
            let padding = (y_max - y_min) * 0.1;
            (
                (y_min - padding).max(if self.y_log_scale { 1e-10 } else { 0.0 }),
                y_max + padding,
            )
        }
    }

    /// Get X range (number of epochs/steps).
    fn x_range(&self) -> (f64, f64) {
        let max_len = self
            .series
            .iter()
            .map(|s| s.values.len())
            .max()
            .unwrap_or(0);
        (0.0, max_len.saturating_sub(1) as f64)
    }

    /// Transform Y value (linear or log scale).
    fn transform_y(&self, y: f64, y_min: f64, y_max: f64) -> f64 {
        if self.y_log_scale {
            let log_min = y_min.max(1e-10).ln();
            let log_max = y_max.ln();
            let log_y = y.max(1e-10).ln();
            (log_y - log_min) / (log_max - log_min)
        } else {
            (y - y_min) / (y_max - y_min)
        }
    }
}

impl Default for LossCurve {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn measure(&self, constraints: Constraints) -> Size {
        Size::new(
            constraints.max_width.min(80.0),
            constraints.max_height.min(20.0),
        )
    }

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

    #[allow(clippy::too_many_lines)]
    fn paint(&self, canvas: &mut dyn Canvas) {
        if self.bounds.width < 15.0 || self.bounds.height < 5.0 {
            return;
        }

        let margin_left = 8.0;
        let margin_bottom = 2.0;
        let margin_right = 2.0;

        let plot_x = self.bounds.x + margin_left;
        let plot_y = self.bounds.y;
        let plot_width = self.bounds.width - margin_left - margin_right;
        let plot_height = self.bounds.height - margin_bottom;

        if plot_width <= 0.0 || plot_height <= 0.0 {
            return;
        }

        let (y_min, y_max) = self.y_range();
        let (x_min, x_max) = self.x_range();

        let label_style = TextStyle {
            color: Color::new(0.6, 0.6, 0.6, 1.0),
            ..Default::default()
        };

        // Draw Y axis labels
        for i in 0..=4 {
            let t = i as f64 / 4.0;
            let y_val = if self.y_log_scale {
                let log_min = y_min.max(1e-10).ln();
                let log_max = y_max.ln();
                (log_min + (log_max - log_min) * (1.0 - t)).exp()
            } else {
                y_min + (y_max - y_min) * (1.0 - t)
            };
            let y_pos = plot_y + plot_height * t as f32;

            if y_pos >= plot_y && y_pos < plot_y + plot_height {
                let label = if y_val < 0.01 {
                    format!("{y_val:.1e}")
                } else if y_val < 1.0 {
                    format!("{y_val:.3}")
                } else {
                    format!("{y_val:.2}")
                };
                canvas.draw_text(
                    &format!("{label:>7}"),
                    Point::new(self.bounds.x, y_pos),
                    &label_style,
                );
            }
        }

        // Draw X axis labels
        let x_ticks = 5.min(x_max as usize);
        for i in 0..=x_ticks {
            let t = i as f64 / x_ticks as f64;
            let x_val = x_min + (x_max - x_min) * t;
            let x_pos = plot_x + plot_width * t as f32;

            if x_pos >= plot_x && x_pos < plot_x + plot_width - 3.0 {
                let label = format!("{x_val:.0}");
                canvas.draw_text(
                    &label,
                    Point::new(x_pos, plot_y + plot_height),
                    &label_style,
                );
            }
        }

        // Draw each series
        for (series_idx, series) in self.series.iter().enumerate() {
            if series.values.is_empty() {
                continue;
            }

            // Get smoothed values from cache
            let smoothed = if series_idx < self.smoothed_cache.len() && series.smoothed {
                &self.smoothed_cache[series_idx]
            } else {
                &series.values
            };

            // Draw raw line (dimmed) if enabled
            if self.show_raw && series.smoothed {
                let raw_style = TextStyle {
                    color: Color::new(
                        series.color.r * 0.4,
                        series.color.g * 0.4,
                        series.color.b * 0.4,
                        0.5,
                    ),
                    ..Default::default()
                };

                self.draw_line_braille(
                    canvas,
                    &series.values,
                    &raw_style,
                    plot_x,
                    plot_y,
                    plot_width,
                    plot_height,
                    y_min,
                    y_max,
                    x_max,
                );
            }

            // Draw smoothed line
            let style = TextStyle {
                color: series.color,
                ..Default::default()
            };

            self.draw_line_braille(
                canvas,
                smoothed,
                &style,
                plot_x,
                plot_y,
                plot_width,
                plot_height,
                y_min,
                y_max,
                x_max,
            );
        }

        // Draw legend
        if !self.series.is_empty() {
            for (i, series) in self.series.iter().enumerate() {
                let style = TextStyle {
                    color: series.color,
                    ..Default::default()
                };
                let legend_y = plot_y + i as f32;
                canvas.draw_text(
                    &format!("─ {}", series.name),
                    Point::new(plot_x + plot_width - 15.0, legend_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 []
    }
}

impl LossCurve {
    /// Draw a line using braille characters.
    #[allow(clippy::too_many_arguments)]
    fn draw_line_braille(
        &self,
        canvas: &mut dyn Canvas,
        values: &[f64],
        style: &TextStyle,
        plot_x: f32,
        plot_y: f32,
        plot_width: f32,
        plot_height: f32,
        y_min: f64,
        y_max: f64,
        x_max: f64,
    ) {
        if values.is_empty() || x_max <= 0.0 {
            return;
        }

        let cols = plot_width as usize;
        let rows = (plot_height * 4.0) as usize; // 4 braille dots per row

        if cols == 0 || rows == 0 {
            return;
        }

        let mut grid = vec![vec![false; rows]; cols];

        // Plot points onto grid
        for (i, &y) in values.iter().enumerate() {
            if !y.is_finite() {
                continue;
            }

            let x_norm = i as f64 / x_max;
            let y_norm = self.transform_y(y, y_min, y_max);

            if !(0.0..=1.0).contains(&x_norm) || !(0.0..=1.0).contains(&y_norm) {
                continue;
            }

            let gx = ((x_norm * (cols - 1) as f64).round() as usize).min(cols.saturating_sub(1));
            let gy =
                (((1.0 - y_norm) * (rows - 1) as f64).round() as usize).min(rows.saturating_sub(1));

            grid[gx][gy] = true;

            // Connect to previous point
            if i > 0 {
                let prev_y = values[i - 1];
                if prev_y.is_finite() {
                    let prev_x_norm = (i - 1) as f64 / x_max;
                    let prev_y_norm = self.transform_y(prev_y, y_min, y_max);

                    if (0.0..=1.0).contains(&prev_x_norm) && (0.0..=1.0).contains(&prev_y_norm) {
                        let prev_gx = ((prev_x_norm * (cols - 1) as f64).round() as usize)
                            .min(cols.saturating_sub(1));
                        let prev_gy = (((1.0 - prev_y_norm) * (rows - 1) as f64).round() as usize)
                            .min(rows.saturating_sub(1));

                        Self::draw_line_bresenham(&mut grid, prev_gx, prev_gy, gx, gy);
                    }
                }
            }
        }

        // Render grid as braille
        let char_rows = plot_height as usize;
        for cy in 0..char_rows {
            for (cx, column) in grid.iter().enumerate() {
                let mut dots = 0u8;
                for dy in 0..4 {
                    let gy = cy * 4 + dy;
                    if gy < rows && column[gy] {
                        dots |= 1 << dy;
                    }
                }

                if dots > 0 {
                    let braille_idx = dots as usize;
                    let ch = if braille_idx < BRAILLE_UP.len() {
                        BRAILLE_UP[braille_idx]
                    } else {
                        '⣿'
                    };
                    canvas.draw_text(
                        &ch.to_string(),
                        Point::new(plot_x + cx as f32, plot_y + cy as f32),
                        style,
                    );
                }
            }
        }
    }

    /// Draw a line between two points using Bresenham's algorithm.
    #[allow(clippy::cast_possible_wrap)]
    fn draw_line_bresenham(grid: &mut [Vec<bool>], x0: usize, y0: usize, x1: usize, y1: usize) {
        let dx = (x1 as isize - x0 as isize).abs();
        let dy = -(y1 as isize - y0 as isize).abs();
        let sx: isize = if x0 < x1 { 1 } else { -1 };
        let sy: isize = if y0 < y1 { 1 } else { -1 };
        let mut err = dx + dy;

        let mut x = x0 as isize;
        let mut y = y0 as isize;

        let cols = grid.len() as isize;
        let rows = if cols > 0 { grid[0].len() as isize } else { 0 };

        loop {
            if x >= 0 && x < cols && y >= 0 && y < rows {
                grid[x as usize][y as usize] = true;
            }

            if x == x1 as isize && y == y1 as isize {
                break;
            }

            let e2 = 2 * err;
            if e2 >= dy {
                err += dy;
                x += sx;
            }
            if e2 <= dx {
                err += dx;
                y += sy;
            }
        }
    }
}

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

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

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

    fn verify(&self) -> BrickVerification {
        let mut passed = Vec::new();
        let mut failed = Vec::new();

        if self.bounds.width >= 15.0 && self.bounds.height >= 5.0 {
            passed.push(BrickAssertion::max_latency_ms(16));
        } else {
            failed.push((
                BrickAssertion::max_latency_ms(16),
                "Size too small".to_string(),
            ));
        }

        BrickVerification {
            passed,
            failed,
            verification_time: Duration::from_micros(5),
        }
    }

    fn to_html(&self) -> String {
        String::new()
    }

    fn to_css(&self) -> String {
        String::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{CellBuffer, DirectTerminalCanvas};

    #[test]
    fn test_loss_curve_creation() {
        let curve =
            LossCurve::new().add_series("train", vec![1.0, 0.8, 0.6, 0.4, 0.3, 0.25], Color::BLUE);
        assert_eq!(curve.series.len(), 1);
    }

    #[test]
    fn test_ema_smoothing() {
        let curve = LossCurve::new().with_ema(EmaConfig { alpha: 0.5 });
        let values = vec![1.0, 0.0, 1.0, 0.0, 1.0];
        let smoothed = curve.compute_ema(&values);

        assert_eq!(smoothed.len(), values.len());
        // First value should be unchanged
        assert!((smoothed[0] - 1.0).abs() < 0.001);
        // Subsequent values should be smoothed
        assert!((smoothed[1] - 0.5).abs() < 0.001);
    }

    #[test]
    fn test_ema_empty_values() {
        let curve = LossCurve::new();
        let smoothed = curve.compute_ema(&[]);
        assert!(smoothed.is_empty());
    }

    #[test]
    fn test_ema_with_nan_values() {
        let curve = LossCurve::new().with_ema(EmaConfig { alpha: 0.5 });
        let values = vec![1.0, f64::NAN, 0.5];
        let smoothed = curve.compute_ema(&values);
        assert_eq!(smoothed.len(), 3);
        assert!((smoothed[0] - 1.0).abs() < 0.001);
        // NaN should keep previous value
        assert!((smoothed[1] - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_ema_config_default() {
        let config = EmaConfig::default();
        assert!((config.alpha - 0.6).abs() < 0.001);
    }

    #[test]
    fn test_log_scale() {
        let curve = LossCurve::new().with_log_scale(true);
        let y_norm = curve.transform_y(1.0, 0.1, 10.0);
        assert!(y_norm > 0.0 && y_norm < 1.0);
    }

    #[test]
    fn test_linear_scale() {
        let curve = LossCurve::new().with_log_scale(false);
        let y_norm = curve.transform_y(5.0, 0.0, 10.0);
        assert!((y_norm - 0.5).abs() < 0.001);
    }

    #[test]
    fn test_multi_series() {
        let curve = LossCurve::new()
            .add_series("train", vec![1.0, 0.5, 0.3], Color::BLUE)
            .add_series("val", vec![1.1, 0.6, 0.4], Color::RED);
        assert_eq!(curve.series.len(), 2);
    }

    #[test]
    fn test_with_x_label() {
        let curve = LossCurve::new().with_x_label("Steps");
        assert_eq!(curve.x_label, Some("Steps".to_string()));
    }

    #[test]
    fn test_with_y_label() {
        let curve = LossCurve::new().with_y_label("MSE");
        assert_eq!(curve.y_label, Some("MSE".to_string()));
    }

    #[test]
    fn test_with_raw_visible() {
        let curve = LossCurve::new().with_raw_visible(false);
        assert!(!curve.show_raw);

        let curve2 = LossCurve::new().with_raw_visible(true);
        assert!(curve2.show_raw);
    }

    #[test]
    fn test_update_series() {
        let mut curve = LossCurve::new().add_series("train", vec![1.0, 0.8], Color::BLUE);
        curve.update_series(0, vec![0.5, 0.3, 0.1]);
        assert_eq!(curve.series[0].values.len(), 3);
    }

    #[test]
    fn test_update_series_invalid_index() {
        let mut curve = LossCurve::new().add_series("train", vec![1.0], Color::BLUE);
        curve.update_series(5, vec![0.5]); // Invalid index, should be ignored
        assert_eq!(curve.series[0].values.len(), 1);
    }

    #[test]
    fn test_y_range_empty() {
        let curve = LossCurve::new();
        let (y_min, y_max) = curve.y_range();
        assert!(y_min < y_max);
    }

    #[test]
    fn test_y_range_with_data() {
        let curve = LossCurve::new().add_series("train", vec![1.0, 2.0, 3.0], Color::BLUE);
        let (y_min, y_max) = curve.y_range();
        assert!(y_min <= 1.0);
        assert!(y_max >= 3.0);
    }

    #[test]
    fn test_x_range() {
        let curve =
            LossCurve::new().add_series("train", vec![1.0, 2.0, 3.0, 4.0, 5.0], Color::BLUE);
        let (x_min, x_max) = curve.x_range();
        assert!((x_min - 0.0).abs() < 0.001);
        assert!((x_max - 4.0).abs() < 0.001);
    }

    #[test]
    fn test_x_range_empty() {
        let curve = LossCurve::new();
        let (x_min, x_max) = curve.x_range();
        assert!((x_min - 0.0).abs() < 0.001);
        assert!((x_max - 0.0).abs() < 0.001);
    }

    #[test]
    fn test_loss_curve_measure() {
        let curve = LossCurve::new();
        let constraints = Constraints::new(0.0, 100.0, 0.0, 50.0);
        let size = curve.measure(constraints);
        assert_eq!(size.width, 80.0);
        assert_eq!(size.height, 20.0);
    }

    #[test]
    fn test_loss_curve_layout_and_paint() {
        let mut curve = LossCurve::new()
            .add_series("train", vec![2.0, 1.5, 1.0, 0.8, 0.6], Color::BLUE)
            .add_series("val", vec![2.2, 1.8, 1.2, 1.0, 0.9], Color::RED);

        let mut buffer = CellBuffer::new(60, 20);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        let result = curve.layout(Rect::new(0.0, 0.0, 60.0, 20.0));
        assert_eq!(result.size.width, 60.0);
        assert_eq!(result.size.height, 20.0);

        curve.paint(&mut canvas);

        // Verify something was rendered
        let cells = buffer.cells();
        let non_empty = cells.iter().filter(|c| !c.symbol.is_empty()).count();
        assert!(non_empty > 0, "Loss curve should render some content");
    }

    #[test]
    fn test_loss_curve_paint_with_log_scale() {
        let mut curve = LossCurve::new().with_log_scale(true).add_series(
            "train",
            vec![1.0, 0.1, 0.01, 0.001],
            Color::BLUE,
        );

        let mut buffer = CellBuffer::new(60, 20);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        curve.layout(Rect::new(0.0, 0.0, 60.0, 20.0));
        curve.paint(&mut canvas);
    }

    #[test]
    fn test_loss_curve_paint_small_bounds() {
        let mut curve = LossCurve::new().add_series("train", vec![1.0, 0.5], Color::BLUE);

        let mut buffer = CellBuffer::new(10, 3);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        curve.layout(Rect::new(0.0, 0.0, 10.0, 3.0));
        curve.paint(&mut canvas);
        // Should not crash with small bounds
    }

    #[test]
    fn test_loss_curve_paint_with_raw_hidden() {
        let mut curve = LossCurve::new().with_raw_visible(false).add_series(
            "train",
            vec![1.0, 0.8, 0.6, 0.4],
            Color::BLUE,
        );

        let mut buffer = CellBuffer::new(60, 20);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        curve.layout(Rect::new(0.0, 0.0, 60.0, 20.0));
        curve.paint(&mut canvas);
    }

    #[test]
    fn test_loss_curve_paint_noisy_data() {
        let values: Vec<f64> = (0..100)
            .map(|i| 2.0 * (-i as f64 / 30.0).exp() + 0.1 * (i as f64 * 0.5).sin())
            .collect();

        let mut curve = LossCurve::new()
            .with_ema(EmaConfig { alpha: 0.1 })
            .add_series("train", values, Color::BLUE);

        let mut buffer = CellBuffer::new(80, 25);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        curve.layout(Rect::new(0.0, 0.0, 80.0, 25.0));
        curve.paint(&mut canvas);
    }

    #[test]
    fn test_loss_curve_paint_with_nan() {
        let values = vec![1.0, f64::NAN, 0.5, f64::INFINITY, 0.3];
        let mut curve = LossCurve::new().add_series("train", values, Color::BLUE);

        let mut buffer = CellBuffer::new(60, 20);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        curve.layout(Rect::new(0.0, 0.0, 60.0, 20.0));
        curve.paint(&mut canvas);
    }

    #[test]
    fn test_loss_curve_ensure_cache() {
        let mut curve = LossCurve::new().add_series("train", vec![1.0, 0.5, 0.3], Color::BLUE);
        curve.ensure_cache();
        assert_eq!(curve.smoothed_cache.len(), 1);
        assert_eq!(curve.smoothed_cache[0].len(), 3);
    }

    #[test]
    fn test_loss_curve_assertions() {
        let curve = LossCurve::default();
        assert!(!curve.assertions().is_empty());
    }

    #[test]
    fn test_loss_curve_verify_valid() {
        let mut curve = LossCurve::default();
        curve.bounds = Rect::new(0.0, 0.0, 80.0, 20.0);
        assert!(curve.verify().is_valid());
    }

    #[test]
    fn test_loss_curve_verify_invalid() {
        let mut curve = LossCurve::default();
        curve.bounds = Rect::new(0.0, 0.0, 10.0, 3.0);
        assert!(!curve.verify().is_valid());
    }

    #[test]
    fn test_loss_curve_children() {
        let curve = LossCurve::default();
        assert!(curve.children().is_empty());
    }

    #[test]
    fn test_loss_curve_children_mut() {
        let mut curve = LossCurve::default();
        assert!(curve.children_mut().is_empty());
    }

    #[test]
    fn test_loss_curve_brick_name() {
        let curve = LossCurve::new();
        assert_eq!(curve.brick_name(), "LossCurve");
    }

    #[test]
    fn test_loss_curve_budget() {
        let curve = LossCurve::new();
        let budget = curve.budget();
        assert!(budget.layout_ms > 0);
    }

    #[test]
    fn test_loss_curve_to_html() {
        let curve = LossCurve::new();
        assert!(curve.to_html().is_empty());
    }

    #[test]
    fn test_loss_curve_to_css() {
        let curve = LossCurve::new();
        assert!(curve.to_css().is_empty());
    }

    #[test]
    fn test_loss_curve_type_id() {
        let curve = LossCurve::new();
        let type_id = Widget::type_id(&curve);
        assert_eq!(type_id, TypeId::of::<LossCurve>());
    }

    #[test]
    fn test_loss_curve_event() {
        let mut curve = LossCurve::new();
        let event = Event::Resize {
            width: 80.0,
            height: 24.0,
        };
        assert!(curve.event(&event).is_none());
    }

    #[test]
    fn test_empty_series() {
        let curve = LossCurve::new().add_series("empty", vec![], Color::BLUE);
        let (y_min, y_max) = curve.y_range();
        assert!(y_min < y_max);
    }

    #[test]
    fn test_bresenham_line() {
        let mut grid = vec![vec![false; 10]; 10];
        LossCurve::draw_line_bresenham(&mut grid, 0, 0, 9, 9);
        // Check diagonal has points
        assert!(grid[0][0]);
        assert!(grid[9][9]);
    }

    #[test]
    fn test_bresenham_line_reverse() {
        let mut grid = vec![vec![false; 10]; 10];
        LossCurve::draw_line_bresenham(&mut grid, 9, 9, 0, 0);
        assert!(grid[0][0]);
        assert!(grid[9][9]);
    }

    #[test]
    fn test_bresenham_horizontal() {
        let mut grid = vec![vec![false; 10]; 10];
        LossCurve::draw_line_bresenham(&mut grid, 0, 5, 9, 5);
        for x in 0..10 {
            assert!(grid[x][5]);
        }
    }

    #[test]
    fn test_bresenham_vertical() {
        let mut grid = vec![vec![false; 10]; 10];
        LossCurve::draw_line_bresenham(&mut grid, 5, 0, 5, 9);
        for y in 0..10 {
            assert!(grid[5][y]);
        }
    }
}