krabka-metadata 0.6.0

Versioned metadata record types + immutable image for Krabka
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
//! Immutable snapshot of the cluster's metadata state. Only
//! [`MetadataImage::apply`] mutates it, and the Raft state machine calls that
//! method. Everywhere else reads it through shared references and `Arc` clones.

use std::{
    collections::{BTreeMap, HashMap},
    ops::Bound,
};

use krabka_security::{KafkaPrincipal, SaslMechanism, ScramCredential};
use krabka_units::{ByteRate, convert::ByteRateExt};
use uuid::Uuid;

use crate::{
    acl::{AclEntry, PatternType, ResourceType},
    break_glass::{BreakGlassApproval, BreakGlassProposalRecord},
    error::MetadataError,
    records::{
        BrokerConfigRecord, BrokerRegistrationRecord, ClientMetricsConfigRecord, ClientQuotaRecord,
        ControllerRegistrationRecord, DEFAULT_BROKER_CONFIG_NODE_ID, DelegationTokenRecord,
        FeatureLevelRecord, FeaturesEpochRecord, GroupConfigRecord, KRaftVersionRecord,
        MetadataRecord, NodeId, PartitionElrRecord, PartitionOffsetAdvanceRecord, PartitionRecord,
        ProducerIdsRecord, QuotaEntity, ScramCredentialRecord, TopicConfigRecord, TopicRecord,
        VotersRecord,
    },
    write_freeze::TopicFreezeRecord,
};

pub type EntityKey = Vec<(String, Option<String>)>;

/// In-memory image type for a single delegation
/// token (KIP-48). Mirrors [`DelegationTokenRecord`] without any tombstone
/// concerns. The apply path handles tombstones as removals.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DelegationToken {
    pub token_id: String,
    pub owner: KafkaPrincipal,
    pub requester: KafkaPrincipal,
    pub issue_timestamp_ms: i64,
    pub expiry_timestamp_ms: i64,
    pub max_timestamp_ms: i64,
    pub renewers: Vec<KafkaPrincipal>,
}

impl DelegationToken {
    #[must_use]
    pub fn from_record(rec: &DelegationTokenRecord) -> Self {
        Self {
            token_id: rec.token_id.clone(),
            owner: rec.owner.clone(),
            requester: rec.requester.clone(),
            issue_timestamp_ms: rec.issue_timestamp_ms,
            expiry_timestamp_ms: rec.expiry_timestamp_ms,
            max_timestamp_ms: rec.max_timestamp_ms,
            renewers: rec.renewers.clone(),
        }
    }

    /// The record that recreates this token.
    #[must_use]
    pub fn to_record(&self) -> DelegationTokenRecord {
        DelegationTokenRecord {
            token_id: self.token_id.clone(),
            owner: self.owner.clone(),
            requester: self.requester.clone(),
            issue_timestamp_ms: self.issue_timestamp_ms,
            expiry_timestamp_ms: self.expiry_timestamp_ms,
            max_timestamp_ms: self.max_timestamp_ms,
            renewers: self.renewers.clone(),
        }
    }

    /// Kafka's `TokenInformation.ownerOrRenewer`: whether `principal` is the
    /// token's owner, the principal that created it, or a listed renewer.
    #[must_use]
    pub fn owner_or_renewer(&self, principal: &KafkaPrincipal) -> bool {
        &self.owner == principal
            || &self.requester == principal
            || self.renewers.contains(principal)
    }
}

#[must_use]
pub fn canonicalize_entity(mut tuple: Vec<(String, Option<String>)>) -> EntityKey {
    tuple.sort_by(|a, b| a.0.cmp(&b.0));
    tuple
}

/// Static discriminant name for a [`MetadataRecord`], used as tracing span
/// context. The span skips the record itself to keep spans cheap and free of
/// PII.
fn record_variant(rec: &MetadataRecord) -> &'static str {
    match rec {
        MetadataRecord::V1Topic(_) => "V1Topic",
        MetadataRecord::V1Partition(_) => "V1Partition",
        MetadataRecord::V1BrokerRegistration(_) => "V1BrokerRegistration",
        MetadataRecord::V1DeleteTopic(_) => "V1DeleteTopic",
        MetadataRecord::V1TopicConfig(_) => "V1TopicConfig",
        MetadataRecord::V1ScramCredential(_) => "V1ScramCredential",
        MetadataRecord::V1DeleteScramCredential(_) => "V1DeleteScramCredential",
        MetadataRecord::V1AccessControlEntry(_) => "V1AccessControlEntry",
        MetadataRecord::V1DeleteAccessControlEntry(_) => "V1DeleteAccessControlEntry",
        MetadataRecord::V1BrokerConfig(_) => "V1BrokerConfig",
        MetadataRecord::V1ClientQuota(_) => "V1ClientQuota",
        MetadataRecord::V1ProducerIds(_) => "V1ProducerIds",
        MetadataRecord::V1DelegationToken(_) => "V1DelegationToken",
        MetadataRecord::V1DeleteDelegationToken(_) => "V1DeleteDelegationToken",
        MetadataRecord::V1UnregisterBroker(_) => "V1UnregisterBroker",
        MetadataRecord::V1KRaftVersion(_) => "V1KRaftVersion",
        MetadataRecord::V1Voters(_) => "V1Voters",
        MetadataRecord::V1FeatureLevel(_) => "V1FeatureLevel",
        MetadataRecord::V1ClientMetricsConfig(_) => "V1ClientMetricsConfig",
        MetadataRecord::V1FeaturesEpoch(_) => "V1FeaturesEpoch",
        MetadataRecord::V1PartitionDirAssignment(_) => "V1PartitionDirAssignment",
        MetadataRecord::V1PartitionOffsetAdvance(_) => "V1PartitionOffsetAdvance",
        MetadataRecord::V1GroupConfig(_) => "V1GroupConfig",
        MetadataRecord::V1ControllerRegistration(_) => "V1ControllerRegistration",
        MetadataRecord::V1TopicFreeze(_) => "V1TopicFreeze",
        MetadataRecord::V1BreakGlassProposal(_) => "V1BreakGlassProposal",
        MetadataRecord::V1DeleteBreakGlassProposal(_) => "V1DeleteBreakGlassProposal",
        MetadataRecord::V1PartitionElr(_) => "V1PartitionElr",
        MetadataRecord::V1PartitionRecovery(_) => "V1PartitionRecovery",
        MetadataRecord::V1PartitionUpdate(_) => "V1PartitionUpdate",
        MetadataRecord::V1BrokerRegistrationChange(_) => "V1BrokerRegistrationChange",
        MetadataRecord::V1UnregisterController(_) => "V1UnregisterController",
    }
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct MetadataImage {
    cluster_id: Uuid,
    topics: HashMap<String, TopicRecord>,
    /// KIP-516 reverse index: topic UUID -> topic name. `apply()` maintains it
    /// next to `topics`. A snapshot replay rebuilds it, because every record
    /// flows through `apply()`, including snapshot installs.
    topic_ids: HashMap<Uuid, String>,
    /// Partition records grouped by owning topic. The by-topic grouping is
    /// the scaling-critical index: [`Self::partitions_of`] resolves a topic
    /// in O(1) and touches only that topic's partitions. Per-topic hot paths
    /// such as Metadata responses and reconcile loops therefore stay
    /// O(total partitions) and not O(topics × partitions). The inner
    /// `BTreeMap` keys by partition index, so per-topic iteration runs in
    /// ascending-index order.
    partitions: HashMap<String, BTreeMap<i32, PartitionRecord>>,
    partition_next_offsets: HashMap<(String, i32), i64>,
    partition_elr: HashMap<(String, i32), (Vec<NodeId>, Vec<NodeId>)>,
    partition_recovery: HashMap<(String, i32), crate::LeaderRecoveryState>,
    brokers: HashMap<NodeId, BrokerRegistrationRecord>,
    controllers: HashMap<NodeId, ControllerRegistrationRecord>,
    topic_configs: HashMap<String, BTreeMap<String, String>>,
    broker_configs: HashMap<NodeId, BTreeMap<String, String>>,
    client_metrics_configs: HashMap<String, BTreeMap<String, String>>,
    group_configs: HashMap<String, BTreeMap<String, String>>,
    scram_credentials: HashMap<(String, SaslMechanism), ScramCredential>,
    acls_literal: HashMap<(ResourceType, String), Vec<AclEntry>>,
    acls_prefixed: HashMap<ResourceType, Vec<AclEntry>>,
    /// KFC-9 write-freeze registry, split by pattern type. The prefixed side
    /// is a `BTreeMap` and not the `Vec` that `acls_prefixed` uses, because
    /// the resolve runs one hop from the produce path and needs the ordered
    /// reverse walk that finds the longest matching prefix.
    freezes_literal: HashMap<String, TopicFreezeRecord>,
    freezes_prefixed: BTreeMap<String, TopicFreezeRecord>,
    /// KFC-9 break-glass proposals, keyed by proposal id.
    break_glass: HashMap<Uuid, BreakGlassProposalRecord>,
    client_quotas: HashMap<EntityKey, BTreeMap<String, f64>>,
    producer_ids: Option<ProducerIdsRecord>,
    delegation_tokens: HashMap<String, DelegationToken>,
    kraft_version: u16,
    voters: crate::voters::VoterSet,
    feature_levels: BTreeMap<String, i16>,
    /// KIP-584 finalized-features epoch. It is `-1` until the first
    /// `V1FeatureLevel` record applies, and it then increases monotonically,
    /// once per applied record. It is deterministic across replicas, because
    /// records apply in committed-log order on every node.
    features_epoch: i64,
}

/// True when `candidate` is `stored` plus zero or more appended entries.
///
/// This is the concurrent-approval rule of KFC-9 in one predicate: an
/// approver may add its own entry to the list it read, and may do nothing
/// else to that list.
fn extends(stored: &[BreakGlassApproval], candidate: &[BreakGlassApproval]) -> bool {
    candidate.len() >= stored.len() && candidate[..stored.len()] == *stored
}

/// Selects which broker byte-rate config key to read.
#[derive(Debug, Clone, Copy)]
pub enum ThrottleKind {
    Leader,
    Follower,
    AlterLogDirs,
}

impl MetadataImage {
    #[must_use]
    pub fn new(cluster_id: Uuid) -> Self {
        Self {
            cluster_id,
            topics: HashMap::new(),
            topic_ids: HashMap::new(),
            partitions: HashMap::new(),
            partition_next_offsets: HashMap::new(),
            partition_elr: HashMap::new(),
            partition_recovery: HashMap::new(),
            brokers: HashMap::new(),
            controllers: HashMap::new(),
            topic_configs: HashMap::new(),
            broker_configs: HashMap::new(),
            client_metrics_configs: HashMap::new(),
            group_configs: HashMap::new(),
            scram_credentials: HashMap::new(),
            acls_literal: HashMap::new(),
            acls_prefixed: HashMap::new(),
            freezes_literal: HashMap::new(),
            freezes_prefixed: BTreeMap::new(),
            break_glass: HashMap::new(),
            client_quotas: HashMap::new(),
            producer_ids: None,
            delegation_tokens: HashMap::new(),
            kraft_version: 0,
            voters: crate::voters::VoterSet::default(),
            feature_levels: BTreeMap::new(),
            features_epoch: -1,
        }
    }

    #[must_use]
    pub fn cluster_id(&self) -> Uuid {
        self.cluster_id
    }

    pub fn topics(&self) -> impl Iterator<Item = &TopicRecord> {
        self.topics.values()
    }

    #[tracing::instrument(level = "debug", skip_all, fields(topic = %name))]
    #[must_use]
    pub fn topic(&self, name: &str) -> Option<&TopicRecord> {
        self.topics.get(name)
    }

    /// KIP-516: resolve a topic by its UUID. O(1) through the `topic_ids` index.
    #[tracing::instrument(level = "debug", skip_all, fields(topic_id = %id))]
    #[must_use]
    pub fn topic_by_id(&self, id: &Uuid) -> Option<&TopicRecord> {
        let name = self.topic_ids.get(id)?;
        self.topics.get(name)
    }

    /// KIP-516: resolve a topic name by its UUID.
    #[must_use]
    pub fn topic_name_by_id(&self, id: &Uuid) -> Option<&str> {
        self.topic_ids.get(id).map(String::as_str)
    }

    #[tracing::instrument(level = "debug", skip_all, fields(topic = %topic, partition = idx))]
    #[must_use]
    pub fn partition(&self, topic: &str, idx: i32) -> Option<&PartitionRecord> {
        self.partitions.get(topic)?.get(&idx)
    }

    /// All partitions of `topic`, in ascending partition-index order.
    /// The by-topic index gives an O(1) topic lookup and an
    /// O(own partitions) iteration. The method never scans the partitions of
    /// other topics, so a per-topic caller stays proportional to the topic it
    /// asks about, even in images with tens of thousands of topics.
    pub fn partitions_of(&self, topic: &str) -> impl Iterator<Item = &PartitionRecord> {
        self.partitions
            .get(topic)
            .into_iter()
            .flat_map(BTreeMap::values)
    }

    /// The committed next-offset for a diskless partition, if any advance has
    /// been applied.
    #[must_use]
    pub fn partition_next_offset(&self, topic: &str, partition: i32) -> Option<i64> {
        self.partition_next_offsets
            .get(&(topic.to_string(), partition))
            .copied()
    }

    #[must_use]
    pub fn partition_elr(&self, topic: &str, partition: i32) -> (&[NodeId], &[NodeId]) {
        self.partition_elr
            .get(&(topic.to_string(), partition))
            .map_or((&[], &[]), |(eligible, last_known)| {
                (eligible.as_slice(), last_known.as_slice())
            })
    }

    #[must_use]
    pub fn leader_recovery_state(&self, topic: &str, partition: i32) -> crate::LeaderRecoveryState {
        self.partition_recovery
            .get(&(topic.to_string(), partition))
            .copied()
            .unwrap_or_default()
    }

    /// The live partition count for `topic`, derived from the partitions
    /// map and not from the stored `TopicRecord.partitions` field. This is
    /// the authoritative count for the KIP-631 round-trip, because the
    /// KIP-631 `TopicRecord` carries no partition count. It is also the
    /// authoritative count for the `validate` partition-count-grew check.
    /// Returns 0 for an unknown topic, or for a topic with no applied
    /// partition records. O(1).
    #[must_use]
    pub fn topic_partition_count(&self, topic: &str) -> i32 {
        self.partitions
            .get(topic)
            .map_or(0, |parts| i32::try_from(parts.len()).unwrap_or(i32::MAX))
    }

    /// Total partition count across every topic. O(topics).
    fn total_partitions(&self) -> usize {
        self.partitions.values().map(BTreeMap::len).sum()
    }

    /// Single-pass iterator over every partition in the image. Each record
    /// carries its own `topic` and `partition` fields. Topic order is
    /// unspecified. Within a topic the order is ascending partition index.
    /// The iterator is O(P) in total partition count. The cluster-wide
    /// maintenance loops for failover, rebalance, reassignment, and metrics
    /// use this instead of a per-topic loop.
    pub fn all_partitions(&self) -> impl Iterator<Item = &PartitionRecord> {
        self.partitions.values().flat_map(BTreeMap::values)
    }

    /// All partitions where a reassignment is currently in flight, that is,
    /// where `adding_replicas` or `removing_replicas` is non-empty.
    pub fn reassignments_in_flight(&self) -> impl Iterator<Item = &PartitionRecord> + '_ {
        self.all_partitions()
            .filter(|p| !p.adding_replicas.is_empty() || !p.removing_replicas.is_empty())
    }

    /// Currently-effective config overrides for `topic`, or `None` if no
    /// `V1TopicConfig` record has applied for this topic since the last
    /// `V1DeleteTopic`, or since image creation.
    #[must_use]
    pub fn topic_config(&self, topic: &str) -> Option<&BTreeMap<String, String>> {
        self.topic_configs.get(topic)
    }

    /// Per-broker config overrides for `node_id`, or `None` if no
    /// `V1BrokerConfig` record has been applied for this broker.
    #[must_use]
    pub fn broker_config(&self, node_id: NodeId) -> Option<&BTreeMap<String, String>> {
        self.broker_configs.get(&node_id)
    }

    /// Cluster-wide dynamic broker-config defaults, or `None` when no default
    /// broker config has been applied.
    #[must_use]
    pub fn default_broker_config(&self) -> Option<&BTreeMap<String, String>> {
        self.broker_config(DEFAULT_BROKER_CONFIG_NODE_ID)
    }

    /// Returns the KIP-73 throttle rate for `node_id` and `kind`.
    /// Returns `None` if the config key is absent, unparseable, or negative.
    /// `-1` is Kafka's convention for "disabled" or "unlimited".
    #[must_use]
    pub fn broker_throttle_rate(&self, node_id: NodeId, kind: ThrottleKind) -> Option<ByteRate> {
        let key = match kind {
            ThrottleKind::Leader => "leader.replication.throttled.rate",
            ThrottleKind::Follower => "follower.replication.throttled.rate",
            ThrottleKind::AlterLogDirs => "replica.alter.log.dirs.io.max.bytes.per.second",
        };
        let raw = self
            .broker_config(node_id)
            .and_then(|configs| configs.get(key))
            .or_else(|| self.default_broker_config()?.get(key))?;
        let v: i64 = raw.parse().ok()?;
        (v >= 0).then(|| ByteRate::from_bytes_per_sec(v))
    }

    /// Override map for a single KIP-714 client-metrics subscription.
    #[must_use]
    pub fn client_metrics_config(&self, name: &str) -> Option<&BTreeMap<String, String>> {
        self.client_metrics_configs.get(name)
    }

    /// All configured client-metrics subscriptions, `(name, overrides)`.
    pub fn client_metrics_subscriptions(
        &self,
    ) -> impl Iterator<Item = (&String, &BTreeMap<String, String>)> {
        self.client_metrics_configs.iter()
    }

    /// Dynamic KIP-1071 overrides for one GROUP config resource.
    #[must_use]
    pub fn group_config(&self, group_id: &str) -> Option<&BTreeMap<String, String>> {
        self.group_configs.get(group_id)
    }

    /// All configured GROUP resources.
    pub fn group_configs(&self) -> impl Iterator<Item = (&String, &BTreeMap<String, String>)> {
        self.group_configs.iter()
    }

    #[must_use]
    pub fn client_quotas(&self) -> &HashMap<EntityKey, BTreeMap<String, f64>> {
        &self.client_quotas
    }

    #[must_use]
    pub fn scram_credential(
        &self,
        user: &str,
        mechanism: SaslMechanism,
    ) -> Option<&ScramCredential> {
        self.scram_credentials.get(&(user.to_string(), mechanism))
    }

    /// All distinct users with at least one SCRAM credential. Order is
    /// unspecified.
    #[must_use]
    pub fn scram_credentials_users(&self) -> Vec<String> {
        self.scram_credentials
            .keys()
            .map(|(u, _)| u.clone())
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect()
    }

    /// All `(mechanism, iterations)` pairs for `user`. Empty if user has
    /// no SCRAM credentials. Order is unspecified.
    #[must_use]
    pub fn scram_credentials_for_user(&self, user: &str) -> Vec<(SaslMechanism, u32)> {
        self.scram_credentials
            .iter()
            .filter(|((u, _), _)| u == user)
            .map(|((_, mech), cred)| (*mech, cred.iterations))
            .collect()
    }

    #[must_use]
    pub fn broker(&self, node_id: NodeId) -> Option<&BrokerRegistrationRecord> {
        self.brokers.get(&node_id)
    }

    #[must_use]
    pub fn controller(&self, node_id: NodeId) -> Option<&ControllerRegistrationRecord> {
        self.controllers.get(&node_id)
    }

    /// KIP-903: the broker epoch (registration commit offset) for `node_id`,
    /// or `None` if the broker is not registered in this image.
    #[must_use]
    pub fn broker_epoch(&self, node_id: NodeId) -> Option<i64> {
        self.brokers.get(&node_id).map(|b| b.broker_epoch)
    }

    /// First producer ID not covered by any committed allocation block.
    #[must_use]
    pub fn next_producer_id(&self) -> i64 {
        self.producer_ids.as_ref().map_or(0, |r| r.next_producer_id)
    }

    pub fn brokers(&self) -> impl Iterator<Item = &BrokerRegistrationRecord> {
        self.brokers.values()
    }

    pub fn controllers(&self) -> impl Iterator<Item = &ControllerRegistrationRecord> {
        self.controllers.values()
    }

    #[must_use]
    pub fn kraft_version(&self) -> u16 {
        self.kraft_version
    }

    #[must_use]
    pub fn voters(&self) -> &crate::voters::VoterSet {
        &self.voters
    }

    /// Iterate every ACL that could possibly match `(rt, rn)`:
    /// - all literal entries at `(rt, rn)`
    /// - all literal entries at `(rt, "*")`, the `WILDCARD_RESOURCE`, which
    ///   matches every resource of that type. See Kafka's `AclAuthorizer` and
    ///   `StandardAuthorizer`. This is what `kafka-acls --topic '*'` produces.
    /// - all prefixed entries whose `resource_name` is a prefix of `rn`
    pub fn matching_acls<'a>(
        &'a self,
        rt: ResourceType,
        rn: &'a str,
    ) -> impl Iterator<Item = &'a AclEntry> + 'a {
        let literal_iter = self
            .acls_literal
            .get(&(rt, rn.to_string()))
            .into_iter()
            .flatten();
        // Literal "*" is the WILDCARD_RESOURCE and matches every resource of
        // this type. Skip it when `rn` is already "*" to avoid duplicating the
        // entries already produced by `literal_iter`.
        let wildcard_iter = (rn != "*")
            .then(|| self.acls_literal.get(&(rt, "*".to_string())))
            .into_iter()
            .flatten()
            .flatten();
        let prefixed_iter = self
            .acls_prefixed
            .get(&rt)
            .into_iter()
            .flatten()
            .filter(move |e| rn.starts_with(&e.resource_name));
        literal_iter.chain(wildcard_iter).chain(prefixed_iter)
    }

    /// All ACL entries, both literal and prefixed, across all resource types.
    /// `DescribeAcls` uses this.
    pub fn all_acls(&self) -> impl Iterator<Item = &AclEntry> {
        self.acls_literal
            .values()
            .flatten()
            .chain(self.acls_prefixed.values().flatten())
    }

    /// The write-freeze entry that covers `topic`, or `None` when the topic
    /// accepts writes (KFC-9).
    ///
    /// A cluster with no freeze costs two `is_empty` tests, because the
    /// produce path calls this once per topic per request. A literal entry
    /// beats every prefix entry, and the longest matching prefix wins among
    /// the prefix entries.
    ///
    /// An internal topic is never frozen. The `__` name convention is the
    /// test, so a prefix scope of `""` or `"_"` cannot take
    /// `__consumer_offsets` down with the rest of the cluster.
    #[must_use]
    pub fn topic_freeze(&self, topic: &str) -> Option<&TopicFreezeRecord> {
        if self.freezes_literal.is_empty() && self.freezes_prefixed.is_empty() {
            return None;
        }
        if topic.starts_with("__") {
            return None;
        }
        if let Some(freeze) = self.freezes_literal.get(topic) {
            return Some(freeze);
        }
        // Keys that sort at or below `topic`, walked downwards. Among the
        // prefixes of `topic`, the longest one sorts highest, so the first
        // match is the winner. The bound pair is what keeps the lookup key a
        // borrowed `str`: `RangeBounds<str>` has no impl for `..=&str`.
        self.freezes_prefixed
            .range::<str, _>((Bound::Unbounded, Bound::Included(topic)))
            .rev()
            .find(|(scope, _)| topic.starts_with(scope.as_str()))
            .map(|(_, freeze)| freeze)
    }

    /// Every live write-freeze entry, literal and prefixed (KFC-9). A thawed
    /// entry is removed on apply, so every entry here has `frozen: true`.
    /// Order is unspecified.
    pub fn topic_freezes(&self) -> impl Iterator<Item = &TopicFreezeRecord> {
        self.freezes_literal
            .values()
            .chain(self.freezes_prefixed.values())
    }

    /// Look up a break-glass proposal by its id (KFC-9).
    #[must_use]
    pub fn break_glass_proposal(&self, id: Uuid) -> Option<&BreakGlassProposalRecord> {
        self.break_glass.get(&id)
    }

    /// Every break-glass proposal still in the image (KFC-9), including the
    /// consumed and withdrawn ones that the expiry sweep has not yet removed.
    /// Order is unspecified.
    pub fn break_glass_proposals(&self) -> impl Iterator<Item = &BreakGlassProposalRecord> {
        self.break_glass.values()
    }

    /// Look up a delegation token by its `token_id` (KIP-48).
    #[must_use]
    pub fn delegation_token_by_id(&self, token_id: &str) -> Option<&DelegationToken> {
        self.delegation_tokens.get(token_id)
    }

    /// All tokens owned by `owner` (KIP-48). The match on the owning
    /// [`KafkaPrincipal`] is exact. Order is unspecified.
    #[must_use]
    pub fn delegation_tokens_by_owner(&self, owner: &KafkaPrincipal) -> Vec<&DelegationToken> {
        self.delegation_tokens
            .values()
            .filter(|t| &t.owner == owner)
            .collect()
    }

    /// Tokens that `principal` may see through `DescribeDelegationToken`
    /// without `DescribeToken` permission: those it owns, created or may
    /// renew ([`DelegationToken::owner_or_renewer`]). Order is unspecified.
    #[must_use]
    pub fn delegation_tokens_visible_to(
        &self,
        principal: &KafkaPrincipal,
    ) -> Vec<&DelegationToken> {
        self.delegation_tokens
            .values()
            .filter(|t| t.owner_or_renewer(principal))
            .collect()
    }

    /// Every delegation token currently in the
    /// image (KIP-48). `DescribeDelegationToken` uses this for callers with
    /// `DescribeToken` permission on the cluster.
    pub fn all_delegation_tokens(&self) -> impl Iterator<Item = &DelegationToken> {
        self.delegation_tokens.values()
    }

    /// KIP-48: the delegation token whose HMAC under `secret_key` is `hmac`.
    ///
    /// `RenewDelegationToken` and `ExpireDelegationToken` identify a token by
    /// HMAC on the wire and not by `token_id`. The image keeps no HMAC, as
    /// Kafka's metadata does not, so each token's HMAC is recomputed with
    /// [`krabka_security::compute_token_hmac`]. The scan is linear over the
    /// small in-memory token map.
    #[must_use]
    pub fn delegation_token_by_hmac(
        &self,
        secret_key: &[u8],
        hmac: &[u8],
    ) -> Option<&DelegationToken> {
        self.delegation_tokens
            .values()
            .find(|t| krabka_security::compute_token_hmac(secret_key, &t.token_id) == hmac)
    }

    /// KIP-584: finalized feature levels, keyed by feature name. Empty
    /// until an `UpdateFeatures` call lands a `V1FeatureLevel` record.
    #[must_use]
    pub fn finalized_features(&self) -> &BTreeMap<String, i16> {
        &self.feature_levels
    }

    /// KIP-584 finalized-features epoch. `-1` ("unknown") until the first
    /// feature is finalized.
    #[must_use]
    pub fn finalized_features_epoch(&self) -> i64 {
        self.features_epoch
    }

    /// The finalized `metadata.version` level, or `None` if no
    /// `V1FeatureLevel` for `metadata.version` has applied. That is a
    /// pre-bootstrap or legacy image, which is `MetadataVersion.UNKNOWN`.
    #[must_use]
    pub fn finalized_metadata_version(&self) -> Option<i16> {
        self.feature_levels
            .get(crate::metadata_version::METADATA_VERSION_FEATURE)
            .copied()
    }

    /// KIP-584: the finalized level for an arbitrary feature, or `None` if it
    /// has not been finalized. Generic counterpart to
    /// [`Self::finalized_metadata_version`].
    #[must_use]
    pub fn finalized_feature(&self, name: &str) -> Option<i16> {
        self.feature_levels.get(name).copied()
    }

    /// The minimum `metadata.version` level that the live image needs: the
    /// floor that a downgrade must not drop below. It rises with feature-gated
    /// state present in the image: `KRaft` SCRAM creds, delegation tokens, and
    /// KIP-1276 CIDR ACL hosts. The baseline is `METADATA_VERSION_MIN`.
    #[must_use]
    pub fn min_required_metadata_version(&self) -> i16 {
        use crate::metadata_version::{
            CIDR_ACL_MIN_LEVEL, DELEGATION_TOKEN_MIN_LEVEL, METADATA_VERSION_MIN, SCRAM_MIN_LEVEL,
        };
        let mut floor = METADATA_VERSION_MIN;
        if !self.scram_credentials.is_empty() {
            floor = floor.max(SCRAM_MIN_LEVEL);
        }
        if !self.delegation_tokens.is_empty() {
            floor = floor.max(DELEGATION_TOKEN_MIN_LEVEL);
        }
        if self.all_acls().any(|acl| is_cidr_acl_host(&acl.host)) {
            floor = floor.max(CIDR_ACL_MIN_LEVEL);
        }
        floor
    }

    /// Metadata removals needed to materialize this image at `target`.
    /// An empty result means the downgrade is lossless. The controller emits
    /// these records before the trailing metadata.version feature record only
    /// for an explicitly unsafe downgrade.
    #[must_use]
    pub fn metadata_version_downgrade_records(&self, target: i16) -> Vec<MetadataRecord> {
        use crate::{
            metadata_version::{
                DELEGATION_TOKEN_MIN_LEVEL, DIRECTORY_ASSIGNMENT_MIN_LEVEL, SCRAM_MIN_LEVEL,
            },
            records::{DeleteDelegationTokenRecord, DeleteScramCredentialRecord},
        };

        let mut records = Vec::new();
        if target < crate::metadata_version::CIDR_ACL_MIN_LEVEL {
            let mut cidr_acls: Vec<&AclEntry> = self
                .all_acls()
                .filter(|acl| is_cidr_acl_host(&acl.host))
                .collect();
            cidr_acls.sort_by(|a, b| {
                (&a.resource_name, &a.principal, &a.host).cmp(&(
                    &b.resource_name,
                    &b.principal,
                    &b.host,
                ))
            });
            records.extend(cidr_acls.into_iter().map(|acl| {
                MetadataRecord::V1DeleteAccessControlEntry(crate::AclEntryFilter {
                    resource_type: Some(acl.resource_type),
                    resource_name: Some(acl.resource_name.clone()),
                    pattern_type: Some(acl.pattern_type),
                    principal: Some(acl.principal.clone()),
                    host: Some(acl.host.clone()),
                    operation: Some(acl.operation),
                    permission_type: Some(acl.permission_type),
                })
            }));
        }
        if target < SCRAM_MIN_LEVEL {
            records.extend(self.scram_credentials.keys().map(|(user, mechanism)| {
                MetadataRecord::V1DeleteScramCredential(DeleteScramCredentialRecord {
                    user: user.clone(),
                    mechanism: *mechanism,
                })
            }));
        }
        if target < DELEGATION_TOKEN_MIN_LEVEL {
            records.extend(self.delegation_tokens.keys().map(|token_id| {
                MetadataRecord::V1DeleteDelegationToken(DeleteDelegationTokenRecord {
                    token_id: token_id.clone(),
                })
            }));
        }
        records.extend(self.brokers.values().filter_map(|broker| {
            let mut projected = broker.clone();
            if target < DIRECTORY_ASSIGNMENT_MIN_LEVEL {
                projected.log_dirs.clear();
            }
            if target < crate::metadata_version::CORDONED_LOG_DIRS_MIN_LEVEL {
                projected.cordoned_log_dirs = None;
            }
            (projected != *broker).then_some(MetadataRecord::V1BrokerRegistration(projected))
        }));
        // Below `3.7-IV2` a partition record carries no directories, and Kafka
        // reads it back as every replica `MIGRATING`.
        if target < DIRECTORY_ASSIGNMENT_MIN_LEVEL {
            for partition in self.all_partitions() {
                records.extend(
                    partition
                        .replicas
                        .iter()
                        .zip(&partition.directories)
                        .filter(|(_, directory)| **directory != crate::directory_id::MIGRATING)
                        .map(|(replica, _)| {
                            MetadataRecord::V1PartitionDirAssignment(
                                crate::records::PartitionDirAssignmentRecord {
                                    topic: partition.topic.clone(),
                                    partition: partition.partition,
                                    replica: *replica,
                                    directory: crate::directory_id::MIGRATING,
                                },
                            )
                        }),
                );
            }
        }
        records
    }

    /// Apply one record. Returns the previous record, for `V1Topic` and
    /// `V1BrokerRegistration`, so the caller can observe overwrite cases.
    /// The method is infallible. The controller pre-validates against the
    /// current image before it submits to Raft. Apply must never fail on a
    /// committed entry.
    #[tracing::instrument(level = "info", skip_all, fields(record = record_variant(rec)))]
    // exhaustive match over MetadataRecord
    pub fn apply(&mut self, rec: &MetadataRecord) {
        match rec {
            MetadataRecord::V1Topic(t) => {
                // If a topic with this name already exists under a different
                // id, drop the stale id entry before re-indexing.
                if let Some(prev) = self.topics.get(&t.name)
                    && prev.topic_id != t.topic_id
                {
                    self.topic_ids.remove(&prev.topic_id);
                }
                self.topic_ids.insert(t.topic_id, t.name.clone());
                // The denormalized `partitions` count is a derived cache of the
                // partitions map (KIP-631 `TopicRecord` carries no partition
                // count), maintained incrementally as `V1Partition` records
                // apply. A brand-new topic starts at 0 and the partition records
                // that follow it in log order restore the real count; a
                // re-registration of an existing topic keeps its partitions in
                // the map, so preserve the running count rather than trusting
                // the (KIP-631-zeroed) record field.
                let mut rec = t.clone();
                rec.partitions = self.topics.get(&t.name).map_or(0, |prev| prev.partitions);
                self.topics.insert(t.name.clone(), rec);
            }
            MetadataRecord::V1Partition(p) => self.apply_partition(p),
            MetadataRecord::V1BrokerRegistration(b) => {
                self.brokers.insert(b.node_id, b.clone());
            }
            MetadataRecord::V1BrokerRegistrationChange(change) => {
                self.apply_registration_change(change);
            }
            MetadataRecord::V1UnregisterBroker(rec) => self.apply_unregister_broker(rec),
            MetadataRecord::V1ControllerRegistration(c) => {
                self.controllers.insert(c.node_id, c.clone());
            }
            MetadataRecord::V1UnregisterController(rec) => {
                self.controllers.remove(&rec.node_id);
            }
            MetadataRecord::V1DeleteTopic(d) => {
                if let Some(prev) = self.topics.get(&d.name) {
                    self.topic_ids.remove(&prev.topic_id);
                }
                self.topics.remove(&d.name);
                self.partitions.remove(&d.name);
                self.partition_next_offsets.retain(|(t, _), _| t != &d.name);
                self.partition_elr.retain(|(t, _), _| t != &d.name);
                self.partition_recovery.retain(|(t, _), _| t != &d.name);
                self.topic_configs.remove(&d.name);
            }
            MetadataRecord::V1TopicConfig(c) => {
                if c.overrides.is_empty() {
                    self.topic_configs.remove(&c.topic);
                } else {
                    self.topic_configs
                        .insert(c.topic.clone(), c.overrides.clone());
                }
            }
            MetadataRecord::V1ScramCredential(r) => {
                self.scram_credentials.insert(
                    (r.user.clone(), r.mechanism),
                    ScramCredential {
                        mechanism: r.mechanism,
                        salt: r.salt.clone(),
                        stored_key: r.stored_key.clone(),
                        server_key: r.server_key.clone(),
                        iterations: r.iterations,
                    },
                );
            }
            MetadataRecord::V1DeleteScramCredential(r) => {
                self.scram_credentials
                    .remove(&(r.user.clone(), r.mechanism));
            }
            MetadataRecord::V1AccessControlEntry(entry) => {
                let bucket = match entry.pattern_type {
                    PatternType::Literal => self
                        .acls_literal
                        .entry((entry.resource_type, entry.resource_name.clone()))
                        .or_default(),
                    PatternType::Prefixed => {
                        self.acls_prefixed.entry(entry.resource_type).or_default()
                    }
                };
                // Last-write-wins on full-tuple equality.
                bucket.retain(|e| e != entry);
                bucket.push(entry.clone());
            }
            MetadataRecord::V1DeleteAccessControlEntry(filter) => {
                self.acls_literal.retain(|_, v| {
                    v.retain(|e| !filter.matches(e));
                    !v.is_empty()
                });
                self.acls_prefixed.retain(|_, v| {
                    v.retain(|e| !filter.matches(e));
                    !v.is_empty()
                });
            }
            MetadataRecord::V1BrokerConfig(rec) => {
                let entry = self.broker_configs.entry(rec.node_id).or_default();
                match &rec.config_value {
                    Some(v) => {
                        entry.insert(rec.config_name.clone(), v.clone());
                    }
                    None => {
                        entry.remove(&rec.config_name);
                    }
                }
            }
            MetadataRecord::V1ClientQuota(rec) => {
                let key = canonicalize_entity(
                    rec.entity
                        .iter()
                        .map(|e| (e.entity_type.clone(), e.entity_name.clone()))
                        .collect(),
                );
                let configs = self.client_quotas.entry(key).or_default();
                match rec.config_value {
                    Some(v) => {
                        configs.insert(rec.config_key.clone(), v);
                    }
                    None => {
                        configs.remove(&rec.config_key);
                    }
                }
            }
            MetadataRecord::V1ProducerIds(rec) => {
                if rec.next_producer_id > self.next_producer_id() {
                    self.producer_ids = Some(*rec);
                } else {
                    tracing::error!(
                        current = self.next_producer_id(),
                        proposed = rec.next_producer_id,
                        "ignoring non-monotonic producer ID allocation record"
                    );
                }
            }
            // Replacement semantics (KIP-48) — same
            // `token_id` overwrites the prior entry (used by Create
            // and Renew). Tombstone removes by `token_id`.
            MetadataRecord::V1DelegationToken(rec) => {
                self.delegation_tokens
                    .insert(rec.token_id.clone(), DelegationToken::from_record(rec));
            }
            MetadataRecord::V1DeleteDelegationToken(rec) => {
                self.delegation_tokens.remove(&rec.token_id);
            }
            MetadataRecord::V1KRaftVersion(r) => {
                self.kraft_version = r.kraft_version;
            }
            MetadataRecord::V1Voters(r) => {
                self.voters = r.voters.clone();
            }
            MetadataRecord::V1FeatureLevel(rec) => {
                if rec.level == 0 {
                    self.feature_levels.remove(&rec.name);
                } else {
                    self.feature_levels.insert(rec.name.clone(), rec.level);
                }
                // Monotonic epoch: -1 -> 0 on the first record, then +1.
                self.features_epoch = self.features_epoch.saturating_add(1).max(0);
            }
            MetadataRecord::V1ClientMetricsConfig(c) => {
                if c.configs.is_empty() {
                    self.client_metrics_configs.remove(&c.name);
                } else {
                    self.client_metrics_configs
                        .insert(c.name.clone(), c.configs.clone());
                }
            }
            MetadataRecord::V1GroupConfig(c) => {
                if c.configs.is_empty() {
                    self.group_configs.remove(&c.group_id);
                } else {
                    self.group_configs
                        .insert(c.group_id.clone(), c.configs.clone());
                }
            }
            // Snapshot-only: restore the epoch verbatim rather than bumping.
            // Emitted last by `to_records` so it overrides the value the
            // preceding `V1FeatureLevel` replays accumulated. Never reaches
            // the live log (not a controller-submittable change).
            MetadataRecord::V1FeaturesEpoch(rec) => {
                self.features_epoch = rec.epoch;
            }
            // KIP-858 directory-assignment delta: merge ONLY the reporting
            // replica's `directories` slot. Never touches leader/isr/replicas/
            // adding/removing, so a concurrent reassignment or ISR change is
            // preserved regardless of commit order.
            MetadataRecord::V1PartitionDirAssignment(r) => {
                if let Some(pr) = self
                    .partitions
                    .get_mut(&r.topic)
                    .and_then(|parts| parts.get_mut(&r.partition))
                    && let Some(slot) = pr.replicas.iter().position(|n| *n == r.replica)
                {
                    // An empty list is every replica `MIGRATING`, as Kafka's
                    // `PartitionRegistration.defaultToMigrating` reads it.
                    if pr.directories.len() < pr.replicas.len() {
                        pr.directories
                            .resize(pr.replicas.len(), crate::directory_id::MIGRATING);
                    }
                    pr.directories[slot] = r.directory;
                }
            }
            // Diskless offset delta: bump the partition's committed next-offset.
            MetadataRecord::V1PartitionOffsetAdvance(r) => {
                *self
                    .partition_next_offsets
                    .entry((r.topic.clone(), r.partition))
                    .or_insert(0) += r.count;
            }
            MetadataRecord::V1PartitionElr(r) => self.apply_partition_elr(r),
            MetadataRecord::V1PartitionRecovery(r) => {
                self.apply_partition_recovery(&r.topic, r.partition, r.state);
            }
            MetadataRecord::V1PartitionUpdate(r) => self.apply_partition_update(r),
            // KFC-9 write-freeze registry.
            MetadataRecord::V1TopicFreeze(r) => self.apply_topic_freeze(r),
            // KFC-9 break-glass proposal. Replacement semantics on the id:
            // an approval and a consumption each write the whole record back.
            // `validate` is what stops a replacement from losing an approval.
            MetadataRecord::V1BreakGlassProposal(r) => {
                self.break_glass.insert(r.proposal_id, r.clone());
            }
            MetadataRecord::V1DeleteBreakGlassProposal(id) => {
                self.break_glass.remove(id);
            }
        }
    }

    /// Kafka's `ClusterControlManager.replayRegistrationChange` applies the
    /// change only to the registration at the record's epoch. An unknown
    /// broker or another epoch is a no-op.
    fn apply_registration_change(
        &mut self,
        change: &crate::records::BrokerRegistrationChangeRecord,
    ) {
        if let Some(broker) = self.brokers.get_mut(&change.node_id)
            && broker.broker_epoch == change.broker_epoch
        {
            *broker = change.applied_to(broker);
        }
    }

    /// Kafka's `ClusterControlManager.replay(UnregisterBrokerRecord)` removes
    /// only the registration at the record's epoch. An unknown broker or
    /// another epoch is a no-op.
    fn apply_unregister_broker(&mut self, rec: &crate::records::UnregisterBrokerRecord) {
        if self.broker_epoch(rec.node_id) == Some(rec.broker_epoch) {
            self.brokers.remove(&rec.node_id);
        }
    }

    fn apply_partition(&mut self, partition: &PartitionRecord) {
        let is_new = self
            .partitions
            .entry(partition.topic.clone())
            .or_default()
            .insert(partition.partition, partition.clone())
            .is_none();
        if let Some(topic) = self.topics.get_mut(&partition.topic) {
            if is_new {
                topic.partitions = topic.partitions.saturating_add(1);
            }
            topic.replication_factor =
                i16::try_from(partition.replicas.len()).unwrap_or(topic.replication_factor);
        }
    }

    fn apply_partition_update(&mut self, update: &crate::PartitionUpdateRecord) {
        let key = (update.partition.topic.clone(), update.partition.partition);
        self.apply_partition(&update.partition);
        if let (Some(eligible), Some(last_known)) =
            (&update.eligible_leader_replicas, &update.last_known_elr)
        {
            self.apply_partition_elr(&PartitionElrRecord {
                topic: key.0.clone(),
                partition: key.1,
                eligible_leader_replicas: eligible.clone(),
                last_known_elr: last_known.clone(),
            });
        }
        if let Some(state) = update.recovery_state {
            self.apply_partition_recovery(&key.0, key.1, state);
        }
    }

    fn apply_partition_elr(&mut self, record: &PartitionElrRecord) {
        let key = (record.topic.clone(), record.partition);
        if record.eligible_leader_replicas.is_empty() && record.last_known_elr.is_empty() {
            self.partition_elr.remove(&key);
        } else {
            self.partition_elr.insert(
                key,
                (
                    record.eligible_leader_replicas.clone(),
                    record.last_known_elr.clone(),
                ),
            );
        }
    }

    fn apply_partition_recovery(
        &mut self,
        topic: &str,
        partition: i32,
        state: crate::LeaderRecoveryState,
    ) {
        let key = (topic.to_owned(), partition);
        if state == crate::LeaderRecoveryState::Recovered {
            self.partition_recovery.remove(&key);
        } else {
            self.partition_recovery.insert(key, state);
        }
    }

    /// Upsert or remove one write-freeze registry entry (KFC-9).
    ///
    /// `frozen` is the sentinel: a `true` record upserts the entry into the
    /// registry that its pattern type selects, and a `false` record removes
    /// it. That keeps the thaw attributed in the raft log and needs no second
    /// record type.
    fn apply_topic_freeze(&mut self, rec: &TopicFreezeRecord) {
        match (rec.pattern_type, rec.frozen) {
            (PatternType::Literal, true) => {
                self.freezes_literal.insert(rec.scope.clone(), rec.clone());
            }
            (PatternType::Literal, false) => {
                self.freezes_literal.remove(&rec.scope);
            }
            (PatternType::Prefixed, true) => {
                self.freezes_prefixed.insert(rec.scope.clone(), rec.clone());
            }
            (PatternType::Prefixed, false) => {
                self.freezes_prefixed.remove(&rec.scope);
            }
        }
    }

    /// Serialize the image into the minimal sequence of [`MetadataRecord`]s
    /// whose in-order [`apply`](Self::apply) reproduces this image exactly.
    /// This is the inverse of `apply` over a sequence of non-tombstone
    /// records: each stored entry maps to the record that would create it.
    ///
    /// The method deliberately never emits tombstone or removal records:
    /// `V1DeleteTopic`, `V1DeleteScramCredential`,
    /// `V1DeleteAccessControlEntry`, `V1DeleteDelegationToken`, and
    /// `V1UnregisterBroker`. A snapshot captures resulting *state*, not
    /// deletion history. `V1BrokerConfig` deletes vanish in the same way. The
    /// method emits only the surviving key and value pairs, as `Some(value)`
    /// sets.
    ///
    /// The method emits records in dependency order, so a fresh image that
    /// `apply`s them never sees a dangling reference: brokers,
    /// broker-configs, topics, partitions, topic-configs, SCRAM creds,
    /// ACLs, client quotas, delegation tokens.
    #[tracing::instrument(
        level = "debug",
        skip_all,
        fields(topics = self.topics.len(), partitions = self.total_partitions())
    )]
    #[must_use]
    pub fn to_records(&self) -> Vec<MetadataRecord> {
        let mut out = Vec::new();

        // Brokers before their configs.
        for b in self.brokers.values() {
            out.push(MetadataRecord::V1BrokerRegistration(b.clone()));
        }
        for controller in self.controllers.values() {
            out.push(MetadataRecord::V1ControllerRegistration(controller.clone()));
        }
        for (node_id, configs) in &self.broker_configs {
            for (config_name, config_value) in configs {
                out.push(MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
                    node_id: *node_id,
                    config_name: config_name.clone(),
                    config_value: Some(config_value.clone()),
                }));
            }
        }

        // Topics before partitions and topic-configs (apply-side validate
        // and the snapshot's own DeleteTopic-clears-configs invariant both
        // assume the topic exists first).
        for t in self.topics.values() {
            out.push(MetadataRecord::V1Topic(t.clone()));
        }
        for p in self.all_partitions() {
            out.push(MetadataRecord::V1Partition(p.clone()));
        }
        self.push_partition_offset_advances(&mut out);
        for ((topic, partition), (eligible, last_known)) in &self.partition_elr {
            out.push(MetadataRecord::V1PartitionElr(crate::PartitionElrRecord {
                topic: topic.clone(),
                partition: *partition,
                eligible_leader_replicas: eligible.clone(),
                last_known_elr: last_known.clone(),
            }));
        }
        for ((topic, partition), state) in &self.partition_recovery {
            out.push(MetadataRecord::V1PartitionRecovery(
                crate::PartitionRecoveryRecord {
                    topic: topic.clone(),
                    partition: *partition,
                    state: *state,
                },
            ));
        }
        for (topic, overrides) in &self.topic_configs {
            out.push(MetadataRecord::V1TopicConfig(TopicConfigRecord {
                topic: topic.clone(),
                overrides: overrides.clone(),
            }));
        }

        // SCRAM credentials.
        for ((user, mechanism), cred) in &self.scram_credentials {
            out.push(MetadataRecord::V1ScramCredential(ScramCredentialRecord {
                user: user.clone(),
                mechanism: *mechanism,
                salt: cred.salt.clone(),
                stored_key: cred.stored_key.clone(),
                server_key: cred.server_key.clone(),
                iterations: cred.iterations,
            }));
        }

        // ACLs (literal + prefixed). Each stored entry already carries its
        // own pattern_type, so re-applying routes it back to the right
        // bucket.
        for entry in self.all_acls() {
            out.push(MetadataRecord::V1AccessControlEntry(entry.clone()));
        }

        // Client quotas. The key is the canonicalized entity tuple; rebuild
        // a QuotaEntity list from it and emit one record per config pair.
        // apply re-canonicalizes, so the round-trip is stable.
        for (entity_key, configs) in &self.client_quotas {
            let entity: Vec<QuotaEntity> = entity_key
                .iter()
                .map(|(entity_type, entity_name)| QuotaEntity {
                    entity_type: entity_type.clone(),
                    entity_name: entity_name.clone(),
                })
                .collect();
            for (config_key, config_value) in configs {
                out.push(MetadataRecord::V1ClientQuota(ClientQuotaRecord {
                    entity: entity.clone(),
                    config_key: config_key.clone(),
                    config_value: Some(*config_value),
                }));
            }
        }

        if let Some(record) = self.producer_ids {
            out.push(MetadataRecord::V1ProducerIds(record));
        }

        // Delegation tokens.
        for tok in self.delegation_tokens.values() {
            out.push(MetadataRecord::V1DelegationToken(DelegationTokenRecord {
                token_id: tok.token_id.clone(),
                owner: tok.owner.clone(),
                requester: tok.requester.clone(),
                issue_timestamp_ms: tok.issue_timestamp_ms,
                expiry_timestamp_ms: tok.expiry_timestamp_ms,
                max_timestamp_ms: tok.max_timestamp_ms,
                renewers: tok.renewers.clone(),
            }));
        }

        // Client-metrics subscriptions (KIP-714).
        for (name, configs) in &self.client_metrics_configs {
            out.push(MetadataRecord::V1ClientMetricsConfig(
                ClientMetricsConfigRecord {
                    name: name.clone(),
                    configs: configs.clone(),
                },
            ));
        }

        // KIP-1071 dynamic GROUP resources.
        for (group_id, configs) in &self.group_configs {
            out.push(MetadataRecord::V1GroupConfig(GroupConfigRecord {
                group_id: group_id.clone(),
                configs: configs.clone(),
            }));
        }

        // KFC-9 write-freeze registry and break-glass proposals. Both are
        // independent of every other record, and both are lost without this:
        // a snapshot install would thaw the cluster and drop every pending
        // approval. Every stored freeze has `frozen: true`, so re-applying it
        // routes it back to the same registry.
        for freeze in self.topic_freezes() {
            out.push(MetadataRecord::V1TopicFreeze(freeze.clone()));
        }
        for proposal in self.break_glass.values() {
            out.push(MetadataRecord::V1BreakGlassProposal(proposal.clone()));
        }

        // KIP-584 finalized features: one record per live feature, in the
        // BTreeMap's deterministic key order. (level == 0 is the delete
        // sentinel and is never stored, so every emitted level is >= 1 and
        // re-applies as a set.) The trailing V1FeaturesEpoch then pins the
        // epoch verbatim — the preceding feature records each bump it, but the
        // bumped value reflects the live feature count, not the original apply
        // history, so it must be overwritten. Emitted whenever any feature was
        // ever finalized (epoch left -1 only on a pristine image), so an image
        // that finalized then removed every feature still restores its epoch.
        for (name, level) in &self.feature_levels {
            out.push(MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
                name: name.clone(),
                level: *level,
            }));
        }
        if self.features_epoch >= 0 {
            out.push(MetadataRecord::V1FeaturesEpoch(FeaturesEpochRecord {
                epoch: self.features_epoch,
            }));
        }

        // KIP-853 controller-quorum state. Both MUST be emitted or a snapshot
        // recovery / learner install silently drops them: the voter set goes
        // empty (breaking `DescribeQuorum` and KIP-853 auto-join) and the
        // cluster `kraft.version` reverts to 0. The cluster `kraft_version`
        // in particular has no other persistence path — unlike the openraft
        // membership (restored from `SnapshotMeta.last_membership`), it is not
        // carried anywhere outside the metadata records, so the image is its
        // only source of truth. Independent of every other record, so order
        // here is irrelevant. The reconfig coordinator commits the openraft
        // membership change and the authoritative `V1Voters` record under one
        // lock, so the emitted voter set mirrors `last_membership` for the
        // same committed prefix (any transient one-reconfig skew self-heals as
        // the trailing entry replays from the retained log — the same
        // eventual lockstep that holds in live operation).
        if self.kraft_version != 0 {
            out.push(MetadataRecord::V1KRaftVersion(KRaftVersionRecord {
                kraft_version: self.kraft_version,
            }));
        }
        if !self.voters.is_empty() {
            out.push(MetadataRecord::V1Voters(VotersRecord {
                voters: self.voters.clone(),
            }));
        }

        out
    }

    fn push_partition_offset_advances(&self, out: &mut Vec<MetadataRecord>) {
        for ((topic, partition), count) in &self.partition_next_offsets {
            out.push(MetadataRecord::V1PartitionOffsetAdvance(
                PartitionOffsetAdvanceRecord {
                    topic: topic.clone(),
                    partition: *partition,
                    count: *count,
                },
            ));
        }
    }

    /// Reconstruct an image from a `cluster_id` and a record sequence,
    /// typically the output of [`Self::to_records`] read back from a snapshot.
    /// The method `new`s an empty image and `apply`s each record in order.
    #[tracing::instrument(level = "info", skip_all, fields(records = records.len()))]
    #[must_use]
    pub fn from_records(cluster_id: Uuid, records: &[MetadataRecord]) -> Self {
        let mut image = Self::new(cluster_id);
        for rec in records {
            image.apply(rec);
        }
        image
    }

    /// Synchronous pre-validation: returns `Ok` if the record would be a
    /// no-conflict apply, otherwise the appropriate error.
    /// `Controller::submit_change` uses this before it forwards to openraft.
    ///
    /// # Errors
    /// Returns the conflict that would prevent the record from being applied.
    #[tracing::instrument(level = "debug", skip_all, fields(record = record_variant(rec)), err)]
    pub fn validate(&self, rec: &MetadataRecord) -> Result<(), MetadataError> {
        match rec {
            MetadataRecord::V1Topic(t) => {
                if let Some(existing) = self.topics.get(&t.name) {
                    // Updating an existing topic is allowed only if it's a
                    // strict partition-count expansion that preserves
                    // identity: same topic_id, same replication_factor,
                    // partitions strictly growing. CreatePartitions emits
                    // exactly this. Identical re-submits stay rejected so
                    // CreateTopics' idempotency contract still holds.
                    // Partition count derives from the partitions map (the
                    // KIP-631 round-trip does not carry the stored
                    // `TopicRecord.partitions`), so the expansion check reads
                    // the live count rather than `existing.partitions`.
                    if existing.topic_id != t.topic_id
                        || existing.replication_factor != t.replication_factor
                        || t.partitions <= self.topic_partition_count(&t.name)
                    {
                        return Err(MetadataError::TopicExists(t.name.clone()));
                    }
                    return Ok(());
                }
                // A new topic is valid regardless of the `TopicRecord.partitions`
                // count: KIP-631 framing does not carry that field, so a
                // freshly-decoded `V1Topic` round-trips back as partitions=0 with
                // the real count arriving via the following `V1Partition` records.
                // Partition-count validity (`> 0`) is enforced at the CreateTopics
                // request boundary, not on the metadata-record apply path.
                Ok(())
            }
            MetadataRecord::V1Partition(p) => {
                if !self.topics.contains_key(&p.topic) {
                    return Err(MetadataError::UnknownTopic(p.topic.clone()));
                }
                Ok(())
            }
            MetadataRecord::V1PartitionUpdate(update) => {
                if !self.topics.contains_key(&update.partition.topic) {
                    return Err(MetadataError::UnknownTopic(update.partition.topic.clone()));
                }
                Ok(())
            }
            MetadataRecord::V1PartitionElr(record) => {
                if self.partition(&record.topic, record.partition).is_none() {
                    return Err(MetadataError::UnknownTopic(record.topic.clone()));
                }
                Ok(())
            }
            MetadataRecord::V1PartitionRecovery(record) => {
                if self.partition(&record.topic, record.partition).is_none() {
                    return Err(MetadataError::UnknownTopic(record.topic.clone()));
                }
                Ok(())
            }
            MetadataRecord::V1DeleteTopic(d) => {
                if !self.topics.contains_key(&d.name) {
                    return Err(MetadataError::UnknownTopic(d.name.clone()));
                }
                Ok(())
            }
            MetadataRecord::V1TopicConfig(c) => {
                if !self.topics.contains_key(&c.topic) {
                    return Err(MetadataError::UnknownTopic(c.topic.clone()));
                }
                Ok(())
            }
            // Kafka's `ClusterControlManager` refuses to replay a registration
            // change or an unregistration unless the broker is registered at
            // the record's epoch, and a controller unregistration unless the
            // controller is registered. Apply treats those as no-ops, so they
            // are refused here, where the submitter still learns of it.
            MetadataRecord::V1BrokerRegistrationChange(change) => {
                if self.broker_epoch(change.node_id) != Some(change.broker_epoch) {
                    return Err(MetadataError::InvalidRecord(
                        "broker registration change for an epoch the broker is not registered at",
                    ));
                }
                Ok(())
            }
            MetadataRecord::V1UnregisterBroker(rec) => {
                if self.broker_epoch(rec.node_id) != Some(rec.broker_epoch) {
                    return Err(MetadataError::InvalidRecord(
                        "broker unregistration for an epoch the broker is not registered at",
                    ));
                }
                Ok(())
            }
            MetadataRecord::V1UnregisterController(rec) => {
                if !self.controllers.contains_key(&rec.node_id) {
                    return Err(MetadataError::InvalidRecord(
                        "controller unregistration for a controller that is not registered",
                    ));
                }
                Ok(())
            }
            MetadataRecord::V1BrokerRegistration(_)
            | MetadataRecord::V1ControllerRegistration(_)
            | MetadataRecord::V1ScramCredential(_)
            | MetadataRecord::V1DeleteScramCredential(_)
            | MetadataRecord::V1AccessControlEntry(_)
            | MetadataRecord::V1DeleteAccessControlEntry(_)
            | MetadataRecord::V1BrokerConfig(_)
            | MetadataRecord::V1ClientQuota(_)
            // Delegation-token records have no
            // topic-store concerns and admission is gated by the
            // handler-side checks (KIP-48 §protocol errors), so the
            // image-level validate is unconditional Ok.
            | MetadataRecord::V1DelegationToken(_)
            | MetadataRecord::V1DeleteDelegationToken(_)
            // KIP-853: voter-set / kraft.version records are validated by
            // the reconfiguration coordinator before submission; the
            // image-level apply is an unconditional replacement.
            | MetadataRecord::V1KRaftVersion(_)
            | MetadataRecord::V1Voters(_)
            // KIP-584: feature-level admission is fully gated by the
            // UpdateFeatures handler (supported-range + downgrade checks);
            // image-level apply is an idempotent map upsert.
            | MetadataRecord::V1FeatureLevel(_)
            // KIP-714: client-metrics subscription config. The
            // IncrementalAlterConfigs handler validates the subscription
            // name and config keys before submission; image-level apply
            // is an idempotent map upsert (or removal on empty map).
            | MetadataRecord::V1ClientMetricsConfig(_)
            // KIP-1071: GROUP config keys and values are validated by the
            // IncrementalAlterConfigs handler before this whole-map upsert.
            | MetadataRecord::V1GroupConfig(_)
            // Snapshot-only epoch carrier: only ever produced by `to_records`
            // and replayed on snapshot install, never submitted as a change.
            // Validated permissively for match-exhaustiveness.
            | MetadataRecord::V1FeaturesEpoch(_)
            // KIP-858 directory-assignment delta: a merge into one replica's
            // `directories` slot. The handler already resolved topic/partition/
            // replica against the image; apply is an idempotent slot upsert
            // (a no-op if the partition or replica is unknown).
            | MetadataRecord::V1PartitionDirAssignment(_)
            // Diskless offset-sequencer delta. The handler supplies a positive
            // count; image-level apply is an unconditional increment.
            | MetadataRecord::V1PartitionOffsetAdvance(_)
            // KFC-9: the expiry sweep removes a proposal by id, and apply is
            // idempotent against an id that is already gone.
            | MetadataRecord::V1DeleteBreakGlassProposal(_) => Ok(()),
            // KFC-9: a freeze scope must not be empty, and that is the only
            // check. The scope deliberately does NOT have to name an existing
            // topic: a prefix scope names none, and a freeze put on a
            // namespace BEFORE a restore writes into it is the
            // disaster-recovery case the feature exists for.
            MetadataRecord::V1TopicFreeze(freeze) => {
                if freeze.scope.is_empty() {
                    return Err(MetadataError::EmptyFreezeScope);
                }
                Ok(())
            }
            // KFC-9: the concurrent-approval rule. Two approvers read the
            // same image and each submits the approval list it read plus its
            // own entry. Without this check the second write overwrites the
            // first, and the proposal never reaches two principals. So an
            // incoming list must be the stored list plus zero or more
            // appended entries, and a settled proposal takes no change at
            // all.
            MetadataRecord::V1BreakGlassProposal(proposal) => {
                let Some(stored) = self.break_glass.get(&proposal.proposal_id) else {
                    return Ok(());
                };
                if stored.consumed_at_ms != 0 {
                    return Err(MetadataError::BreakGlassProposalConsumed(
                        proposal.proposal_id,
                    ));
                }
                if stored.withdrawn {
                    return Err(MetadataError::BreakGlassProposalWithdrawn(
                        proposal.proposal_id,
                    ));
                }
                if !extends(&stored.approvals, &proposal.approvals) {
                    return Err(MetadataError::BreakGlassApprovalsNotAnExtension(
                        proposal.proposal_id,
                    ));
                }
                Ok(())
            }
            MetadataRecord::V1ProducerIds(record) => {
                if record.next_producer_id <= self.next_producer_id() {
                    return Err(MetadataError::InvalidRecord(
                        "next producer ID must advance monotonically",
                    ));
                }
                if self.broker_epoch(record.broker_id) != Some(record.broker_epoch) {
                    return Err(MetadataError::InvalidRecord(
                        "producer ID allocation has a stale broker epoch",
                    ));
                }
                Ok(())
            }
        }
    }
}

/// KIP-1276: Kafka's `AclControlManager.validateHostPattern` treats any ACL
/// host containing `/` as a CIDR block, which needs `metadata.version`
/// `4.4-IV1`.
fn is_cidr_acl_host(host: &str) -> bool {
    host.contains('/')
}

#[cfg(test)]
mod tests {
    use krabka_units::bytes_per_sec;

    use super::*;

    /// `record_variant` maps each enum variant to its exact discriminant
    /// name. Assertions on concrete mappings kill whole-fn replacements, for
    /// example a return of `""` or of a constant.
    /// Three readers on the image had no caller in any test: a
    /// `group_configs` or `controllers` replaced by an empty iterator, and a
    /// `controller` replaced by `None`, all describe a cluster with nothing
    /// registered -- which is what an image looks like before any record is
    /// applied, so nothing else notices.
    #[test]
    fn group_config_and_controller_readers_report_applied_records() {
        let mut image = MetadataImage::new(Uuid::nil());
        image.apply(&MetadataRecord::V1GroupConfig(
            crate::records::GroupConfigRecord {
                group_id: "streams-app".into(),
                configs: BTreeMap::from([("streams.num.standby.replicas".into(), "1".into())]),
            },
        ));
        image.apply(&MetadataRecord::V1ControllerRegistration(
            crate::records::ControllerRegistrationRecord {
                node_id: NodeId(3),
                incarnation_id: Uuid::from_u128(7),
                zk_migration_ready: false,
                endpoints: Vec::new(),
                features: BTreeMap::new(),
            },
        ));

        let groups: Vec<_> = image
            .group_configs()
            .map(|(id, cfg)| (id.clone(), cfg.clone()))
            .collect();
        check!(
            groups
                == vec![(
                    "streams-app".to_string(),
                    BTreeMap::from([("streams.num.standby.replicas".to_string(), "1".to_string())])
                )]
        );

        let ids: Vec<_> = image.controllers().map(|c| c.node_id).collect();
        check!(ids == vec![NodeId(3)]);

        // Present and absent, so a reader stuck on `None` is not merely right
        // about the node that is missing.
        check!(image.controller(NodeId(3)).map(|c| c.incarnation_id) == Some(Uuid::from_u128(7)));
        check!(image.controller(NodeId(99)).is_none());
    }

    fn registered_broker(node_id: u64, broker_epoch: i64) -> BrokerRegistrationRecord {
        BrokerRegistrationRecord {
            node_id: NodeId(node_id),
            broker_epoch,
            incarnation_id: Uuid::from_u128(u128::from(node_id)),
            host: format!("broker-{node_id}"),
            port: 9092,
            rack: None,
            endpoints: vec![],
            log_dirs: vec![],
            fenced: true,
            in_controlled_shutdown: false,
            cordoned_log_dirs: None,
            features: BTreeMap::new(),
        }
    }

    /// Kafka's `ClusterControlManager` replays a `BrokerRegistrationChangeRecord`
    /// or an `UnregisterBrokerRecord` only against the registration at the
    /// record's epoch. Validate refuses the others and apply ignores them.
    #[test]
    fn registration_change_and_unregister_apply_only_at_the_registered_epoch() {
        use crate::records::{
            BrokerRegistrationChangeRecord, FencingChange, UnregisterBrokerRecord,
        };

        let registered = registered_broker(4, 12);
        let unfenced = BrokerRegistrationRecord {
            fenced: false,
            ..registered.clone()
        };
        let unfence = |node_id, broker_epoch| {
            MetadataRecord::V1BrokerRegistrationChange(BrokerRegistrationChangeRecord {
                fenced: FencingChange::Unfence,
                ..BrokerRegistrationChangeRecord::no_change(NodeId(node_id), broker_epoch)
            })
        };
        let unregister = |node_id, broker_epoch| {
            MetadataRecord::V1UnregisterBroker(UnregisterBrokerRecord {
                node_id: NodeId(node_id),
                broker_epoch,
            })
        };
        let stale_change = Err(MetadataError::InvalidRecord(
            "broker registration change for an epoch the broker is not registered at",
        ));
        let stale_unregister = Err(MetadataError::InvalidRecord(
            "broker unregistration for an epoch the broker is not registered at",
        ));
        for (case, record, want_validate, want_broker) in [
            (
                "unfence at the epoch",
                unfence(4, 12),
                Ok(()),
                Some(unfenced),
            ),
            (
                "unfence at a stale epoch",
                unfence(4, 11),
                stale_change.clone(),
                Some(registered.clone()),
            ),
            (
                "unfence an unknown broker",
                unfence(5, 12),
                stale_change,
                Some(registered.clone()),
            ),
            ("unregister at the epoch", unregister(4, 12), Ok(()), None),
            (
                "unregister at a stale epoch",
                unregister(4, 11),
                stale_unregister.clone(),
                Some(registered.clone()),
            ),
            (
                "unregister an unknown broker",
                unregister(5, 12),
                stale_unregister,
                Some(registered.clone()),
            ),
        ] {
            let mut image = MetadataImage::new(Uuid::nil());
            image.apply(&MetadataRecord::V1BrokerRegistration(registered.clone()));
            check!(image.validate(&record) == want_validate, "{case}");
            image.apply(&record);
            check!(image.broker(NodeId(4)).cloned() == want_broker, "{case}");
        }
    }

    /// KIP-1312: `UnregisterControllerRecord` removes the controller's
    /// registration. Kafka refuses to replay it for an unregistered id.
    #[test]
    fn unregister_controller_removes_the_registration() {
        use crate::records::{ControllerRegistrationRecord, UnregisterControllerRecord};

        let mut image = MetadataImage::new(Uuid::nil());
        for node_id in [3000, 3001] {
            image.apply(&MetadataRecord::V1ControllerRegistration(
                ControllerRegistrationRecord {
                    node_id: NodeId(node_id),
                    incarnation_id: Uuid::from_u128(u128::from(node_id)),
                    zk_migration_ready: false,
                    endpoints: Vec::new(),
                    features: BTreeMap::new(),
                },
            ));
        }
        let unregister = |node_id| {
            MetadataRecord::V1UnregisterController(UnregisterControllerRecord {
                node_id: NodeId(node_id),
            })
        };

        check!(image.validate(&unregister(3000)) == Ok(()));
        image.apply(&unregister(3000));
        let ids: Vec<_> = image.controllers().map(|c| c.node_id).collect();
        check!(ids == vec![NodeId(3001)]);

        check!(
            image.validate(&unregister(3000))
                == Err(MetadataError::InvalidRecord(
                    "controller unregistration for a controller that is not registered",
                ))
        );
        image.apply(&unregister(3000));
        let ids: Vec<_> = image.controllers().map(|c| c.node_id).collect();
        check!(ids == vec![NodeId(3001)]);
    }

    /// Deleting a topic drops that topic's offset sequencer state and leaves
    /// every other topic's alone. The retain predicate is what decides which is
    /// which: inverted, deleting one topic keeps only that topic's offsets and
    /// discards the rest of the cluster's.
    #[test]
    fn deleting_a_topic_drops_only_its_own_partition_offsets() {
        let mut image = MetadataImage::new(Uuid::nil());
        for topic in ["orders", "payments"] {
            image.apply(&MetadataRecord::V1PartitionOffsetAdvance(
                crate::records::PartitionOffsetAdvanceRecord {
                    topic: topic.into(),
                    partition: 0,
                    count: 5,
                },
            ));
        }
        check!(
            (
                image.partition_next_offset("orders", 0),
                image.partition_next_offset("payments", 0),
            ) == (Some(5), Some(5))
        );

        image.apply(&MetadataRecord::V1DeleteTopic(
            crate::records::DeleteTopicRecord {
                name: "orders".into(),
            },
        ));
        check!(
            (
                image.partition_next_offset("orders", 0),
                image.partition_next_offset("payments", 0),
            ) == (None, Some(5))
        );
    }

    fn broker_at(node_id: NodeId, epoch: i64) -> MetadataRecord {
        MetadataRecord::V1BrokerRegistration(crate::records::BrokerRegistrationRecord {
            node_id,
            broker_epoch: epoch,
            incarnation_id: Uuid::nil(),
            host: "h".into(),
            port: 9092,
            rack: None,
            log_dirs: vec![],
            fenced: false,
            in_controlled_shutdown: false,
            cordoned_log_dirs: None,
            endpoints: vec![],
            features: BTreeMap::new(),
        })
    }

    /// `validate` gates producer-ID allocation on two conditions, and both
    /// boundaries were unasserted: the id must strictly advance, and the
    /// record's broker epoch must be the epoch the image holds. Inverted, the
    /// first accepts exactly the replays it exists to reject, and the second
    /// accepts only the stale epochs.
    #[test]
    fn producer_id_allocation_must_advance_and_carry_the_live_epoch() {
        let mut image = MetadataImage::new(Uuid::nil());
        image.apply(&broker_at(NodeId(1), 11));
        image.apply(&MetadataRecord::V1ProducerIds(
            crate::records::ProducerIdsRecord {
                broker_id: NodeId(1),
                broker_epoch: 11,
                next_producer_id: 1000,
            },
        ));

        let allocation = |next: i64, epoch: i64| {
            MetadataRecord::V1ProducerIds(crate::records::ProducerIdsRecord {
                broker_id: NodeId(1),
                broker_epoch: epoch,
                next_producer_id: next,
            })
        };

        check!(image.validate(&allocation(2000, 11)).is_ok());
        // Equal is not an advance, and neither is going backwards.
        check!(image.validate(&allocation(1000, 11)).is_err());
        check!(image.validate(&allocation(999, 11)).is_err());
        // A stale epoch is refused even when the id does advance.
        check!(image.validate(&allocation(2000, 10)).is_err());
    }

    /// `apply` keeps the higher allocation and drops the rest. At equality it
    /// keeps what it has, so a record carrying the same id from another broker
    /// must not displace the one already recorded -- which only the snapshot
    /// shows, since the id itself is unchanged either way.
    #[test]
    fn an_equal_producer_id_allocation_does_not_replace_the_recorded_one() {
        let mut image = MetadataImage::new(Uuid::nil());
        image.apply(&broker_at(NodeId(1), 11));
        image.apply(&broker_at(NodeId(2), 22));
        let original = crate::records::ProducerIdsRecord {
            broker_id: NodeId(1),
            broker_epoch: 11,
            next_producer_id: 1000,
        };
        image.apply(&MetadataRecord::V1ProducerIds(original));
        image.apply(&MetadataRecord::V1ProducerIds(
            crate::records::ProducerIdsRecord {
                broker_id: NodeId(2),
                broker_epoch: 22,
                next_producer_id: 1000,
            },
        ));

        let recorded: Vec<_> = image
            .to_records()
            .into_iter()
            .filter(|r| matches!(r, MetadataRecord::V1ProducerIds(_)))
            .collect();
        check!(recorded == vec![MetadataRecord::V1ProducerIds(original)]);
    }

    #[test]
    fn record_variant_returns_exact_discriminant_names() {
        let topic = MetadataRecord::V1Topic(TopicRecord {
            name: "orders".into(),
            topic_id: Uuid::new_v4(),
            partitions: 3,
            replication_factor: 2,
        });
        assert2::assert!(record_variant(&topic) == "V1Topic");

        let feature = MetadataRecord::V1FeatureLevel(crate::records::FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 1,
        });
        assert2::assert!(record_variant(&feature) == "V1FeatureLevel");
    }

    use assert2::check;

    use crate::{
        acl::{AclEntryFilter, AclOperation, PermissionType},
        records::{
            BrokerConfigRecord, ClientQuotaRecord, DeleteDelegationTokenRecord,
            DeleteScramCredentialRecord, DeleteTopicRecord, FeatureLevelRecord, LeaderEpoch,
            PartitionOffsetAdvanceRecord, QuotaEntity, ScramCredentialRecord,
        },
    };

    fn img() -> MetadataImage {
        MetadataImage::new(Uuid::nil())
    }

    #[test]
    fn fresh_image_has_no_features_and_unknown_epoch() {
        let m = img();
        assert2::assert!(m.finalized_features().is_empty());
        assert2::assert!(m.finalized_features_epoch() == -1);
    }

    #[test]
    fn finalized_feature_reads_arbitrary_name() {
        let mut m = MetadataImage::new(uuid::Uuid::nil());
        assert2::assert!(m.finalized_feature("transaction.version").is_none());
        m.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "transaction.version".into(),
            level: 2,
        }));
        assert2::assert!(m.finalized_feature("transaction.version") == Some(2));
    }

    #[test]
    fn apply_feature_level_sets_level_and_bumps_epoch() {
        let mut m = img();
        m.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 1,
        }));
        assert2::assert!(m.finalized_features().get("metadata.version") == Some(&1));
        assert2::assert!(m.finalized_features_epoch() == 0);

        // A second apply bumps the epoch again (monotonic).
        m.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 1,
        }));
        assert2::assert!(m.finalized_features_epoch() == 1);
    }

    #[test]
    fn apply_feature_level_zero_deletes_entry() {
        let mut m = img();
        m.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 1,
        }));
        m.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 0,
        }));
        assert2::assert!(
            (
                m.finalized_features().get("metadata.version"),
                m.finalized_features_epoch(),
            ) == (None, 1)
        );
    }

    #[test]
    fn to_records_from_records_round_trips() {
        let cid = Uuid::new_v4();
        let mut image = MetadataImage::new(cid);
        image.apply(&MetadataRecord::V1Topic(TopicRecord {
            name: "orders".into(),
            topic_id: Uuid::new_v4(),
            partitions: 3,
            replication_factor: 2,
        }));
        assert2::assert!(MetadataImage::from_records(cid, &image.to_records()) == image);
    }

    #[test]
    fn offset_advance_applies_as_monotonic_delta() {
        let mut image = img();
        let advance = |count: i64| {
            MetadataRecord::V1PartitionOffsetAdvance(PartitionOffsetAdvanceRecord {
                topic: "t".into(),
                partition: 0,
                count,
            })
        };

        image.apply(&advance(3));
        image.apply(&advance(2));
        image.apply(&MetadataRecord::V1PartitionOffsetAdvance(
            PartitionOffsetAdvanceRecord {
                topic: "t".into(),
                partition: 1,
                count: 7,
            },
        ));

        assert2::assert!(image.partition_next_offset("t", 0) == Some(5));
        assert2::assert!(image.partition_next_offset("t", 1) == Some(7));
    }

    #[test]
    fn offset_advance_survives_snapshot_round_trip() {
        let mut image = img();
        image.apply(&MetadataRecord::V1PartitionOffsetAdvance(
            PartitionOffsetAdvanceRecord {
                topic: "t".into(),
                partition: 0,
                count: 5,
            },
        ));

        let restored = MetadataImage::from_records(Uuid::nil(), &image.to_records());

        assert2::assert!(restored.partition_next_offset("t", 0) == Some(5));
    }

    fn apply_broker_fixture(image: &mut MetadataImage) {
        for record in [
            MetadataRecord::V1BrokerRegistration(BrokerRegistrationRecord {
                node_id: NodeId(1),
                broker_epoch: 0,
                incarnation_id: Uuid::nil(),
                host: "h1".into(),
                port: 9092,
                rack: Some("r1".into()),
                log_dirs: vec![],
                fenced: false,
                in_controlled_shutdown: false,
                cordoned_log_dirs: None,
                endpoints: vec![crate::records::BrokerEndpoint {
                    name: "EXTERNAL".into(),
                    host: "ext".into(),
                    port: 9093,
                    protocol: krabka_security::ListenerProtocol::SaslSsl,
                }],
                features: std::collections::BTreeMap::new(),
            }),
            MetadataRecord::V1BrokerRegistration(BrokerRegistrationRecord {
                node_id: NodeId(2),
                broker_epoch: 0,
                incarnation_id: Uuid::nil(),
                host: "h2".into(),
                port: 9092,
                rack: None,
                log_dirs: vec![],
                fenced: false,
                in_controlled_shutdown: false,
                cordoned_log_dirs: None,
                endpoints: vec![],
                features: std::collections::BTreeMap::new(),
            }),
            MetadataRecord::V1UnregisterBroker(crate::records::UnregisterBrokerRecord {
                node_id: NodeId(2),
                broker_epoch: 0,
            }),
            MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
                node_id: NodeId(1),
                config_name: "leader.replication.throttled.rate".into(),
                config_value: Some("2048".into()),
            }),
            MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
                node_id: NodeId(1),
                config_name: "follower.replication.throttled.rate".into(),
                config_value: Some("4096".into()),
            }),
            MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
                node_id: NodeId(1),
                config_name: "follower.replication.throttled.rate".into(),
                config_value: None,
            }),
        ] {
            image.apply(&record);
        }
    }

    /// Exercises every stored variant that the image can hold, that is, the 9
    /// state-producing records. It also exercises tombstones whose effects
    /// must NOT leak into the snapshot. The round-trip must reproduce the
    /// image exactly.
    #[test]
    // exhaustive fixture over every stored variant
    fn to_records_round_trips_all_variants() {
        use krabka_security::{KafkaPrincipal, SaslMechanism};

        let cid = Uuid::new_v4();
        let mut image = MetadataImage::new(cid);

        // One broker survives and one is unregistered; one config survives
        // and one is deleted back to empty.
        apply_broker_fixture(&mut image);

        // Topics + partitions (one topic deleted → topic, partitions and
        // its config must vanish).
        image.apply(&MetadataRecord::V1Topic(TopicRecord {
            name: "orders".into(),
            topic_id: Uuid::new_v4(),
            partitions: 2,
            replication_factor: 2,
        }));
        image.apply(&MetadataRecord::V1Topic(TopicRecord {
            name: "doomed".into(),
            topic_id: Uuid::new_v4(),
            partitions: 1,
            replication_factor: 1,
        }));
        image.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "orders".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1), NodeId(2)],
            isr: vec![NodeId(1), NodeId(2)],
            leader_epoch: LeaderEpoch(4),
            adding_replicas: vec![NodeId(2)],
            removing_replicas: vec![],
            directories: vec![],
            partition_epoch: 0,
        }));
        image.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "orders".into(),
            partition: 1,
            leader: NodeId(2),
            replicas: vec![NodeId(2), NodeId(1)],
            isr: vec![NodeId(2)],
            leader_epoch: LeaderEpoch(7),
            adding_replicas: vec![],
            removing_replicas: vec![NodeId(1)],
            directories: vec![],
            partition_epoch: 0,
        }));
        image.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "doomed".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1)],
            isr: vec![NodeId(1)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![],
            removing_replicas: vec![],
            directories: vec![],
            partition_epoch: 0,
        }));

        // Topic config.
        let mut overrides = std::collections::BTreeMap::new();
        overrides.insert("retention.ms".to_string(), "60000".to_string());
        overrides.insert("segment.bytes".to_string(), "1048576".to_string());
        image.apply(&MetadataRecord::V1TopicConfig(TopicConfigRecord {
            topic: "orders".into(),
            overrides,
        }));

        // Delete the doomed topic (clears its partition + config too).
        image.apply(&MetadataRecord::V1DeleteTopic(DeleteTopicRecord {
            name: "doomed".into(),
        }));

        // SCRAM creds (one survives, one deleted).
        image.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "alice".into(),
            mechanism: SaslMechanism::ScramSha512,
            salt: vec![1; 16],
            stored_key: vec![2; 64],
            server_key: vec![3; 64],
            iterations: 8192,
        }));
        image.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "bob".into(),
            mechanism: SaslMechanism::ScramSha256,
            salt: vec![4; 16],
            stored_key: vec![5; 32],
            server_key: vec![6; 32],
            iterations: 4096,
        }));
        image.apply(&MetadataRecord::V1DeleteScramCredential(
            DeleteScramCredentialRecord {
                user: "bob".into(),
                mechanism: SaslMechanism::ScramSha256,
            },
        ));

        // ACLs: literal + prefixed survive; a deleted one must not.
        let literal = AclEntry {
            resource_type: ResourceType::Topic,
            resource_name: "orders".into(),
            pattern_type: PatternType::Literal,
            principal: "User:alice".into(),
            host: "*".into(),
            operation: AclOperation::Read,
            permission_type: PermissionType::Allow,
        };
        let prefixed = AclEntry {
            resource_type: ResourceType::Group,
            resource_name: "cg-".into(),
            pattern_type: PatternType::Prefixed,
            principal: "User:alice".into(),
            host: "*".into(),
            operation: AclOperation::Read,
            permission_type: PermissionType::Allow,
        };
        let doomed_acl = AclEntry {
            resource_type: ResourceType::Cluster,
            resource_name: "kafka-cluster".into(),
            pattern_type: PatternType::Literal,
            principal: "User:eve".into(),
            host: "*".into(),
            operation: AclOperation::Alter,
            permission_type: PermissionType::Deny,
        };
        image.apply(&MetadataRecord::V1AccessControlEntry(literal));
        image.apply(&MetadataRecord::V1AccessControlEntry(prefixed));
        image.apply(&MetadataRecord::V1AccessControlEntry(doomed_acl));
        image.apply(&MetadataRecord::V1DeleteAccessControlEntry(
            AclEntryFilter {
                resource_type: Some(ResourceType::Cluster),
                ..AclEntryFilter::default()
            },
        ));

        // Client quotas.
        image.apply(&MetadataRecord::V1ClientQuota(ClientQuotaRecord {
            entity: vec![
                QuotaEntity {
                    entity_type: "user".into(),
                    entity_name: Some("alice".into()),
                },
                QuotaEntity {
                    entity_type: "client-id".into(),
                    entity_name: Some("app1".into()),
                },
            ],
            config_key: "producer_byte_rate".into(),
            config_value: Some(1024.0),
        }));
        image.apply(&MetadataRecord::V1ClientQuota(ClientQuotaRecord {
            entity: vec![QuotaEntity {
                entity_type: "user".into(),
                entity_name: None,
            }],
            config_key: "consumer_byte_rate".into(),
            config_value: Some(2048.0),
        }));

        // Delegation tokens (one survives, one deleted).
        let alice = KafkaPrincipal {
            principal_type: "User".into(),
            name: "alice".into(),
        };
        let bob = KafkaPrincipal {
            principal_type: "User".into(),
            name: "bob".into(),
        };
        image.apply(&MetadataRecord::V1DelegationToken(DelegationTokenRecord {
            token_id: "tok-1".into(),
            owner: alice.clone(),
            requester: alice,
            issue_timestamp_ms: 1_000,
            expiry_timestamp_ms: 5_000,
            max_timestamp_ms: 10_000,
            renewers: vec![bob.clone()],
        }));
        image.apply(&MetadataRecord::V1DelegationToken(DelegationTokenRecord {
            token_id: "tok-2".into(),
            owner: bob.clone(),
            requester: bob,
            issue_timestamp_ms: 1_000,
            expiry_timestamp_ms: 5_000,
            max_timestamp_ms: 10_000,
            renewers: vec![],
        }));
        image.apply(&MetadataRecord::V1DeleteDelegationToken(
            DeleteDelegationTokenRecord {
                token_id: "tok-2".into(),
            },
        ));

        // KIP-584 finalized features: two live features at a non-trivial epoch
        // (the metadata.version re-finalize bumps the epoch past the live
        // count), so the trailing V1FeaturesEpoch carrier is exercised too.
        image.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 24,
        }));
        image.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 25,
        }));
        image.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "group.version".into(),
            level: 1,
        }));
        assert2::assert!(image.finalized_features_epoch() == 2);

        let rebuilt = MetadataImage::from_records(cid, &image.to_records());
        assert2::assert!(rebuilt == image);
    }

    /// Finalized features and their epoch must survive a `to_records` and
    /// `from_records` round-trip exactly. A trailing snapshot-only record
    /// carries the epoch verbatim, and apply does NOT recompute it by a bump.
    /// That epoch is the history of `UpdateFeatures` applies, so an image whose
    /// epoch exceeds its live feature count still reproduces exactly. This test
    /// guards the regression where `to_records` emitted no feature records at
    /// all and the snapshot path silently dropped every finalized feature.
    #[test]
    fn to_records_round_trips_features_and_epoch() {
        let cid = Uuid::new_v4();
        let mut image = MetadataImage::new(cid);
        // Four applies → epoch climbs -1 → 0 → 1 → 2 → 3, while only two
        // features remain live (metadata.version re-finalized once).
        image.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 24,
        }));
        image.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 25,
        }));
        image.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "group.version".into(),
            level: 1,
        }));
        image.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 25,
        }));
        assert2::assert!(image.finalized_features_epoch() == 3);

        let rebuilt = MetadataImage::from_records(cid, &image.to_records());
        assert2::assert!(rebuilt == image);
        // Belt-and-suspenders beyond derived PartialEq: the two fields the
        // snapshot path used to lose.
        check!(
            (
                rebuilt.finalized_features().get("metadata.version"),
                rebuilt.finalized_features().get("group.version"),
                rebuilt.finalized_features_epoch(),
            ) == (Some(&25), Some(&1), 3)
        );
    }

    /// The KIP-853 voter set and the finalized cluster `kraft.version` must
    /// survive `to_records` and `from_records`. If `to_records` drops both, a
    /// snapshot recovery or a learner install rebuilds the image with an EMPTY
    /// voter set and `kraft_version = 0`. That breaks `DescribeQuorum` and
    /// KIP-853 auto-join. The cluster `kraft_version` has no other persistence
    /// source, because it is not part of openraft's membership, so it can only
    /// round-trip through a `V1KRaftVersion` record.
    #[test]
    fn to_records_preserves_voters_and_kraft_version() {
        use crate::{
            records::{KRaftVersionRecord, VotersRecord},
            voters::{KRaftVersionRange, Voter, VoterEndpoint, VoterSet},
        };

        let cid = Uuid::new_v4();
        let mut image = MetadataImage::new(cid);
        image.apply(&MetadataRecord::V1KRaftVersion(KRaftVersionRecord {
            kraft_version: 1,
        }));
        let voters = VoterSet::from_voters([
            Voter {
                id: NodeId(1),
                directory_id: Uuid::from_u128(1),
                endpoints: vec![VoterEndpoint {
                    name: "CONTROLLER".into(),
                    host: "127.0.0.1".into(),
                    port: 9093,
                }],
                kraft_version: KRaftVersionRange::default(),
            },
            Voter {
                id: NodeId(2),
                directory_id: Uuid::from_u128(2),
                endpoints: vec![VoterEndpoint {
                    name: "CONTROLLER".into(),
                    host: "127.0.0.1".into(),
                    port: 9094,
                }],
                kraft_version: KRaftVersionRange { min: 0, max: 1 },
            },
        ]);
        image.apply(&MetadataRecord::V1Voters(VotersRecord {
            voters: voters.clone(),
        }));

        let rebuilt = MetadataImage::from_records(cid, &image.to_records());

        assert2::assert!(rebuilt == image);
    }

    fn topic(name: &str, partitions: i32) -> MetadataRecord {
        MetadataRecord::V1Topic(TopicRecord {
            name: name.into(),
            topic_id: Uuid::new_v4(),
            partitions,
            replication_factor: 1,
        })
    }

    #[test]
    fn apply_topic_inserts() {
        let mut m = img();
        m.apply(&topic("t", 3));
        assert2::assert!(m.topic("t").is_some());
    }

    #[test]
    fn apply_dir_assignment_merges_one_slot_without_clobbering_reassignment() {
        // Regression guard for the KIP-858 dir-assignment clobber: a directory
        // report applied as a delta must set only the reporting replica's slot
        // and leave a concurrent reassignment's `adding_replicas` intact.
        let mut m = img();
        m.apply(&topic("t", 1));
        m.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "t".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1), NodeId(2), NodeId(3)],
            isr: vec![NodeId(1), NodeId(2)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![NodeId(3)],
            removing_replicas: vec![],
            directories: vec![uuid::Uuid::nil(), uuid::Uuid::nil(), uuid::Uuid::nil()],
            partition_epoch: 0,
        }));
        let dir = uuid::Uuid::from_u128(0xABCD);
        m.apply(&MetadataRecord::V1PartitionDirAssignment(
            crate::records::PartitionDirAssignmentRecord {
                topic: "t".into(),
                partition: 0,
                replica: NodeId(2),
                directory: dir,
            },
        ));
        let pr = m.partition("t", 0).unwrap();
        // Slot 1 (replica 2) set; slots 0 and 2 untouched. The delta did NOT
        // clobber the in-flight reassignment.
        assert2::assert!(
            *pr == PartitionRecord {
                topic: "t".to_string(),
                partition: 0,
                leader: NodeId(1),
                replicas: vec![NodeId(1), NodeId(2), NodeId(3)],
                isr: vec![NodeId(1), NodeId(2)],
                leader_epoch: krabka_ids::LeaderEpoch(0),
                adding_replicas: vec![NodeId(3)],
                removing_replicas: vec![],
                directories: vec![uuid::Uuid::nil(), dir, uuid::Uuid::nil()],
                partition_epoch: 0,
            }
        );
    }

    #[test]
    fn apply_delete_clears_partitions() {
        let mut m = img();
        m.apply(&topic("t", 2));
        m.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "t".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1)],
            isr: vec![NodeId(1)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![],
            removing_replicas: vec![],
            directories: vec![],
            partition_epoch: 0,
        }));
        m.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "t".into(),
            partition: 1,
            leader: NodeId(1),
            replicas: vec![NodeId(1)],
            isr: vec![NodeId(1)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![],
            removing_replicas: vec![],
            directories: vec![],
            partition_epoch: 0,
        }));
        assert2::assert!(m.partitions_of("t").count() == 2);
        m.apply(&MetadataRecord::V1DeleteTopic(DeleteTopicRecord {
            name: "t".into(),
        }));
        assert2::assert!(m.topic("t").is_none());
        assert2::assert!(m.partitions_of("t").count() == 0);
    }

    #[test]
    fn validate_topic_exists_rejected() {
        let mut m = img();
        m.apply(&topic("t", 1));
        let err = m.validate(&topic("t", 1)).unwrap_err();
        assert2::assert!(matches!(err, MetadataError::TopicExists(_)));
    }

    /// Apply `count` partition records, with indices `0..count`, for `topic`,
    /// so the derived partition count of the image matches a realistic topic.
    /// The `validate` partition-count check reads this derived count and not
    /// the stored `TopicRecord.partitions`.
    fn apply_partitions(m: &mut MetadataImage, topic: &str, count: i32) {
        for p in 0..count {
            m.apply(&MetadataRecord::V1Partition(PartitionRecord {
                topic: topic.into(),
                partition: p,
                leader: NodeId(1),
                replicas: vec![NodeId(1)],
                isr: vec![NodeId(1)],
                leader_epoch: LeaderEpoch(0),
                adding_replicas: vec![],
                removing_replicas: vec![],
                directories: vec![],
                partition_epoch: 0,
            }));
        }
    }

    #[test]
    fn validate_topic_partition_count_increase_allowed() {
        let mut m = img();
        m.apply(&topic("t", 1));
        apply_partitions(&mut m, "t", 1);
        let existing = m.topic("t").unwrap().clone();
        let updated = MetadataRecord::V1Topic(TopicRecord {
            name: "t".into(),
            topic_id: existing.topic_id,
            partitions: 3,
            replication_factor: existing.replication_factor,
        });
        assert2::assert!(m.validate(&updated).is_ok());
    }

    #[test]
    fn validate_topic_partition_count_decrease_rejected() {
        let mut m = img();
        m.apply(&topic("t", 3));
        apply_partitions(&mut m, "t", 3);
        let existing = m.topic("t").unwrap().clone();
        let updated = MetadataRecord::V1Topic(TopicRecord {
            name: "t".into(),
            topic_id: existing.topic_id,
            partitions: 1,
            replication_factor: existing.replication_factor,
        });
        let err = m.validate(&updated).unwrap_err();
        assert2::assert!(matches!(err, MetadataError::TopicExists(_)));
    }

    #[test]
    fn validate_delete_unknown_topic_rejected() {
        let m = img();
        let err = m
            .validate(&MetadataRecord::V1DeleteTopic(DeleteTopicRecord {
                name: "ghost".into(),
            }))
            .unwrap_err();
        assert2::assert!(matches!(err, MetadataError::UnknownTopic(_)));
    }

    #[test]
    fn validate_partition_for_unknown_topic_rejected() {
        let m = img();
        let p = MetadataRecord::V1Partition(PartitionRecord {
            topic: "ghost".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1)],
            isr: vec![NodeId(1)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![],
            removing_replicas: vec![],
            directories: vec![],
            partition_epoch: 0,
        });
        let err = m.validate(&p).unwrap_err();
        assert2::assert!(matches!(err, MetadataError::UnknownTopic(_)));
    }

    #[test]
    fn broker_registration_is_idempotent() {
        let mut m = img();
        let b = MetadataRecord::V1BrokerRegistration(BrokerRegistrationRecord {
            node_id: NodeId(1),
            broker_epoch: 0,
            incarnation_id: Uuid::nil(),
            host: "h".into(),
            port: 9092,
            rack: None,
            log_dirs: vec![],
            fenced: false,
            in_controlled_shutdown: false,
            cordoned_log_dirs: None,
            endpoints: vec![],
            features: std::collections::BTreeMap::new(),
        });
        m.apply(&b);
        m.apply(&b);
        assert2::assert!(m.brokers().count() == 1);
    }

    #[test]
    fn apply_topic_config_inserts() {
        let mut m = img();
        m.apply(&topic("t", 1));
        let mut overrides = std::collections::BTreeMap::new();
        overrides.insert("retention.ms".to_string(), "60000".to_string());
        m.apply(&MetadataRecord::V1TopicConfig(
            crate::records::TopicConfigRecord {
                topic: "t".into(),
                overrides: overrides.clone(),
            },
        ));
        assert2::assert!(m.topic_config("t") == Some(&overrides));
    }

    #[test]
    fn apply_topic_config_replaces_previous() {
        let mut m = img();
        m.apply(&topic("t", 1));

        let mut first = std::collections::BTreeMap::new();
        first.insert("retention.ms".to_string(), "60000".to_string());
        first.insert("segment.bytes".to_string(), "1024".to_string());
        m.apply(&MetadataRecord::V1TopicConfig(
            crate::records::TopicConfigRecord {
                topic: "t".into(),
                overrides: first,
            },
        ));

        let mut second = std::collections::BTreeMap::new();
        second.insert("retention.ms".to_string(), "120000".to_string());
        m.apply(&MetadataRecord::V1TopicConfig(
            crate::records::TopicConfigRecord {
                topic: "t".into(),
                overrides: second.clone(),
            },
        ));

        // segment.bytes is GONE — last-write-wins is authoritative.
        assert2::assert!(m.topic_config("t") == Some(&second));
    }

    #[test]
    fn delete_topic_clears_configs() {
        let mut m = img();
        m.apply(&topic("t", 1));
        let mut overrides = std::collections::BTreeMap::new();
        overrides.insert("retention.ms".to_string(), "60000".to_string());
        m.apply(&MetadataRecord::V1TopicConfig(
            crate::records::TopicConfigRecord {
                topic: "t".into(),
                overrides,
            },
        ));
        m.apply(&MetadataRecord::V1DeleteTopic(
            crate::records::DeleteTopicRecord { name: "t".into() },
        ));
        assert2::assert!(m.topic_config("t").is_none());
    }

    #[test]
    fn validate_topic_config_for_unknown_topic_rejected() {
        let m = img();
        let r = MetadataRecord::V1TopicConfig(crate::records::TopicConfigRecord {
            topic: "ghost".into(),
            overrides: std::collections::BTreeMap::new(),
        });
        let err = m.validate(&r).unwrap_err();
        assert2::assert!(matches!(err, MetadataError::UnknownTopic(_)));
    }

    #[test]
    fn apply_scram_credential_stores() {
        let mut m = img();
        m.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "alice".into(),
            mechanism: krabka_security::SaslMechanism::ScramSha512,
            salt: vec![1; 16],
            stored_key: vec![2; 64],
            server_key: vec![3; 64],
            iterations: 4096,
        }));
        let got = m.scram_credential("alice", krabka_security::SaslMechanism::ScramSha512);
        assert2::assert!(got.map(|credential| credential.iterations) == Some(4096));
    }

    #[test]
    fn apply_scram_credential_last_write_wins() {
        let mut m = img();
        let mech = krabka_security::SaslMechanism::ScramSha512;
        m.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "alice".into(),
            mechanism: mech,
            salt: vec![1; 16],
            stored_key: vec![2; 64],
            server_key: vec![3; 64],
            iterations: 4096,
        }));
        m.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "alice".into(),
            mechanism: mech,
            salt: vec![9; 16],
            stored_key: vec![9; 64],
            server_key: vec![9; 64],
            iterations: 8192,
        }));
        let got = m.scram_credential("alice", mech).unwrap();
        assert2::assert!((got.iterations, &got.salt) == (8192, &vec![9; 16]));
    }

    #[test]
    fn delete_scram_credential_removes() {
        let mut m = img();
        let mech = krabka_security::SaslMechanism::ScramSha512;
        m.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "alice".into(),
            mechanism: mech,
            salt: vec![1; 16],
            stored_key: vec![2; 64],
            server_key: vec![3; 64],
            iterations: 4096,
        }));
        m.apply(&MetadataRecord::V1DeleteScramCredential(
            DeleteScramCredentialRecord {
                user: "alice".into(),
                mechanism: mech,
            },
        ));
        assert2::assert!(m.scram_credential("alice", mech).is_none());
    }

    fn topic_read_for_alice() -> AclEntry {
        AclEntry {
            resource_type: ResourceType::Topic,
            resource_name: "foo".into(),
            pattern_type: PatternType::Literal,
            principal: "User:alice".into(),
            host: "*".into(),
            operation: AclOperation::Read,
            permission_type: PermissionType::Allow,
        }
    }

    fn topic_prefixed_team() -> AclEntry {
        AclEntry {
            resource_type: ResourceType::Topic,
            resource_name: "team-".into(),
            pattern_type: PatternType::Prefixed,
            principal: "User:alice".into(),
            host: "*".into(),
            operation: AclOperation::Read,
            permission_type: PermissionType::Allow,
        }
    }

    #[test]
    fn apply_v1_access_control_entry_literal_stores_in_literal_map() {
        let mut m = img();
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_read_for_alice()));
        let hits: Vec<_> = m.matching_acls(ResourceType::Topic, "foo").collect();
        assert2::assert!(
            hits.iter()
                .map(|entry| entry.resource_name.as_str())
                .collect::<Vec<_>>()
                == vec!["foo"]
        );
    }

    #[test]
    fn apply_v1_access_control_entry_replaces_identical_entry() {
        let mut m = img();
        let acl = topic_read_for_alice();
        m.apply(&MetadataRecord::V1AccessControlEntry(acl.clone()));
        m.apply(&MetadataRecord::V1AccessControlEntry(acl));

        let hits: Vec<_> = m.matching_acls(ResourceType::Topic, "foo").collect();
        assert2::assert!(hits.len() == 1);
    }

    #[test]
    fn apply_v1_access_control_entry_prefixed_stores_in_prefixed_vec() {
        let mut m = img();
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_prefixed_team()));
        let hits: Vec<_> = m.matching_acls(ResourceType::Topic, "team-foo").collect();
        assert2::assert!(
            hits.iter()
                .map(|entry| entry.resource_name.as_str())
                .collect::<Vec<_>>()
                == vec!["team-"]
        );
        // Non-matching resource: empty.
        let none: Vec<_> = m.matching_acls(ResourceType::Topic, "other").collect();
        assert2::assert!(none.is_empty());
    }

    #[test]
    fn matching_acls_combines_literal_and_prefixed() {
        let mut m = img();
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_read_for_alice()));
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_prefixed_team()));
        let hits_foo: Vec<_> = m.matching_acls(ResourceType::Topic, "foo").collect();
        let hits_team: Vec<_> = m.matching_acls(ResourceType::Topic, "team-x").collect();
        assert2::assert!((hits_foo.len(), hits_team.len()) == (1, 1));
    }

    fn topic_wildcard_allow() -> AclEntry {
        AclEntry {
            resource_type: ResourceType::Topic,
            resource_name: "*".into(),
            pattern_type: PatternType::Literal,
            principal: "User:alice".into(),
            host: "*".into(),
            operation: AclOperation::Read,
            permission_type: PermissionType::Allow,
        }
    }

    fn topic_wildcard_deny() -> AclEntry {
        AclEntry {
            resource_type: ResourceType::Topic,
            resource_name: "*".into(),
            pattern_type: PatternType::Literal,
            principal: "User:alice".into(),
            host: "*".into(),
            operation: AclOperation::Read,
            permission_type: PermissionType::Deny,
        }
    }

    #[test]
    fn matching_acls_literal_wildcard_allow_matches_arbitrary_topic() {
        let mut m = img();
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_wildcard_allow()));
        // An arbitrarily-named topic the grant never mentions by name.
        let hits: Vec<_> = m
            .matching_acls(ResourceType::Topic, "some-random-topic")
            .collect();
        assert2::assert!(hits == vec![&topic_wildcard_allow()]);
        // Requesting "*" itself must not double-count the wildcard entry.
        let star: Vec<_> = m.matching_acls(ResourceType::Topic, "*").collect();
        assert2::assert!(star.len() == 1);
    }

    #[test]
    fn matching_acls_literal_wildcard_deny_matches_arbitrary_topic() {
        let mut m = img();
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_wildcard_deny()));
        let hits: Vec<_> = m
            .matching_acls(ResourceType::Topic, "another-random-topic")
            .collect();
        assert2::assert!(hits == vec![&topic_wildcard_deny()]);
    }

    #[test]
    fn apply_v1_delete_access_control_entry_removes_matching() {
        let mut m = img();
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_read_for_alice()));
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_prefixed_team()));
        let filter = AclEntryFilter {
            resource_type: Some(ResourceType::Topic),
            pattern_type: Some(PatternType::Literal),
            ..AclEntryFilter::default()
        };
        m.apply(&MetadataRecord::V1DeleteAccessControlEntry(filter));
        let hits_foo: Vec<_> = m.matching_acls(ResourceType::Topic, "foo").collect();
        let hits_team: Vec<_> = m.matching_acls(ResourceType::Topic, "team-x").collect();
        assert2::assert!((hits_foo.len(), hits_team.len()) == (0, 1));
    }

    #[test]
    fn apply_v1_delete_access_control_entry_no_match_is_noop() {
        let mut m = img();
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_read_for_alice()));
        let filter = AclEntryFilter {
            resource_type: Some(ResourceType::Group),
            ..AclEntryFilter::default()
        };
        m.apply(&MetadataRecord::V1DeleteAccessControlEntry(filter));
        let hits: Vec<_> = m.matching_acls(ResourceType::Topic, "foo").collect();
        assert2::assert!(hits.len() == 1);
    }

    #[test]
    fn all_acls_returns_every_entry() {
        let mut m = img();
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_read_for_alice()));
        m.apply(&MetadataRecord::V1AccessControlEntry(topic_prefixed_team()));
        assert2::assert!(m.all_acls().count() == 2);
    }

    #[test]
    fn all_partitions_count_matches_map_size() {
        let mut m = img();
        m.apply(&topic("t", 3));
        for p in 0..3 {
            m.apply(&MetadataRecord::V1Partition(PartitionRecord {
                topic: "t".into(),
                partition: p,
                leader: NodeId(1),
                replicas: vec![NodeId(1)],
                isr: vec![NodeId(1)],
                leader_epoch: LeaderEpoch(0),
                adding_replicas: vec![],
                removing_replicas: vec![],
                directories: vec![],
                partition_epoch: 0,
            }));
        }
        m.apply(&topic("u", 1));
        m.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "u".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1)],
            isr: vec![NodeId(1)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![],
            removing_replicas: vec![],
            directories: vec![],
            partition_epoch: 0,
        }));
        // 3 partitions for "t" + 1 for "u" = 4, one walk, no per-topic filter.
        assert2::assert!(m.all_partitions().count() == 4);
        assert2::assert!(
            m.all_partitions().count()
                == m.partitions_of("t").count() + m.partitions_of("u").count()
        );
    }

    /// `total_partitions` sums the by-topic index across every topic. It backs
    /// the `to_records` snapshot tracing field, whose span is inert under the
    /// test subscriber. This test therefore pins its value directly. Without
    /// that pin, a constant-return mutant survives and no behavioral test
    /// catches it.
    #[test]
    fn total_partitions_sums_across_topics() {
        let mut m = img();
        // Empty image: no topics, no partitions.
        check!(m.total_partitions() == 0);

        // "a" gets 2 partitions, "b" gets 1 → 3 total across the index.
        m.apply(&topic("a", 2));
        m.apply(&topic("b", 1));
        for (name, idx) in [("a", 0), ("a", 1), ("b", 0)] {
            m.apply(&MetadataRecord::V1Partition(PartitionRecord {
                topic: name.into(),
                partition: idx,
                leader: NodeId(1),
                replicas: vec![NodeId(1)],
                isr: vec![NodeId(1)],
                leader_epoch: LeaderEpoch(0),
                adding_replicas: vec![],
                removing_replicas: vec![],
                directories: vec![],
                partition_epoch: 0,
            }));
        }
        check!(m.total_partitions() == 3);
        // Matches an independent full walk, and is not the mutant constant 1.
        check!(m.total_partitions() == m.all_partitions().count());
    }

    /// Pins the `partitions_of` ordering contract: ascending partition-index
    /// order regardless of apply order. The `Metadata` and
    /// `DescribeTopicPartitions` rows and the cursor-based pagination rely on
    /// it.
    #[test]
    fn partitions_of_yields_ascending_partition_index_order() {
        let mut m = img();
        m.apply(&topic("t", 3));
        for p in [2, 0, 1] {
            m.apply(&MetadataRecord::V1Partition(PartitionRecord {
                topic: "t".into(),
                partition: p,
                leader: NodeId(1),
                replicas: vec![NodeId(1)],
                isr: vec![NodeId(1)],
                leader_epoch: LeaderEpoch(0),
                adding_replicas: vec![],
                removing_replicas: vec![],
                directories: vec![],
                partition_epoch: 0,
            }));
        }
        let idxs: Vec<i32> = m.partitions_of("t").map(|p| p.partition).collect();
        assert2::assert!(idxs == vec![0, 1, 2]);
    }

    /// A delete of one topic must not disturb the partitions of another topic
    /// in the by-topic index. `partitions_of` for the deleted topic goes
    /// empty, and its sibling stays intact.
    #[test]
    fn delete_topic_leaves_other_topics_partitions_intact() {
        let mut m = img();
        m.apply(&topic("doomed", 1));
        m.apply(&topic("survivor", 1));
        for name in ["doomed", "survivor"] {
            m.apply(&MetadataRecord::V1Partition(PartitionRecord {
                topic: name.into(),
                partition: 0,
                leader: NodeId(1),
                replicas: vec![NodeId(1)],
                isr: vec![NodeId(1)],
                leader_epoch: LeaderEpoch(0),
                adding_replicas: vec![],
                removing_replicas: vec![],
                directories: vec![],
                partition_epoch: 0,
            }));
        }
        m.apply(&MetadataRecord::V1DeleteTopic(DeleteTopicRecord {
            name: "doomed".into(),
        }));
        check!(
            (
                m.partitions_of("doomed").count(),
                m.topic_partition_count("doomed"),
                m.partitions_of("survivor").count(),
                m.partition("survivor", 0).is_some(),
                m.all_partitions().count(),
            ) == (0, 0, 1, true, 1)
        );
    }

    #[test]
    fn reassignments_in_flight_excludes_idle_partitions() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1Topic(TopicRecord {
            name: "foo".into(),
            topic_id: uuid::Uuid::nil(),
            partitions: 1,
            replication_factor: 3,
        }));
        img.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "foo".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1), NodeId(2), NodeId(3)],
            isr: vec![NodeId(1), NodeId(2), NodeId(3)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![],
            removing_replicas: vec![],
            directories: vec![],
            partition_epoch: 0,
        }));
        assert2::assert!(img.reassignments_in_flight().count() == 0);
    }

    #[test]
    fn reassignments_in_flight_returns_partitions_with_adding() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1Topic(TopicRecord {
            name: "foo".into(),
            topic_id: uuid::Uuid::nil(),
            partitions: 1,
            replication_factor: 3,
        }));
        img.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "foo".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1), NodeId(2), NodeId(3), NodeId(4)],
            isr: vec![NodeId(1), NodeId(2), NodeId(3)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![NodeId(4)],
            removing_replicas: vec![],
            directories: vec![],
            partition_epoch: 0,
        }));
        let rows: Vec<_> = img.reassignments_in_flight().collect();
        assert2::assert!(
            rows.iter()
                .map(|row| row.adding_replicas.clone())
                .collect::<Vec<_>>()
                == vec![vec![NodeId(4)]]
        );
    }

    #[test]
    fn reassignments_in_flight_returns_partitions_with_removing() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1Topic(TopicRecord {
            name: "foo".into(),
            topic_id: uuid::Uuid::nil(),
            partitions: 1,
            replication_factor: 3,
        }));
        img.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "foo".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1), NodeId(2), NodeId(3)],
            isr: vec![NodeId(1), NodeId(2), NodeId(3)],
            leader_epoch: LeaderEpoch(0),
            adding_replicas: vec![],
            removing_replicas: vec![NodeId(3)],
            directories: vec![],
            partition_epoch: 0,
        }));
        let rows: Vec<_> = img.reassignments_in_flight().collect();
        assert2::assert!(
            rows.iter()
                .map(|row| row.removing_replicas.clone())
                .collect::<Vec<_>>()
                == vec![vec![NodeId(3)]]
        );
    }

    #[test]
    fn reassignments_in_flight_covers_multiple_topics() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        for name in ["foo", "bar"] {
            img.apply(&MetadataRecord::V1Topic(TopicRecord {
                name: name.into(),
                topic_id: uuid::Uuid::nil(),
                partitions: 1,
                replication_factor: 3,
            }));
            img.apply(&MetadataRecord::V1Partition(PartitionRecord {
                topic: name.into(),
                partition: 0,
                leader: NodeId(1),
                replicas: vec![NodeId(1), NodeId(2), NodeId(3), NodeId(4)],
                isr: vec![NodeId(1), NodeId(2), NodeId(3)],
                leader_epoch: LeaderEpoch(0),
                adding_replicas: vec![NodeId(4)],
                removing_replicas: vec![],
                directories: vec![],
                partition_epoch: 0,
            }));
        }
        assert2::assert!(img.reassignments_in_flight().count() == 2);
    }

    #[test]
    fn broker_config_set_inserts_into_image() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
            node_id: NodeId(1),
            config_name: "leader.replication.throttled.rate".into(),
            config_value: Some("2048".into()),
        }));
        let bc = img.broker_config(NodeId(1)).expect("broker config");
        assert2::assert!(bc.get("leader.replication.throttled.rate") == Some(&"2048".to_string()));
    }

    #[test]
    fn broker_config_delete_removes_from_image() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
            node_id: NodeId(1),
            config_name: "leader.replication.throttled.rate".into(),
            config_value: Some("2048".into()),
        }));
        img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
            node_id: NodeId(1),
            config_name: "leader.replication.throttled.rate".into(),
            config_value: None,
        }));
        let bc = img
            .broker_config(NodeId(1))
            .expect("broker_configs entry retained");
        assert2::assert!(bc.get("leader.replication.throttled.rate").is_none());
    }

    #[test]
    fn broker_throttle_rate_parses_positive_value() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
            node_id: NodeId(1),
            config_name: "leader.replication.throttled.rate".into(),
            config_value: Some("2048".into()),
        }));
        assert2::assert!(
            img.broker_throttle_rate(NodeId(1), ThrottleKind::Leader) == Some(bytes_per_sec(2048))
        );

        img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
            node_id: NodeId(1),
            config_name: "replica.alter.log.dirs.io.max.bytes.per.second".into(),
            config_value: Some("4096".into()),
        }));
        assert2::assert!(
            img.broker_throttle_rate(NodeId(1), ThrottleKind::AlterLogDirs)
                == Some(bytes_per_sec(4096))
        );
    }

    /// The config value is a `String` holding Kafka's `int64` quota, and the
    /// quantity stores `f64`. Every magnitude below 2^53 round-trips exactly,
    /// which covers the whole range the config key can express in practice.
    /// This test pins that, so the accessor is not quietly lossy at the top
    /// end.
    #[test]
    fn broker_throttle_rate_round_trips_large_quotas_exactly() {
        for raw in ["0", "1", "1073741824", "9007199254740992"] {
            let mut img = MetadataImage::new(uuid::Uuid::nil());
            img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
                node_id: NodeId(7),
                config_name: "follower.replication.throttled.rate".into(),
                config_value: Some(raw.into()),
            }));
            let rate = img
                .broker_throttle_rate(NodeId(7), ThrottleKind::Follower)
                .expect("configured rate");
            assert2::check!(rate.bytes_per_sec_i64().to_string() == raw);
        }
    }

    #[test]
    fn broker_throttle_rate_returns_none_for_negative_one() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
            node_id: NodeId(1),
            config_name: "leader.replication.throttled.rate".into(),
            config_value: Some("-1".into()),
        }));
        assert2::assert!(
            img.broker_throttle_rate(NodeId(1), ThrottleKind::Leader)
                .is_none()
        );
    }

    #[test]
    fn broker_throttle_rate_uses_cluster_default() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
            node_id: DEFAULT_BROKER_CONFIG_NODE_ID,
            config_name: "leader.replication.throttled.rate".into(),
            config_value: Some("2048".into()),
        }));

        assert2::assert!(
            img.default_broker_config()
                .and_then(|configs| configs.get("leader.replication.throttled.rate"))
                .map(String::as_str)
                == Some("2048")
        );
        assert2::assert!(
            img.broker_throttle_rate(NodeId(7), ThrottleKind::Leader) == Some(bytes_per_sec(2048))
        );
    }

    #[test]
    fn per_broker_disabled_rate_overrides_cluster_default() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        for (node_id, value) in [(DEFAULT_BROKER_CONFIG_NODE_ID, "2048"), (NodeId(7), "-1")] {
            img.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
                node_id,
                config_name: "leader.replication.throttled.rate".into(),
                config_value: Some(value.into()),
            }));
        }

        assert2::assert!(
            img.broker_throttle_rate(NodeId(7), ThrottleKind::Leader)
                .is_none()
        );
    }

    #[test]
    fn client_quota_apply_inserts_canonicalized() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        // Input order: (user, client-id) — should canonicalize to (client-id, user).
        img.apply(&MetadataRecord::V1ClientQuota(ClientQuotaRecord {
            entity: vec![
                QuotaEntity {
                    entity_type: "user".into(),
                    entity_name: Some("alice".into()),
                },
                QuotaEntity {
                    entity_type: "client-id".into(),
                    entity_name: Some("app1".into()),
                },
            ],
            config_key: "producer_byte_rate".into(),
            config_value: Some(1024.0),
        }));
        let key: EntityKey = vec![
            ("client-id".into(), Some("app1".into())),
            ("user".into(), Some("alice".into())),
        ];
        let configs = img
            .client_quotas()
            .get(&key)
            .expect("entry under canonical key");
        assert2::assert!(configs.get("producer_byte_rate") == Some(&1024.0));
    }

    #[test]
    fn client_quota_apply_delete_removes_key() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1ClientQuota(ClientQuotaRecord {
            entity: vec![QuotaEntity {
                entity_type: "user".into(),
                entity_name: Some("alice".into()),
            }],
            config_key: "producer_byte_rate".into(),
            config_value: Some(1024.0),
        }));
        img.apply(&MetadataRecord::V1ClientQuota(ClientQuotaRecord {
            entity: vec![QuotaEntity {
                entity_type: "user".into(),
                entity_name: Some("alice".into()),
            }],
            config_key: "producer_byte_rate".into(),
            config_value: None,
        }));
        let key: EntityKey = vec![("user".into(), Some("alice".into()))];
        let configs = img.client_quotas().get(&key).expect("entry retained");
        assert2::assert!(configs.get("producer_byte_rate").is_none());
    }

    #[test]
    fn client_quota_default_entity_uses_none_name() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1ClientQuota(ClientQuotaRecord {
            entity: vec![QuotaEntity {
                entity_type: "user".into(),
                entity_name: None,
            }],
            config_key: "producer_byte_rate".into(),
            config_value: Some(512.0),
        }));
        let key: EntityKey = vec![("user".into(), None)];
        assert2::assert!(img.client_quotas().contains_key(&key));
    }

    #[test]
    fn canonicalize_sorts_alphabetically_by_entity_type() {
        let input = vec![
            ("user".to_string(), Some("alice".to_string())),
            ("client-id".to_string(), Some("app1".to_string())),
        ];
        let canon = canonicalize_entity(input);
        assert2::assert!(
            canon
                .iter()
                .map(|(kind, _)| kind.as_str())
                .collect::<Vec<_>>()
                == vec!["client-id", "user"]
        );
    }

    #[test]
    fn scram_credentials_users_returns_distinct_users() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "alice".into(),
            mechanism: SaslMechanism::ScramSha512,
            salt: vec![1, 2, 3],
            stored_key: vec![4, 5, 6],
            server_key: vec![7, 8, 9],
            iterations: 4096,
        }));
        img.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "bob".into(),
            mechanism: SaslMechanism::ScramSha512,
            salt: vec![1, 2, 3],
            stored_key: vec![4, 5, 6],
            server_key: vec![7, 8, 9],
            iterations: 4096,
        }));
        let mut users = img.scram_credentials_users();
        users.sort();
        assert2::assert!(users == vec!["alice".to_string(), "bob".to_string()]);
    }

    fn principal(pt: &str, name: &str) -> KafkaPrincipal {
        KafkaPrincipal {
            principal_type: pt.into(),
            name: name.into(),
        }
    }

    fn dt_record(
        token_id: &str,
        owner: KafkaPrincipal,
        expiry_timestamp_ms: i64,
        renewers: Vec<KafkaPrincipal>,
    ) -> MetadataRecord {
        MetadataRecord::V1DelegationToken(DelegationTokenRecord {
            token_id: token_id.into(),
            owner: owner.clone(),
            requester: owner,
            issue_timestamp_ms: 1_000,
            expiry_timestamp_ms,
            max_timestamp_ms: 10_000,
            renewers,
        })
    }

    #[test]
    fn apply_delegation_token_insert_and_replace() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        let alice = principal("User", "alice");

        img.apply(&dt_record("tok-1", alice.clone(), 5_000, vec![]));
        let got = img.delegation_token_by_id("tok-1").expect("token present");
        assert2::assert!(got.expiry_timestamp_ms == 5_000);
        assert2::assert!(&got.owner == &alice);

        // Same token_id, different expiry — replace, not duplicate.
        img.apply(&dt_record("tok-1", alice.clone(), 7_500, vec![]));
        let got = img.delegation_token_by_id("tok-1").expect("token present");
        assert2::assert!(got.expiry_timestamp_ms == 7_500);
        assert2::assert!(img.all_delegation_tokens().count() == 1);
    }

    #[test]
    fn apply_delete_delegation_token_removes_from_image() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        let alice = principal("User", "alice");

        img.apply(&dt_record("tok-1", alice, 5_000, vec![]));
        assert2::assert!(img.delegation_token_by_id("tok-1").is_some());

        img.apply(&MetadataRecord::V1DeleteDelegationToken(
            DeleteDelegationTokenRecord {
                token_id: "tok-1".into(),
            },
        ));
        assert2::assert!(img.delegation_token_by_id("tok-1").is_none());
        assert2::assert!(img.all_delegation_tokens().count() == 0);
    }

    #[test]
    fn delegation_token_by_hmac_recomputes_each_tokens_hmac() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&dt_record(
            "tok-a",
            principal("User", "alice"),
            5_000,
            vec![],
        ));
        img.apply(&dt_record("tok-b", principal("User", "bob"), 5_000, vec![]));
        let hmac_b = krabka_security::compute_token_hmac(b"key", "tok-b");

        // (secret key, presented hmac, token found)
        let cases: [(&[u8], &[u8], Option<&str>); 3] = [
            (b"key", &hmac_b, Some("tok-b")),
            // The same HMAC under another key names no token.
            (b"other", &hmac_b, None),
            (b"key", &[0xCC; 64], None),
        ];
        for (secret_key, hmac, expected) in cases {
            check!(
                img.delegation_token_by_hmac(secret_key, hmac)
                    .map(|t| t.token_id.as_str())
                    == expected
            );
        }
    }

    #[test]
    fn delegation_tokens_by_owner_filters_correctly() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        let alice = principal("User", "alice");
        let bob = principal("User", "bob");

        img.apply(&dt_record("a-1", alice.clone(), 5_000, vec![]));
        img.apply(&dt_record("a-2", alice.clone(), 6_000, vec![bob.clone()]));
        img.apply(&dt_record("b-1", bob.clone(), 7_000, vec![]));

        let alice_tokens = img.delegation_tokens_by_owner(&alice);
        assert2::assert!(alice_tokens.len() == 2);
        assert2::assert!(alice_tokens.iter().all(|token| token.owner == alice));

        let bob_tokens = img.delegation_tokens_by_owner(&bob);
        assert2::assert!(
            bob_tokens
                .iter()
                .map(|token| token.token_id.as_str())
                .collect::<Vec<_>>()
                == vec!["b-1"]
        );

        // visible_to: bob owns b-1 and is renewer on a-2 → 2 tokens.
        let bob_visible = img.delegation_tokens_visible_to(&bob);
        assert2::assert!(bob_visible.len() == 2);
        let mut ids: Vec<&str> = bob_visible.iter().map(|t| t.token_id.as_str()).collect();
        ids.sort_unstable();
        assert2::assert!(ids == vec!["a-2", "b-1"]);
    }

    /// Kafka's `TokenInformation.ownerOrRenewer` counts the requester too, so
    /// an administrator sees a token it created on another owner's behalf.
    #[test]
    fn delegation_tokens_are_visible_to_their_requester() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        let alice = principal("User", "alice");
        let admin = principal("User", "admin");
        img.apply(&MetadataRecord::V1DelegationToken(DelegationTokenRecord {
            requester: admin.clone(),
            ..match dt_record("a-1", alice.clone(), 5_000, vec![]) {
                MetadataRecord::V1DelegationToken(record) => record,
                _ => unreachable!("dt_record builds a token record"),
            }
        }));
        img.apply(&dt_record("a-2", alice.clone(), 5_000, vec![]));

        // (principal, visible token ids)
        let cases = [
            (&admin, vec!["a-1"]),
            (&alice, vec!["a-1", "a-2"]),
            (&principal("User", "carol"), vec![]),
        ];
        for (who, expected) in cases {
            let mut ids: Vec<&str> = img
                .delegation_tokens_visible_to(who)
                .iter()
                .map(|t| t.token_id.as_str())
                .collect();
            ids.sort_unstable();
            check!(ids == expected, "{who}");
        }
    }

    #[test]
    fn applies_voters_and_version() {
        let mut image = MetadataImage::default();
        image.apply(&MetadataRecord::V1KRaftVersion(
            crate::records::KRaftVersionRecord { kraft_version: 1 },
        ));
        image.apply(&MetadataRecord::V1Voters(crate::records::VotersRecord {
            voters: crate::voters::VoterSet::from_voters([crate::voters::Voter {
                id: NodeId(1),
                directory_id: uuid::Uuid::nil(),
                endpoints: vec![],
                kraft_version: crate::voters::KRaftVersionRange::default(),
            }]),
        }));
        assert2::assert!(image.kraft_version() == 1);
        assert2::assert!(image.voters().contains(NodeId(1)));
    }

    #[test]
    fn scram_credentials_for_user_returns_pairs() {
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        img.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "alice".into(),
            mechanism: SaslMechanism::ScramSha512,
            salt: vec![1, 2, 3],
            stored_key: vec![4, 5, 6],
            server_key: vec![7, 8, 9],
            iterations: 8192,
        }));
        let pairs = img.scram_credentials_for_user("alice");
        assert2::assert!(pairs == vec![(SaslMechanism::ScramSha512, 8192)]);
        assert2::assert!(img.scram_credentials_for_user("ghost").is_empty());
    }

    #[test]
    fn finalized_metadata_version_reads_feature_map() {
        use crate::records::FeatureLevelRecord;
        let mut m = img();
        assert2::assert!(m.finalized_metadata_version() == None);
        m.apply(&MetadataRecord::V1FeatureLevel(FeatureLevelRecord {
            name: "metadata.version".into(),
            level: 19,
        }));
        assert2::assert!(m.finalized_metadata_version() == Some(19));
    }

    #[test]
    fn min_required_metadata_version_baseline_is_min() {
        use crate::metadata_version::METADATA_VERSION_MIN;
        let m = img();
        assert2::assert!(m.min_required_metadata_version() == METADATA_VERSION_MIN);
    }

    #[test]
    fn client_metrics_config_apply_and_clear() {
        use crate::records::ClientMetricsConfigRecord;
        let mut img = MetadataImage::new(uuid::Uuid::nil());
        let mut cfgs = std::collections::BTreeMap::new();
        cfgs.insert("interval.ms".to_string(), "60000".to_string());
        img.apply(&MetadataRecord::V1ClientMetricsConfig(
            ClientMetricsConfigRecord {
                name: "sub-a".into(),
                configs: cfgs,
            },
        ));
        assert2::assert!(
            img.client_metrics_config("sub-a")
                .and_then(|m| m.get("interval.ms"))
                .map(String::as_str)
                == Some("60000")
        );
        assert2::assert!(img.client_metrics_subscriptions().count() == 1);
        img.apply(&MetadataRecord::V1ClientMetricsConfig(
            ClientMetricsConfigRecord {
                name: "sub-a".into(),
                configs: std::collections::BTreeMap::new(),
            },
        ));
        assert2::assert!(img.client_metrics_config("sub-a").is_none());
        assert2::assert!(img.client_metrics_subscriptions().count() == 0);
    }

    #[test]
    fn group_config_apply_snapshot_and_clear() {
        let mut image = MetadataImage::new(Uuid::nil());
        let record = MetadataRecord::V1GroupConfig(GroupConfigRecord {
            group_id: "streams-app".into(),
            configs: BTreeMap::from([("streams.num.standby.replicas".into(), "1".into())]),
        });
        image.apply(&record);
        assert2::assert!(
            image
                .group_config("streams-app")
                .and_then(|configs| configs.get("streams.num.standby.replicas"))
                .map(String::as_str)
                == Some("1")
        );
        assert2::assert!(image.to_records().iter().any(|item| item == &record));

        image.apply(&MetadataRecord::V1GroupConfig(GroupConfigRecord {
            group_id: "streams-app".into(),
            configs: BTreeMap::new(),
        }));
        assert2::assert!(image.group_config("streams-app").is_none());
    }

    #[test]
    fn producer_id_boundary_is_monotonic_and_survives_snapshot() {
        let mut image = MetadataImage::new(Uuid::nil());
        image.apply(&MetadataRecord::V1ProducerIds(ProducerIdsRecord {
            broker_id: NodeId(1),
            broker_epoch: 4,
            next_producer_id: 1_000,
        }));
        image.apply(&MetadataRecord::V1ProducerIds(ProducerIdsRecord {
            broker_id: NodeId(2),
            broker_epoch: 8,
            next_producer_id: 2_000,
        }));
        image.apply(&MetadataRecord::V1ProducerIds(ProducerIdsRecord {
            broker_id: NodeId(1),
            broker_epoch: 4,
            next_producer_id: 1_000,
        }));
        assert2::assert!(image.next_producer_id() == 2_000);

        let restored = MetadataImage::from_records(Uuid::nil(), &image.to_records());
        assert2::assert!(restored.next_producer_id() == 2_000);
    }

    /// KIP-1276: an ACL whose host is a CIDR block raises the floor to
    /// `4.4-IV1`, and a downgrade below it removes exactly those ACLs.
    #[test]
    fn cidr_acl_hosts_follow_the_kip_1276_gate() {
        use crate::{
            AclEntryFilter,
            acl::{AclOperation, PermissionType},
            metadata_version::{CIDR_ACL_MIN_LEVEL, METADATA_VERSION_MIN},
        };

        let acl = |host: &str| AclEntry {
            resource_type: ResourceType::Topic,
            resource_name: "orders".into(),
            pattern_type: PatternType::Literal,
            principal: "User:alice".into(),
            host: host.into(),
            operation: AclOperation::Read,
            permission_type: PermissionType::Allow,
        };
        let delete = |host: &str| {
            let entry = acl(host);
            MetadataRecord::V1DeleteAccessControlEntry(AclEntryFilter {
                resource_type: Some(entry.resource_type),
                resource_name: Some(entry.resource_name),
                pattern_type: Some(entry.pattern_type),
                principal: Some(entry.principal),
                host: Some(entry.host),
                operation: Some(entry.operation),
                permission_type: Some(entry.permission_type),
            })
        };
        for (case, hosts, floor, cidr_hosts) in [
            ("no ACLs", vec![], METADATA_VERSION_MIN, vec![]),
            (
                "wildcard and address",
                vec!["*", "10.0.0.1"],
                METADATA_VERSION_MIN,
                vec![],
            ),
            (
                "IPv4 CIDR",
                vec!["*", "10.0.0.0/8"],
                CIDR_ACL_MIN_LEVEL,
                vec!["10.0.0.0/8"],
            ),
            (
                "IPv6 CIDR",
                vec!["2001:db8::/32"],
                CIDR_ACL_MIN_LEVEL,
                vec!["2001:db8::/32"],
            ),
        ] {
            let mut image = img();
            for host in &hosts {
                image.apply(&MetadataRecord::V1AccessControlEntry(acl(host)));
            }
            let want_cleanup: Vec<_> = cidr_hosts.into_iter().map(delete).collect();
            assert2::check!(
                (
                    image.min_required_metadata_version(),
                    image.metadata_version_downgrade_records(CIDR_ACL_MIN_LEVEL),
                    image.metadata_version_downgrade_records(CIDR_ACL_MIN_LEVEL - 1),
                ) == (floor, vec![], want_cleanup.clone()),
                "case {case}"
            );
            for record in &want_cleanup {
                image.apply(record);
            }
            assert2::check!(
                image.min_required_metadata_version() == METADATA_VERSION_MIN,
                "case {case} after cleanup"
            );
        }
    }

    #[test]
    fn min_required_metadata_version_rises_with_scram_and_tokens() {
        use krabka_security::{KafkaPrincipal, SaslMechanism};

        use crate::metadata_version::{DELEGATION_TOKEN_MIN_LEVEL, SCRAM_MIN_LEVEL};
        let mut m = img();
        m.apply(&MetadataRecord::V1ScramCredential(
            crate::records::ScramCredentialRecord {
                user: "alice".into(),
                mechanism: SaslMechanism::ScramSha512,
                salt: vec![1; 16],
                stored_key: vec![2; 64],
                server_key: vec![3; 64],
                iterations: 4096,
            },
        ));
        assert2::assert!(m.min_required_metadata_version() == SCRAM_MIN_LEVEL);
        m.apply(&MetadataRecord::V1DelegationToken(
            crate::records::DelegationTokenRecord {
                token_id: "t1".into(),
                owner: KafkaPrincipal {
                    principal_type: "User".into(),
                    name: "alice".into(),
                },
                requester: KafkaPrincipal {
                    principal_type: "User".into(),
                    name: "alice".into(),
                },
                issue_timestamp_ms: 1,
                expiry_timestamp_ms: 5,
                max_timestamp_ms: 10,
                renewers: vec![],
            },
        ));
        assert2::assert!(m.min_required_metadata_version() == DELEGATION_TOKEN_MIN_LEVEL);
    }

    #[test]
    fn downgrade_projection_removes_scram_and_tokens_below_their_record_gates() {
        use krabka_security::{KafkaPrincipal, SaslMechanism};

        use crate::{
            metadata_version::{METADATA_VERSION_MIN, SCRAM_MIN_LEVEL},
            records::{
                DelegationTokenRecord, DeleteDelegationTokenRecord, DeleteScramCredentialRecord,
                ScramCredentialRecord,
            },
        };

        let mut image = img();
        image.apply(&MetadataRecord::V1ScramCredential(ScramCredentialRecord {
            user: "alice".into(),
            mechanism: SaslMechanism::ScramSha512,
            salt: vec![1; 16],
            stored_key: vec![2; 64],
            server_key: vec![3; 64],
            iterations: 4096,
        }));
        image.apply(&MetadataRecord::V1DelegationToken(DelegationTokenRecord {
            token_id: "token-1".into(),
            owner: KafkaPrincipal {
                principal_type: "User".into(),
                name: "alice".into(),
            },
            requester: KafkaPrincipal {
                principal_type: "User".into(),
                name: "alice".into(),
            },
            issue_timestamp_ms: 1,
            expiry_timestamp_ms: 5,
            max_timestamp_ms: 10,
            renewers: vec![],
        }));

        // At the level that introduced each feature the state is still
        // supported, so nothing is cleaned up. Read as `target <= MIN_LEVEL`,
        // a downgrade *to* the introducing level deletes the very credentials
        // and tokens that level exists to support.
        assert2::assert!(
            image
                .metadata_version_downgrade_records(SCRAM_MIN_LEVEL)
                .iter()
                .all(|r| !matches!(r, MetadataRecord::V1DeleteScramCredential(_)))
        );
        assert2::assert!(
            image
                .metadata_version_downgrade_records(
                    crate::metadata_version::DELEGATION_TOKEN_MIN_LEVEL
                )
                .iter()
                .all(|r| !matches!(r, MetadataRecord::V1DeleteDelegationToken(_)))
        );

        let cleanup = image.metadata_version_downgrade_records(SCRAM_MIN_LEVEL - 1);
        assert2::assert!(
            cleanup
                == vec![
                    MetadataRecord::V1DeleteScramCredential(DeleteScramCredentialRecord {
                        user: "alice".into(),
                        mechanism: SaslMechanism::ScramSha512,
                    }),
                    MetadataRecord::V1DeleteDelegationToken(DeleteDelegationTokenRecord {
                        token_id: "token-1".into(),
                    }),
                ]
        );

        for record in &cleanup {
            image.apply(record);
        }
        assert2::assert!(image.scram_credentials_for_user("alice").is_empty());
        assert2::assert!(image.all_delegation_tokens().next().is_none());
        assert2::assert!(image.min_required_metadata_version() == METADATA_VERSION_MIN);
    }

    #[test]
    fn downgrade_projection_clears_directory_fields_below_kip_858() {
        use crate::metadata_version::DIRECTORY_ASSIGNMENT_MIN_LEVEL;

        let directory = Uuid::from_u128(0xD1);
        let mut image = img();
        let broker = BrokerRegistrationRecord {
            node_id: NodeId(1),
            broker_epoch: 7,
            incarnation_id: Uuid::from_u128(1),
            host: "broker-1".into(),
            port: 9092,
            rack: None,
            endpoints: vec![],
            log_dirs: vec![directory],
            fenced: false,
            in_controlled_shutdown: false,
            cordoned_log_dirs: None,
            features: std::collections::BTreeMap::new(),
        };
        image.apply(&MetadataRecord::V1BrokerRegistration(broker.clone()));
        image.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "orders".into(),
            partition: 0,
            leader: NodeId(1),
            replicas: vec![NodeId(1), NodeId(2)],
            isr: vec![NodeId(1), NodeId(2)],
            directories: vec![directory, crate::directory_id::MIGRATING],
            ..Default::default()
        }));

        assert2::assert!(
            image
                .metadata_version_downgrade_records(DIRECTORY_ASSIGNMENT_MIN_LEVEL)
                .is_empty()
        );
        let cleanup = image.metadata_version_downgrade_records(DIRECTORY_ASSIGNMENT_MIN_LEVEL - 1);
        let mut projected_broker = broker;
        projected_broker.log_dirs.clear();
        assert2::assert!(
            cleanup
                == vec![
                    MetadataRecord::V1BrokerRegistration(projected_broker),
                    MetadataRecord::V1PartitionDirAssignment(
                        crate::records::PartitionDirAssignmentRecord {
                            topic: "orders".into(),
                            partition: 0,
                            replica: NodeId(1),
                            directory: crate::directory_id::MIGRATING,
                        },
                    ),
                ]
        );

        for record in &cleanup {
            image.apply(record);
        }
        assert2::assert!(
            image.partition("orders", 0).expect("partition").directories
                == vec![crate::directory_id::MIGRATING; 2]
        );
        assert2::assert!(image.broker(NodeId(1)).expect("broker").log_dirs.is_empty());
    }

    /// KIP-1066 cordoned log dirs exist from `4.3-IV0`: a downgrade below it
    /// re-registers the broker without them, and below `3.7-IV2` without its
    /// log dirs as well.
    #[test]
    fn downgrade_projection_clears_cordoned_log_dirs_below_kip_1066() {
        use crate::metadata_version::{
            CORDONED_LOG_DIRS_MIN_LEVEL, DIRECTORY_ASSIGNMENT_MIN_LEVEL,
        };

        let directory = Uuid::from_u128(0xD1);
        let broker = BrokerRegistrationRecord {
            node_id: NodeId(1),
            broker_epoch: 7,
            incarnation_id: Uuid::from_u128(1),
            host: "broker-1".into(),
            port: 9092,
            rack: None,
            endpoints: vec![],
            log_dirs: vec![directory],
            fenced: false,
            in_controlled_shutdown: false,
            cordoned_log_dirs: Some(vec![directory]),
            features: std::collections::BTreeMap::new(),
        };
        let mut image = img();
        image.apply(&MetadataRecord::V1BrokerRegistration(broker.clone()));

        let uncordoned = BrokerRegistrationRecord {
            cordoned_log_dirs: None,
            ..broker.clone()
        };
        let without_dirs = BrokerRegistrationRecord {
            log_dirs: vec![],
            ..uncordoned.clone()
        };
        for (target, want) in [
            (CORDONED_LOG_DIRS_MIN_LEVEL, vec![]),
            (
                CORDONED_LOG_DIRS_MIN_LEVEL - 1,
                vec![MetadataRecord::V1BrokerRegistration(uncordoned)],
            ),
            (
                DIRECTORY_ASSIGNMENT_MIN_LEVEL - 1,
                vec![MetadataRecord::V1BrokerRegistration(without_dirs)],
            ),
        ] {
            assert2::check!(
                image.metadata_version_downgrade_records(target) == want,
                "target {target}"
            );
        }
    }

    #[test]
    fn topic_by_id_resolves_and_drops_on_delete() {
        use crate::records::{MetadataRecord, TopicRecord};
        let mut img = MetadataImage::new(Uuid::nil());
        let id = Uuid::from_u128(0x1234_5678_9abc_def0_1122_3344_5566_7788);
        img.apply(&MetadataRecord::V1Topic(TopicRecord {
            name: "orders".into(),
            topic_id: id,
            partitions: 1,
            replication_factor: 1,
        }));

        // Resolves by id to the same record `topic(name)` returns.
        assert2::assert!(img.topic_by_id(&id).map(|t| t.name.as_str()) == Some("orders"));
        assert2::assert!(img.topic_name_by_id(&id) == Some("orders"));

        // After delete the id no longer resolves and the name index is gone.
        img.apply(&MetadataRecord::V1DeleteTopic(
            crate::records::DeleteTopicRecord {
                name: "orders".into(),
            },
        ));
        assert2::assert!(img.topic_by_id(&id).is_none());
        assert2::assert!(img.topic_name_by_id(&id).is_none());
    }

    #[test]
    fn broker_epoch_reads_back_registered_epoch() {
        let mut image = MetadataImage::new(Uuid::nil());
        image.apply(&MetadataRecord::V1BrokerRegistration(
            BrokerRegistrationRecord {
                node_id: NodeId(5),
                broker_epoch: 99,
                incarnation_id: Uuid::nil(),
                host: "h".into(),
                port: 9092,
                rack: None,
                log_dirs: vec![],
                fenced: false,
                in_controlled_shutdown: false,
                cordoned_log_dirs: None,
                endpoints: vec![],
                features: std::collections::BTreeMap::new(),
            },
        ));
        assert2::assert!(image.broker_epoch(NodeId(5)) == Some(99));
        assert2::assert!(image.broker_epoch(NodeId(404)) == None);
    }

    // --- mutation-coverage tests --------------------------------------------

    #[test]
    fn fresh_image_accessors() {
        let id = Uuid::from_u128(0xABCD);
        let image = MetadataImage::new(id);
        assert2::assert!(image.cluster_id() == id);
        assert2::assert!(image.kraft_version() == 0);
    }

    #[test]
    fn broker_lookup_returns_registered_record() {
        let mut image = MetadataImage::new(Uuid::nil());
        image.apply(&MetadataRecord::V1BrokerRegistration(
            BrokerRegistrationRecord {
                node_id: NodeId(7),
                broker_epoch: 1,
                incarnation_id: Uuid::nil(),
                host: "h".into(),
                port: 9092,
                rack: None,
                log_dirs: vec![],
                fenced: false,
                in_controlled_shutdown: false,
                cordoned_log_dirs: None,
                endpoints: vec![],
                features: std::collections::BTreeMap::new(),
            },
        ));
        let b = image
            .broker(NodeId(7))
            .expect("registered broker resolvable");
        assert2::assert!((b.node_id, image.broker(NodeId(404)).is_none()) == (NodeId(7), true));
    }

    #[test]
    fn reapplying_topic_with_new_id_drops_stale_id_index() {
        let mut image = MetadataImage::new(Uuid::nil());
        let old_id = Uuid::from_u128(1);
        let new_id = Uuid::from_u128(2);
        let topic = |id| {
            MetadataRecord::V1Topic(TopicRecord {
                name: "t".into(),
                topic_id: id,
                partitions: 1,
                replication_factor: 1,
            })
        };
        image.apply(&topic(old_id));
        assert2::assert!(image.topic_by_id(&old_id).is_some());
        // Re-apply the same NAME with a different id: the stale id index entry
        // must be dropped (the `prev.topic_id != t.topic_id` guard).
        image.apply(&topic(new_id));
        assert2::assert!(image.topic_by_id(&new_id).is_some());
        assert2::assert!(image.topic_by_id(&old_id).is_none());
    }

    #[test]
    fn broker_throttle_rate_allows_zero() {
        // A configured rate of 0 is a real (zero) value, distinct from absent
        // or -1 (disabled). The cap check is `v < 0` (not `<=`), so 0 -> Some(0).
        let mut image = MetadataImage::new(Uuid::nil());
        image.apply(&MetadataRecord::V1BrokerConfig(BrokerConfigRecord {
            node_id: NodeId(3),
            config_name: "leader.replication.throttled.rate".into(),
            config_value: Some("0".into()),
        }));
        assert2::assert!(
            image.broker_throttle_rate(NodeId(3), ThrottleKind::Leader)
                == Some(<ByteRate as ByteRateExt>::ZERO)
        );
    }

    #[test]
    fn dir_assignment_resizes_directories_to_replica_count() {
        // KIP-858: a dir-assignment delta for a partition whose `directories`
        // vec is shorter than `replicas` must grow it (to `replicas.len()`,
        // padded with `MIGRATING`, which is how Kafka reads an empty list)
        // before writing the reporting replica's slot. Without the resize the
        // slot write would index out of bounds.
        let mut image = MetadataImage::new(Uuid::nil());
        image.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "t".into(),
            partition: 0,
            leader: NodeId(10),
            replicas: vec![NodeId(10), NodeId(20)],
            isr: vec![NodeId(10), NodeId(20)],
            directories: vec![], // not yet reported
            ..Default::default()
        }));
        let dir = Uuid::from_u128(0xD15);
        image.apply(&MetadataRecord::V1PartitionDirAssignment(
            crate::records::PartitionDirAssignmentRecord {
                topic: "t".into(),
                partition: 0,
                replica: NodeId(10),
                directory: dir,
            },
        ));
        let pr = image.partition("t", 0).expect("partition present");
        // Replica 10 is slot 0; replica 20 stays `MIGRATING`.
        assert2::assert!(pr.directories == vec![dir, crate::directory_id::MIGRATING]);
    }

    #[test]
    fn dir_assignment_updates_existing_full_length_directories() {
        let mut image = MetadataImage::new(Uuid::nil());
        let old = Uuid::from_u128(0xAAA);
        image.apply(&MetadataRecord::V1Partition(PartitionRecord {
            topic: "t".into(),
            partition: 0,
            leader: NodeId(10),
            replicas: vec![NodeId(10), NodeId(20)],
            isr: vec![NodeId(10), NodeId(20)],
            directories: vec![old, Uuid::nil()],
            ..Default::default()
        }));
        let dir = Uuid::from_u128(0xD15);
        image.apply(&MetadataRecord::V1PartitionDirAssignment(
            crate::records::PartitionDirAssignmentRecord {
                topic: "t".into(),
                partition: 0,
                replica: NodeId(20),
                directory: dir,
            },
        ));

        let pr = image.partition("t", 0).expect("partition present");
        assert2::assert!(pr.directories == vec![old, dir]);
    }
    // ---------- KFC-9 write-freeze registry ----------

    fn freeze(scope: &str, pattern_type: PatternType) -> TopicFreezeRecord {
        TopicFreezeRecord {
            scope: scope.into(),
            pattern_type,
            frozen: true,
            reason: "DR cutover".into(),
            set_by: "User:alice".into(),
            set_at_ms: 1_700_000_000_000,
            proposal_id: Uuid::nil(),
            key_id: "alice-yubi".into(),
            signature: vec![0xAB; 64],
        }
    }

    #[test]
    fn an_unfrozen_cluster_resolves_no_freeze() {
        check!(img().topic_freeze("orders").is_none());
        check!(img().topic_freezes().count() == 0);
    }

    #[test]
    fn a_literal_freeze_beats_a_prefix_and_the_longest_prefix_wins() {
        let mut image = img();
        for (scope, pattern_type) in [
            ("tenant-a.", PatternType::Prefixed),
            ("tenant-a.orders.", PatternType::Prefixed),
            ("tenant-a.orders.eu", PatternType::Literal),
        ] {
            image.apply(&MetadataRecord::V1TopicFreeze(freeze(scope, pattern_type)));
        }

        for (label, topic, want) in [
            (
                "a literal entry beats every prefix entry",
                "tenant-a.orders.eu",
                Some("tenant-a.orders.eu"),
            ),
            (
                "the longest matching prefix wins",
                "tenant-a.orders.us",
                Some("tenant-a.orders."),
            ),
            (
                "a shorter prefix still matches",
                "tenant-a.billing",
                Some("tenant-a."),
            ),
            ("no scope covers the topic", "tenant-b.orders", None),
            ("a topic that sorts below every scope", "aaa", None),
        ] {
            check!(
                image.topic_freeze(topic).map(|f| f.scope.as_str()) == want,
                "{label}"
            );
        }
    }

    #[test]
    fn an_internal_topic_is_never_frozen() {
        for (label, scope, pattern_type, covered) in [
            (
                "an empty prefix scope",
                "",
                PatternType::Prefixed,
                Some("orders"),
            ),
            (
                "a single-underscore prefix scope",
                "_",
                PatternType::Prefixed,
                Some("_metrics"),
            ),
            (
                "a literal internal name",
                "__consumer_offsets",
                PatternType::Literal,
                None,
            ),
        ] {
            let mut image = img();
            image.apply(&MetadataRecord::V1TopicFreeze(freeze(scope, pattern_type)));

            check!(
                image.topic_freeze("__consumer_offsets").is_none(),
                "{label}"
            );
            if let Some(topic) = covered {
                // The registry is live, so the exemption comes from the name
                // and not from an empty registry.
                check!(image.topic_freeze(topic).is_some(), "{label}");
            }
        }
    }

    #[test]
    fn a_thaw_removes_the_registry_entry() {
        for (label, pattern_type, topic) in [
            ("a literal scope", PatternType::Literal, "orders"),
            ("a prefixed scope", PatternType::Prefixed, "orders-eu"),
        ] {
            let mut image = img();
            let frozen = freeze("orders", pattern_type);
            image.apply(&MetadataRecord::V1TopicFreeze(frozen.clone()));
            check!(image.topic_freeze(topic).is_some(), "{label}");

            image.apply(&MetadataRecord::V1TopicFreeze(TopicFreezeRecord {
                frozen: false,
                proposal_id: Uuid::from_u128(0xB1),
                ..frozen
            }));
            check!(image.topic_freeze(topic).is_none(), "{label}");
            check!(image.topic_freezes().count() == 0, "{label}");
        }
    }

    #[test]
    fn a_later_freeze_replaces_the_entry_at_the_same_scope() {
        let mut image = img();
        image.apply(&MetadataRecord::V1TopicFreeze(freeze(
            "orders",
            PatternType::Literal,
        )));
        let replacement = TopicFreezeRecord {
            reason: "tenant offboarding".into(),
            set_by: "User:bob".into(),
            set_at_ms: 1_700_000_060_000,
            ..freeze("orders", PatternType::Literal)
        };
        image.apply(&MetadataRecord::V1TopicFreeze(replacement.clone()));

        check!(image.topic_freeze("orders") == Some(&replacement));
        check!(image.topic_freezes().count() == 1);
    }

    // ---------- KFC-9 break-glass proposals ----------

    fn approval(principal: &str) -> BreakGlassApproval {
        BreakGlassApproval {
            principal: principal.into(),
            approved_at_ms: 1_700_000_060_000,
            key_id: format!("{principal}-yubi"),
            signature: vec![0xCD; 64],
        }
    }

    fn proposal(id: Uuid) -> BreakGlassProposalRecord {
        BreakGlassProposalRecord {
            proposal_id: id,
            action: crate::break_glass::BreakGlassAction::ThawTopicFreeze,
            target: "literal:orders".into(),
            proposer: "User:alice".into(),
            reason: "incident 4711 closed".into(),
            created_at_ms: 1_700_000_000_000,
            expires_at_ms: 1_700_000_600_000,
            approvals: Vec::new(),
            consumed_at_ms: 0,
            withdrawn: false,
        }
    }

    #[test]
    fn a_break_glass_proposal_upserts_and_deletes() {
        let id = Uuid::from_u128(0xB1);
        let mut image = img();
        let record = proposal(id);
        image.apply(&MetadataRecord::V1BreakGlassProposal(record.clone()));
        check!(image.break_glass_proposal(id) == Some(&record));
        check!(image.break_glass_proposals().count() == 1);

        let approved = BreakGlassProposalRecord {
            approvals: vec![approval("User:bob")],
            ..record
        };
        image.apply(&MetadataRecord::V1BreakGlassProposal(approved.clone()));
        check!(image.break_glass_proposal(id) == Some(&approved));

        image.apply(&MetadataRecord::V1DeleteBreakGlassProposal(id));
        check!(image.break_glass_proposal(id).is_none());
        check!(image.break_glass_proposals().count() == 0);
    }

    #[test]
    fn to_records_preserves_both_freeze_registries_and_the_proposals() {
        let cid = Uuid::new_v4();
        let mut image = MetadataImage::new(cid);
        let literal = freeze("orders", PatternType::Literal);
        let prefixed = freeze("tenant-a.", PatternType::Prefixed);
        let pending = BreakGlassProposalRecord {
            approvals: vec![approval("User:bob")],
            ..proposal(Uuid::from_u128(0xB1))
        };
        image.apply(&MetadataRecord::V1TopicFreeze(literal.clone()));
        image.apply(&MetadataRecord::V1TopicFreeze(prefixed.clone()));
        image.apply(&MetadataRecord::V1BreakGlassProposal(pending.clone()));

        let rebuilt = MetadataImage::from_records(cid, &image.to_records());

        check!(rebuilt == image);
        check!(rebuilt.topic_freeze("orders") == Some(&literal));
        check!(rebuilt.topic_freeze("tenant-a.billing") == Some(&prefixed));
        check!(rebuilt.break_glass_proposal(pending.proposal_id) == Some(&pending));
    }

    // ---------- KFC-9 validate ----------

    #[test]
    fn validate_rejects_only_an_empty_freeze_scope() {
        let image = img();
        for (label, scope, pattern_type, want) in [
            (
                "a literal scope naming a topic that does not exist",
                "orders",
                PatternType::Literal,
                Ok(()),
            ),
            (
                "a prefix scope, which names no topic by construction",
                "tenant-a.",
                PatternType::Prefixed,
                Ok(()),
            ),
            (
                "an empty literal scope",
                "",
                PatternType::Literal,
                Err(MetadataError::EmptyFreezeScope),
            ),
            (
                "an empty prefix scope",
                "",
                PatternType::Prefixed,
                Err(MetadataError::EmptyFreezeScope),
            ),
        ] {
            let rec = MetadataRecord::V1TopicFreeze(freeze(scope, pattern_type));
            check!(image.validate(&rec) == want, "{label}");
        }
    }

    #[test]
    fn validate_accepts_a_break_glass_proposal_that_is_not_yet_stored() {
        let image = img();
        let rec = MetadataRecord::V1BreakGlassProposal(proposal(Uuid::from_u128(0xB1)));
        check!(image.validate(&rec) == Ok(()));
    }

    #[test]
    fn validate_accepts_only_an_appended_approval() {
        let id = Uuid::from_u128(0xB1);
        let (alice, bob, carol) = (
            approval("User:alice"),
            approval("User:bob"),
            approval("User:carol"),
        );
        let mut image = img();
        let stored = BreakGlassProposalRecord {
            approvals: vec![alice.clone(), bob.clone()],
            ..proposal(id)
        };
        image.apply(&MetadataRecord::V1BreakGlassProposal(stored.clone()));

        for (label, approvals, want) in [
            (
                "the stored list unchanged, which a consumption writes back",
                vec![alice.clone(), bob.clone()],
                Ok(()),
            ),
            (
                "one appended approval",
                vec![alice.clone(), bob.clone(), carol.clone()],
                Ok(()),
            ),
            (
                "a reordered list",
                vec![bob.clone(), alice.clone()],
                Err(MetadataError::BreakGlassApprovalsNotAnExtension(id)),
            ),
            (
                "a truncated list",
                vec![alice.clone()],
                Err(MetadataError::BreakGlassApprovalsNotAnExtension(id)),
            ),
            (
                "an empty list",
                Vec::new(),
                Err(MetadataError::BreakGlassApprovalsNotAnExtension(id)),
            ),
            (
                "a replaced approval",
                vec![alice.clone(), carol.clone()],
                Err(MetadataError::BreakGlassApprovalsNotAnExtension(id)),
            ),
        ] {
            let candidate = MetadataRecord::V1BreakGlassProposal(BreakGlassProposalRecord {
                approvals,
                ..stored.clone()
            });
            check!(image.validate(&candidate) == want, "{label}");
        }
    }

    /// Two approvers read the same image and each submits the list it read
    /// plus its own entry. Without the extension rule the second write drops
    /// the first approval, and the proposal never reaches two principals.
    #[test]
    fn a_concurrent_approval_cannot_drop_the_stored_one() {
        let id = Uuid::from_u128(0xB1);
        let mut image = img();
        image.apply(&MetadataRecord::V1BreakGlassProposal(proposal(id)));

        let by_bob = BreakGlassProposalRecord {
            approvals: vec![approval("User:bob")],
            ..proposal(id)
        };
        let by_carol = BreakGlassProposalRecord {
            approvals: vec![approval("User:carol")],
            ..proposal(id)
        };
        check!(image.validate(&MetadataRecord::V1BreakGlassProposal(by_bob.clone())) == Ok(()));
        image.apply(&MetadataRecord::V1BreakGlassProposal(by_bob));

        check!(
            image.validate(&MetadataRecord::V1BreakGlassProposal(by_carol))
                == Err(MetadataError::BreakGlassApprovalsNotAnExtension(id))
        );

        let both = BreakGlassProposalRecord {
            approvals: vec![approval("User:bob"), approval("User:carol")],
            ..proposal(id)
        };
        check!(image.validate(&MetadataRecord::V1BreakGlassProposal(both)) == Ok(()));
    }

    #[test]
    fn validate_rejects_every_change_to_a_settled_proposal() {
        let id = Uuid::from_u128(0xB1);
        for (label, settled, want) in [
            (
                "a consumed proposal",
                BreakGlassProposalRecord {
                    consumed_at_ms: 1_700_000_120_000,
                    ..proposal(id)
                },
                MetadataError::BreakGlassProposalConsumed(id),
            ),
            (
                "a withdrawn proposal",
                BreakGlassProposalRecord {
                    withdrawn: true,
                    ..proposal(id)
                },
                MetadataError::BreakGlassProposalWithdrawn(id),
            ),
        ] {
            let mut image = img();
            image.apply(&MetadataRecord::V1BreakGlassProposal(settled.clone()));

            for (change_label, candidate) in [
                (
                    "an appended approval",
                    BreakGlassProposalRecord {
                        approvals: vec![approval("User:bob")],
                        ..settled.clone()
                    },
                ),
                (
                    "a second consumption",
                    BreakGlassProposalRecord {
                        consumed_at_ms: 1_700_000_180_000,
                        ..settled.clone()
                    },
                ),
                ("the record unchanged", settled.clone()),
            ] {
                let rec = MetadataRecord::V1BreakGlassProposal(candidate);
                check!(
                    image.validate(&rec) == Err(want.clone()),
                    "{label}, {change_label}"
                );
            }
        }
    }

    #[test]
    fn validate_accepts_a_break_glass_tombstone_for_any_id() {
        let image = img();
        let rec = MetadataRecord::V1DeleteBreakGlassProposal(Uuid::from_u128(0xB1));
        check!(image.validate(&rec) == Ok(()));
    }
}