rustling 0.8.0

A blazingly fast library for computational linguistics
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
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
"""Tests for rustling.chat.CHAT."""

import datetime
import warnings

import pytest

from rustling.chat import (
    CHAT,
    Age,
    ChangeableHeader,
    Gra,
    Headers,
    Participant,
    Token,
    Utterance,
)

BASIC_CHAT = (
    "@UTF8\n"
    "@Begin\n"
    "@Participants:\tCHI Child, MOT Mother\n"
    "*CHI:\tI want cookie .\n"
    "%mor:\tpro|I v|want n|cookie .\n"
    "%gra:\t1|2|SUBJ 2|0|ROOT 3|2|OBJ 4|2|PUNCT\n"
    "*MOT:\tno .\n"
    "%mor:\tco|no .\n"
    "%gra:\t1|0|ROOT 2|1|PUNCT\n"
    "@End\n"
)

TWO_FILES = [
    "@UTF8\n@Begin\n*CHI:\thi .\n@End\n",
    "@UTF8\n@Begin\n*MOT:\tbye .\n@End\n",
]


class TestCHATFromStrs:
    def test_basic_parsing(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert reader.n_files == 1

    def test_empty_strs(self):
        reader = CHAT.from_strs([])
        assert reader.n_files == 0

    def test_ids_provided(self):
        reader = CHAT.from_strs([BASIC_CHAT], ids=["my_file"])
        assert reader.file_paths == ["my_file"]

    def test_ids_auto_generated(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        paths = reader.file_paths
        assert len(paths) == 1
        # UUID format: 8-4-4-4-12 hex chars
        assert len(paths[0]) == 36

    def test_ids_length_mismatch(self):
        with pytest.raises(ValueError):
            CHAT.from_strs(["content1", "content2"], ids=["only_one"])

    def test_multiple_files(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        assert reader.n_files == 2
        assert reader.file_paths == ["a", "b"]


class TestFromUtterances:
    def test_round_trip(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        new_reader = CHAT.from_utterances(utts)
        assert new_reader.n_files == 1
        new_utts = new_reader.utterances()
        assert len(new_utts) == len(utts)
        for orig, rebuilt in zip(utts, new_utts):
            assert orig == rebuilt

    def test_subset(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        new_reader = CHAT.from_utterances(utts[:1])
        new_utts = new_reader.utterances()
        assert len(new_utts) == 1
        assert new_utts[0].participant == "CHI"

    def test_empty(self):
        new_reader = CHAT.from_utterances([])
        assert new_reader.n_files == 1
        assert len(new_reader.utterances()) == 0

    def test_words(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        new_reader = CHAT.from_utterances(utts)
        assert new_reader.words() == reader.words()

    def test_to_strs(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        new_reader = CHAT.from_utterances(utts)
        strs = new_reader.to_strs()
        assert len(strs) == 1
        assert "*CHI:" in strs[0]
        assert "%mor:" in strs[0]
        assert "@End" in strs[0]

    def test_serialization_round_trip(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        new_reader = CHAT.from_utterances(utts)
        strs = new_reader.to_strs()
        reparsed = CHAT.from_strs(strs)
        reparsed_utts = reparsed.utterances()
        assert len(reparsed_utts) == len(utts)
        for orig, reparsed_utt in zip(utts, reparsed_utts):
            assert orig.participant == reparsed_utt.participant
            assert orig.tokens == reparsed_utt.tokens


class TestUtterances:
    def test_utterances_flat(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        assert len(utts) == 2
        assert utts[0].participant == "CHI"
        assert utts[1].participant == "MOT"

    def test_utterances_by_file(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances(by_file=True)
        assert len(utts) == 1  # one file
        assert len(utts[0]) == 2  # two utterances

    def test_utterances_multiple_files_by_file(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        utts = reader.utterances(by_file=True)
        assert len(utts) == 2
        assert len(utts[0]) == 1
        assert len(utts[1]) == 1

    def test_utterance_type(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        assert isinstance(utts[0], Utterance)

    def test_utterance_tokens(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        tokens = utts[0].tokens
        assert tokens is not None
        assert len(tokens) == 4  # I, want, cookie, .
        assert tokens[0].word == "I"
        assert tokens[1].word == "want"
        assert tokens[2].word == "cookie"

    def test_utterance_tiers(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        tiers = utts[0].tiers
        assert tiers is not None
        assert "CHI" in tiers
        assert "%mor" in tiers
        assert "%gra" in tiers

    def test_utterance_audible(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        assert utts[0].audible == "I want cookie ."

    def test_utterance_audible_constructed(self):
        utt = Utterance(tokens=[Token("hello"), Token("world")])
        assert utt.audible == "hello world"

    def test_utterance_audible_none_tokens(self):
        utt = Utterance()
        assert utt.audible is None


class TestTokens:
    def test_token_pos(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        tokens = utts[0].tokens
        assert tokens is not None
        assert tokens[0].pos == "pro"
        assert tokens[1].pos == "v"
        assert tokens[2].pos == "n"

    def test_token_mor(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        tokens = utts[0].tokens
        assert tokens is not None
        assert tokens[0].mor == "I"
        assert tokens[1].mor == "want"
        assert tokens[2].mor == "cookie"

    def test_token_gra(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        tokens = utts[0].tokens
        assert tokens is not None
        gra = tokens[0].gra
        assert isinstance(gra, Gra)
        assert gra.dep == 1
        assert gra.head == 2
        assert gra.rel == "SUBJ"

    def test_no_mor_tier(self):
        chat_str = "@UTF8\n@Begin\n*CHI:\thello world .\n@End\n"
        reader = CHAT.from_strs([chat_str])
        utts = reader.utterances()
        tokens = utts[0].tokens
        assert tokens is not None
        assert tokens[0].pos is None
        assert tokens[0].mor is None
        assert tokens[0].gra is None


class TestWords:
    def test_words_flat(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        words = reader.words()
        assert "I" in words
        assert "want" in words
        assert "cookie" in words
        assert "no" in words

    def test_words_by_utterance(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        words = reader.words(by_utterance=True)
        assert isinstance(words, list)
        assert isinstance(words[0], list)
        assert words[0] == ["I", "want", "cookie", "."]

    def test_words_by_file(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        words = reader.words(by_file=True)
        assert len(words) == 2  # two files

    def test_words_by_utterance_and_files(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        words = reader.words(by_utterance=True, by_file=True)
        assert len(words) == 2  # two files
        assert isinstance(words[0], list)
        assert isinstance(words[0][0], list)


class TestChatTokens:
    def test_tokens_flat(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        tokens = reader.tokens()
        assert isinstance(tokens[0], Token)
        assert tokens[0].word == "I"
        assert tokens[0].pos == "pro"

    def test_tokens_by_utterance(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        tokens = reader.tokens(by_utterance=True)
        assert isinstance(tokens[0], list)
        assert isinstance(tokens[0][0], Token)


class TestTimeMarks:
    def test_time_marks_present(self):
        chat_str = "@UTF8\n@Begin\n*CHI:\thello . \x15123_456\x15\n@End\n"
        reader = CHAT.from_strs([chat_str])
        utts = reader.utterances()
        assert utts[0].time_marks == (123, 456)

    def test_time_marks_absent(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        assert utts[0].time_marks is None


class TestFilter:
    def test_filter_files(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["action.cha", "codes.cha"])
        filtered = reader.filter(files="action")
        assert filtered.n_files == 1
        assert filtered.file_paths == ["action.cha"]

    def test_filter_single_participant(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="CHI")
        utts = filtered.utterances()
        assert len(utts) == 1
        assert utts[0].participant == "CHI"

    def test_filter_multiple_participants_list(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants=["CHI", "MOT"])
        utts = filtered.utterances()
        assert len(utts) == 2

    def test_filter_single_participant_as_list(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants=["MOT"])
        utts = filtered.utterances()
        assert len(utts) == 1
        assert utts[0].participant == "MOT"

    def test_filter_regex_alternation(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="CHI|MOT")
        utts = filtered.utterances()
        assert len(utts) == 2

    def test_filter_no_match(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="INV")
        utts = filtered.utterances()
        assert len(utts) == 0

    def test_filter_auto_anchored(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        # "CH" should NOT match "CHI" due to auto-anchoring.
        filtered = reader.filter(participants="CH")
        utts = filtered.utterances()
        assert len(utts) == 0

    def test_filter_words(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="CHI")
        words = filtered.words()
        assert "I" in words
        assert "want" in words
        assert "no" not in words

    def test_filter_tokens(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="CHI")
        tokens = filtered.tokens()
        assert all(t.word != "no" for t in tokens)

    def test_filter_participants_header(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="CHI")
        parts = filtered.participants()
        codes = [p.code for p in parts]
        assert "CHI" in codes
        assert "MOT" not in codes

    def test_filter_files_and_participants(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["action.cha", "codes.cha"])
        filtered = reader.filter(files="action", participants="CHI")
        assert filtered.n_files == 1
        utts = filtered.utterances()
        assert all(u.participant == "CHI" for u in utts)

    def test_filter_does_not_mutate_original(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        _ = reader.filter(participants="CHI")
        assert len(reader.utterances()) == 2

    def test_filter_invalid_regex(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        with pytest.raises(ValueError, match="Invalid participant regex"):
            reader.filter(participants="[invalid")

    def test_filter_wrong_type(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        with pytest.raises(TypeError):
            reader.filter(participants=123)

    def test_filter_empty_list(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        with pytest.raises(ValueError, match="must not be empty"):
            reader.filter(participants=[])

    def test_filter_participant_lookahead(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="(?=.*H).+")
        utts = filtered.utterances()
        assert len(utts) == 1
        assert utts[0].participant == "CHI"

    def test_filter_participant_negative_lookahead(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="(?!CHI).+")
        utts = filtered.utterances()
        assert len(utts) == 1
        assert utts[0].participant == "MOT"

    def test_filter_files_lookahead(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["action.cha", "codes.cha"])
        filtered = reader.filter(files="(?=.*action).+")
        assert filtered.n_files == 1
        assert filtered.file_paths == ["action.cha"]

    def test_filter_files_negative_lookahead(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["action.cha", "codes.cha"])
        filtered = reader.filter(files="^(?!.*action).+")
        assert filtered.n_files == 1
        assert filtered.file_paths == ["codes.cha"]


class TestLen:
    def test_len_raises(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        with pytest.raises(TypeError):
            len(reader)  # type: ignore[arg-type]


class TestRepr:
    def test_repr(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert "1 file(s)" in repr(reader)


class TestReprHtml:
    def test_basic_html(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        html = utts[0]._repr_html_()
        assert isinstance(html, str)
        assert "<table" in html
        assert "*CHI:" in html
        assert "want" in html
        assert "%mor:" in html
        assert "%gra:" in html
        # Reconstructed %mor from token fields
        assert "pro|I" in html
        assert "v|want" in html
        assert "n|cookie" in html
        # Reconstructed %gra from token fields
        assert "1|2|SUBJ" in html
        assert "2|0|ROOT" in html

    def test_html_no_annotations(self):
        chat_str = "@UTF8\n@Begin\n*CHI:\thello world .\n@End\n"
        reader = CHAT.from_strs([chat_str])
        utts = reader.utterances()
        html = utts[0]._repr_html_()
        assert "*CHI:" in html
        assert "hello" in html
        assert "world" in html
        assert "%mor:" not in html
        assert "%gra:" not in html

    def test_html_time_marks(self):
        chat_str = (
            "@UTF8\n@Begin\n@Participants:\tCHI Child\n"
            "*CHI:\thi . \x150_1500\x15\n@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        utts = reader.utterances()
        html = utts[0]._repr_html_()
        assert "0" in html
        assert "1500" in html
        assert "ms" in html
        assert "rustling-utterance-wrapper" in html

    def test_html_no_time_marks(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        html = utts[0]._repr_html_()
        assert "rustling-utterance-wrapper" not in html

    def test_html_escaping(self):
        utt = Utterance(
            participant="CHI",
            tokens=[Token(word="a<b")],
            time_marks=None,
            tiers={"CHI": "a<b"},
        )
        html = utt._repr_html_()
        assert "a&lt;b" in html
        assert "a<b" not in html.replace("a&lt;b", "")

    def test_html_empty_tokens(self):
        utt = Utterance(
            participant="CHI",
            tokens=[],
            time_marks=None,
            tiers={"CHI": ""},
        )
        html = utt._repr_html_()
        assert "<table" in html
        assert "*CHI:" in html


class TestCleanUtterance:
    """Test that CHAT annotations are cleaned in parsed output."""

    def test_removes_overlap_markers(self):
        chat_str = "@UTF8\n@Begin\n*CHI:\tI [<] want cookie .\n@End\n"
        reader = CHAT.from_strs([chat_str])
        words = reader.words()
        assert "[<]" not in words

    def test_removes_error_markers(self):
        chat_str = "@UTF8\n@Begin\n*CHI:\tgoed [*] .\n@End\n"
        reader = CHAT.from_strs([chat_str])
        words = reader.words()
        assert "[*]" not in words
        assert "goed" in words

    def test_removes_explanations(self):
        chat_str = "@UTF8\n@Begin\n*CHI:\tI want [= desire] cookie .\n@End\n"
        reader = CHAT.from_strs([chat_str])
        words = reader.words()
        assert "[=" not in " ".join(words)
        assert "desire" not in " ".join(words)

    def test_removes_xxx(self):
        chat_str = "@UTF8\n@Begin\n*CHI:\txxx hello .\n@End\n"
        reader = CHAT.from_strs([chat_str])
        words = reader.words()
        assert "xxx" not in words
        assert "hello" in words


class TestMorAlignment:
    def test_postclitic(self):
        chat_str = (
            "@UTF8\n@Begin\n"
            "*CHI:\tthat's good .\n"
            "%mor:\tpro:dem|that~cop|be&3S adj|good .\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        utts = reader.utterances()
        tokens = utts[0].tokens
        tokens = utts[0].tokens
        assert tokens is not None
        assert tokens[0].word == "that's"
        assert tokens[0].pos == "pro:dem"
        # Clitic token has empty word
        assert tokens[1].word == ""
        assert tokens[1].pos == "cop"
        assert tokens[2].word == "good"
        assert tokens[2].pos == "adj"


class TestContinuationLines:
    def test_continuation_joined(self):
        chat_str = "@UTF8\n@Begin\n" "*CHI:\tI want\n" "\tcookie .\n" "@End\n"
        reader = CHAT.from_strs([chat_str])
        words = reader.words()
        assert "I" in words
        assert "want" in words
        assert "cookie" in words


class TestLeadingWhitespace:
    def test_leading_whitespace_stripped(self):
        chat_str = (
            "  @UTF8\n"
            "  @Begin\n"
            "  @Participants:\tCHI Child, MOT Mother\n"
            "  *CHI:\tI want cookie .\n"
            "  %mor:\tpro|I v|want n|cookie .\n"
            "  *MOT:\tno .\n"
            "  %mor:\tco|no .\n"
            "  @End\n"
        )
        reader = CHAT.from_strs([chat_str])
        utts = reader.utterances()
        assert len(utts) == 2
        words = reader.words()
        assert words == ["I", "want", "cookie", ".", "no", "."]


class TestFromDir:
    def test_from_dir(self, testchat_good_dir):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        assert reader.n_files > 0

    def test_from_dir_with_path(self, testchat_good_dir):
        """from_dir accepts pathlib.Path directly."""
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(testchat_good_dir, strict=False)
        assert reader.n_files > 0

    def test_from_dir_with_match(self, testchat_good_dir):
        reader = CHAT.from_dir(str(testchat_good_dir), match="action")
        paths = reader.file_paths
        assert len(paths) > 0
        assert all("action" in p for p in paths)

    def test_utterances_from_real_files(self, testchat_good_dir):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        utts = reader.utterances()
        assert len(utts) > 0

    def test_words_from_real_files(self, testchat_good_dir):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        words = reader.words()
        assert len(words) > 0

    def test_testchat_strict_compliance(self, testchat_good_dir):
        """Report which testchat/good files would fail strict=True."""
        try:
            CHAT.from_dir(str(testchat_good_dir), strict=True)
        except ValueError as e:
            warnings.warn(
                f"testchat/good files failing strict=True:\n\n{e}",
                stacklevel=1,
            )

    def test_testchat_bad_files_catch_errors(self, testchat_bad_dir):
        """Every testchat/bad file must raise a parsing error with strict=True."""
        # Files requiring complex cross-tier or context-dependent checks
        # not yet implemented (Tier 3+4 validation rules).
        known_exceptions = {
            # Cross-tier checks (%mor, %gra, %pho):
            "mor-commas.cha",
            "mor-empty.cha",
            "latetalkers.cha",
            "mornumber-spanish.cha",
            "mornumber-spanish-2.cha",
            "pho-group-compound.cha",
            "pho-repetition.cha",
            "pho-repetition-bad.cha",
            # Media status vs bullet validation:
            "media-needs-bullets.cha",
            "media-notrans-bullets.cha",
            "media-unlinked-bullets.cha",
            # Retrace followed-by-content checks:
            "retrace-in-group-bad.cha",
            "retrace-multiple-no-following.cha",
            "retrace-no-following-content.cha",
            # Quotation nesting:
            "quotation-nested.cha",
            # Language-level checks:
            "language-different-speakers.cha",
            "zho-f.cha",
            # CA segment repetition:
            "ca-segment-repetition.cha",
            "ca-segment-repetition-bad-content.cha",
            # [x N] bracket context:
            "repetition.cha",
            "grouprepetition.cha",
            "x-repetition.cha",
            # @Options sign/heritage/bullet required:
            "sign.cha",
            "words-sign.cha",
            "heritage.cha",
            "heritage-lsfal14a.cha",
            "bs5.cha",
            # Participant code edge cases:
            "who.cha",
            # Other:
            "zero-others.cha",
            "space-bracket.cha",
        }
        no_error_files = []
        for path in sorted(testchat_bad_dir.glob("*.cha")):
            if path.name in known_exceptions:
                continue
            try:
                CHAT.from_files([str(path)], strict=True)
            except Exception:
                pass
            else:
                no_error_files.append(path.name)
        assert not no_error_files, (
            f"{len(no_error_files)} testchat/bad files that raised no parsing error:\n"
            + "\n".join(no_error_files)
        )

    def test_private_data_strict_compliance(self, private_data_dir):
        """Report which private test data files would fail strict=True."""
        try:
            CHAT.from_dir(str(private_data_dir), strict=True)
        except ValueError as e:
            warnings.warn(
                f"Private test data files failing strict=True:\n\n{e}",
                stacklevel=1,
            )


class TestFromFiles:
    def test_from_files(self, testchat_good_dir):
        import glob

        cha_files = sorted(glob.glob(str(testchat_good_dir / "*.cha")))[:3]
        if cha_files:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                reader = CHAT.from_files(cha_files, strict=False)
            assert reader.n_files == len(cha_files)

    def test_from_files_with_path(self, testchat_good_dir):
        """from_files accepts pathlib.Path objects in the list."""
        paths = sorted(testchat_good_dir.glob("*.cha"))[:3]
        if paths:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                reader = CHAT.from_files(paths, strict=False)
            assert reader.n_files == len(paths)

    def test_from_files_mixed_str_and_path(self, testchat_good_dir):
        """from_files accepts a mix of str and pathlib.Path."""
        all_paths = sorted(testchat_good_dir.glob("*.cha"))[:2]
        if len(all_paths) >= 2:
            mixed = [str(all_paths[0]), all_paths[1]]
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                reader = CHAT.from_files(mixed, strict=False)
            assert reader.n_files == 2


class TestAppend:
    def test_append(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*MOT:\tbye .\n@End\n"], ids=["b"])
        a.append(b)
        assert a.n_files == 2
        assert a.file_paths == ["a", "b"]

    def test_append_does_not_modify_other(self):
        a = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\tyes .\n@End\n"], ids=["c"])
        a.append(b)
        assert a.n_files == 3
        assert b.n_files == 1

    def test_append_left(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*MOT:\tbye .\n@End\n"], ids=["b"])
        a.append_left(b)
        assert a.file_paths == ["b", "a"]

    def test_append_left_preserves_order(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(TWO_FILES, ids=["b", "c"])
        a.append_left(b)
        assert a.file_paths == ["b", "c", "a"]


class TestExtend:
    def test_extend(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*MOT:\tbye .\n@End\n"], ids=["b"])
        c = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\tyes .\n@End\n"], ids=["c"])
        a.extend([b, c])
        assert a.n_files == 3
        assert a.file_paths == ["a", "b", "c"]

    def test_extend_left(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*MOT:\tbye .\n@End\n"], ids=["b"])
        c = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\tyes .\n@End\n"], ids=["c"])
        a.extend_left([b, c])
        assert a.file_paths == ["b", "c", "a"]

    def test_extend_empty_list(self):
        a = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        a.extend([])
        assert a.n_files == 2


class TestPop:
    def test_pop(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        popped = reader.pop()
        assert reader.n_files == 1
        assert reader.file_paths == ["a"]
        assert popped.n_files == 1
        assert popped.file_paths == ["b"]

    def test_pop_left(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        popped = reader.pop_left()
        assert reader.n_files == 1
        assert reader.file_paths == ["b"]
        assert popped.n_files == 1
        assert popped.file_paths == ["a"]

    def test_pop_empty_raises(self):
        reader = CHAT.from_strs([])
        with pytest.raises(IndexError):
            reader.pop()

    def test_pop_left_empty_raises(self):
        reader = CHAT.from_strs([])
        with pytest.raises(IndexError):
            reader.pop_left()

    def test_pop_preserves_data(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        popped = reader.pop()
        assert popped.words() == ["bye", "."]


class TestClear:
    def test_clear(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        reader.clear()
        assert reader.n_files == 0

    def test_clear_empty(self):
        reader = CHAT.from_strs([])
        reader.clear()
        assert reader.n_files == 0


class TestAdd:
    def test_add_creates_new_chat(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*MOT:\tbye .\n@End\n"], ids=["b"])
        c = a + b
        assert c.n_files == 2
        assert c.file_paths == ["a", "b"]

    def test_add_does_not_mutate_operands(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*MOT:\tbye .\n@End\n"], ids=["b"])
        _ = a + b
        assert a.n_files == 1
        assert a.file_paths == ["a"]
        assert b.n_files == 1
        assert b.file_paths == ["b"]

    def test_add_multi_file(self):
        a = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\tyes .\n@End\n"], ids=["c"])
        c = a + b
        assert c.n_files == 3
        assert c.file_paths == ["a", "b", "c"]

    def test_iadd_mutates_in_place(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*MOT:\tbye .\n@End\n"], ids=["b"])
        a += b
        assert a.n_files == 2
        assert a.file_paths == ["a", "b"]

    def test_iadd_does_not_mutate_other(self):
        a = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"], ids=["a"])
        b = CHAT.from_strs(["@UTF8\n@Begin\n*MOT:\tbye .\n@End\n"], ids=["b"])
        a += b
        assert b.n_files == 1
        assert b.file_paths == ["b"]


class TestBool:
    def test_empty_default_is_falsy(self):
        assert not CHAT()

    def test_empty_from_strs_is_falsy(self):
        assert not CHAT.from_strs([])

    def test_with_data_is_truthy(self):
        reader = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"])
        assert reader

    def test_after_clear_is_falsy(self):
        reader = CHAT.from_strs(["@UTF8\n@Begin\n*CHI:\thi .\n@End\n"])
        reader.clear()
        assert not reader


THREE_FILES = [
    "@UTF8\n@Begin\n*CHI:\thi .\n@End\n",
    "@UTF8\n@Begin\n*MOT:\tbye .\n@End\n",
    "@UTF8\n@Begin\n*CHI:\tyes .\n@End\n",
]

FOUR_FILES = [
    "@UTF8\n@Begin\n*CHI:\thi .\n@End\n",
    "@UTF8\n@Begin\n*MOT:\tbye .\n@End\n",
    "@UTF8\n@Begin\n*CHI:\tyes .\n@End\n",
    "@UTF8\n@Begin\n*MOT:\tok .\n@End\n",
]


class TestIterAndGetitem:
    # -- __iter__ --

    def test_iter_yields_chat_objects(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        items = list(reader)
        assert len(items) == 2
        assert all(isinstance(item, CHAT) for item in items)

    def test_iter_each_has_one_file(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        items = list(reader)
        assert items[0].n_files == 1
        assert items[1].n_files == 1

    def test_iter_preserves_order(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        items = list(reader)
        assert items[0].file_paths == ["a"]
        assert items[1].file_paths == ["b"]

    def test_iter_preserves_data(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        items = list(reader)
        assert items[0].words() == ["hi", "."]
        assert items[1].words() == ["bye", "."]

    def test_iter_empty_reader(self):
        reader = CHAT.from_strs([])
        assert list(reader) == []

    def test_iter_does_not_mutate(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        _ = list(reader)
        assert reader.n_files == 2

    def test_iter_for_loop(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        count = 0
        for item in reader:
            assert isinstance(item, CHAT)
            count += 1
        assert count == 2

    # -- __getitem__ with int --

    def test_getitem_positive_index(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        item = reader[0]
        assert isinstance(item, CHAT)
        assert item.n_files == 1
        assert item.file_paths == ["a"]

    def test_getitem_second_index(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        assert reader[1].file_paths == ["b"]

    def test_getitem_negative_index(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        assert reader[-1].file_paths == ["b"]
        assert reader[-2].file_paths == ["a"]

    def test_getitem_out_of_range(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        with pytest.raises(IndexError):
            reader[5]

    def test_getitem_negative_out_of_range(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        with pytest.raises(IndexError):
            reader[-3]

    def test_getitem_preserves_data(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        assert reader[0].words() == ["hi", "."]
        assert reader[1].words() == ["bye", "."]

    def test_getitem_does_not_mutate(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        _ = reader[0]
        assert reader.n_files == 2

    def test_getitem_empty_reader(self):
        reader = CHAT.from_strs([])
        with pytest.raises(IndexError):
            reader[0]

    # -- __getitem__ with slice --

    def test_getitem_slice_basic(self):
        reader = CHAT.from_strs(THREE_FILES, ids=["a", "b", "c"])
        result = reader[:2]
        assert isinstance(result, CHAT)
        assert result.n_files == 2
        assert result.file_paths == ["a", "b"]

    def test_getitem_slice_with_start(self):
        reader = CHAT.from_strs(THREE_FILES, ids=["a", "b", "c"])
        result = reader[1:]
        assert result.n_files == 2
        assert result.file_paths == ["b", "c"]

    def test_getitem_slice_with_start_and_stop(self):
        reader = CHAT.from_strs(FOUR_FILES, ids=["a", "b", "c", "d"])
        result = reader[1:3]
        assert result.n_files == 2
        assert result.file_paths == ["b", "c"]

    def test_getitem_slice_with_step(self):
        reader = CHAT.from_strs(FOUR_FILES, ids=["a", "b", "c", "d"])
        result = reader[::2]
        assert result.n_files == 2
        assert result.file_paths == ["a", "c"]

    def test_getitem_slice_negative(self):
        reader = CHAT.from_strs(THREE_FILES, ids=["a", "b", "c"])
        result = reader[-2:]
        assert result.n_files == 2
        assert result.file_paths == ["b", "c"]

    def test_getitem_slice_negative_step(self):
        reader = CHAT.from_strs(THREE_FILES, ids=["a", "b", "c"])
        result = reader[::-1]
        assert result.n_files == 3
        assert result.file_paths == ["c", "b", "a"]

    def test_getitem_slice_empty_result(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        result = reader[5:10]
        assert isinstance(result, CHAT)
        assert result.n_files == 0

    def test_getitem_slice_full(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        result = reader[:]
        assert result.n_files == 2
        assert result.file_paths == ["a", "b"]

    def test_getitem_slice_does_not_mutate(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        _ = reader[:1]
        assert reader.n_files == 2

    def test_getitem_slice_preserves_data(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        result = reader[:1]
        assert result.words() == ["hi", "."]

    # -- __getitem__ type error --

    def test_getitem_invalid_type(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        with pytest.raises(TypeError):
            reader["key"]  # type: ignore[index]


class TestToStrs:
    def test_to_strs_basic(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        strs = reader.to_strs()
        assert len(strs) == 1
        assert "@UTF8" in strs[0]
        assert "@Begin" in strs[0]
        assert "@End" in strs[0]
        assert "*CHI:" in strs[0]
        assert "%mor:" in strs[0]

    def test_to_strs_multiple_files(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        strs = reader.to_strs()
        assert len(strs) == 2

    def test_to_strs_round_trip(self):
        reader1 = CHAT.from_strs([BASIC_CHAT])
        strs = reader1.to_strs()
        reader2 = CHAT.from_strs(strs)
        assert reader1.words() == reader2.words()
        assert len(reader1.utterances()) == len(reader2.utterances())

    def test_to_strs_empty(self):
        reader = CHAT.from_strs([])
        assert reader.to_strs() == []

    def test_to_strs_round_trip_real_files(self, testchat_good_dir):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
            strs = reader.to_strs()
            reader2 = CHAT.from_strs(strs, strict=False)
        assert reader.words() == reader2.words()


class TestToFiles:
    def test_to_files_single_file(self, tmp_path):
        reader = CHAT.from_strs([BASIC_CHAT])
        out_dir = str(tmp_path / "output")
        reader.to_files(out_dir)
        reader2 = CHAT.from_dir(out_dir)
        assert reader2.words() == reader.words()

    def test_to_files_with_path(self, tmp_path):
        """to_files accepts pathlib.Path directly."""
        reader = CHAT.from_strs([BASIC_CHAT])
        out_dir = tmp_path / "output"
        reader.to_files(out_dir)
        reader2 = CHAT.from_dir(out_dir)
        assert reader2.words() == reader.words()

    def test_to_files_directory(self, tmp_path):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        out_dir = str(tmp_path / "output")
        reader.to_files(out_dir)
        reader2 = CHAT.from_dir(out_dir)
        assert reader2.n_files == 2

    def test_to_files_custom_filenames(self, tmp_path):
        import os

        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        out_dir = str(tmp_path / "output")
        reader.to_files(out_dir, filenames=["file1.cha", "file2.cha"])
        assert os.path.exists(os.path.join(out_dir, "file1.cha"))
        assert os.path.exists(os.path.join(out_dir, "file2.cha"))

    def test_to_files_default_filenames_from_ids(self, tmp_path):
        import os

        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        out_dir = str(tmp_path / "output")
        reader.to_files(out_dir)
        # IDs "a" and "b" are not UUIDs, so filenames are derived from them.
        assert os.path.exists(os.path.join(out_dir, "a.cha"))
        assert os.path.exists(os.path.join(out_dir, "b.cha"))

    def test_to_files_default_filenames_numbered(self, tmp_path):
        import os

        # No ids -> UUIDs are generated -> numbered fallback.
        reader = CHAT.from_strs(TWO_FILES)
        out_dir = str(tmp_path / "output")
        reader.to_files(out_dir)
        assert os.path.exists(os.path.join(out_dir, "0001.cha"))
        assert os.path.exists(os.path.join(out_dir, "0002.cha"))

    def test_to_files_filename_count_mismatch_raises(self, tmp_path):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        with pytest.raises(ValueError):
            reader.to_files(
                str(tmp_path / "output"),
                filenames=["only_one.cha"],
            )

    def test_to_files_preserves_filenames(self, testchat_good_dir, tmp_path):
        import os

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        out_dir = str(tmp_path / "output")
        reader.to_files(out_dir)
        # Filenames should be derived from original file paths.
        for fp in reader.file_paths:
            expected = os.path.splitext(os.path.basename(fp))[0] + ".cha"
            assert os.path.exists(os.path.join(out_dir, expected)), expected

    def test_to_files_round_trip_real_files(self, testchat_good_dir, tmp_path):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
            out_dir = str(tmp_path / "output")
            reader.to_files(out_dir)
            reader2 = CHAT.from_dir(out_dir, strict=False)
        assert reader.words() == reader2.words()


class TestToElan:
    def test_to_elan_returns_elan_object(self):
        from rustling.elan import ELAN

        reader = CHAT.from_strs([BASIC_CHAT])
        elan = reader.to_elan()
        assert isinstance(elan, ELAN)
        assert elan.n_files == 1

    def test_to_elan_tiers(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        elan = reader.to_elan()
        tiers_dict = elan.tiers()[0]
        tier_ids = list(tiers_dict.keys())
        assert "CHI" in tier_ids
        assert "MOT" in tier_ids
        assert "mor@CHI" in tier_ids
        assert "gra@CHI" in tier_ids
        assert "mor@MOT" in tier_ids
        assert "gra@MOT" in tier_ids

    def test_to_elan_strs(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        strs = reader.to_elan_strs()
        assert len(strs) == 1
        assert "ANNOTATION_DOCUMENT" in strs[0]
        assert "TIER_ID" in strs[0]

    def test_to_elan_dep_tier_structure(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        elan = reader.to_elan()
        tiers_dict = elan.tiers()[0]
        chi_tier = tiers_dict["CHI"]
        mor_chi = tiers_dict["mor@CHI"]
        assert chi_tier.parent_id is None
        assert mor_chi.parent_id == "CHI"
        # CHI has 1 utterance in BASIC_CHAT.
        assert len(chi_tier.annotations) == 1
        assert chi_tier.annotations[0].value == "I want cookie ."
        assert len(mor_chi.annotations) == 1
        assert mor_chi.annotations[0].value == "pro|I v|want n|cookie ."

    def test_to_elan_multiple_files(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        elan = reader.to_elan()
        assert elan.n_files == 2

    def test_to_elan_files_directory(self, tmp_path):
        from rustling.elan import ELAN

        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        out_dir = str(tmp_path / "output")
        reader.to_elan_files(out_dir)
        elan = ELAN.from_dir(out_dir)
        assert elan.n_files == 2

    def test_to_elan_files_custom_filenames(self, tmp_path):
        import os

        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        out_dir = str(tmp_path / "output")
        reader.to_elan_files(out_dir, filenames=["a.eaf", "b.eaf"])
        assert os.path.exists(os.path.join(out_dir, "a.eaf"))
        assert os.path.exists(os.path.join(out_dir, "b.eaf"))

    def test_to_elan_files_default_filenames_from_ids(self, tmp_path):
        import os

        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        out_dir = str(tmp_path / "output")
        reader.to_elan_files(out_dir)
        # IDs "a" and "b" are not UUIDs, so filenames derive as a.eaf, b.eaf.
        assert os.path.exists(os.path.join(out_dir, "a.eaf"))
        assert os.path.exists(os.path.join(out_dir, "b.eaf"))

    def test_to_elan_files_default_filenames_numbered(self, tmp_path):
        import os

        reader = CHAT.from_strs(TWO_FILES)
        out_dir = str(tmp_path / "output")
        reader.to_elan_files(out_dir)
        assert os.path.exists(os.path.join(out_dir, "0001.eaf"))
        assert os.path.exists(os.path.join(out_dir, "0002.eaf"))

    def test_to_elan_files_preserves_filenames(self, testchat_good_dir, tmp_path):
        import os

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        out_dir = str(tmp_path / "output")
        reader.to_elan_files(out_dir)
        # Filenames should be derived from original .cha paths with .eaf extension.
        for fp in reader.file_paths:
            expected = os.path.splitext(os.path.basename(fp))[0] + ".eaf"
            assert os.path.exists(os.path.join(out_dir, expected)), expected

    def test_to_elan_files_round_trip_real_files(self, testchat_good_dir, tmp_path):
        from rustling.elan import ELAN

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        out_dir = str(tmp_path / "output")
        reader.to_elan_files(out_dir)
        elan = ELAN.from_dir(out_dir)
        assert elan.n_files == reader.n_files


class TestPopAndStitch:
    def test_pop_and_append(self):
        reader = CHAT.from_strs(TWO_FILES, ids=["a", "b"])
        n = reader.n_files
        popped = reader.pop()
        assert reader.n_files == n - 1
        reader.append(popped)
        assert reader.n_files == n

    def test_pop_and_append_real_files(self, testchat_good_dir):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        n = reader.n_files
        all_words = reader.words()
        popped = reader.pop()
        reader.append(popped)
        assert reader.n_files == n
        assert reader.words() == all_words


HEADER_CHAT = (
    "@UTF8\n"
    "@PID:\t11312/c-00044068-1\n"
    "@Begin\n"
    "@Languages:\teng, zho\n"
    "@Participants:\tCHI Child Target_Child, MOT Mary Mother\n"
    "@ID:\teng|brown|CHI|2;10.05|male|||Target_Child|||\n"
    "@ID:\teng|brown|MOT||female|||Mother|||\n"
    "@Date:\t25-JAN-1983\n"
    "@Location:\tBoston, MA, USA\n"
    "@Media:\tabe88, audio, missing\n"
    "@Situation:\tPlaying with toys\n"
    "@Comment:\tFirst recording\n"
    "@Comment:\tfor Yiddish/Hebrew glossary see file 4504\n"
    "*CHI:\thello .\n"
    "@New Episode\n"
    "*MOT:\thi .\n"
    "@Comment:\tChild laughs\n"
    "*CHI:\tcookie .\n"
    "@End\n"
)


class TestHeaders:
    def test_headers_returns_list(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        headers = reader.headers()
        assert isinstance(headers, list)
        assert len(headers) == 1
        assert isinstance(headers[0], Headers)

    def test_pid(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert h.pid == "11312/c-00044068-1"

    def test_languages(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert h.languages == ["eng", "zho"]

    def test_languages_method(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        assert reader.languages() == ["eng", "zho"]

    def test_languages_by_file(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        result = reader.languages(by_file=True)
        assert result == [["eng", "zho"]]

    def test_date(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert h.date == datetime.date(1983, 1, 25)

    def test_date_iso(self):
        chat_str = HEADER_CHAT.replace("25-JAN-1983", "1983-01-25")
        reader = CHAT.from_strs([chat_str])
        h = reader.headers()[0]
        assert h.date == datetime.date(1983, 1, 25)

    def test_date_unparseable(self):
        chat_str = HEADER_CHAT.replace("25-JAN-1983", "not-a-date")
        reader = CHAT.from_strs([chat_str])
        h = reader.headers()[0]
        assert h.date is None

    def test_location(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert h.location == "Boston, MA, USA"

    def test_situation(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert h.situation == "Playing with toys"

    def test_media(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert isinstance(h.media, dict)
        assert h.media["filename"] == "abe88"
        assert h.media["format"] == "audio"
        assert h.media["status"] == "missing"

    def test_comments(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert isinstance(h.comments, list)
        assert len(h.comments) == 2
        assert h.comments[0] == "First recording"
        assert h.comments[1] == "for Yiddish/Hebrew glossary see file 4504"

    def test_comments_empty(self):
        simple = (
            "@UTF8\n@Begin\n@Participants:\tCHI Child Target_Child\n"
            "*CHI:\thello .\n@End\n"
        )
        reader = CHAT.from_strs([simple])
        h = reader.headers()[0]
        assert h.comments is None

    def test_other_empty_by_default(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert isinstance(h.other, dict)


class TestParticipants:
    def test_participants_count(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        h = reader.headers()[0]
        assert len(h.participants) == 2

    def test_participant_fields(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        chi = reader.headers()[0].participants[0]
        assert isinstance(chi, Participant)
        assert chi.code == "CHI"
        assert chi.name == "Child"
        assert chi.role == "Target_Child"

    def test_participant_id_fields(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        chi = reader.headers()[0].participants[0]
        assert chi.language == "eng"
        assert chi.corpus == "brown"
        assert chi.sex == "male"

    def test_participant_age(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        chi = reader.headers()[0].participants[0]
        assert isinstance(chi.age, Age)
        assert chi.age.years == 2
        assert chi.age.months == 10
        assert chi.age.days == 5

    def test_age_in_months(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        chi = reader.headers()[0].participants[0]
        assert chi.age is not None
        months = chi.age.in_months()
        assert months == pytest.approx(34.1667, abs=0.01)

    def test_participant_no_age(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        mot = reader.headers()[0].participants[1]
        assert mot.code == "MOT"
        assert mot.age is None
        assert mot.sex == "female"

    def test_participants_method(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        parts = reader.participants()
        assert len(parts) == 2
        assert [p.code for p in parts] == ["CHI", "MOT"]

    def test_participants_by_file(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        result = reader.participants(by_file=True)
        assert len(result) == 1
        assert isinstance(result[0], list)
        assert len(result[0]) == 2

    def test_participant_specific_headers(self):
        chat_str = (
            "@UTF8\n"
            "@Begin\n"
            "@Languages:\teng\n"
            "@Participants:\tCHI Ross Target_Child\n"
            "@ID:\teng|macwhinney|CHI|2;06.||||Target_Child|||\n"
            "@Birth of CHI:\t28-JUN-2001\n"
            "@Birthplace of CHI:\tPittsburgh, PA\n"
            "@L1 of CHI:\teng\n"
            "*CHI:\thello .\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        chi = reader.headers()[0].participants[0]
        assert chi.birth == "28-JUN-2001"
        assert chi.birthplace == "Pittsburgh, PA"
        assert chi.l1 == "eng"


class TestHeadersMultipleFiles:
    def test_headers_multiple_files(self):
        strs = [
            "@UTF8\n@Begin\n@Languages:\teng\n"
            "@Participants:\tCHI Child Target_Child\n"
            "*CHI:\thi .\n@End\n",
            "@UTF8\n@Begin\n@Languages:\tzho\n"
            "@Participants:\tMOT Mother Mother\n"
            "*MOT:\tbye .\n@End\n",
        ]
        reader = CHAT.from_strs(strs)
        headers = reader.headers()
        assert len(headers) == 2
        assert headers[0].languages == ["eng"]
        assert headers[1].languages == ["zho"]

    def test_languages_flat_multiple_files(self):
        strs = [
            "@UTF8\n@Begin\n@Languages:\teng\n"
            "@Participants:\tCHI Child Target_Child\n"
            "*CHI:\thi .\n@End\n",
            "@UTF8\n@Begin\n@Languages:\tzho\n"
            "@Participants:\tMOT Mother Mother\n"
            "*MOT:\tbye .\n@End\n",
        ]
        reader = CHAT.from_strs(strs)
        assert reader.languages() == ["eng", "zho"]

    def test_participants_flat_multiple_files(self):
        strs = [
            "@UTF8\n@Begin\n@Languages:\teng\n"
            "@Participants:\tCHI Child Target_Child\n"
            "*CHI:\thi .\n@End\n",
            "@UTF8\n@Begin\n@Languages:\tzho\n"
            "@Participants:\tMOT Mother Mother\n"
            "*MOT:\tbye .\n@End\n",
        ]
        reader = CHAT.from_strs(strs)
        parts = reader.participants()
        assert len(parts) == 2
        assert [p.code for p in parts] == ["CHI", "MOT"]


class TestHeadersRealFiles:
    def test_headers_from_dir(self, testchat_good_dir):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        headers = reader.headers()
        assert len(headers) == reader.n_files
        for h in headers:
            assert isinstance(h, Headers)

    def test_languages_from_dir(self, testchat_good_dir):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        langs = reader.languages(by_file=True)
        assert len(langs) == reader.n_files
        for file_langs in langs:
            assert isinstance(file_langs, list)

    def test_participants_from_dir(self, testchat_good_dir):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            reader = CHAT.from_dir(str(testchat_good_dir), strict=False)
        parts = reader.participants(by_file=True)
        assert len(parts) == reader.n_files
        for file_parts in parts:
            assert isinstance(file_parts, list)
            for p in file_parts:
                assert isinstance(p, Participant)
                assert p.code  # non-empty


MISALIGNED_CHAT = (
    "@UTF8\n"
    "@Begin\n"
    "@Participants:\tCHI Child\n"
    "*CHI:\tI want cookie .\n"
    "%mor:\tpro|I v|want .\n"  # 2 non-clitic mor items vs 3 words + punct
    "@End\n"
)

WELL_FORMED_CHAT = (
    "@UTF8\n"
    "@Begin\n"
    "@Participants:\tCHI Child\n"
    "*CHI:\tI want cookie .\n"
    "%mor:\tpro|I v|want n|cookie .\n"
    "@End\n"
)


class TestStrictMode:
    def test_strict_true_raises(self):
        with pytest.raises(ValueError, match="misalignment"):
            CHAT.from_strs([MISALIGNED_CHAT], strict=True)

    def test_strict_default_raises(self):
        """Default strict=True should raise on misalignment."""
        with pytest.raises(ValueError, match="misalignment"):
            CHAT.from_strs([MISALIGNED_CHAT])

    def test_strict_true_error_message_details(self):
        with pytest.raises(ValueError, match="CHI") as exc_info:
            CHAT.from_strs([MISALIGNED_CHAT], strict=True)
        msg = str(exc_info.value)
        assert "%mor" in msg or "mor" in msg
        assert "strict=False" in msg

    def test_strict_false_warns(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            CHAT.from_strs([MISALIGNED_CHAT], strict=False)
            assert len(w) == 1
            assert issubclass(w[0].category, UserWarning)
            assert "misalignment" in str(w[0].message)

    def test_strict_false_empty_tokens(self):
        with warnings.catch_warnings(record=True):
            warnings.simplefilter("always")
            reader = CHAT.from_strs([MISALIGNED_CHAT], strict=False)
        utts = reader.utterances()
        assert len(utts) == 1
        assert utts[0].tokens == []

    def test_strict_false_preserves_tiers(self):
        with warnings.catch_warnings(record=True):
            warnings.simplefilter("always")
            reader = CHAT.from_strs([MISALIGNED_CHAT], strict=False)
        utts = reader.utterances()
        tiers = utts[0].tiers
        assert tiers is not None
        assert "CHI" in tiers
        assert "%mor" in tiers
        assert "cookie" in tiers["CHI"]

    def test_no_misalignment_strict_true_ok(self):
        reader = CHAT.from_strs([WELL_FORMED_CHAT], strict=True)
        utts = reader.utterances()
        assert len(utts) == 1
        assert utts[0].tokens is not None
        assert len(utts[0].tokens) == 4  # I, want, cookie, .

    def test_strict_parallel_false(self):
        with pytest.raises(ValueError, match="misalignment"):
            CHAT.from_strs([MISALIGNED_CHAT], parallel=False, strict=True)

        with warnings.catch_warnings(record=True):
            warnings.simplefilter("always")
            reader = CHAT.from_strs([MISALIGNED_CHAT], parallel=False, strict=False)
        utts = reader.utterances()
        assert utts[0].tokens == []
        assert utts[0].tiers is not None
        assert "%mor" in utts[0].tiers


class TestDevelopmentalMeasures:
    def test_mlum_basic(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.mlum()
        assert result == [3.0]

    def test_mlu_aliases_mlum(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert reader.mlu() == reader.mlum()

    def test_mluw_basic(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.mluw()
        assert result == [3.0]

    def test_ttr_basic(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.ttr()
        assert result == [1.0]

    def test_mlum_multiple_files(self):
        reader = CHAT.from_strs([BASIC_CHAT, BASIC_CHAT], ids=["a", "b"])
        result = reader.mlum()
        assert len(result) == 2
        assert result == [3.0, 3.0]

    def test_mluw_multiple_files(self):
        reader = CHAT.from_strs([BASIC_CHAT, BASIC_CHAT], ids=["a", "b"])
        result = reader.mluw()
        assert len(result) == 2
        assert result == [3.0, 3.0]

    def test_ttr_multiple_files(self):
        reader = CHAT.from_strs([BASIC_CHAT, BASIC_CHAT], ids=["a", "b"])
        result = reader.ttr()
        assert len(result) == 2
        assert result == [1.0, 1.0]

    def test_mlum_empty(self):
        reader = CHAT.from_strs([])
        assert reader.mlum() == []

    def test_mluw_empty(self):
        reader = CHAT.from_strs([])
        assert reader.mluw() == []

    def test_ttr_empty(self):
        reader = CHAT.from_strs([])
        assert reader.ttr() == []

    def test_ttr_repeated_words(self):
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\tno no no .\n"
            "%mor:\tco|no co|no co|no .\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        result = reader.ttr()
        assert result == pytest.approx([1.0 / 3.0])

    def test_mlum_participant(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert reader.mlum(participant="MOT") == [1.0]

    def test_mluw_participant(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert reader.mluw(participant="MOT") == [1.0]

    def test_mlum_n_none(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert reader.mlum(n=None) == [3.0]

    def test_mluw_n_none(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert reader.mluw(n=None) == [3.0]

    def test_mlum_n_truncation(self):
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\tI want cookie .\n"
            "%mor:\tpro|I v|want n|cookie .\n"
            "*CHI:\tno .\n"
            "%mor:\tco|no .\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        # All CHI utterances: (3 + 1) / 2 = 2.0
        assert reader.mlum(n=None) == [2.0]
        # First 1 utterance only: 3 / 1 = 3.0
        assert reader.mlum(n=1) == [3.0]

    def test_mluw_n_truncation(self):
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\tI want cookie .\n"
            "%mor:\tpro|I v|want n|cookie .\n"
            "*CHI:\tno .\n"
            "%mor:\tco|no .\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        # All CHI utterances: (3 + 1) / 2 = 2.0
        assert reader.mluw(n=None) == [2.0]
        # First 1 utterance only: 3 / 1 = 3.0
        assert reader.mluw(n=1) == [3.0]

    def test_ttr_participant(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        # MOT says "no" -> 1 unique / 1 total = 1.0
        assert reader.ttr(participant="MOT") == [1.0]

    def test_ttr_n_none(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert reader.ttr(n=None) == [1.0]

    def test_ttr_n_truncation(self):
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\tno no no yes yes .\n"
            "%mor:\tco|no co|no co|no co|yes co|yes .\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        # All 5 tokens: 2 unique / 5 total = 0.4
        assert reader.ttr(n=None) == pytest.approx([2.0 / 5.0])
        # First 3 tokens ("no", "no", "no"): 1 unique / 3 total = 1/3
        assert reader.ttr(n=3) == pytest.approx([1.0 / 3.0])

    def test_measures_with_filter(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        filtered = reader.filter(participants="CHI")
        assert filtered.mlum() == [3.0]
        assert filtered.mluw() == [3.0]
        assert filtered.ttr() == [1.0]

    def test_measures_return_type(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert isinstance(reader.mlum(), list)
        assert isinstance(reader.mluw(), list)
        assert isinstance(reader.ttr(), list)
        assert all(isinstance(v, float) for v in reader.mlum())


class TestAges:
    def test_ages_basic(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        result = reader.ages()
        assert len(result) == 1
        age = result[0]
        assert isinstance(age, Age)
        assert age.years == 2
        assert age.months == 10
        assert age.days == 5

    def test_ages_no_chi(self):
        chat_str = (
            "@UTF8\n@Begin\n" "@Participants:\tMOT Mother\n" "*MOT:\thello .\n" "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        assert reader.ages() == [None]

    def test_ages_empty(self):
        reader = CHAT.from_strs([])
        assert reader.ages() == []

    def test_ages_multiple_files(self):
        reader = CHAT.from_strs([HEADER_CHAT, HEADER_CHAT], ids=["a", "b"])
        result = reader.ages()
        assert len(result) == 2
        assert result[0] is not None and result[0].years == 2
        assert result[1] is not None and result[1].years == 2


class TestWordNgrams:
    def test_word_ngrams_unigrams(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        counter = reader.word_ngrams(1)
        # BASIC_CHAT: "*CHI: I want cookie ." and "*MOT: no ."
        # Words: I, want, cookie, ., no, .
        assert counter.get(["I"]) == 1
        assert counter.get(["want"]) == 1
        assert counter.get(["cookie"]) == 1
        assert counter.get(["no"]) == 1

    def test_word_ngrams_bigrams(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        counter = reader.word_ngrams(2)
        assert counter.get(["I", "want"]) == 1
        assert counter.get(["want", "cookie"]) == 1

    def test_word_ngrams_returns_ngrams(self):
        from rustling.ngram import Ngrams

        reader = CHAT.from_strs([BASIC_CHAT])
        counter = reader.word_ngrams(1)
        assert isinstance(counter, Ngrams)
        assert counter.n == 1

    def test_word_ngrams_no_cross_utterance(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        counter = reader.word_ngrams(2)
        # "." ends first utterance, "no" starts second. Should not have (".", "no").
        assert counter.get([".", "no"]) == 0

    def test_word_ngrams_empty_reader(self):
        reader = CHAT.from_strs([])
        counter = reader.word_ngrams(1)
        assert len(counter) == 0
        assert counter.total() == 0

    def test_word_ngrams_multiple_files(self):
        reader = CHAT.from_strs(
            [
                "@UTF8\n@Begin\n*CHI:\thi .\n@End\n",
                "@UTF8\n@Begin\n*MOT:\thi .\n@End\n",
            ],
            ids=["a", "b"],
        )
        counter = reader.word_ngrams(1)
        assert counter.get(["hi"]) == 2


class TestHeadTail:
    def test_head_returns_utterances(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.head()
        assert type(result).__name__ == "Utterances"

    def test_head_repr_displays_formatted(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.head()
        text = repr(result)
        assert "*CHI:" in text
        assert "*MOT:" in text
        # repr should contain actual newlines, not escaped ones
        assert "\n" in text

    def test_head_str_matches_repr(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.head()
        assert str(result) == repr(result)

    def test_head_len(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert len(reader.head()) == 2
        assert len(reader.head(1)) == 1

    def test_head_getitem(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.head()
        assert isinstance(result[0], Utterance)
        assert result[0].participant == "CHI"
        assert result[-1].participant == "MOT"

    def test_head_getitem_out_of_range(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.head(1)
        with pytest.raises(IndexError):
            result[5]

    def test_head_iter(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.head()
        participants = [u.participant for u in result]
        assert participants == ["CHI", "MOT"]

    def test_head_n_1(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.head(1)
        text = repr(result)
        assert "*CHI:" in text
        assert "*MOT:" not in text

    def test_tail_n_1(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.tail(1)
        text = repr(result)
        assert "*MOT:" in text
        assert "*CHI:" not in text

    def test_head_with_mor_and_gra(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        text = repr(reader.head(1))
        assert "%mor:" in text
        assert "%gra:" in text
        assert "pro|I" in text
        assert "1|2|SUBJ" in text

    def test_head_no_mor(self):
        chat_str = "@UTF8\n@Begin\n*CHI:\thello world .\n@End\n"
        reader = CHAT.from_strs([chat_str])
        text = repr(reader.head())
        assert "*CHI:" in text
        assert "%mor:" not in text
        assert "%gra:" not in text

    def test_head_empty_reader(self):
        reader = CHAT.from_strs([])
        result = reader.head()
        assert len(result) == 0
        assert repr(result) == ""

    def test_tail_empty_reader(self):
        reader = CHAT.from_strs([])
        result = reader.tail()
        assert len(result) == 0
        assert repr(result) == ""

    def test_head_separation(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        text = repr(reader.head())
        assert "\n\n" in text

    def test_utterance_to_str(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        s = utts[0].to_str()
        assert isinstance(s, str)
        assert "*CHI:" in s

    def test_head_time_marks(self):
        chat_str = (
            "@UTF8\n@Begin\n@Participants:\tCHI Child\n"
            "*CHI:\thi . \x150_1500\x15\n@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        text = repr(reader.head())
        assert "0" in text
        assert "1500" in text
        assert "ms" in text

    def test_head_column_alignment(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        text = repr(reader.head(1))
        lines = text.strip().split("\n")
        # participant, %mor, %gra rows should all be present
        assert len(lines) >= 3
        # All lines should start with a label of the same padded width
        first_data_offsets = []
        for line in lines:
            # Find the position after the label where data starts
            idx = line.find("  ")
            if idx >= 0:
                first_data_offsets.append(idx)
        # All offsets should be the same
        assert len(set(first_data_offsets)) == 1

    def test_head_repr_html(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.head()
        html = result._repr_html_()
        assert "<table" in html
        assert "*CHI:" in html


class TestChangeableHeaders:
    """Test that changeable headers appear interleaved with utterances."""

    def test_changeable_headers_in_utterances(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        utts = reader.utterances()
        # HEADER_CHAT has 3 real utterances + 2 mid-file changeable headers
        # (@New Episode and @Comment: Child laughs)
        # File-level headers (@Comment: First recording, etc.) are NOT included.
        assert len(utts) == 5

    def test_ordering_preserved(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        utts = reader.utterances()
        # Expected mid-file order:
        # 0: *CHI: hello .
        # 1: @New Episode
        # 2: *MOT: hi .
        # 3: @Comment: Child laughs
        # 4: *CHI: cookie .
        assert utts[0].participant == "CHI"
        assert utts[0].changeable_header is None
        assert isinstance(utts[1].changeable_header, ChangeableHeader.NewEpisode)
        assert utts[2].participant == "MOT"
        assert isinstance(utts[3].changeable_header, ChangeableHeader.Comment)
        assert utts[3].changeable_header.value == "Child laughs"
        assert utts[4].participant == "CHI"

    def test_changeable_header_fields_none(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        utts = reader.utterances()
        headers = [u for u in utts if u.changeable_header is not None]
        assert len(headers) == 2
        for h in headers:
            assert h.participant is None
            assert h.tokens is None
            assert h.tiers is None

    def test_real_utterance_has_no_changeable_header(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        utts = reader.utterances()
        real = [u for u in utts if u.changeable_header is None]
        assert len(real) == 3
        for u in real:
            assert u.participant is not None
            assert u.tokens is not None
            assert u.tiers is not None

    def test_file_level_headers_excluded(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        utts = reader.utterances()
        # The file-level @Comment headers should NOT appear in utterances.
        comments = [
            u
            for u in utts
            if u.changeable_header is not None
            and isinstance(u.changeable_header, ChangeableHeader.Comment)
        ]
        # Only the mid-file @Comment: Child laughs, not the file-level ones.
        assert len(comments) == 1
        assert comments[0].changeable_header.value == "Child laughs"

    def test_words_skips_headers(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        words = reader.words()
        assert "hello" in words
        assert "hi" in words
        assert "cookie" in words
        # No changeable header content in words
        assert len(words) == 6  # hello, ., hi, ., cookie, .

    def test_filter_drops_changeable_headers(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        filtered = reader.filter(participants="CHI")
        utts = filtered.utterances()
        # Only CHI utterances (hello, cookie); changeable headers dropped
        assert len(utts) == 2
        assert all(u.participant == "CHI" for u in utts)
        assert all(u.changeable_header is None for u in utts)


BASIC_CHAT_2 = (
    "@UTF8\n"
    "@Begin\n"
    "@Participants:\tMOT Mother\n"
    "*MOT:\tgood morning .\n"
    "%mor:\tadj|good n|morning .\n"
    "%gra:\t1|2|MOD 2|0|ROOT 3|2|PUNCT\n"
    "@End\n"
)


class TestInfo:
    def test_info_single_file(self, capsys):
        reader = CHAT.from_strs([BASIC_CHAT])
        reader.info()
        output = capsys.readouterr().out
        lines = output.strip().split("\n")
        assert lines[0] == "1 files"
        assert lines[1] == "2 utterances"
        assert lines[2] == "6 words"
        # Single file: no table
        assert len(lines) == 3

    def test_info_multiple_files(self, capsys):
        reader = CHAT.from_strs([BASIC_CHAT, BASIC_CHAT_2], ids=["a.cha", "b.cha"])
        reader.info()
        output = capsys.readouterr().out
        lines = output.strip().split("\n")
        assert lines[0] == "2 files"
        assert lines[1] == "3 utterances"
        assert lines[2] == "9 words"
        # Table header, separator, 2 data rows, truncation
        assert "Utterance Count" in lines[3]
        assert "Word Count" in lines[3]
        assert "File Path" in lines[3]
        # Separator line (all dashes and spaces)
        assert set(lines[4].strip()) <= {"-", " "}
        # Data rows
        assert "#1" in lines[5]
        assert "a.cha" in lines[5]
        assert "#2" in lines[6]
        assert "b.cha" in lines[6]

    def test_info_verbose(self, capsys):
        files = [BASIC_CHAT] * 7
        ids = [f"file{i}.cha" for i in range(7)]
        reader = CHAT.from_strs(files, ids=ids)
        reader.info(verbose=True)
        output = capsys.readouterr().out
        assert "7 files" in output
        assert "#7" in output
        assert "verbose" not in output

    def test_info_not_verbose_truncates(self, capsys):
        files = [BASIC_CHAT] * 7
        ids = [f"file{i}.cha" for i in range(7)]
        reader = CHAT.from_strs(files, ids=ids)
        reader.info()
        output = capsys.readouterr().out
        assert "#5" in output
        assert "#6" not in output
        assert "set `verbose` to True for all the files" in output

    def test_info_empty(self, capsys):
        reader = CHAT.from_strs([])
        reader.info()
        output = capsys.readouterr().out
        lines = output.strip().split("\n")
        assert lines[0] == "0 files"
        assert lines[1] == "0 utterances"
        assert lines[2] == "0 words"

    def test_no_changeable_headers_in_basic(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        assert len(utts) == 2
        assert all(u.changeable_header is None for u in utts)

    def test_to_str_changeable_header(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        utts = reader.utterances()
        # @New Episode
        assert utts[1].to_str() == "@New Episode"
        # @Comment: Child laughs
        assert utts[3].to_str() == "@Comment:\tChild laughs"

    def test_head_includes_headers(self):
        reader = CHAT.from_strs([HEADER_CHAT])
        result = reader.head(10)
        has_header = any(u.changeable_header is not None for u in result)
        assert has_header


class TestIPSyn:
    def test_ipsyn_basic(self):
        # BASIC_CHAT has "I want cookie ." with SUBJ/OBJ/ROOT GRA
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.ipsyn()
        assert len(result) == 1
        # Should score: N1 (noun), N2 (pronoun), V1 (verb), S1 (2+ words),
        # S2 (subj-verb -> credits S1), S3 (verb-obj -> credits S1),
        # S4 (SVO -> credits S2, S3), plus more
        assert result[0] > 0

    def test_ipsyn_empty(self):
        reader = CHAT.from_strs([])
        assert reader.ipsyn() == []

    def test_ipsyn_no_matching_participant(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        assert reader.ipsyn(participant="INV") == [0]

    def test_ipsyn_participant(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        # MOT only says "no ." — not much to score
        result = reader.ipsyn(participant="MOT")
        assert len(result) == 1
        assert result[0] < reader.ipsyn()[0]

    def test_ipsyn_n_truncation(self):
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\tI want cookie .\n"
            "%mor:\tpro|I v|want n|cookie .\n"
            "%gra:\t1|2|SUBJ 2|0|ROOT 3|2|OBJ 4|2|PUNCT\n"
            "*CHI:\tthe big dog is running .\n"
            "%mor:\tdet|the adj|big n|dog cop|be&3S v|run-PRESP .\n"
            "%gra:\t1|3|DET 2|3|MOD 3|4|SUBJ 4|0|ROOT 5|4|PRED 6|4|PUNCT\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        score_all = reader.ipsyn(n=None)[0]
        score_one = reader.ipsyn(n=1)[0]
        # More utterances should give >= score
        assert score_all >= score_one

    def test_ipsyn_n_none(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.ipsyn(n=None)
        assert len(result) == 1

    def test_ipsyn_multiple_files(self):
        reader = CHAT.from_strs([BASIC_CHAT, BASIC_CHAT], ids=["a", "b"])
        result = reader.ipsyn()
        assert len(result) == 2
        assert result[0] == result[1]

    def test_ipsyn_return_type(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.ipsyn()
        assert isinstance(result, list)
        assert all(isinstance(v, int) for v in result)

    def test_ipsyn_max_score_bounded(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        result = reader.ipsyn()
        assert 0 <= result[0] <= 112

    def test_ipsyn_noun_phrase_items(self):
        # Test N1 (noun), N4 (det/mod + noun), N5 (article + noun, credits N4)
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\tthe cookie .\n"
            "%mor:\tdet|the n|cookie .\n"
            "%gra:\t1|2|DET 2|0|ROOT 3|2|PUNCT\n"
            "*CHI:\tthe cookie .\n"
            "%mor:\tdet|the n|cookie .\n"
            "%gra:\t1|2|DET 2|0|ROOT 3|2|PUNCT\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        result = reader.ipsyn()[0]
        # N1 (noun) = 2, N5 (det+noun) = 2, N4 (credited by N5) = 2, S1 = 2
        assert result >= 8

    def test_ipsyn_verb_items(self):
        # Test V1 (verb), V7 (progressive -PRESP), V12 (past -PAST)
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\the is running .\n"
            "%mor:\tpro|he cop|be&3S v|run-PRESP .\n"
            "%gra:\t1|2|SUBJ 2|0|ROOT 3|2|PRED 4|2|PUNCT\n"
            "*CHI:\the walked .\n"
            "%mor:\tpro|he v|walk-PAST .\n"
            "%gra:\t1|2|SUBJ 2|0|ROOT 3|2|PUNCT\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        result = reader.ipsyn()[0]
        # V1 (verb) >= 1, V7 (progressive) >= 1, V12 (past) >= 1
        assert result >= 3

    def test_ipsyn_question_items(self):
        # Test Q1 (intonation question), Q9 (why/when/which/whose)
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\tis it good ?\n"
            "%mor:\tcop|be&3S pro|it adj|good ?\n"
            "%gra:\t1|0|ROOT 2|1|SUBJ 3|1|PRED 4|1|PUNCT\n"
            "*CHI:\twhy ?\n"
            "%mor:\tadv:wh|why ?\n"
            "%gra:\t1|0|ROOT 2|1|PUNCT\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        result = reader.ipsyn()[0]
        # Q1 (intonation question) >= 1, Q9 (why) >= 1
        assert result >= 2

    def test_ipsyn_no_mor_no_gra(self):
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\thello world .\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        # No %mor/%gra means tokens are empty, should get 0
        assert reader.ipsyn() == [0]

    def test_ipsyn_svo_scores_s4_and_credits(self):
        # "I want cookie" has S-V-O structure
        # S4 credits S2 and S3; S2 credits S1; S3 credits S1
        chat_str = (
            "@UTF8\n@Begin\n"
            "@Participants:\tCHI Child\n"
            "*CHI:\tI want cookie .\n"
            "%mor:\tpro|I v|want n|cookie .\n"
            "%gra:\t1|2|SUBJ 2|0|ROOT 3|2|OBJ 4|2|PUNCT\n"
            "*CHI:\tI eat food .\n"
            "%mor:\tpro|I v|eat n|food .\n"
            "%gra:\t1|2|SUBJ 2|0|ROOT 3|2|OBJ 4|2|PUNCT\n"
            "@End\n"
        )
        reader = CHAT.from_strs([chat_str])
        result = reader.ipsyn()[0]
        # S4(2) + S2(2) + S3(2) + S1(2) + N1(2) + N2(2) + V1(2) = 14 minimum
        assert result >= 14


# ---------------------------------------------------------------------------
# Tests for mor_tier / gra_tier kwargs
# ---------------------------------------------------------------------------

CHAT_WITH_XMOR = (
    "@UTF8\n"
    "@Begin\n"
    "@Participants:\tCHI Child\n"
    "*CHI:\tI want cookie .\n"
    "%xmor:\tpro|I v|want n|cookie .\n"
    "%xgra:\t1|2|SUBJ 2|0|ROOT 3|2|OBJ 4|2|PUNCT\n"
    "@End\n"
)


class TestMorGraTierKwargs:
    def test_custom_tier_names(self):
        reader = CHAT.from_strs([CHAT_WITH_XMOR], mor_tier="xmor", gra_tier="xgra")
        tokens = reader.tokens()
        assert tokens[0].pos == "pro"
        assert tokens[0].mor == "I"
        assert tokens[0].gra == Gra(dep=1, head=2, rel="SUBJ")

    def test_custom_tiers_default_ignores(self):
        """Default tiers should not pick up %xmor/%xgra data."""
        reader = CHAT.from_strs([CHAT_WITH_XMOR])
        tokens = reader.tokens()
        assert tokens[0].mor is None
        assert tokens[0].gra is None

    def test_none_mor_disables_both(self):
        reader = CHAT.from_strs([BASIC_CHAT], mor_tier=None)
        tokens = reader.tokens()
        assert tokens[0].mor is None
        assert tokens[0].gra is None

    def test_none_gra_disables_both(self):
        reader = CHAT.from_strs([BASIC_CHAT], gra_tier=None)
        tokens = reader.tokens()
        assert tokens[0].mor is None
        assert tokens[0].gra is None

    def test_both_none_disables(self):
        reader = CHAT.from_strs([BASIC_CHAT], mor_tier=None, gra_tier=None)
        tokens = reader.tokens()
        assert tokens[0].mor is None
        assert tokens[0].gra is None

    def test_utterance_tier_names_custom(self):
        reader = CHAT.from_strs([CHAT_WITH_XMOR], mor_tier="xmor", gra_tier="xgra")
        utts = reader.utterances()
        assert utts[0].mor_tier_name == "%xmor"
        assert utts[0].gra_tier_name == "%xgra"

    def test_utterance_tier_names_default(self):
        reader = CHAT.from_strs([BASIC_CHAT])
        utts = reader.utterances()
        assert utts[0].mor_tier_name == "%mor"
        assert utts[0].gra_tier_name == "%gra"

    def test_utterance_tier_names_disabled(self):
        reader = CHAT.from_strs([BASIC_CHAT], mor_tier=None, gra_tier=None)
        utts = reader.utterances()
        assert utts[0].mor_tier_name is None
        assert utts[0].gra_tier_name is None

    def test_to_strs_roundtrip_custom_tiers(self):
        reader = CHAT.from_strs([CHAT_WITH_XMOR], mor_tier="xmor", gra_tier="xgra")
        strs = reader.to_strs()
        assert "%xmor:" in strs[0]
        assert "%xgra:" in strs[0]
        assert "%mor:" not in strs[0]
        assert "%gra:" not in strs[0]