aprender-test-lib 0.39.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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
//! HAR (HTTP Archive) Recording and Playback (Feature G.2)
//!
//! Implements HAR 1.2 format for recording and replaying HTTP traffic.
//! This enables reproducible E2E tests by capturing network interactions.
//!
//! ## EXTREME TDD: Tests written FIRST per Popperian falsification
//!
//! ## Toyota Way Application
//!
//! - **Mieruka**: HAR files make network interactions visible and auditable
//! - **Poka-Yoke**: Type-safe HAR structures prevent invalid recordings
//! - **Jidoka**: Immediate feedback on HAR parsing/validation errors

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

// =============================================================================
// HAR 1.2 Format Structures
// =============================================================================

/// HAR file root structure (HAR 1.2 specification)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Har {
    /// HAR log container
    pub log: HarLog,
}

impl Har {
    /// Create a new empty HAR file
    #[must_use]
    pub fn new() -> Self {
        Self { log: HarLog::new() }
    }

    /// Parse HAR from JSON string
    ///
    /// # Errors
    ///
    /// Returns error if JSON parsing fails
    pub fn from_json(json: &str) -> Result<Self, HarError> {
        serde_json::from_str(json).map_err(|e| HarError::ParseError(e.to_string()))
    }

    /// Serialize HAR to JSON string
    ///
    /// # Errors
    ///
    /// Returns error if serialization fails
    pub fn to_json(&self) -> Result<String, HarError> {
        serde_json::to_string_pretty(self).map_err(|e| HarError::SerializeError(e.to_string()))
    }

    /// Get number of entries
    #[must_use]
    pub fn entry_count(&self) -> usize {
        self.log.entries.len()
    }

    /// Add an entry
    pub fn add_entry(&mut self, entry: HarEntry) {
        self.log.entries.push(entry);
    }

    /// Find entry by URL
    #[must_use]
    pub fn find_by_url(&self, url: &str) -> Option<&HarEntry> {
        self.log.entries.iter().find(|e| e.request.url == url)
    }

    /// Find entries matching URL pattern (glob-style)
    #[must_use]
    pub fn find_matching(&self, pattern: &str) -> Vec<&HarEntry> {
        self.log
            .entries
            .iter()
            .filter(|e| url_matches_pattern(&e.request.url, pattern))
            .collect()
    }
}

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

/// HAR log structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarLog {
    /// HAR format version (always "1.2")
    pub version: String,
    /// Creator application info
    pub creator: HarCreator,
    /// Browser info (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub browser: Option<HarBrowser>,
    /// List of recorded entries
    pub entries: Vec<HarEntry>,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarLog {
    /// Create a new HAR log
    #[must_use]
    pub fn new() -> Self {
        Self {
            version: "1.2".to_string(),
            creator: HarCreator::probar(),
            browser: None,
            entries: Vec::new(),
            comment: None,
        }
    }
}

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

/// Creator information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarCreator {
    /// Creator name
    pub name: String,
    /// Creator version
    pub version: String,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarCreator {
    /// Create Probar creator info
    #[must_use]
    pub fn probar() -> Self {
        Self {
            name: "Probar".to_string(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            comment: None,
        }
    }
}

/// Browser information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarBrowser {
    /// Browser name
    pub name: String,
    /// Browser version
    pub version: String,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarBrowser {
    /// Create browser info
    #[must_use]
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            comment: None,
        }
    }
}

/// A single HAR entry (request/response pair)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarEntry {
    /// Start time (ISO 8601)
    #[serde(rename = "startedDateTime")]
    pub started_date_time: String,
    /// Total time in milliseconds
    pub time: f64,
    /// Request details
    pub request: HarRequest,
    /// Response details
    pub response: HarResponse,
    /// Cache details
    pub cache: HarCache,
    /// Timing details
    pub timings: HarTimings,
    /// Server IP address (optional)
    #[serde(rename = "serverIPAddress", skip_serializing_if = "Option::is_none")]
    pub server_ip_address: Option<String>,
    /// Connection ID (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connection: Option<String>,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarEntry {
    /// Create a new entry
    #[must_use]
    pub fn new(request: HarRequest, response: HarResponse) -> Self {
        Self {
            started_date_time: chrono_now_iso(),
            time: 0.0,
            request,
            response,
            cache: HarCache::default(),
            timings: HarTimings::default(),
            server_ip_address: None,
            connection: None,
            comment: None,
        }
    }

    /// Set timing in milliseconds
    #[must_use]
    pub fn with_time(mut self, time_ms: f64) -> Self {
        self.time = time_ms;
        self
    }

    /// Set server IP
    #[must_use]
    pub fn with_server_ip(mut self, ip: impl Into<String>) -> Self {
        self.server_ip_address = Some(ip.into());
        self
    }
}

/// HTTP request in HAR format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarRequest {
    /// HTTP method
    pub method: String,
    /// Request URL
    pub url: String,
    /// HTTP version
    #[serde(rename = "httpVersion")]
    pub http_version: String,
    /// Cookies
    pub cookies: Vec<HarCookie>,
    /// Headers
    pub headers: Vec<HarHeader>,
    /// Query string parameters
    #[serde(rename = "queryString")]
    pub query_string: Vec<HarQueryParam>,
    /// POST data (optional)
    #[serde(rename = "postData", skip_serializing_if = "Option::is_none")]
    pub post_data: Option<HarPostData>,
    /// Headers size in bytes (-1 if unknown)
    #[serde(rename = "headersSize")]
    pub headers_size: i64,
    /// Body size in bytes (-1 if unknown)
    #[serde(rename = "bodySize")]
    pub body_size: i64,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarRequest {
    /// Create a GET request
    #[must_use]
    pub fn get(url: impl Into<String>) -> Self {
        Self::new("GET", url)
    }

    /// Create a POST request
    #[must_use]
    pub fn post(url: impl Into<String>) -> Self {
        Self::new("POST", url)
    }

    /// Create a new request
    #[must_use]
    pub fn new(method: impl Into<String>, url: impl Into<String>) -> Self {
        Self {
            method: method.into(),
            url: url.into(),
            http_version: "HTTP/1.1".to_string(),
            cookies: Vec::new(),
            headers: Vec::new(),
            query_string: Vec::new(),
            post_data: None,
            headers_size: -1,
            body_size: -1,
            comment: None,
        }
    }

    /// Add a header
    #[must_use]
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push(HarHeader::new(name, value));
        self
    }

    /// Add POST data
    #[must_use]
    pub fn with_post_data(mut self, data: HarPostData) -> Self {
        self.post_data = Some(data);
        self
    }
}

/// HTTP response in HAR format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarResponse {
    /// HTTP status code
    pub status: u16,
    /// Status text
    #[serde(rename = "statusText")]
    pub status_text: String,
    /// HTTP version
    #[serde(rename = "httpVersion")]
    pub http_version: String,
    /// Cookies
    pub cookies: Vec<HarCookie>,
    /// Headers
    pub headers: Vec<HarHeader>,
    /// Response content
    pub content: HarContent,
    /// Redirect URL (if any)
    #[serde(rename = "redirectURL")]
    pub redirect_url: String,
    /// Headers size in bytes (-1 if unknown)
    #[serde(rename = "headersSize")]
    pub headers_size: i64,
    /// Body size in bytes (-1 if unknown)
    #[serde(rename = "bodySize")]
    pub body_size: i64,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarResponse {
    /// Create a successful response
    #[must_use]
    pub fn ok() -> Self {
        Self::new(200, "OK")
    }

    /// Create a not found response
    #[must_use]
    pub fn not_found() -> Self {
        Self::new(404, "Not Found")
    }

    /// Create a new response
    #[must_use]
    pub fn new(status: u16, status_text: impl Into<String>) -> Self {
        Self {
            status,
            status_text: status_text.into(),
            http_version: "HTTP/1.1".to_string(),
            cookies: Vec::new(),
            headers: Vec::new(),
            content: HarContent::default(),
            redirect_url: String::new(),
            headers_size: -1,
            body_size: -1,
            comment: None,
        }
    }

    /// Add a header
    #[must_use]
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push(HarHeader::new(name, value));
        self
    }

    /// Set response content
    #[must_use]
    pub fn with_content(mut self, content: HarContent) -> Self {
        self.content = content;
        self
    }

    /// Set JSON body
    #[must_use]
    pub fn with_json(mut self, body: impl Into<String>) -> Self {
        self.content = HarContent::json(body);
        self.headers
            .push(HarHeader::new("Content-Type", "application/json"));
        self
    }
}

/// HTTP header
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarHeader {
    /// Header name
    pub name: String,
    /// Header value
    pub value: String,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarHeader {
    /// Create a new header
    #[must_use]
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            comment: None,
        }
    }
}

/// Cookie
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarCookie {
    /// Cookie name
    pub name: String,
    /// Cookie value
    pub value: String,
    /// Path (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    /// Domain (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domain: Option<String>,
    /// Expires (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires: Option<String>,
    /// HTTP only flag
    #[serde(rename = "httpOnly", skip_serializing_if = "Option::is_none")]
    pub http_only: Option<bool>,
    /// Secure flag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub secure: Option<bool>,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarCookie {
    /// Create a new cookie
    #[must_use]
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            path: None,
            domain: None,
            expires: None,
            http_only: None,
            secure: None,
            comment: None,
        }
    }
}

/// Query string parameter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarQueryParam {
    /// Parameter name
    pub name: String,
    /// Parameter value
    pub value: String,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarQueryParam {
    /// Create a new query parameter
    #[must_use]
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            comment: None,
        }
    }
}

/// POST data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarPostData {
    /// MIME type
    #[serde(rename = "mimeType")]
    pub mime_type: String,
    /// Form parameters (for urlencoded)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub params: Vec<HarPostParam>,
    /// Raw text content
    pub text: String,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarPostData {
    /// Create JSON POST data
    #[must_use]
    pub fn json(body: impl Into<String>) -> Self {
        Self {
            mime_type: "application/json".to_string(),
            params: Vec::new(),
            text: body.into(),
            comment: None,
        }
    }

    /// Create form-urlencoded POST data
    #[must_use]
    pub fn form(params: Vec<HarPostParam>) -> Self {
        Self {
            mime_type: "application/x-www-form-urlencoded".to_string(),
            params,
            text: String::new(),
            comment: None,
        }
    }
}

/// POST parameter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarPostParam {
    /// Parameter name
    pub name: String,
    /// Parameter value (optional for file uploads)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    /// File name (for file uploads)
    #[serde(rename = "fileName", skip_serializing_if = "Option::is_none")]
    pub file_name: Option<String>,
    /// Content type (for file uploads)
    #[serde(rename = "contentType", skip_serializing_if = "Option::is_none")]
    pub content_type: Option<String>,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarPostParam {
    /// Create a new POST parameter
    #[must_use]
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: Some(value.into()),
            file_name: None,
            content_type: None,
            comment: None,
        }
    }
}

/// Response content
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HarContent {
    /// Content size in bytes
    pub size: i64,
    /// Compression size (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compression: Option<i64>,
    /// MIME type
    #[serde(rename = "mimeType")]
    pub mime_type: String,
    /// Response text (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    /// Encoding (e.g., "base64")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encoding: Option<String>,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl HarContent {
    /// Create JSON content
    #[must_use]
    pub fn json(body: impl Into<String>) -> Self {
        let text = body.into();
        let size = text.len() as i64;
        Self {
            size,
            compression: None,
            mime_type: "application/json".to_string(),
            text: Some(text),
            encoding: None,
            comment: None,
        }
    }

    /// Create text content
    #[must_use]
    pub fn text(body: impl Into<String>) -> Self {
        let text = body.into();
        let size = text.len() as i64;
        Self {
            size,
            compression: None,
            mime_type: "text/plain".to_string(),
            text: Some(text),
            encoding: None,
            comment: None,
        }
    }

    /// Create HTML content
    #[must_use]
    pub fn html(body: impl Into<String>) -> Self {
        let text = body.into();
        let size = text.len() as i64;
        Self {
            size,
            compression: None,
            mime_type: "text/html".to_string(),
            text: Some(text),
            encoding: None,
            comment: None,
        }
    }
}

/// Cache details
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HarCache {
    /// Before request cache state (optional)
    #[serde(rename = "beforeRequest", skip_serializing_if = "Option::is_none")]
    pub before_request: Option<HarCacheState>,
    /// After request cache state (optional)
    #[serde(rename = "afterRequest", skip_serializing_if = "Option::is_none")]
    pub after_request: Option<HarCacheState>,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

/// Cache state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarCacheState {
    /// Expiry time (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires: Option<String>,
    /// Last access time (optional)
    #[serde(rename = "lastAccess", skip_serializing_if = "Option::is_none")]
    pub last_access: Option<String>,
    /// ETag (optional)
    #[serde(rename = "eTag", skip_serializing_if = "Option::is_none")]
    pub etag: Option<String>,
    /// Hit count (optional)
    #[serde(rename = "hitCount", skip_serializing_if = "Option::is_none")]
    pub hit_count: Option<u32>,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

/// Timing details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HarTimings {
    /// Time spent in blocked queue (-1 if not applicable)
    pub blocked: f64,
    /// DNS resolution time (-1 if not applicable)
    pub dns: f64,
    /// Time to establish connection (-1 if not applicable)
    pub connect: f64,
    /// Time to send request
    pub send: f64,
    /// Time waiting for response
    pub wait: f64,
    /// Time to receive response
    pub receive: f64,
    /// SSL/TLS negotiation time (-1 if not applicable)
    pub ssl: f64,
    /// Optional comment
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl Default for HarTimings {
    fn default() -> Self {
        Self {
            blocked: -1.0,
            dns: -1.0,
            connect: -1.0,
            send: 0.0,
            wait: 0.0,
            receive: 0.0,
            ssl: -1.0,
            comment: None,
        }
    }
}

impl HarTimings {
    /// Create new timings with defaults
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Total time
    #[must_use]
    pub fn total(&self) -> f64 {
        let mut total = 0.0;
        if self.blocked > 0.0 {
            total += self.blocked;
        }
        if self.dns > 0.0 {
            total += self.dns;
        }
        if self.connect > 0.0 {
            total += self.connect;
        }
        total += self.send;
        total += self.wait;
        total += self.receive;
        total
    }
}

// =============================================================================
// HAR Recording and Playback
// =============================================================================

/// HAR recording options
#[derive(Debug, Clone)]
pub struct HarOptions {
    /// Behavior when request not found in HAR
    pub not_found: NotFoundBehavior,
    /// Update HAR with new requests
    pub update: bool,
    /// URL pattern to match (glob-style)
    pub url_pattern: Option<String>,
}

impl Default for HarOptions {
    fn default() -> Self {
        Self {
            not_found: NotFoundBehavior::Fallback,
            update: false,
            url_pattern: None,
        }
    }
}

impl HarOptions {
    /// Create new options with abort on not found
    #[must_use]
    pub fn abort_on_not_found() -> Self {
        Self {
            not_found: NotFoundBehavior::Abort,
            ..Default::default()
        }
    }

    /// Create new options with fallback on not found
    #[must_use]
    pub fn fallback_on_not_found() -> Self {
        Self {
            not_found: NotFoundBehavior::Fallback,
            ..Default::default()
        }
    }

    /// Enable update mode
    #[must_use]
    pub fn with_update(mut self, update: bool) -> Self {
        self.update = update;
        self
    }

    /// Set URL pattern filter
    #[must_use]
    pub fn with_pattern(mut self, pattern: impl Into<String>) -> Self {
        self.url_pattern = Some(pattern.into());
        self
    }
}

/// Behavior when request not found in HAR
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotFoundBehavior {
    /// Abort the request
    Abort,
    /// Fall back to real network
    Fallback,
}

/// HAR recorder for capturing network traffic
#[derive(Debug)]
pub struct HarRecorder {
    /// Recorded HAR data
    har: Har,
    /// Output path
    path: PathBuf,
    /// Whether recording is active
    active: bool,
    /// URL filter pattern
    filter: Option<String>,
}

impl HarRecorder {
    /// Create a new HAR recorder
    #[must_use]
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self {
            har: Har::new(),
            path: path.into(),
            active: false,
            filter: None,
        }
    }

    /// Start recording
    pub fn start(&mut self) {
        self.active = true;
    }

    /// Stop recording
    pub fn stop(&mut self) {
        self.active = false;
    }

    /// Check if recording is active
    #[must_use]
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// Set URL filter pattern
    pub fn set_filter(&mut self, pattern: impl Into<String>) {
        self.filter = Some(pattern.into());
    }

    /// Record a request/response pair
    pub fn record(&mut self, entry: HarEntry) {
        if !self.active {
            return;
        }

        // Apply filter if set
        if let Some(ref pattern) = self.filter {
            if !url_matches_pattern(&entry.request.url, pattern) {
                return;
            }
        }

        self.har.add_entry(entry);
    }

    /// Get recorded HAR
    #[must_use]
    pub fn har(&self) -> &Har {
        &self.har
    }

    /// Get entry count
    #[must_use]
    pub fn entry_count(&self) -> usize {
        self.har.entry_count()
    }

    /// Save HAR to file
    ///
    /// # Errors
    ///
    /// Returns error if file writing fails
    pub fn save(&self) -> Result<(), HarError> {
        let json = self.har.to_json()?;
        std::fs::write(&self.path, json).map_err(|e| HarError::IoError(e.to_string()))
    }
}

/// HAR player for replaying recorded traffic
#[derive(Debug)]
pub struct HarPlayer {
    /// HAR data to replay
    har: Har,
    /// Options for playback
    options: HarOptions,
}

impl HarPlayer {
    /// Create a new HAR player
    #[must_use]
    pub fn new(har: Har, options: HarOptions) -> Self {
        Self { har, options }
    }

    /// Load HAR from file
    ///
    /// # Errors
    ///
    /// Returns error if file reading or parsing fails
    pub fn from_file(path: impl Into<PathBuf>, options: HarOptions) -> Result<Self, HarError> {
        let path = path.into();
        let content =
            std::fs::read_to_string(&path).map_err(|e| HarError::IoError(e.to_string()))?;
        let har = Har::from_json(&content)?;
        Ok(Self::new(har, options))
    }

    /// Find matching response for a request
    #[must_use]
    pub fn find_response(&self, method: &str, url: &str) -> Option<&HarResponse> {
        // Check URL pattern filter
        if let Some(ref pattern) = self.options.url_pattern {
            if !url_matches_pattern(url, pattern) {
                return None;
            }
        }

        // Find matching entry
        self.har.log.entries.iter().find_map(|entry| {
            if entry.request.method == method && entry.request.url == url {
                Some(&entry.response)
            } else {
                None
            }
        })
    }

    /// Get behavior for not found requests
    #[must_use]
    pub fn not_found_behavior(&self) -> NotFoundBehavior {
        self.options.not_found
    }

    /// Get entry count
    #[must_use]
    pub fn entry_count(&self) -> usize {
        self.har.entry_count()
    }
}

// =============================================================================
// Errors
// =============================================================================

/// HAR-related errors
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HarError {
    /// JSON parsing error
    ParseError(String),
    /// JSON serialization error
    SerializeError(String),
    /// I/O error
    IoError(String),
    /// Request not found in HAR
    NotFound(String),
}

impl std::fmt::Display for HarError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ParseError(msg) => write!(f, "HAR parse error: {msg}"),
            Self::SerializeError(msg) => write!(f, "HAR serialize error: {msg}"),
            Self::IoError(msg) => write!(f, "HAR I/O error: {msg}"),
            Self::NotFound(url) => write!(f, "Request not found in HAR: {url}"),
        }
    }
}

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

// =============================================================================
// Helpers
// =============================================================================

/// Generate current ISO 8601 timestamp
fn chrono_now_iso() -> String {
    // Simple implementation without chrono dependency
    "2024-01-01T00:00:00.000Z".to_string()
}

/// Check if URL matches pattern (simple contains match)
fn url_matches_pattern(url: &str, pattern: &str) -> bool {
    // Simple contains matching for now
    // Strip glob wildcards for basic matching
    let clean_pattern = pattern
        .replace("**", "")
        .replace('*', "")
        .trim_matches('/')
        .to_string();

    if clean_pattern.is_empty() {
        return true; // Empty pattern matches everything
    }

    url.contains(&clean_pattern)
}

// =============================================================================
// Tests
// =============================================================================

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

    // =========================================================================
    // Hâ‚€-HAR-01 to Hâ‚€-HAR-10: HAR Structure Tests
    // =========================================================================

    #[test]
    fn h0_har_01_new_creates_empty_har() {
        let har = Har::new();
        assert_eq!(har.log.version, "1.2");
        assert_eq!(har.entry_count(), 0);
    }

    #[test]
    fn h0_har_02_log_has_probar_creator() {
        let har = Har::new();
        assert_eq!(har.log.creator.name, "Probar");
    }

    #[test]
    fn h0_har_03_add_entry() {
        let mut har = Har::new();
        let entry = HarEntry::new(HarRequest::get("http://example.com"), HarResponse::ok());
        har.add_entry(entry);
        assert_eq!(har.entry_count(), 1);
    }

    #[test]
    fn h0_har_04_find_by_url() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://example.com/api"),
            HarResponse::ok(),
        ));
        assert!(har.find_by_url("http://example.com/api").is_some());
        assert!(har.find_by_url("http://other.com").is_none());
    }

    #[test]
    fn h0_har_05_serialization_roundtrip() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com"),
            HarResponse::ok(),
        ));
        let json = har.to_json().unwrap();
        let parsed = Har::from_json(&json).unwrap();
        assert_eq!(parsed.entry_count(), 1);
    }

    #[test]
    fn h0_har_06_request_get() {
        let req = HarRequest::get("http://test.com");
        assert_eq!(req.method, "GET");
        assert_eq!(req.url, "http://test.com");
    }

    #[test]
    fn h0_har_07_request_post() {
        let req = HarRequest::post("http://test.com");
        assert_eq!(req.method, "POST");
    }

    #[test]
    fn h0_har_08_request_with_header() {
        let req = HarRequest::get("http://test.com").with_header("Accept", "application/json");
        assert_eq!(req.headers.len(), 1);
        assert_eq!(req.headers[0].name, "Accept");
    }

    #[test]
    fn h0_har_09_response_ok() {
        let resp = HarResponse::ok();
        assert_eq!(resp.status, 200);
        assert_eq!(resp.status_text, "OK");
    }

    #[test]
    fn h0_har_10_response_not_found() {
        let resp = HarResponse::not_found();
        assert_eq!(resp.status, 404);
    }

    // =========================================================================
    // Hâ‚€-HAR-11 to Hâ‚€-HAR-20: Content and Data Tests
    // =========================================================================

    #[test]
    fn h0_har_11_response_with_json() {
        let resp = HarResponse::ok().with_json(r#"{"key": "value"}"#);
        assert_eq!(resp.content.mime_type, "application/json");
        assert!(resp.content.text.is_some());
    }

    #[test]
    fn h0_har_12_content_json() {
        let content = HarContent::json(r#"{"test": true}"#);
        assert_eq!(content.mime_type, "application/json");
        assert!(content.size > 0);
    }

    #[test]
    fn h0_har_13_content_text() {
        let content = HarContent::text("Hello, World!");
        assert_eq!(content.mime_type, "text/plain");
    }

    #[test]
    fn h0_har_14_content_html() {
        let content = HarContent::html("<html></html>");
        assert_eq!(content.mime_type, "text/html");
    }

    #[test]
    fn h0_har_15_post_data_json() {
        let data = HarPostData::json(r#"{"name": "test"}"#);
        assert_eq!(data.mime_type, "application/json");
    }

    #[test]
    fn h0_har_16_post_data_form() {
        let data = HarPostData::form(vec![HarPostParam::new("field", "value")]);
        assert_eq!(data.mime_type, "application/x-www-form-urlencoded");
        assert_eq!(data.params.len(), 1);
    }

    #[test]
    fn h0_har_17_cookie_creation() {
        let cookie = HarCookie::new("session", "abc123");
        assert_eq!(cookie.name, "session");
        assert_eq!(cookie.value, "abc123");
    }

    #[test]
    fn h0_har_18_header_creation() {
        let header = HarHeader::new("X-Custom", "value");
        assert_eq!(header.name, "X-Custom");
        assert_eq!(header.value, "value");
    }

    #[test]
    fn h0_har_19_query_param() {
        let param = HarQueryParam::new("page", "1");
        assert_eq!(param.name, "page");
        assert_eq!(param.value, "1");
    }

    #[test]
    fn h0_har_20_entry_with_time() {
        let entry =
            HarEntry::new(HarRequest::get("http://test.com"), HarResponse::ok()).with_time(150.0);
        assert!((entry.time - 150.0).abs() < f64::EPSILON);
    }

    // =========================================================================
    // Hâ‚€-HAR-21 to Hâ‚€-HAR-30: Recording Tests
    // =========================================================================

    #[test]
    fn h0_har_21_recorder_new() {
        let recorder = HarRecorder::new("test.har");
        assert!(!recorder.is_active());
        assert_eq!(recorder.entry_count(), 0);
    }

    #[test]
    fn h0_har_22_recorder_start_stop() {
        let mut recorder = HarRecorder::new("test.har");
        recorder.start();
        assert!(recorder.is_active());
        recorder.stop();
        assert!(!recorder.is_active());
    }

    #[test]
    fn h0_har_23_recorder_record_when_active() {
        let mut recorder = HarRecorder::new("test.har");
        recorder.start();
        recorder.record(HarEntry::new(
            HarRequest::get("http://test.com"),
            HarResponse::ok(),
        ));
        assert_eq!(recorder.entry_count(), 1);
    }

    #[test]
    fn h0_har_24_recorder_skip_when_inactive() {
        let mut recorder = HarRecorder::new("test.har");
        recorder.record(HarEntry::new(
            HarRequest::get("http://test.com"),
            HarResponse::ok(),
        ));
        assert_eq!(recorder.entry_count(), 0);
    }

    #[test]
    fn h0_har_25_recorder_filter() {
        let mut recorder = HarRecorder::new("test.har");
        recorder.start();
        recorder.set_filter("/api/");
        recorder.record(HarEntry::new(
            HarRequest::get("http://test.com/api/users"),
            HarResponse::ok(),
        ));
        recorder.record(HarEntry::new(
            HarRequest::get("http://test.com/static/image.png"),
            HarResponse::ok(),
        ));
        // Only API request recorded (filter uses contains match)
        assert_eq!(recorder.entry_count(), 1);
    }

    #[test]
    fn h0_har_26_options_default() {
        let options = HarOptions::default();
        assert_eq!(options.not_found, NotFoundBehavior::Fallback);
        assert!(!options.update);
    }

    #[test]
    fn h0_har_27_options_abort_on_not_found() {
        let options = HarOptions::abort_on_not_found();
        assert_eq!(options.not_found, NotFoundBehavior::Abort);
    }

    #[test]
    fn h0_har_28_options_with_update() {
        let options = HarOptions::default().with_update(true);
        assert!(options.update);
    }

    #[test]
    fn h0_har_29_options_with_pattern() {
        let options = HarOptions::default().with_pattern("**/api/**");
        assert!(options.url_pattern.is_some());
    }

    #[test]
    fn h0_har_30_recorder_har_access() {
        let recorder = HarRecorder::new("test.har");
        let har = recorder.har();
        assert_eq!(har.log.version, "1.2");
    }

    // =========================================================================
    // Hâ‚€-HAR-31 to Hâ‚€-HAR-40: Playback Tests
    // =========================================================================

    #[test]
    fn h0_har_31_player_new() {
        let har = Har::new();
        let player = HarPlayer::new(har, HarOptions::default());
        assert_eq!(player.entry_count(), 0);
    }

    #[test]
    fn h0_har_32_player_find_response() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/api"),
            HarResponse::ok().with_json(r#"{"found": true}"#),
        ));
        let player = HarPlayer::new(har, HarOptions::default());
        let resp = player.find_response("GET", "http://test.com/api");
        assert!(resp.is_some());
        assert_eq!(resp.unwrap().status, 200);
    }

    #[test]
    fn h0_har_33_player_not_found() {
        let har = Har::new();
        let player = HarPlayer::new(har, HarOptions::default());
        let resp = player.find_response("GET", "http://missing.com");
        assert!(resp.is_none());
    }

    #[test]
    fn h0_har_34_player_not_found_behavior() {
        let player = HarPlayer::new(Har::new(), HarOptions::abort_on_not_found());
        assert_eq!(player.not_found_behavior(), NotFoundBehavior::Abort);
    }

    #[test]
    fn h0_har_35_timings_default() {
        let timings = HarTimings::default();
        assert!(timings.blocked < 0.0);
        assert!(timings.dns < 0.0);
    }

    #[test]
    fn h0_har_36_timings_total() {
        let mut timings = HarTimings::new();
        timings.send = 10.0;
        timings.wait = 50.0;
        timings.receive = 20.0;
        assert!((timings.total() - 80.0).abs() < f64::EPSILON);
    }

    #[test]
    fn h0_har_37_browser_info() {
        let browser = HarBrowser::new("Chromium", "120.0");
        assert_eq!(browser.name, "Chromium");
        assert_eq!(browser.version, "120.0");
    }

    #[test]
    fn h0_har_38_entry_with_server_ip() {
        let entry = HarEntry::new(HarRequest::get("http://test.com"), HarResponse::ok())
            .with_server_ip("192.168.1.1");
        assert_eq!(entry.server_ip_address, Some("192.168.1.1".to_string()));
    }

    #[test]
    fn h0_har_39_error_display() {
        let err = HarError::ParseError("invalid json".to_string());
        assert!(format!("{err}").contains("parse error"));
    }

    #[test]
    fn h0_har_40_error_not_found() {
        let err = HarError::NotFound("http://missing.com".to_string());
        assert!(format!("{err}").contains("not found"));
    }

    // =========================================================================
    // Hâ‚€-HAR-41 to Hâ‚€-HAR-50: Advanced Tests
    // =========================================================================

    #[test]
    fn h0_har_41_find_matching_empty() {
        let har = Har::new();
        let matches = har.find_matching("**/api/**");
        assert!(matches.is_empty());
    }

    #[test]
    fn h0_har_42_cache_default() {
        let cache = HarCache::default();
        assert!(cache.before_request.is_none());
        assert!(cache.after_request.is_none());
    }

    #[test]
    fn h0_har_43_response_with_header() {
        let resp = HarResponse::ok().with_header("X-Request-Id", "123");
        assert_eq!(resp.headers.len(), 1);
    }

    #[test]
    fn h0_har_44_request_with_post_data() {
        let req = HarRequest::post("http://test.com").with_post_data(HarPostData::json(r#"{}"#));
        assert!(req.post_data.is_some());
    }

    #[test]
    fn h0_har_45_response_with_content() {
        let resp = HarResponse::ok().with_content(HarContent::text("body"));
        assert_eq!(resp.content.text, Some("body".to_string()));
    }

    #[test]
    fn h0_har_46_parse_error() {
        let result = Har::from_json("invalid json");
        assert!(result.is_err());
    }

    #[test]
    fn h0_har_47_har_default() {
        let har = Har::default();
        assert_eq!(har.log.version, "1.2");
    }

    #[test]
    fn h0_har_48_log_default() {
        let log = HarLog::default();
        assert!(log.entries.is_empty());
    }

    #[test]
    fn h0_har_49_timings_new() {
        let timings = HarTimings::new();
        assert!(timings.ssl < 0.0);
    }

    #[test]
    fn h0_har_50_content_default() {
        let content = HarContent::default();
        assert!(content.text.is_none());
        assert_eq!(content.size, 0);
    }

    // =========================================================================
    // Hâ‚€-HAR-51 to Hâ‚€-HAR-70: Additional Coverage Tests
    // =========================================================================

    #[test]
    fn h0_har_51_error_serialize_display() {
        let err = HarError::SerializeError("failed to serialize".to_string());
        let msg = format!("{err}");
        assert!(msg.contains("serialize error"));
        assert!(msg.contains("failed to serialize"));
    }

    #[test]
    fn h0_har_52_error_io_display() {
        let err = HarError::IoError("file not found".to_string());
        let msg = format!("{err}");
        assert!(msg.contains("I/O error"));
        assert!(msg.contains("file not found"));
    }

    #[test]
    fn h0_har_53_options_fallback_on_not_found() {
        let options = HarOptions::fallback_on_not_found();
        assert_eq!(options.not_found, NotFoundBehavior::Fallback);
        assert!(!options.update);
        assert!(options.url_pattern.is_none());
    }

    #[test]
    fn h0_har_54_player_find_response_with_pattern_no_match() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/users"),
            HarResponse::ok(),
        ));
        let options = HarOptions::default().with_pattern("/api/");
        let player = HarPlayer::new(har, options);
        // URL doesn't match pattern, should return None
        let resp = player.find_response("GET", "http://test.com/users");
        assert!(resp.is_none());
    }

    #[test]
    fn h0_har_55_player_find_response_with_pattern_match() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/api/users"),
            HarResponse::ok().with_json(r#"{"users": []}"#),
        ));
        let options = HarOptions::default().with_pattern("/api/");
        let player = HarPlayer::new(har, options);
        let resp = player.find_response("GET", "http://test.com/api/users");
        assert!(resp.is_some());
        assert_eq!(resp.unwrap().status, 200);
    }

    #[test]
    fn h0_har_56_player_find_response_method_mismatch() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/api"),
            HarResponse::ok(),
        ));
        let player = HarPlayer::new(har, HarOptions::default());
        // Wrong method
        let resp = player.find_response("POST", "http://test.com/api");
        assert!(resp.is_none());
    }

    #[test]
    fn h0_har_57_timings_total_with_positive_values() {
        let mut timings = HarTimings::new();
        timings.blocked = 5.0;
        timings.dns = 10.0;
        timings.connect = 15.0;
        timings.send = 2.0;
        timings.wait = 100.0;
        timings.receive = 50.0;
        // Total should be 5 + 10 + 15 + 2 + 100 + 50 = 182
        assert!((timings.total() - 182.0).abs() < f64::EPSILON);
    }

    #[test]
    fn h0_har_58_timings_total_with_mixed_values() {
        let mut timings = HarTimings::new();
        // blocked and dns remain -1 (not applicable)
        timings.connect = 20.0;
        timings.send = 5.0;
        timings.wait = 30.0;
        timings.receive = 10.0;
        // Total = 20 + 5 + 30 + 10 = 65 (blocked/dns excluded)
        assert!((timings.total() - 65.0).abs() < f64::EPSILON);
    }

    #[test]
    fn h0_har_59_find_matching_with_matches() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/api/users"),
            HarResponse::ok(),
        ));
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/api/posts"),
            HarResponse::ok(),
        ));
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/static/image.png"),
            HarResponse::ok(),
        ));
        let matches = har.find_matching("/api/");
        assert_eq!(matches.len(), 2);
    }

    #[test]
    fn h0_har_60_url_matches_pattern_glob_wildcards() {
        // Pattern with ** glob
        assert!(url_matches_pattern(
            "http://test.com/api/users",
            "**/api/**"
        ));
        // Pattern with * glob
        assert!(url_matches_pattern("http://test.com/api/test", "*/api/*"));
        // Exact substring
        assert!(url_matches_pattern("http://example.com/path", "example"));
        // No match
        assert!(!url_matches_pattern("http://other.com", "example"));
    }

    #[test]
    fn h0_har_61_url_matches_pattern_empty() {
        // Empty pattern after stripping globs matches everything
        assert!(url_matches_pattern("http://any.com/path", "**"));
        assert!(url_matches_pattern("http://any.com/path", "*"));
        assert!(url_matches_pattern("http://any.com/path", ""));
    }

    #[test]
    fn h0_har_62_recorder_save_error() {
        let recorder = HarRecorder::new("/nonexistent/path/that/cannot/be/written/test.har");
        let result = recorder.save();
        assert!(result.is_err());
        if let Err(HarError::IoError(msg)) = result {
            assert!(!msg.is_empty());
        } else {
            panic!("Expected IoError");
        }
    }

    #[test]
    fn h0_har_63_player_from_file_not_found() {
        let result = HarPlayer::from_file("/nonexistent/file.har", HarOptions::default());
        assert!(result.is_err());
        if let Err(HarError::IoError(msg)) = result {
            assert!(!msg.is_empty());
        } else {
            panic!("Expected IoError");
        }
    }

    #[test]
    fn h0_har_64_player_from_file_invalid_json() {
        // Create a temp file with invalid JSON
        let temp_path = std::env::temp_dir().join("test_invalid_har.json");
        std::fs::write(&temp_path, "not valid json").unwrap();
        let result = HarPlayer::from_file(&temp_path, HarOptions::default());
        std::fs::remove_file(&temp_path).ok();
        assert!(result.is_err());
        if let Err(HarError::ParseError(msg)) = result {
            assert!(!msg.is_empty());
        } else {
            panic!("Expected ParseError");
        }
    }

    #[test]
    fn h0_har_65_recorder_save_and_load_roundtrip() {
        let temp_path = std::env::temp_dir().join("test_har_roundtrip.har");
        let mut recorder = HarRecorder::new(&temp_path);
        recorder.start();
        recorder.record(HarEntry::new(
            HarRequest::get("http://test.com/api").with_header("Accept", "application/json"),
            HarResponse::ok().with_json(r#"{"status": "ok"}"#),
        ));
        recorder.stop();
        recorder.save().expect("Save should succeed");

        let player = HarPlayer::from_file(&temp_path, HarOptions::default()).unwrap();
        assert_eq!(player.entry_count(), 1);
        let resp = player.find_response("GET", "http://test.com/api");
        assert!(resp.is_some());
        assert_eq!(resp.unwrap().status, 200);

        std::fs::remove_file(&temp_path).ok();
    }

    #[test]
    fn h0_har_66_har_error_implements_error_trait() {
        let err: Box<dyn std::error::Error> = Box::new(HarError::NotFound("test".to_string()));
        // Just verify it compiles and can be used as Box<dyn Error>
        assert!(!err.to_string().is_empty());
    }

    #[test]
    fn h0_har_67_request_new_custom_method() {
        let req = HarRequest::new("DELETE", "http://test.com/resource/123");
        assert_eq!(req.method, "DELETE");
        assert_eq!(req.url, "http://test.com/resource/123");
        assert_eq!(req.http_version, "HTTP/1.1");
        assert!(req.cookies.is_empty());
        assert!(req.headers.is_empty());
        assert!(req.query_string.is_empty());
        assert!(req.post_data.is_none());
        assert_eq!(req.headers_size, -1);
        assert_eq!(req.body_size, -1);
    }

    #[test]
    fn h0_har_68_response_new_custom_status() {
        let resp = HarResponse::new(201, "Created");
        assert_eq!(resp.status, 201);
        assert_eq!(resp.status_text, "Created");
        assert_eq!(resp.http_version, "HTTP/1.1");
        assert!(resp.cookies.is_empty());
        assert!(resp.headers.is_empty());
        assert!(resp.redirect_url.is_empty());
        assert_eq!(resp.headers_size, -1);
        assert_eq!(resp.body_size, -1);
    }

    #[test]
    fn h0_har_69_chrono_now_iso_format() {
        // Verify the timestamp format is valid ISO 8601
        let timestamp = chrono_now_iso();
        assert!(timestamp.ends_with('Z'));
        assert!(timestamp.contains('T'));
        assert!(timestamp.contains('-'));
    }

    #[test]
    fn h0_har_70_find_by_url_multiple_entries() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/first"),
            HarResponse::new(200, "First"),
        ));
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/second"),
            HarResponse::new(201, "Second"),
        ));
        har.add_entry(HarEntry::new(
            HarRequest::get("http://test.com/first"),
            HarResponse::new(202, "First Again"),
        ));
        // find_by_url returns the first match
        let entry = har.find_by_url("http://test.com/first");
        assert!(entry.is_some());
        assert_eq!(entry.unwrap().response.status, 200);
    }

    #[test]
    fn h0_har_71_har_log_browser_and_comment() {
        let mut log = HarLog::new();
        log.browser = Some(HarBrowser::new("Firefox", "115.0"));
        log.comment = Some("Test HAR log".to_string());
        assert!(log.browser.is_some());
        assert_eq!(log.browser.as_ref().unwrap().name, "Firefox");
        assert!(log.comment.is_some());
    }

    #[test]
    fn h0_har_72_cookie_optional_fields() {
        let mut cookie = HarCookie::new("session", "abc123");
        cookie.path = Some("/".to_string());
        cookie.domain = Some("example.com".to_string());
        cookie.expires = Some("2025-01-01T00:00:00Z".to_string());
        cookie.http_only = Some(true);
        cookie.secure = Some(true);
        cookie.comment = Some("Session cookie".to_string());

        assert_eq!(cookie.path, Some("/".to_string()));
        assert_eq!(cookie.domain, Some("example.com".to_string()));
        assert_eq!(cookie.http_only, Some(true));
        assert_eq!(cookie.secure, Some(true));
    }

    #[test]
    fn h0_har_73_post_param_file_upload() {
        let mut param = HarPostParam::new("file", "");
        param.value = None;
        param.file_name = Some("document.pdf".to_string());
        param.content_type = Some("application/pdf".to_string());
        param.comment = Some("Uploaded file".to_string());

        assert!(param.value.is_none());
        assert_eq!(param.file_name, Some("document.pdf".to_string()));
        assert_eq!(param.content_type, Some("application/pdf".to_string()));
    }

    #[test]
    fn h0_har_74_content_with_encoding() {
        let mut content = HarContent::json(r#"{"data": "test"}"#);
        content.encoding = Some("base64".to_string());
        content.compression = Some(100);
        content.comment = Some("Compressed content".to_string());

        assert_eq!(content.encoding, Some("base64".to_string()));
        assert_eq!(content.compression, Some(100));
    }

    #[test]
    fn h0_har_75_cache_state_fields() {
        let state = HarCacheState {
            expires: Some("2025-12-31T23:59:59Z".to_string()),
            last_access: Some("2024-01-01T00:00:00Z".to_string()),
            etag: Some("abc123".to_string()),
            hit_count: Some(42),
            comment: Some("Cache hit".to_string()),
        };

        assert_eq!(state.hit_count, Some(42));
        assert!(state.etag.is_some());
    }

    #[test]
    fn h0_har_76_cache_with_states() {
        let mut cache = HarCache::default();
        cache.before_request = Some(HarCacheState {
            expires: None,
            last_access: None,
            etag: Some("before".to_string()),
            hit_count: Some(1),
            comment: None,
        });
        cache.after_request = Some(HarCacheState {
            expires: None,
            last_access: None,
            etag: Some("after".to_string()),
            hit_count: Some(2),
            comment: None,
        });
        cache.comment = Some("Cache test".to_string());

        assert!(cache.before_request.is_some());
        assert!(cache.after_request.is_some());
        assert_eq!(
            cache.before_request.as_ref().unwrap().etag,
            Some("before".to_string())
        );
    }

    #[test]
    fn h0_har_77_timings_with_comment() {
        let mut timings = HarTimings::new();
        timings.comment = Some("Timing comment".to_string());
        assert!(timings.comment.is_some());
    }

    #[test]
    fn h0_har_78_entry_optional_fields() {
        let mut entry = HarEntry::new(HarRequest::get("http://test.com"), HarResponse::ok());
        entry.connection = Some("1234".to_string());
        entry.comment = Some("Entry comment".to_string());

        assert_eq!(entry.connection, Some("1234".to_string()));
        assert!(entry.comment.is_some());
    }

    #[test]
    fn h0_har_79_browser_with_comment() {
        let mut browser = HarBrowser::new("Chrome", "120.0");
        browser.comment = Some("Browser comment".to_string());
        assert!(browser.comment.is_some());
    }

    #[test]
    fn h0_har_80_creator_has_version() {
        let creator = HarCreator::probar();
        assert_eq!(creator.name, "Probar");
        // Version should be cargo package version
        assert!(!creator.version.is_empty());
        assert!(creator.comment.is_none());
    }

    #[test]
    fn h0_har_81_header_with_comment() {
        let mut header = HarHeader::new("Content-Type", "application/json");
        header.comment = Some("Header comment".to_string());
        assert!(header.comment.is_some());
    }

    #[test]
    fn h0_har_82_query_param_with_comment() {
        let mut param = HarQueryParam::new("page", "1");
        param.comment = Some("Pagination".to_string());
        assert!(param.comment.is_some());
    }

    #[test]
    fn h0_har_83_post_data_with_comment() {
        let mut data = HarPostData::json(r#"{"test": true}"#);
        data.comment = Some("POST body".to_string());
        assert!(data.comment.is_some());
    }

    #[test]
    fn h0_har_84_request_with_comment() {
        let mut req = HarRequest::get("http://test.com");
        req.comment = Some("Test request".to_string());
        assert!(req.comment.is_some());
    }

    #[test]
    fn h0_har_85_response_with_comment() {
        let mut resp = HarResponse::ok();
        resp.comment = Some("Test response".to_string());
        assert!(resp.comment.is_some());
    }

    #[test]
    fn h0_har_86_serialization_with_all_optional_fields() {
        let mut har = Har::new();
        har.log.browser = Some(HarBrowser::new("Chrome", "120.0"));
        har.log.comment = Some("Test log".to_string());

        let mut entry = HarEntry::new(HarRequest::get("http://test.com"), HarResponse::ok())
            .with_server_ip("127.0.0.1")
            .with_time(100.0);
        entry.connection = Some("conn-1".to_string());
        entry.comment = Some("Entry".to_string());

        har.add_entry(entry);

        // Serialize and deserialize
        let json = har.to_json().unwrap();
        let parsed = Har::from_json(&json).unwrap();

        assert!(parsed.log.browser.is_some());
        assert!(parsed.log.comment.is_some());
        assert_eq!(parsed.entry_count(), 1);
    }

    #[test]
    fn h0_har_87_timings_zero_values() {
        let mut timings = HarTimings::new();
        timings.blocked = 0.0; // Zero, not negative
        timings.dns = 0.0;
        timings.connect = 0.0;
        timings.send = 0.0;
        timings.wait = 0.0;
        timings.receive = 0.0;
        // Zero values for blocked/dns/connect should NOT be added (only > 0)
        assert!((timings.total() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn h0_har_88_url_pattern_with_slashes() {
        // Pattern with leading/trailing slashes should be trimmed
        assert!(url_matches_pattern(
            "http://test.com/api/v1/users",
            "/api/v1/"
        ));
        assert!(url_matches_pattern(
            "http://test.com/api/v1/users",
            "api/v1"
        ));
    }

    #[test]
    fn h0_har_89_find_matching_partial_pattern() {
        let mut har = Har::new();
        har.add_entry(HarEntry::new(
            HarRequest::get("http://example.com/users/123"),
            HarResponse::ok(),
        ));
        har.add_entry(HarEntry::new(
            HarRequest::get("http://other.com/posts"),
            HarResponse::ok(),
        ));
        // Pattern "users" should match first entry
        let matches = har.find_matching("users");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].request.url, "http://example.com/users/123");
    }

    #[test]
    fn h0_har_90_not_found_behavior_equality() {
        assert_eq!(NotFoundBehavior::Abort, NotFoundBehavior::Abort);
        assert_eq!(NotFoundBehavior::Fallback, NotFoundBehavior::Fallback);
        assert_ne!(NotFoundBehavior::Abort, NotFoundBehavior::Fallback);
    }
}