doppio 2.4.1

A typed compiler pipeline for plain-text Ledger accounting -- parse, resolve, and elaborate .ledger files with a library API built for programmatic use.
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
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
//! Beancount frontend (experimental): parse `.beancount` source text into an
//! [`ast::Journal`].
//!
//! Three layers, mirroring [`crate::grammars::ledger`] and
//! [`crate::grammars::hledger`]:
//!
//! 1. **[`BeancountParser`]** -- the pest-derived parser generated from
//!    `beancount.pest`.
//! 2. **[`parse_beancount`]** -- a convenience wrapper that uses a no-op
//!    include opener; useful in unit tests.
//! 3. **[`BeancountFrontend`]** -- implements [`crate::frontend::Frontend`]
//!    so the CLI dispatches `.beancount` files to this module.
//!
//! ## Mapping decisions (lowering Beancount → [`ast`])
//!
//! Beancount's directive set is wider than ledger-cli's, so a few directives
//! collapse onto existing AST nodes rather than gaining new variants:
//!
//! | Beancount | AST |
//! |-----------|-----|
//! | `open Account [currencies] [booking]` | [`Directive::Account`] |
//! | `close Account` | [`Entry::Comment`] (no semantics, preserved verbatim) |
//! | `commodity SYMBOL` | [`Directive::Commodity`] |
//! | `*` / `!` / `txn` transactions | [`Entry::Transaction`] |
//! | `balance Account amount` | [`Entry::Assertion`] (strict) |
//! | `price COMM amount` | [`Entry::HistoricalPrice`] |
//! | `note` / `document` / `event` / `query` / `custom` | [`Entry::Comment`] |
//! | `pad target source` | [`Entry::Pad`] -- marker; #147 owns elaboration |
//! | `option` / `plugin` | [`Entry::Comment`] |
//! | `include "path"` | recursively parsed via the same opener as hledger |
//! | `#tag` / `^link` on a transaction | collected into `Transaction.tags`. `#tag` becomes a bare tag (`vacation`); `^link` keeps its prefix (`^trip-001`), so a Beancount link is a doppio tag whose name starts with `^`. |
//!
//! ## Known limitations / stubs
//!
//! - The lot syntax `{cost, date, label}` is parsed best-effort (TODO #185):
//!   comma-split parts are classified as cost (first amount-looking part),
//!   date (ISO), or quoted label. The wildcard `{*}` form is accepted but
//!   the wildcard is silently dropped, and the cost commodity vs held
//!   commodity distinction is collapsed.
//! - The total-cost `{{total}}` form is supported: the adapter records
//!   `cost_is_total = true` on the lot annotation and the elaborator
//!   divides by the posting's unit count to derive per-unit basis.
//!   The proto wire format always carries the canonical per-unit form.
//! - `pad` is preserved as an [`Entry::Pad`] marker but the elaborator does
//!   not yet act on it; the algorithm is the subject of #147.
//! - Org-mode outline headings (`*`, `**`, `***`, ... at column 0)
//!   are accepted and silently dropped, matching Beancount itself.
//! - Shebang lines (`#!/usr/bin/env bean-check`) and Org-mode
//!   file-level startup directives (`#+TITLE: ...`, `#+STARTUP:
//!   showall`, etc.) are accepted and silently dropped.
//!
//! ## String escape sequences
//!
//! Recognised inside double-quoted strings: `\\`, `\"`, `\n`, `\t`,
//! `\r`. Unrecognised escapes (e.g. `\q`) pass through verbatim with
//! the leading backslash preserved, rather than erroring. An escaped
//! quote inside a string literal does not terminate the string.
//!
//! ## Lexically-scoped tag and metadata directives
//!
//! `pushtag #foo` / `poptag #foo` and `pushmeta key: value` /
//! `popmeta key:` are supported. The active set persists across
//! `include` directives (matching Beancount). Mismatched pops are
//! silently ignored. Active pushtags are unioned with each
//! transaction's own `#tag`/`^link` set; active pushmetas are added
//! to the transaction's metadata map (where most-recent-pushed wins
//! per key).

pub mod writer;

use crate::ast::*;
use pest::Parser as _;
use pest::iterators::{Pair, Pairs};
use pest::pratt_parser::PrattParser;
use pest_derive::Parser;
use rust_decimal::Decimal;
use std::path::PathBuf;
use std::sync::LazyLock;

/// The raw pest parser generated from `beancount.pest` via `pest_derive`.
#[derive(Parser)]
#[grammar = "grammars/beancount/beancount.pest"]
pub(crate) struct BeancountParser;

// ---
// Internal parser state
// ---
struct Parser<F: Fn(&str) -> Result<String, Box<dyn std::error::Error>>> {
    opener: F,
    base_path: PathBuf,
    /// Lexically-scoped tags pushed by `pushtag` and removed by
    /// `poptag`. Stored as a stack so multiple pushes of the same tag
    /// are independent (each pop removes the most recent occurrence).
    /// Persists across recursive `include` resolution.
    active_tags: Vec<String>,
    /// Lexically-scoped metadata key/value pairs pushed by `pushmeta`
    /// and removed by `popmeta`. Stack semantics mirror `active_tags`
    /// so the most-recent value wins for a given key.
    active_meta: Vec<(String, String)>,
    /// `option "key" "value"` directives encountered during the parse.
    /// Captured here (rather than just dropped as `Entry::Comment`) so
    /// the frontend can interpret options like
    /// `inferred_tolerance_default` after the parse.
    options: Vec<(String, String)>,
}

impl<F: Fn(&str) -> Result<String, Box<dyn std::error::Error>>> Parser<F> {
    fn parse(&mut self, input: &str) -> Result<Journal, Box<dyn std::error::Error>> {
        let pairs = BeancountParser::parse(Rule::journal, input)?;
        let mut entries = Vec::new();

        for pair in pairs.into_iter().next().unwrap().into_inner() {
            match pair.as_rule() {
                Rule::transaction => {
                    let mut tx = parse_transaction(pair)?;
                    // Apply active pushtags as an additional bare-tag note
                    // (resolve_metadata splits each `:a:b:` note independently
                    // so multiple tag-bearing notes union cleanly).
                    if !self.active_tags.is_empty() {
                        tx.notes.push(format!(":{}:", self.active_tags.join(":")));
                    }
                    // Apply active pushmetas as additional `key: value` notes.
                    // Iterate in stack order so the most recently pushed value
                    // for any given key wins (resolve_metadata's BTreeMap
                    // overwrites on duplicate insert).
                    for (k, v) in &self.active_meta {
                        tx.notes.push(format!("{k}: {v}"));
                    }
                    entries.push(Entry::Transaction(tx));
                }
                Rule::open_directive => {
                    entries.push(Entry::Directive(parse_open_directive(pair)));
                }
                Rule::close_directive => {
                    // Preserved as a comment so the source line survives the
                    // round-trip; resolution discards comments.
                    entries.push(Entry::Comment(format!("close {}", pair.as_str().trim())));
                }
                Rule::commodity_directive => {
                    entries.push(Entry::Directive(parse_commodity_directive(pair)));
                }
                Rule::balance_directive => {
                    entries.push(Entry::Assertion(parse_balance_directive(pair)));
                }
                Rule::price_directive => {
                    entries.push(Entry::HistoricalPrice(parse_price_directive(pair)));
                }
                Rule::pad_directive => {
                    entries.push(Entry::Pad(parse_pad_directive(pair)));
                }
                Rule::option_directive => {
                    // Capture the (key, value) for later interpretation
                    // by BeancountFrontend (e.g. inferred_tolerance_default).
                    // Also push as Comment so the source line survives the
                    // round-trip.
                    let raw = pair.as_str().trim().to_string();
                    let mut inner = pair.into_inner();
                    let key = string_inner(inner.next().expect("option key"));
                    let value = string_inner(inner.next().expect("option value"));
                    self.options.push((key, value));
                    entries.push(Entry::Comment(raw));
                }
                Rule::note_directive
                | Rule::document_directive
                | Rule::event_directive
                | Rule::query_directive
                | Rule::custom_directive
                | Rule::plugin_directive => {
                    entries.push(Entry::Comment(pair.as_str().trim().to_string()));
                }
                Rule::pushtag_directive => {
                    // `pushtag #name`: capture the tag (skipping the `#`).
                    let tag_pair = pair
                        .into_inner()
                        .find(|p| p.as_rule() == Rule::tag)
                        .expect("pushtag must contain a tag");
                    self.active_tags.push(tag_pair.as_str()[1..].to_string());
                }
                Rule::poptag_directive => {
                    // `poptag #name`: remove the most recent matching tag.
                    // Mismatched pops are silently ignored (Beancount itself
                    // emits a warning, but we don't have a warning channel).
                    let tag_pair = pair
                        .into_inner()
                        .find(|p| p.as_rule() == Rule::tag)
                        .expect("poptag must contain a tag");
                    let name = &tag_pair.as_str()[1..];
                    if let Some(idx) = self.active_tags.iter().rposition(|t| t == name) {
                        self.active_tags.remove(idx);
                    }
                }
                Rule::pushmeta_directive => {
                    // `pushmeta key: value`: capture the key and the
                    // verbatim value text. Strip outer quotes on
                    // string-typed values (`pushmeta phase: "Q1"`) so the
                    // active-meta stack carries the user-visible value,
                    // not the quoted token text -- mirrors how
                    // `metadata_line_to_note` handles the same shape.
                    let mut inner = pair.into_inner();
                    let key = inner.next().unwrap().as_str().to_string();
                    let value = inner
                        .next()
                        .map(|v| {
                            let raw = v.as_str().trim();
                            if raw.len() >= 2 && raw.starts_with('"') && raw.ends_with('"') {
                                raw[1..raw.len() - 1].to_string()
                            } else {
                                raw.to_string()
                            }
                        })
                        .unwrap_or_default();
                    self.active_meta.push((key, value));
                }
                Rule::popmeta_directive => {
                    // `popmeta key:`: remove the most recent value pushed
                    // for that key.
                    let mut inner = pair.into_inner();
                    let key = inner.next().unwrap().as_str();
                    if let Some(idx) = self.active_meta.iter().rposition(|(k, _)| k == key) {
                        self.active_meta.remove(idx);
                    }
                }
                Rule::comment_line => {
                    entries.push(Entry::Comment(pair.as_str().to_string()));
                }
                Rule::org_mode_heading | Rule::shebang_line | Rule::org_mode_meta => {
                    // Beancount accepts these as top-level no-ops:
                    //   - `*` / `**` / ... outline headings (#190)
                    //   - `#!/usr/bin/env bean-web` shebangs (#199)
                    //   - `#+STARTUP: showall` org-mode startup (#199)
                    // Preserve as comments so the source line survives
                    // the round-trip; resolution discards them.
                    entries.push(Entry::Comment(pair.as_str().to_string()));
                }
                Rule::include_directive => {
                    let raw = pair.into_inner().next().unwrap();
                    let path_str = string_inner(raw);
                    let include_path = self.base_path.join(&path_str);
                    let new_input = (self.opener)(include_path.as_os_str().to_str().unwrap())?;
                    let new_base_path = include_path
                        .parent()
                        .map(std::path::PathBuf::from)
                        .unwrap_or_else(|| self.base_path.clone());
                    let old_base_path = std::mem::replace(&mut self.base_path, new_base_path);
                    entries.append(&mut self.parse(&new_input)?.entries);
                    let _ = std::mem::replace(&mut self.base_path, old_base_path);
                }
                _ => {}
            }
        }

        Ok(Journal { entries })
    }
}

// ---
// Atom helpers
// ---
fn parse_date(pair: Pair<Rule>) -> Date {
    // date = ${ year ~ "-" ~ monthdate ~ "-" ~ monthdate }
    let mut inner = pair.into_inner();
    let year: i32 = inner.next().unwrap().as_str().parse().unwrap();
    let month: u32 = inner.next().unwrap().as_str().parse().unwrap();
    let day: u32 = inner.next().unwrap().as_str().parse().unwrap();
    Date {
        year: Some(year),
        month,
        date: day,
    }
}

fn parse_flag(s: &str) -> TransactionState {
    match s {
        "*" => TransactionState::Cleared,
        "!" => TransactionState::Pending,
        // `txn` keyword has no state-equivalent connotation in ledger semantics;
        // treat it as uncleared (the conservative interpretation).
        _ => TransactionState::Uncleared,
    }
}

/// Extract and decode the inner text of a `string` pair: strip the
/// surrounding quotes and interpret recognised backslash escape
/// sequences.
///
/// Recognised: `\\`, `\"`, `\n`, `\t`, `\r`. Unrecognised escapes
/// (e.g. `\q`) pass through verbatim with the leading backslash
/// preserved -- this matches Python's "be lenient on unknown escapes
/// in string literals" stance and avoids losing source bytes.
fn string_inner(pair: Pair<Rule>) -> String {
    // string = ${ "\"" ~ string_inner ~ "\"" }
    let inner_pair = pair
        .into_inner()
        .next()
        .expect("string must contain string_inner");
    decode_string_escapes(inner_pair.as_str())
}

fn decode_string_escapes(raw: &str) -> String {
    let mut out = String::with_capacity(raw.len());
    let mut chars = raw.chars();
    while let Some(c) = chars.next() {
        if c != '\\' {
            out.push(c);
            continue;
        }
        match chars.next() {
            Some('\\') => out.push('\\'),
            Some('"') => out.push('"'),
            Some('n') => out.push('\n'),
            Some('t') => out.push('\t'),
            Some('r') => out.push('\r'),
            // Unknown escape: keep the backslash and the following
            // character verbatim. This is intentionally lenient.
            Some(other) => {
                out.push('\\');
                out.push(other);
            }
            // Trailing lone backslash (only possible if the grammar
            // somehow let one through; preserve it).
            None => out.push('\\'),
        }
    }
    out
}

// ---
// Transactions + postings
// ---
fn parse_transaction(pair: Pair<Rule>) -> Result<Transaction, Box<dyn std::error::Error>> {
    let mut inner = pair.into_inner();
    let header_pair = inner.next().expect("transaction must have a txn_header");

    let mut header_fields = header_pair.into_inner();
    let date = parse_date(header_fields.next().unwrap());

    let mut state = TransactionState::Uncleared;
    let mut payee_or_desc: Vec<String> = Vec::new();
    let mut notes: Vec<String> = Vec::new();
    let mut tags: Vec<String> = Vec::new();

    for p in header_fields {
        match p.as_rule() {
            Rule::flag => state = parse_flag(p.as_str()),
            Rule::string => payee_or_desc.push(string_inner(p)),
            // Both `#tag` and `^link` land in Transaction.tags, but
            // the `^` prefix is preserved so the Beancount
            // tag-vs-link distinction can be recovered downstream:
            // a Beancount link is encoded as a doppio tag that
            // starts with `^`. The `#` prefix is stripped because
            // every other ledger-cli-style tag is bare.
            Rule::tag => tags.push(p.as_str()[1..].to_string()),
            Rule::link => tags.push(p.as_str().to_string()),
            Rule::note => {
                notes.push(p.into_inner().as_str().trim().to_string());
            }
            _ => {}
        }
    }

    // Emit collected tags/links as a single ledger-cli-style bare-tag
    // note. resolve_metadata splits on `:` and pushes each into
    // Transaction.tags, so multiple tags survive (they would otherwise
    // clobber each other if encoded as `key: value` metadata).
    if !tags.is_empty() {
        notes.push(format!(":{}:", tags.join(":")));
    }

    // Beancount transaction headers carry up to two strings: `"payee" "narration"`
    // or just `"narration"`. We map: one string -> description; two strings
    // -> code = payee, description = narration. The code field is the closest
    // ledger-cli analogue for the payee slot.
    let (code, description) = match payee_or_desc.len() {
        0 => (None, String::new()),
        1 => (None, payee_or_desc.remove(0)),
        _ => {
            let payee = payee_or_desc.remove(0);
            let narration = payee_or_desc.remove(0);
            (Some(payee), narration)
        }
    };

    let mut postings = Vec::new();

    for p in inner {
        match p.as_rule() {
            Rule::posting => postings.push(parse_posting(p)?),
            Rule::metadata_line => {
                notes.push(metadata_line_to_note(p));
            }
            _ => {}
        }
    }

    Ok(Transaction {
        date,
        secondary_date: None,
        state,
        code,
        description,
        notes,
        postings,
    })
}

fn metadata_line_to_note(pair: Pair<Rule>) -> String {
    // metadata_line = ${ indent+ ~ metadata_key ~ ":" ~ ws* ~ metadata_value ~ (NEWLINE | EOI) }
    let mut inner = pair.into_inner();
    let key = inner.next().unwrap().as_str().to_string();
    let val = inner
        .next()
        .map(|v| {
            // Beancount has typed metadata values: a value that is enclosed in
            // double-quotes is a string literal whose user-visible value is the
            // unquoted contents (mirrors `option "key" "value"`). Strip the
            // outer quotes so consumers compare against bean-check's
            // `entry.meta` cleanly. Non-quoted values pass through as-is.
            let raw = v.as_str().trim();
            if raw.len() >= 2 && raw.starts_with('"') && raw.ends_with('"') {
                raw[1..raw.len() - 1].to_string()
            } else {
                raw.to_string()
            }
        })
        .unwrap_or_default();
    format!("{key}: {val}")
}

fn parse_posting(pair: Pair<Rule>) -> Result<Posting, Box<dyn std::error::Error>> {
    let mut state = TransactionState::Uncleared;
    let mut account = String::new();
    let mut amount: Option<AmountDetails> = None;
    let mut notes: Vec<String> = Vec::new();

    for p in pair.into_inner() {
        match p.as_rule() {
            Rule::flag => state = parse_flag(p.as_str()),
            Rule::posting_account => {
                let inner_pair = p
                    .into_inner()
                    .next()
                    .expect("posting_account must have one child");
                account = inner_pair.as_str().trim().to_string();
            }
            Rule::amount_logic => amount = Some(parse_amount_logic(p)?),
            Rule::note => notes.push(p.into_inner().as_str().trim().to_string()),
            Rule::metadata_line => notes.push(metadata_line_to_note(p)),
            _ => {}
        }
    }

    Ok(Posting {
        account,
        amount,
        state,
        notes,
        kind: PostingKind::Real,
    })
}

fn parse_amount_logic(pair: Pair<Rule>) -> Result<AmountDetails, Box<dyn std::error::Error>> {
    // amount_logic = ${ (ws{2,} | "\t") ~ value_expr ~ (ws* ~ lot_annotation_or_price)* }
    let mut value: Option<ValueExpr> = None;
    let mut lot_annotation = LotAnnotation::default();
    let mut has_lot_annotation = false;
    let mut lot_pricing: Option<LotPricing> = None;

    for p in pair.into_inner() {
        match p.as_rule() {
            Rule::value_expr => value = Some(parse_expr(p)),
            Rule::lot_annotation => {
                // The `{{total}}` form: the grammar matches it before
                // `{cost}` so the raw token text starts with `{{`. The
                // elaborator divides by the posting's unit count when
                // applying the cost (see #193).
                if p.as_str().starts_with("{{") {
                    lot_annotation.cost_is_total = true;
                }
                has_lot_annotation = true;
                merge_lot_annotation_into(p, &mut lot_annotation);
            }
            Rule::lot_price => {
                let s = p.as_str();
                let inner_expr = parse_expr(p.into_inner().next().unwrap());
                if s.starts_with("@@") {
                    lot_pricing = Some(LotPricing::Total(inner_expr));
                } else {
                    lot_pricing = Some(LotPricing::Unit(inner_expr));
                }
            }
            _ => {}
        }
    }

    Ok(AmountDetails::Amount {
        value: value.expect("amount_logic without a value_expr"),
        lot_annotation: has_lot_annotation.then_some(lot_annotation),
        lot_pricing,
        balance_assertion: None,
    })
}

/// Parse the inner text of a Beancount `{...}` or `{{...}}` lot
/// annotation.
///
/// Best-effort: comma-split the inner text and classify each part.
/// - First amount-looking part (`<number> <COMMODITY>`) becomes the cost.
/// - First ISO date becomes the lot date.
/// - First quoted string becomes the lot label.
/// - A bare `*` token (the cost wildcard) is skipped.
///
/// Both single- and double-brace forms route through this function;
/// the caller (`parse_amount_logic`) detects the `{{...}}` shape and
/// sets [`LotAnnotation::cost_is_total`] on the accumulator before
/// merging, and the elaborator divides by the posting's unit count
/// when applying a total-cost lot. The cost commodity is preserved
/// in the elaborated `Lot.cost` (a multi-commodity [`Amount`] on the
/// wire format), so downstream consumers can distinguish the cost
/// basis commodity from the held commodity.
///
/// # Known gap
///
/// The wildcard `{*}` / `{*, ...}` form ("automatic cost") is parsed
/// without error but the wildcard sentinel is dropped: there is no
/// AST representation for "infer this lot's cost from the
/// inventory." This is intentional -- bean-check 3.2.0 itself
/// errors with "Cost merging is not supported yet" on the same
/// input, so there is no canonical reference behaviour to mirror.
/// File a fresh issue if a real consumer surfaces a need.
fn merge_lot_annotation_into(pair: Pair<Rule>, acc: &mut LotAnnotation) {
    // lot_annotation = ${ ("{{" ~ lot_inner_total ~ "}}") | ("{" ~ lot_inner ~ "}") }
    let inner = pair
        .into_inner()
        .next()
        .expect("lot_annotation must have an inner");
    let raw = inner.as_str();
    for part in raw.split(',') {
        let part = part.trim();
        if part.is_empty() || part == "*" {
            continue;
        }
        // Quoted label?
        if let Some(stripped) = part.strip_prefix('"').and_then(|s| s.strip_suffix('"')) {
            if acc.note.is_none() {
                acc.note = Some(stripped.to_string());
            }
            continue;
        }
        // ISO date?
        if let Ok(d) = chrono::NaiveDate::parse_from_str(part, "%Y-%m-%d") {
            if acc.date.is_none() {
                acc.date = Some(d);
            }
            continue;
        }
        // Otherwise: try to parse as a value expression (the cost).
        if acc.cost.is_none()
            && let Ok(mut pairs) = BeancountParser::parse(Rule::value_expr, part)
            && let Some(p) = pairs.next()
        {
            acc.cost = Some(parse_expr(p));
        }
    }
}

// ---
// Directives
// ---
fn parse_open_directive(pair: Pair<Rule>) -> Directive {
    // open_directive = ${ date ~ ws+ ~ "open" ~ ws+ ~ account
    //   ~ (ws+ ~ commodity_list)? ~ (ws+ ~ booking_method)?
    //   ~ (ws* ~ note)? ~ (NEWLINE | EOI) ~ metadata_line* }
    let mut inner = pair.into_inner();
    let _date = parse_date(inner.next().unwrap()); // date is captured at the marker level; not used in Directive::Account
    let name = inner.next().unwrap().as_str().to_string();

    let mut notes = Vec::new();
    let mut items = Vec::new();

    for p in inner {
        match p.as_rule() {
            Rule::commodity_list => {
                // Currencies on `open` aren't representable as a structured
                // AccountItem yet; preserve them as a metadata note.
                let list: Vec<&str> = p.into_inner().map(|c| c.as_str()).collect();
                notes.push(format!("currencies: {}", list.join(",")));
            }
            Rule::booking_method => {
                let inner_pair = p.into_inner().next().unwrap();
                let raw = inner_pair.as_str();
                if let Some(method) = parse_booking_method(raw) {
                    items.push(crate::ast::AccountItem::Booking(method));
                } else {
                    // Unknown spelling -- preserve as a free-form note for diagnostics.
                    notes.push(format!("booking: {raw}"));
                }
            }
            Rule::note => notes.push(p.into_inner().as_str().trim().to_string()),
            Rule::metadata_line => notes.push(metadata_line_to_note(p)),
            _ => {}
        }
    }

    Directive::Account { name, notes, items }
}

/// Map Beancount's `Booking` spelling on the `open` directive to the
/// structured [`crate::resolution::BookingMethod`]. Unknown values
/// return `None`; callers fall through to a free-form note so the
/// raw text is still observable.
fn parse_booking_method(raw: &str) -> Option<crate::resolution::BookingMethod> {
    use crate::resolution::BookingMethod::*;
    Some(match raw {
        "STRICT" => Strict,
        "STRICT_WITH_SIZE" => StrictWithSize,
        "NONE" => None,
        "AVERAGE" => Average,
        "FIFO" => Fifo,
        "LIFO" => Lifo,
        "HIFO" => Hifo,
        _ => return Option::None,
    })
}

fn parse_commodity_directive(pair: Pair<Rule>) -> Directive {
    // commodity_directive = ${ date ~ ws+ ~ "commodity" ~ ws+ ~ commodity ... }
    let mut inner = pair.into_inner();
    let _date = parse_date(inner.next().unwrap());
    let name = inner.next().unwrap().as_str().to_string();

    let mut notes = Vec::new();
    let items = Vec::new();

    for p in inner {
        match p.as_rule() {
            Rule::note => notes.push(p.into_inner().as_str().trim().to_string()),
            Rule::metadata_line => notes.push(metadata_line_to_note(p)),
            _ => {}
        }
    }

    Directive::Commodity { name, notes, items }
}

fn parse_balance_directive(pair: Pair<Rule>) -> AssertionDirective {
    // balance_directive = ${ date ~ ws+ ~ "balance" ~ ws+ ~ account ~ ws+ ~ value_expr ... }
    let mut inner = pair.into_inner();
    let date = parse_date(inner.next().unwrap());
    let account = inner.next().unwrap().as_str().to_string();
    let value_expr_pair = inner
        .find(|p| p.as_rule() == Rule::value_expr)
        .expect("balance_directive must have a value_expr");
    let amount = parse_expr(value_expr_pair);

    AssertionDirective {
        date,
        account,
        amount,
        // Beancount balance assertions are strict by definition (the
        // tolerance comes from the precision of the asserted amount, not
        // from a weak-vs-strict toggle as in ledger-cli/hledger).
        strict: true,
    }
}

fn parse_price_directive(pair: Pair<Rule>) -> HistoricalPrice {
    // price_directive = ${ date ~ ws+ ~ "price" ~ ws+ ~ commodity ~ ws+ ~ value_expr ... }
    let mut inner = pair.into_inner();
    let date = parse_date(inner.next().unwrap());
    let commodity = inner.next().unwrap().as_str().to_string();
    let value_expr_pair = inner
        .find(|p| p.as_rule() == Rule::value_expr)
        .expect("price_directive must have a value_expr");
    let price = parse_expr(value_expr_pair);

    HistoricalPrice {
        date,
        time: None,
        commodity,
        price,
    }
}

fn parse_pad_directive(pair: Pair<Rule>) -> PadDirective {
    // pad_directive = ${ date ~ ws+ ~ "pad" ~ ws+ ~ account ~ ws+ ~ account ... }
    let mut inner = pair.into_inner();
    let date = parse_date(inner.next().unwrap());
    let target_account = inner.next().unwrap().as_str().to_string();
    let source_account = inner.next().unwrap().as_str().to_string();

    PadDirective {
        date,
        target_account,
        source_account,
    }
}

// ---
// Value expression parser (Pratt) -- shape mirrors hledger's.
// ---
static PRATT: LazyLock<PrattParser<Rule>> = LazyLock::new(|| {
    use Rule::*;
    use pest::pratt_parser::{Assoc::*, Op};
    PrattParser::new()
        .op(Op::infix(add, Left) | Op::infix(sub, Left))
        .op(Op::infix(mul, Left) | Op::infix(div, Left))
        .op(Op::prefix(prefix_op))
});

fn parse_expr(pair: Pair<Rule>) -> ValueExpr {
    // value_expr = ${ expr ~ (ws+ ~ commodity)? }
    let mut inner = pair.into_inner();
    let expr_pair = inner.next().expect("empty value_expr");
    let mut ast = run_pratt(expr_pair.into_inner());

    if let Some(comm_pair) = inner.next() {
        ast = ValueExpr::Typed {
            expr: Box::new(ast),
            commodity: comm_pair.as_str().to_string(),
        };
    }
    ast
}

fn run_pratt(pairs: Pairs<Rule>) -> ValueExpr {
    PRATT
        .map_primary(|pair| match pair.as_rule() {
            Rule::term => run_pratt(pair.into_inner()),
            Rule::primary => {
                let base = pair.into_inner().next().expect("primary must have a base");
                run_pratt(pest::iterators::Pairs::single(base))
            }
            Rule::amount => {
                let mut inner = pair.into_inner();
                let first = inner.next().unwrap();
                match first.as_rule() {
                    Rule::number => {
                        let val = clean_decimal(first.as_str());
                        let comm = inner.next().map(|c| c.as_str().to_string());
                        ValueExpr::Amount {
                            value: val,
                            commodity: comm,
                        }
                    }
                    _ => unreachable!("unexpected amount sub-rule: {:?}", first.as_rule()),
                }
            }
            Rule::commodity => ValueExpr::Commodity(pair.as_str().to_string()),
            Rule::expr => run_pratt(pair.into_inner()),
            _ => unreachable!("unexpected primary rule: {:?}", pair.as_rule()),
        })
        .map_prefix(|op, expr| ValueExpr::Unary {
            op: if op.as_str() == "-" { Op::Sub } else { Op::Add },
            expr: Box::new(expr),
        })
        .map_infix(|lhs, op, rhs| {
            let op = match op.as_rule() {
                Rule::add => Op::Add,
                Rule::sub => Op::Sub,
                Rule::mul => Op::Mul,
                Rule::div => Op::Div,
                _ => unreachable!(),
            };
            ValueExpr::Binary {
                lhs: Box::new(lhs),
                rhs: Box::new(rhs),
                op,
            }
        })
        .parse(pairs)
}

fn clean_decimal(s: &str) -> Decimal {
    s.replace(',', "").parse().unwrap_or(Decimal::ZERO)
}

// ---
// Date-order normalisation
// ---
/// Beancount evaluates entries in **date order**, not source order. We
/// stable-sort the AST entries here so the resolver and elaborator can
/// stay source-order-agnostic. Undated entries (directives, comments)
/// keep their original relative position and sort before any dated
/// entry.
///
/// Within the same date, Beancount applies a fixed ordering that
/// matters for the pad/balance interaction (#147) and for balance
/// assertions that should fire BEFORE any same-date transaction
/// touching the asserted account (#212):
///
/// 1. **Pad** -- registers a "pending pad" for the next balance.
/// 2. **Balance** -- assertion fires at the *beginning* of the date,
///    consuming any pending pad. Per Beancount's documentation:
///    "The balance directive applies for the *beginning* of the date."
/// 3. **Transactions / HistoricalPrice / other dated** -- happen
///    during the date, in source order (stable sort tiebreak).
fn sort_entries_by_date(entries: &mut [Entry]) {
    entries.sort_by_key(entry_sort_key);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum SameDayOrder {
    /// Pad registers a pending fill before any same-date balance.
    Pad,
    /// Balance asserts at the start of the date, after pad.
    Balance,
    /// Transactions, prices, etc. -- "during" the date.
    During,
}

fn entry_sort_key(entry: &Entry) -> (Option<Date>, SameDayOrder) {
    (entry_date_key(entry), entry_sub_order(entry))
}

fn entry_date_key(entry: &Entry) -> Option<Date> {
    match entry {
        Entry::Transaction(t) => Some(t.date.clone()),
        Entry::Assertion(a) => Some(a.date.clone()),
        Entry::HistoricalPrice(hp) => Some(hp.date.clone()),
        Entry::Pad(p) => Some(p.date.clone()),
        Entry::Directive(_)
        | Entry::Comment(_)
        | Entry::AutoRule(_)
        | Entry::CommodityConversion { .. } => None,
    }
}

fn entry_sub_order(entry: &Entry) -> SameDayOrder {
    match entry {
        Entry::Pad(_) => SameDayOrder::Pad,
        Entry::Assertion(_) => SameDayOrder::Balance,
        _ => SameDayOrder::During,
    }
}

// ---
// Convenience function (test-only)
// ---
/// Parse Beancount source with no `include` support.
///
/// Any `include` directive in the input is silently resolved to an empty
/// string (no entries pulled in). Useful for unit tests and standalone
/// parsing. Entries are date-sorted before return (matching the
/// Beancount language semantics).
#[cfg(test)]
pub(crate) fn parse_beancount(input: &str) -> Result<Journal, Box<dyn std::error::Error>> {
    Ok(parse_beancount_with_options(input)?.0)
}

/// Like [`parse_beancount`] but also returns any `option` directives
/// captured during the parse. Used by the elaboration test helper to
/// honour `inferred_tolerance_default`.
#[cfg(test)]
pub(crate) fn parse_beancount_with_options(
    input: &str,
) -> Result<(Journal, Vec<(String, String)>), Box<dyn std::error::Error>> {
    let mut parser = Parser {
        opener: |_| Ok(String::new()),
        base_path: PathBuf::new(),
        active_tags: Vec::new(),
        active_meta: Vec::new(),
        options: Vec::new(),
    };
    let mut journal = parser.parse(input)?;
    sort_entries_by_date(&mut journal.entries);
    Ok((journal, parser.options))
}

// ---
// Frontend impl
// ---
/// The Beancount file-format frontend (experimental).
///
/// Recognises `.beancount` files and lowers them to a
/// [`crate::resolution::HIR`] via the Beancount PEG grammar and the
/// AST adapter in this module.
///
/// ## Extension dispatch
///
/// | Extension | Frontend |
/// |-----------|----------|
/// | `.beancount` | [`BeancountFrontend`] |
///
/// ## Experimental
///
/// The Beancount frontend is shipped behind the `M#9` milestone marker
/// because the `pad` directive evaluator (#147) is not yet implemented:
/// pads are preserved in the AST as markers but produce no balancing
/// transaction during elaboration. See the module-level docs for the
/// full mapping table and known gaps.
pub struct BeancountFrontend;

/// Construct the default [`crate::resolution::ElaborationConfig`] for
/// files written in Beancount syntax: half-smallest-precision
/// per-transaction balance tolerance (matching bean-check's
/// `0.5 * 10^(-min_scale)` rule, #198), cost-basis lot pricing with
/// explicit gain/loss postings, and subtree-aware top-level `balance`
/// directives (matching bean-check's account-tree aggregation, #214).
///
/// Per-commodity tolerance overrides set via Beancount's
/// `option "inferred_tolerance_default" "COMMODITY:VALUE"` are journal-
/// level state and live on
/// [`crate::resolution::GlobalContext::tolerance_overrides`]; they
/// layer on top of this default at elaboration time.
///
/// Available as a free function so test code can construct the config
/// without instantiating [`BeancountFrontend`]; the trait method
/// [`crate::frontend::Frontend::elaboration_defaults`] delegates here.
pub fn beancount_defaults() -> crate::resolution::ElaborationConfig {
    crate::resolution::ElaborationConfig {
        tolerance_mode: crate::resolution::ToleranceMode::FractionOfSmallestPrecision(
            rust_decimal::Decimal::new(5, 1),
        ),
        balance_mode: crate::resolution::BalanceMode::CostBasis,
        assertion_scope: crate::resolution::AssertionScope::Subtree,
        lot_validation_mode: crate::resolution::LotValidationMode::Strict,
        default_booking_method: crate::resolution::BookingMethod::Strict,
        // Beancount requires an explicit cost on every lot-bearing posting;
        // implicit-cost inference does not apply.
        infer_implicit_total_cost: false,
    }
}

impl crate::frontend::Frontend for BeancountFrontend {
    fn extensions(&self) -> &'static [&'static str] {
        &["beancount"]
    }

    fn elaboration_defaults(&self) -> crate::resolution::ElaborationConfig {
        beancount_defaults()
    }

    fn parse(
        &self,
        input: &str,
        base_path: &std::path::Path,
        opener: &crate::frontend::Opener,
    ) -> Result<crate::resolution::HIR, Box<dyn std::error::Error>> {
        let mut parser = Parser {
            opener: |path: &str| opener(path),
            base_path: base_path.to_path_buf(),
            active_tags: Vec::new(),
            active_meta: Vec::new(),
            options: Vec::new(),
        };
        let mut ast_journal = parser.parse(input)?;
        // Beancount is date-ordered, not source-ordered. Sort once at the
        // outermost call (after include resolution has flattened the tree)
        // so resolver + elaborator can treat the entries as already in
        // chronological order.
        sort_entries_by_date(&mut ast_journal.entries);
        let mut hir: crate::resolution::HIR = ast_journal.try_into()?;
        // Honour `option "inferred_tolerance_default" "COMMODITY:VALUE"`
        // directives. Each occurrence overrides any previous value for
        // the same commodity (last write wins). The directive is
        // file-level; nested includes contribute to the same map. This
        // is journal state derived from the source and stays on
        // GlobalContext -- the per-commodity override layers on top of
        // ElaborationConfig::tolerance_mode at elaboration time.
        for (k, v) in &parser.options {
            if k == "inferred_tolerance_default"
                && let Some((commodity, decimal)) = v.split_once(':')
                && let Ok(d) = decimal.trim().parse::<rust_decimal::Decimal>()
            {
                hir.global_context
                    .tolerance_overrides
                    .insert(commodity.trim().to_string(), d);
            }
        }
        Ok(hir)
    }

    fn write_journal(
        &self,
        hir: &crate::resolution::HIR,
        w: &mut dyn std::io::Write,
    ) -> std::io::Result<()> {
        writer::write(hir, w)
    }
}

// ---
// Tests
// ---
#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal::dec;

    /// The fixture covers every directive type the grammar supports;
    /// it is the contract for #145.
    const SAMPLE: &str = include_str!("../../../tests/fixtures/sample.beancount");

    // -- grammar-level coverage (carried over from #145) ----------------------

    #[test]
    fn sample_fixture_parses() {
        BeancountParser::parse(Rule::journal, SAMPLE).unwrap_or_else(|e| {
            panic!("sample.beancount failed to parse:\n{e}");
        });
    }

    fn parse_one(rule: Rule, input: &str) {
        BeancountParser::parse(rule, input)
            .unwrap_or_else(|e| panic!("expected `{input}` to match {rule:?}, got: {e}"));
    }

    #[test]
    fn date_iso_only() {
        parse_one(Rule::date, "2024-01-15");
    }

    #[test]
    fn date_rejects_slash_separator() {
        assert!(BeancountParser::parse(Rule::date, "2024/01/15").is_err());
    }

    #[test]
    fn currency_uppercase_identifier() {
        parse_one(Rule::commodity, "USD");
        parse_one(Rule::commodity, "EUR");
        parse_one(Rule::commodity, "AAPL");
        parse_one(Rule::commodity, "BTC");
        parse_one(Rule::commodity, "VHT_2024");
    }

    #[test]
    fn currency_must_start_uppercase() {
        assert!(BeancountParser::parse(Rule::commodity, "usd").is_err());
    }

    #[test]
    fn account_colon_segments() {
        parse_one(Rule::account, "Assets:Bank:Checking");
        parse_one(Rule::account, "Equity:Opening-Balances");
        parse_one(Rule::account, "Income:US:Acme:Salary");
    }

    #[test]
    fn account_requires_at_least_two_segments() {
        assert!(BeancountParser::parse(Rule::account, "Assets").is_err());
    }

    #[test]
    fn string_double_quoted() {
        parse_one(Rule::string, "\"hello world\"");
        parse_one(Rule::string, "\"\"");
    }

    #[test]
    fn string_with_escape_sequences() {
        // Escaped quote inside the literal must not terminate the string.
        parse_one(Rule::string, "\"hello \\\" world\"");
        // Common escapes the grammar must accept.
        parse_one(Rule::string, "\"line1\\nline2\"");
        parse_one(Rule::string, "\"col1\\tcol2\"");
        parse_one(Rule::string, "\"path C:\\\\Users\\\\me\"");
    }

    #[test]
    fn decode_string_escapes_recognises_known_sequences() {
        assert_eq!(decode_string_escapes("hello"), "hello");
        assert_eq!(decode_string_escapes(r"line1\nline2"), "line1\nline2");
        assert_eq!(decode_string_escapes(r"col1\tcol2"), "col1\tcol2");
        assert_eq!(decode_string_escapes(r"x\rclear"), "x\rclear");
        assert_eq!(decode_string_escapes(r#"say \"hi\""#), r#"say "hi""#);
        assert_eq!(decode_string_escapes(r"a\\b"), r"a\b");
    }

    #[test]
    fn decode_string_escapes_passes_unknown_through() {
        // \q is not a recognised escape; the backslash + char survive.
        assert_eq!(decode_string_escapes(r"a\qb"), r"a\qb");
        // Trailing lone backslash also survives.
        assert_eq!(decode_string_escapes(r"a\"), r"a\");
    }

    #[test]
    fn flag_recognises_star_bang_txn() {
        parse_one(Rule::flag, "*");
        parse_one(Rule::flag, "!");
        parse_one(Rule::flag, "txn");
    }

    #[test]
    fn tag_and_link_chars() {
        parse_one(Rule::tag, "#vacation");
        parse_one(Rule::tag, "#trip-2024");
        parse_one(Rule::link, "^statement-2024-01");
    }

    #[test]
    fn plugin_directive_with_and_without_arg() {
        parse_one(Rule::plugin_directive, "plugin \"beancount.plugins.foo\"\n");
        parse_one(
            Rule::plugin_directive,
            "plugin \"beancount.plugins.foo\" \"argument-string\"\n",
        );
    }

    #[test]
    fn org_mode_heading_grammar_rule() {
        parse_one(Rule::org_mode_heading, "* Personal");
        parse_one(Rule::org_mode_heading, "** Bank accounts");
        parse_one(Rule::org_mode_heading, "*** Deeply nested");
    }

    #[test]
    fn shebang_and_org_mode_meta_grammar_rules() {
        parse_one(Rule::shebang_line, "#!/usr/bin/env bean-check");
        parse_one(Rule::shebang_line, "#!/usr/bin/python3");
        parse_one(Rule::org_mode_meta, "#+STARTUP: showall");
        parse_one(Rule::org_mode_meta, "#+TITLE: My Personal Journal");
        parse_one(Rule::org_mode_meta, "#+OPTIONS: toc:nil");
    }

    #[test]
    fn beancount_file_with_shebang_and_org_meta_prelude() {
        // Real upstream Beancount files (e.g. the bean-web examples)
        // open with a shebang and one or more org-mode #+ directives;
        // both must be accepted as top-level no-ops.
        let input = "\
#!/usr/bin/env bean-check
#+TITLE: Test journal
#+STARTUP: showall

2024-01-01 open Assets:Cash USD
2024-01-15 * \"Test\"
  Assets:Cash      100.00 USD
  Income:Initial  -100.00 USD
";
        let journal = parse_beancount(input).expect("parse with prelude");
        let txn_count = journal
            .entries
            .iter()
            .filter(|e| matches!(e, Entry::Transaction(_)))
            .count();
        assert_eq!(txn_count, 1, "transaction should still be picked up");
        // Shebang + two #+ lines preserved as comments.
        let comment_count = journal
            .entries
            .iter()
            .filter(|e| matches!(e, Entry::Comment(_)))
            .count();
        assert!(
            comment_count >= 3,
            "shebang + 2 #+ lines should each become Entry::Comment, got {} comments",
            comment_count
        );
    }

    // -- adapter / Frontend tests --------------------------------------------

    #[test]
    fn simple_complete_transaction() {
        let input = "\
2024-01-12 * \"Acme Studio\" \"Invoice 0123\"
  Assets:Bank:Checking          3400.00 USD
  Income:Salary                -3400.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!("expected transaction");
        };
        assert!(matches!(tx.state, TransactionState::Cleared));
        assert_eq!(tx.code.as_deref(), Some("Acme Studio"));
        assert_eq!(tx.description, "Invoice 0123");
        assert_eq!(tx.postings.len(), 2);
        assert_eq!(tx.postings[0].account, "Assets:Bank:Checking");
    }

    #[test]
    fn flagged_transaction_with_single_narration() {
        let input = "\
2024-01-15 ! \"Groceries\"
  Liabilities:CreditCard         -87.43 USD
  Expenses:Food                   87.43 USD
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!();
        };
        assert!(matches!(tx.state, TransactionState::Pending));
        assert!(tx.code.is_none(), "single-string header has no payee/code");
        assert_eq!(tx.description, "Groceries");
    }

    #[test]
    fn txn_keyword_is_uncleared() {
        let input = "\
2024-02-15 txn \"Apple lot purchase\"
  Assets:Brokerage              10 AAPL
  Assets:Bank:Checking       -1825.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!();
        };
        assert!(matches!(tx.state, TransactionState::Uncleared));
    }

    #[test]
    fn tags_and_links_collected_as_bare_tag_note() {
        let input = "\
2024-01-12 * \"Trip\" #vacation #beach ^trip-001
  Expenses:Travel    100.00 USD
  Assets:Bank:Checking
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!();
        };
        // The AST stores them as a single `:a:b:c:` note that resolve_metadata
        // lifts into Transaction.tags.
        assert!(
            tx.notes.iter().any(|n| n == ":vacation:beach:^trip-001:"),
            "expected a bare-tag note (link prefix preserved), got notes={:?}",
            tx.notes
        );
    }

    #[test]
    fn tags_and_links_land_in_resolved_transaction_tags() {
        // End-to-end: parse a Beancount transaction with #tag and ^link, run
        // it through resolution, and verify both kinds end up in
        // Transaction.tags (links collapse onto tags). This guards against the
        // earlier `tag:NAME` encoding which clobbered itself in the metadata
        // map for multi-tag transactions.
        let input = "\
2024-01-01 open Expenses:Travel
2024-01-01 open Assets:Bank:Checking USD

2024-01-12 * \"Trip\" #vacation #beach ^trip-001
  Expenses:Travel        100.00 USD
  Assets:Bank:Checking  -100.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let tx_entry = hir
            .entries
            .iter()
            .find(|e| matches!(e.data, crate::resolution::Entry::Transaction(_)))
            .expect("a transaction entry should be present");
        let crate::resolution::Entry::Transaction(ref tx) = tx_entry.data else {
            unreachable!();
        };
        let tags: std::collections::HashSet<&str> = tx.tags.iter().map(String::as_str).collect();
        assert!(tags.contains("vacation"), "tags={:?}", tx.tags);
        assert!(tags.contains("beach"), "tags={:?}", tx.tags);
        // Beancount `^link`s land in tags too, but with the `^` prefix
        // preserved so consumers can recover the link-vs-tag distinction.
        assert!(tags.contains("^trip-001"), "tags={:?}", tx.tags);
        // Nothing should have leaked into the metadata map.
        assert!(
            !tx.metadata.contains_key("tag"),
            "metadata leak: {:?}",
            tx.metadata
        );
        assert!(
            !tx.metadata.contains_key("link"),
            "metadata leak: {:?}",
            tx.metadata
        );
    }

    #[test]
    fn open_directive_becomes_account_directive() {
        let input = "2024-01-01 open Assets:Bank:Checking USD\n";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Directive(Directive::Account { name, notes, .. }) = &journal.entries[0] else {
            panic!("expected Account directive, got {:?}", journal.entries[0]);
        };
        assert_eq!(name, "Assets:Bank:Checking");
        assert!(notes.iter().any(|n| n.starts_with("currencies:")));
    }

    #[test]
    fn open_with_booking_method() {
        let input = "2024-01-01 open Assets:Brokerage AAPL,USD \"FIFO\"\n";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Directive(Directive::Account {
            name, notes, items, ..
        }) = &journal.entries[0]
        else {
            panic!();
        };
        assert_eq!(name, "Assets:Brokerage");
        let booking = items.iter().find_map(|i| match i {
            crate::ast::AccountItem::Booking(b) => Some(*b),
            _ => Option::None,
        });
        assert_eq!(booking, Some(crate::resolution::BookingMethod::Fifo));
        assert!(notes.iter().any(|n| n.contains("AAPL,USD")));
    }

    #[test]
    fn open_with_unknown_booking_method_falls_back_to_note() {
        // Unknown spellings should not panic the parser; preserve as a
        // free-form note so the raw text is still observable.
        let input = "2024-01-01 open Assets:Brokerage AAPL \"WACKY_METHOD\"\n";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Directive(Directive::Account { notes, items, .. }) = &journal.entries[0] else {
            panic!();
        };
        assert!(
            !items
                .iter()
                .any(|i| matches!(i, crate::ast::AccountItem::Booking(_)))
        );
        assert!(notes.iter().any(|n| n == "booking: WACKY_METHOD"));
    }

    #[test]
    fn close_directive_becomes_comment() {
        let input = "2024-12-31 close Liabilities:CreditCard\n";
        let journal = parse_beancount(input).expect("parse");
        assert!(matches!(journal.entries[0], Entry::Comment(_)));
    }

    #[test]
    fn commodity_directive_round_trip() {
        let input = "\
2024-01-01 commodity USD
  name: \"US Dollar\"
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Directive(Directive::Commodity { name, notes, .. }) = &journal.entries[0] else {
            panic!();
        };
        assert_eq!(name, "USD");
        assert!(notes.iter().any(|n| n.starts_with("name:")));
    }

    #[test]
    fn balance_directive_becomes_assertion() {
        let input = "2024-01-15 balance Assets:Bank:Checking 5000.00 USD\n";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Assertion(a) = &journal.entries[0] else {
            panic!();
        };
        assert_eq!(a.account, "Assets:Bank:Checking");
        assert_eq!(a.date.year, Some(2024));
        assert_eq!(a.date.month, 1);
        assert_eq!(a.date.date, 15);
        assert!(a.strict, "Beancount balance assertions are strict");
        match &a.amount {
            ValueExpr::Amount { value, commodity } => {
                assert_eq!(*value, dec!(5000.00));
                assert_eq!(commodity.as_deref(), Some("USD"));
            }
            other => panic!("unexpected amount shape: {other:?}"),
        }
    }

    #[test]
    fn price_directive_becomes_historical_price() {
        let input = "2024-02-15 price AAPL 182.50 USD\n";
        let journal = parse_beancount(input).expect("parse");
        let Entry::HistoricalPrice(hp) = &journal.entries[0] else {
            panic!();
        };
        assert_eq!(hp.commodity, "AAPL");
        assert_eq!(hp.date.year, Some(2024));
    }

    #[test]
    fn pad_directive_becomes_pad_marker() {
        let input = "2024-01-01 pad Assets:Bank:Checking Equity:Opening-Balances\n";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Pad(p) = &journal.entries[0] else {
            panic!("expected Pad marker, got {:?}", journal.entries[0]);
        };
        assert_eq!(p.target_account, "Assets:Bank:Checking");
        assert_eq!(p.source_account, "Equity:Opening-Balances");
    }

    #[test]
    fn note_document_event_query_custom_become_comments() {
        let input = "\
2024-01-20 note Assets:Bank:Checking \"Need to verify\"
2024-01-22 document Assets:Bank:Checking \"jan-statement.txt\"
2024-01-01 event \"location\" \"Berlin\"
2024-06-30 query \"q1\" \"SELECT date\"
2024-01-01 custom \"budget\" \"Expenses:Food\" 200.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        // All five preserve as comments (no semantic mapping yet).
        assert_eq!(
            journal.entries.len(),
            5,
            "five entries; got {}",
            journal.entries.len()
        );
        for e in &journal.entries {
            assert!(matches!(e, Entry::Comment(_)));
        }
    }

    #[test]
    fn empty_lot_annotation_parses_as_missing_cost() {
        // `{}` is Beancount's "MISSING cost" spec: a request to the
        // elaborator to apply the account's booking method against
        // the running inventory rather than spelling out a specific
        // lot key. The parser captures this as
        // `lot_annotation = Some(ann)` with `ann.cost = None` -- the
        // top-level `Some` distinguishes "annotation present" from
        // "no annotation at all", and the inner `cost: None`
        // distinguishes "MISSING cost" from "explicit cost".
        let input = "\
2024-03-15 * \"Sell with empty cost spec\"
  Assets:Brokerage   -5 AAPL {}
  Assets:Cash       1000 USD
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!();
        };
        let Some(AmountDetails::Amount { lot_annotation, .. }) = &tx.postings[0].amount else {
            panic!("expected Amount details");
        };
        let ann = lot_annotation.as_ref().expect("lot annotation present");
        assert!(ann.cost.is_none(), "empty {{}} should leave cost MISSING");
        assert!(ann.date.is_none());
        assert!(ann.note.is_none());
    }

    #[test]
    fn partial_lot_annotation_date_only_parses_as_missing_cost() {
        // `{2024-01-15}` is a partial cost spec: cost is MISSING, but
        // the booking method gets a date hint to narrow which lots
        // are eligible. Same shape as empty `{}` from the cost-MISSING
        // standpoint.
        let input = "\
2024-03-15 * \"Sell with date-only lot spec\"
  Assets:Brokerage   -5 AAPL {2024-01-15}
  Assets:Cash       1000 USD
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!();
        };
        let Some(AmountDetails::Amount { lot_annotation, .. }) = &tx.postings[0].amount else {
            panic!("expected Amount details");
        };
        let ann = lot_annotation.as_ref().expect("lot annotation present");
        assert!(
            ann.cost.is_none(),
            "date-only spec should leave cost MISSING"
        );
        assert_eq!(ann.date, chrono::NaiveDate::from_ymd_opt(2024, 1, 15));
    }

    #[test]
    fn lot_annotation_cost_date_label() {
        let input = "\
2024-02-15 * \"Apple lot purchase\"
  Assets:Brokerage   10 AAPL {182.50 USD, 2024-02-15, \"buy-2024-02\"}
  Assets:Bank:Checking       -1825.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!();
        };
        let Some(AmountDetails::Amount { lot_annotation, .. }) = &tx.postings[0].amount else {
            panic!("expected Amount details");
        };
        let ann = lot_annotation.as_ref().expect("lot annotation present");
        assert!(ann.cost.is_some(), "cost should be parsed");
        assert_eq!(
            ann.date,
            chrono::NaiveDate::from_ymd_opt(2024, 2, 15),
            "date should be parsed"
        );
        assert_eq!(ann.note.as_deref(), Some("buy-2024-02"));
    }

    #[test]
    fn double_brace_total_cost_lot_elaborates_to_per_unit() {
        // `{{1825 USD}}` declares the *total* lot cost; the elaborator
        // divides by 10 (the unit count) to derive a per-unit basis of
        // 182.50, which is what flows through the rest of the pipeline.
        // The transaction balances at $1825 cash either way.
        let total_form = "\
2024-01-01 open Assets:Brokerage USD
2024-01-01 open Assets:Bank:Checking USD

2024-02-15 * \"Apple lot purchase (total-cost form)\"
  Assets:Brokerage    10 AAPL {{1825.00 USD}}
  Assets:Bank:Checking      -1825.00 USD
";
        let per_unit_form = "\
2024-01-01 open Assets:Brokerage USD
2024-01-01 open Assets:Bank:Checking USD

2024-02-15 * \"Apple lot purchase (per-unit form)\"
  Assets:Brokerage    10 AAPL {182.50 USD}
  Assets:Bank:Checking      -1825.00 USD
";
        let total_elab = elaborate(total_form).expect("`{{total}}` should elaborate");
        let per_unit_elab = elaborate(per_unit_form).expect("`{cost}` should elaborate");

        // Both forms should produce identical elaborated lot cost on the
        // brokerage posting (the canonical per-unit form).
        let total_lot = total_elab.transactions[0].postings[0]
            .lot
            .as_ref()
            .expect("lot present (total form)");
        let per_unit_lot = per_unit_elab.transactions[0].postings[0]
            .lot
            .as_ref()
            .expect("lot present (per-unit form)");
        assert_eq!(
            total_lot, per_unit_lot,
            "total and per-unit forms should produce identical elaborated lots"
        );
    }

    /// Cost-spec arithmetic: parenthesised expressions inside the
    /// `{...}` lot annotation evaluate per the same Pratt parser the
    /// posting amount uses. Pin a few representative shapes so a
    /// future grammar / adapter refactor can't silently drop them.
    /// Probed against bean-check 3.2.0; same per-unit basis on both
    /// sides. Refs #185.
    #[test]
    fn lot_cost_parenthesised_addition() {
        // {(150 + 5) USD} → per-unit 155 USD; transaction balances at -1550.
        let input = "\
2024-01-01 open Assets:Brokerage USD
2024-01-01 open Assets:Bank:Checking USD

2024-02-15 * \"Buy with computed basis\"
  Assets:Brokerage    10 AAPL {(150.00 + 5.00) USD}
  Assets:Bank:Checking      -1550.00 USD
";
        let elab = elaborate(input).expect("parenthesised lot-cost arithmetic must elaborate");
        let lot = elab.transactions[0].postings[0]
            .lot
            .as_ref()
            .expect("lot present");
        let cost = lot.cost.as_ref().expect("cost present");
        let usd = cost
            .by_commodity
            .get("USD")
            .expect("USD cost present")
            .to_decimal();
        assert_eq!(
            usd,
            dec!(155.00),
            "per-unit basis from `(150 + 5) USD` should be 155.00; got {usd}"
        );
    }

    /// Cost-spec arithmetic: division derives the per-unit basis from
    /// a total. Equivalent to `{{1500 USD}}` for 10 units but written
    /// as `{1500/10 USD}` instead of the double-brace shorthand. Both
    /// must produce the same elaborated lot.
    #[test]
    fn lot_cost_division_matches_double_brace_total_form() {
        let arithmetic_form = "\
2024-01-01 open Assets:Brokerage USD
2024-01-01 open Assets:Bank:Checking USD

2024-02-15 * \"Buy with explicit divide\"
  Assets:Brokerage    10 AAPL {(1500.00 / 10) USD}
  Assets:Bank:Checking      -1500.00 USD
";
        let total_brace_form = "\
2024-01-01 open Assets:Brokerage USD
2024-01-01 open Assets:Bank:Checking USD

2024-02-15 * \"Buy with double-brace total\"
  Assets:Brokerage    10 AAPL {{1500.00 USD}}
  Assets:Bank:Checking      -1500.00 USD
";
        let lhs = elaborate(arithmetic_form).expect("explicit-divide lot must elaborate");
        let rhs = elaborate(total_brace_form).expect("double-brace lot must elaborate");
        let lhs_lot = lhs.transactions[0].postings[0]
            .lot
            .as_ref()
            .expect("lot present (arithmetic)");
        let rhs_lot = rhs.transactions[0].postings[0]
            .lot
            .as_ref()
            .expect("lot present (double-brace)");
        assert_eq!(
            lhs_lot, rhs_lot,
            "explicit-divide and double-brace forms should produce identical elaborated lots"
        );
    }

    /// Cost-spec arithmetic: nested parentheses + mixed operators.
    /// `((1500 + 50) / 10) USD` → per-unit 155.00.
    #[test]
    fn lot_cost_nested_parens_with_mixed_operators() {
        let input = "\
2024-01-01 open Assets:Brokerage USD
2024-01-01 open Assets:Bank:Checking USD

2024-02-15 * \"Buy with computed basis (commission included)\"
  Assets:Brokerage    10 AAPL {((1500.00 + 50.00) / 10) USD}
  Assets:Bank:Checking      -1550.00 USD
";
        let elab = elaborate(input).expect("nested-parens lot-cost must elaborate");
        let lot = elab.transactions[0].postings[0]
            .lot
            .as_ref()
            .expect("lot present");
        let cost = lot.cost.as_ref().expect("cost present");
        let usd = cost
            .by_commodity
            .get("USD")
            .expect("USD cost present")
            .to_decimal();
        assert_eq!(usd, dec!(155.00));
    }

    #[test]
    fn lot_price_at_per_unit() {
        let input = "\
2024-05-15 * \"Buy euros\"
  Assets:Cash:EUR    500.00 EUR @ 1.07 USD
  Assets:Bank:Checking
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!();
        };
        let Some(AmountDetails::Amount { lot_pricing, .. }) = &tx.postings[0].amount else {
            panic!();
        };
        assert!(matches!(lot_pricing, Some(LotPricing::Unit(_))));
    }

    #[test]
    fn arithmetic_amount_in_posting() {
        let input = "\
2024-03-01 *
  Expenses:Food    (50.00 + 50.00) USD
  Assets:Bank:Checking
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = &journal.entries[0] else {
            panic!();
        };
        let Some(AmountDetails::Amount { value, .. }) = &tx.postings[0].amount else {
            panic!();
        };
        assert!(
            matches!(value, ValueExpr::Typed { .. }),
            "expected Typed wrapping arithmetic, got {value:?}"
        );
    }

    #[test]
    fn sample_fixture_round_trips_through_resolution() {
        // The full parity fixture should walk through parse + resolution
        // without errors.
        let journal = parse_beancount(SAMPLE).expect("parse sample.beancount");
        assert!(!journal.entries.is_empty());
        let _hir: crate::resolution::HIR = journal.try_into().expect("resolve sample.beancount");
    }

    #[test]
    fn sample_fixture_round_trips_through_elaboration() {
        // End-to-end: parse + resolve + elaborate. The pad+balance pair
        // in the fixture (`pad Assets:Bank:Checking Equity:Opening-Balances`
        // followed by `balance Assets:Bank:Checking 5000.00 USD` after a
        // 3400 USD deposit on Jan 12) should reconcile cleanly: the pad
        // synthesizes a 1600 USD balancing transaction so the assertion
        // passes.
        let journal = parse_beancount(SAMPLE).expect("parse sample.beancount");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve sample.beancount");
        let elab: crate::elaboration::Journal = crate::elaborate(hir, &beancount_defaults())
            .expect("elaborate sample.beancount (pad+balance must reconcile)");
        // The synthesized pad transaction is tagged with metadata `pad: <source>`.
        let pad_tx_count = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .count();
        assert_eq!(
            pad_tx_count, 1,
            "exactly one synthesized pad transaction expected"
        );
    }

    #[test]
    fn frontend_extensions_lists_beancount() {
        use crate::frontend::Frontend;
        assert!(BeancountFrontend.extensions().contains(&"beancount"));
    }

    #[test]
    fn org_mode_outline_journal_parses_and_resolves() {
        // Beancount accepts journals edited as Org-mode outlines (Emacs
        // org-mode is the canonical case but the language itself permits
        // these lines anywhere -- they are top-level no-ops). The
        // headings round-trip through parse + resolution without
        // affecting the directives that follow.
        let input = "\
* Personal
** Bank accounts
2024-01-01 open Assets:Bank:Checking USD
*** Transactions
2024-01-12 * \"Salary\"
  Assets:Bank:Checking      3400.00 USD
  Income:Salary            -3400.00 USD
";
        let journal = parse_beancount(input).expect("parse with org-mode headings");

        // Three headings preserved as Entry::Comment (alongside the
        // open and the transaction).
        let comment_count = journal
            .entries
            .iter()
            .filter(|e| matches!(e, Entry::Comment(_)))
            .count();
        assert_eq!(
            comment_count, 3,
            "three org-mode headings preserved as comments, got entries={:?}",
            journal.entries
        );

        let open_count = journal
            .entries
            .iter()
            .filter(|e| matches!(e, Entry::Directive(Directive::Account { .. })))
            .count();
        assert_eq!(open_count, 1);
        let txn_count = journal
            .entries
            .iter()
            .filter(|e| matches!(e, Entry::Transaction(_)))
            .count();
        assert_eq!(txn_count, 1);

        // Resolution discards the headings (they are comments).
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let resolved_txn_count = hir
            .entries
            .iter()
            .filter(|e| matches!(e.data, crate::resolution::Entry::Transaction(_)))
            .count();
        assert_eq!(resolved_txn_count, 1);
    }

    #[test]
    fn transaction_string_with_embedded_escapes() {
        // The narration contains an escaped quote and an explicit newline
        // sequence. Both must (a) parse without terminating the string
        // early, and (b) be decoded into their actual characters in the
        // resulting Transaction.description.
        let input = "\
2024-01-01 open Expenses:Food
2024-01-01 open Assets:Cash USD

2024-03-10 * \"order #42 \\\"weekly\\\" delivery\\nnote: replace tomatoes\"
  Expenses:Food      12.34 USD
  Assets:Cash       -12.34 USD
";
        let journal = parse_beancount(input).expect("parse");
        let Entry::Transaction(tx) = journal
            .entries
            .iter()
            .find(|e| matches!(e, Entry::Transaction(_)))
            .expect("transaction present")
        else {
            unreachable!()
        };
        assert_eq!(
            tx.description, "order #42 \"weekly\" delivery\nnote: replace tomatoes",
            "string escapes should be decoded into actual characters"
        );
    }

    // -- pushtag/poptag, pushmeta/popmeta (#188 / #189) ---------------------

    fn resolved_tx<'a>(hir: &'a crate::resolution::HIR) -> Vec<&'a crate::resolution::Transaction> {
        hir.entries
            .iter()
            .filter_map(|e| match &e.data {
                crate::resolution::Entry::Transaction(t) => Some(t),
                _ => None,
            })
            .collect()
    }

    #[test]
    fn pushtag_scopes_tag_to_subsequent_transactions() {
        let input = "\
2024-01-01 open Expenses:Travel
2024-01-01 open Assets:Cash USD

pushtag #trip-2024

2024-06-15 * \"Hotel\"
  Expenses:Travel    100.00 USD
  Assets:Cash       -100.00 USD

2024-06-16 * \"Dinner\"
  Expenses:Travel     40.00 USD
  Assets:Cash        -40.00 USD

poptag #trip-2024

2024-07-01 * \"Outside the trip\"
  Expenses:Travel     20.00 USD
  Assets:Cash        -20.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let txs = resolved_tx(&hir);
        assert_eq!(txs.len(), 3);
        // First two transactions inherit the pushtag, third does not.
        assert!(
            txs[0].tags.iter().any(|t| t == "trip-2024"),
            "txs[0]={:?}",
            txs[0].tags
        );
        assert!(
            txs[1].tags.iter().any(|t| t == "trip-2024"),
            "txs[1]={:?}",
            txs[1].tags
        );
        assert!(
            !txs[2].tags.iter().any(|t| t == "trip-2024"),
            "third transaction must NOT have the popped tag, got: {:?}",
            txs[2].tags
        );
    }

    #[test]
    fn nested_pushtags_compose() {
        let input = "\
2024-01-01 open Expenses:Travel
2024-01-01 open Assets:Cash USD

pushtag #project-acme
pushtag #travel

2024-02-15 * \"Site visit\"
  Expenses:Travel    300.00 USD
  Assets:Cash       -300.00 USD

poptag #travel

2024-02-20 * \"Project meeting (no travel)\"
  Expenses:Travel     50.00 USD
  Assets:Cash        -50.00 USD

poptag #project-acme
";
        let journal = parse_beancount(input).expect("parse");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let txs = resolved_tx(&hir);
        assert_eq!(txs.len(), 2);
        // First transaction has BOTH active pushtags.
        let t0_tags: std::collections::HashSet<&str> =
            txs[0].tags.iter().map(String::as_str).collect();
        assert!(
            t0_tags.contains("project-acme"),
            "tx0 tags={:?}",
            txs[0].tags
        );
        assert!(t0_tags.contains("travel"), "tx0 tags={:?}", txs[0].tags);
        // Second has only `project-acme` -- `travel` was popped.
        let t1_tags: std::collections::HashSet<&str> =
            txs[1].tags.iter().map(String::as_str).collect();
        assert!(t1_tags.contains("project-acme"));
        assert!(!t1_tags.contains("travel"));
    }

    #[test]
    fn pushtag_unions_with_transaction_local_tags() {
        // The transaction's own #tags coexist with pushtags.
        let input = "\
2024-01-01 open Expenses:Food
2024-01-01 open Assets:Cash USD

pushtag #household

2024-03-10 * \"Groceries\" #weekly
  Expenses:Food      50.00 USD
  Assets:Cash       -50.00 USD

poptag #household
";
        let journal = parse_beancount(input).expect("parse");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let txs = resolved_tx(&hir);
        let tags: std::collections::HashSet<&str> =
            txs[0].tags.iter().map(String::as_str).collect();
        assert!(
            tags.contains("household"),
            "pushtag missing: {:?}",
            txs[0].tags
        );
        assert!(
            tags.contains("weekly"),
            "transaction-local tag missing: {:?}",
            txs[0].tags
        );
    }

    #[test]
    fn poptag_with_no_matching_push_is_silently_ignored() {
        // Beancount itself emits a warning on a mismatched pop; we just
        // ignore it (no warning channel today). The journal must still
        // parse and resolve cleanly.
        let input = "\
2024-01-01 open Expenses:Food
2024-01-01 open Assets:Cash USD

poptag #never-pushed

2024-03-10 * \"Lunch\"
  Expenses:Food      10.00 USD
  Assets:Cash       -10.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let txs = resolved_tx(&hir);
        assert_eq!(txs.len(), 1);
        assert!(
            txs[0].tags.is_empty(),
            "transaction should have no tags, got {:?}",
            txs[0].tags
        );
    }

    #[test]
    fn pushmeta_scopes_metadata_to_subsequent_transactions() {
        let input = "\
2024-01-01 open Expenses:Consulting
2024-01-01 open Assets:Cash USD

pushmeta project: \"acme-rebrand\"

2024-06-15 * \"Design review\"
  Expenses:Consulting   500.00 USD
  Assets:Cash          -500.00 USD

popmeta project:

2024-07-01 * \"Outside the project\"
  Expenses:Consulting   100.00 USD
  Assets:Cash          -100.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let txs = resolved_tx(&hir);
        assert_eq!(txs.len(), 2);
        // First inherits the pushmeta. The parser strips the outer
        // double-quotes on string-typed values so consumers see the
        // user-visible string, matching bean-check's `entry.meta`.
        assert_eq!(
            txs[0].metadata.get("project").map(String::as_str),
            Some("acme-rebrand"),
            "pushmeta missing on tx0: {:?}",
            txs[0].metadata
        );
        // Second does NOT have the popped metadata key.
        assert!(
            !txs[1].metadata.contains_key("project"),
            "popped metadata leaked: {:?}",
            txs[1].metadata
        );
    }

    #[test]
    fn pushmeta_most_recent_value_wins_for_same_key() {
        // Two pushes of the same key without an intervening pop -- the
        // later push wins.
        let input = "\
2024-01-01 open Expenses:Consulting
2024-01-01 open Assets:Cash USD

pushmeta phase: \"discovery\"
pushmeta phase: \"delivery\"

2024-08-15 * \"Sprint\"
  Expenses:Consulting   1000.00 USD
  Assets:Cash         -1000.00 USD
";
        let journal = parse_beancount(input).expect("parse");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let txs = resolved_tx(&hir);
        assert_eq!(
            txs[0].metadata.get("phase").map(String::as_str),
            Some("delivery"),
            "later pushmeta should win: {:?}",
            txs[0].metadata
        );
    }

    #[test]
    fn pushmeta_does_not_leak_into_transactions_before_the_push() {
        // Date-sort puts a Jan 5 transaction before a Mar 1 pushmeta even
        // when source order has the push first. Pushmeta is a *parse-time*
        // construct and must be applied at parse time -- the active set at
        // the moment a transaction is parsed is what the transaction inherits.
        // (Source order: open, push, txn -- so the txn DOES inherit.)
        let input = "\
2024-01-01 open Expenses:Food
2024-01-01 open Assets:Cash USD

pushmeta tag: \"taxable\"

2024-01-05 * \"Lunch\"
  Expenses:Food     10.00 USD
  Assets:Cash      -10.00 USD

popmeta tag:
";
        let journal = parse_beancount(input).expect("parse");
        let hir: crate::resolution::HIR = journal.try_into().expect("resolve");
        let txs = resolved_tx(&hir);
        assert_eq!(
            txs[0].metadata.get("tag").map(String::as_str),
            Some("taxable")
        );
    }

    // -- balance tolerance (#198) -------------------------------------------

    #[test]
    fn sub_tolerance_residual_is_absorbed_into_synthetic_account() {
        // 100.00 + (-100.005) = -0.005 USD. Tolerance for the
        // least-precise posting (scale 2) is 0.5 * 10^-2 = 0.005.
        // residual <= tolerance, so the elaborator synthesizes a
        // posting on the empty-string account that absorbs +0.005 USD.
        let input = "\
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Salary USD

2024-01-15 * \"Sub-tolerance residual\"
  Assets:Cash         100.00 USD
  Income:Salary      -100.005 USD
";
        let elab = elaborate(input).expect("sub-tolerance residual must elaborate");
        // Locate the synthesized rounding posting (account == "").
        let mut found = None;
        for tx in &elab.transactions {
            for p in &tx.postings {
                if p.account.is_empty() {
                    found = Some(p);
                }
            }
        }
        let p = found.expect("a synthesized empty-account posting should exist");
        let amt = p
            .amount
            .as_ref()
            .and_then(|a| a.by_commodity.get("USD"))
            .expect("rounding posting in USD");
        // 0.005 -> mantissa 5, scale 3, mantissa_high 0
        assert_eq!(
            (amt.mantissa_low, amt.mantissa_high, amt.scale),
            (5, 0, 3),
            "rounding posting should absorb +0.005 USD"
        );
    }

    #[test]
    fn over_tolerance_residual_still_errors() {
        // 100.00 + (-100.05) = -0.05 USD. Tolerance for scale-2 postings
        // is 0.005. 0.05 > 0.005 -> reject.
        let input = "\
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Salary USD

2024-01-15 * \"Over-tolerance residual\"
  Assets:Cash         100.00 USD
  Income:Salary      -100.05 USD
";
        let err = elaborate(input).expect_err("over-tolerance residual must reject");
        let msg = format!("{err:?}");
        assert!(
            msg.contains("TransactionDoesNotBalance"),
            "expected TransactionDoesNotBalance, got: {msg}"
        );
    }

    #[test]
    fn inferred_tolerance_default_option_overrides_per_commodity() {
        // Without the option directive, this 0.05 USD residual is over
        // tolerance and rejects (covered by `over_tolerance_residual_still_errors`).
        // With `option \"inferred_tolerance_default\" \"USD:0.1\"`, the
        // per-commodity override raises USD's tolerance to 0.1 absolute,
        // so the same 0.05 residual is now within tolerance.
        let input = "\
option \"inferred_tolerance_default\" \"USD:0.1\"

2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Salary USD

2024-01-15 * \"Larger residual within override\"
  Assets:Cash         100.00 USD
  Income:Salary      -100.05 USD
";
        let elab = elaborate(input).expect("USD override should accept the 0.05 residual");
        // Synthesized empty-account posting should absorb +0.05 USD.
        let mut found_amt: Option<rust_decimal::Decimal> = None;
        for tx in &elab.transactions {
            for p in &tx.postings {
                if p.account.is_empty() {
                    if let Some(amt) = p.amount.as_ref().and_then(|a| a.by_commodity.get("USD")) {
                        // Reconstruct decimal: mantissa / 10^scale (sign in mantissa_high).
                        let mantissa = (amt.mantissa_high as i128) << 64 | amt.mantissa_low as i128;
                        let d = rust_decimal::Decimal::from_i128_with_scale(mantissa, amt.scale);
                        found_amt = Some(d);
                    }
                }
            }
        }
        assert_eq!(
            found_amt,
            Some(rust_decimal::Decimal::new(5, 2)),
            "rounding posting should absorb +0.05 USD when override permits it"
        );
    }

    #[test]
    fn inferred_tolerance_default_does_not_apply_to_other_commodities() {
        // The override is per-commodity. A USD override doesn't widen
        // EUR tolerance.
        let input = "\
option \"inferred_tolerance_default\" \"USD:0.1\"

2024-01-01 open Assets:Cash:EUR EUR
2024-01-01 open Income:Salary:EUR EUR

2024-01-15 * \"EUR over default tolerance\"
  Assets:Cash:EUR         100.00 EUR
  Income:Salary:EUR      -100.05 EUR
";
        let err = elaborate(input).expect_err("USD override must not affect EUR");
        assert!(format!("{err:?}").contains("TransactionDoesNotBalance"));
    }

    // -- pad evaluator (#147) -----------------------------------------------

    fn elaborate(input: &str) -> Result<crate::elaboration::Journal, Box<dyn std::error::Error>> {
        let (journal, options) = parse_beancount_with_options(input)?;
        let mut hir: crate::resolution::HIR = journal.try_into()?;
        // Beancount's `option "inferred_tolerance_default"` populates
        // `tolerance_overrides` (per-commodity overrides; layer on top
        // of the config's `tolerance_mode` at elaboration time). The
        // base semantic config is `beancount_defaults()`.
        for (k, v) in &options {
            if k == "inferred_tolerance_default"
                && let Some((commodity, decimal)) = v.split_once(':')
                && let Ok(d) = decimal.trim().parse::<rust_decimal::Decimal>()
            {
                hir.global_context
                    .tolerance_overrides
                    .insert(commodity.trim().to_string(), d);
            }
        }
        Ok(crate::elaborate(hir, &beancount_defaults())?)
    }

    #[test]
    fn pad_fills_gap_to_next_balance_assertion() {
        // Pad on Jan 1, 3400 USD deposit on Jan 12, balance assertion on
        // Jan 15 expects 5000 USD. The pad must synthesize a 1600 USD
        // (= 5000 - 3400) balancing transaction back-dated to Jan 1.
        let input = "\
2024-01-01 open Assets:Bank:Checking USD
2024-01-01 open Income:Salary USD
2024-01-01 open Equity:Opening-Balances

2024-01-01 pad Assets:Bank:Checking Equity:Opening-Balances
2024-01-15 balance Assets:Bank:Checking 5000.00 USD

2024-01-12 *
  Assets:Bank:Checking      3400.00 USD
  Income:Salary            -3400.00 USD
";
        let elab = elaborate(input).expect("elaborate (pad+balance must reconcile)");
        let pad_txs: Vec<_> = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .collect();
        assert_eq!(pad_txs.len(), 1, "expected exactly one synthesized pad txn");
        let pad_tx = pad_txs[0];
        assert_eq!(
            pad_tx.metadata.get("pad").map(String::as_str),
            Some("Equity:Opening-Balances"),
            "pad provenance metadata should record the source account"
        );
        // Date is Jan 1 (pad's date), epoch days. NaiveDate::from_ymd(2024,1,1) -> 19723
        assert_eq!(
            pad_tx.date,
            chrono::NaiveDate::from_ymd_opt(2024, 1, 1)
                .unwrap()
                .signed_duration_since(chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap())
                .num_days() as i32,
            "synthesized pad txn should be back-dated to the pad directive"
        );
        assert_eq!(pad_tx.postings.len(), 2);
        // Target gets +1600, source gets -1600.
        let target = pad_tx
            .postings
            .iter()
            .find(|p| p.account == "Assets:Bank:Checking")
            .expect("target posting present");
        let source = pad_tx
            .postings
            .iter()
            .find(|p| p.account == "Equity:Opening-Balances")
            .expect("source posting present");
        let target_usd = target
            .amount
            .as_ref()
            .and_then(|a| a.by_commodity.get("USD"))
            .expect("target amount in USD");
        let source_usd = source
            .amount
            .as_ref()
            .and_then(|a| a.by_commodity.get("USD"))
            .expect("source amount in USD");
        // 1600.00 -> mantissa 160000, scale 2; mantissa_high == 0 (positive)
        assert_eq!(
            (
                target_usd.mantissa_low,
                target_usd.mantissa_high,
                target_usd.scale
            ),
            (160000, 0, 2),
            "target should receive +1600.00"
        );
        // -1600.00 -> sign-extended high half is -1
        assert_eq!(
            source_usd.mantissa_high, -1,
            "source amount should be negative"
        );
        assert_eq!(source_usd.scale, 2);
    }

    #[test]
    fn pad_without_following_balance_is_silently_dropped() {
        // No `balance` directive on Assets:Bank:Checking after the pad ->
        // pad is silently discarded; no synthesized transaction.
        let input = "\
2024-01-01 open Assets:Bank:Checking USD
2024-01-01 open Equity:Opening-Balances

2024-01-01 pad Assets:Bank:Checking Equity:Opening-Balances
";
        let elab = elaborate(input).expect("elaborate");
        let pad_txs = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .count();
        assert_eq!(
            pad_txs, 0,
            "pad without a follow-up balance assertion must be dropped, not error"
        );
    }

    #[test]
    fn most_recent_pad_wins_when_multiple_precede_one_balance() {
        // Two pads on the same target account before any balance assertion;
        // Beancount keeps only the most recent. The Jan 5 pad's source
        // (Equity:Late-Init) should be the synthesized txn's source, not
        // the Jan 1 pad's (Equity:Opening-Balances).
        let input = "\
2024-01-01 open Assets:Bank:Checking USD
2024-01-01 open Equity:Opening-Balances
2024-01-01 open Equity:Late-Init

2024-01-01 pad Assets:Bank:Checking Equity:Opening-Balances
2024-01-05 pad Assets:Bank:Checking Equity:Late-Init
2024-01-15 balance Assets:Bank:Checking 1000.00 USD
";
        let elab = elaborate(input).expect("elaborate");
        let pad_txs: Vec<_> = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .collect();
        assert_eq!(pad_txs.len(), 1, "exactly one synthesized pad txn");
        let pad_tx = pad_txs[0];
        assert_eq!(
            pad_tx.metadata.get("pad").map(String::as_str),
            Some("Equity:Late-Init"),
            "the most recent pad's source must be used"
        );
        // The synthesized txn's date matches the *winning* pad's date (Jan 5).
        assert_eq!(
            pad_tx.date,
            chrono::NaiveDate::from_ymd_opt(2024, 1, 5)
                .unwrap()
                .signed_duration_since(chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap())
                .num_days() as i32,
        );
    }

    /// A pad followed by two same-day assertions on different
    /// commodities synthesises a separate pad transaction per
    /// commodity. The pad is not consumed by the first assertion.
    /// See #220.
    #[test]
    fn pad_covers_every_asserted_commodity_on_same_target() {
        let input = "\
2024-01-01 open Assets:Cash
2024-01-01 open Equity:OpeningBalances

2024-01-02 pad  Assets:Cash  Equity:OpeningBalances

2024-01-15 balance  Assets:Cash    200 CAD
2024-01-15 balance  Assets:Cash    300 USD
";
        let elab = elaborate(input).expect("multi-commodity pad must reconcile");
        let pad_txs: Vec<_> = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .collect();
        assert_eq!(
            pad_txs.len(),
            2,
            "one pad txn per asserted commodity, expected 2"
        );
        // Each pad txn places its delta on the literal pad target.
        let cad_pad = pad_txs
            .iter()
            .find(|t| {
                t.postings
                    .iter()
                    .any(|p| p.account == "Assets:Cash" && p.amount_in("CAD").is_some())
            })
            .expect("CAD pad txn");
        let cad_target = cad_pad
            .postings
            .iter()
            .find(|p| p.account == "Assets:Cash")
            .unwrap();
        assert_eq!(cad_target.amount_in("CAD"), Some(dec!(200)));
        let usd_pad = pad_txs
            .iter()
            .find(|t| {
                t.postings
                    .iter()
                    .any(|p| p.account == "Assets:Cash" && p.amount_in("USD").is_some())
            })
            .expect("USD pad txn");
        let usd_target = usd_pad
            .postings
            .iter()
            .find(|p| p.account == "Assets:Cash")
            .unwrap();
        assert_eq!(usd_target.amount_in("USD"), Some(dec!(300)));
    }

    /// Repeating an assertion on an already-padded commodity is a
    /// no-op: the pad's `diff` is zero so no second pad transaction
    /// is synthesised. Pins the natural-no-op behaviour after the
    /// pad-not-consumed change for #220.
    #[test]
    fn pad_does_not_double_synthesise_for_repeated_commodity() {
        let input = "\
2024-01-01 open Assets:Cash
2024-01-01 open Equity:OpeningBalances

2024-01-02 pad  Assets:Cash  Equity:OpeningBalances

2024-01-15 balance  Assets:Cash    200 USD
2024-01-31 balance  Assets:Cash    200 USD
";
        let elab = elaborate(input).expect("repeated same-commodity assertion must not double-pad");
        let pad_txs = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .count();
        assert_eq!(
            pad_txs, 1,
            "exactly one pad txn for the single padded commodity"
        );
    }

    /// After a pad has fired for a commodity, a real direct posting on
    /// the target followed by a second balance assertion in the *same*
    /// commodity must FAIL if the new assertion doesn't match running.
    /// The pad must not silently re-fire and rescue the assertion.
    /// bean-check rejects this; doppio must too. Regression for the
    /// bug surfaced in PR #225 review.
    #[test]
    fn pad_does_not_re_fire_after_real_posting_in_same_commodity() {
        let input = "\
2024-01-01 open Assets:Cash USD
2024-01-01 open Equity:Open
2024-01-01 open Expenses:Food USD

2024-01-01 pad  Assets:Cash  Equity:Open

2024-01-15 balance  Assets:Cash    100 USD

2024-02-01 * \"Spend\"
  Assets:Cash    -50 USD
  Expenses:Food

2024-03-01 balance  Assets:Cash    25 USD
";
        let err = elaborate(input).expect_err(
            "second USD assertion must fail because the pad was consumed by the first; \
             a real posting between them moved running to 50, not 25",
        );
        let s = format!("{err:?}");
        assert!(
            s.contains("BalanceAssertionFailed"),
            "expected BalanceAssertionFailed, got: {s}"
        );
    }

    /// Per-commodity firing is independent: the EUR pad fires
    /// independently of the USD firing, even after a real EUR posting
    /// between pad and EUR balance. bean-check accepts this; doppio
    /// must match.
    #[test]
    fn pad_per_commodity_independent_of_other_commodity_postings() {
        let input = "\
2024-01-01 open Assets:Cash
2024-01-01 open Equity:Open
2024-01-01 open Equity:Other

2024-01-01 pad  Assets:Cash  Equity:Open

2024-01-15 balance  Assets:Cash    100 USD

2024-02-01 * \"Buy EUR\"
  Assets:Cash    50 EUR
  Equity:Other

2024-03-01 balance  Assets:Cash    200 EUR
";
        let elab = elaborate(input).expect(
            "EUR pad must fire independently; USD prior firing does not consume it for EUR",
        );
        let pad_txs: Vec<_> = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .collect();
        assert_eq!(
            pad_txs.len(),
            2,
            "one pad txn per fired commodity (USD, EUR)"
        );
    }

    #[test]
    fn pad_with_zero_gap_emits_no_transaction() {
        // If the running balance already equals the asserted amount, the
        // pad has nothing to fill -- no synthesized transaction.
        let input = "\
2024-01-01 open Assets:Bank:Checking USD
2024-01-01 open Income:Salary USD
2024-01-01 open Equity:Opening-Balances

2024-01-01 pad Assets:Bank:Checking Equity:Opening-Balances
2024-01-15 balance Assets:Bank:Checking 1000.00 USD

2024-01-10 *
  Assets:Bank:Checking      1000.00 USD
  Income:Salary            -1000.00 USD
";
        let elab = elaborate(input).expect("elaborate");
        let pad_txs = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .count();
        assert_eq!(
            pad_txs, 0,
            "pad whose gap is zero should not emit a synthesized transaction"
        );
    }

    // -- subtree balance / pad (#214) ---------------------------------------

    /// `balance` on a parent account aggregates the subtree: the parent
    /// has no direct postings, the child does, and the asserted amount
    /// equals the descendant's balance. Direct-only semantics would
    /// see 0 on the parent and reject; subtree semantics pass.
    #[test]
    fn balance_directive_aggregates_subtree() {
        let input = "\
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Salary USD
2024-01-01 open Income:Salary:Base USD

2024-06-01 * \"Salary\"
  Assets:Cash             100.00 USD
  Income:Salary:Base     -100.00 USD

2024-12-31 balance Income:Salary -100.00 USD
";
        elaborate(input).expect("balance on parent must reach the descendant's posting");
    }

    /// `pad` computes its corrective amount against the *subtree* sum
    /// of the next-balance account, not just the literal account's
    /// direct balance. The synthesized posting still lands on the
    /// literal pad target.
    #[test]
    fn pad_residual_uses_subtree_sum() {
        let input = "\
2024-01-01 open Equity:OpeningBalances
2024-01-01 open Assets:Bank USD
2024-01-01 open Assets:Bank:Checking USD
2024-01-01 open Assets:Bank:Savings USD

2024-01-02 pad Assets:Bank Equity:OpeningBalances

2024-01-15 * \"Existing\"
  Assets:Bank:Checking   100.00 USD
  Assets:Bank:Savings    200.00 USD
  Equity:OpeningBalances

2024-12-31 balance Assets:Bank 1000.00 USD
";
        let elab = elaborate(input).expect("subtree pad must reconcile against subtree sum");
        let pad_txs: Vec<_> = elab
            .transactions
            .iter()
            .filter(|t| t.metadata.contains_key("pad"))
            .collect();
        assert_eq!(pad_txs.len(), 1, "exactly one pad txn expected");
        let pad_tx = pad_txs[0];
        // Pad amount = 1000 - (100 + 200) = +700 on the parent account.
        let target = pad_tx
            .postings
            .iter()
            .find(|p| p.account == "Assets:Bank")
            .expect("synthesized posting must land on the literal pad target");
        assert_eq!(target.amount_in("USD"), Some(dec!(700.00)));
        let source = pad_tx
            .postings
            .iter()
            .find(|p| p.account == "Equity:OpeningBalances")
            .expect("synthesized source posting");
        assert_eq!(source.amount_in("USD"), Some(dec!(-700.00)));
    }

    /// Direct-balance assertion still works when the named account is
    /// itself the leaf (no descendants); the subtree reduces to a
    /// single account.
    #[test]
    fn balance_directive_on_leaf_is_unchanged() {
        let input = "\
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Salary USD

2024-06-01 * \"Salary\"
  Assets:Cash             100.00 USD
  Income:Salary          -100.00 USD

2024-12-31 balance Income:Salary -100.00 USD
";
        elaborate(input).expect("leaf-account assertion must still pass");
    }

    // ----------------------------------------------------------------------
    // Auto-booking tests (#238)
    // ----------------------------------------------------------------------

    /// FIFO booking: a `{}` reduction matches against the oldest lot
    /// in the inventory. The single MISSING-cost posting is replaced
    /// by one explicit-cost booked posting carrying the matched lot's
    /// cost basis.
    #[test]
    fn fifo_books_against_oldest_lot() {
        let input = "\
2024-01-01 open Assets:Brokerage \"FIFO\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-02-15 * \"Buy more\"
  Assets:Brokerage   10 GOLD {1600.00 USD}
  Assets:Cash       -16000.00 USD

2024-03-15 * \"Sell with empty cost spec\"
  Assets:Brokerage   -5 GOLD {} @ 1700.00 USD
  Assets:Cash       8500.00 USD
  Income:Trading
";
        let elab = elaborate(input).expect("FIFO booking should succeed");
        let sell_tx = elab
            .transactions
            .iter()
            .find(|t| t.description == "Sell with empty cost spec")
            .unwrap();
        let booked = sell_tx
            .postings
            .iter()
            .find(|p| p.account == "Assets:Brokerage")
            .expect("brokerage posting present");
        assert_eq!(booked.amount_in("GOLD"), Some(dec!(-5)));
        assert_eq!(
            booked.lot_cost_in("USD"),
            Some(dec!(1500.00)),
            "FIFO should match the oldest (1500 USD) lot"
        );
    }

    /// FIFO booking with a multi-lot reduction: `-15 GOLD {}` against
    /// 10 + 10 should split into one -10 GOLD {1500} posting and one
    /// -5 GOLD {1600} posting on the brokerage account.
    #[test]
    fn fifo_splits_multi_lot_reduction() {
        let input = "\
2024-01-01 open Assets:Brokerage \"FIFO\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-02-15 * \"Buy more\"
  Assets:Brokerage   10 GOLD {1600.00 USD}
  Assets:Cash       -16000.00 USD

2024-03-15 * \"Sell across two lots\"
  Assets:Brokerage   -15 GOLD {} @ 1700.00 USD
  Assets:Cash       25500.00 USD
  Income:Trading
";
        let elab = elaborate(input).expect("FIFO multi-lot booking should succeed");
        let sell_tx = elab
            .transactions
            .iter()
            .find(|t| t.description == "Sell across two lots")
            .unwrap();
        let brokerage_postings: Vec<_> = sell_tx
            .postings
            .iter()
            .filter(|p| p.account == "Assets:Brokerage")
            .collect();
        assert_eq!(
            brokerage_postings.len(),
            2,
            "expected two booked postings, one per matched lot"
        );
        // Order: oldest lot first.
        assert_eq!(brokerage_postings[0].amount_in("GOLD"), Some(dec!(-10)));
        assert_eq!(
            brokerage_postings[0].lot_cost_in("USD"),
            Some(dec!(1500.00))
        );
        assert_eq!(brokerage_postings[1].amount_in("GOLD"), Some(dec!(-5)));
        assert_eq!(
            brokerage_postings[1].lot_cost_in("USD"),
            Some(dec!(1600.00))
        );
    }

    /// LIFO booking: same fixture as FIFO but matches the most-recent
    /// lot first.
    #[test]
    fn lifo_books_against_newest_lot() {
        let input = "\
2024-01-01 open Assets:Brokerage \"LIFO\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-02-15 * \"Buy more\"
  Assets:Brokerage   10 GOLD {1600.00 USD}
  Assets:Cash       -16000.00 USD

2024-03-15 * \"Sell\"
  Assets:Brokerage   -5 GOLD {} @ 1700.00 USD
  Assets:Cash       8500.00 USD
  Income:Trading
";
        let elab = elaborate(input).expect("LIFO booking should succeed");
        let sell_tx = elab
            .transactions
            .iter()
            .find(|t| t.description == "Sell")
            .unwrap();
        let booked = sell_tx
            .postings
            .iter()
            .find(|p| p.account == "Assets:Brokerage")
            .expect("brokerage posting present");
        assert_eq!(
            booked.lot_cost_in("USD"),
            Some(dec!(1600.00)),
            "LIFO should match the newest (1600 USD) lot"
        );
    }

    /// HIFO booking: matches the highest-cost lot first.
    #[test]
    fn hifo_books_against_highest_cost_lot() {
        let input = "\
2024-01-01 open Assets:Brokerage \"HIFO\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-02-15 * \"Buy expensive\"
  Assets:Brokerage   10 GOLD {1700.00 USD}
  Assets:Cash       -17000.00 USD

2024-02-20 * \"Buy cheaper\"
  Assets:Brokerage   10 GOLD {1600.00 USD}
  Assets:Cash       -16000.00 USD

2024-03-15 * \"Sell\"
  Assets:Brokerage   -5 GOLD {} @ 1750.00 USD
  Assets:Cash       8750.00 USD
  Income:Trading
";
        let elab = elaborate(input).expect("HIFO booking should succeed");
        let sell_tx = elab
            .transactions
            .iter()
            .find(|t| t.description == "Sell")
            .unwrap();
        let booked = sell_tx
            .postings
            .iter()
            .find(|p| p.account == "Assets:Brokerage")
            .expect("brokerage posting present");
        assert_eq!(
            booked.lot_cost_in("USD"),
            Some(dec!(1700.00)),
            "HIFO should match the most-expensive (1700 USD) lot"
        );
    }

    /// STRICT + `{}` against multiple eligible lots is an
    /// AmbiguousLotMatch error.
    #[test]
    fn strict_rejects_ambiguous_empty_lot_spec() {
        let input = "\
2024-01-01 open Assets:Brokerage \"STRICT\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-02-15 * \"Buy more\"
  Assets:Brokerage   10 GOLD {1600.00 USD}
  Assets:Cash       -16000.00 USD

2024-03-15 * \"Ambiguous sell\"
  Assets:Brokerage   -5 GOLD {} @ 1700.00 USD
  Assets:Cash       8500.00 USD
  Income:Trading
";
        let err = elaborate(input).expect_err("STRICT should reject ambiguous {} reduction");
        assert!(
            format!("{err:?}").contains("AmbiguousLotMatch"),
            "expected AmbiguousLotMatch, got {err:?}"
        );
    }

    /// STRICT + `{2024-01-15}` (date hint narrowing to a single lot)
    /// resolves unambiguously and books that lot.
    #[test]
    fn strict_accepts_partial_spec_that_uniquely_resolves() {
        let input = "\
2024-01-01 open Assets:Brokerage \"STRICT\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-02-15 * \"Buy more\"
  Assets:Brokerage   10 GOLD {1600.00 USD}
  Assets:Cash       -16000.00 USD

2024-03-15 * \"Sell from January lot\"
  Assets:Brokerage   -5 GOLD {2024-01-15} @ 1700.00 USD
  Assets:Cash       8500.00 USD
  Income:Trading
";
        let elab = elaborate(input).expect("STRICT + date hint should resolve");
        let sell_tx = elab
            .transactions
            .iter()
            .find(|t| t.description == "Sell from January lot")
            .unwrap();
        let booked = sell_tx
            .postings
            .iter()
            .find(|p| p.account == "Assets:Brokerage")
            .expect("brokerage posting present");
        assert_eq!(booked.lot_cost_in("USD"), Some(dec!(1500.00)));
    }

    /// Cost-basis-aware gain inference (#242): a `{}` reduction
    /// balanced via `@price` with a null `Income:Trading` posting
    /// must fill the null with the gain (cost-basis residual), not
    /// with zero (the @price-derived residual). With FIFO matching
    /// 5 GOLD against the oldest lot (1500 USD), cost basis is -7500
    /// and cash is +8500, so Income:Trading absorbs -1000 USD.
    #[test]
    fn fifo_null_income_posting_fills_with_cost_basis_gain() {
        let input = "\
2024-01-01 open Assets:Brokerage \"FIFO\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-03-15 * \"Sell with empty cost spec\"
  Assets:Brokerage   -5 GOLD {} @ 1700.00 USD
  Assets:Cash       8500.00 USD
  Income:Trading
";
        let elab = elaborate(input).expect("gain inference should succeed");
        let sell_tx = elab
            .transactions
            .iter()
            .find(|t| t.description == "Sell with empty cost spec")
            .unwrap();
        let income = sell_tx
            .postings
            .iter()
            .find(|p| p.account == "Income:Trading")
            .expect("Income:Trading null posting present");
        assert_eq!(
            income.amount_in("USD"),
            Some(dec!(-1000.00)),
            "null Income:Trading should absorb the cost-basis gain (cash 8500 - cost basis 7500 = 1000)"
        );
    }

    /// FIFO multi-lot reduction with a null Income:Trading: bean-check
    /// fills it with -2500 (gain = 25500 cash - 23000 cost basis).
    /// doppio must match.
    #[test]
    fn fifo_multi_lot_null_income_fills_with_total_gain() {
        let input = "\
2024-01-01 open Assets:Brokerage \"FIFO\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy first lot\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-02-15 * \"Buy second lot\"
  Assets:Brokerage   10 GOLD {1600.00 USD}
  Assets:Cash       -16000.00 USD

2024-03-15 * \"Sell across two lots\"
  Assets:Brokerage   -15 GOLD {} @ 1700.00 USD
  Assets:Cash       25500.00 USD
  Income:Trading
";
        let elab = elaborate(input).expect("multi-lot gain inference should succeed");
        let sell_tx = elab
            .transactions
            .iter()
            .find(|t| t.description == "Sell across two lots")
            .unwrap();
        let income = sell_tx
            .postings
            .iter()
            .find(|p| p.account == "Income:Trading")
            .expect("Income:Trading null posting present");
        // 25500 cash - (10*1500 + 5*1600) = 25500 - 23000 = 2500 gain
        assert_eq!(income.amount_in("USD"), Some(dec!(-2500.00)));
    }

    /// NONE booking bypasses the booking pass entirely; the posting
    /// is recorded with cost=None even when other lots exist.
    #[test]
    fn none_booking_passes_through_unchanged() {
        let input = "\
2024-01-01 open Assets:Brokerage \"NONE\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-03-15 * \"Sell with no booking\"
  Assets:Brokerage   -5 GOLD {} @ 1700.00 USD
  Assets:Cash       8500.00 USD
  Income:Trading
";
        let elab = elaborate(input).expect("NONE booking accepts the posting");
        let sell_tx = elab
            .transactions
            .iter()
            .find(|t| t.description == "Sell with no booking")
            .unwrap();
        let booked = sell_tx
            .postings
            .iter()
            .find(|p| p.account == "Assets:Brokerage")
            .expect("brokerage posting present");
        // Under NONE booking, the user's `{}` is preserved as a
        // cost-MISSING lot annotation rather than rewritten by the
        // booking pass. The end result is functionally equivalent to
        // a bare reduction.
        assert_eq!(booked.amount_in("GOLD"), Some(dec!(-5)));
        assert_eq!(
            booked.lot_cost_in("USD"),
            None,
            "NONE should leave cost MISSING"
        );
    }

    /// Over-reduction: a `{}` reduction asking for more units than
    /// the inventory holds.
    #[test]
    fn over_reduction_in_booking_errors() {
        let input = "\
2024-01-01 open Assets:Brokerage \"FIFO\"
2024-01-01 open Assets:Cash USD
2024-01-01 open Income:Trading

2024-01-15 * \"Buy\"
  Assets:Brokerage   10 GOLD {1500.00 USD}
  Assets:Cash       -15000.00 USD

2024-03-15 * \"Sell more than we have\"
  Assets:Brokerage   -25 GOLD {} @ 1700.00 USD
  Assets:Cash       42500.00 USD
  Income:Trading
";
        let err = elaborate(input).expect_err("over-reduction should error");
        assert!(
            format!("{err:?}").contains("OverReductionInBooking"),
            "expected OverReductionInBooking, got {err:?}"
        );
    }

    /// Augmenting posting with `{}` (positive units, MISSING cost)
    /// is not supported in the first cut.
    #[test]
    fn augmenting_posting_with_missing_cost_errors() {
        let input = "\
2024-01-01 open Assets:Brokerage \"FIFO\"
2024-01-01 open Assets:Cash USD

2024-03-15 * \"Buy with empty cost spec\"
  Assets:Brokerage    5 GOLD {} @ 1700.00 USD
  Assets:Cash       -8500.00 USD
";
        let err = elaborate(input).expect_err("augmenting + MISSING should error");
        assert!(
            format!("{err:?}").contains("AugmentingPostingWithMissingCost"),
            "expected AugmentingPostingWithMissingCost, got {err:?}"
        );
    }
}