aprender-test-lib 0.37.0

Probar: Rust-native testing framework with pixel coverage, TUI snapshots, and visual regression
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
//! BrickHouse: Budgeted Composition of Bricks (PROBAR-SPEC-009)
//!
//! A `BrickHouse` composes multiple bricks with a total performance budget.
//! Individual bricks contribute to the house budget, and the house validates
//! that the sum of brick budgets does not exceed the house budget.
//!
//! # Design Philosophy
//!
//! ```text
//! BrickHouse(1000ms)
//! ├── StatusBrick(50ms)
//! ├── WaveformBrick(100ms)
//! ├── TranscriptionBrick(600ms)
//! └── ControlsBrick(50ms)
//! Total: 800ms < 1000ms budget ✓
//! ```
//!
//! # Jidoka: Stop-the-Line
//!
//! If any brick exceeds its budget, the BrickHouse triggers a Jidoka alert
//! and halts rendering. This prevents cascading performance failures.
//!
//! # Example
//!
//! ```rust,ignore
//! use probar::brick_house::{BrickHouse, BrickHouseBuilder};
//!
//! let house = BrickHouseBuilder::new("whisper-app")
//!     .budget_ms(1000)
//!     .brick(status_brick, 50)
//!     .brick(waveform_brick, 100)
//!     .brick(transcription_brick, 600)
//!     .build()?;
//! ```
//!
//! # References
//!
//! - Toyota Production System: Jidoka (autonomation)
//! - PROBAR-SPEC-009: Bug Hunting Probador

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::brick::{
    Brick, BrickBudget, BrickError, BrickPhase, BrickResult, BrickVerification, BudgetViolation,
};

/// A composed house of bricks with a total performance budget.
///
/// The BrickHouse ensures:
/// 1. Sum of brick budgets ≤ house budget
/// 2. All brick assertions pass before rendering
/// 3. Runtime budget violations trigger Jidoka alerts
#[derive(Debug)]
pub struct BrickHouse {
    /// House name for identification
    name: String,
    /// Total budget for the house
    budget: BrickBudget,
    /// Bricks with their allocated budgets
    bricks: Vec<BrickEntry>,
    /// Budget report from last render
    last_report: Option<BudgetReport>,
}

/// Entry for a brick in the house
struct BrickEntry {
    /// The brick instance
    brick: Arc<dyn Brick>,
    /// Allocated budget (may differ from brick's intrinsic budget)
    allocated_ms: u32,
    /// Last measured render time
    last_render_time: Option<Duration>,
}

impl std::fmt::Debug for BrickEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BrickEntry")
            .field("brick_name", &self.brick.brick_name())
            .field("allocated_ms", &self.allocated_ms)
            .field("last_render_time", &self.last_render_time)
            .finish()
    }
}

/// Report on budget usage after a render cycle
#[derive(Debug, Clone)]
pub struct BudgetReport {
    /// House name
    pub house_name: String,
    /// Total budget allocated
    pub total_budget_ms: u32,
    /// Total time used
    pub total_used_ms: u32,
    /// Individual brick timings
    pub brick_timings: HashMap<String, BrickTiming>,
    /// Any budget violations
    pub violations: Vec<BudgetViolation>,
    /// Timestamp of report
    pub timestamp: std::time::SystemTime,
}

/// Timing information for a single brick
#[derive(Debug, Clone)]
pub struct BrickTiming {
    /// Brick name
    pub name: String,
    /// Allocated budget
    pub budget_ms: u32,
    /// Actual time used
    pub used_ms: u32,
    /// Whether budget was exceeded
    pub exceeded: bool,
}

impl BudgetReport {
    /// Check if the house stayed within budget
    #[must_use]
    pub fn within_budget(&self) -> bool {
        self.violations.is_empty() && self.total_used_ms <= self.total_budget_ms
    }

    /// Get budget utilization as percentage
    #[must_use]
    pub fn utilization(&self) -> f32 {
        if self.total_budget_ms == 0 {
            0.0
        } else {
            (self.total_used_ms as f32 / self.total_budget_ms as f32) * 100.0
        }
    }

    /// Get all violations
    #[must_use]
    pub fn violations(&self) -> &[BudgetViolation] {
        &self.violations
    }
}

impl BrickHouse {
    /// Create a new brick house with the given name and budget
    #[must_use]
    pub fn new(name: impl Into<String>, budget_ms: u32) -> Self {
        Self {
            name: name.into(),
            budget: BrickBudget::uniform(budget_ms),
            bricks: Vec::new(),
            last_report: None,
        }
    }

    /// Add a brick with a specific budget allocation
    ///
    /// # Errors
    ///
    /// Returns an error if adding this brick would exceed the house budget.
    pub fn add_brick(&mut self, brick: Arc<dyn Brick>, budget_ms: u32) -> BrickResult<()> {
        let current_total: u32 = self.bricks.iter().map(|b| b.allocated_ms).sum();
        let new_total = current_total + budget_ms;

        if new_total > self.budget.total_ms {
            return Err(BrickError::BudgetExceeded(BudgetViolation {
                brick_name: brick.brick_name().to_string(),
                budget: self.budget,
                actual: Duration::from_millis(new_total as u64),
                phase: None,
            }));
        }

        self.bricks.push(BrickEntry {
            brick,
            allocated_ms: budget_ms,
            last_render_time: None,
        });

        Ok(())
    }

    /// Get the house name
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the total budget
    #[must_use]
    pub fn budget(&self) -> BrickBudget {
        self.budget
    }

    /// Get the number of bricks
    #[must_use]
    pub fn brick_count(&self) -> usize {
        self.bricks.len()
    }

    /// Get remaining budget after allocations
    #[must_use]
    pub fn remaining_budget_ms(&self) -> u32 {
        let allocated: u32 = self.bricks.iter().map(|b| b.allocated_ms).sum();
        self.budget.total_ms.saturating_sub(allocated)
    }

    /// Verify all bricks in the house
    ///
    /// Returns verification results for all bricks.
    pub fn verify_all(&self) -> Vec<(&str, BrickVerification)> {
        self.bricks
            .iter()
            .map(|entry| {
                let name = entry.brick.brick_name();
                let verification = entry.brick.verify();
                (name, verification)
            })
            .collect()
    }

    /// Check if the house can render (all bricks valid)
    #[must_use]
    pub fn can_render(&self) -> bool {
        self.bricks.iter().all(|entry| entry.brick.can_render())
    }

    /// Render all bricks and track timing
    ///
    /// Returns the generated HTML for all bricks.
    ///
    /// # Errors
    ///
    /// Returns an error if any brick exceeds its budget (Jidoka).
    pub fn render(&mut self) -> BrickResult<String> {
        let mut html_parts = Vec::new();
        let mut timings = HashMap::new();
        let mut violations = Vec::new();
        let mut total_used_ms = 0u32;

        for entry in &mut self.bricks {
            let start = Instant::now();

            // Verify before render
            let verification = entry.brick.verify();
            if !verification.is_valid() {
                let (assertion, reason) = verification
                    .failed
                    .first()
                    .map(|(a, r)| (a.clone(), r.clone()))
                    .unwrap_or_else(|| {
                        (crate::brick::BrickAssertion::TextVisible, "Unknown".into())
                    });
                return Err(BrickError::AssertionFailed { assertion, reason });
            }

            // Generate HTML
            let html = entry.brick.to_html();
            html_parts.push(html);

            let elapsed = start.elapsed();
            let elapsed_ms = elapsed.as_millis() as u32;
            entry.last_render_time = Some(elapsed);
            total_used_ms += elapsed_ms;

            let exceeded = elapsed_ms > entry.allocated_ms;
            let brick_name = entry.brick.brick_name().to_string();

            timings.insert(
                brick_name.clone(),
                BrickTiming {
                    name: brick_name.clone(),
                    budget_ms: entry.allocated_ms,
                    used_ms: elapsed_ms,
                    exceeded,
                },
            );

            if exceeded {
                violations.push(BudgetViolation {
                    brick_name,
                    budget: BrickBudget::uniform(entry.allocated_ms),
                    actual: elapsed,
                    phase: Some(BrickPhase::Paint),
                });
            }
        }

        // Store report
        self.last_report = Some(BudgetReport {
            house_name: self.name.clone(),
            total_budget_ms: self.budget.total_ms,
            total_used_ms,
            brick_timings: timings,
            violations: violations.clone(),
            timestamp: std::time::SystemTime::now(),
        });

        // Jidoka: stop-the-line on violations
        if !violations.is_empty() {
            return Err(BrickError::BudgetExceeded(
                violations.into_iter().next().expect("violations not empty"),
            ));
        }

        Ok(html_parts.join("\n"))
    }

    /// Get the last budget report
    #[must_use]
    pub fn last_report(&self) -> Option<&BudgetReport> {
        self.last_report.as_ref()
    }

    /// Generate combined CSS for all bricks
    #[must_use]
    pub fn to_css(&self) -> String {
        self.bricks
            .iter()
            .map(|entry| entry.brick.to_css())
            .collect::<Vec<_>>()
            .join("\n")
    }
}

/// Builder for constructing a BrickHouse
pub struct BrickHouseBuilder {
    name: String,
    budget_ms: u32,
    bricks: Vec<(Arc<dyn Brick>, u32)>,
}

impl std::fmt::Debug for BrickHouseBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BrickHouseBuilder")
            .field("name", &self.name)
            .field("budget_ms", &self.budget_ms)
            .field("brick_count", &self.bricks.len())
            .finish()
    }
}

impl BrickHouseBuilder {
    /// Create a new builder with the given house name
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            budget_ms: 1000, // Default 1 second
            bricks: Vec::new(),
        }
    }

    /// Set the total budget in milliseconds
    #[must_use]
    pub fn budget_ms(mut self, ms: u32) -> Self {
        self.budget_ms = ms;
        self
    }

    /// Add a brick with a specific budget allocation
    #[must_use]
    pub fn brick(mut self, brick: Arc<dyn Brick>, budget_ms: u32) -> Self {
        self.bricks.push((brick, budget_ms));
        self
    }

    /// Build the BrickHouse
    ///
    /// # Errors
    ///
    /// Returns an error if the total brick budgets exceed the house budget.
    pub fn build(self) -> BrickResult<BrickHouse> {
        let total_brick_budget: u32 = self.bricks.iter().map(|(_, ms)| *ms).sum();

        if total_brick_budget > self.budget_ms {
            return Err(BrickError::BudgetExceeded(BudgetViolation {
                brick_name: self.name.clone(),
                budget: BrickBudget::uniform(self.budget_ms),
                actual: Duration::from_millis(total_brick_budget as u64),
                phase: None,
            }));
        }

        let mut house = BrickHouse::new(self.name, self.budget_ms);
        for (brick, budget) in self.bricks {
            house.add_brick(brick, budget)?;
        }

        Ok(house)
    }
}

/// Jidoka alert for budget violations
///
/// This struct captures the context when a brick exceeds its budget,
/// enabling root cause analysis.
#[derive(Debug, Clone)]
pub struct JidokaAlert {
    /// The brick house that triggered the alert
    pub house_name: String,
    /// The specific brick that exceeded budget
    pub brick_name: String,
    /// Budget that was exceeded
    pub budget_ms: u32,
    /// Actual time taken
    pub actual_ms: u32,
    /// Phase where violation occurred
    pub phase: Option<BrickPhase>,
    /// Timestamp of alert
    pub timestamp: std::time::SystemTime,
    /// Stack trace (if available)
    pub stack_trace: Option<String>,
}

impl JidokaAlert {
    /// Create a new alert from a budget violation
    #[must_use]
    pub fn from_violation(house_name: &str, violation: &BudgetViolation) -> Self {
        Self {
            house_name: house_name.to_string(),
            brick_name: violation.brick_name.clone(),
            budget_ms: violation.budget.total_ms,
            actual_ms: violation.actual.as_millis() as u32,
            phase: violation.phase,
            timestamp: std::time::SystemTime::now(),
            stack_trace: None,
        }
    }

    /// Get the overage percentage
    #[must_use]
    pub fn overage_percent(&self) -> f32 {
        if self.budget_ms == 0 {
            0.0
        } else {
            ((self.actual_ms as f32 / self.budget_ms as f32) - 1.0) * 100.0
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::brick::BrickAssertion;

    struct SimpleBrick {
        name: &'static str,
    }

    impl Brick for SimpleBrick {
        fn brick_name(&self) -> &'static str {
            self.name
        }

        fn assertions(&self) -> &[BrickAssertion] {
            &[]
        }

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

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

        fn to_html(&self) -> String {
            format!("<div class=\"{}\">{}</div>", self.name, self.name)
        }

        fn to_css(&self) -> String {
            format!(".{} {{ display: block; }}", self.name)
        }
    }

    #[test]
    fn test_brick_house_creation() {
        let house = BrickHouse::new("test-house", 1000);
        assert_eq!(house.name(), "test-house");
        assert_eq!(house.budget().total_ms, 1000);
        assert_eq!(house.brick_count(), 0);
    }

    #[test]
    fn test_brick_house_add_brick() {
        let mut house = BrickHouse::new("test-house", 1000);
        let brick = Arc::new(SimpleBrick { name: "test" });

        house.add_brick(brick, 100).expect("should add brick");
        assert_eq!(house.brick_count(), 1);
        assert_eq!(house.remaining_budget_ms(), 900);
    }

    #[test]
    fn test_brick_house_budget_exceeded() {
        let mut house = BrickHouse::new("test-house", 100);
        let brick1 = Arc::new(SimpleBrick { name: "brick1" });
        let brick2 = Arc::new(SimpleBrick { name: "brick2" });

        house.add_brick(brick1, 60).expect("should add first brick");
        let result = house.add_brick(brick2, 60);

        assert!(result.is_err());
    }

    #[test]
    fn test_brick_house_builder() {
        let brick1 = Arc::new(SimpleBrick { name: "status" });
        let brick2 = Arc::new(SimpleBrick { name: "content" });

        let house = BrickHouseBuilder::new("app")
            .budget_ms(1000)
            .brick(brick1, 100)
            .brick(brick2, 200)
            .build()
            .expect("should build house");

        assert_eq!(house.brick_count(), 2);
        assert_eq!(house.remaining_budget_ms(), 700);
    }

    #[test]
    fn test_brick_house_builder_exceeds_budget() {
        let brick1 = Arc::new(SimpleBrick { name: "big" });
        let brick2 = Arc::new(SimpleBrick { name: "bigger" });

        let result = BrickHouseBuilder::new("app")
            .budget_ms(100)
            .brick(brick1, 60)
            .brick(brick2, 60)
            .build();

        assert!(result.is_err());
    }

    #[test]
    fn test_brick_house_render() {
        let brick = Arc::new(SimpleBrick { name: "test" });
        let mut house = BrickHouse::new("test-house", 1000);
        house.add_brick(brick, 100).expect("should add brick");

        let html = house.render().expect("should render");
        assert!(html.contains("test"));
    }

    #[test]
    fn test_jidoka_alert() {
        let violation = BudgetViolation {
            brick_name: "slow-brick".into(),
            budget: BrickBudget::uniform(100),
            actual: Duration::from_millis(150),
            phase: Some(BrickPhase::Paint),
        };

        let alert = JidokaAlert::from_violation("app", &violation);
        assert_eq!(alert.overage_percent(), 50.0);
    }

    #[test]
    fn test_budget_report_within_budget() {
        let report = BudgetReport {
            house_name: "test".into(),
            total_budget_ms: 1000,
            total_used_ms: 500,
            brick_timings: HashMap::new(),
            violations: vec![],
            timestamp: std::time::SystemTime::now(),
        };
        assert!(report.within_budget());
        assert_eq!(report.utilization(), 50.0);
    }

    #[test]
    fn test_budget_report_over_budget() {
        let violation = BudgetViolation {
            brick_name: "test".into(),
            budget: BrickBudget::uniform(100),
            actual: Duration::from_millis(150),
            phase: None,
        };
        let report = BudgetReport {
            house_name: "test".into(),
            total_budget_ms: 1000,
            total_used_ms: 1500,
            brick_timings: HashMap::new(),
            violations: vec![violation],
            timestamp: std::time::SystemTime::now(),
        };
        assert!(!report.within_budget());
        assert!(!report.violations().is_empty());
    }

    #[test]
    fn test_budget_report_zero_budget() {
        let report = BudgetReport {
            house_name: "test".into(),
            total_budget_ms: 0,
            total_used_ms: 0,
            brick_timings: HashMap::new(),
            violations: vec![],
            timestamp: std::time::SystemTime::now(),
        };
        assert_eq!(report.utilization(), 0.0);
    }

    #[test]
    fn test_brick_timing() {
        let timing = BrickTiming {
            name: "test".into(),
            budget_ms: 100,
            used_ms: 50,
            exceeded: false,
        };
        assert_eq!(timing.name, "test");
        assert!(!timing.exceeded);
    }

    #[test]
    fn test_brick_timing_exceeded() {
        let timing = BrickTiming {
            name: "slow".into(),
            budget_ms: 100,
            used_ms: 150,
            exceeded: true,
        };
        assert!(timing.exceeded);
    }

    #[test]
    fn test_brick_house_verify_all() {
        let brick1 = Arc::new(SimpleBrick { name: "brick1" });
        let brick2 = Arc::new(SimpleBrick { name: "brick2" });
        let mut house = BrickHouse::new("test", 1000);
        house.add_brick(brick1, 100).unwrap();
        house.add_brick(brick2, 100).unwrap();

        let verifications = house.verify_all();
        assert_eq!(verifications.len(), 2);
    }

    #[test]
    fn test_brick_house_can_render() {
        let brick = Arc::new(SimpleBrick { name: "test" });
        let mut house = BrickHouse::new("test", 1000);
        house.add_brick(brick, 100).unwrap();

        assert!(house.can_render());
    }

    #[test]
    fn test_brick_entry_debug() {
        let brick = Arc::new(SimpleBrick { name: "test" });
        let entry = BrickEntry {
            brick,
            allocated_ms: 100,
            last_render_time: Some(Duration::from_millis(50)),
        };
        let debug_str = format!("{:?}", entry);
        assert!(debug_str.contains("test"));
        assert!(debug_str.contains("100"));
    }

    #[test]
    fn test_brick_house_to_css() {
        let brick1 = Arc::new(SimpleBrick { name: "brick1" });
        let brick2 = Arc::new(SimpleBrick { name: "brick2" });
        let mut house = BrickHouse::new("test", 1000);
        house.add_brick(brick1, 100).unwrap();
        house.add_brick(brick2, 100).unwrap();

        let css = house.to_css();
        assert!(css.contains("brick1"));
        assert!(css.contains("brick2"));
    }

    #[test]
    fn test_brick_house_last_report_none() {
        let house = BrickHouse::new("test", 1000);
        assert!(house.last_report().is_none());
    }

    #[test]
    fn test_brick_house_render_populates_report() {
        let brick = Arc::new(SimpleBrick { name: "test" });
        let mut house = BrickHouse::new("test-house", 1000);
        house.add_brick(brick, 100).unwrap();

        let _ = house.render().unwrap();

        let report = house.last_report();
        assert!(report.is_some());
        let report = report.unwrap();
        assert_eq!(report.house_name, "test-house");
        assert!(report.brick_timings.contains_key("test"));
    }

    #[test]
    fn test_budget_report_violations() {
        let violation = BudgetViolation {
            brick_name: "slow".into(),
            budget: BrickBudget::uniform(100),
            actual: Duration::from_millis(150),
            phase: Some(BrickPhase::Paint),
        };
        let report = BudgetReport {
            house_name: "test".into(),
            total_budget_ms: 1000,
            total_used_ms: 150,
            brick_timings: HashMap::new(),
            violations: vec![violation],
            timestamp: std::time::SystemTime::now(),
        };

        assert!(!report.within_budget());
        assert_eq!(report.violations().len(), 1);
    }

    #[test]
    fn test_brick_house_builder_debug() {
        let builder = BrickHouseBuilder::new("test-app").budget_ms(500);
        let debug_str = format!("{:?}", builder);
        assert!(debug_str.contains("test-app"));
        assert!(debug_str.contains("500"));
    }

    #[test]
    fn test_brick_house_debug() {
        let mut house = BrickHouse::new("test-house", 1000);
        let brick = Arc::new(SimpleBrick { name: "test" });
        house.add_brick(brick, 100).unwrap();

        let debug_str = format!("{:?}", house);
        assert!(debug_str.contains("test-house"));
        assert!(debug_str.contains("1000"));
    }

    #[test]
    fn test_jidoka_alert_zero_budget() {
        let violation = BudgetViolation {
            brick_name: "test".into(),
            budget: BrickBudget::uniform(0),
            actual: Duration::from_millis(10),
            phase: None,
        };

        let alert = JidokaAlert::from_violation("house", &violation);
        assert_eq!(alert.overage_percent(), 0.0);
    }

    #[test]
    fn test_jidoka_alert_fields() {
        let violation = BudgetViolation {
            brick_name: "slow-brick".into(),
            budget: BrickBudget::uniform(100),
            actual: Duration::from_millis(200),
            phase: Some(BrickPhase::Layout),
        };

        let alert = JidokaAlert::from_violation("my-house", &violation);
        assert_eq!(alert.house_name, "my-house");
        assert_eq!(alert.brick_name, "slow-brick");
        assert_eq!(alert.budget_ms, 100);
        assert_eq!(alert.actual_ms, 200);
        assert!(alert.phase.is_some());
        assert!(alert.stack_trace.is_none());
        assert_eq!(alert.overage_percent(), 100.0);
    }

    #[test]
    fn test_brick_entry_debug_no_render_time() {
        let brick = Arc::new(SimpleBrick { name: "test" });
        let entry = BrickEntry {
            brick,
            allocated_ms: 100,
            last_render_time: None,
        };
        let debug_str = format!("{:?}", entry);
        assert!(debug_str.contains("None"));
    }

    // Test for a brick that fails verification
    struct FailingBrick {
        name: &'static str,
    }

    impl Brick for FailingBrick {
        fn brick_name(&self) -> &'static str {
            self.name
        }

        fn assertions(&self) -> &[BrickAssertion] {
            &[BrickAssertion::TextVisible]
        }

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

        fn verify(&self) -> BrickVerification {
            BrickVerification {
                passed: vec![],
                failed: vec![(BrickAssertion::TextVisible, "Text not visible".to_string())],
                verification_time: Duration::from_micros(1),
            }
        }

        fn to_html(&self) -> String {
            format!("<div class=\"{}\">{}</div>", self.name, self.name)
        }

        fn to_css(&self) -> String {
            format!(".{} {{ display: block; }}", self.name)
        }

        fn can_render(&self) -> bool {
            false
        }
    }

    #[test]
    fn test_brick_house_render_failing_brick() {
        let brick = Arc::new(FailingBrick { name: "failing" });
        let mut house = BrickHouse::new("test-house", 1000);
        house.add_brick(brick, 100).unwrap();

        let result = house.render();
        assert!(result.is_err());
    }

    #[test]
    fn test_brick_house_can_render_with_failing_brick() {
        let brick = Arc::new(FailingBrick { name: "failing" });
        let mut house = BrickHouse::new("test-house", 1000);
        house.add_brick(brick, 100).unwrap();

        assert!(!house.can_render());
    }

    #[test]
    fn test_brick_house_multiple_bricks_render() {
        let brick1 = Arc::new(SimpleBrick { name: "header" });
        let brick2 = Arc::new(SimpleBrick { name: "content" });
        let brick3 = Arc::new(SimpleBrick { name: "footer" });

        let mut house = BrickHouse::new("page", 1000);
        house.add_brick(brick1, 100).unwrap();
        house.add_brick(brick2, 200).unwrap();
        house.add_brick(brick3, 100).unwrap();

        let html = house.render().unwrap();
        assert!(html.contains("header"));
        assert!(html.contains("content"));
        assert!(html.contains("footer"));
    }

    #[test]
    fn test_brick_house_builder_with_many_bricks() {
        let brick1 = Arc::new(SimpleBrick { name: "a" });
        let brick2 = Arc::new(SimpleBrick { name: "b" });
        let brick3 = Arc::new(SimpleBrick { name: "c" });

        let house = BrickHouseBuilder::new("multi")
            .budget_ms(1000)
            .brick(brick1, 100)
            .brick(brick2, 200)
            .brick(brick3, 300)
            .build()
            .unwrap();

        assert_eq!(house.brick_count(), 3);
        assert_eq!(house.remaining_budget_ms(), 400);
    }

    #[test]
    fn test_budget_report_utilization_100_percent() {
        let report = BudgetReport {
            house_name: "test".into(),
            total_budget_ms: 100,
            total_used_ms: 100,
            brick_timings: HashMap::new(),
            violations: vec![],
            timestamp: std::time::SystemTime::now(),
        };
        assert_eq!(report.utilization(), 100.0);
    }

    #[test]
    fn test_budget_report_utilization_over_budget() {
        let report = BudgetReport {
            house_name: "test".into(),
            total_budget_ms: 100,
            total_used_ms: 200,
            brick_timings: HashMap::new(),
            violations: vec![],
            timestamp: std::time::SystemTime::now(),
        };
        assert_eq!(report.utilization(), 200.0);
        // Over budget but no violations means it's not within budget
        assert!(!report.within_budget());
    }

    #[test]
    fn test_brick_house_empty_render() {
        let mut house = BrickHouse::new("empty", 1000);
        let html = house.render().unwrap();
        assert!(html.is_empty());
    }

    #[test]
    fn test_brick_house_empty_css() {
        let house = BrickHouse::new("empty", 1000);
        let css = house.to_css();
        assert!(css.is_empty());
    }

    #[test]
    fn test_brick_house_verify_all_empty() {
        let house = BrickHouse::new("empty", 1000);
        let verifications = house.verify_all();
        assert!(verifications.is_empty());
    }
}