aprender-test-lib 0.40.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
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
//! Brick Architecture: Tests ARE the Interface (PROBAR-SPEC-009)
//!
//! This module implements the core Brick abstraction where UI components
//! are defined by their test assertions, not by their implementation.
//!
//! # Design Philosophy
//!
//! The Brick Architecture inverts the traditional UI/Test relationship:
//!
//! ```text
//! Traditional: Widget → Tests
//! Brick:       Brick(Assertions) → Widget + Tests
//! ```
//!
//! A `Brick` defines:
//! 1. **Assertions**: What must be true (contrast ratio, visibility, latency)
//! 2. **Budget**: Performance envelope (max render time in ms)
//! 3. **Events**: State transitions that trigger assertions
//!
//! # Zero-Artifact Architecture (PROBAR-SPEC-009-P7)
//!
//! The following brick types generate all web artifacts from Rust:
//!
//! - [`WorkerBrick`] - Web Worker JavaScript and Rust web_sys bindings
//! - [`EventBrick`] - DOM event handlers
//! - [`AudioBrick`] - AudioWorklet processor code
//!
//! # Popperian Falsification
//!
//! Each assertion is a falsifiable hypothesis. If ANY assertion fails,
//! the brick is falsified and cannot render.
//!
//! # Example
//!
//! ```rust,ignore
//! use probar::brick::{Brick, BrickAssertion, BrickBudget};
//!
//! #[derive(Brick)]
//! #[brick(
//!     html = "div.transcription",
//!     budget_ms = 100,
//!     assertions = [text_visible, contrast_ratio(4.5)]
//! )]
//! struct TranscriptionBrick {
//!     text: String,
//!     is_final: bool,
//! }
//! ```
//!
//! # References
//!
//! - Popper, K. (1959). The Logic of Scientific Discovery
//! - Beizer, B. (1990). Software Testing Techniques
//! - PROBAR-SPEC-009: Bug Hunting Probador

// Zero-Artifact submodules (PROBAR-SPEC-009-P7)
pub mod audio;
pub mod compute;
pub mod deterministic;
pub mod distributed;
pub mod event;
pub mod pipeline;
pub mod tui;
pub mod web_sys_gen;
pub mod widget;
pub mod worker;

// Re-export submodule types
pub use audio::{AudioBrick, AudioParam, RingBufferConfig};
pub use compute::{
    ComputeBrick, ElementwiseOp, ReduceKind, TensorBinding, TensorType, TileOp, TileStrategy,
};
pub use deterministic::{
    BrickHistory, BrickState, DeterministicBrick, DeterministicClock, DeterministicRng,
    ExecutionTrace, GuardSeverity, GuardViolation, GuardedBrick, InvariantGuard, StateValue,
};
pub use distributed::{
    Backend, BackendSelector, BrickCoordinator, BrickDataTracker, BrickInput, BrickMessage,
    BrickOutput, DataLocation, DistributedBrick, ExecutionMetrics, MultiBrickExecutor,
    SchedulerStats, Subscription, TaskSpec, WorkStealingScheduler, WorkStealingTask, WorkerId,
    WorkerQueue, WorkerStats,
};
pub use event::{EventBinding, EventBrick, EventHandler, EventType};
pub use pipeline::{
    AuditEntry, BrickPipeline, BrickStage, Checkpoint, PipelineAuditCollector, PipelineContext,
    PipelineData, PipelineError, PipelineMetadata, PipelineResult, PrivacyTier, StageTrace,
    ValidationLevel, ValidationMessage, ValidationResult,
};
pub use tui::{
    AnalyzerBrick, CielabColor, CollectorBrick, CollectorError, PanelBrick, PanelId, PanelState,
    RingBuffer,
};
pub use web_sys_gen::{
    get_base_url, BlobUrl, CustomEventDispatcher, EventDetail, FetchClient, GeneratedWebSys,
    GenerationMetadata, PerformanceTiming, WebSysError, GENERATION_METADATA,
};
pub use widget::{
    commands_to_gpu_instances, Canvas, Constraints, CornerRadius, DrawCommand, Event, GpuInstance,
    LayoutResult, LineCap, LineJoin, Modifiers, RecordingCanvas, Rect, RenderMetrics, Size,
    StrokeStyle, TextStyle, Transform2D, Widget, WidgetColor, WidgetExt, WidgetMouseButton,
    WidgetPoint,
};
pub use worker::{
    BrickWorkerMessage, BrickWorkerMessageDirection, FieldType, MessageField, WorkerBrick,
    WorkerTransition,
};

use std::time::Duration;

/// Brick assertion that must be verified at runtime.
///
/// Assertions are falsifiable hypotheses about the UI state.
/// If any assertion fails, the brick is falsified.
#[derive(Debug, Clone, PartialEq)]
pub enum BrickAssertion {
    /// Text content must be visible (not hidden, not zero-opacity)
    TextVisible,

    /// WCAG 2.1 AA contrast ratio requirement (4.5:1 for normal text)
    ContrastRatio(f32),

    /// Maximum render latency in milliseconds
    MaxLatencyMs(u32),

    /// Element must be present in DOM
    ElementPresent(String),

    /// Element must be focusable for accessibility
    Focusable,

    /// Custom assertion with name and validation function ID
    Custom {
        /// Assertion name for error reporting
        name: String,
        /// Validation function identifier
        validator_id: u64,
    },
}

impl BrickAssertion {
    /// Create a text visibility assertion
    #[must_use]
    pub const fn text_visible() -> Self {
        Self::TextVisible
    }

    /// Create a contrast ratio assertion (WCAG 2.1 AA)
    #[must_use]
    pub const fn contrast_ratio(ratio: f32) -> Self {
        Self::ContrastRatio(ratio)
    }

    /// Create a max latency assertion
    #[must_use]
    pub const fn max_latency_ms(ms: u32) -> Self {
        Self::MaxLatencyMs(ms)
    }

    /// Create an element presence assertion
    #[must_use]
    pub fn element_present(selector: impl Into<String>) -> Self {
        Self::ElementPresent(selector.into())
    }
}

/// Performance budget for a brick.
///
/// Budgets are enforced at runtime. Exceeding the budget triggers
/// a Jidoka (stop-the-line) alert.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BrickBudget {
    /// Maximum time for measure phase
    pub measure_ms: u32,
    /// Maximum time for layout phase
    pub layout_ms: u32,
    /// Maximum time for paint phase
    pub paint_ms: u32,
    /// Total budget (may be less than sum of phases)
    pub total_ms: u32,
}

impl BrickBudget {
    /// Create a budget with equal distribution across phases
    #[must_use]
    pub const fn uniform(total_ms: u32) -> Self {
        let phase_ms = total_ms / 3;
        Self {
            measure_ms: phase_ms,
            layout_ms: phase_ms,
            paint_ms: phase_ms,
            total_ms,
        }
    }

    /// Create a custom budget with specified phase limits
    #[must_use]
    pub const fn new(measure_ms: u32, layout_ms: u32, paint_ms: u32) -> Self {
        Self {
            measure_ms,
            layout_ms,
            paint_ms,
            total_ms: measure_ms + layout_ms + paint_ms,
        }
    }

    /// Convert to Duration
    #[must_use]
    pub const fn as_duration(&self) -> Duration {
        Duration::from_millis(self.total_ms as u64)
    }
}

impl Default for BrickBudget {
    fn default() -> Self {
        // Default: 16ms total for 60fps
        Self::uniform(16)
    }
}

/// Result of verifying brick assertions
#[derive(Debug, Clone)]
pub struct BrickVerification {
    /// All assertions that passed
    pub passed: Vec<BrickAssertion>,
    /// All assertions that failed with reasons
    pub failed: Vec<(BrickAssertion, String)>,
    /// Time taken to verify
    pub verification_time: Duration,
}

impl BrickVerification {
    /// Check if all assertions passed
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.failed.is_empty()
    }

    /// Get the falsification score (passed / total)
    #[must_use]
    pub fn score(&self) -> f32 {
        let total = self.passed.len() + self.failed.len();
        if total == 0 {
            1.0
        } else {
            self.passed.len() as f32 / total as f32
        }
    }
}

/// Budget violation report
#[derive(Debug, Clone)]
pub struct BudgetViolation {
    /// Name of the brick that violated
    pub brick_name: String,
    /// Budget that was exceeded
    pub budget: BrickBudget,
    /// Actual time taken
    pub actual: Duration,
    /// Phase that exceeded (if known)
    pub phase: Option<BrickPhase>,
}

/// Rendering phase for budget tracking
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BrickPhase {
    /// Measure phase (compute intrinsic size)
    Measure,
    /// Layout phase (position children)
    Layout,
    /// Paint phase (generate draw commands)
    Paint,
}

/// Core Brick trait - the foundation of the Brick Architecture.
///
/// All UI components implement this trait. The trait defines:
/// 1. Assertions that must pass for the brick to be valid
/// 2. Performance budget that must not be exceeded
/// 3. HTML/CSS generation for rendering targets
///
/// # Trait Bound
///
/// Presentar's `Widget` trait requires `Brick`:
/// ```rust,ignore
/// pub trait Widget: Brick + Send + Sync { ... }
/// ```
///
/// This ensures every widget has verifiable assertions and budgets.
pub trait Brick: Send + Sync {
    /// Get the brick's unique type name
    fn brick_name(&self) -> &'static str;

    /// Get all assertions for this brick
    fn assertions(&self) -> &[BrickAssertion];

    /// Get the performance budget
    fn budget(&self) -> BrickBudget;

    /// Verify all assertions against current state
    ///
    /// Returns a verification result with passed/failed assertions.
    fn verify(&self) -> BrickVerification;

    /// Generate HTML for this brick (WASM target)
    ///
    /// Returns the HTML string that represents this brick.
    /// Must be deterministic (same state → same output).
    fn to_html(&self) -> String;

    /// Generate CSS for this brick (WASM target)
    ///
    /// Returns the CSS rules for styling this brick.
    /// Must be deterministic and scoped to avoid conflicts.
    fn to_css(&self) -> String;

    /// Get the test ID for DOM queries
    fn test_id(&self) -> Option<&str> {
        None
    }

    /// Check if this brick can be rendered (all assertions pass)
    fn can_render(&self) -> bool {
        self.verify().is_valid()
    }
}

/// Yuan Gate: Zero-swallow error handling for bricks
///
/// Named after the Yuan dynasty's strict quality standards.
/// Every error must be explicitly handled - no silent drops.
#[derive(Debug, Clone)]
pub enum BrickError {
    /// Assertion failed during verification
    AssertionFailed {
        /// The assertion that failed
        assertion: BrickAssertion,
        /// Reason for failure
        reason: String,
    },

    /// Budget exceeded during rendering
    BudgetExceeded(BudgetViolation),

    /// Invalid state transition
    InvalidTransition {
        /// Current state
        from: String,
        /// Attempted target state
        to: String,
        /// Reason transition is invalid
        reason: String,
    },

    /// Missing required child brick
    MissingChild {
        /// Expected child brick name
        expected: String,
    },

    /// HTML generation failed
    HtmlGenerationFailed {
        /// Reason for failure
        reason: String,
    },
}

impl std::fmt::Display for BrickError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::AssertionFailed { assertion, reason } => {
                write!(f, "Assertion {assertion:?} failed: {reason}")
            }
            Self::BudgetExceeded(violation) => {
                write!(
                    f,
                    "Budget exceeded for {}: {:?} > {:?}",
                    violation.brick_name, violation.actual, violation.budget.total_ms
                )
            }
            Self::InvalidTransition { from, to, reason } => {
                write!(f, "Invalid transition {from} -> {to}: {reason}")
            }
            Self::MissingChild { expected } => {
                write!(f, "Missing required child brick: {expected}")
            }
            Self::HtmlGenerationFailed { reason } => {
                write!(f, "HTML generation failed: {reason}")
            }
        }
    }
}

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

/// Result type for brick operations
pub type BrickResult<T> = Result<T, BrickError>;

#[cfg(test)]
mod tests {
    use super::*;

    struct TestBrick {
        text: String,
        visible: bool,
    }

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

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

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

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

            for assertion in self.assertions() {
                match assertion {
                    BrickAssertion::TextVisible => {
                        if self.visible && !self.text.is_empty() {
                            passed.push(assertion.clone());
                        } else {
                            failed.push((assertion.clone(), "Text not visible".into()));
                        }
                    }
                    BrickAssertion::ContrastRatio(_) => {
                        // Assume pass for test
                        passed.push(assertion.clone());
                    }
                    _ => passed.push(assertion.clone()),
                }
            }

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

        fn to_html(&self) -> String {
            format!(r#"<div class="test-brick">{}</div>"#, self.text)
        }

        fn to_css(&self) -> String {
            ".test-brick { color: #fff; background: #000; }".into()
        }
    }

    #[test]
    fn test_brick_verification_passes() {
        let brick = TestBrick {
            text: "Hello".into(),
            visible: true,
        };

        let result = brick.verify();
        assert!(result.is_valid());
        assert_eq!(result.score(), 1.0);
    }

    #[test]
    fn test_brick_verification_fails() {
        let brick = TestBrick {
            text: String::new(),
            visible: false,
        };

        let result = brick.verify();
        assert!(!result.is_valid());
        assert!(result.score() < 1.0);
    }

    #[test]
    fn test_budget_uniform() {
        let budget = BrickBudget::uniform(30);
        assert_eq!(budget.total_ms, 30);
        assert_eq!(budget.measure_ms, 10);
    }

    #[test]
    fn test_can_render_valid() {
        let brick = TestBrick {
            text: "Hello".into(),
            visible: true,
        };
        assert!(brick.can_render());
    }

    #[test]
    fn test_can_render_invalid() {
        let brick = TestBrick {
            text: String::new(),
            visible: false,
        };
        assert!(!brick.can_render());
    }

    #[test]
    fn test_brick_assertion_constructors() {
        let text_vis = BrickAssertion::text_visible();
        assert!(matches!(text_vis, BrickAssertion::TextVisible));

        let contrast = BrickAssertion::contrast_ratio(4.5);
        assert!(
            matches!(contrast, BrickAssertion::ContrastRatio(r) if (r - 4.5).abs() < f32::EPSILON)
        );

        let latency = BrickAssertion::max_latency_ms(100);
        assert!(matches!(latency, BrickAssertion::MaxLatencyMs(100)));

        let elem = BrickAssertion::element_present("div.test");
        assert!(matches!(elem, BrickAssertion::ElementPresent(s) if s == "div.test"));
    }

    #[test]
    fn test_brick_assertion_focusable() {
        let focusable = BrickAssertion::Focusable;
        assert!(matches!(focusable, BrickAssertion::Focusable));
    }

    #[test]
    fn test_brick_assertion_custom() {
        let custom = BrickAssertion::Custom {
            name: "test_assertion".into(),
            validator_id: 42,
        };
        match custom {
            BrickAssertion::Custom { name, validator_id } => {
                assert_eq!(name, "test_assertion");
                assert_eq!(validator_id, 42);
            }
            _ => panic!("Expected Custom variant"),
        }
    }

    #[test]
    fn test_budget_new() {
        let budget = BrickBudget::new(5, 10, 15);
        assert_eq!(budget.measure_ms, 5);
        assert_eq!(budget.layout_ms, 10);
        assert_eq!(budget.paint_ms, 15);
        assert_eq!(budget.total_ms, 30);
    }

    #[test]
    fn test_budget_default() {
        let budget = BrickBudget::default();
        assert_eq!(budget.total_ms, 16); // 60fps
    }

    #[test]
    fn test_budget_as_duration() {
        let budget = BrickBudget::uniform(100);
        let duration = budget.as_duration();
        assert_eq!(duration, Duration::from_millis(100));
    }

    #[test]
    fn test_verification_score_empty() {
        let verification = BrickVerification {
            passed: vec![],
            failed: vec![],
            verification_time: Duration::from_micros(10),
        };
        assert_eq!(verification.score(), 1.0); // Empty = perfect score
        assert!(verification.is_valid());
    }

    #[test]
    fn test_verification_score_partial() {
        let verification = BrickVerification {
            passed: vec![BrickAssertion::TextVisible],
            failed: vec![(BrickAssertion::Focusable, "Not focusable".into())],
            verification_time: Duration::from_micros(10),
        };
        assert_eq!(verification.score(), 0.5);
        assert!(!verification.is_valid());
    }

    #[test]
    fn test_brick_phase_variants() {
        let measure = BrickPhase::Measure;
        let layout = BrickPhase::Layout;
        let paint = BrickPhase::Paint;

        assert!(matches!(measure, BrickPhase::Measure));
        assert!(matches!(layout, BrickPhase::Layout));
        assert!(matches!(paint, BrickPhase::Paint));
        assert_ne!(measure, layout);
    }

    #[test]
    fn test_budget_violation() {
        let violation = BudgetViolation {
            brick_name: "TestBrick".into(),
            budget: BrickBudget::uniform(16),
            actual: Duration::from_millis(50),
            phase: Some(BrickPhase::Paint),
        };
        assert_eq!(violation.brick_name, "TestBrick");
        assert_eq!(violation.phase, Some(BrickPhase::Paint));
    }

    #[test]
    fn test_brick_to_html_css() {
        let brick = TestBrick {
            text: "Test".into(),
            visible: true,
        };
        let html = brick.to_html();
        let css = brick.to_css();

        assert!(html.contains("test-brick"));
        assert!(html.contains("Test"));
        assert!(css.contains(".test-brick"));
    }

    #[test]
    fn test_brick_name() {
        let brick = TestBrick {
            text: "Test".into(),
            visible: true,
        };
        assert_eq!(brick.brick_name(), "TestBrick");
    }

    #[test]
    fn test_brick_assertions_list() {
        let brick = TestBrick {
            text: "Test".into(),
            visible: true,
        };
        let assertions = brick.assertions();
        assert_eq!(assertions.len(), 2);
        assert!(matches!(assertions[0], BrickAssertion::TextVisible));
    }

    #[test]
    fn test_brick_budget_method() {
        let brick = TestBrick {
            text: "Test".into(),
            visible: true,
        };
        let budget = brick.budget();
        assert_eq!(budget.total_ms, 16);
    }

    // ===== BrickError Display tests =====

    #[test]
    fn test_brick_error_display_assertion_failed() {
        let error = BrickError::AssertionFailed {
            assertion: BrickAssertion::TextVisible,
            reason: "Element is hidden".into(),
        };
        let display = format!("{error}");
        assert!(display.contains("Assertion"));
        assert!(display.contains("TextVisible"));
        assert!(display.contains("Element is hidden"));
    }

    #[test]
    fn test_brick_error_display_budget_exceeded() {
        let violation = BudgetViolation {
            brick_name: "MyBrick".into(),
            budget: BrickBudget::uniform(16),
            actual: Duration::from_millis(100),
            phase: None,
        };
        let error = BrickError::BudgetExceeded(violation);
        let display = format!("{error}");
        assert!(display.contains("Budget exceeded"));
        assert!(display.contains("MyBrick"));
    }

    #[test]
    fn test_brick_error_display_invalid_transition() {
        let error = BrickError::InvalidTransition {
            from: "idle".into(),
            to: "running".into(),
            reason: "Missing prerequisite".into(),
        };
        let display = format!("{error}");
        assert!(display.contains("Invalid transition"));
        assert!(display.contains("idle"));
        assert!(display.contains("running"));
        assert!(display.contains("Missing prerequisite"));
    }

    #[test]
    fn test_brick_error_display_missing_child() {
        let error = BrickError::MissingChild {
            expected: "ChildWidget".into(),
        };
        let display = format!("{error}");
        assert!(display.contains("Missing required child brick"));
        assert!(display.contains("ChildWidget"));
    }

    #[test]
    fn test_brick_error_display_html_generation_failed() {
        let error = BrickError::HtmlGenerationFailed {
            reason: "Template parse error".into(),
        };
        let display = format!("{error}");
        assert!(display.contains("HTML generation failed"));
        assert!(display.contains("Template parse error"));
    }

    #[test]
    fn test_brick_error_is_std_error() {
        let error: &dyn std::error::Error = &BrickError::MissingChild {
            expected: "Test".into(),
        };
        // Verify Error trait is implemented
        assert!(error.source().is_none());
    }

    // ===== test_id default implementation =====

    #[test]
    fn test_brick_test_id_default() {
        let brick = TestBrick {
            text: "Test".into(),
            visible: true,
        };
        // Default implementation returns None
        assert!(brick.test_id().is_none());
    }

    // ===== BrickAssertion Clone and PartialEq =====

    #[test]
    fn test_brick_assertion_clone() {
        let original = BrickAssertion::Custom {
            name: "custom_check".into(),
            validator_id: 123,
        };
        let cloned = original.clone();
        assert_eq!(original, cloned);
    }

    #[test]
    fn test_brick_assertion_partial_eq() {
        let a1 = BrickAssertion::ContrastRatio(4.5);
        let a2 = BrickAssertion::ContrastRatio(4.5);
        let a3 = BrickAssertion::ContrastRatio(7.0);

        assert_eq!(a1, a2);
        assert_ne!(a1, a3);
    }

    #[test]
    fn test_brick_assertion_element_present_eq() {
        let a1 = BrickAssertion::element_present("div.test");
        let a2 = BrickAssertion::element_present("div.test");
        let a3 = BrickAssertion::element_present("span.other");

        assert_eq!(a1, a2);
        assert_ne!(a1, a3);
    }

    // ===== BrickBudget Clone and PartialEq =====

    #[test]
    fn test_brick_budget_clone() {
        let original = BrickBudget::new(5, 10, 15);
        let cloned = original;
        assert_eq!(original, cloned);
    }

    #[test]
    fn test_brick_budget_partial_eq() {
        let b1 = BrickBudget::new(5, 10, 15);
        let b2 = BrickBudget::new(5, 10, 15);
        let b3 = BrickBudget::new(1, 2, 3);

        assert_eq!(b1, b2);
        assert_ne!(b1, b3);
    }

    // ===== BrickVerification Clone and Debug =====

    #[test]
    fn test_brick_verification_clone() {
        let original = BrickVerification {
            passed: vec![BrickAssertion::TextVisible],
            failed: vec![(BrickAssertion::Focusable, "Not focusable".into())],
            verification_time: Duration::from_micros(50),
        };
        let cloned = original;
        assert_eq!(cloned.passed.len(), 1);
        assert_eq!(cloned.failed.len(), 1);
        assert_eq!(cloned.verification_time, Duration::from_micros(50));
    }

    #[test]
    fn test_brick_verification_debug() {
        let verification = BrickVerification {
            passed: vec![BrickAssertion::TextVisible],
            failed: vec![],
            verification_time: Duration::from_micros(100),
        };
        let debug_str = format!("{verification:?}");
        assert!(debug_str.contains("BrickVerification"));
        assert!(debug_str.contains("passed"));
        assert!(debug_str.contains("TextVisible"));
    }

    // ===== BudgetViolation Clone and Debug =====

    #[test]
    fn test_budget_violation_clone() {
        let original = BudgetViolation {
            brick_name: "ClonedBrick".into(),
            budget: BrickBudget::uniform(16),
            actual: Duration::from_millis(32),
            phase: Some(BrickPhase::Measure),
        };
        let cloned = original;
        assert_eq!(cloned.brick_name, "ClonedBrick");
        assert_eq!(cloned.phase, Some(BrickPhase::Measure));
    }

    #[test]
    fn test_budget_violation_debug() {
        let violation = BudgetViolation {
            brick_name: "DebugBrick".into(),
            budget: BrickBudget::uniform(16),
            actual: Duration::from_millis(50),
            phase: None,
        };
        let debug_str = format!("{violation:?}");
        assert!(debug_str.contains("BudgetViolation"));
        assert!(debug_str.contains("DebugBrick"));
    }

    #[test]
    fn test_budget_violation_no_phase() {
        let violation = BudgetViolation {
            brick_name: "NoPhaseBrick".into(),
            budget: BrickBudget::uniform(16),
            actual: Duration::from_millis(50),
            phase: None,
        };
        assert!(violation.phase.is_none());
    }

    // ===== BrickPhase Copy and Clone =====

    #[test]
    fn test_brick_phase_copy() {
        let phase = BrickPhase::Layout;
        let copied = phase;
        assert_eq!(phase, copied);
    }

    #[test]
    fn test_brick_phase_clone() {
        let phase = BrickPhase::Paint;
        #[allow(clippy::clone_on_copy)]
        let cloned = phase.clone();
        assert_eq!(phase, cloned);
    }

    #[test]
    fn test_brick_phase_debug() {
        let phase = BrickPhase::Measure;
        let debug_str = format!("{phase:?}");
        assert_eq!(debug_str, "Measure");
    }

    // ===== BrickAssertion Debug =====

    #[test]
    fn test_brick_assertion_debug() {
        let assertion = BrickAssertion::MaxLatencyMs(100);
        let debug_str = format!("{assertion:?}");
        assert!(debug_str.contains("MaxLatencyMs"));
        assert!(debug_str.contains("100"));
    }

    #[test]
    fn test_brick_assertion_custom_debug() {
        let assertion = BrickAssertion::Custom {
            name: "my_validator".into(),
            validator_id: 999,
        };
        let debug_str = format!("{assertion:?}");
        assert!(debug_str.contains("Custom"));
        assert!(debug_str.contains("my_validator"));
        assert!(debug_str.contains("999"));
    }

    // ===== BrickBudget Debug =====

    #[test]
    fn test_brick_budget_debug() {
        let budget = BrickBudget::new(5, 10, 15);
        let debug_str = format!("{budget:?}");
        assert!(debug_str.contains("BrickBudget"));
        assert!(debug_str.contains("measure_ms"));
        assert!(debug_str.contains('5'));
    }

    // ===== BrickError Clone and Debug =====

    #[test]
    fn test_brick_error_clone() {
        let original = BrickError::MissingChild {
            expected: "SomeChild".into(),
        };
        let cloned = original;
        match cloned {
            BrickError::MissingChild { expected } => {
                assert_eq!(expected, "SomeChild");
            }
            _ => panic!("Expected MissingChild variant"),
        }
    }

    #[test]
    fn test_brick_error_debug() {
        let error = BrickError::HtmlGenerationFailed {
            reason: "Syntax error".into(),
        };
        let debug_str = format!("{error:?}");
        assert!(debug_str.contains("HtmlGenerationFailed"));
        assert!(debug_str.contains("Syntax error"));
    }

    // ===== Edge cases =====

    #[test]
    fn test_verification_all_failed() {
        let verification = BrickVerification {
            passed: vec![],
            failed: vec![
                (BrickAssertion::TextVisible, "Hidden".into()),
                (BrickAssertion::Focusable, "Not focusable".into()),
            ],
            verification_time: Duration::from_micros(10),
        };
        assert_eq!(verification.score(), 0.0);
        assert!(!verification.is_valid());
    }

    #[test]
    fn test_verification_all_passed() {
        let verification = BrickVerification {
            passed: vec![
                BrickAssertion::TextVisible,
                BrickAssertion::Focusable,
                BrickAssertion::ContrastRatio(4.5),
            ],
            failed: vec![],
            verification_time: Duration::from_micros(10),
        };
        assert_eq!(verification.score(), 1.0);
        assert!(verification.is_valid());
    }

    #[test]
    fn test_budget_uniform_edge_case() {
        // Test with value that doesn't divide evenly by 3
        let budget = BrickBudget::uniform(10);
        assert_eq!(budget.measure_ms, 3);
        assert_eq!(budget.layout_ms, 3);
        assert_eq!(budget.paint_ms, 3);
        // total_ms is preserved exactly
        assert_eq!(budget.total_ms, 10);
    }

    #[test]
    fn test_budget_zero() {
        let budget = BrickBudget::uniform(0);
        assert_eq!(budget.measure_ms, 0);
        assert_eq!(budget.layout_ms, 0);
        assert_eq!(budget.paint_ms, 0);
        assert_eq!(budget.total_ms, 0);
        assert_eq!(budget.as_duration(), Duration::from_millis(0));
    }

    // ===== Test brick with custom test_id =====

    struct TestBrickWithId {
        id: &'static str,
    }

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

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

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

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

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

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

        fn test_id(&self) -> Option<&str> {
            Some(self.id)
        }
    }

    #[test]
    fn test_brick_with_custom_test_id() {
        let brick = TestBrickWithId { id: "my-test-id" };
        assert_eq!(brick.test_id(), Some("my-test-id"));
    }

    #[test]
    fn test_brick_with_id_can_render() {
        let brick = TestBrickWithId { id: "test" };
        // Empty assertions means valid
        assert!(brick.can_render());
    }

    // ===== Additional assertion variant coverage =====

    #[test]
    fn test_all_assertion_variants_are_covered() {
        // Ensure we exercise all variants for coverage
        let variants: Vec<BrickAssertion> = vec![
            BrickAssertion::TextVisible,
            BrickAssertion::ContrastRatio(4.5),
            BrickAssertion::MaxLatencyMs(100),
            BrickAssertion::ElementPresent("div".into()),
            BrickAssertion::Focusable,
            BrickAssertion::Custom {
                name: "test".into(),
                validator_id: 1,
            },
        ];

        for variant in &variants {
            // Exercise Debug
            let _ = format!("{variant:?}");
            // Exercise Clone
            let cloned = variant.clone();
            // Exercise PartialEq
            assert_eq!(variant, &cloned);
        }
    }

    // ===== Brick trait method coverage for TestBrick =====

    #[test]
    fn test_brick_verify_with_empty_text_visible() {
        // Edge case: visible = true but text is empty
        let brick = TestBrick {
            text: String::new(),
            visible: true,
        };
        let result = brick.verify();
        // Should fail because text is empty even though visible is true
        assert!(!result.is_valid());
    }

    #[test]
    fn test_brick_verify_with_text_not_visible() {
        // Edge case: text is present but not visible
        let brick = TestBrick {
            text: "Some text".into(),
            visible: false,
        };
        let result = brick.verify();
        // Should fail because visible is false
        assert!(!result.is_valid());
    }
}