allure-core 0.1.7

Core types and runtime for Allure test reporting
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
//! Allure data model types for test results, steps, attachments, and containers.

use serde::{Deserialize, Serialize};

use crate::enums::{LabelName, LinkType, ParameterMode, Severity, Stage, Status};

/// Main test result structure written to `{uuid}-result.json`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TestResult {
    /// Unique identifier for this test result
    pub uuid: String,

    /// History ID for tracking test across runs (MD5 of fullName + parameters)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub history_id: Option<String>,

    /// Test case ID for Allure TestOps integration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub test_case_id: Option<String>,

    /// Test name (display title)
    pub name: String,

    /// Fully qualified test name (module::function)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full_name: Option<String>,

    /// Markdown description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// HTML description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description_html: Option<String>,

    /// Test result status
    pub status: Status,

    /// Additional status details (message, trace, flaky, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_details: Option<StatusDetails>,

    /// Test execution stage
    pub stage: Stage,

    /// Test steps
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub steps: Vec<StepResult>,

    /// Test attachments
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<Attachment>,

    /// Test parameters
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,

    /// Test labels (tags, severity, owner, etc.)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub labels: Vec<Label>,

    /// External links (issues, TMS, etc.)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub links: Vec<Link>,

    /// Test start time (Unix timestamp in milliseconds)
    pub start: i64,

    /// Test stop time (Unix timestamp in milliseconds)
    pub stop: i64,
}

impl TestResult {
    /// Creates a new test result with the given name and UUID.
    pub fn new(uuid: String, name: String) -> Self {
        let now = current_time_ms();
        Self {
            uuid,
            history_id: None,
            test_case_id: None,
            name,
            full_name: None,
            description: None,
            description_html: None,
            status: Status::Unknown,
            status_details: None,
            stage: Stage::Running,
            steps: Vec::new(),
            attachments: Vec::new(),
            parameters: Vec::new(),
            labels: Vec::new(),
            links: Vec::new(),
            start: now,
            stop: now,
        }
    }

    /// Adds a label to the test result.
    pub fn add_label(&mut self, name: impl Into<String>, value: impl Into<String>) {
        self.labels.push(Label {
            name: name.into(),
            value: value.into(),
        });
    }

    /// Adds a label using a reserved label name.
    pub fn add_label_name(&mut self, name: LabelName, value: impl Into<String>) {
        self.add_label(name.as_str(), value);
    }

    /// Adds a link to the test result.
    pub fn add_link(&mut self, url: impl Into<String>, name: Option<String>, link_type: LinkType) {
        self.links.push(Link {
            name,
            url: url.into(),
            r#type: Some(link_type),
        });
    }

    /// Adds a parameter to the test result.
    pub fn add_parameter(&mut self, name: impl Into<String>, value: impl Into<String>) {
        self.parameters.push(Parameter {
            name: name.into(),
            value: value.into(),
            excluded: None,
            mode: None,
        });
    }

    /// Adds an attachment to the test result.
    pub fn add_attachment(&mut self, attachment: Attachment) {
        self.attachments.push(attachment);
    }

    /// Adds a step to the test result.
    pub fn add_step(&mut self, step: StepResult) {
        self.steps.push(step);
    }

    /// Sets the test status.
    pub fn set_status(&mut self, status: Status) {
        self.status = status;
    }

    /// Marks the test as finished with the current time.
    pub fn finish(&mut self) {
        self.stop = current_time_ms();
        self.stage = Stage::Finished;
    }

    /// Marks the test as passed.
    pub fn pass(&mut self) {
        self.status = Status::Passed;
        self.finish();
    }

    /// Marks the test as failed with an optional message.
    pub fn fail(&mut self, message: Option<String>, trace: Option<String>) {
        self.status = Status::Failed;
        if message.is_some() || trace.is_some() {
            self.status_details = Some(StatusDetails {
                message,
                trace,
                ..Default::default()
            });
        }
        self.finish();
    }

    /// Marks the test as broken with an optional message.
    pub fn broken(&mut self, message: Option<String>, trace: Option<String>) {
        self.status = Status::Broken;
        if message.is_some() || trace.is_some() {
            self.status_details = Some(StatusDetails {
                message,
                trace,
                ..Default::default()
            });
        }
        self.finish();
    }
}

/// Step result within a test.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StepResult {
    /// Optional UUID for the step
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uuid: Option<String>,

    /// Step name (display title)
    pub name: String,

    /// Step status
    pub status: Status,

    /// Additional status details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_details: Option<StatusDetails>,

    /// Step execution stage
    pub stage: Stage,

    /// Nested steps
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub steps: Vec<StepResult>,

    /// Step attachments
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<Attachment>,

    /// Step parameters
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,

    /// Step start time (Unix timestamp in milliseconds)
    pub start: i64,

    /// Step stop time (Unix timestamp in milliseconds)
    pub stop: i64,
}

impl StepResult {
    /// Creates a new step with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        let now = current_time_ms();
        Self {
            uuid: None,
            name: name.into(),
            status: Status::Unknown,
            status_details: None,
            stage: Stage::Running,
            steps: Vec::new(),
            attachments: Vec::new(),
            parameters: Vec::new(),
            start: now,
            stop: now,
        }
    }

    /// Adds a nested step.
    pub fn add_step(&mut self, step: StepResult) {
        self.steps.push(step);
    }

    /// Adds an attachment to the step.
    pub fn add_attachment(&mut self, attachment: Attachment) {
        self.attachments.push(attachment);
    }

    /// Adds a parameter to the step.
    pub fn add_parameter(&mut self, name: impl Into<String>, value: impl Into<String>) {
        self.parameters.push(Parameter {
            name: name.into(),
            value: value.into(),
            excluded: None,
            mode: None,
        });
    }

    /// Marks the step as passed.
    pub fn pass(&mut self) {
        self.status = Status::Passed;
        self.stage = Stage::Finished;
        self.stop = current_time_ms();
    }

    /// Marks the step as failed.
    pub fn fail(&mut self, message: Option<String>, trace: Option<String>) {
        self.status = Status::Failed;
        self.stage = Stage::Finished;
        self.stop = current_time_ms();
        if message.is_some() || trace.is_some() {
            self.status_details = Some(StatusDetails {
                message,
                trace,
                ..Default::default()
            });
        }
    }

    /// Marks the step as broken.
    pub fn broken(&mut self, message: Option<String>, trace: Option<String>) {
        self.status = Status::Broken;
        self.stage = Stage::Finished;
        self.stop = current_time_ms();
        if message.is_some() || trace.is_some() {
            self.status_details = Some(StatusDetails {
                message,
                trace,
                ..Default::default()
            });
        }
    }
}

/// Additional status details for test results and steps.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StatusDetails {
    /// Whether this is a known issue
    #[serde(skip_serializing_if = "Option::is_none")]
    pub known: Option<bool>,

    /// Whether the test is muted
    #[serde(skip_serializing_if = "Option::is_none")]
    pub muted: Option<bool>,

    /// Whether the test is flaky
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flaky: Option<bool>,

    /// Error message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,

    /// Stack trace
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trace: Option<String>,
}

/// Label for categorizing and filtering tests.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Label {
    /// Label name (can be a reserved name or custom)
    pub name: String,

    /// Label value
    pub value: String,
}

impl Label {
    /// Creates a new label.
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
        }
    }

    /// Creates a label from a reserved label name.
    pub fn from_name(name: LabelName, value: impl Into<String>) -> Self {
        Self::new(name.as_str(), value)
    }

    /// Creates an epic label.
    pub fn epic(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Epic, value)
    }

    /// Creates a feature label.
    pub fn feature(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Feature, value)
    }

    /// Creates a story label.
    pub fn story(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Story, value)
    }

    /// Creates a suite label.
    pub fn suite(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Suite, value)
    }

    /// Creates a parent suite label.
    pub fn parent_suite(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::ParentSuite, value)
    }

    /// Creates a sub-suite label.
    pub fn sub_suite(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::SubSuite, value)
    }

    /// Creates a severity label.
    pub fn severity(severity: Severity) -> Self {
        Self::from_name(LabelName::Severity, severity.as_str())
    }

    /// Creates an owner label.
    pub fn owner(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Owner, value)
    }

    /// Creates a tag label.
    pub fn tag(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Tag, value)
    }

    /// Creates an Allure ID label.
    pub fn allure_id(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::AllureId, value)
    }

    /// Creates a host label.
    pub fn host(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Host, value)
    }

    /// Creates a thread label.
    pub fn thread(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Thread, value)
    }

    /// Creates a framework label.
    pub fn framework(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Framework, value)
    }

    /// Creates a language label.
    pub fn language(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Language, value)
    }

    /// Creates a package label.
    pub fn package(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::Package, value)
    }

    /// Creates a test class label.
    pub fn test_class(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::TestClass, value)
    }

    /// Creates a test method label.
    pub fn test_method(value: impl Into<String>) -> Self {
        Self::from_name(LabelName::TestMethod, value)
    }
}

/// External link associated with a test.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Link {
    /// Link display name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Link URL
    pub url: String,

    /// Link type (issue, tms, or custom)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<LinkType>,
}

impl Link {
    /// Creates a new link.
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            name: None,
            url: url.into(),
            r#type: None,
        }
    }

    /// Creates a link with a name.
    pub fn with_name(url: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            name: Some(name.into()),
            url: url.into(),
            r#type: None,
        }
    }

    /// Creates an issue link.
    pub fn issue(url: impl Into<String>, name: Option<String>) -> Self {
        Self {
            name,
            url: url.into(),
            r#type: Some(LinkType::Issue),
        }
    }

    /// Creates a TMS (Test Management System) link.
    pub fn tms(url: impl Into<String>, name: Option<String>) -> Self {
        Self {
            name,
            url: url.into(),
            r#type: Some(LinkType::Tms),
        }
    }
}

/// Test parameter with optional display options.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Parameter {
    /// Parameter name
    pub name: String,

    /// Parameter value
    pub value: String,

    /// Whether to exclude from history ID calculation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub excluded: Option<bool>,

    /// Display mode (default, hidden, masked)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<ParameterMode>,
}

impl Parameter {
    /// Creates a new parameter.
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            excluded: None,
            mode: None,
        }
    }

    /// Creates a parameter that is excluded from history ID calculation.
    pub fn excluded(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            excluded: Some(true),
            mode: None,
        }
    }

    /// Creates a hidden parameter.
    pub fn hidden(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            excluded: None,
            mode: Some(ParameterMode::Hidden),
        }
    }

    /// Creates a masked parameter (for sensitive values).
    pub fn masked(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            excluded: None,
            mode: Some(ParameterMode::Masked),
        }
    }
}

/// Attachment file reference.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Attachment {
    /// Attachment display name
    pub name: String,

    /// Source file name (UUID-based)
    pub source: String,

    /// MIME type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<String>,
}

impl Attachment {
    /// Creates a new attachment.
    pub fn new(
        name: impl Into<String>,
        source: impl Into<String>,
        mime_type: Option<String>,
    ) -> Self {
        Self {
            name: name.into(),
            source: source.into(),
            r#type: mime_type,
        }
    }
}

/// Container for test fixtures (setup/teardown).
/// Written to `{uuid}-container.json`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TestResultContainer {
    /// Unique identifier for this container
    pub uuid: String,

    /// Container name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// UUIDs of test results that use this fixture
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub children: Vec<String>,

    /// Setup/before fixtures
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub befores: Vec<FixtureResult>,

    /// Teardown/after fixtures
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub afters: Vec<FixtureResult>,

    /// Container start time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start: Option<i64>,

    /// Container stop time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<i64>,
}

impl TestResultContainer {
    /// Creates a new container with the given UUID.
    pub fn new(uuid: String) -> Self {
        Self {
            uuid,
            name: None,
            children: Vec::new(),
            befores: Vec::new(),
            afters: Vec::new(),
            start: None,
            stop: None,
        }
    }

    /// Adds a test result UUID as a child of this container.
    pub fn add_child(&mut self, test_uuid: String) {
        self.children.push(test_uuid);
    }

    /// Adds a before fixture.
    pub fn add_before(&mut self, fixture: FixtureResult) {
        self.befores.push(fixture);
    }

    /// Adds an after fixture.
    pub fn add_after(&mut self, fixture: FixtureResult) {
        self.afters.push(fixture);
    }
}

/// Fixture result (setup or teardown).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FixtureResult {
    /// Fixture name
    pub name: String,

    /// Fixture status
    pub status: Status,

    /// Additional status details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_details: Option<StatusDetails>,

    /// Fixture execution stage
    pub stage: Stage,

    /// Nested steps
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub steps: Vec<StepResult>,

    /// Fixture attachments
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<Attachment>,

    /// Fixture parameters
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,

    /// Fixture start time
    pub start: i64,

    /// Fixture stop time
    pub stop: i64,
}

impl FixtureResult {
    /// Creates a new fixture with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        let now = current_time_ms();
        Self {
            name: name.into(),
            status: Status::Unknown,
            status_details: None,
            stage: Stage::Running,
            steps: Vec::new(),
            attachments: Vec::new(),
            parameters: Vec::new(),
            start: now,
            stop: now,
        }
    }

    /// Marks the fixture as passed.
    pub fn pass(&mut self) {
        self.status = Status::Passed;
        self.stage = Stage::Finished;
        self.stop = current_time_ms();
    }

    /// Marks the fixture as failed.
    pub fn fail(&mut self, message: Option<String>, trace: Option<String>) {
        self.status = Status::Failed;
        self.stage = Stage::Finished;
        self.stop = current_time_ms();
        if message.is_some() || trace.is_some() {
            self.status_details = Some(StatusDetails {
                message,
                trace,
                ..Default::default()
            });
        }
    }
}

/// Category definition for defect classification.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Category {
    /// Category name
    pub name: String,

    /// Statuses that match this category
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub matched_statuses: Vec<Status>,

    /// Regex pattern to match against error message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_regex: Option<String>,

    /// Regex pattern to match against stack trace
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trace_regex: Option<String>,

    /// Whether matching tests should be marked as flaky
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flaky: Option<bool>,
}

impl Category {
    /// Creates a new category with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            matched_statuses: Vec::new(),
            message_regex: None,
            trace_regex: None,
            flaky: None,
        }
    }

    /// Adds a matched status.
    pub fn with_status(mut self, status: Status) -> Self {
        self.matched_statuses.push(status);
        self
    }

    /// Sets the message regex pattern.
    pub fn with_message_regex(mut self, regex: impl Into<String>) -> Self {
        self.message_regex = Some(regex.into());
        self
    }

    /// Sets the trace regex pattern.
    pub fn with_trace_regex(mut self, regex: impl Into<String>) -> Self {
        self.trace_regex = Some(regex.into());
        self
    }

    /// Marks matching tests as flaky.
    pub fn as_flaky(mut self) -> Self {
        self.flaky = Some(true);
        self
    }
}

/// Returns the current time in milliseconds since Unix epoch.
pub fn current_time_ms() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0)
}

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

    #[test]
    fn test_test_result_new() {
        let result = TestResult::new("test-uuid".to_string(), "Test Name".to_string());
        assert_eq!(result.uuid, "test-uuid");
        assert_eq!(result.name, "Test Name");
        assert_eq!(result.status, Status::Unknown);
        assert_eq!(result.stage, Stage::Running);
    }

    #[test]
    fn test_test_result_serialization() {
        let mut result = TestResult::new("uuid-123".to_string(), "My Test".to_string());
        result.add_label_name(LabelName::Epic, "Identity");
        result.add_label_name(LabelName::Severity, "critical");
        result.pass();

        let json = serde_json::to_string_pretty(&result).unwrap();
        assert!(json.contains("\"uuid\": \"uuid-123\""));
        assert!(json.contains("\"name\": \"My Test\""));
        assert!(json.contains("\"status\": \"passed\""));
        assert!(json.contains("\"epic\""));
    }

    #[test]
    fn test_step_result() {
        let mut step = StepResult::new("Step 1");
        step.add_parameter("input", "value");
        step.pass();

        assert_eq!(step.status, Status::Passed);
        assert_eq!(step.stage, Stage::Finished);
        assert_eq!(step.parameters.len(), 1);
    }

    #[test]
    fn test_label_constructors() {
        let epic = Label::epic("My Epic");
        assert_eq!(epic.name, "epic");
        assert_eq!(epic.value, "My Epic");

        let severity = Label::severity(Severity::Critical);
        assert_eq!(severity.name, "severity");
        assert_eq!(severity.value, "critical");
    }

    #[test]
    fn test_link_constructors() {
        let issue = Link::issue("https://jira.com/PROJ-123", Some("PROJ-123".to_string()));
        assert_eq!(issue.r#type, Some(LinkType::Issue));
        assert_eq!(issue.url, "https://jira.com/PROJ-123");
    }

    #[test]
    fn test_parameter_modes() {
        let masked = Parameter::masked("password", "secret123");
        assert_eq!(masked.mode, Some(ParameterMode::Masked));

        let excluded = Parameter::excluded("timestamp", "123456");
        assert_eq!(excluded.excluded, Some(true));
    }

    #[test]
    fn test_container() {
        let mut container = TestResultContainer::new("container-uuid".to_string());
        container.add_child("test-1".to_string());
        container.add_child("test-2".to_string());

        let mut before = FixtureResult::new("setup");
        before.pass();
        container.add_before(before);

        assert_eq!(container.children.len(), 2);
        assert_eq!(container.befores.len(), 1);
    }

    #[test]
    fn test_category() {
        let category = Category::new("Infrastructure Issues")
            .with_status(Status::Broken)
            .with_message_regex(".*timeout.*")
            .as_flaky();

        assert_eq!(category.name, "Infrastructure Issues");
        assert_eq!(category.matched_statuses, vec![Status::Broken]);
        assert_eq!(category.message_regex, Some(".*timeout.*".to_string()));
        assert_eq!(category.flaky, Some(true));
    }

    #[test]
    fn test_test_result_fail_and_broken_details() {
        let mut result = TestResult::new("u1".to_string(), "Name".to_string());
        result.fail(Some("boom".into()), Some("trace".into()));
        assert_eq!(result.status, Status::Failed);
        let details = result.status_details.unwrap();
        assert_eq!(details.message.as_deref(), Some("boom"));
        assert_eq!(details.trace.as_deref(), Some("trace"));
        assert_eq!(result.stage, Stage::Finished);

        let mut broken = TestResult::new("u2".to_string(), "Name2".to_string());
        broken.broken(None, None);
        assert_eq!(broken.status, Status::Broken);
        assert!(broken.status_details.is_none());
        assert_eq!(broken.stage, Stage::Finished);
    }

    #[test]
    fn test_step_result_fail_and_broken_details() {
        let mut step = StepResult::new("fail-step");
        step.fail(Some("oops".into()), None);
        assert_eq!(step.status, Status::Failed);
        assert_eq!(
            step.status_details.unwrap().message.as_deref(),
            Some("oops")
        );

        let mut broken = StepResult::new("broken-step");
        broken.broken(None, Some("trace".into()));
        assert_eq!(broken.status, Status::Broken);
        assert_eq!(
            broken.status_details.unwrap().trace.as_deref(),
            Some("trace")
        );
    }

    #[test]
    fn test_fixture_result_fail_sets_details() {
        let mut fixture = FixtureResult::new("setup");
        fixture.fail(Some("failed".into()), Some("trace".into()));
        assert_eq!(fixture.status, Status::Failed);
        assert_eq!(fixture.stage, Stage::Finished);
        let details = fixture.status_details.unwrap();
        assert_eq!(details.message.as_deref(), Some("failed"));
        assert_eq!(details.trace.as_deref(), Some("trace"));
    }

    #[test]
    fn test_parameter_hidden_flag() {
        let hidden = Parameter::hidden("secret", "value");
        assert_eq!(hidden.mode, Some(ParameterMode::Hidden));
        assert_eq!(hidden.excluded, None);
    }

    #[test]
    fn test_link_with_name_and_default() {
        let named = Link::with_name("https://example.test", "Example");
        assert_eq!(named.name.as_deref(), Some("Example"));
        assert_eq!(named.r#type, None);

        let plain = Link::new("https://example.test");
        assert_eq!(plain.r#type, None);
    }

    #[test]
    fn test_test_result_set_status_and_finish() {
        let mut result = TestResult::new("u3".to_string(), "Name3".to_string());
        result.set_status(Status::Skipped);
        result.finish();
        assert_eq!(result.status, Status::Skipped);
        assert_eq!(result.stage, Stage::Finished);
        assert!(result.stop >= result.start);
    }

    #[test]
    fn test_label_constructors_cover_all_variants() {
        let labels = vec![
            Label::story("story"),
            Label::suite("suite"),
            Label::parent_suite("parent"),
            Label::sub_suite("sub"),
            Label::owner("owner"),
            Label::tag("tag"),
            Label::allure_id("123"),
            Label::host("localhost"),
            Label::thread("thread-1"),
            Label::framework("framework"),
            Label::language("rust"),
            Label::package("pkg"),
            Label::test_class("cls"),
            Label::test_method("meth"),
        ];
        assert_eq!(labels.len(), 14);
        assert!(labels.iter().any(|l| l.name == "testMethod"));
        assert!(labels.iter().any(|l| l.name == "package"));
    }

    #[test]
    fn test_parameter_new_and_link_tms() {
        let param = Parameter::new("key", "val");
        assert_eq!(param.name, "key");
        assert!(param.mode.is_none());
        assert!(param.excluded.is_none());

        let tms = Link::tms("https://tms", Some("TMS-1".into()));
        assert_eq!(tms.r#type, Some(LinkType::Tms));
        assert_eq!(tms.name.as_deref(), Some("TMS-1"));
    }
}