rustledger-parser 0.23.0

Beancount parser with error recovery and full syntax support
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
//! Integration tests for the parser crate.
//!
//! Tests cover all directive types, error recovery, edge cases, and real-world scenarios.

use rustledger_core::Directive;
use rustledger_parser::{ParseError, ParseErrorKind, ParseResult, parse, parse_directives};

// ============================================================================
// Helper Functions
// ============================================================================

fn parse_ok(source: &str) -> ParseResult {
    let result = parse(source);
    assert!(
        result.errors.is_empty(),
        "expected no errors, got: {:?}",
        result.errors
    );
    result
}

fn count_directive_type(result: &ParseResult, type_name: &str) -> usize {
    result
        .directives
        .iter()
        .filter(|d| match &d.value {
            Directive::Open(_) => type_name == "open",
            Directive::Close(_) => type_name == "close",
            Directive::Transaction(_) => type_name == "transaction",
            Directive::Balance(_) => type_name == "balance",
            Directive::Pad(_) => type_name == "pad",
            Directive::Price(_) => type_name == "price",
            Directive::Event(_) => type_name == "event",
            Directive::Note(_) => type_name == "note",
            Directive::Document(_) => type_name == "document",
            Directive::Commodity(_) => type_name == "commodity",
            Directive::Query(_) => type_name == "query",
            Directive::Custom(_) => type_name == "custom",
        })
        .count()
}

// ============================================================================
// Basic Directive Parsing
// ============================================================================

#[test]
fn balance_tolerance_follows_an_arithmetic_amount() {
    // The tolerance was being swallowed INTO the amount expression, so
    // `0.25 + 0.75 ~ 0.01 USD` parsed as the non-expression
    // `0.25 + 0.75 ~ 0.01` and was reported malformed. Only the
    // currency-less arithmetic form hit it: with a currency after the
    // expression the scan stopped there and parsed fine, which is why the
    // plain-number form worked and its arithmetic sibling did not (#2191).
    let forms = [
        ("100.00 USD", dec_str("100.00")),
        ("100.00 ~ 0.01 USD", dec_str("100.00")),
        ("100.00 USD ~ 0.01 USD", dec_str("100.00")),
        ("0.25 + 0.75 USD", dec_str("1.00")),
        ("0.25 + 0.75 ~ 0.01 USD", dec_str("1.00")),
        ("0.25 + 0.75 USD ~ 0.01 USD", dec_str("1.00")),
        ("(0.25 + 0.75) ~ 0.01 USD", dec_str("1.00")),
    ];

    for (form, want) in forms {
        let src = format!("2024-01-15 balance Assets:Cash {form}\n");
        let result = rustledger_parser::parse(&src);
        assert!(
            result.errors.is_empty(),
            "`{form}` must parse: {:?}",
            result.errors,
        );
        let balance = result
            .directives
            .iter()
            .find_map(|d| match &d.value {
                Directive::Balance(b) => Some(b),
                _ => None,
            })
            .unwrap_or_else(|| panic!("`{form}` produced no balance directive"));
        // The VALUE matters more than the parse. An earlier bug in this same
        // scan fell back to "take the first NUMBER", which turned
        // `(1 + 5) / 2.1` into a silent assertion against 1 -- parsed
        // cleanly, asserted the wrong thing.
        assert_eq!(
            balance.amount.number, want,
            "`{form}` evaluated to the wrong amount",
        );
    }
}

#[test]
fn a_price_rejects_a_tolerance_instead_of_discarding_it() {
    // `~` ends the amount expression on a `balance`, which is what makes
    // `0.25 + 0.75 ~ 0.01 USD` parse. Applying that to `price` as well would
    // be a regression, not a fix: a price has no tolerance, so the amount
    // would evaluate and the tolerance would vanish without a word.
    //
    // The plain form was already accepted that way before any of this --
    // `~` is not an arithmetic operator, so the scan took the fast path and
    // fell back to the first NUMBER, yielding 1.10 and dropping the rest.
    // A value the author wrote and the parser discarded in silence is worse
    // than a diagnosed error, so both forms are now diagnosed.
    for src in [
        "2024-01-01 price USD 1.10 ~ 0.01 EUR\n",
        "2024-01-01 price USD 1.10 + 0.05 ~ 0.01 EUR\n",
    ] {
        let result = rustledger_parser::parse(src);
        assert!(
            result.errors.iter().any(|e| matches!(
                &e.kind,
                rustledger_parser::ParseErrorKind::SyntaxError(m)
                    if m.contains("a price has no tolerance")
            )),
            "`{src}` must be diagnosed, not silently truncated: {:?}",
            result.errors,
        );
        assert!(
            !result
                .directives
                .iter()
                .any(|d| matches!(d.value, Directive::Price(_))),
            "`{src}` must not also yield a price directive",
        );
    }

    // The tolerance-free forms still parse, and to the right value.
    for (src, want) in [
        ("2024-01-01 price USD 1.10 EUR\n", dec_str("1.10")),
        ("2024-01-01 price USD 1.10 + 0.05 EUR\n", dec_str("1.15")),
        ("2024-01-01 price USD (1.10 + 0.05) EUR\n", dec_str("1.15")),
    ] {
        let result = rustledger_parser::parse(src);
        assert!(result.errors.is_empty(), "`{src}`: {:?}", result.errors);
        let price = result
            .directives
            .iter()
            .find_map(|d| match &d.value {
                Directive::Price(p) => Some(p),
                _ => None,
            })
            .unwrap_or_else(|| panic!("`{src}` produced no price"));
        assert_eq!(price.amount.number, want, "`{src}`");
    }
}

#[test]
fn every_directive_either_keeps_tags_or_refuses_them() {
    // The failure mode this session kept finding is a third state: accept the
    // tag, then drop it because the model has nowhere to put it. `query` was
    // in it -- absent from both the accepting set and the refusing set --
    // while the refusal message names the rule its omission broke.
    //
    // A census rather than one case, so a directive added later that forgets
    // to pick a side is caught here instead of by a user.
    let refuses = [
        ("open", "2024-01-01 open Assets:A USD"),
        ("close", "2024-01-01 close Assets:A"),
        ("commodity", "2024-01-01 commodity USD"),
        ("event", "2024-01-01 event \"location\" \"NYC\""),
        ("price", "2024-01-01 price USD 1.10 EUR"),
        ("balance", "2024-01-01 balance Assets:A 0 USD"),
        ("pad", "2024-01-01 pad Assets:A Equity:O"),
        // Joined this set once beancount settled it: 3.2.3 answers
        // `2024-01-01 query "n" "SELECT date" #qtag` with `syntax error,
        // unexpected TAG`, and its `Query` namedtuple has no field for one
        // either. It used to accept the tag and discard it (#2194).
        ("query", "2024-01-01 query \"n\" \"SELECT date\""),
    ];
    for (name, header) in refuses {
        for sigil in ["#atag", "^alink"] {
            let src = format!("{header} {sigil}\n");
            let result = rustledger_parser::parse(&src);
            assert!(
                !result.errors.is_empty(),
                "`{name}` accepted {sigil} it has no field to hold",
            );
        }
        // And the tag-free form is still fine -- a rejection that also
        // rejected valid input would pass the assertion above.
        let result = rustledger_parser::parse(&format!("{header}\n"));
        assert!(
            result.errors.is_empty(),
            "`{name}` broke without a tag: {:?}",
            result.errors,
        );
    }

    // The three that DO keep them.
    let keeps = [
        ("note", "2024-01-01 note Assets:A \"n\" #atag ^alink\n"),
        (
            "document",
            "2024-01-01 document Assets:A \"/x.pdf\" #atag ^alink\n",
        ),
        (
            "transaction",
            "2024-01-01 * \"t\" #atag ^alink\n  Assets:A  1 USD\n  Equity:O\n",
        ),
    ];
    for (name, src) in keeps {
        let result = rustledger_parser::parse(src);
        assert!(result.errors.is_empty(), "`{name}`: {:?}", result.errors);
        let (tags, links) = result
            .directives
            .iter()
            .find_map(|d| match &d.value {
                Directive::Note(n) => Some((n.tags.len(), n.links.len())),
                Directive::Document(x) => Some((x.tags.len(), x.links.len())),
                Directive::Transaction(t) => Some((t.tags.len(), t.links.len())),
                _ => None,
            })
            .unwrap_or_else(|| panic!("`{name}` produced no directive"));
        assert_eq!((tags, links), (1, 1), "`{name}` did not keep both");
    }
}

/// The divergence table in the docs must describe what actually ships.
///
/// `docs/reference/compatibility.md` is where a user goes to find out why
/// their file loads here and not in beancount, and it restates the same rows
/// the test below pins. Two copies of one table drift, and the copy that goes
/// stale is the prose one, because nothing runs it.
///
/// So this runs it: every row of the markdown table is parsed and checked
/// against the behavior it claims. It does NOT re-check beancount -- that
/// column is a recorded measurement, not something reproducible without the
/// container -- only our own half, which is the half that can change under
/// the document.
#[test]
fn the_documented_tolerance_table_matches_behavior() {
    let doc = std::fs::read_to_string(
        std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../../docs/reference/compatibility.md"),
    )
    .expect("compatibility.md must be readable from the parser crate");

    let section = doc
        .split("### 6. Balance Tolerance Grammar")
        .nth(1)
        .and_then(|s| s.split("### 7.").next())
        .expect("the tolerance section must exist under that heading");

    let mut rows = 0;
    for line in section.lines() {
        let cells: Vec<&str> = line.split('|').map(str::trim).collect();
        // `| form | beancount | rustledger |` -> ["", form, bc, rl, ""]
        if cells.len() != 5 || !cells[1].starts_with('`') {
            continue;
        }
        let form = cells[1].trim_matches('`');
        let claim = cells[3].replace('*', "");
        let claim = claim.trim();
        let expect_ok = match claim {
            "ok" | "accepted" | "ok (kept)" | "accepted (kept)" => true,
            "diagnosed" => false,
            other => panic!("unrecognized claim {other:?} for `{form}` -- add it here"),
        };

        let src = format!("2024-01-15 balance Assets:C {form}\n");
        let result = rustledger_parser::parse(&src);
        assert_eq!(
            result.errors.is_empty(),
            expect_ok,
            "docs say `{form}` is {claim}, but parsing gave {:?}",
            result.errors,
        );
        rows += 1;
    }

    // A table that stopped being found, or whose format changed, would make
    // every assertion above vacuous.
    assert!(
        rows >= 8,
        "only {rows} rows recognized in the documented table; the format \
         probably changed and this test stopped checking anything",
    );
}

#[test]
fn balance_tolerance_accepts_one_reading_and_diagnoses_none() {
    // beancount's grammar is `NUMBER ~ NUMBER CURRENCY` -- one currency,
    // trailing -- and it rejects any currency before the `~`. We differ in
    // both directions on one rule: accept what has exactly one meaning,
    // diagnose what has none or contradicts itself (#2193).
    //
    // Every row below was checked against beancount 3.2.3; the `bc` column
    // records what it does, so a future reader can see which way each row
    // diverges and why.
    let cases: &[(&str, bool, &str)] = &[
        // form, accepted by us, what beancount does
        ("1.00 USD", true, "ok"),
        ("1.00 ~ 0.01 USD", true, "ok"),
        ("0.25 + 0.75 ~ 0.01 USD", true, "ok"),
        ("(0.25 + 0.75) ~ 0.01 USD", true, "ok"),
        ("1.00 ~ 0.005 + 0.005 USD", true, "ok"),
        ("1.00 ~ 0.005 * 2 USD", true, "ok"),
        // Laxer than beancount ON PURPOSE: the currency is stated twice and
        // agrees, so there is one reading, and it canonicalizes to
        // `1.00 ~ 0.01 USD` losslessly.
        ("1.00 USD ~ 0.01 USD", true, "syntax error"),
        ("0.25 + 0.75 USD ~ 0.01 USD", true, "syntax error"),
        // Stricter than we used to be: these say something unkeepable and
        // were being read in part and discarded in part.
        ("1.00 USD ~ 0.01 EUR", false, "syntax error"),
        ("1.00 ~ 0.001 0.02 USD", false, "syntax error"),
        ("1.00 ~ 0.01 ~ 0.02 USD", false, "syntax error"),
        // A `~` announcing a tolerance that is not there. Accepted as though
        // unwritten, and `rledger format` then deleted the tilde.
        ("1.00 USD ~", false, "syntax error"),
        ("1.00 ~ USD", false, "syntax error"),
    ];

    for (form, accepted, bc) in cases {
        let src = format!("2024-01-15 balance Assets:C {form}\n");
        let result = rustledger_parser::parse(&src);
        assert_eq!(
            result.errors.is_empty(),
            *accepted,
            "`{form}` (beancount: {bc}) -- errors were {:?}",
            result.errors,
        );
    }

    // The accepted redundant form must mean exactly what the canonical one
    // means, or "one reading" is not true.
    let redundant = rustledger_parser::parse("2024-01-15 balance Assets:C 1.00 USD ~ 0.01 USD\n");
    let canonical = rustledger_parser::parse("2024-01-15 balance Assets:C 1.00 ~ 0.01 USD\n");
    let bal = |r: &rustledger_parser::ParseResult| {
        r.directives
            .iter()
            .find_map(|d| match &d.value {
                Directive::Balance(b) => Some((b.amount.clone(), b.tolerance)),
                _ => None,
            })
            .expect("a balance")
    };
    assert_eq!(
        bal(&redundant),
        bal(&canonical),
        "the redundant spelling must parse to the same amount and tolerance",
    );

    // Each rejection must be diagnosed for the RIGHT reason. The second
    // tilde used to fall into the juxtaposed-numbers arm, whose message
    // says the numbers are "side by side" when a `~` sits between them.
    let msg_for = |form: &str| {
        rustledger_parser::parse(&format!("2024-01-15 balance Assets:C {form}\n"))
            .errors
            .iter()
            .find_map(|e| match &e.kind {
                rustledger_parser::ParseErrorKind::SyntaxError(m) => Some(m.clone()),
                _ => None,
            })
            .unwrap_or_default()
    };
    assert!(
        msg_for("1.00 USD ~ 0.01 EUR").contains("same currency as the amount"),
        "currency mismatch must say so: {}",
        msg_for("1.00 USD ~ 0.01 EUR"),
    );
    assert!(
        msg_for("1.00 ~ 0.001 0.02 USD").contains("Two numbers side by side"),
        "juxtaposed numbers must say so: {}",
        msg_for("1.00 ~ 0.001 0.02 USD"),
    );
    assert!(
        msg_for("1.00 USD ~").contains("must be followed by a tolerance"),
        "a dangling tilde must say so: {}",
        msg_for("1.00 USD ~"),
    );
    assert!(
        msg_for("1.00 ~ 0.01 ~ 0.02 USD").contains("one tolerance"),
        "a second tilde must say so, not blame juxtaposed numbers: {}",
        msg_for("1.00 ~ 0.01 ~ 0.02 USD"),
    );

    // A malformed tolerance gets the diagnostic that explains it, and not
    // ours on top. `.005` is a number beancount accepts and our lexer does
    // not (filed separately); on that input the region is not
    // expression-shaped, so the juxtaposed-numbers check stays quiet rather
    // than claiming a second number "was being discarded" from something that
    // never parsed.
    let dotted = rustledger_parser::parse("2024-01-15 balance Assets:C 1.00 ~ .005 + .005 USD\n");
    assert!(
        !dotted.errors.is_empty(),
        "fixture must still be rejected for the real reason",
    );
    assert!(
        !dotted.errors.iter().any(|e| matches!(
            &e.kind,
            rustledger_parser::ParseErrorKind::SyntaxError(m) if m.contains("Two numbers side by side")
        )),
        "must not add a tolerance complaint to input the lexer already refused: {:?}",
        dotted.errors,
    );

    // And a tolerance that is arithmetic still evaluates, so the new check
    // does not catch the multi-number case it is supposed to allow.
    let arith = rustledger_parser::parse("2024-01-15 balance Assets:C 1.00 ~ 0.005 + 0.005 USD\n");
    assert_eq!(
        bal(&arith).1,
        Some(dec_str("0.010")),
        "`~ 0.005 + 0.005` must evaluate, not be rejected as two numbers",
    );
}

fn dec_str(s: &str) -> rustledger_core::Decimal {
    s.parse().expect("test decimal")
}

#[test]
fn test_parse_open_directive() {
    let source = r"2024-01-01 open Assets:Bank:Checking USD, EUR";
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "open"), 1);

    if let Directive::Open(open) = &result.directives[0].value {
        assert_eq!(open.account, "Assets:Bank:Checking");
        assert_eq!(open.currencies, vec!["USD", "EUR"]);
    } else {
        panic!("expected open directive");
    }
}

#[test]
fn test_parse_close_directive() {
    let source = r"2024-12-31 close Assets:Bank:OldAccount";
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "close"), 1);

    if let Directive::Close(close) = &result.directives[0].value {
        assert_eq!(close.account, "Assets:Bank:OldAccount");
    } else {
        panic!("expected close directive");
    }
}

#[test]
fn test_parse_simple_transaction() {
    let source = r#"
2024-01-15 * "Coffee Shop" "Morning coffee"
  Expenses:Food:Coffee  5.00 USD
  Assets:Cash
"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "transaction"), 1);

    if let Directive::Transaction(txn) = &result.directives[0].value {
        assert_eq!(txn.payee.as_deref(), Some("Coffee Shop"));
        assert_eq!(txn.narration.as_str(), "Morning coffee");
        assert_eq!(txn.postings.len(), 2);
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn string_escapes_are_decoded() {
    // The semantic value strips quotes AND decodes escapes (Beancount):
    // \" -> ", \\ -> \, \t -> tab, unknown \x -> x.
    let source = "2024-01-15 * \"q=\\\"hi\\\" bs=\\\\ tab=\\t x=\\x\"\n  \
                  Assets:Cash  -5.00 USD\n  Expenses:X  5.00 USD\n";
    let result = parse_ok(source);
    if let Directive::Transaction(txn) = &result.directives[0].value {
        assert_eq!(txn.narration.as_str(), "q=\"hi\" bs=\\ tab=\t x=x");
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn test_parse_transaction_with_tags_and_links() {
    let source = r#"
2024-01-15 * "Dinner" #food #restaurant ^receipt-123
  Expenses:Food  45.00 USD
  Assets:Cash
"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "transaction"), 1);

    if let Directive::Transaction(txn) = &result.directives[0].value {
        assert!(txn.tags.iter().any(|t| t.as_str() == "food"));
        assert!(txn.tags.iter().any(|t| t.as_str() == "restaurant"));
        assert!(txn.links.iter().any(|l| l.as_str() == "receipt-123"));
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn test_parse_balance_directive() {
    let source = r"2024-01-31 balance Assets:Bank:Checking 1000.00 USD";
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "balance"), 1);

    if let Directive::Balance(bal) = &result.directives[0].value {
        assert_eq!(bal.account, "Assets:Bank:Checking");
        assert_eq!(bal.amount.number.to_string(), "1000.00");
        assert_eq!(bal.amount.currency, "USD");
    } else {
        panic!("expected balance");
    }
}

#[test]
fn test_parse_pad_directive() {
    let source = r"2024-01-01 pad Assets:Bank:Checking Equity:Opening-Balances";
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "pad"), 1);

    if let Directive::Pad(pad) = &result.directives[0].value {
        assert_eq!(pad.account, "Assets:Bank:Checking");
        assert_eq!(pad.source_account, "Equity:Opening-Balances");
    } else {
        panic!("expected pad");
    }
}

#[test]
fn test_parse_price_directive() {
    let source = r"2024-01-15 price AAPL 185.50 USD";
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "price"), 1);

    if let Directive::Price(price) = &result.directives[0].value {
        assert_eq!(price.currency, "AAPL");
        assert_eq!(price.amount.number.to_string(), "185.50");
        assert_eq!(price.amount.currency, "USD");
    } else {
        panic!("expected price");
    }
}

#[test]
fn test_parse_event_directive() {
    let source = r#"2024-01-01 event "location" "New York""#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "event"), 1);

    if let Directive::Event(event) = &result.directives[0].value {
        assert_eq!(event.event_type, "location");
        assert_eq!(event.value, "New York");
    } else {
        panic!("expected event");
    }
}

#[test]
fn test_parse_note_directive() {
    let source = r#"2024-01-15 note Assets:Bank:Checking "Account review completed""#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "note"), 1);

    if let Directive::Note(note) = &result.directives[0].value {
        assert_eq!(note.account, "Assets:Bank:Checking");
        assert_eq!(note.comment, "Account review completed");
    } else {
        panic!("expected note");
    }
}

#[test]
fn test_parse_document_directive() {
    let source = r#"2024-01-15 document Assets:Bank:Checking "/path/to/statement.pdf""#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "document"), 1);

    if let Directive::Document(doc) = &result.directives[0].value {
        assert_eq!(doc.account, "Assets:Bank:Checking");
        assert_eq!(doc.path, "/path/to/statement.pdf");
    } else {
        panic!("expected document");
    }
}

#[test]
fn test_parse_commodity_directive() {
    let source = r#"2024-01-01 commodity USD
  name: "US Dollar""#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "commodity"), 1);

    if let Directive::Commodity(comm) = &result.directives[0].value {
        assert_eq!(comm.currency, "USD");
    } else {
        panic!("expected commodity");
    }
}

#[test]
fn test_commodity_precision_metadata_round_trips() {
    // Issue #991: per-commodity `precision: N` metadata must round-trip
    // through parse → format → parse without changing value or type.
    use rustledger_core::{FormatConfig, MetaValue, format_directives};

    let source = "2024-01-01 commodity USD\n  precision: 2\n";
    let parsed = parse_ok(source);
    let Directive::Commodity(comm) = &parsed.directives[0].value else {
        panic!("expected commodity");
    };
    // First parse: an unquoted integer literal is `Int(2)`, not a Number/string.
    assert_eq!(
        comm.meta.get("precision"),
        Some(&MetaValue::Int(2)),
        "parser must produce Int(2) for unquoted integer metadata"
    );

    // Format the directive and re-parse — the value must survive unchanged.
    let formatted = format_directives([&parsed.directives[0].value], &FormatConfig::default());
    let reparsed = parse_ok(&formatted);
    let Directive::Commodity(comm2) = &reparsed.directives[0].value else {
        panic!("expected commodity after re-parse");
    };
    assert_eq!(
        comm2.meta.get("precision"),
        Some(&MetaValue::Int(2)),
        "round-tripped precision must remain Int(2); got formatted: {formatted:?}"
    );
}

#[test]
fn test_parse_query_directive() {
    let source = r#"2024-01-01 query "expenses" "SELECT account, SUM(position)""#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "query"), 1);

    if let Directive::Query(q) = &result.directives[0].value {
        assert_eq!(q.name, "expenses");
        assert!(q.query.contains("SELECT"));
    } else {
        panic!("expected query");
    }
}

#[test]
fn test_parse_custom_directive() {
    let source = r#"2024-01-01 custom "budget" Expenses:Food 500.00 USD"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "custom"), 1);
}

/// Regression: custom directive values must preserve a leading `MINUS` sign.
/// `extract_custom_values` previously had no `MINUS` arm, so `-50.00` emitted
/// `+50.00`. All three value-token extractors share `value_tokens_to_meta`.
///
/// The tag/link half of this test was REMOVED with #1958. It asserted that a
/// custom directive carries `Tag`/`Link` values, which beancount does not
/// accept at all — `custom "budget" -50.00 USD #quarterly ...` is a
/// `ParserSyntaxError: unexpected TAG` there and produces no directive.
/// Checked against beancount before the expectation was changed. The sign
/// claim is untouched and is what this test is actually for; rejection is
/// covered by `custom_and_pushmeta_reject_taglinks_by_their_own_rules`.
#[test]
fn test_custom_directive_preserves_sign_and_tag_link() {
    use rust_decimal_macros::dec;
    use rustledger_core::{Amount, Currency, MetaValue};

    let source = r#"2024-01-01 custom "budget" -50.00 USD TRUE"#;
    let result = parse_ok(source);
    let spanned = result
        .directives
        .iter()
        .find(|d| matches!(d.value, Directive::Custom(_)))
        .expect("expected a custom directive");
    let Directive::Custom(custom) = &spanned.value else {
        unreachable!()
    };

    assert_eq!(
        custom.values,
        vec![
            // Signed amount keeps its sign (was emitted as +50.00).
            MetaValue::Amount(Amount::new(dec!(-50.00), Currency::new("USD"))),
            MetaValue::Bool(true),
        ],
        "custom values: {:?}",
        custom.values
    );
}

/// Regression: a `pushmeta` value of `NUMBER CURRENCY` must parse as an
/// `Amount`. Before the three value-token walks were unified through
/// `value_tokens_to_meta`, `pushmeta_value` returned on the `NUMBER` token and
/// dropped the currency (`5 USD` became `Number(5)`).
#[test]
fn test_pushmeta_value_with_currency_is_amount() {
    use rust_decimal_macros::dec;
    use rustledger_core::{Amount, Currency, MetaValue};

    let source = r"
pushmeta budget: 5 USD
2024-01-01 open Assets:Cash USD
popmeta budget:
";
    let result = parse_ok(source);
    let open = result
        .directives
        .iter()
        .find_map(|d| match &d.value {
            Directive::Open(o) => Some(o),
            _ => None,
        })
        .expect("expected an open directive");
    assert_eq!(
        open.meta.get("budget"),
        Some(&MetaValue::Amount(Amount::new(
            dec!(5),
            Currency::new("USD")
        ))),
        "pushmeta budget should be Amount(5 USD); meta: {:?}",
        open.meta
    );
}

// ============================================================================
// Options, Includes, and Plugins
// ============================================================================

#[test]
fn test_parse_options() {
    let source = r#"
option "title" "My Ledger"
option "operating_currency" "USD"
option "operating_currency" "EUR"
"#;
    let result = parse_ok(source);
    assert_eq!(result.options.len(), 3);
    assert_eq!(result.options[0].0, "title");
    assert_eq!(result.options[0].1, "My Ledger");
}

#[test]
fn test_parse_includes() {
    let source = r#"
include "accounts.beancount"
include "transactions/2024.beancount"
"#;
    let result = parse_ok(source);
    assert_eq!(result.includes.len(), 2);
    assert_eq!(result.includes[0].0, "accounts.beancount");
    assert_eq!(result.includes[1].0, "transactions/2024.beancount");
}

#[test]
fn test_parse_plugins() {
    let source = r#"
plugin "beancount.plugins.leafonly"
plugin "beancount.plugins.check_commodity" "config_string"
"#;
    let result = parse_ok(source);
    assert_eq!(result.plugins.len(), 2);
    assert_eq!(result.plugins[0].0, "beancount.plugins.leafonly");
    assert!(result.plugins[0].1.is_none());
    assert_eq!(result.plugins[1].0, "beancount.plugins.check_commodity");
    assert_eq!(result.plugins[1].1, Some("config_string".to_string()));
}

// ============================================================================
// Complex Transactions
// ============================================================================

#[test]
fn test_parse_transaction_with_cost() {
    let source = r#"
2024-01-15 * "Buy stock"
  Assets:Brokerage  10 AAPL {185.50 USD}
  Assets:Cash  -1855.00 USD
"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "transaction"), 1);

    if let Directive::Transaction(txn) = &result.directives[0].value {
        let posting = &txn.postings[0];
        assert!(posting.cost.is_some());
        let cost = posting.cost.as_deref().unwrap();
        assert_eq!(
            cost.number.unwrap().per_unit().unwrap().to_string(),
            "185.50"
        );
        assert_eq!(cost.currency.as_deref(), Some("USD"));
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn test_parse_transaction_with_price() {
    let source = r#"
2024-01-15 * "Currency exchange"
  Assets:USD  100.00 USD @ 0.85 EUR
  Assets:EUR  -85.00 EUR
"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "transaction"), 1);

    if let Directive::Transaction(txn) = &result.directives[0].value {
        let posting = &txn.postings[0];
        assert!(posting.price.is_some());
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn test_parse_transaction_with_total_cost() {
    let source = r#"
2024-01-15 * "Buy stock with fees"
  Assets:Brokerage  10 AAPL {{1860.00 USD}}
  Assets:Cash  -1860.00 USD
"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "transaction"), 1);

    if let Directive::Transaction(txn) = &result.directives[0].value {
        let posting = &txn.postings[0];
        assert!(posting.cost.is_some());
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn test_parse_transaction_with_metadata() {
    let source = r#"
2024-01-15 * "Purchase"
  receipt: "scan-001.pdf"
  category: "office"
  Expenses:Office  100.00 USD
    item: "Printer paper"
  Assets:Cash
"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "transaction"), 1);

    if let Directive::Transaction(txn) = &result.directives[0].value {
        assert!(txn.meta.contains_key("receipt"));
        assert!(txn.meta.contains_key("category"));
        assert!(txn.postings[0].meta.contains_key("item"));
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn test_parse_boolean_metadata() {
    let source = r#"
2024-01-15 * "Test"
  recurring: TRUE
  active: FALSE
  enabled: True
  disabled: False
  Expenses:Test  100.00 USD
  Assets:Cash
"#;
    let result = parse_ok(source);

    if let Directive::Transaction(txn) = &result.directives[0].value {
        use rustledger_core::MetaValue;
        assert_eq!(txn.meta.get("recurring"), Some(&MetaValue::Bool(true)));
        assert_eq!(txn.meta.get("active"), Some(&MetaValue::Bool(false)));
        assert_eq!(txn.meta.get("enabled"), Some(&MetaValue::Bool(true)));
        assert_eq!(txn.meta.get("disabled"), Some(&MetaValue::Bool(false)));
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn test_parse_extended_transaction_flags() {
    // Test all extended flags parse correctly
    for (flag, expected) in [
        ("P", 'P'), // Pad-generated
        ("S", 'S'), // Summarization
        ("T", 'T'), // Transfer
        ("C", 'C'), // Conversion
        ("U", 'U'), // Unrealized
        ("R", 'R'), // Return
        ("M", 'M'), // Merge
        ("#", '#'), // Bookmarked
        ("?", '?'), // Needs investigation
    ] {
        let source = format!(
            r#"
2024-01-15 {flag} "Test transaction"
  Expenses:Test  100 USD
  Assets:Cash
"#
        );
        let result = parse_ok(&source);
        if let Directive::Transaction(txn) = &result.directives[0].value {
            assert_eq!(
                txn.flag, expected,
                "Flag {flag} should parse as '{expected}'"
            );
        } else {
            panic!("expected transaction for flag {flag}");
        }
    }
}

// ============================================================================
// Error Recovery
// ============================================================================

#[test]
fn test_error_recovery_continues_parsing() {
    let source = r"
2024-01-01 open Assets:Bank

; This line has an error
2024-01-15 invalid directive here

2024-01-31 close Assets:Bank
";
    let result = parse(source);

    // Should have errors
    assert!(!result.errors.is_empty(), "expected parse errors");

    // But should still have parsed valid directives
    assert!(
        count_directive_type(&result, "open") >= 1,
        "should have parsed open directive"
    );
}

#[test]
fn test_error_on_invalid_date() {
    let source = r"2024-13-45 open Assets:Bank";
    let result = parse(source);
    assert!(!result.errors.is_empty(), "expected error for invalid date");
}

#[test]
fn test_parse_single_digit_month() {
    // Beancount accepts YYYY-M-DD (single-digit month)
    let source = "2024-1-15 open Assets:Checking\n";
    let result = parse(source);
    assert!(
        result.errors.is_empty(),
        "unexpected errors for single-digit month: {:?}",
        result.errors
    );
    assert_eq!(count_directive_type(&result, "open"), 1);
    if let Directive::Open(open) = &result.directives[0].value {
        assert_eq!(open.date, rustledger_core::naive_date(2024, 1, 15).unwrap());
    } else {
        panic!("expected open directive");
    }
}

#[test]
fn test_parse_single_digit_day() {
    // Beancount accepts YYYY-MM-D (single-digit day)
    let source = "2024-01-5 open Assets:Cash USD\n";
    let result = parse(source);
    assert!(
        result.errors.is_empty(),
        "unexpected errors for single-digit day: {:?}",
        result.errors
    );
    assert_eq!(count_directive_type(&result, "open"), 1);
}

#[test]
fn test_parse_single_digit_month_and_day() {
    // Beancount accepts YYYY-M-D (single-digit month and day)
    let source = "2024-1-1 open Assets:Cash USD\n";
    let result = parse(source);
    assert!(
        result.errors.is_empty(),
        "unexpected errors for single-digit month/day: {:?}",
        result.errors
    );
    assert_eq!(count_directive_type(&result, "open"), 1);
    if let Directive::Open(open) = &result.directives[0].value {
        assert_eq!(open.date, rustledger_core::naive_date(2024, 1, 1).unwrap());
    } else {
        panic!("expected open directive");
    }
}

#[test]
fn test_error_invalid_leap_year_date() {
    // Feb 29 in a non-leap year should produce a descriptive error
    let source = "2023-02-29 open Assets:Cash USD\n";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected error for invalid leap-year date"
    );
    let err = &result.errors[0];
    assert!(
        matches!(err.kind, ParseErrorKind::InvalidDateValue(_)),
        "expected InvalidDateValue error kind, got: {:?}",
        err.kind
    );
    let msg = err.message();
    assert!(
        msg.contains("day") && msg.contains("out of range"),
        "expected error mentioning 'day' and 'out of range', got: '{msg}'"
    );
    assert!(
        msg.contains("2023-02"),
        "expected error mentioning '2023-02', got: '{msg}'"
    );
}

#[test]
fn test_error_invalid_date_month_out_of_range() {
    // Month 13 should produce a descriptive error
    let source = "2024-13-01 open Assets:Cash USD\n";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected error for month out of range"
    );
    let err = &result.errors[0];
    assert!(
        matches!(err.kind, ParseErrorKind::InvalidDateValue(_)),
        "expected InvalidDateValue error kind, got: {:?}",
        err.kind
    );
    let msg = err.message();
    assert!(
        msg.contains("month") && msg.contains("out of range"),
        "expected error mentioning 'month' and 'out of range', got: '{msg}'"
    );
}

#[test]
fn test_error_on_invalid_account() {
    let source = r"2024-01-01 open lowercase:invalid";
    let result = parse(source);
    // Account names must start with a capital letter
    assert!(
        !result.errors.is_empty(),
        "expected error for invalid account"
    );
}

// ============================================================================
// Edge Cases
// ============================================================================

#[test]
fn test_parse_empty_input() {
    let result = parse("");
    assert!(result.errors.is_empty());
    assert!(result.directives.is_empty());
}

#[test]
fn test_parse_only_comments() {
    let source = r"
; This is a comment
; Another comment
";
    let result = parse_ok(source);
    assert!(result.directives.is_empty());
    // Verify comments are captured
    assert_eq!(result.comments.len(), 2);
    assert!(result.comments[0].value.contains("This is a comment"));
    assert!(result.comments[1].value.contains("Another comment"));
}

#[test]
fn test_parse_comments_with_directives() {
    let source = r#"
; Header comment
option "operating_currency" "USD"

; Section comment
2024-01-01 open Assets:Bank USD
  description: "Main account"

; Footer comment
"#;
    let result = parse_ok(source);

    // Should have 1 directive (open)
    assert_eq!(result.directives.len(), 1);

    // Should have 1 option
    assert_eq!(result.options.len(), 1);

    // Should have 3 comments
    assert_eq!(result.comments.len(), 3);
    assert!(result.comments[0].value.contains("Header comment"));
    assert!(result.comments[1].value.contains("Section comment"));
    assert!(result.comments[2].value.contains("Footer comment"));
}

#[test]
fn test_parse_unicode_in_narration() {
    let source = r#"2024-01-15 * "Café ☕" "Latte mit Milch"
  Expenses:Food  5.00 EUR
  Assets:Cash"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "transaction"), 1);

    if let Directive::Transaction(txn) = &result.directives[0].value {
        assert_eq!(txn.payee.as_deref(), Some("Café ☕"));
        assert_eq!(txn.narration.as_str(), "Latte mit Milch");
    } else {
        panic!("expected transaction");
    }
}

#[test]
fn test_parse_negative_amounts() {
    let source = r#"
2024-01-15 * "Refund"
  Assets:Bank  -50.00 USD
  Expenses:Food
"#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "transaction"), 1);
}

#[test]
fn test_parse_large_numbers() {
    let source = r"2024-01-15 price BTC 15000.00 USD";
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "price"), 1);
}

#[test]
fn test_parse_booking_method() {
    let source = r#"2024-01-01 open Assets:Stock "FIFO""#;
    let result = parse_ok(source);
    assert_eq!(count_directive_type(&result, "open"), 1);

    if let Directive::Open(open) = &result.directives[0].value {
        assert_eq!(open.booking, Some("FIFO".to_string()));
    } else {
        panic!("expected open");
    }
}

// ============================================================================
// Real-World Scenarios
// ============================================================================

#[test]
fn test_parse_complete_ledger() {
    let source = r#"
; Main ledger file
option "title" "Personal Finance"
option "operating_currency" "USD"

plugin "beancount.plugins.auto_accounts"

2024-01-01 open Assets:Bank:Checking USD
2024-01-01 open Assets:Bank:Savings USD
2024-01-01 open Expenses:Food
2024-01-01 open Expenses:Transport
2024-01-01 open Income:Salary

2024-01-01 pad Assets:Bank:Checking Equity:Opening-Balances

2024-01-15 * "Employer" "Monthly salary"
  Income:Salary  -5000.00 USD
  Assets:Bank:Checking  5000.00 USD

2024-01-16 * "Grocery Store" "Weekly groceries" #food
  Expenses:Food  150.00 USD
  Assets:Bank:Checking

2024-01-17 * "Gas Station" "Fill up"
  Expenses:Transport  45.00 USD
  Assets:Bank:Checking

2024-01-31 balance Assets:Bank:Checking 4805.00 USD

2024-01-31 note Assets:Bank:Checking "Reconciled with bank statement"
"#;
    let result = parse_ok(source);

    assert_eq!(result.options.len(), 2);
    assert_eq!(result.plugins.len(), 1);
    assert_eq!(count_directive_type(&result, "open"), 5);
    assert_eq!(count_directive_type(&result, "pad"), 1);
    assert_eq!(count_directive_type(&result, "transaction"), 3);
    assert_eq!(count_directive_type(&result, "balance"), 1);
    assert_eq!(count_directive_type(&result, "note"), 1);
}

#[test]
fn test_parse_investment_ledger() {
    let source = r#"
2024-01-01 open Assets:Brokerage AAPL, GOOG, USD
2024-01-01 open Income:Dividends
2024-01-01 open Income:Capital-Gains

2024-01-01 commodity AAPL
  name: "Apple Inc."

2024-01-15 * "Buy Apple stock"
  Assets:Brokerage  10 AAPL {185.00 USD, 2024-01-15}
  Assets:Brokerage  -1850.00 USD

2024-02-15 * "Receive dividend"
  Assets:Brokerage  5.00 USD
  Income:Dividends  -5.00 USD

2024-03-15 price AAPL 190.00 USD

2024-04-15 * "Sell Apple stock"
  Assets:Brokerage  -5 AAPL {185.00 USD, 2024-01-15}
  Assets:Brokerage  950.00 USD
  Income:Capital-Gains  -25.00 USD
"#;
    let result = parse_ok(source);

    assert_eq!(count_directive_type(&result, "open"), 3);
    assert_eq!(count_directive_type(&result, "commodity"), 1);
    assert_eq!(count_directive_type(&result, "transaction"), 3);
    assert_eq!(count_directive_type(&result, "price"), 1);
}

// ============================================================================
// parse_directives API
// ============================================================================

#[test]
fn test_parse_directives_simple() {
    let source = r#"
option "title" "Test"
2024-01-01 open Assets:Bank
"#;
    let (directives, errors) = parse_directives(source);
    assert!(errors.is_empty());
    assert_eq!(directives.len(), 1);
}

// ============================================================================
// Conformance: invalid inputs that must be rejected (pta-standards suite)
// ============================================================================

/// Case: invalid-leading-decimal
/// Amounts must have an integer part before the decimal point (.50 is invalid).
/// Valid amounts like 0.50 must still be accepted.
#[test]
fn test_reject_leading_decimal() {
    let source = "2024-01-15 * \"Test\"\n  Expenses:Food  .50 USD\n  Assets:Checking\n";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected parse error for leading decimal amount '.50 USD'"
    );
}

/// Positive counterpart: amounts with an integer part must still be accepted.
#[test]
fn test_accept_decimal_with_integer_part() {
    let source = "2024-01-15 * \"Test\"\n  Expenses:Food  0.50 USD\n  Assets:Checking\n";
    let result = parse(source);
    assert!(
        result.errors.is_empty(),
        "valid amount '0.50 USD' should parse without errors, errors: {:?}",
        result.errors
    );
}

/// Case: invalid-booking-method-lowercase / booking-method-case-sensitive
/// Booking methods must be uppercase (FIFO, STRICT, `STRICT_WITH_SIZE`, LIFO, HIFO, NONE, AVERAGE).
/// Lowercase variants like "fifo" must be rejected.
#[test]
fn test_reject_lowercase_booking_method() {
    let source = "2024-01-01 open Assets:Stock AAPL \"fifo\"\n";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected parse error for lowercase booking method 'fifo'"
    );
}

/// Counterpart: uppercase booking method must still be accepted.
#[test]
fn test_accept_uppercase_booking_method() {
    let source = "2024-01-01 open Assets:Stock AAPL \"FIFO\"\n";
    let result = parse(source);
    assert!(
        result.errors.is_empty(),
        "uppercase booking method 'FIFO' should be valid, errors: {:?}",
        result.errors
    );
}

/// `STRICT_WITH_SIZE` booking method must be accepted on open directives.
#[test]
fn test_accept_strict_with_size_booking_method() {
    let source = "2024-01-01 open Assets:Stock AAPL \"STRICT_WITH_SIZE\"\n";
    let result = parse(source);
    assert!(
        result.errors.is_empty(),
        "booking method 'STRICT_WITH_SIZE' should be valid, errors: {:?}",
        result.errors
    );

    if let Directive::Open(open) = &result.directives[0].value {
        assert_eq!(open.booking, Some("STRICT_WITH_SIZE".to_string()));
    } else {
        panic!("expected open directive");
    }
}

/// Invalid booking method error message should include `STRICT_WITH_SIZE` in the valid list.
#[test]
fn test_invalid_booking_method_error_includes_strict_with_size() {
    let source = "2024-01-01 open Assets:Stock AAPL \"invalid_method\"\n";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected parse error for invalid booking method"
    );
    let error_msg = result.errors[0].message();
    assert!(
        error_msg.contains("STRICT_WITH_SIZE"),
        "error message should list STRICT_WITH_SIZE as a valid method, got: {error_msg}"
    );
}

/// Case: invalid-metadata-uppercase-key
/// Metadata keys must start with a lowercase ASCII letter.
/// Keys starting with uppercase (e.g. "Category:") must be rejected.
#[test]
fn test_reject_uppercase_metadata_key() {
    let source =
        "2024-01-15 * \"Test\"\n  Category: \"test\"\n  Expenses:Food  50 USD\n  Assets:Checking\n";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected parse error for metadata key starting with uppercase 'Category:'"
    );
}

/// Case: invalid-balance-no-amount
/// Balance directives require both an account and an amount+currency.
#[test]
fn test_reject_balance_without_amount() {
    let source = "2024-01-15 balance Assets:Checking\n";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected parse error for balance directive without amount"
    );
}

/// Case: invalid-pad-no-source
/// Pad directives require both a target account and a source account.
#[test]
fn test_reject_pad_without_source_account() {
    let source = "2024-01-15 pad Assets:Checking\n";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected parse error for pad directive without source account"
    );
}

/// Case: unicode-account-name
/// Unicode letters (CJK, Cyrillic, etc.) are valid in account names.
/// This extends beyond the beancount v3 spec's ASCII restriction, which
/// was an artifact of the C flex lexer's poor Unicode support.
#[test]
fn test_accept_unicode_account_name() {
    let source = "2024-01-01 open Assets:銀行口座\n";
    let result = parse(source);
    assert!(
        result.errors.is_empty(),
        "Unicode account names should parse successfully, got: {:?}",
        result
            .errors
            .iter()
            .map(rustledger_parser::ParseError::message)
            .collect::<Vec<_>>()
    );
}

/// Case: invalid-cost-unclosed (issue #736)
/// A cost specification must be closed with `}` on the same logical line
/// as the opening `{`. Hitting a newline before the closing brace is a
/// parse error — the parser must not silently consume tokens on following
/// posting lines looking for a close brace.
#[test]
fn test_reject_unclosed_cost_brace() {
    let source = "\
2024-01-01 open Assets:Stock
2024-01-01 open Assets:Cash USD

2024-01-15 *
  Assets:Stock 10 AAPL {150 USD
  Assets:Cash -1500 USD
";
    let result = parse(source);
    assert!(
        result
            .errors
            .iter()
            .any(|e| e.message().contains("unclosed cost")),
        "expected 'unclosed cost' parse error, got: {:?}",
        result
            .errors
            .iter()
            .map(ParseError::message)
            .collect::<Vec<_>>()
    );
}

/// Regression: an incomplete final directive at EOF (no trailing newline
/// and no account name) must produce a parse error, not be silently
/// dropped by the top-level error-recovery loop. Guards against a Copilot
/// review finding from PR #740 where an overly-eager early-break on an
/// empty stream could mask real EOF syntax errors.
#[test]
fn test_reject_incomplete_final_directive_at_eof() {
    let source = "2024-01-01 open";
    let result = parse(source);
    assert!(
        !result.errors.is_empty(),
        "expected parse error for incomplete open directive at EOF, got: {:?}",
        result
            .errors
            .iter()
            .map(ParseError::message)
            .collect::<Vec<_>>()
    );
}

/// `{N # T CCY}` is beancount's `compound_amount`: per-unit `N` AND lump
/// total `T`; the cost totals `units*N + T`. An earlier version of this
/// test pinned the form to `Total{T}` — itself a partial fix (the parser
/// once dropped `T` entirely) that baked in dropping `N` instead; #1700
/// corrected the parse to carry both components as written.
#[test]
fn test_cost_spec_n_hash_t_parses_compound() {
    use rust_decimal_macros::dec;
    use rustledger_core::CostNumber;

    let source = "
2024-01-01 open Assets:Stock
2024-01-01 open Assets:Cash USD

2024-01-15 *
  Assets:Stock  10 STK {50 # 1500 USD}
  Assets:Cash  -1500.00 USD
";
    let result = parse_ok(source);
    let txn = result
        .directives
        .iter()
        .find_map(|d| match &d.value {
            Directive::Transaction(t) => Some(t),
            _ => None,
        })
        .expect("transaction present");
    let cost = txn.postings[0]
        .value
        .cost
        .as_ref()
        .expect("cost spec present");
    assert_eq!(
        cost.number,
        Some(CostNumber::Compound {
            per_unit: dec!(50),
            total: dec!(1500)
        }),
        "the `#` form must carry BOTH components as written (#1700): \
         beancount's compound_amount weighs N*per_unit + total"
    );
    assert_eq!(
        cost.currency
            .as_ref()
            .map(rustledger_core::Currency::as_str),
        Some("USD"),
        "currency must still be captured after the `# T` clause"
    );
}

/// Regression: an unclosed cost brace followed by EOF (no trailing newline)
/// should also produce a parse error, not silently drop the cost.
#[test]
fn test_reject_unclosed_cost_brace_at_eof() {
    let source = "\
2024-01-01 open Assets:Stock
2024-01-01 open Assets:Cash USD

2024-01-15 *
  Assets:Stock 10 AAPL {150 USD";
    let result = parse(source);
    assert!(
        result
            .errors
            .iter()
            .any(|e| e.message().contains("unclosed cost")),
        "expected 'unclosed cost' parse error at EOF, got: {:?}",
        result
            .errors
            .iter()
            .map(ParseError::message)
            .collect::<Vec<_>>()
    );
}

/// `parse_without_occurrences` skips the LSP-only occurrence indices but is
/// otherwise identical to `parse` (the processing path's view). Pins the
/// optimization that drops ~40k per-token allocations on the load path.
#[test]
fn parse_without_occurrences_skips_indices_but_matches_directives() {
    let src = "2020-01-01 open Assets:Cash USD\n\
               2020-02-01 * \"p\" \"m\"\n  Assets:Cash 5.00 USD\n  Income:Salary\n";
    let full = rustledger_parser::parse(src);
    let lean = rustledger_parser::parse_without_occurrences(src);
    // Full parse collects occurrences; lean parse does not.
    assert!(
        !full.account_occurrences.is_empty() && !full.currency_occurrences.is_empty(),
        "full parse should collect occurrences"
    );
    assert!(
        lean.account_occurrences.is_empty() && lean.currency_occurrences.is_empty(),
        "lean parse must skip occurrence collection"
    );
    // Everything the processing pipeline consumes must be byte-for-byte
    // identical — only the occurrence indices differ. Compare full contents
    // (via Debug), not just counts, so a content regression that preserves
    // lengths can't slip through.
    assert_eq!(
        format!("{:?}", full.directives),
        format!("{:?}", lean.directives),
        "directives must be identical"
    );
    assert_eq!(
        format!("{:?}", full.errors),
        format!("{:?}", lean.errors),
        "errors must be identical"
    );
    assert_eq!(
        format!("{:?}", full.options),
        format!("{:?}", lean.options),
        "options must be identical"
    );
}

/// #1930: any NON-ASCII character is valid inside an account-name component.
///
/// Found by the beancount oracle's error axis: `Assets:CORP✨` is a committed
/// fixture in fava-portfolio-returns that beancount loads and rledger rejected
/// with P0012. Probing beancount showed its real rule is broader than "Unicode
/// letters" — symbols (`So`), arrows, and `No` digits all pass — so restricting
/// to `\p{L}` meant refusing files that exist.
///
/// The ASCII cases are the point of the test, not padding: they pin the
/// boundary that makes the widening safe. Every character with syntactic
/// meaning in beancount is ASCII, so an account name still cannot swallow a
/// price annotation or a cost brace.
#[test]
fn account_names_accept_any_non_ascii_but_no_ascii_punctuation() {
    for name in [
        "Assets:CORP✨", // So — the reported case
        "Assets:CORP½",  // No
        "Assets:CORP→",  // Sm
        "Assets:CORPé",  // L, already worked
        "Assets:CORP、", // ideographic punctuation
    ] {
        assert!(
            rustledger_parser::is_valid_account_name(name),
            "{name} must be accepted (beancount accepts it)",
        );
    }
    // Unicode whitespace and separators ARE accepted, by both tools. Pinned
    // so the sharp edge is visible in the suite and not only in a doc
    // comment: `Assets:A\u{a0}B` is ONE account, visually identical to
    // `Assets:A B`. Verified against beancount individually — excluding them
    // would reject files beancount loads, which is the bug #1930 fixes.
    for name in ["Assets:A\u{a0}B", "Assets:A\u{2028}B", "Assets:A\u{200b}B"] {
        assert!(
            rustledger_parser::is_valid_account_name(name),
            "{name:?} must be accepted (beancount accepts it)",
        );
    }
    for name in [
        "Assets:CORP_x", // ASCII punctuation
        "Assets:CORP.x",
        "Assets:CORP@x", // would collide with the price sigil
        "Assets:CORP{x", // would collide with a cost spec
        "Assets:corp✨", // component must start ASCII-uppercase
        "Assets:✨x",    // ...and a symbol is not a valid start
    ] {
        assert!(
            !rustledger_parser::is_valid_account_name(name),
            "{name} must be rejected (beancount rejects it)",
        );
    }
}

/// #1949: a `#tag` or `^link` is a parse error on directives that do not take
/// one — and is still fine where beancount allows it.
///
/// The accept half is not padding. beancount v3 DOES take tags and links on
/// `note` and `document`, and we already agreed with it there, so a blanket
/// "reject trailing tokens on non-transaction directives" would have broken
/// the two cases that were already right. That is why the check is a
/// per-directive call rather than one rule in the dispatcher.
#[test]
fn tags_and_links_are_rejected_only_where_beancount_rejects_them() {
    let pre = "2018-01-01 open Assets:CORP\n2018-01-01 open Equity:Opening\n";
    for (body, what) in [
        ("2018-06-01 open Assets:New #tag\n", "open/tag"),
        ("2018-06-01 open Assets:New ^lnk\n", "open/link"),
        ("2018-06-01 close Assets:CORP #tag\n", "close/tag"),
        (
            "2018-06-01 balance Assets:CORP 0.00 USD #tag\n",
            "balance/tag",
        ),
        ("2018-06-01 commodity EUR #tag\n", "commodity/tag"),
        ("2018-06-01 event \"loc\" \"here\" #tag\n", "event/tag"),
        ("2018-06-01 price EUR 1.10 USD #tag\n", "price/tag"),
        (
            "2018-06-01 pad Assets:CORP Equity:Opening #tag\n",
            "pad/tag",
        ),
    ] {
        let parsed = rustledger_parser::parse(&format!("{pre}{body}"));
        assert!(
            !parsed.errors.is_empty(),
            "{what}: must be a parse error (beancount rejects it)",
        );
    }
    for (body, what) in [
        ("2018-06-01 note Assets:CORP \"n\" #tag\n", "note/tag"),
        ("2018-06-01 note Assets:CORP \"n\" ^lnk\n", "note/link"),
        (
            "2018-06-01 document Assets:CORP \"/tmp/x.pdf\" #tag\n",
            "document/tag",
        ),
    ] {
        let parsed = rustledger_parser::parse(&format!("{pre}{body}"));
        assert!(
            parsed.errors.is_empty(),
            "{what}: must parse cleanly (beancount accepts it), got {:?}",
            parsed.errors,
        );
    }
    // A metadata VALUE may be a tag. The check scans DIRECT child tokens only,
    // and metadata lives in child NODES — this is the input a descendant walk
    // would wrongly reject, which is the worse mistake of the two.
    let parsed = rustledger_parser::parse("2018-01-01 open Assets:A\n  category: #groceries\n");
    assert!(
        parsed.errors.is_empty(),
        "a tag-valued metadata entry must still parse, got {:?}",
        parsed.errors,
    );
}

/// #1955: a metadata key needs at least two characters, as in beancount.
///
/// The reject case is the fix; the accept cases are the point of the test.
/// Only the LENGTH diverged — every other part of the key rule already matched
/// beancount — so this pins the boundary rather than the one bug, and would
/// catch a fix that over-tightened the character classes while it was at it.
#[test]
fn metadata_keys_need_at_least_two_characters() {
    let parse =
        |key: &str| rustledger_parser::parse(&format!("2018-01-01 open Assets:A\n  {key}: 42\n"));
    assert!(
        !parse("k").errors.is_empty(),
        "a single-character key must be rejected (beancount: LexerError)",
    );
    for key in ["kk", "k1", "k-", "k_", "abc"] {
        let parsed = parse(key);
        assert!(
            parsed.errors.is_empty(),
            "{key}: must still be accepted (beancount accepts it), got {:?}",
            parsed.errors,
        );
    }
    // Already agreed before this change, kept so a future edit to the rule
    // cannot quietly relax it.
    assert!(
        !parse("A").errors.is_empty(),
        "an uppercase start must stay rejected",
    );
}

/// #1954: a `^link` is not a valid metadata value; a `#tag` is.
///
/// The asymmetry is the point. Tags and links lex as sibling kinds and are
/// handled as a pair almost everywhere, so rejecting both is the natural
/// mistake — and it would break input beancount accepts. The accept cases
/// below are what stop a future edit from making it.
#[test]
fn a_link_is_not_a_valid_metadata_value() {
    let p = rustledger_parser::parse;
    assert!(
        !p("2018-01-01 open Assets:A\n  ref: ^inv-1\n")
            .errors
            .is_empty(),
        "a link as a metadata value must be rejected (beancount rejects it)",
    );
    for (src, what) in [
        // NOTE the `^lnk` on the transaction: without a `^` anywhere in the
        // source the whole scan is skipped by its `contains('^')` guard, so a
        // tag-only fixture would pass even if the scan rejected TAG too. That
        // is exactly what the first version of this test did — it asserted
        // nothing about the scan's contents.
        (
            "2018-01-01 * \"t\" ^lnk\n  cat: #groceries\n  Assets:A  1.00 USD\n  Assets:B -1.00 USD\n",
            "tag as a metadata value, in a file that DOES contain a link",
        ),
        (
            "2018-01-01 open Assets:A\n  cat: #groceries\n",
            "tag as a metadata value",
        ),
        (
            "2018-01-01 * \"t\" ^inv-1\n  Assets:A  1.00 USD\n  Assets:B -1.00 USD\n",
            "link on a TRANSACTION, where links belong",
        ),
        (
            "2018-01-01 * \"t\" #tag ^inv-1\n  Assets:A  1.00 USD\n  Assets:B -1.00 USD\n",
            "tag and link together on a transaction",
        ),
    ] {
        let parsed = p(src);
        assert!(
            parsed.errors.is_empty(),
            "{what}: must still parse cleanly, got {:?}",
            parsed.errors,
        );
    }
    // Green and red must agree; the scan lives in the shared entry point so
    // this is a guard on that staying true.
    for src in [
        "2018-01-01 open Assets:A\n  ref: ^inv-1\n",
        "2018-01-01 open Assets:A\n  cat: #groceries\n",
        "2018-01-01 * \"t\" ^inv-1\n  Assets:A  1.00 USD\n  Assets:B -1.00 USD\n",
    ] {
        let green = rustledger_parser::parse(src);
        let red = rustledger_parser::cst::parse_red_only(src);
        assert_eq!(
            green.errors.len(),
            red.errors.len(),
            "green/red error-count mismatch for {src}: {:?} vs {:?}",
            green.errors,
            red.errors,
        );
    }
}

/// #1958: tags and links in `custom` / `pushmeta` values — two DIFFERENT rules.
///
/// `pushmeta` follows the metadata rule (a tag is valid, a link is not);
/// `custom` takes neither. The pushmeta-tag accept case is the load-bearing
/// one: `value_tokens_to_meta` serves both callers, so a single check in that
/// shared helper would either let a tag through in `custom` or wrongly reject
/// one here. This test is what makes that mistake fail.
#[test]
fn custom_and_pushmeta_reject_taglinks_by_their_own_rules() {
    let p = rustledger_parser::parse;
    for (src, what) in [
        ("2024-01-15 custom \"b\" ^link\n", "custom: link"),
        ("2024-01-15 custom \"b\" #tag\n", "custom: tag"),
        (
            "pushmeta ref: ^x\n2018-01-01 open Assets:A\npopmeta ref:\n",
            "pushmeta: link",
        ),
    ] {
        assert!(
            !p(src).errors.is_empty(),
            "{what}: must be rejected (beancount rejects it)"
        );
    }
    for (src, what) in [
        // THE asymmetry. A tag is valid in a pushmeta value and must survive.
        (
            "pushmeta ref: #t\n2018-01-01 open Assets:A\npopmeta ref:\n",
            "pushmeta: tag",
        ),
        (
            "2024-01-15 custom \"b\" \"s\" 42 TRUE\n",
            "custom: string/number/bool",
        ),
        ("2024-01-15 custom \"b\" 2024-06-01\n", "custom: date"),
        ("2024-01-15 custom \"b\" Assets:A\n", "custom: account"),
        (
            "pushmeta ref: 42\n2018-01-01 open Assets:A\npopmeta ref:\n",
            "pushmeta: number",
        ),
        // Links and tags where they DO belong, in a file containing both
        // sigils so the scan runs rather than being skipped by its guard.
        (
            "2018-01-01 open Assets:A\n2018-01-01 open Assets:B\n\
             2018-06-01 * \"t\" #tg ^lk\n  Assets:A  1.00 USD\n  Assets:B -1.00 USD\n",
            "tag and link on a transaction",
        ),
    ] {
        let parsed = p(src);
        assert!(
            parsed.errors.is_empty(),
            "{what}: must parse cleanly, got {:?}",
            parsed.errors
        );
    }
    for src in [
        "2024-01-15 custom \"b\" #tag\n",
        "pushmeta ref: #t\n2018-01-01 open Assets:A\npopmeta ref:\n",
    ] {
        let green = rustledger_parser::parse(src);
        let red = rustledger_parser::cst::parse_red_only(src);
        assert_eq!(
            green.errors.len(),
            red.errors.len(),
            "green/red mismatch for {src}: {:?} vs {:?}",
            green.errors,
            red.errors
        );
    }
}

#[test]
fn note_and_document_keep_tags_after_a_blank_line() {
    // A directive node begins with the trivia that precedes it, so a blank
    // line puts a NEWLINE token before the header. The collection loop broke
    // on the first NEWLINE and dropped everything -- which is the way ledgers
    // are normally written, so this was the common case, not the corner.
    //
    // `document` had it too, long before `note` had any tags to lose: it
    // predates #2160 and nothing caught it, because every fixture put the
    // directive immediately after another one.
    let src = "2024-01-01 open Assets:A USD\n\
               2024-01-05 * \"t\"\n\
              \x20 Assets:A  1.00 USD\n\
              \x20 Equity:O\n\
               \n\
               2024-01-20 note Assets:A \"n\" #ntag ^nlink\n\
               \n\
               2024-01-21 document Assets:A \"/tmp/x.pdf\" #dtag ^dlink\n";
    let result = rustledger_parser::parse(src);

    let note = result
        .directives
        .iter()
        .find_map(|d| match &d.value {
            Directive::Note(n) => Some(n),
            _ => None,
        })
        .expect("fixture must yield a note");
    assert_eq!(
        note.tags
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>(),
        vec!["ntag"],
        "a blank line before the note must not eat its tags"
    );
    assert_eq!(note.links.len(), 1, "nor its links");

    let doc = result
        .directives
        .iter()
        .find_map(|d| match &d.value {
            Directive::Document(x) => Some(x),
            _ => None,
        })
        .expect("fixture must yield a document");
    assert_eq!(
        doc.tags.iter().map(ToString::to_string).collect::<Vec<_>>(),
        vec!["dtag"],
        "same for a document, which had this bug before notes existed"
    );
    assert_eq!(doc.links.len(), 1, "nor its links");
}

#[test]
fn note_preserves_tags_and_links() {
    // The parser always accepted `#tag` / `^link` on a note header and threw
    // them away, because `Note` had nowhere to hold them (#2160). It did not
    // even diagnose them: `convert_note` does not call
    // `reject_tags_and_links`, unlike `commodity` and `event`, so they parsed
    // clean and vanished.
    let src = "2024-01-01 open Assets:A USD\n\
               2024-01-05 note Assets:A \"note text\" #ntag ^nlink\n\
               2024-01-06 note Assets:A \"plain\"\n";
    let result = rustledger_parser::parse(src);
    assert!(result.errors.is_empty(), "fixture must parse clean");

    let notes: Vec<&rustledger_core::Note> = result
        .directives
        .iter()
        .filter_map(|d| match &d.value {
            Directive::Note(n) => Some(n),
            _ => None,
        })
        .collect();
    assert_eq!(notes.len(), 2, "fixture must yield both notes");

    let tagged = notes[0];
    assert_eq!(
        tagged
            .tags
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>(),
        vec!["ntag"],
        "a note's tags must survive parsing"
    );
    assert_eq!(
        tagged
            .links
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>(),
        vec!["nlink"],
        "a note's links must survive parsing"
    );

    // And a note without them is empty, not defaulted to the previous note's.
    assert!(notes[1].tags.is_empty() && notes[1].links.is_empty());
}