patchloom 0.31.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
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
// ── doc module tests ──────────────────────────────────────────────
use crate::ops::doc::*;
use crate::selector;
use serde_json::json;

mod basic {
    use super::*;

    #[test]
    fn detect_format_json() {
        assert!(matches!(
            detect_format("config.json").unwrap(),
            FileFormat::Json
        ));
    }

    #[test]
    fn detect_format_yaml() {
        assert!(matches!(
            detect_format("config.yaml").unwrap(),
            FileFormat::Yaml
        ));
        assert!(matches!(
            detect_format("config.yml").unwrap(),
            FileFormat::Yaml
        ));
    }

    #[test]
    fn detect_format_toml() {
        assert!(matches!(
            detect_format("Cargo.toml").unwrap(),
            FileFormat::Toml
        ));
    }

    #[test]
    fn yaml_merge_keys_resolved() {
        let yaml = "defaults: &d\n  timeout: 30\n  retries: 3\nstaging:\n  <<: *d\n";
        let val = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        assert_eq!(val["staging"]["retries"], json!(3));
        assert_eq!(val["staging"]["timeout"], json!(30));
        // The merge key itself must be removed.
        assert!(val["staging"].get("<<").is_none());
    }

    #[test]
    fn parse_and_serialize_json_roundtrip() {
        let input = "{\n  \"a\": 1\n}\n";
        let val = parse_doc(input, &FileFormat::Json).unwrap();
        assert_eq!(val, json!({"a": 1}));
        let out = serialize_value(&val, &FileFormat::Json).unwrap();
        assert_eq!(out, input);
    }

    #[test]
    fn parse_empty_json_is_empty_object() {
        assert_eq!(parse_doc("", &FileFormat::Json).unwrap(), json!({}));
        assert_eq!(
            parse_doc("   \n\t  ", &FileFormat::Json).unwrap(),
            json!({})
        );
    }

    #[test]
    fn parse_and_serialize_yaml_roundtrip() {
        let input = "a: 1\n";
        let val = parse_doc(input, &crate::ops::doc::FileFormat::Yaml).unwrap();
        assert_eq!(val, json!({"a": 1}));
        let out = serialize_value(&val, &crate::ops::doc::FileFormat::Yaml).unwrap();
        assert_eq!(out, input);
    }

    #[test]
    fn parse_and_serialize_toml_roundtrip() {
        let input = "a = 1\n";
        let val = parse_doc(input, &FileFormat::Toml).unwrap();
        assert_eq!(val, json!({"a": 1}));
        // TOML pretty serialization may differ slightly; just ensure it parses back
        let out = serialize_value(&val, &FileFormat::Toml).unwrap();
        let reparsed = parse_doc(&out, &FileFormat::Toml).unwrap();
        assert_eq!(reparsed, json!({"a": 1}));
    }

    #[test]
    fn navigate_mut_existing_key() {
        let mut val = json!({"a": {"b": 42}});
        let seg = crate::selector::parse("a.b").unwrap();
        let found = navigate_mut(&mut val, &seg, false, "doc.set").unwrap();
        assert_eq!(*found, json!(42));
    }

    #[test]
    fn navigate_mut_create_missing_key() {
        let mut val = json!({"a": 1});
        let seg = crate::selector::parse("b.c").unwrap();
        let found = navigate_mut(&mut val, &seg, true, "doc.set").unwrap();
        // created as empty object, then descended into "c" which was also created
        assert!(found.is_object());
    }

    #[test]
    fn navigate_mut_array_index() {
        let mut val = json!({"items": [10, 20, 30]});
        let seg = crate::selector::parse("items[1]").unwrap();
        let found = navigate_mut(&mut val, &seg, false, "doc.set").unwrap();
        assert_eq!(*found, json!(20));
    }

    #[test]
    fn deep_merge_objects() {
        let mut base = json!({"a": 1, "b": {"c": 2}});
        let other = json!({"b": {"d": 3}, "e": 4});
        deep_merge(&mut base, &other);
        assert_eq!(base, json!({"a": 1, "b": {"c": 2, "d": 3}, "e": 4}));
    }

    #[test]
    fn set_at_path_simple_key() {
        let mut root = json!({"a": 1});
        let sel = crate::selector::parse("b").unwrap();
        set_at_path(&mut root, &sel, json!(2)).unwrap();
        assert_eq!(root, json!({"a": 1, "b": 2}));
    }

    #[test]
    fn set_at_path_nested_creates_intermediates() {
        let mut root = json!({});
        let sel = crate::selector::parse("a.b.c").unwrap();
        set_at_path(&mut root, &sel, json!("deep")).unwrap();
        assert_eq!(root, json!({"a": {"b": {"c": "deep"}}}));
    }

    #[test]
    fn set_at_path_array_index() {
        let mut root = json!({"items": [10, 20, 30]});
        let sel = crate::selector::parse("items[1]").unwrap();
        set_at_path(&mut root, &sel, json!(99)).unwrap();
        assert_eq!(root, json!({"items": [10, 99, 30]}));
    }

    // Happy-path delete_where / delete_at_selector / update_matching wildcard
    // live in navigate.rs co-located tests (single home; avoid dual names).

    #[test]
    fn delete_at_selector_array_index() {
        let mut root = json!({"items": [10, 20, 30]});
        let sel = crate::selector::parse("items[1]").unwrap();
        assert!(delete_at_selector(&mut root, &sel).unwrap());
        assert_eq!(root, json!({"items": [10, 30]}));
    }

    #[test]
    fn move_at_path_renames_key() {
        let mut root = json!({"old_name": "value", "other": 1});
        let from = crate::selector::parse("old_name").unwrap();
        let to = crate::selector::parse("new_name").unwrap();
        move_at_path(&mut root, &from, &to).unwrap();
        assert_eq!(root, json!({"other": 1, "new_name": "value"}));
    }

    #[test]
    fn move_at_path_to_nested_creates_intermediates() {
        let mut root = json!({"src": 42});
        let from = crate::selector::parse("src").unwrap();
        let to = crate::selector::parse("a.b.dst").unwrap();
        move_at_path(&mut root, &from, &to).unwrap();
        assert_eq!(root, json!({"a": {"b": {"dst": 42}}}));
    }

    #[test]
    fn update_matching_by_key() {
        let mut val = json!({"a": {"b": "old"}});
        let seg = crate::selector::parse("a.b").unwrap();
        let count = update_matching(&mut val, &seg, &json!("new")).unwrap();
        assert_eq!(count, 1);
        assert_eq!(val, json!({"a": {"b": "new"}}));
    }

    // update_matching_wildcard: see navigate.rs co-located tests.

    #[test]
    fn update_matching_predicate() {
        let mut val = json!({"items": [
            {"name": "a", "v": 1},
            {"name": "b", "v": 2}
        ]});
        let seg = crate::selector::parse("items[name=b].v").unwrap();
        let count = update_matching(&mut val, &seg, &json!(42)).unwrap();
        assert_eq!(count, 1);
        assert_eq!(val["items"][1]["v"], json!(42));
        // First item unchanged
        assert_eq!(val["items"][0]["v"], json!(1));
    }

    #[test]
    fn toml_deep_nested_table_creation() {
        let toml = "# header\n[app]\nname = \"x\"\n";
        let old = parse_doc(toml, &FileFormat::Toml).unwrap();
        let mut newv = old.clone();
        set_at_path(
            &mut newv,
            &crate::selector::parse("app.server.tls.port").unwrap(),
            json!(9443),
        )
        .unwrap();
        let result = serialize_value_preserving(toml, &old, &newv, &FileFormat::Toml).unwrap();
        let reparsed: serde_json::Value = toml_edit::de::from_str(&result).expect("valid");
        assert_eq!(
            reparsed,
            json!({"app":{"name":"x","server":{"tls":{"port":9443}}}})
        );
        assert!(result.contains("# header"));
    }

    #[test]
    fn mutation_set() {
        let mut root = json!({"a": 1});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Set {
                selector: "b".into(),
                value: json!(2),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Applied));
        assert_eq!(root, json!({"a": 1, "b": 2}));
    }

    #[test]
    fn mutation_delete_existing() {
        let mut root = json!({"a": 1, "b": 2});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Delete {
                selector: "b".into(),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Removed(1)));
        assert_eq!(root, json!({"a": 1}));
    }

    #[test]
    fn mutation_merge() {
        let mut root = json!({"a": 1});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Merge {
                selector: None,
                value: json!({"b": 2}),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Applied));
        assert_eq!(root, json!({"a": 1, "b": 2}));
    }

    #[test]
    fn mutation_update_array_root_bare_key_is_type_error() {
        let mut root = json!([{"tags": ["a"]}, {"tags": ["b"]}]);
        let original = root.clone();
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Update {
                selector: "tags".into(),
                value: json!(["z"]),
            },
        )
        .unwrap();
        match result {
            MutationResult::TypeError(msg) => {
                assert!(
                    msg.contains("0.tags") || msg.contains("[0].tags"),
                    "expected multi-doc hint, got: {msg}"
                );
            }
            other => panic!("expected TypeError, got {other:?}"),
        }
        assert_eq!(root, original);
    }

    #[test]
    fn mutation_append_array_root_bare_key_is_type_error() {
        let mut root = json!([{"tags": ["a"]}, {"tags": ["b"]}]);
        let err = apply_doc_mutation(
            &mut root,
            DocMutation::Append {
                selector: "tags".into(),
                value: json!("z"),
            },
        )
        .unwrap_err();
        assert!(
            crate::exit::is_type_error(&err),
            "expected type_error, got: {err}"
        );
        let msg = err.to_string();
        assert!(
            msg.contains("0.tags") || msg.contains("[0].tags"),
            "expected multi-doc hint, got: {msg}"
        );
    }

    #[test]
    fn mutation_merge_array_root_with_object_is_type_error() {
        // Multi-doc YAML is modeled as a top-level array. Merging an object
        // overlay must not replace the entire stream (silent data loss).
        let mut root = json!([{"a": 1}, {"b": 2}]);
        let original = root.clone();
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Merge {
                selector: None,
                value: json!({"c": 3}),
            },
        )
        .unwrap();
        match result {
            MutationResult::TypeError(msg) => {
                assert!(
                    msg.contains("top-level array") || msg.contains("multi-document"),
                    "expected multi-doc guidance, got: {msg}"
                );
            }
            other => panic!("expected TypeError, got {other:?}"),
        }
        assert_eq!(root, original, "root must be unchanged on type error");
    }

    #[test]
    fn mutation_merge_into_multi_doc_element_via_selector() {
        let mut root = json!([{"a": 1}, {"b": 2}]);
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Merge {
                selector: Some("0".into()),
                value: json!({"c": 3}),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Applied));
        assert_eq!(root, json!([{"a": 1, "c": 3}, {"b": 2}]));
    }

    #[test]
    fn mutation_merge_array_root_with_array_is_type_error() {
        // Array overlay also fully replaces via deep_merge (sibling of #1872).
        // Found by MPI: object overlay was refused; array overlay still applied.
        let mut root = json!([{"a": 1}, {"b": 2}]);
        let original = root.clone();
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Merge {
                selector: None,
                value: json!([{"a": 9}, {"b": 9}]),
            },
        )
        .unwrap();
        match result {
            MutationResult::TypeError(msg) => {
                assert!(
                    msg.contains("top-level array") || msg.contains("multi-document"),
                    "expected multi-doc guidance, got: {msg}"
                );
            }
            other => panic!("expected TypeError, got {other:?}"),
        }
        assert_eq!(root, original, "root must be unchanged on type error");
    }

    #[test]
    fn mutation_append_to_array() {
        let mut root = json!({"items": [1, 2]});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Append {
                selector: "items".into(),
                value: json!(3),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Applied));
        assert_eq!(root["items"], json!([1, 2, 3]));
    }

    #[test]
    fn mutation_prepend_to_array() {
        let mut root = json!({"items": [2, 3]});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Prepend {
                selector: "items".into(),
                value: json!(1),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Applied));
        assert_eq!(root["items"], json!([1, 2, 3]));
    }

    #[test]
    fn mutation_update_matching() {
        let mut root = json!({"items": [{"id": 1, "v": "a"}, {"id": 2, "v": "b"}]});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Update {
                selector: "items[id=1]".into(),
                value: json!({"id": 1, "v": "updated"}),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Applied));
        assert_eq!(root["items"][0]["v"], "updated");
    }

    #[test]
    fn mutation_move_key() {
        let mut root = json!({"src": 42, "dst": {}});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Move {
                from: "src".into(),
                to: "dst.val".into(),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Applied));
        assert_eq!(root, json!({"dst": {"val": 42}}));
    }

    #[test]
    fn mutation_ensure_missing() {
        let mut root = json!({"a": 1});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Ensure {
                selector: "b".into(),
                value: json!(2),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Applied));
        assert_eq!(root, json!({"a": 1, "b": 2}));
    }

    #[test]
    fn mutation_delete_where_matching() {
        let mut root = json!({"items": [{"k": "a"}, {"k": "b"}]});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::DeleteWhere {
                selector: "items".into(),
                predicate: "k=a".into(),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::Removed(1)));
        assert_eq!(root["items"].as_array().unwrap().len(), 1);
    }

    #[test]
    fn parse_value_bare_string() {
        assert_eq!(parse_value("hello"), json!("hello"));
    }

    #[test]
    fn parse_value_integer() {
        assert_eq!(parse_value("42"), json!(42));
    }

    #[test]
    fn parse_value_bool() {
        assert_eq!(parse_value("true"), json!(true));
    }

    #[test]
    fn parse_value_null() {
        assert_eq!(parse_value("null"), serde_json::Value::Null);
    }

    #[test]
    fn parse_value_json_object() {
        assert_eq!(parse_value(r#"{"a":1}"#), json!({"a": 1}));
    }

    #[test]
    fn parse_value_false_is_bool() {
        let result = parse_value("false");
        assert_eq!(result, json!(false));
        assert!(result.is_boolean());
    }

    #[test]
    fn flatten_value_nested() {
        let val = json!({"a": {"b": 1}, "c": [2, 3]});
        let mut buf = String::new();
        let mut out = Vec::new();
        flatten_value(&val, &mut buf, &mut out);
        let paths: Vec<&str> = out.iter().map(|(p, _)| p.as_str()).collect();
        assert!(paths.contains(&"a.b"));
        assert!(paths.contains(&"c[0]"));
        assert!(paths.contains(&"c[1]"));
    }

    #[test]
    fn flatten_value_quotes_dot_keys() {
        // Regression: keys containing '.' were unquoted, making "a.b" (single
        // key) indistinguishable from nested a -> b.
        let val = json!({"a.b": 1, "normal": {"nested": 2}});
        let mut buf = String::new();
        let mut out = Vec::new();
        flatten_value(&val, &mut buf, &mut out);
        let paths: Vec<&str> = out.iter().map(|(p, _)| p.as_str()).collect();
        // Dot-key should be quoted
        assert!(
            paths.contains(&r#""a.b""#),
            "dot-containing key should be quoted: {paths:?}"
        );
        // Normal nested path should NOT be quoted
        assert!(paths.contains(&"normal.nested"));
    }

    #[test]
    fn diff_values_detects_changes() {
        let a = json!({"x": 1, "y": 2});
        let b = json!({"x": 1, "y": 3, "z": 4});
        let mut buf = String::new();
        let mut out = Vec::new();
        diff_values(&a, &b, &mut buf, &mut out);
        assert_eq!(out.len(), 2); // y changed, z added
        assert!(out.iter().any(|e| e.path == "y" && e.kind == "changed"));
        assert!(out.iter().any(|e| e.path == "z" && e.kind == "added"));
    }

    #[test]
    fn yaml_plain_strings_do_not_need_quoting() {
        assert!(!needs_yaml_quoting("hello"));
        assert!(!needs_yaml_quoting("foo-bar"));
        assert!(!needs_yaml_quoting("some_value_123"));
        assert!(!needs_yaml_quoting("v1.2.3"));
    }
}

mod edge_cases {
    use super::*;

    #[test]
    fn yaml_merge_key_existing_wins() {
        let yaml = "base: &b\n  x: 1\nchild:\n  <<: *b\n  x: 99\n";
        let val = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        assert_eq!(val["child"]["x"], json!(99));
    }

    #[test]
    fn yaml_merge_key_multiple() {
        let yaml = "a: &a\n  x: 1\nb: &b\n  y: 2\nc:\n  <<:\n    - *a\n    - *b\n";
        let val = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        assert_eq!(val["c"]["x"], json!(1));
        assert_eq!(val["c"]["y"], json!(2));
    }

    #[test]
    fn deep_merge_overwrites_non_object() {
        let mut base = json!({"a": "string"});
        let other = json!({"a": {"nested": true}});
        deep_merge(&mut base, &other);
        assert_eq!(base, json!({"a": {"nested": true}}));
    }

    #[test]
    fn delete_where_no_match_returns_zero() {
        let mut root = json!({"items": [{"name": "a"}]});
        let sel = crate::selector::parse("items").unwrap();
        let removed = delete_where(&mut root, &sel, "name=zzz").unwrap();
        assert_eq!(removed, 0);
    }

    /// Agents often emit `value=X` for scalar arrays (LLM prior); treat as
    /// element match when items are not objects with a `value` field.
    #[test]
    fn delete_where_value_alias_matches_scalar_elements() {
        let mut root = json!({"tags": ["a", "b", "a"]});
        let sel = crate::selector::parse("tags").unwrap();
        let removed = delete_where(&mut root, &sel, "value=a").unwrap();
        assert_eq!(removed, 2);
        assert_eq!(root["tags"], json!(["b"]));
    }

    /// Object arrays with a real `value` field still match that field.
    #[test]
    fn delete_where_value_key_matches_object_field() {
        let mut root = json!({"items": [{"value": "a"}, {"value": "b"}]});
        let sel = crate::selector::parse("items").unwrap();
        let removed = delete_where(&mut root, &sel, "value=a").unwrap();
        assert_eq!(removed, 1);
        assert_eq!(root["items"], json!([{"value": "b"}]));
    }

    #[test]
    fn delete_where_trims_whitespace() {
        let mut root = json!({"items": [{"name": "a"}, {"name": "b"}]});
        let sel = crate::selector::parse("items").unwrap();
        // Spaces around key and value should be trimmed.
        let removed = delete_where(&mut root, &sel, " name = a ").unwrap();
        assert_eq!(removed, 1);
        assert_eq!(root["items"].as_array().unwrap().len(), 1);
        assert_eq!(root["items"][0]["name"], "b");
    }

    // delete_at_selector_missing_returns_false: see navigate.rs co-located tests.

    #[test]
    fn move_at_path_to_array_index() {
        let mut root = json!({"src": "x", "arr": [1, 2, 3]});
        let from = crate::selector::parse("src").unwrap();
        let to = crate::selector::parse("arr[1]").unwrap();
        move_at_path(&mut root, &from, &to).unwrap();
        let arr = root["arr"].as_array().unwrap();
        assert_eq!(arr.len(), 4);
        assert_eq!(arr[1], json!("x"));
    }

    #[test]
    fn update_matching_chained_predicate_updates_row() {
        let mut val = json!({
            "data": [
                {"type": "server", "port": 9000},
                {"type": "web", "port": 80}
            ]
        });
        let seg = crate::selector::parse("data[type=server][port>8000].port").unwrap();
        let count = update_matching(&mut val, &seg, &json!(443)).unwrap();
        assert_eq!(count, 1);
        assert_eq!(val["data"][0]["port"], json!(443));
        assert_eq!(val["data"][1]["port"], json!(80));
    }

    #[test]
    fn update_matching_not_on_object_replaces_record() {
        let mut val = json!({"item": {"name": "a"}});
        let seg = crate::selector::parse("item[!deprecated]").unwrap();
        let count = update_matching(&mut val, &seg, &json!({"name": "b"})).unwrap();
        assert_eq!(count, 1);
        assert_eq!(val, json!({"item": {"name": "b"}}));
    }

    #[test]
    fn update_matching_non_numeric_field_is_invalid_input() {
        let mut val = json!({"servers": [{"port": "abc"}]});
        let seg = crate::selector::parse("servers[port>8000]").unwrap();
        let err = update_matching(&mut val, &seg, &json!(1)).unwrap_err();
        assert!(
            crate::api::is_invalid_input(&err),
            "expected invalid_input, got {err}"
        );
    }

    #[test]
    fn update_matching_missing_key_returns_zero() {
        let mut val = json!({"a": 1});
        let seg = crate::selector::parse("b.c").unwrap();
        let count = update_matching(&mut val, &seg, &json!("x")).unwrap();
        assert_eq!(count, 0);
    }

    #[test]
    fn yaml_sequence_root_mutation_not_lost() {
        let yaml = "- item1\n- item2\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new.as_array_mut().unwrap().push(json!("item3"));

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert!(result.contains("item3"), "appended item missing: {result}");
        assert!(result.contains("item1"), "item1 missing: {result}");
        assert!(result.contains("item2"), "item2 missing: {result}");
    }

    #[test]
    fn yaml_mapping_to_scalar_root_type_change_not_lost() {
        let yaml = "name: app\nversion: 1\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let new = json!("overridden");

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert!(
            result.contains("overridden"),
            "root type change lost: {result}"
        );
    }

    #[test]
    fn yaml_mapping_to_array_root_type_change_not_lost() {
        let yaml = "name: app\nversion: 1\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let new = json!(["a", "b"]);

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert!(result.contains("- a"), "array item '- a' missing: {result}");
        assert!(result.contains("- b"), "array item '- b' missing: {result}");
    }

    #[test]
    fn yaml_single_document_marker_accepted() {
        // A single document with an explicit `---` marker is valid and parses fine.
        let yaml = "---\nname: only\n";
        let val = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        assert_eq!(val, json!({"name": "only"}));

        let mut new = val.clone();
        new["name"] = json!("updated");
        let result = serialize_value_preserving(yaml, &val, &new, &FileFormat::Yaml).unwrap();
        let reparsed: serde_json::Value = serde_yaml_ng::from_str(&result).unwrap();
        assert_eq!(reparsed, json!({"name": "updated"}));
    }

    #[test]
    fn mutation_delete_missing() {
        let mut root = json!({"a": 1});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Delete {
                selector: "z".into(),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::NoMatch));
    }

    #[test]
    fn mutation_update_no_match() {
        let mut root = json!({"items": [{"id": 1}]});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Update {
                selector: "items[id=99]".into(),
                value: json!({"id": 99}),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::NoMatch));
    }

    #[test]
    fn mutation_ensure_existing() {
        let mut root = json!({"a": 1});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Ensure {
                selector: "a".into(),
                value: json!(99),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::AlreadyExists));
        assert_eq!(root["a"], 1); // unchanged
    }

    #[test]
    fn mutation_delete_where_no_match() {
        let mut root = json!({"items": [{"k": "a"}]});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::DeleteWhere {
                selector: "items".into(),
                predicate: "k=z".into(),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::NoMatch));
    }

    #[test]
    fn yaml_empty_string_needs_quoting() {
        assert!(needs_yaml_quoting(""));
    }

    // -- parse_value edge cases (#978) -----------------------------------

    #[test]
    fn parse_value_float() {
        assert_eq!(parse_value("1.5"), json!(1.5));
    }

    #[test]
    fn parse_value_negative_integer() {
        assert_eq!(parse_value("-42"), json!(-42));
    }

    #[test]
    fn parse_value_json_array() {
        assert_eq!(parse_value("[1,2,3]"), json!([1, 2, 3]));
    }

    #[test]
    fn parse_value_json_object_with_spaces() {
        assert_eq!(parse_value(r#"{"a": 1}"#), json!({"a": 1}));
    }

    #[test]
    fn parse_value_quoted_string_with_escapes() {
        // JSON-escaped inner quotes: the input is a valid JSON string literal.
        let result = parse_value(r#""hello\"world""#);
        assert_eq!(result, json!("hello\"world"));
        assert!(result.is_string());
    }
}

mod error_handling {
    use super::*;

    #[test]
    fn detect_format_unsupported() {
        detect_format("readme.txt").expect_err("expected error");
    }

    #[test]
    fn detect_format_no_extension() {
        detect_format("Makefile").expect_err("expected error");
    }

    #[test]
    fn navigate_mut_missing_key_no_create() {
        let mut val = json!({"a": 1});
        let seg = crate::selector::parse("b").unwrap();
        navigate_mut(&mut val, &seg, false, "doc.set").expect_err("expected error");
    }

    #[test]
    fn navigate_mut_index_out_of_bounds() {
        let mut val = json!({"items": [10]});
        let seg = crate::selector::parse("items[5]").unwrap();
        navigate_mut(&mut val, &seg, false, "doc.set").expect_err("expected error");
    }

    #[test]
    fn set_at_path_out_of_bounds_index_fails() {
        let mut root = json!({"items": [1]});
        let sel = crate::selector::parse("items[5]").unwrap();
        set_at_path(&mut root, &sel, json!(99)).expect_err("expected error");
    }

    #[test]
    fn delete_where_non_array_fails() {
        let mut root = json!({"items": "not-an-array"});
        let sel = crate::selector::parse("items").unwrap();
        delete_where(&mut root, &sel, "k=v").expect_err("expected error");
    }

    #[test]
    fn delete_where_double_equals_rejected() {
        let mut root =
            json!({"items": [{"name": "a", "keep": false}, {"name": "b", "keep": true}]});
        let sel = crate::selector::parse("items").unwrap();
        let err = delete_where(&mut root, &sel, "keep == false").unwrap_err();
        assert!(
            err.to_string().contains("'=='"),
            "expected == rejection, got: {err}"
        );
        // Array must be unchanged (no silent removal).
        assert_eq!(root["items"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn delete_where_empty_key_rejected() {
        let mut root = json!({"items": [{"name": "a"}]});
        let sel = crate::selector::parse("items").unwrap();
        let err = delete_where(&mut root, &sel, "=value").unwrap_err();
        assert!(
            err.to_string().contains("empty"),
            "expected empty-key error, got: {err}"
        );
    }

    #[test]
    fn move_at_path_missing_source_fails() {
        let mut root = json!({"a": 1});
        let from = crate::selector::parse("nonexistent").unwrap();
        let to = crate::selector::parse("b").unwrap();
        move_at_path(&mut root, &from, &to).expect_err("expected error");
    }

    #[test]
    fn move_at_path_array_root_bare_key_is_type_error() {
        // Multi-doc YAML is a top-level array. Bare-key move must type_error
        // with index hints (not invalid_input "parent is array").
        let mut root = json!([{"port": 80}, {"port": 443}]);
        let original = root.clone();
        let from = crate::selector::parse("port").unwrap();
        let to = crate::selector::parse("http_port").unwrap();
        let err = move_at_path(&mut root, &from, &to).unwrap_err();
        assert!(
            crate::exit::is_type_error(&err),
            "expected type_error, got: {err}"
        );
        let msg = err.to_string();
        assert!(
            msg.contains("0.port") || msg.contains("[0].port"),
            "expected multi-doc index hint, got: {msg}"
        );
        assert_eq!(root, original, "root must be unchanged on type error");
    }

    #[test]
    fn move_at_path_array_root_bare_target_is_type_error() {
        // Indexed source + bare target at array root also needs type_error.
        let mut root = json!([{"port": 80}, {"port": 443}]);
        let original = root.clone();
        let from = crate::selector::parse("0.port").unwrap();
        let to = crate::selector::parse("http_port").unwrap();
        let err = move_at_path(&mut root, &from, &to).unwrap_err();
        assert!(
            crate::exit::is_type_error(&err),
            "expected type_error, got: {err}"
        );
        let msg = err.to_string();
        assert!(
            msg.contains("0.http_port") || msg.contains("[0].http_port"),
            "expected multi-doc index hint on target, got: {msg}"
        );
        assert_eq!(root, original, "root must be unchanged on type error");
    }

    #[test]
    fn move_at_path_empty_from_selector_fails() {
        let mut root = json!({"a": 1});
        let from: Vec<crate::selector::Segment> = vec![];
        let to = crate::selector::parse("b").unwrap();
        move_at_path(&mut root, &from, &to).expect_err("expected error");
    }

    #[test]
    fn yaml_multi_document_parsed_as_array() {
        // Multi-document YAML (--- separated) is parsed as an array of documents.
        let yaml = "---\nname: first\n---\nname: second\n";
        let val = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let arr = val.as_array().expect("multi-doc should be an array");
        assert_eq!(arr.len(), 2);
        assert_eq!(arr[0]["name"], json!("first"));
        assert_eq!(arr[1]["name"], json!("second"));
    }

    #[test]
    fn yaml_single_doc_with_leading_separator_is_not_multi() {
        // A single document with a leading --- is standard YAML, not multi-doc.
        let yaml = "---\nname: only\n";
        let val = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        // Should be parsed as a single object, not wrapped in an array.
        assert!(
            val.is_object(),
            "single doc should be an object, got: {val}"
        );
        assert_eq!(val["name"], json!("only"));
    }

    #[test]
    fn yaml_multi_document_get_second_doc() {
        // Read operations can address individual documents by index.
        let yaml = "---\napiVersion: v1\nkind: ConfigMap\n---\napiVersion: v1\nkind: Service\n";
        let val = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let arr = val.as_array().expect("multi-doc should be an array");
        assert_eq!(arr[1]["kind"], json!("Service"));
    }

    #[test]
    fn is_multi_document_detects_two_docs() {
        assert!(is_multi_document_yaml("---\nfirst: 1\n---\nsecond: 2\n"));
    }

    #[test]
    fn is_multi_document_single_leading_separator() {
        // A single leading --- is standard YAML preamble, not multi-doc.
        assert!(!is_multi_document_yaml("---\nname: only\n"));
    }

    #[test]
    fn is_multi_document_no_separator() {
        assert!(!is_multi_document_yaml("name: value\ncount: 1\n"));
    }

    #[test]
    fn is_multi_document_dashes_in_value_not_separator() {
        // "---" embedded in a value (not on its own line) is not a separator.
        assert!(!is_multi_document_yaml("name: ---foo\ncount: 1\n"));
    }

    #[test]
    fn is_multi_document_without_leading_separator() {
        // Two documents without a leading ---.
        assert!(is_multi_document_yaml("first: 1\n---\nsecond: 2\n"));
    }

    #[test]
    fn is_multi_document_three_docs() {
        assert!(is_multi_document_yaml("---\na: 1\n---\nb: 2\n---\nc: 3\n"));
    }

    #[test]
    fn is_multi_document_trailing_whitespace_after_separator() {
        // YAML spec allows trailing whitespace after ---.
        assert!(is_multi_document_yaml("---\nfirst: 1\n---  \nsecond: 2\n"));
    }

    #[test]
    fn is_multi_document_comment_after_separator() {
        // YAML spec allows comments after ---.
        assert!(is_multi_document_yaml(
            "--- # doc1\nfirst: 1\n--- # doc2\nsecond: 2\n"
        ));
    }

    #[test]
    fn is_multi_document_consecutive_separators() {
        // Two consecutive --- with no content between them.
        assert!(is_multi_document_yaml("---\n---\nsecond: 2\n"));
    }

    #[test]
    fn yaml_multi_document_with_merge_keys() {
        // Multi-document YAML where at least one document uses <<: merge keys.
        // Merge key resolution should work correctly per document.
        let yaml = "---\ndefaults: &defaults\n  timeout: 30\n  retries: 3\nservice:\n  name: api\n  <<: *defaults\n---\nkind: Service\nport: 8080\n";
        let val = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let arr = val.as_array().expect("multi-doc should be an array");
        assert_eq!(arr.len(), 2);
        // First doc: merge key should be resolved.
        assert_eq!(arr[0]["service"]["timeout"], json!(30));
        assert_eq!(arr[0]["service"]["retries"], json!(3));
        assert_eq!(arr[0]["service"]["name"], json!("api"));
        // Second doc: plain mapping, no merge keys.
        assert_eq!(arr[1]["kind"], json!("Service"));
        assert_eq!(arr[1]["port"], json!(8080));
    }

    #[test]
    fn yaml_multi_document_set_preserves_separators() {
        // Writing multi-doc YAML must keep --- separators (not collapse to a
        // single sequence document). kubectl apply -f requires multi-doc form.
        let yaml = "apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: demo\ndata:\n  key: value\n---\napiVersion: v1\nkind: Service\nmetadata:\n  name: demo-svc\nspec:\n  ports:\n    - port: 80\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new[0]["data"]["key"] = json!("newval");
        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();

        assert!(
            is_multi_document_yaml(&result),
            "write must preserve multi-doc stream, got:\n{result}"
        );
        assert!(
            !result.trim_start().starts_with("- "),
            "must not serialize multi-doc as a YAML sequence, got:\n{result}"
        );
        assert!(
            result.contains("key: newval") || result.contains("key: \"newval\""),
            "updated value missing:\n{result}"
        );
        assert!(
            result.contains("kind: Service"),
            "second document must be preserved:\n{result}"
        );

        // Round-trip: still addressable by document index.
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed[0]["data"]["key"], json!("newval"));
        assert_eq!(reparsed[1]["kind"], json!("Service"));
        assert_eq!(reparsed[1]["spec"]["ports"][0]["port"], json!(80));
    }

    /// Unrelated field edit must keep anchors, aliases, and merge keys instead of
    /// expanding every map into a full copy (agent configs use defaults via <<).
    #[test]
    fn yaml_unrelated_edit_preserves_anchors_aliases_and_merges() {
        let yaml = "\
defaults: &defaults
  timeout: 30
  retries: 3
  log_level: info
staging:
  <<: *defaults
  host: staging.example.com
production:
  <<: *defaults
  host: prod.example.com
  log_level: warning
app_name: my-service
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["app_name"] = json!("other-service");

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();

        assert!(
            result.contains("&defaults"),
            "anchor definition must survive unrelated edit:\n{result}"
        );
        let merge_count = result.matches("<<:").count();
        assert_eq!(
            merge_count, 2,
            "expected two merge keys preserved, got {merge_count}:\n{result}"
        );
        assert!(
            !result.contains("timeout: 30\n  retries: 3\n  log_level: info\nstaging:\n  timeout:"),
            "must not expand defaults into staging as a full copy:\n{result}"
        );
        assert!(
            result.contains("app_name: other-service")
                || result.contains("app_name: \"other-service\""),
            "edited field missing:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["app_name"], json!("other-service"));
        assert_eq!(reparsed["staging"]["timeout"], json!(30));
        assert_eq!(reparsed["staging"]["host"], json!("staging.example.com"));
        assert_eq!(reparsed["production"]["log_level"], json!("warning"));
    }

    /// Local override of a merge-inherited field adds an explicit key and
    /// keeps the `<<` merge (YAML override semantics).
    #[test]
    fn yaml_merge_override_preserves_merge_key() {
        let yaml = "\
defaults: &d
  timeout: 30
  retries: 3
staging:
  <<: *d
  name: api
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["staging"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("&d") && result.contains("<<: *d"),
            "anchor and merge must remain when overriding inherited field:\n{result}"
        );
        assert!(
            result.contains("timeout: 60"),
            "local override missing:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["staging"]["timeout"], json!(60));
        assert_eq!(reparsed["staging"]["retries"], json!(3));
        assert_eq!(reparsed["defaults"]["timeout"], json!(30));
    }

    /// Pure alias (`key: *anchor`) must stay an alias when a sibling field changes.
    #[test]
    fn yaml_pure_alias_preserved_on_sibling_edit() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a: *shared
service_b: *shared
label: keep
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["label"] = json!("changed");

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(result.contains("&shared"), "anchor must remain:\n{result}");
        assert_eq!(
            result.matches("*shared").count(),
            2,
            "both aliases must remain:\n{result}"
        );
        assert!(
            !result.contains("service_a:\n  timeout:"),
            "must not expand pure aliases into full maps:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["label"], json!("changed"));
        assert_eq!(reparsed["service_a"]["timeout"], json!(30));
        assert_eq!(reparsed["service_b"]["retries"], json!(3));
    }

    /// Interior override of a pure mapping alias becomes `<<: *name` plus the
    /// local key. Sibling aliases stay aliases.
    #[test]
    fn yaml_pure_alias_interior_override_becomes_merge() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a: *shared
service_b: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_a"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("&shared"),
            "anchor definition must remain:\n{result}"
        );
        assert!(
            result.contains("<<: *shared"),
            "override must keep identity via merge:\n{result}"
        );
        assert!(
            result.contains("timeout: 60"),
            "local override missing:\n{result}"
        );
        assert!(
            result.contains("service_b: *shared"),
            "sibling alias must stay a pure alias:\n{result}"
        );
        assert!(
            !result.contains("service_a:\n  timeout: 60\n  retries: 3"),
            "must not expand the alias into a full concrete map:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"]["timeout"], json!(60));
        assert_eq!(reparsed["service_a"]["retries"], json!(3));
        assert_eq!(reparsed["service_b"]["timeout"], json!(30));
        assert_eq!(reparsed["shared"]["timeout"], json!(30));
    }

    /// Two pure aliases edited in one write both become merges.
    #[test]
    fn yaml_two_pure_aliases_become_merges() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a: *shared
service_b: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_a"]["timeout"] = json!(60);
        new["service_b"]["retries"] = json!(9);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert_eq!(
            result,
            "\
shared: &shared
  timeout: 30
  retries: 3
service_a:
  <<: *shared
  timeout: 60
service_b:
  <<: *shared
  retries: 9
"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"]["timeout"], json!(60));
        assert_eq!(reparsed["service_b"]["retries"], json!(9));
        assert_eq!(reparsed["service_a"]["retries"], json!(3));
        assert_eq!(reparsed["service_b"]["timeout"], json!(30));
    }

    /// Adding a key under a pure mapping alias is a local addition beside `<<`.
    #[test]
    fn yaml_pure_alias_add_key_becomes_merge() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_a"]["region"] = json!("us-east");

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("&shared") && result.contains("<<: *shared"),
            "add-key must convert alias to merge:\n{result}"
        );
        assert!(
            result.contains("region: us-east") || result.contains("region: \"us-east\""),
            "new key missing:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"]["region"], json!("us-east"));
        assert_eq!(reparsed["service_a"]["timeout"], json!(30));
        assert_eq!(reparsed["shared"]["timeout"], json!(30));
    }

    /// Deleting an inherited key cannot be expressed with `<<`. Expand that site.
    #[test]
    fn yaml_pure_alias_delete_inherited_still_expands() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a: *shared
service_b: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_a"]
            .as_object_mut()
            .unwrap()
            .shift_remove("retries");

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("&shared"),
            "anchor definition must remain:\n{result}"
        );
        assert!(
            result.contains("service_b: *shared"),
            "untouched sibling must stay an alias:\n{result}"
        );
        assert!(
            !result.contains("service_a: *shared") && !result.contains("service_a:\n  <<:"),
            "deleted inherited key cannot stay a merge:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert!(
            reparsed["service_a"].get("retries").is_none(),
            "retries must be gone from service_a:\n{result}"
        );
        assert_eq!(reparsed["service_a"]["timeout"], json!(30));
        assert_eq!(reparsed["service_b"]["retries"], json!(3));
    }

    /// Replacing a pure alias with a map that is not a key-superset expands.
    /// Keeping `<<` would leak inherited keys into the semantic value.
    #[test]
    fn yaml_pure_alias_non_superset_replace_expands() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_a"] = json!({"name": "api"});

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("&shared"),
            "anchor definition must remain:\n{result}"
        );
        assert!(
            !result.contains("<<:"),
            "non-superset replace must not keep a merge:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"], json!({"name": "api"}));
        assert!(reparsed["service_a"].get("timeout").is_none());
    }

    /// Sequence item that is a mapping alias converts to merge on override.
    #[test]
    fn yaml_sequence_alias_item_override_becomes_merge() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
items:
  - *shared
  - *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["items"][0]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("&shared") && result.contains("<<: *shared"),
            "sequence alias override must become merge:\n{result}"
        );
        assert!(
            result.contains("- <<: *shared"),
            "first item must become a merge:\n{result}"
        );
        assert!(
            result.contains("  - *shared"),
            "untouched second item must stay a pure alias:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["items"][0]["timeout"], json!(60));
        assert_eq!(reparsed["items"][0]["retries"], json!(3));
        assert_eq!(reparsed["items"][1]["timeout"], json!(30));
    }

    /// Alias as a field inside a sequence item (`items[1].cfg: *shared`).
    /// The walk must recurse mappings in the list and rewrite only that site.
    #[test]
    fn yaml_alias_field_inside_sequence_item_becomes_merge() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
items:
  - name: a
    cfg: *shared
  - name: b
    cfg: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["items"][1]["cfg"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("name: a") && result.contains("cfg: *shared"),
            "first item cfg must stay a pure alias:\n{result}"
        );
        assert!(
            result.contains("<<: *shared") && result.contains("timeout: 60"),
            "second item cfg must become a merge:\n{result}"
        );
        assert_eq!(
            result.matches("cfg: *shared").count(),
            1,
            "only the unedited cfg should remain a pure alias:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["items"][0]["cfg"]["timeout"], json!(30));
        assert_eq!(reparsed["items"][1]["cfg"]["timeout"], json!(60));
        assert_eq!(reparsed["items"][1]["cfg"]["retries"], json!(3));
        assert_eq!(reparsed["items"][1]["name"], json!("b"));
    }

    /// Same mapping key (`cfg: *shared`) under two sibling objects.
    /// File-wide unique-line matching would rewrite the first site.
    #[test]
    fn yaml_sibling_objects_same_alias_key_rewrites_the_edited_site() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a:
  cfg: *shared
service_b:
  cfg: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_b"]["cfg"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert_eq!(
            result,
            "\
shared: &shared
  timeout: 30
  retries: 3
service_a:
  cfg: *shared
service_b:
  cfg:
    <<: *shared
    timeout: 60
"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"]["cfg"]["timeout"], json!(30));
        assert_eq!(reparsed["service_b"]["cfg"]["timeout"], json!(60));
        assert_eq!(reparsed["service_b"]["cfg"]["retries"], json!(3));
    }

    /// Three `cfg: *shared` sites; edit only the middle occurrence.
    #[test]
    fn yaml_three_sibling_alias_keys_rewrites_middle_site() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a:
  cfg: *shared
service_b:
  cfg: *shared
service_c:
  cfg: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_b"]["cfg"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert_eq!(
            result,
            "\
shared: &shared
  timeout: 30
  retries: 3
service_a:
  cfg: *shared
service_b:
  cfg:
    <<: *shared
    timeout: 60
service_c:
  cfg: *shared
"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"]["cfg"]["timeout"], json!(30));
        assert_eq!(reparsed["service_b"]["cfg"]["timeout"], json!(60));
        assert_eq!(reparsed["service_c"]["cfg"]["timeout"], json!(30));
    }

    /// Two sibling lists share `*shared`. Edit the second list only.
    #[test]
    fn yaml_sibling_sequence_alias_rewrites_the_edited_list() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
first:
  - *shared
second:
  - *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["second"][0]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert_eq!(
            result,
            "\
shared: &shared
  timeout: 30
  retries: 3
first:
  - *shared
second:
  - <<: *shared
    timeout: 60
"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["first"][0]["timeout"], json!(30));
        assert_eq!(reparsed["second"][0]["timeout"], json!(60));
        assert_eq!(reparsed["second"][0]["retries"], json!(3));
    }

    /// CRLF alias lines must still convert to merge and keep CRLF.
    #[test]
    fn yaml_pure_alias_override_preserves_crlf() {
        let yaml = "shared: &shared\r\n  timeout: 30\r\n  retries: 3\r\nservice_a: *shared\r\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_a"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("\r\n") && result.contains("<<: *shared"),
            "CRLF alias override must become merge:\n{result:?}"
        );
        assert!(
            !result.replace("\r\n", "").contains('\n'),
            "must not mix bare LF into CRLF file: {result:?}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"]["timeout"], json!(60));
        assert_eq!(reparsed["service_a"]["retries"], json!(3));
    }

    /// Replacing a pure alias with `{}` must stay an object, not null.
    #[test]
    fn yaml_pure_alias_replace_with_empty_object() {
        let yaml = "shared: &shared\n  timeout: 30\nservice_a: *shared\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_a"] = json!({});

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert_eq!(result, "shared: &shared\n  timeout: 30\nservice_a:\n  {}\n");
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"], json!({}));
        assert!(reparsed["service_a"].get("timeout").is_none());
    }

    /// Nested `parent.child: *shared` is the same interior-alias case as a
    /// top-level key; the walk must find the alias under the parent mapping.
    #[test]
    fn yaml_nested_mapping_alias_becomes_merge() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
parent:
  child: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["parent"]["child"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("&shared") && result.contains("<<: *shared"),
            "nested alias override must become merge:\n{result}"
        );
        assert!(
            result.contains("child:\n    <<: *shared"),
            "nested alias must become a block merge:\n{result}"
        );
        assert!(
            result.contains("timeout: 60"),
            "local override missing:\n{result}"
        );
        assert!(
            !result.contains("child: *shared"),
            "edited nested alias must not stay a pure alias:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["parent"]["child"]["timeout"], json!(60));
        assert_eq!(reparsed["parent"]["child"]["retries"], json!(3));
        assert_eq!(reparsed["shared"]["timeout"], json!(30));
    }

    /// A trailing comment on `key: *alias` stays on the key after the splice.
    #[test]
    fn yaml_pure_alias_keeps_trailing_comment() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
service_a: *shared  # inherited
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["service_a"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("service_a:  # inherited") && result.contains("<<: *shared"),
            "comment must stay on the key line after splice:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["service_a"]["timeout"], json!(60));
        assert_eq!(reparsed["service_a"]["retries"], json!(3));
    }

    /// Multi-doc streams serialize per document. A pure alias in doc 1 must
    /// become a merge without dumping doc 0.
    #[test]
    fn yaml_multi_doc_second_doc_pure_alias_becomes_merge() {
        let yaml = "\
---
kind: Config
name: keep
---
shared: &shared
  timeout: 30
  retries: 3
service_a: *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new[1]["service_a"]["timeout"] = json!(60);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            is_multi_document_yaml(&result),
            "must stay multi-document:\n{result}"
        );
        assert!(
            result.contains("name: keep") || result.contains("name: \"keep\""),
            "first document must stay intact:\n{result}"
        );
        assert!(
            result.contains("&shared") && result.contains("<<: *shared"),
            "second-doc alias override must become merge:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed[0]["name"], json!("keep"));
        assert_eq!(reparsed[1]["service_a"]["timeout"], json!(60));
        assert_eq!(reparsed[1]["service_a"]["retries"], json!(3));
    }

    /// Keys that are not YAML plain identifiers skip the alias-line splice
    /// (they are interpolated into a regex). Semantic edit still applies.
    #[test]
    fn yaml_non_plain_key_alias_still_edits() {
        let yaml = "\
shared: &shared
  timeout: 30
  retries: 3
\"foo:bar\": *shared
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["foo:bar"]["timeout"] = json!(60);

        let file: yaml_edit::YamlFile = yaml.parse().unwrap();
        let splice =
            super::super::yaml_cst::rewrite_yaml_alias_object_edits(yaml, &file, &old, &new)
                .unwrap();
        assert!(
            splice.is_none(),
            "non-plain key must skip the alias-line splice, got {splice:?}"
        );

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["foo:bar"]["timeout"], json!(60));
        assert_eq!(reparsed["foo:bar"]["retries"], json!(3));
        assert_eq!(reparsed["shared"]["timeout"], json!(30));
    }

    /// New key under a merge map is a local addition; merge/anchor stay.
    #[test]
    fn yaml_add_key_under_merge_map_preserves_merge() {
        let yaml = "\
defaults: &d
  timeout: 30
staging:
  <<: *d
  name: api
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["staging"]["region"] = json!("us-east");

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("<<: *d") && result.contains("&d"),
            "merge/anchor must remain after local key add:\n{result}"
        );
        assert!(
            result.contains("region: us-east") || result.contains("region: \"us-east\""),
            "new key missing:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed["staging"]["region"], json!("us-east"));
        assert_eq!(reparsed["staging"]["timeout"], json!(30));
    }

    /// Multi-document stream: unrelated edit in one doc keeps that doc's merges.
    #[test]
    fn yaml_multi_doc_unrelated_edit_preserves_merges() {
        let yaml = "\
---
defaults: &defaults
  timeout: 30
service:
  <<: *defaults
  name: api
---
kind: Service
port: 8080
";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new[0]["service"]["name"] = json!("api-v2");
        new[1]["port"] = json!(9090);

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            is_multi_document_yaml(&result),
            "must stay multi-document:\n{result}"
        );
        assert!(
            result.contains("&defaults") && result.contains("<<: *defaults"),
            "first doc anchors/merges must survive an edit in that document:\n{result}"
        );
        assert!(
            result.contains("name: api-v2") || result.contains("name: \"api-v2\""),
            "first doc edit missing:\n{result}"
        );
        assert!(
            result.contains("port: 9090"),
            "second doc edit missing:\n{result}"
        );
    }

    #[test]
    fn yaml_multi_document_delete_first_preserves_surviving_comments() {
        // Whole-doc delete must not pair body[0] onto former doc 1.
        let yaml = "# doc A comment\nname: alpha\n---\n# doc B comment\nname: beta\n---\n# doc C comment\nname: gamma\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new.as_array_mut().unwrap().remove(0);
        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("# doc B comment") && result.contains("name: beta"),
            "surviving beta must keep its comment, got:\n{result}"
        );
        assert!(
            result.contains("# doc C comment") && result.contains("name: gamma"),
            "surviving gamma must keep its comment, got:\n{result}"
        );
        assert!(
            !result.contains("# doc A comment"),
            "deleted alpha comment must not stick to beta, got:\n{result}"
        );
        assert!(
            !result.contains("name: alpha"),
            "deleted alpha must be gone, got:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed.as_array().map(|a| a.len()), Some(2));
        assert_eq!(reparsed[0]["name"], json!("beta"));
        assert_eq!(reparsed[1]["name"], json!("gamma"));
    }

    #[test]
    fn yaml_multi_document_set_preserves_crlf_separators() {
        // Windows agent editing multi-doc k8s manifests must keep CRLF stream.
        let yaml = "---\r\na: 1\r\n---\r\nb: 2\r\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new[0]["a"] = json!(9);
        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("\r\n"),
            "multi-doc write must preserve CRLF separators, got: {result:?}"
        );
        assert!(
            !result.replace("\r\n", "").contains('\n'),
            "must not mix bare LF into CRLF multi-doc stream: {result:?}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed[0]["a"], json!(9));
        assert_eq!(reparsed[1]["b"], json!(2));
    }

    #[test]
    fn yaml_multi_document_set_second_doc_preserves_first() {
        let yaml = "---\nname: alpha\nversion: 1\n---\nname: beta\nversion: 2\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new[1]["version"] = json!(99);
        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();

        assert!(is_multi_document_yaml(&result), "got:\n{result}");
        assert!(
            result.starts_with("---"),
            "leading marker preserved:\n{result}"
        );
        let reparsed = parse_doc(&result, &FileFormat::Yaml).unwrap();
        assert_eq!(reparsed[0]["name"], json!("alpha"));
        assert_eq!(reparsed[0]["version"], json!(1));
        assert_eq!(reparsed[1]["name"], json!("beta"));
        assert_eq!(reparsed[1]["version"], json!(99));
    }

    #[test]
    fn yaml_multi_document_unchanged_returns_original() {
        let yaml = "first: 1\n---\nsecond: 2\n";
        let val = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let result = serialize_value_preserving(yaml, &val, &val, &FileFormat::Yaml).unwrap();
        assert_eq!(result, yaml);
    }

    #[test]
    fn split_multi_document_yaml_without_leading_marker() {
        let (leading, bodies) = split_multi_document_yaml("first: 1\n---\nsecond: 2\n");
        assert!(!leading);
        assert_eq!(bodies.len(), 2);
        assert!(bodies[0].contains("first: 1"));
        assert!(bodies[1].contains("second: 2"));
    }

    #[test]
    fn split_multi_document_yaml_with_leading_marker() {
        let (leading, bodies) = split_multi_document_yaml("---\na: 1\n---\nb: 2\n");
        assert!(leading);
        assert_eq!(bodies.len(), 2);
        assert!(bodies[0].contains("a: 1"));
        assert!(bodies[1].contains("b: 2"));
    }

    #[test]
    fn mutation_append_to_non_array() {
        let mut root = json!({"items": "not-array"});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Append {
                selector: "items".into(),
                value: json!(1),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::TypeError(_)));
    }

    #[test]
    fn mutation_move_missing_source() {
        let mut root = json!({"a": 1});
        let err = apply_doc_mutation(
            &mut root,
            DocMutation::Move {
                from: "nonexistent".into(),
                to: "b".into(),
            },
        )
        .unwrap_err();
        assert!(
            err.to_string()
                .contains("source key 'nonexistent' not found")
        );
    }

    #[test]
    fn mutation_prepend_to_non_array() {
        let mut root = json!({"items": "not-array"});
        let result = apply_doc_mutation(
            &mut root,
            DocMutation::Prepend {
                selector: "items".into(),
                value: json!(1),
            },
        )
        .unwrap();
        assert!(matches!(result, MutationResult::TypeError(_)));
    }
}

mod security {
    use super::*;

    #[test]
    fn deep_merge_depth_limit() {
        // Build a deeply nested structure beyond MAX_MERGE_DEPTH (128)
        let mut deep_val = json!("leaf");
        for _ in 0..130 {
            deep_val = json!({"n": deep_val});
        }
        let mut base = json!({});
        deep_merge(&mut base, &deep_val);
        // Should not panic; at depth 128 it clones instead of recursing
        assert!(base.is_object());
    }
}

mod regression {
    use super::*;

    // ── gap closing regressions (#824/#825 style fidelity) ───────────
    #[test]
    fn yaml_deep_nested_object_creation() {
        // Deep (2+) new intermediates: in-memory always correct.
        // Preserving serialize may use fallback for some >1-level cases
        // (data correct, comments may be hoisted). We assert in-memory +
        // plain serialize roundtrip.
        let mut v = json!({"root": 1});
        set_at_path(&mut v, &crate::selector::parse("a.b.c").unwrap(), json!(42)).unwrap();
        assert_eq!(v, json!({"root":1, "a":{"b":{"c":42}}}));

        let plain = serialize_value(&v, &FileFormat::Yaml).unwrap();
        let reparsed: serde_json::Value = serde_yaml_ng::from_str(&plain).unwrap();
        assert_eq!(reparsed, json!({"root":1, "a":{"b":{"c":42}}}));
    }

    #[test]
    fn yaml_nested_keys_create_correct_structure() {
        // Regression for #824: new nested via set/ensure/merge must produce
        // valid indented mappings, not "key:\nleaf: " (null parent + sibling).
        let yaml = "a: 1\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut newv = old.clone();
        set_at_path(
            &mut newv,
            &crate::selector::parse("server.port").unwrap(),
            json!(9090),
        )
        .unwrap();
        let result = serialize_value_preserving(yaml, &old, &newv, &FileFormat::Yaml).unwrap();
        let reparsed: serde_json::Value = serde_yaml_ng::from_str(&result).unwrap();
        assert_eq!(reparsed, json!({"a":1, "server":{"port":9090}}));
        // Also for ensure path (no-op if exists, but create case)
        let mut new2 = old.clone();
        // simulate ensure by set since not exist
        set_at_path(
            &mut new2,
            &crate::selector::parse("settings.debug").unwrap(),
            json!(false),
        )
        .unwrap();
        let r2 = serialize_value_preserving(yaml, &old, &new2, &FileFormat::Yaml).unwrap();
        let p2: serde_json::Value = serde_yaml_ng::from_str(&r2).unwrap();
        assert_eq!(p2, json!({"a":1, "settings":{"debug":false}}));
        // merge case
        let mut new3 = old.clone();
        deep_merge(&mut new3, &json!({"server": {"port": 9090}}));
        let r3 = serialize_value_preserving(yaml, &old, &new3, &FileFormat::Yaml).unwrap();
        let p3: serde_json::Value = serde_yaml_ng::from_str(&r3).unwrap();
        assert_eq!(p3, json!({"a":1, "server":{"port":9090}}));
    }

    #[test]
    fn yaml_array_shrink_with_modification_not_lost() {
        // Regression: complex array shrinkage (shrink + modify remaining
        // elements) must not be silently dropped. The CST path cannot
        // handle this case, so it should fall through to serialize_value.
        let yaml = "# Config\nname: app\ntags:\n  - alpha\n  - beta\n  - gamma\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let new = json!({"name": "app", "tags": ["MODIFIED"]});

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        let parsed: serde_json::Value = serde_yaml_ng::from_str(&result).unwrap();
        assert_eq!(
            parsed, new,
            "serialized YAML must match target value: {result}"
        );
    }

    #[test]
    fn yaml_nested_complex_shrinkage_in_same_length_outer() {
        // Regression: same-length outer array with complex inner array
        // shrinkage (not a subsequence). apply_yaml_sequence_diff must
        // propagate the failure flag from the nested mapping diff.
        let yaml = "# Comment\nitems:\n  - name: a\n    tags:\n      - x\n      - y\n  - name: b\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let new = json!({"items": [{"name": "a", "tags": ["MODIFIED"]}, {"name": "b"}]});

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        let parsed: serde_json::Value = serde_yaml_ng::from_str(&result).unwrap();
        assert_eq!(
            parsed, new,
            "nested complex shrinkage must not be silently dropped: {result}"
        );
    }

    #[test]
    fn yaml_nested_array_growth_detected() {
        // Regression: has_array_growth_diffs must recurse into same-length
        // array elements to detect nested growth (e.g., adding a port to
        // the first server's ports array while the outer array stays the
        // same length).
        let yaml = "# config comment\nservers:\n  - name: web\n    ports:\n      - 80\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        new["servers"][0]["ports"]
            .as_array_mut()
            .unwrap()
            .push(json!(443));

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert!(
            result.contains("443"),
            "nested array growth should not be silently dropped: {result}"
        );
        assert!(
            result.contains("# config comment"),
            "comments should be preserved: {result}"
        );
    }

    #[test]
    fn yaml_nested_array_append_produces_yaml_edit_parseable_output() {
        // Regression for #972: doc_append on a deeply nested array (e.g., K8s
        // env vars inside a container) could produce YAML with altered indentation.
        // serde_yaml_ng accepted the result, but yaml_edit (CST parser) rejected it,
        // causing subsequent doc_set calls to fail with "did not find expected key".
        //
        // Uses realistic K8s-style indentation: containers at 8 spaces, entries
        // at 10 spaces (the exact pattern from the bug report).
        let yaml = concat!(
            "apiVersion: apps/v1\n",
            "kind: Deployment\n",
            "metadata:\n",
            "  name: my-app\n",
            "spec:\n",
            "  replicas: 1\n",
            "  template:\n",
            "    spec:\n",
            "      containers:\n",
            "        - name: main\n",
            "          image: my-app:latest\n",
            "          env:\n",
            "            - name: DB_HOST\n",
            "              value: postgres.default:5432\n",
            "            - name: API_URL\n",
            "              valueFrom:\n",
            "                configMapKeyRef:\n",
            "                  name: config\n",
            "                  key: api-url\n",
            "          resources:\n",
            "            limits:\n",
            "              cpu: \"1\"\n",
            "              memory: 512Mi\n",
        );
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        // Append a new env var with a URL value (the original bug trigger).
        new["spec"]["template"]["spec"]["containers"][0]["env"]
            .as_array_mut()
            .unwrap()
            .push(
                json!({"name": "OTEL_ENDPOINT", "value": "http://otel-collector.monitoring:4317"}),
            );

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        // Must round-trip through serde_yaml_ng.
        let reparsed: serde_json::Value = serde_yaml_ng::from_str(&result).unwrap();
        assert_eq!(reparsed, new, "serialized YAML must match target: {result}");
        // Must also be parseable by yaml_edit (the CST library used by doc_set).
        assert!(
            result.parse::<yaml_edit::YamlFile>().is_ok(),
            "result must be parseable by yaml_edit for subsequent doc_set: {result}"
        );
        // After the append, a subsequent set on a different path must succeed.
        // This simulates the #972 scenario: doc_append then doc_set.
        let mut final_val = new.clone();
        final_val["spec"]["template"]["spec"]["containers"][0]["resources"]["limits"]["cpu"] =
            json!("2");
        let result2 = serialize_value_preserving(&result, &new, &final_val, &FileFormat::Yaml)
            .expect("doc_set after doc_append must not fail");
        let reparsed2: serde_json::Value = serde_yaml_ng::from_str(&result2).unwrap();
        assert_eq!(
            reparsed2, final_val,
            "second operation must produce correct output: {result2}"
        );
    }
}

mod yaml_cst_cleanup {
    use super::*;

    #[test]
    fn delete_last_key_no_trailing_whitespace() {
        let yaml = "top:\n  first: aaa\n  second: bbb\n  third: ccc\n";
        let old = json!({"top": {"first": "aaa", "second": "bbb", "third": "ccc"}});
        let new = json!({"top": {"first": "aaa", "second": "bbb"}});
        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        // No line should have trailing whitespace
        for line in result.lines() {
            assert_eq!(
                line,
                line.trim_end(),
                "trailing whitespace found in: {:?}",
                line
            );
        }
        // Must end with exactly one newline
        assert!(result.ends_with('\n'), "missing final newline");
        assert!(
            !result.ends_with("\n\n"),
            "double final newline: {:?}",
            &result[result.len().saturating_sub(4)..]
        );
    }

    #[test]
    fn yaml_cst_set_preserves_crlf_line_endings() {
        // Real Windows path: doc set on CRLF YAML with comments uses CST preserve.
        let yaml = "server:\r\n  port: 8080\r\n  # keep me\r\n  host: localhost\r\n";
        let old = json!({"server": {"port": 8080, "host": "localhost"}});
        let new = json!({"server": {"port": 9090, "host": "localhost"}});
        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(
            result.contains("\r\n"),
            "CST preserve path must keep CRLF, got: {result:?}"
        );
        assert!(result.contains("9090"), "updated port missing: {result:?}");
        assert!(
            result.contains("# keep me"),
            "comment must be preserved: {result:?}"
        );
        // Dominant EOL should remain CRLF (no mixed bare LF after strip).
        let without_crlf = result.replace("\r\n", "");
        assert!(
            !without_crlf.contains('\n'),
            "must not introduce bare LF into CRLF file: {result:?}"
        );
    }

    #[test]
    fn delete_last_key_in_nested_section() {
        let yaml = "server:\n  host: localhost\n  port: 8080\n  workers: 4\n\ndb:\n  url: pg\n  pool: 10\n";
        let old = json!({"server": {"host": "localhost", "port": 8080, "workers": 4}, "db": {"url": "pg", "pool": 10}});
        let new = json!({"server": {"host": "localhost", "port": 8080, "workers": 4}, "db": {"url": "pg"}});
        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        for line in result.lines() {
            assert_eq!(line, line.trim_end(), "trailing whitespace: {:?}", line);
        }
        assert!(result.ends_with('\n'));
        assert!(result.contains("url: pg"));
        assert!(!result.contains("pool"));
    }

    #[test]
    fn delete_first_key_preserves_quotes_and_order() {
        let yaml = "app:\n  name: \"my-app\"\n  version: \"1.0.0\"\n  enabled: \"true\"\n  port: \"8080\"\n";
        let old = json!({"app": {"name": "my-app", "version": "1.0.0", "enabled": "true", "port": "8080"}});
        let mut new = old.clone();
        new.as_object_mut()
            .unwrap()
            .get_mut("app")
            .unwrap()
            .as_object_mut()
            .unwrap()
            .shift_remove("name");

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        // Quotes must be preserved on untouched values.
        assert!(
            result.contains("\"1.0.0\""),
            "version quotes lost: {result}"
        );
        assert!(result.contains("\"true\""), "enabled quotes lost: {result}");
        assert!(result.contains("\"8080\""), "port quotes lost: {result}");
        // Key ordering must match original (minus deleted key).
        let version_pos = result.find("version").unwrap();
        let enabled_pos = result.find("enabled").unwrap();
        let port_pos = result.find("port").unwrap();
        assert!(
            version_pos < enabled_pos && enabled_pos < port_pos,
            "key order changed: {result}"
        );
        // Deleted key must be gone.
        assert!(!result.contains("name"), "deleted key still present");
        // Indentation must be consistent (2-space).
        for line in result.lines() {
            if line.starts_with(' ') {
                let indent = line.len() - line.trim_start().len();
                assert_eq!(indent, 2, "wrong indentation on: {line:?}");
            }
        }
    }

    #[test]
    fn delete_first_key_quoted_type_sensitive_values() {
        // Values that would change YAML type if unquoted.
        let yaml =
            "config:\n  remove_me: x\n  enabled: \"true\"\n  count: \"42\"\n  ratio: \"1.0\"\n";
        let old =
            json!({"config": {"remove_me": "x", "enabled": "true", "count": "42", "ratio": "1.0"}});
        let mut new = old.clone();
        new.as_object_mut()
            .unwrap()
            .get_mut("config")
            .unwrap()
            .as_object_mut()
            .unwrap()
            .shift_remove("remove_me");

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        // Re-parse to verify types are still strings (not bool/int/float).
        let reparsed: serde_json::Value = serde_yaml_ng::from_str(&result).unwrap();
        assert_eq!(
            reparsed["config"]["enabled"],
            json!("true"),
            "enabled became bool"
        );
        assert_eq!(reparsed["config"]["count"], json!("42"), "count became int");
        assert_eq!(
            reparsed["config"]["ratio"],
            json!("1.0"),
            "ratio became float"
        );
    }

    #[test]
    fn delete_middle_key_preserves_formatting() {
        let yaml = "app:\n  name: \"my-app\"\n  version: \"1.0.0\"\n  port: \"8080\"\n";
        let old = json!({"app": {"name": "my-app", "version": "1.0.0", "port": "8080"}});
        let mut new = old.clone();
        new.as_object_mut()
            .unwrap()
            .get_mut("app")
            .unwrap()
            .as_object_mut()
            .unwrap()
            .shift_remove("version");

        let result = serialize_value_preserving(yaml, &old, &new, &FileFormat::Yaml).unwrap();
        assert!(result.contains("\"my-app\""), "name quotes lost: {result}");
        assert!(result.contains("\"8080\""), "port quotes lost: {result}");
        assert!(!result.contains("version"));
    }
}

mod format_preservation {
    use super::*;
    use ::proptest::prelude::*;

    #[test]
    fn yaml_new_key_in_existing_nested_preserves_sibling_comment() {
        let yaml = "server:\n  # keep host\n  host: localhost\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut newv = old.clone();
        set_at_path(
            &mut newv,
            &crate::selector::parse("server.port").unwrap(),
            json!(9090),
        )
        .unwrap();
        let result = serialize_value_preserving(yaml, &old, &newv, &FileFormat::Yaml).unwrap();
        assert!(result.contains("# keep host"));
        assert!(
            result.contains("9090") || result.contains("port"),
            "new port value missing"
        );
        // Note: when adding a brand-new key inside a sub, current CST may
        // place it at wrong level (fallback produces correct data). Full
        // attachment for new siblings inside subs is a remaining gap.
    }

    // -- TOML comment preservation ----------------------------------------

    #[test]
    fn toml_comment_preserved_on_set() {
        let toml = "# top\n[server]\nhost = \"localhost\" # hostname\nport = 8080\n";
        let old = parse_doc(toml, &FileFormat::Toml).unwrap();
        let mut new = old.clone();
        set_at_path(
            &mut new,
            &[
                selector::Segment::Key("server".into()),
                selector::Segment::Key("port".into()),
            ],
            json!(9090),
        )
        .unwrap();

        let result = serialize_value_preserving(toml, &old, &new, &FileFormat::Toml).unwrap();
        assert!(result.contains("# top"), "top comment missing");
        assert!(result.contains("# hostname"), "inline comment missing");
        assert!(result.contains("9090"), "new value missing");
        assert!(!result.contains("8080"), "old value still present");
    }

    #[test]
    fn toml_comment_preserved_on_delete() {
        let toml = "# keep this\n[section]\na = 1\nb = 2 # inline\n";
        let old = parse_doc(toml, &FileFormat::Toml).unwrap();
        let mut new = old.clone();
        // Delete key "a" from section.
        new.as_object_mut()
            .unwrap()
            .get_mut("section")
            .unwrap()
            .as_object_mut()
            .unwrap()
            .remove("a");

        let result = serialize_value_preserving(toml, &old, &new, &FileFormat::Toml).unwrap();
        assert!(result.contains("# keep this"), "top comment missing");
        assert!(result.contains("# inline"), "inline comment missing");
        assert!(result.contains("b = 2"), "surviving key missing");
        assert!(!result.contains("a = 1"), "deleted key still present");
    }

    #[test]
    fn toml_section_order_preserved() {
        let toml = "[z_last]\nval = 1\n\n[a_first]\nval = 2\n";
        let old = parse_doc(toml, &FileFormat::Toml).unwrap();
        let mut new = old.clone();
        set_at_path(
            &mut new,
            &[
                selector::Segment::Key("a_first".into()),
                selector::Segment::Key("val".into()),
            ],
            json!(99),
        )
        .unwrap();

        let result = serialize_value_preserving(toml, &old, &new, &FileFormat::Toml).unwrap();
        let z_pos = result.find("z_last").unwrap();
        let a_pos = result.find("a_first").unwrap();
        assert!(z_pos < a_pos, "section order changed: z@{z_pos} a@{a_pos}");
    }

    #[test]
    fn toml_new_key_inserted_without_breaking_comments() {
        let toml = "# config\n[pkg]\nname = \"app\"\n";
        let old = parse_doc(toml, &FileFormat::Toml).unwrap();
        let mut new = old.clone();
        set_at_path(
            &mut new,
            &[
                selector::Segment::Key("pkg".into()),
                selector::Segment::Key("version".into()),
            ],
            json!("1.0"),
        )
        .unwrap();

        let result = serialize_value_preserving(toml, &old, &new, &FileFormat::Toml).unwrap();
        assert!(result.contains("# config"), "comment missing");
        assert!(result.contains("name = \"app\""), "existing key missing");
        assert!(result.contains("version"), "new key missing");
    }

    #[test]
    fn toml_inline_table_style_preserved() {
        let toml = "[deps]\nserde = { version = 1, features = [\"derive\"] }\n";
        let old = parse_doc(toml, &FileFormat::Toml).unwrap();
        // No change — verify round-trip preserves inline style.
        let result = serialize_value_preserving(toml, &old, &old, &FileFormat::Toml).unwrap();
        assert!(result.contains("serde = {"), "inline table style lost");
    }

    // -- YAML comment preservation ----------------------------------------

    #[test]
    fn yaml_comment_preserved_on_set() {
        let yaml = "# top\nserver:\n  host: localhost # hostname\n  port: 8080\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        set_at_path(
            &mut new,
            &[
                selector::Segment::Key("server".into()),
                selector::Segment::Key("port".into()),
            ],
            json!(9090),
        )
        .unwrap();

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert!(result.contains("# top"), "top comment missing");
        assert!(result.contains("# hostname"), "inline comment missing");
        assert!(result.contains("9090"), "new value missing");
        assert!(!result.contains("8080"), "old value still present");
    }

    #[test]
    fn yaml_comment_preserved_on_delete() {
        let yaml = "# keep this\na: 1\nb: 2 # inline\nc: 3\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        // Delete key "a".
        new.as_object_mut().unwrap().remove("a");

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert!(result.contains("# keep this"), "top comment missing");
        assert!(result.contains("# inline"), "inline comment missing");
        assert!(result.contains("b: 2"), "surviving key missing");
        assert!(!result.contains("a: 1"), "deleted key still present");
    }

    #[test]
    fn yaml_key_order_preserved() {
        let yaml = "z_last: 1\na_first: 2\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        set_at_path(
            &mut new,
            &[selector::Segment::Key("a_first".into())],
            json!(99),
        )
        .unwrap();

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        let z_pos = result.find("z_last").unwrap();
        let a_pos = result.find("a_first").unwrap();
        assert!(z_pos < a_pos, "key order changed: z@{z_pos} a@{a_pos}");
    }

    #[test]
    fn yaml_new_key_inserted_without_breaking_comments() {
        let yaml = "# config\nname: app\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        set_at_path(
            &mut new,
            &[selector::Segment::Key("version".into())],
            json!("1.0"),
        )
        .unwrap();

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert!(result.contains("# config"), "comment missing");
        assert!(result.contains("name: app"), "existing key missing");
        assert!(result.contains("version"), "new key missing");
    }

    #[test]
    fn yaml_noop_roundtrip_preserves_comments() {
        let yaml = "# top comment\nname: app\n# section\nserver:\n  port: 8080\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        // No change — verify round-trip preserves everything.
        let result =
            serialize_value_preserving(yaml, &old, &old, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert_eq!(result, yaml, "no-op roundtrip should be identical");
    }

    #[test]
    fn yaml_ensure_nested_in_existing_subobject_preserves_top_comments() {
        // Mirrors the integration test_doc_ensure_yaml_preserves_comments
        // server.port ensure when "server" key already exists as object.
        let yaml = "# Config\nname: my-app\n\n# Server\nserver:\n  host: localhost\n";
        let old = parse_doc(yaml, &FileFormat::Yaml).unwrap();
        let mut newv = old.clone();
        set_at_path(
            &mut newv,
            &crate::selector::parse("server.port").unwrap(),
            json!(8080),
        )
        .unwrap();
        let result = serialize_value_preserving(yaml, &old, &newv, &FileFormat::Yaml).unwrap();
        // Must preserve top and section comments (the bug was falling back to plain serialize)
        assert!(
            result.contains("# Config"),
            "top comment lost in ensure nested: {result}"
        );
        assert!(
            result.contains("# Server"),
            "section comment lost: {result}"
        );
        let reparsed: serde_json::Value = serde_yaml_ng::from_str(&result).unwrap();
        assert_eq!(
            reparsed,
            json!({"name":"my-app","server":{"host":"localhost","port":8080}})
        );
    }

    #[test]
    fn yaml_section_comment_between_keys_preserved() {
        let yaml = "a: 1\n\n# Section B\nb: 2\n\n# Section C\nc: 3\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let mut new = old.clone();
        set_at_path(&mut new, &[selector::Segment::Key("b".into())], json!(99)).unwrap();

        let result =
            serialize_value_preserving(yaml, &old, &new, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert!(result.contains("# Section B"), "section B comment missing");
        assert!(result.contains("# Section C"), "section C comment missing");
        assert!(result.contains("b: 99"), "new value missing");
        assert!(!result.contains("b: 2"), "old value still present");
    }

    #[test]
    fn yaml_sequence_root_noop_preserves_content() {
        let yaml = "- item1\n- item2\n";
        let old = parse_doc(yaml, &crate::ops::doc::FileFormat::Yaml).unwrap();
        let result =
            serialize_value_preserving(yaml, &old, &old, &crate::ops::doc::FileFormat::Yaml)
                .unwrap();
        assert_eq!(result, yaml, "no-op roundtrip should be identical");
    }

    #[test]
    fn yaml_booleans_need_quoting() {
        for kw in ["true", "false", "yes", "no", "on", "off"] {
            assert!(needs_yaml_quoting(kw), "{kw} should need quoting");
        }
    }

    #[test]
    fn yaml_booleans_case_insensitive() {
        for kw in ["True", "FALSE", "Yes", "NO", "On", "OFF", "TrUe"] {
            assert!(needs_yaml_quoting(kw), "{kw} should need quoting");
        }
    }

    #[test]
    fn yaml_null_and_tilde_need_quoting() {
        assert!(needs_yaml_quoting("null"));
        assert!(needs_yaml_quoting("Null"));
        assert!(needs_yaml_quoting("NULL"));
        assert!(needs_yaml_quoting("~"));
    }

    #[test]
    fn yaml_numbers_need_quoting() {
        assert!(needs_yaml_quoting("42"));
        assert!(needs_yaml_quoting("3.14"));
        assert!(needs_yaml_quoting("-1"));
        assert!(needs_yaml_quoting("0"));
    }

    #[test]
    fn yaml_special_prefix_chars_need_quoting() {
        assert!(needs_yaml_quoting("#comment"));
        assert!(needs_yaml_quoting("&anchor"));
        assert!(needs_yaml_quoting("*alias"));
        assert!(needs_yaml_quoting("?key"));
        assert!(needs_yaml_quoting("|literal"));
        assert!(needs_yaml_quoting(">folded"));
        assert!(needs_yaml_quoting("{flow}"));
        assert!(needs_yaml_quoting("[list]"));
        assert!(needs_yaml_quoting("%directive"));
        assert!(needs_yaml_quoting("@reserved"));
        assert!(needs_yaml_quoting("`backtick"));
        assert!(needs_yaml_quoting("\"quoted"));
        assert!(needs_yaml_quoting("'squoted"));
        assert!(needs_yaml_quoting("!tag"));
    }

    #[test]
    fn yaml_trailing_colon_needs_quoting() {
        assert!(needs_yaml_quoting("host:"));
        assert!(needs_yaml_quoting("value:"));
    }

    #[test]
    fn yaml_special_floats_need_quoting() {
        assert!(needs_yaml_quoting(".inf"));
        assert!(needs_yaml_quoting("-.inf"));
        assert!(needs_yaml_quoting(".nan"));
    }

    #[test]
    fn yaml_colon_space_and_space_hash_need_quoting() {
        assert!(needs_yaml_quoting("key: value"));
        assert!(needs_yaml_quoting("hello #comment"));
    }

    ::proptest::proptest! {
        /// JSON preserving round-trip: set a key, serialize preserving, reparse.
                #[test]
                fn json_preserving_set_round_trip(
                    key in "[a-zA-Z_][a-zA-Z0-9_]{0,10}",
                    set_value in prop_oneof![
                        any::<bool>().prop_map(|b| json!(b)),
                        any::<i64>().prop_map(|n| json!(n)),
                        "[a-zA-Z0-9_]{0,20}".prop_map(|s| json!(s)),
                    ],
                ) {
                    let original_content = "{\"existing\": 1}";
                    let original_value = parse_doc(original_content, &FileFormat::Json).unwrap();
                    let mut new_value = original_value.clone();
                    let sel = selector::parse(&key).unwrap();
                    set_at_path(&mut new_value, &sel, set_value.clone()).unwrap();

                    let serialized = serialize_value_preserving(
                        original_content,
                        &original_value,
                        &new_value,
                        &FileFormat::Json,
                    ).unwrap();
                    let reparsed = parse_doc(&serialized, &FileFormat::Json).unwrap();
                    prop_assert_eq!(&new_value, &reparsed);
                }
    }
}

mod proptest {
    use super::*;
    use ::proptest::prelude::*;

    /// Generate an arbitrary JSON value (limited depth to keep tests fast).
    fn arb_json_value() -> impl Strategy<Value = serde_json::Value> {
        let leaf = prop_oneof![
            Just(json!(null)),
            any::<bool>().prop_map(|b| json!(b)),
            any::<i64>().prop_map(|n| json!(n)),
            "[a-zA-Z0-9_]{0,20}".prop_map(|s| json!(s)),
        ];
        leaf.prop_recursive(3, 32, 5, |inner| {
            prop_oneof![
                prop::collection::vec(inner.clone(), 0..5).prop_map(serde_json::Value::Array),
                prop::collection::vec(("[a-zA-Z_][a-zA-Z0-9_]{0,10}", inner), 0..5).prop_map(
                    |entries| {
                        let map: serde_json::Map<String, serde_json::Value> =
                            entries.into_iter().collect();
                        serde_json::Value::Object(map)
                    }
                ),
            ]
        })
    }

    proptest! {
        /// JSON round-trip: serialize then reparse must produce the same value.
                #[test]
                fn json_round_trip(value in arb_json_value()) {
                    let serialized = serialize_value(&value, &FileFormat::Json).unwrap();
                    let reparsed = parse_doc(&serialized, &FileFormat::Json).unwrap();
                    prop_assert_eq!(&value, &reparsed);
                }

        /// TOML round-trip for object values (TOML root must be a table).
                #[test]
                fn toml_round_trip(entries in prop::collection::vec(
                    ("[a-zA-Z_][a-zA-Z0-9_]{0,10}", prop_oneof![
                        any::<bool>().prop_map(|b| json!(b)),
                        any::<i64>().prop_map(|n| json!(n)),
                        "[a-zA-Z0-9_]{0,20}".prop_map(|s| json!(s)),
                    ]),
                    0..8
                )) {
                    let map: serde_json::Map<String, serde_json::Value> =
                        entries.into_iter().collect();
                    let value = serde_json::Value::Object(map);
                    let serialized = serialize_value(&value, &FileFormat::Toml).unwrap();
                    let reparsed = parse_doc(&serialized, &FileFormat::Toml).unwrap();
                    prop_assert_eq!(&value, &reparsed);
                }

        /// YAML round-trip: serialize then reparse must produce the same value.
                #[test]
                fn yaml_round_trip(value in arb_json_value()) {
                    let serialized = serialize_value(&value, &crate::ops::doc::FileFormat::Yaml).unwrap();
                    let reparsed = parse_doc(&serialized, &crate::ops::doc::FileFormat::Yaml).unwrap();
                    prop_assert_eq!(&value, &reparsed);
                }

        /// JSON set-then-get: setting a key and retrieving it must return the set value.
                #[test]
                fn json_set_then_get(
                    key in "[a-zA-Z_][a-zA-Z0-9_]{0,10}",
                    set_value in prop_oneof![
                        any::<bool>().prop_map(|b| json!(b)),
                        any::<i64>().prop_map(|n| json!(n)),
                        "[a-zA-Z0-9_]{0,20}".prop_map(|s| json!(s)),
                    ],
                ) {
                    let mut root = json!({});
                    let sel = selector::parse(&key).unwrap();
                    set_at_path(&mut root, &sel, set_value.clone()).unwrap();
                    let results = selector::eval(&root, &sel);
                    prop_assert_eq!(results.len(), 1);
                    prop_assert_eq!(results[0], &set_value);
                }

        /// JSON delete-then-has: deleting a key means it no longer exists.
                #[test]
                fn json_delete_then_has(
                    key in "[a-zA-Z_][a-zA-Z0-9_]{0,10}",
                ) {
                    let mut root = json!({ &key: "value" });
                    let sel = selector::parse(&key).unwrap();
                    let deleted = delete_at_selector(&mut root, &sel).unwrap();
                    prop_assert!(deleted);
                    let results = selector::eval(&root, &sel);
                    prop_assert!(results.is_empty());
                }
    }
}

#[test]
fn presentation_style_changed_flags_yaml_sequence_indent() {
    let before = "env:\n  - name: FEATURE_FLAG\n    value: off\n";
    let after = "env:\n- name: FEATURE_FLAG\n  value: on\n";
    assert!(
        presentation_style_changed(before, after, &FileFormat::Yaml),
        "indented vs collapsed block sequence must set style_changed"
    );
    let after_same = "env:\n  - name: FEATURE_FLAG\n    value: on\n";
    assert!(
        !presentation_style_changed(before, after_same, &FileFormat::Yaml),
        "same sequence indent should not flag style"
    );
    assert!(!presentation_style_changed(
        before,
        before,
        &FileFormat::Yaml
    ));
}