matter-commissioning 0.4.0

Matter commissioning state machine: setup payload, attestation, NOC issuance, network commissioning.
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
//! Shared integration-test infrastructure for the M6.6.4 commission-loopback
//! test suite.
//!
//! This module is compiled as part of the `tests/` tree. It is declared from
//! each loopback test file via `mod support;`. Production code must never
//! depend on anything in this module.
//!
//! # Contents (grows across Tasks 7b-11)
//!
//! - **Task 7b** (this file): [`build_mock_device_pki`] – synthetic PAA/PAI/DAC
//!   chain accepted by the Commissioner's real `verify_chain`.
//! - **Task 8** (this file): [`build_attestation_response`], [`build_csr_response`],
//!   [`load_cd_fixture`] – mock-device builders whose outputs the Commissioner's
//!   real verifiers accept.
//!
//! # Key PKCS#8 compatibility note
//!
//! [`matter_crypto::RingSigner::generate`] generates keys via
//! `ring::signature::EcdsaKeyPair::generate_pkcs8` with
//! `ECDSA_P256_SHA256_FIXED_SIGNING`. The [`matter_cert::test_support::build_x509_der`]
//! helper loads the issuer PKCS#8 with `ECDSA_P256_SHA256_ASN1_SIGNING`. Both
//! signing variants share the same PKCS#8 v1 DER key format for P-256 — only
//! the *output* encoding of the signature differs (fixed/IEEE-P1363 vs.
//! ASN.1/DER). Cross-loading the PKCS#8 therefore works without any conversion.
//! This was verified empirically by the Task 7b gate test in `mock_pki.rs`.

#![allow(clippy::unwrap_used, clippy::expect_used)]
// Domain acronyms (PAA, PAI, DAC, VID, PID, EKU, PKCS#8) are prose, not code items.
#![allow(clippy::doc_markdown)]
// paa_pkcs8 / pai_pkcs8 are intentionally paired names.
#![allow(clippy::similar_names)]
// `pub` items in test support modules are used by sibling test binaries
// (commission_loopback.rs in Task 12). Clippy sees this module in isolation
// and thinks they're unreachable; they're not.
#![allow(dead_code, unreachable_pub)]
// The verbatim-copied DER helpers from src/noc/csr.rs use intentional
// truncating casts and panic-in-if (matching the source's allow list).
#![allow(clippy::cast_possible_truncation, clippy::manual_assert)]

use std::path::PathBuf;

use base64::Engine;
use matter_cert::test_support::{build_x509_der, TestCertFields};
use matter_cert::{
    BasicConstraints, DistinguishedName, DnAttribute, Extensions, KeyIdentifier, KeyUsage,
    MatterTime, Signature, TrustedRoots,
};
use matter_commissioning::attestation::{AttestationResponse, Paa, PaaTrustStore};
use matter_commissioning::driver::{
    decode_unsecured, encode_unsecured, AsyncDatagram, DriverError, InMemoryDatagram,
};
use matter_commissioning::CsrResponse;
use matter_crypto::pase::{PasePbkdfParams, PaseVerifier};
use matter_crypto::{CaseCredentials, CaseResponder, CaseSigner as _, RingSigner, Sigma1Outcome};
use matter_transport::{
    DecodeInboundOutput, MrpFlags, PeerHint, ProtocolId, SessionId, SessionManager, SessionRole,
};
use serde::Deserialize;

// ── Constants ────────────────────────────────────────────────────────────────

/// Vendor ID used for the mock device's DAC/PAI (and PAA for completeness).
///
/// 0xFFF1 is the CSA test-fixture VID. MUST match the Certification Declaration
/// fixture used in Task 8's CD verification step.
pub const VID: u16 = 0xFFF1;

/// Product ID encoded in the mock device's DAC.
///
/// MUST match the Certification Declaration fixture used in Task 8.
pub const PID: u16 = 0x8001;

/// EKU compact integer for `id-kp-clientAuth` (OID 1.3.6.1.5.5.7.3.2).
///
/// The `matter-cert` X.509 encoder maps integer `2` to the clientAuth EKU
/// in the `ExtendedKeyUsage` extension. See `x509_builder_gate.rs`.
const EKU_CLIENT_AUTH: u32 = 2;

// ── MockDevicePki ─────────────────────────────────────────────────────────────

/// Synthetic PAA → PAI → DAC chain for use in loopback tests.
///
/// Built by [`build_mock_device_pki`] and validated against the Commissioner's
/// real `verify_chain` before being wired into the mock responder (Task 9).
///
/// The chain uses VID [`VID`] (0xFFF1) and PID [`PID`] (0x8001), matching the
/// CSA Certification Declaration fixture used in Task 8's CD verification.
pub struct MockDevicePki {
    /// The DAC's private key. Used by the mock device to sign
    /// `AttestationElements` (Task 8) and the NOCSR (Task 9). This is what
    /// proves device identity during commissioning.
    pub dac_signer: RingSigner,
    /// DER-encoded Device Attestation Certificate (leaf, not CA).
    ///
    /// Carry this in the `AttestationResponse` TLV (`dac_der` field).
    pub dac_der: Vec<u8>,
    /// DER-encoded Product Attestation Intermediate certificate (CA, pathLen 0).
    ///
    /// Carry this in the `AttestationResponse` TLV (`pai_der` field).
    pub pai_der: Vec<u8>,
    /// DER-encoded Product Attestation Authority certificate (self-signed root).
    ///
    /// Pre-loaded into [`paa_trust_store`](Self::paa_trust_store). Exposed here
    /// so tests can inspect the raw DER if needed.
    pub paa_der: Vec<u8>,
    /// A [`PaaTrustStore`] pre-seeded with the synthetic PAA.
    ///
    /// Pass this to `attestation::chain::verify_chain` (or the Commissioner's
    /// attestation verifier in Task 9) so the chain validates.
    pub paa_trust_store: PaaTrustStore,
}

// ── Chain builder ─────────────────────────────────────────────────────────────

/// Build a synthetic PAA → PAI → DAC chain anchored at `now`.
///
/// All three certificates are generated fresh for each call (new key pairs via
/// [`RingSigner::generate`]). The validity windows bracket `now`:
///
/// - PAA: `now - 365 days` … `now + 3650 days`
/// - PAI: `now - 180 days` … `now + 1825 days`
/// - DAC: `now - 30 days`  … `now + 365 days`
///
/// The extension recipe follows `x509_builder_gate.rs` and the CSA
/// `gen-negative-fixtures.py` reference:
/// - PAA: `BasicConstraints CA:true, pathLen:1` (critical); `KeyUsage keyCertSign+cRLSign` (critical)
/// - PAI: `BasicConstraints CA:true, pathLen:0` (critical); `KeyUsage keyCertSign+cRLSign` (critical); subject VID 0xFFF1
/// - DAC: `BasicConstraints CA:false` (critical); `KeyUsage digitalSignature` (critical); `ExtendedKeyUsage clientAuth` (non-critical); subject VID 0xFFF1 + PID 0x8001
///
/// # Panics
///
/// Panics on any key-generation or DER-encoding failure. These would indicate
/// a broken environment (OS RNG failure, ring/p256 API regression) rather than
/// a test logic error.
pub fn build_mock_device_pki(now: MatterTime) -> MockDevicePki {
    let now_unix = now.to_unix_secs();

    // ── PAA: self-signed root ────────────────────────────────────────────────
    let (paa_signer, paa_pkcs8) = RingSigner::generate().expect("PAA key generation");
    let paa_pk = paa_signer.public_key().clone();

    let paa_dn = DistinguishedName::new(vec![
        DnAttribute::CommonName("Matter Test PAA (mock-device)".into()),
        DnAttribute::VendorId(VID),
    ]);
    let paa_der = build_x509_der(
        TestCertFields {
            serial: vec![0x01],
            issuer: paa_dn.clone(), // self-signed: issuer == subject
            not_before: MatterTime::from_unix_secs(now_unix.saturating_sub(365 * 86_400)),
            not_after: MatterTime::from_unix_secs(now_unix.saturating_add(3650 * 86_400)),
            subject: paa_dn.clone(),
            public_key: paa_pk,
            extensions: Extensions::builder()
                .basic_constraints(Some(BasicConstraints::new(true, Some(1))))
                .key_usage(Some(KeyUsage::KEY_CERT_SIGN | KeyUsage::CRL_SIGN))
                .subject_key_identifier(Some(KeyIdentifier([0xA0; 20])))
                .build(),
            signature: Signature::new([0u8; 64]),
        },
        &paa_pkcs8, // self-signed
    )
    .expect("PAA DER build");

    // ── PAI: signed by PAA, VID-scoped ───────────────────────────────────────
    let (pai_signer, pai_pkcs8) = RingSigner::generate().expect("PAI key generation");
    let pai_pk = pai_signer.public_key().clone();

    let pai_dn = DistinguishedName::new(vec![
        DnAttribute::CommonName("Matter Test PAI (mock-device)".into()),
        DnAttribute::VendorId(VID),
    ]);
    let pai_der = build_x509_der(
        TestCertFields {
            serial: vec![0x02],
            issuer: paa_dn, // byte-for-byte == PAA subject
            not_before: MatterTime::from_unix_secs(now_unix.saturating_sub(180 * 86_400)),
            not_after: MatterTime::from_unix_secs(now_unix.saturating_add(1825 * 86_400)),
            subject: pai_dn.clone(),
            public_key: pai_pk,
            extensions: Extensions::builder()
                .basic_constraints(Some(BasicConstraints::new(true, Some(0))))
                .key_usage(Some(KeyUsage::KEY_CERT_SIGN | KeyUsage::CRL_SIGN))
                .subject_key_identifier(Some(KeyIdentifier([0xB0; 20])))
                .authority_key_identifier(Some(KeyIdentifier([0xA0; 20])))
                .build(),
            signature: Signature::new([0u8; 64]),
        },
        &paa_pkcs8, // signed by PAA
    )
    .expect("PAI DER build");

    // The PAI signer is not returned (only the PAA signer was used for signing
    // the PAI cert, and the PAI private key signs the DAC). Suppress the warning.
    let _ = pai_signer;

    // ── DAC: leaf, signed by PAI, VID + PID, clientAuth EKU ─────────────────
    let (dac_signer, _dac_pkcs8) = RingSigner::generate().expect("DAC key generation");
    let dac_pk = dac_signer.public_key().clone();

    let dac_dn = DistinguishedName::new(vec![
        DnAttribute::CommonName("Matter Test DAC (mock-device)".into()),
        DnAttribute::VendorId(VID),
        DnAttribute::ProductId(PID),
    ]);
    let dac_der = build_x509_der(
        TestCertFields {
            serial: vec![0x03],
            issuer: pai_dn, // byte-for-byte == PAI subject
            not_before: MatterTime::from_unix_secs(now_unix.saturating_sub(30 * 86_400)),
            not_after: MatterTime::from_unix_secs(now_unix.saturating_add(365 * 86_400)),
            subject: dac_dn,
            public_key: dac_pk,
            extensions: Extensions::builder()
                .basic_constraints(Some(BasicConstraints::new(false, None)))
                .key_usage(Some(KeyUsage::DIGITAL_SIGNATURE))
                .extended_key_usage(Some(vec![EKU_CLIENT_AUTH]))
                .subject_key_identifier(Some(KeyIdentifier([0xC0; 20])))
                .authority_key_identifier(Some(KeyIdentifier([0xB0; 20])))
                .build(),
            signature: Signature::new([0u8; 64]),
        },
        &pai_pkcs8, // DAC is signed by PAI
    )
    .expect("DAC DER build");

    // ── Trust store ───────────────────────────────────────────────────────────
    let mut paa_trust_store = PaaTrustStore::empty();
    paa_trust_store.add(Paa::from_der(&paa_der).expect("PAA parses back"));

    MockDevicePki {
        dac_signer,
        dac_der,
        pai_der,
        paa_der,
        paa_trust_store,
    }
}

// ── Task 8: mock-device attestation + CSR response builders ──────────────────

// ── Verbatim copy of DER / PKCS#10 helpers from src/noc/csr.rs #[cfg(test)]  ─
//
// These functions are self-contained test helpers originally defined in the
// `#[cfg(test)] mod tests` block inside `crates/matter-commissioning/src/noc/csr.rs`.
// They are copied verbatim here (with visibility adjusted to module-private) so
// `build_csr_response` can produce a well-formed, self-signed PKCS#10 CSR that
// `verify_csr_response` accepts.  Do NOT "improve" them — correctness parity with
// the source matters more than style.

/// Mint a synthetic PKCS#10 CSR for testing. Returns `(csr_der, public_key_bytes)`.
///
/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn mint_pkcs10_csr(seed: u8) -> (Vec<u8>, [u8; 65]) {
    use p256::ecdsa::{signature::Signer, Signature, SigningKey};

    // Deterministic 32-byte big-endian scalar.
    let mut scalar = [0u8; 32];
    scalar[0] = seed;
    let signing_key = SigningKey::from_slice(&scalar).unwrap();
    let verifying_key = signing_key.verifying_key();
    let encoded = verifying_key.to_encoded_point(false);
    let mut public_key = [0u8; 65];
    public_key.copy_from_slice(encoded.as_bytes());

    let tbs_csr_info = build_pkcs10_csr_info(&public_key);
    let signature: Signature = signing_key.sign(&tbs_csr_info);
    let sig_der = signature.to_der().as_bytes().to_vec();

    let csr_der = wrap_pkcs10_csr(&tbs_csr_info, &sig_der);
    (csr_der, public_key)
}

/// Encode CertificationRequestInfo for a P-256 public key with an
/// empty subject and empty attribute set.
///
/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn build_pkcs10_csr_info(public_key_sec1: &[u8; 65]) -> Vec<u8> {
    let alg_id = encode_der_sequence(&[
        &encode_der_oid(&[1, 2, 840, 10045, 2, 1]),
        &encode_der_oid(&[1, 2, 840, 10045, 3, 1, 7]),
    ]);
    let pk_bit_string = encode_der_bit_string(public_key_sec1);
    let subject_pk_info = encode_der_sequence(&[&alg_id, &pk_bit_string]);
    let subject = encode_der_sequence(&[]);
    let version = encode_der_integer_zero();
    let attributes = encode_der_context_implicit_set(0, &[]);
    encode_der_sequence(&[&version, &subject, &subject_pk_info, &attributes])
}

/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn wrap_pkcs10_csr(tbs: &[u8], signature_der: &[u8]) -> Vec<u8> {
    let alg_id = encode_der_sequence(&[&encode_der_oid(&[1, 2, 840, 10045, 4, 3, 2])]);
    let sig_bit_string = encode_der_bit_string(signature_der);
    encode_der_sequence(&[tbs, &alg_id, &sig_bit_string])
}

/// Compose a synthetic NOCSR TLV blob from raw fields.
///
/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn write_nocsr(
    csr_der: &[u8],
    nonce: &[u8; 32],
    vr1: Option<&[u8]>,
    vr2: Option<&[u8]>,
    vr3: Option<&[u8]>,
) -> Vec<u8> {
    use matter_codec::{Tag, TlvWriter};
    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous).unwrap();
    w.put_bytes(Tag::Context(1), csr_der).unwrap();
    w.put_bytes(Tag::Context(2), nonce).unwrap();
    if let Some(b) = vr1 {
        w.put_bytes(Tag::Context(3), b).unwrap();
    }
    if let Some(b) = vr2 {
        w.put_bytes(Tag::Context(4), b).unwrap();
    }
    if let Some(b) = vr3 {
        w.put_bytes(Tag::Context(5), b).unwrap();
    }
    w.end_container().unwrap();
    buf
}

/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn encode_der_length(body_len: usize) -> Vec<u8> {
    if body_len < 0x80 {
        vec![body_len as u8]
    } else if body_len <= 0xff {
        vec![0x81, body_len as u8]
    } else if body_len <= 0xffff {
        vec![0x82, (body_len >> 8) as u8, body_len as u8]
    } else {
        panic!("test helper: length too big");
    }
}

/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn encode_der_tlv(tag: u8, body: &[u8]) -> Vec<u8> {
    let mut out = vec![tag];
    out.extend_from_slice(&encode_der_length(body.len()));
    out.extend_from_slice(body);
    out
}

/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn encode_der_sequence(parts: &[&[u8]]) -> Vec<u8> {
    let mut body = Vec::new();
    for p in parts {
        body.extend_from_slice(p);
    }
    encode_der_tlv(0x30, &body)
}

/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn encode_der_integer_zero() -> Vec<u8> {
    encode_der_tlv(0x02, &[0x00])
}

/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn encode_der_bit_string(bytes: &[u8]) -> Vec<u8> {
    let mut body = Vec::with_capacity(bytes.len() + 1);
    body.push(0x00);
    body.extend_from_slice(bytes);
    encode_der_tlv(0x03, &body)
}

/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn encode_der_context_implicit_set(tag: u8, parts: &[&[u8]]) -> Vec<u8> {
    let mut body = Vec::new();
    for p in parts {
        body.extend_from_slice(p);
    }
    encode_der_tlv(0xA0 | tag, &body)
}

/// Copied verbatim from `src/noc/csr.rs` `#[cfg(test)] mod tests`.
fn encode_der_oid(arcs: &[u32]) -> Vec<u8> {
    let mut body = Vec::new();
    if arcs.len() < 2 {
        panic!("OID needs at least two arcs");
    }
    body.push((arcs[0] * 40 + arcs[1]) as u8);
    for arc in &arcs[2..] {
        let mut a = *arc;
        let mut digits = Vec::new();
        digits.push((a & 0x7f) as u8);
        a >>= 7;
        while a > 0 {
            digits.push(((a & 0x7f) | 0x80) as u8);
            a >>= 7;
        }
        digits.reverse();
        body.extend_from_slice(&digits);
    }
    encode_der_tlv(0x06, &body)
}

// ── CD fixture loading ────────────────────────────────────────────────────────

#[derive(Deserialize)]
struct CdFixture {
    cd_b64: String,
}

/// Load the Certification Declaration bytes from
/// `test-vectors/commissioning/cd/happy-path.json`.
///
/// The fixture contains a `cd_b64` field with a standard base64-encoded
/// CMS/PKCS#7 `SignedData` blob.  The encoded CD covers VID 0xFFF1 and
/// PID 0x8001 — matching [`VID`] and [`PID`] in the mock PKI.
///
/// # Panics
///
/// Panics if the fixture file is missing or the JSON/base64 is malformed.
/// These would indicate a broken test environment, not a test logic error.
pub fn load_cd_fixture() -> Vec<u8> {
    let mut path: PathBuf = env!("CARGO_MANIFEST_DIR").into();
    path.push("..");
    path.push("..");
    path.push("test-vectors");
    path.push("commissioning");
    path.push("cd");
    path.push("happy-path.json");

    let raw = std::fs::read_to_string(&path).expect("CD fixture file present");
    let f: CdFixture = serde_json::from_str(&raw).expect("CD fixture parses as JSON");
    base64::engine::general_purpose::STANDARD
        .decode(f.cd_b64.as_bytes())
        .expect("cd_b64 is valid base64")
}

// ── Attestation response builder ──────────────────────────────────────────────

/// Fixed timestamp used in `attestation_elements` (seconds since Matter epoch
/// `2000-01-01T00:00:00Z`).  Matches the `AT_UNIX` anchor in `mock_pki.rs`
/// (Unix time 1_800_000_000 ≈ 2027-01-15T08:00:00Z) converted to Matter epoch
/// by subtracting `MATTER_EPOCH_UNIX_SECS` = 946_684_800.
///
/// The `extract_attestation_elements_fields` verifier does not range-check the
/// timestamp, so any fixed value is acceptable.
const AT_MATTER_EPOCH_SECS: u64 = 1_800_000_000u64.saturating_sub(946_684_800);

/// Build a mock-device `AttestationResponse` for use in loopback tests.
///
/// Wire layout of `attestation_elements` (Matter Core Spec §6.2.4, anonymous
/// TLV structure):
/// - Context tag 1: `certification_declaration` (octet string — the CD bytes).
/// - Context tag 2: `attestation_nonce` (32-byte octet string).
/// - Context tag 3: `timestamp` (u64, Matter-epoch seconds).
///
/// The `signature` field is the DAC private key's ECDSA-P256-SHA256 (IEEE P1363
/// fixed-width, 64 bytes) over `attestation_elements || attestation_challenge`.
///
/// # Panics
///
/// Panics if signing fails (not expected for a valid `RingSigner`).
pub fn build_attestation_response(
    cd_bytes: &[u8],
    nonce: [u8; 32],
    challenge: [u8; 16],
    dac_signer: &RingSigner,
) -> AttestationResponse {
    use matter_codec::{Tag, TlvWriter};

    // Build attestation_elements TLV:
    //   { 1: cd_bytes, 2: nonce, 3: timestamp }
    let mut elements = Vec::new();
    {
        let mut w = TlvWriter::new(&mut elements);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(1), cd_bytes).unwrap();
        w.put_bytes(Tag::Context(2), &nonce).unwrap();
        w.put_uint(Tag::Context(3), AT_MATTER_EPOCH_SECS).unwrap();
        w.end_container().unwrap();
    }

    // Sign over elements || challenge.
    let mut tbs = Vec::with_capacity(elements.len() + challenge.len());
    tbs.extend_from_slice(&elements);
    tbs.extend_from_slice(&challenge);
    let signature = dac_signer
        .sign_p256_sha256(&tbs)
        .expect("DAC signing must not fail");

    AttestationResponse {
        attestation_elements: elements,
        signature,
    }
}

// ── CSR response builder ──────────────────────────────────────────────────────

/// Build a mock-device `CSRResponse` for use in loopback tests.
///
/// Wire layout of `nocsr_elements` (Matter Core Spec §11.18.5.6, anonymous
/// TLV structure):
/// - Context tag 1: `csr` (PKCS#10 DER, self-signed with a fresh operational key).
/// - Context tag 2: `csr_nonce` (32-byte octet string — echoes the commissioner's nonce).
///
/// The `attestation_signature` field is the DAC private key's ECDSA-P256-SHA256
/// (IEEE P1363 fixed-width, 64 bytes) over `nocsr_elements || attestation_challenge`.
///
/// The embedded PKCS#10 CSR is generated with `seed=0x42` (deterministic).
/// Task 10/11 only need the CSR's public key to issue a NOC; the exact
/// operational key does not matter for the loopback gate.
///
/// # Panics
///
/// Panics if signing fails (not expected for a valid `RingSigner`).
pub fn build_csr_response(
    csr_nonce: [u8; 32],
    challenge: [u8; 16],
    dac_signer: &RingSigner,
) -> CsrResponse {
    // Mint a synthetic PKCS#10 CSR (self-signed with seed 0x42).
    let (csr_der, _csr_pubkey) = mint_pkcs10_csr(0x42);

    // Build nocsr_elements TLV: { 1: csr_der, 2: csr_nonce }.
    let nocsr_elements = write_nocsr(&csr_der, &csr_nonce, None, None, None);

    // Sign over nocsr_elements || challenge.
    let mut tbs = Vec::with_capacity(nocsr_elements.len() + challenge.len());
    tbs.extend_from_slice(&nocsr_elements);
    tbs.extend_from_slice(&challenge);
    let attestation_signature = dac_signer
        .sign_p256_sha256(&tbs)
        .expect("DAC signing must not fail");

    CsrResponse {
        nocsr_elements,
        attestation_signature,
    }
}

// ── Task 9: device-side Interaction Model codec ───────────────────────────────
//
// The mock device's responder loop (Task 11) needs to:
//   1. Receive the controller's IM request bytes.
//   2. `parse_invoke_request` / `parse_read_request` — device decodes what
//      the controller sent.
//   3. Compute a response.
//   4. `build_invoke_response` / `build_report_data` — device encodes the reply.
//
// These four functions are the **exact inverse** of the controller-side `im`
// codec in `crates/matter-commissioning/src/im/`. Tag layouts are derived
// directly from `im/invoke.rs` and `im/read.rs` — see the cross-references in
// each function's doc comment.

// ── Private TLV helpers ───────────────────────────────────────────────────────
//
// The production `im` module exports `expect_message_struct`, `read_container_members`,
// `read_container_value`, and `skip_container` as `pub(crate)`, which is not
// accessible from integration tests. We re-implement minimal equivalents here.

/// Skip a container that has already been opened (reader positioned just
/// after the `ContainerStart`). Panics on malformed input.
fn tlv_skip_container(r: &mut matter_codec::TlvReader<'_>) {
    use matter_codec::Element;
    let mut depth = 1usize;
    loop {
        match r.next().expect("tlv_skip_container: unexpected EOF") {
            Some(Element::ContainerEnd) => {
                depth -= 1;
                if depth == 0 {
                    return;
                }
            }
            Some(Element::ContainerStart { .. }) => depth += 1,
            None => panic!("tlv_skip_container: stream ended before container closed"),
            Some(_) => {}
        }
    }
}

/// Collect all members of an already-opened container (reader positioned just
/// after the `ContainerStart`) as `(Tag, Value)` pairs. Panics on malformed input.
fn tlv_read_members(
    r: &mut matter_codec::TlvReader<'_>,
) -> Vec<(matter_codec::Tag, matter_codec::Value)> {
    use matter_codec::Element;
    let mut out = Vec::new();
    loop {
        match r.next().expect("tlv_read_members: unexpected EOF") {
            Some(Element::ContainerEnd) => return out,
            None => panic!("tlv_read_members: stream ended before container closed"),
            Some(Element::Scalar { tag, value }) => out.push((tag, value)),
            Some(Element::ContainerStart { tag, kind }) => {
                let v = tlv_read_container_value(r, kind);
                out.push((tag, v));
            }
            Some(_) => {}
        }
    }
}

/// Read the body of an already-opened container of `kind` into a [`matter_codec::Value`].
fn tlv_read_container_value(
    r: &mut matter_codec::TlvReader<'_>,
    kind: matter_codec::ContainerKind,
) -> matter_codec::Value {
    let members = tlv_read_members(r);
    match kind {
        matter_codec::ContainerKind::Structure => matter_codec::Value::Structure(members),
        matter_codec::ContainerKind::Array => {
            matter_codec::Value::Array(members.into_iter().map(|(_, v)| v).collect())
        }
        _ => matter_codec::Value::List(members),
    }
}

/// Parsed single-command `InvokeRequestMessage` (device perspective).
///
/// `fields_tlv` is the `CommandFields` struct re-encoded with an **anonymous**
/// tag, matching the convention used by the production `im::parse_invoke_response`.
pub struct InvokeRequestDecoded {
    /// Path of the incoming command.
    pub path: matter_commissioning::im::CommandPath,
    /// The command-fields struct, re-encoded with an anonymous tag.
    pub fields_tlv: Vec<u8>,
}

/// Parse a single-command `InvokeRequestMessage` produced by
/// [`matter_commissioning::im::build_invoke_request`] (device side).
///
/// Wire layout consumed (derived from `im/invoke.rs::build_invoke_request`):
/// ```text
/// anon struct {
///   ctx(0): bool  SuppressResponse
///   ctx(1): bool  TimedRequest
///   ctx(2): array InvokeRequests [
///     anon struct CommandDataIB {
///       ctx(0): list  CommandPathIB { ctx(0): endpoint, ctx(1): cluster, ctx(2): command }
///       ctx(1): struct CommandFields  (pre-encoded anonymous struct)
///     }
///   ]
///   ctx(0xFF): uint InteractionModelRevision
/// }
/// ```
///
/// Returns `(path, anonymous-tagged CommandFields bytes)`.
///
/// # Panics
///
/// Panics if `bytes` is not a valid `InvokeRequestMessage`. Acceptable in test
/// support code where the input is always controller-generated.
pub fn parse_invoke_request(bytes: &[u8]) -> InvokeRequestDecoded {
    use matter_codec::{ContainerKind, Element, Tag, TlvReader, Value};
    use matter_commissioning::im::CommandPath;

    let mut r = TlvReader::new(bytes);

    // Consume top-level anonymous struct start.
    match r.next().expect("InvokeRequest: first element") {
        Some(Element::ContainerStart {
            tag: Tag::Anonymous,
            kind: ContainerKind::Structure,
        }) => {}
        other => panic!("InvokeRequest: expected anon struct, got {other:?}"),
    }

    // Scan forward to ctx(2) = InvokeRequests array.
    loop {
        match r.next().expect("InvokeRequest: scan for InvokeRequests") {
            Some(Element::ContainerStart {
                tag: Tag::Context(2),
                kind: ContainerKind::Array,
            }) => break,
            Some(Element::ContainerEnd) | None => {
                panic!("InvokeRequest: missing InvokeRequests array")
            }
            Some(Element::ContainerStart { .. }) => {
                // Skip any unknown container (e.g. SuppressResponse is a scalar, but be safe).
                tlv_skip_container(&mut r);
            }
            Some(_) => {}
        }
    }

    // Expect the first CommandDataIB (anonymous struct).
    match r.next().expect("InvokeRequest: first CommandDataIB") {
        Some(Element::ContainerStart {
            kind: ContainerKind::Structure,
            ..
        }) => {}
        _ => panic!("InvokeRequest: missing CommandDataIB struct"),
    }

    // Parse CommandDataIB body: scan for CommandPathIB (ctx 0) and CommandFields (ctx 1).
    let mut path: Option<CommandPath> = None;
    let mut fields_tlv: Vec<u8> = Vec::new();
    loop {
        match r.next().expect("InvokeRequest: scan CommandDataIB members") {
            None => panic!("InvokeRequest: CommandDataIB body ended without end-of-container"),
            Some(Element::ContainerEnd) => break,
            // CommandPathIB list at context tag 0 (matches build_invoke_request → write_command_path).
            Some(Element::ContainerStart {
                tag: Tag::Context(0),
                kind: ContainerKind::List,
            }) => {
                let members = tlv_read_members(&mut r);
                let mut endpoint = None;
                let mut cluster = None;
                let mut command = None;
                for (tag, v) in members {
                    match (tag, v) {
                        (Tag::Context(0), Value::Uint(n)) => {
                            endpoint = Some(u16::try_from(n).expect("endpoint fits u16"));
                        }
                        (Tag::Context(1), Value::Uint(n)) => {
                            cluster = Some(u32::try_from(n).expect("cluster fits u32"));
                        }
                        (Tag::Context(2), Value::Uint(n)) => {
                            command = Some(u32::try_from(n).expect("command fits u32"));
                        }
                        _ => {}
                    }
                }
                path = Some(CommandPath {
                    endpoint: endpoint.expect("CommandPath.endpoint present"),
                    cluster: cluster.expect("CommandPath.cluster present"),
                    command: command.expect("CommandPath.command present"),
                });
            }
            // CommandFields struct at context tag 1 — re-encode as anonymous-tagged blob.
            Some(Element::ContainerStart {
                tag: Tag::Context(1),
                kind,
            }) => {
                let v = tlv_read_container_value(&mut r, kind);
                let mut buf = Vec::new();
                let mut w = matter_codec::TlvWriter::new(&mut buf);
                w.write_value(Tag::Anonymous, &v)
                    .expect("InvokeRequest: re-encode CommandFields");
                fields_tlv = buf;
            }
            Some(Element::ContainerStart { .. }) => tlv_skip_container(&mut r),
            Some(_) => {}
        }
    }

    // No CommandFields → canonicalize to an anonymous empty struct (matches
    // the same convention in `im/invoke.rs::parse_command_data`).
    if fields_tlv.is_empty() {
        let mut buf = Vec::new();
        let mut w = matter_codec::TlvWriter::new(&mut buf);
        w.write_value(Tag::Anonymous, &Value::Structure(Vec::new()))
            .expect("encode empty struct");
        fields_tlv = buf;
    }

    InvokeRequestDecoded {
        path: path.expect("CommandDataIB.CommandPath present"),
        fields_tlv,
    }
}

/// Build an `InvokeResponseMessage` carrying a single `CommandDataIB`.
///
/// Produces bytes that [`matter_commissioning::im::parse_invoke_response`]
/// decodes to `InvokeResponse::Command { path, fields_tlv }`.
///
/// Wire layout produced (derived from `im/invoke.rs::parse_invoke_response`
/// and the proven `build_canned_invoke_response` template in
/// `src/driver/commission.rs`):
/// ```text
/// anon struct {
///   ctx(0): bool  SuppressResponse  (false)
///   ctx(1): array InvokeResponses [
///     anon struct InvokeResponseIB {
///       ctx(0): struct CommandDataIB {
///         ctx(0): list  CommandPathIB { ctx(0): ep, ctx(1): cluster, ctx(2): cmd }
///         ctx(1): struct CommandFields  (re-tagged from anonymous via put_preencoded)
///       }
///     }
///   ]
///   ctx(0xFF): uint InteractionModelRevision
/// }
/// ```
///
/// `fields_tlv` must be an anonymous-tagged TLV struct (e.g. `[0x15, 0x18]`
/// for an empty struct). `put_preencoded` re-tags it to context tag 1.
///
/// # Panics
///
/// Panics if `fields_tlv` is not a valid anonymous-tagged TLV struct. Acceptable
/// in test support code where the input is always codec-generated.
pub fn build_invoke_response(
    path: matter_commissioning::im::CommandPath,
    fields_tlv: &[u8],
) -> Vec<u8> {
    use matter_codec::{Tag, TlvWriter};

    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous).unwrap(); // InvokeResponseMessage
    w.put_bool(Tag::Context(0), false).unwrap(); // SuppressResponse
    w.start_array(Tag::Context(1)).unwrap(); // InvokeResponses
    {
        w.start_structure(Tag::Anonymous).unwrap(); // InvokeResponseIB
        w.start_structure(Tag::Context(0)).unwrap(); // Command = CommandDataIB
                                                     // CommandPathIB list at ctx(0).
        w.start_list(Tag::Context(0)).unwrap();
        w.put_uint(Tag::Context(0), u64::from(path.endpoint))
            .unwrap();
        w.put_uint(Tag::Context(1), u64::from(path.cluster))
            .unwrap();
        w.put_uint(Tag::Context(2), u64::from(path.command))
            .unwrap();
        w.end_container().unwrap(); // CommandPathIB
                                    // CommandFields at ctx(1): splice the anonymous struct blob under context tag 1.
        w.put_preencoded(Tag::Context(1), fields_tlv).unwrap();
        w.end_container().unwrap(); // CommandDataIB
        w.end_container().unwrap(); // InvokeResponseIB
    }
    w.end_container().unwrap(); // InvokeResponses
    w.put_uint(
        Tag::Context(0xFF),
        u64::from(matter_commissioning::im::IM_REVISION),
    )
    .unwrap();
    w.end_container().unwrap(); // InvokeResponseMessage
    buf
}

/// Build an `InvokeResponseMessage` carrying a bare `CommandStatusIB` (status only,
/// no command data). Produces bytes that
/// [`matter_commissioning::im::parse_invoke_response`] decodes to
/// `InvokeResponse::Status(status)`.
///
/// Wire layout produced (derived from `im/invoke.rs::parse_command_status` and the
/// `parses_status_response` test in `im/invoke.rs`):
/// ```text
/// anon struct {
///   ctx(0): bool  SuppressResponse  (false)
///   ctx(1): array InvokeResponses [
///     anon struct InvokeResponseIB {
///       ctx(1): struct CommandStatusIB {           ← Status variant (not Command)
///         ctx(0): list  CommandPathIB { ctx(0): ep, ctx(1): cluster, ctx(2): cmd }
///         ctx(1): struct StatusIB { ctx(0): Status u8 }
///       }
///     }
///   ]
///   ctx(0xFF): uint InteractionModelRevision
/// }
/// ```
///
/// `status_code` is the raw `ImStatus` byte (0x00 = Success).
/// The `path` parameter is required by the `CommandStatusIB` wire format even for
/// success responses.
///
/// # Panics
///
/// Vec-backed `TlvWriter` is infallible; panics indicate a logic error.
pub fn build_invoke_status_response(
    path: matter_commissioning::im::CommandPath,
    status_code: u8,
) -> Vec<u8> {
    use matter_codec::{Tag, TlvWriter};

    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous).unwrap(); // InvokeResponseMessage
    w.put_bool(Tag::Context(0), false).unwrap(); // SuppressResponse
    w.start_array(Tag::Context(1)).unwrap(); // InvokeResponses
    {
        w.start_structure(Tag::Anonymous).unwrap(); // InvokeResponseIB
        w.start_structure(Tag::Context(1)).unwrap(); // Status = CommandStatusIB
                                                     // CommandPathIB list at ctx(0).
        w.start_list(Tag::Context(0)).unwrap();
        w.put_uint(Tag::Context(0), u64::from(path.endpoint))
            .unwrap();
        w.put_uint(Tag::Context(1), u64::from(path.cluster))
            .unwrap();
        w.put_uint(Tag::Context(2), u64::from(path.command))
            .unwrap();
        w.end_container().unwrap(); // CommandPathIB
                                    // StatusIB struct at ctx(1): { ctx(0): Status u8 }
        w.start_structure(Tag::Context(1)).unwrap(); // StatusIB
        w.put_uint(Tag::Context(0), u64::from(status_code)).unwrap(); // Status
        w.end_container().unwrap(); // StatusIB
        w.end_container().unwrap(); // CommandStatusIB
        w.end_container().unwrap(); // InvokeResponseIB
    }
    w.end_container().unwrap(); // InvokeResponses
    w.put_uint(
        Tag::Context(0xFF),
        u64::from(matter_commissioning::im::IM_REVISION),
    )
    .unwrap();
    w.end_container().unwrap(); // InvokeResponseMessage
    buf
}

/// Parse a `ReadRequestMessage` produced by
/// [`matter_commissioning::im::build_read_request`] (device side).
///
/// Wire layout consumed (derived from `im/read.rs::build_read_request`):
/// ```text
/// anon struct {
///   ctx(0): array AttributeRequests [
///     anon list AttributePathIB { ctx(2): endpoint, ctx(3): cluster, ctx(4): attribute }
///   ]
///   ctx(3): bool  FabricFiltered  (false)
///   ctx(0xFF): uint InteractionModelRevision
/// }
/// ```
///
/// Returns a `Vec<AttributePath>` with one entry per `AttributePathIB` in
/// the request.
///
/// # Panics
///
/// Panics if `bytes` is not a valid `ReadRequestMessage`. Acceptable in test
/// support code where the input is always controller-generated.
pub fn parse_read_request(bytes: &[u8]) -> Vec<matter_commissioning::im::AttributePath> {
    use matter_codec::{ContainerKind, Element, Tag, TlvReader, Value};
    use matter_commissioning::im::AttributePath;

    let mut r = TlvReader::new(bytes);

    // Consume top-level anonymous struct start.
    match r.next().expect("ReadRequest: first element") {
        Some(Element::ContainerStart {
            tag: Tag::Anonymous,
            kind: ContainerKind::Structure,
        }) => {}
        other => panic!("ReadRequest: expected anon struct, got {other:?}"),
    }

    // Scan forward to ctx(0) = AttributeRequests array.
    loop {
        match r.next().expect("ReadRequest: scan for AttributeRequests") {
            Some(Element::ContainerStart {
                tag: Tag::Context(0),
                kind: ContainerKind::Array,
            }) => break,
            Some(Element::ContainerEnd) | None => {
                panic!("ReadRequest: missing AttributeRequests array")
            }
            Some(Element::ContainerStart { .. }) => tlv_skip_container(&mut r),
            Some(_) => {}
        }
    }

    // Iterate over AttributePathIB list entries in the array.
    let mut paths = Vec::new();
    loop {
        match r.next().expect("ReadRequest: iterate AttributeRequests") {
            Some(Element::ContainerEnd) => break, // end of AttributeRequests array
            None => panic!("ReadRequest: AttributeRequests array not closed"),
            // Each entry is an anonymous list (AttributePathIB).
            Some(Element::ContainerStart {
                kind: ContainerKind::List,
                ..
            }) => {
                let members = tlv_read_members(&mut r);
                let mut endpoint = None;
                let mut cluster = None;
                let mut attribute = None;
                for (tag, v) in members {
                    match (tag, v) {
                        // ctx(2) = endpoint (matches build_read_request /
                        // the streaming attribute_path_from_reader parser)
                        (Tag::Context(2), Value::Uint(n)) => {
                            endpoint = Some(u16::try_from(n).expect("endpoint fits u16"));
                        }
                        // ctx(3) = cluster
                        (Tag::Context(3), Value::Uint(n)) => {
                            cluster = Some(u32::try_from(n).expect("cluster fits u32"));
                        }
                        // ctx(4) = attribute
                        (Tag::Context(4), Value::Uint(n)) => {
                            attribute = Some(u32::try_from(n).expect("attribute fits u32"));
                        }
                        _ => {}
                    }
                }
                paths.push(AttributePath {
                    endpoint: endpoint.expect("AttributePath.endpoint present"),
                    cluster: cluster.expect("AttributePath.cluster present"),
                    attribute: attribute.expect("AttributePath.attribute present"),
                });
            }
            Some(Element::ContainerStart { .. }) => tlv_skip_container(&mut r),
            Some(_) => {}
        }
    }

    paths
}

/// Build a `ReportDataMessage` carrying one or more `(AttributePath, value_tlv)` pairs.
///
/// Produces bytes that [`matter_commissioning::im::parse_report_data`] decodes
/// to `ReportData { attributes }`.
///
/// `value_tlv` for each entry must be an anonymous-tagged TLV element (scalar
/// or container). `put_preencoded` re-tags it to context tag 2 inside
/// `AttributeData`.
///
/// Wire layout produced (derived from `im/read.rs::parse_report_data` and the
/// proven `build_canned_report_data` template in `src/driver/commission.rs`):
/// ```text
/// anon struct {
///   ctx(1): array AttributeReports [
///     anon struct AttributeReportIB {
///       ctx(1): struct AttributeData {
///         ctx(1): list  AttributePathIB { ctx(2): ep, ctx(3): cluster, ctx(4): attr }
///         ctx(2): <value>   (pre-encoded anonymous element re-tagged to ctx(2))
///       }
///     }
///   ]
///   ctx(0xFF): uint InteractionModelRevision
/// }
/// ```
///
/// # Panics
///
/// Panics if any `value_tlv` entry is not a valid anonymous-tagged TLV element.
/// Acceptable in test support code where values are always codec-generated.
pub fn build_report_data(reports: &[(matter_commissioning::im::AttributePath, &[u8])]) -> Vec<u8> {
    use matter_codec::{Tag, TlvWriter};

    let mut buf = Vec::new();
    let mut w = TlvWriter::new(&mut buf);
    w.start_structure(Tag::Anonymous).unwrap(); // ReportDataMessage
    w.start_array(Tag::Context(1)).unwrap(); // AttributeReports
    for (path, value_tlv) in reports {
        w.start_structure(Tag::Anonymous).unwrap(); // AttributeReportIB
        w.start_structure(Tag::Context(1)).unwrap(); // AttributeData
                                                     // AttributePathIB list at ctx(1) inside AttributeData.
        w.start_list(Tag::Context(1)).unwrap();
        w.put_uint(Tag::Context(2), u64::from(path.endpoint))
            .unwrap();
        w.put_uint(Tag::Context(3), u64::from(path.cluster))
            .unwrap();
        w.put_uint(Tag::Context(4), u64::from(path.attribute))
            .unwrap();
        w.end_container().unwrap(); // AttributePathIB
                                    // Data at ctx(2): splice the anonymous TLV element under context tag 2.
        w.put_preencoded(Tag::Context(2), value_tlv).unwrap();
        w.end_container().unwrap(); // AttributeData
        w.end_container().unwrap(); // AttributeReportIB
    }
    w.end_container().unwrap(); // AttributeReports
    w.put_uint(
        Tag::Context(0xFF),
        u64::from(matter_commissioning::im::IM_REVISION),
    )
    .unwrap();
    w.end_container().unwrap(); // ReportDataMessage
    buf
}

// ── Task 10: mock-device per-stage response table (Ethernet path) ─────────────

/// Reply from the mock device for one IM round-trip.
///
/// `Command(fields_tlv)` carries the anonymous-tagged command-fields TLV blob
/// that Task 11's responder loop passes to [`build_invoke_response`]. The
/// blob is what the Commissioner's per-`Expectation` decoder in
/// `handle_response` reads (e.g. `decode_arm_fail_safe_response`,
/// `decode_noc_response`, etc.).
///
/// `Status(status_code)` signals that the device should reply with a bare
/// `CommandStatusIB` (no payload). Task 11 calls [`build_invoke_status_response`]
/// for this variant. `AddTrustedRootCertificate` (0x003E/0x0B) uses this because
/// the Commissioner's `Expectation::AddTrustedRootResponse` handler checks
/// `payload.first() == Some(&0u8)` — a single 0-byte "signal", not a TLV struct.
///
/// `StatusSignal(byte)` is reserved for the AddTrustedRoot fast path where
/// `on_response(Expectation::AddTrustedRootResponse, &[0x00])` is called
/// directly with a 1-byte sentinel rather than a full IM message. See Task 11
/// for the dispatch.
pub enum DeviceReply {
    /// Anonymous-tagged command-fields TLV. Wrap with [`build_invoke_response`].
    Command(Vec<u8>),
    /// Bare IM status code (0 = Success). Wrap with [`build_invoke_status_response`].
    Status(u8),
}

/// Map a single incoming `InvokeRequest` to the mock device's Ethernet-path reply.
///
/// The returned [`DeviceReply`] is what Task 11's responder loop uses to build
/// the outgoing `InvokeResponseMessage`.
///
/// # Cluster / command coverage (Ethernet happy path)
///
/// | Cluster | Command | Description                               | Reply variant        |
/// |---------|---------|-------------------------------------------|----------------------|
/// | 0x0030  | 0x00    | ArmFailSafe                               | `Command({0:0, 1:""})` |
/// | 0x0030  | 0x02    | SetRegulatoryConfig                       | `Command({0:0, 1:""})` |
/// | 0x0030  | 0x04    | CommissioningComplete                     | `Command({0:0, 1:""})` |
/// | 0x003E  | 0x00    | AttestationRequest                        | `Command({0:elements, 1:sig})` |
/// | 0x003E  | 0x02    | CertificateChainRequest (DAC=1 / PAI=2)   | `Command({0:cert_der})` |
/// | 0x003E  | 0x04    | CSRRequest                                | `Command({0:nocsr, 1:sig})` |
/// | 0x003E  | 0x06    | AddNOC                                    | `Command({0:0, 1:1})` |
/// | 0x003E  | 0x0B    | AddTrustedRootCertificate                 | `Status(0)` |
///
/// The `challenge` is the PASE attestation challenge (16 bytes). `pki` gives
/// access to `dac_signer`, `dac_der`, and `pai_der`.
///
/// # Panics
///
/// Panics on unrecognised `(cluster, command)` pairs — acceptable in test
/// support code where the input is always commissioner-generated.
// The function is long by necessity: one arm per commissioning command, each with
// its own nonce-parsing and TLV-building logic. Collapsing into sub-functions
// would obscure the cluster/command mapping. Suppress the too-many-lines lint.
#[allow(clippy::too_many_lines)]
// The three GeneralCommissioning response arms (ArmFailSafe / SetRegulatoryConfig /
// CommissioningComplete) all call ok_commissioning_response() and are intentionally
// identical — they are distinct commands that happen to share the same response shape.
#[allow(clippy::match_same_arms)]
pub fn respond(
    path: matter_commissioning::im::CommandPath,
    fields_tlv: &[u8],
    challenge: [u8; 16],
    pki: &MockDevicePki,
) -> DeviceReply {
    use matter_codec::{Tag, TlvWriter};

    // Helper: encode the shared {ctx(0): error_code=0, ctx(1): debug_text=""} shape
    // used by ArmFailSafeResponse, SetRegulatoryConfigResponse, and
    // CommissioningCompleteResponse. Matches decode_commissioning_error_response:
    //   ctx(0) = CommissioningErrorEnum (u8), ctx(1) = DebugText (utf8).
    let ok_commissioning_response = || -> Vec<u8> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap(); // anonymous struct (command-fields)
        w.put_uint(Tag::Context(0), 0_u64).unwrap(); // ctx(0): ErrorCode = 0 (OK)
        w.put_utf8(Tag::Context(1), "").unwrap(); // ctx(1): DebugText = ""
        w.end_container().unwrap();
        buf
    };

    match (path.cluster, path.command) {
        // ── GeneralCommissioning cluster (0x0030) ─────────────────────────────

        // ArmFailSafe (0x0030/0x00) → ArmFailSafeResponse (0x0030/0x01)
        // Commissioner decoder: decode_arm_fail_safe_response → decode_commissioning_error_response
        //   expects: anonymous struct { ctx(0): ErrorCode u8, ctx(1): DebugText utf8? }
        (0x0030, 0x00) => DeviceReply::Command(ok_commissioning_response()),

        // SetRegulatoryConfig (0x0030/0x02) → SetRegulatoryConfigResponse (0x0030/0x03)
        // Commissioner decoder: decode_set_regulatory_config_response → decode_commissioning_error_response
        //   expects: same {ctx(0): ErrorCode, ctx(1): DebugText} shape
        (0x0030, 0x02) => DeviceReply::Command(ok_commissioning_response()),

        // CommissioningComplete (0x0030/0x04) → CommissioningCompleteResponse (0x0030/0x05)
        // Commissioner decoder: decode_commissioning_error_response(Stage::SendComplete, ...)
        //   expects: same {ctx(0): ErrorCode, ctx(1): DebugText} shape
        (0x0030, 0x04) => DeviceReply::Command(ok_commissioning_response()),

        // ── OperationalCredentials cluster (0x003E) ───────────────────────────

        // AttestationRequest (0x003E/0x00) → AttestationResponse (0x003E/0x01)
        // Commissioner decoder: decode_attestation_response
        //   expects: anonymous struct { ctx(0): AttestationElements bytes, ctx(1): Signature bytes[64] }
        //
        // Parse the AttestationNonce from the incoming fields (ctx(0): 32-byte nonce).
        (0x003E, 0x00) => {
            use matter_codec::{Element, Tag as CTag, TlvReader, Value};
            // Parse the 32-byte AttestationNonce from request fields (ctx tag 0).
            let mut reader = TlvReader::new(fields_tlv);
            // Skip anonymous struct start.
            let _ = reader.next().expect("AttestationRequest: struct start");
            let mut att_nonce: Option<[u8; 32]> = None;
            loop {
                match reader.next().expect("AttestationRequest: field scan") {
                    Some(Element::ContainerEnd) | None => break,
                    Some(Element::Scalar {
                        tag: CTag::Context(0),
                        value: Value::Bytes(b),
                    }) => {
                        let arr: [u8; 32] = b.as_slice().try_into().expect("32-byte nonce");
                        att_nonce = Some(arr);
                    }
                    Some(_) => {}
                }
            }
            let nonce = att_nonce.expect("AttestationRequest: nonce at ctx(0)");
            let cd = load_cd_fixture();
            let att_resp = build_attestation_response(&cd, nonce, challenge, &pki.dac_signer);
            // Encode as command-fields: { ctx(0): elements bytes, ctx(1): signature bytes }
            // Matches decode_attestation_response field map exactly.
            let mut buf = Vec::new();
            {
                let mut w = TlvWriter::new(&mut buf);
                w.start_structure(Tag::Anonymous).unwrap();
                w.put_bytes(Tag::Context(0), &att_resp.attestation_elements)
                    .unwrap(); // ctx(0): AttestationElements
                w.put_bytes(Tag::Context(1), &att_resp.signature).unwrap(); // ctx(1): Signature (64 bytes)
                w.end_container().unwrap();
            }
            DeviceReply::Command(buf)
        }

        // CertificateChainRequest (0x003E/0x02) → CertificateChainResponse (0x003E/0x03)
        // Commissioner decoder: decode_certificate_chain_response
        //   expects: anonymous struct { ctx(0): Certificate bytes }
        //
        // Request carries ctx(0): CertificateChainTypeEnum — 0x01 = PAI, 0x02 = DAC.
        // Confirmed from CertChainType { Pai = 0x01, Dac = 0x02 } and from the wire
        // payloads: encode_certificate_chain_request(Pai) = [0x15, 0x24, 0x00, 0x01, 0x18]
        //                                               (Dac) = [0x15, 0x24, 0x00, 0x02, 0x18].
        (0x003E, 0x02) => {
            use matter_codec::{Element, Tag as CTag, TlvReader, Value};
            let mut reader = TlvReader::new(fields_tlv);
            let _ = reader.next().expect("CertChainRequest: struct start");
            let mut cert_type: Option<u8> = None;
            loop {
                match reader.next().expect("CertChainRequest: field scan") {
                    Some(Element::ContainerEnd) | None => break,
                    Some(Element::Scalar {
                        tag: CTag::Context(0),
                        value: Value::Uint(n),
                    }) => {
                        cert_type = Some(u8::try_from(n).expect("CertChainType fits u8"));
                    }
                    Some(_) => {}
                }
            }
            let cert_der = match cert_type.expect("CertificateChainRequest: type at ctx(0)") {
                // CertificateChainTypeEnum (spec §11.18.5.2): 1 = DAC, 2 = PAI.
                0x01 => pki.dac_der.clone(),
                0x02 => pki.pai_der.clone(),
                other => panic!(
                    "CertificateChainRequest: unknown CertChainType 0x{other:02X} (expected 0x01=DAC or 0x02=PAI)"
                ),
            };
            // Encode as { ctx(0): cert_der bytes }.
            let mut buf = Vec::new();
            {
                let mut w = TlvWriter::new(&mut buf);
                w.start_structure(Tag::Anonymous).unwrap();
                w.put_bytes(Tag::Context(0), &cert_der).unwrap(); // ctx(0): Certificate
                w.end_container().unwrap();
            }
            DeviceReply::Command(buf)
        }

        // CSRRequest (0x003E/0x04) → CSRResponse (0x003E/0x05)
        // Commissioner decoder: decode_csr_response
        //   expects: anonymous struct { ctx(0): NOCSRElements bytes, ctx(1): Signature bytes[64] }
        //
        // Parse the 32-byte CSRNonce from request fields (ctx tag 0).
        (0x003E, 0x04) => {
            use matter_codec::{Element, Tag as CTag, TlvReader, Value};
            let mut reader = TlvReader::new(fields_tlv);
            let _ = reader.next().expect("CSRRequest: struct start");
            let mut csr_nonce: Option<[u8; 32]> = None;
            loop {
                match reader.next().expect("CSRRequest: field scan") {
                    Some(Element::ContainerEnd) | None => break,
                    Some(Element::Scalar {
                        tag: CTag::Context(0),
                        value: Value::Bytes(b),
                    }) => {
                        let arr: [u8; 32] = b.as_slice().try_into().expect("32-byte CSRNonce");
                        csr_nonce = Some(arr);
                    }
                    Some(_) => {}
                }
            }
            let nonce = csr_nonce.expect("CSRRequest: nonce at ctx(0)");
            let csr_resp = build_csr_response(nonce, challenge, &pki.dac_signer);
            // Encode as { ctx(0): nocsr_elements bytes, ctx(1): attestation_signature bytes }.
            // Matches decode_csr_response field map exactly.
            let mut buf = Vec::new();
            {
                let mut w = TlvWriter::new(&mut buf);
                w.start_structure(Tag::Anonymous).unwrap();
                w.put_bytes(Tag::Context(0), &csr_resp.nocsr_elements)
                    .unwrap(); // ctx(0): NOCSRElements
                w.put_bytes(Tag::Context(1), &csr_resp.attestation_signature)
                    .unwrap(); // ctx(1): AttestationSignature (64 bytes)
                w.end_container().unwrap();
            }
            DeviceReply::Command(buf)
        }

        // AddNOC (0x003E/0x06) → NOCResponse (0x003E/0x08)
        // Commissioner decoder: decode_noc_response
        //   expects: anonymous struct { ctx(0): StatusCode u8, ctx(1): FabricIndex u8, ctx(2)?: DebugText utf8 }
        //   On success: status=0, fabric_index=1.
        (0x003E, 0x06) => {
            let mut buf = Vec::new();
            let mut w = TlvWriter::new(&mut buf);
            w.start_structure(Tag::Anonymous).unwrap();
            w.put_uint(Tag::Context(0), 0_u64).unwrap(); // ctx(0): StatusCode = 0 (OK)
            w.put_uint(Tag::Context(1), 1_u64).unwrap(); // ctx(1): FabricIndex = 1
            w.end_container().unwrap();
            DeviceReply::Command(buf)
        }

        // AddTrustedRootCertificate (0x003E/0x0B) → bare IM status (no response command)
        // Commissioner handler (Expectation::AddTrustedRootResponse):
        //   checks: payload.first() == Some(&0u8)  — the driver passes &[0x00] for success.
        //   Task 11 sees Status(0) and calls build_invoke_status_response + then signals
        //   on_response(AddTrustedRootResponse, &[0x00]).
        (0x003E, 0x0B) => DeviceReply::Status(0),

        // ── NetworkCommissioning cluster (0x0031) — Wi-Fi path (BLE test) ──────
        //
        // Additive arms exercised only by the dual-transport `commission_ble`
        // harness (the Ethernet loopback reports FeatureMap = ETHERNET and never
        // reaches these). Safe for the Ethernet path, which never invokes 0x0031.

        // AddOrUpdateWiFiNetwork (0x0031/0x02) → NetworkConfigResponse (0x0031/0x05)
        // Commissioner decoder: decode_network_config_response
        //   expects: anonymous struct { ctx(0): NetworkingStatus u8 } (0 = Success).
        (0x0031, 0x02) => {
            let mut buf = Vec::new();
            let mut w = TlvWriter::new(&mut buf);
            w.start_structure(Tag::Anonymous).unwrap();
            w.put_uint(Tag::Context(0), 0_u64).unwrap(); // ctx(0): NetworkingStatus = 0 (Success)
            w.end_container().unwrap();
            DeviceReply::Command(buf)
        }

        // AddOrUpdateThreadNetwork (0x0031/0x03) → NetworkConfigResponse (0x0031/0x05)
        // Commissioner decoder: decode_network_config_response
        //   expects: anonymous struct { ctx(0): NetworkingStatus u8 } (0 = Success).
        //
        // Additive Thread arm (M9-C2 Task 7). Only the Thread dual-transport
        // harness reaches this — the Wi-Fi path sends 0x02, the Ethernet path
        // never touches 0x0031 — so it cannot perturb the existing tests. The
        // NetworkConfigResponse shape is identical to the Wi-Fi 0x02 arm.
        (0x0031, 0x03) => {
            let mut buf = Vec::new();
            let mut w = TlvWriter::new(&mut buf);
            w.start_structure(Tag::Anonymous).unwrap();
            w.put_uint(Tag::Context(0), 0_u64).unwrap(); // ctx(0): NetworkingStatus = 0 (Success)
            w.end_container().unwrap();
            DeviceReply::Command(buf)
        }

        // ConnectNetwork (0x0031/0x06) → ConnectNetworkResponse (0x0031/0x07)
        // Commissioner decoder: decode_connect_network_response
        //   expects: anonymous struct { ctx(0): NetworkingStatus u8 } (0 = Success).
        //   Reused unchanged for both Wi-Fi (network_id = SSID) and Thread
        //   (network_id = Extended PAN ID) — the response shape is identical.
        (0x0031, 0x06) => {
            let mut buf = Vec::new();
            let mut w = TlvWriter::new(&mut buf);
            w.start_structure(Tag::Anonymous).unwrap();
            w.put_uint(Tag::Context(0), 0_u64).unwrap(); // ctx(0): NetworkingStatus = 0 (Success)
            w.end_container().unwrap();
            DeviceReply::Command(buf)
        }

        (c, cmd) => panic!(
            "respond: unrecognised (cluster=0x{c:04X}, command=0x{cmd:02X}) — not in the commissioning happy path"
        ),
    }
}

/// Wi-Fi-flavored read responder for the dual-transport `commission_ble`
/// harness: returns `FeatureMap = WIFI` for `NetworkCommissioning` (0x0031)
/// `0xFFFC` so the commissioner takes the Wi-Fi provisioning path; delegates
/// every other attribute to [`respond_read_attribute`]. Kept separate so the
/// shared Ethernet responder (and the loopback test that depends on it) is
/// untouched.
///
/// # Panics
///
/// Panics on unrecognised `(cluster, attribute)` pairs via the delegate.
pub fn respond_read_attribute_wifi(attr_path: matter_commissioning::im::AttributePath) -> Vec<u8> {
    use matter_codec::{Tag, TlvWriter};

    if attr_path.cluster == 0x0031 && attr_path.attribute == 0xFFFC {
        let wifi_feature: u32 =
            matter_commissioning::clusters::network_commissioning::NetworkCommissioningFeature::WIFI
                .bits();
        let mut buf = Vec::new();
        TlvWriter::new(&mut buf)
            .put_uint(Tag::Anonymous, u64::from(wifi_feature))
            .unwrap();
        buf
    } else {
        respond_read_attribute(attr_path)
    }
}

/// Thread-flavored read responder for the dual-transport `commission_ble`
/// harness: returns `FeatureMap = THREAD` for `NetworkCommissioning` (0x0031)
/// `0xFFFC` so the commissioner takes the Thread provisioning path
/// (`AddOrUpdateThreadNetwork` + `ConnectNetwork`); delegates every other
/// attribute — including `ConnectMaxTimeSeconds` (0x0003), which the base
/// responder answers with 30 s — to [`respond_read_attribute`]. Mirrors
/// [`respond_read_attribute_wifi`] so the shared Ethernet responder (and the
/// loopback test that depends on it) stays untouched.
///
/// # Panics
///
/// Panics on unrecognised `(cluster, attribute)` pairs via the delegate.
pub fn respond_read_attribute_thread(
    attr_path: matter_commissioning::im::AttributePath,
) -> Vec<u8> {
    use matter_codec::{Tag, TlvWriter};

    if attr_path.cluster == 0x0031 && attr_path.attribute == 0xFFFC {
        let thread_feature: u32 =
            matter_commissioning::clusters::network_commissioning::NetworkCommissioningFeature::THREAD
                .bits();
        let mut buf = Vec::new();
        TlvWriter::new(&mut buf)
            .put_uint(Tag::Anonymous, u64::from(thread_feature))
            .unwrap();
        buf
    } else {
        respond_read_attribute(attr_path)
    }
}

/// Map a single incoming `ReadRequest` attribute path to the mock device's
/// Ethernet-path attribute value TLV.
///
/// The returned `Vec<u8>` is an **anonymous-tagged** TLV element (scalar or
/// struct) that Task 11 passes to [`build_report_data`] as the `value_tlv`
/// entry.  The driver's `extract_read_payload` re-encodes the value into the
/// format `Commissioner::on_response` expects for each `Expectation`.
///
/// # Attribute coverage
///
/// | Cluster | Attr ID | Description                   | Value TLV                           |
/// |---------|---------|-------------------------------|-------------------------------------|
/// | 0x0030  | 0x0000  | Breadcrumb                    | anonymous uint 0                    |
/// | 0x0030  | 0x0001  | BasicCommissioningInfo        | anonymous struct {ctx(0):60, ctx(1):900} |
/// | 0x0030  | 0x0002  | RegulatoryConfig              | anonymous uint 2 (IndoorOutdoor)    |
/// | 0x0030  | 0x0004  | SupportsConcurrentConnection  | anonymous bool true                 |
/// | 0x0031  | 0xFFFC  | FeatureMap (NetworkComm.)     | anonymous uint 4 (ETHERNET only)   |
///
/// The `CommissioningInfo` struct (attr 0x0001) is what the driver scans for
/// and re-encodes via `write_value(Tag::Anonymous, struct_val)`. The anonymous
/// struct here carries `ctx(0)=failsafe_expiry_length_seconds=60` and
/// `ctx(1)=max_cumulative_failsafe_seconds=900`, matching `BasicCommissioningInfo`.
///
/// The `NetworkCommissioningInfo` value (`0x0031/0xFFFC`) is an anonymous uint
/// with **only** `NetworkCommissioningFeature::ETHERNET` set (bit 2, value 4).
/// This causes the Commissioner to skip the Wi-Fi path and go straight to
/// `Stage::EvictPreviousCaseSessions`.
///
/// # Panics
///
/// Panics on unrecognised `(cluster, attribute)` pairs — acceptable in test
/// support code where the input is always commissioner-generated.
pub fn respond_read_attribute(attr_path: matter_commissioning::im::AttributePath) -> Vec<u8> {
    use matter_codec::{Tag, TlvWriter};

    match (attr_path.cluster, attr_path.attribute) {
        // ── GeneralCommissioning cluster (0x0030) ─────────────────────────────
        // Attribute ids per spec §11.10.6, matching real-device reports
        // (Tapo P110M, M6.6.5 validation).

        // Breadcrumb (0x0030/0x0000): commissioning breadcrumb, starts at 0.
        (0x0030, 0x0000) => {
            let mut buf = Vec::new();
            TlvWriter::new(&mut buf)
                .put_uint(Tag::Anonymous, 0)
                .unwrap();
            buf
        }

        // BasicCommissioningInfo (0x0030/0x0001): the struct the driver scans for.
        // Wire shape after extract_read_payload re-encoding:
        //   anonymous struct { ctx(0): failsafe_expiry_length_seconds u16,
        //                      ctx(1): max_cumulative_failsafe_seconds u16 }
        // decode_basic_commissioning_info reads ctx(0) and ctx(1) from an anonymous struct.
        // The driver's extract_read_payload re-encodes the Value::Structure via
        //   w.write_value(Tag::Anonymous, struct_val).
        // So we emit an anonymous struct here that round-trips through the
        //   build_report_data → parse_report_data → extract_read_payload pipeline.
        (0x0030, 0x0001) => {
            let mut buf = Vec::new();
            let mut w = TlvWriter::new(&mut buf);
            w.start_structure(Tag::Anonymous).unwrap();
            w.put_uint(Tag::Context(0), 60_u64).unwrap(); // failsafe_expiry_length_seconds = 60 s
            w.put_uint(Tag::Context(1), 900_u64).unwrap(); // max_cumulative_failsafe_seconds = 900 s
            w.end_container().unwrap();
            buf
        }

        // RegulatoryConfig (0x0030/0x0002): current regulatory location.
        // Type: u8 enum RegulatoryLocationTypeEnum. 2 = IndoorOutdoor.
        (0x0030, 0x0002) => {
            let mut buf = Vec::new();
            TlvWriter::new(&mut buf)
                .put_uint(Tag::Anonymous, 2)
                .unwrap(); // IndoorOutdoor
            buf
        }

        // SupportsConcurrentConnection (0x0030/0x0004): whether the device
        // supports concurrent commissioning connections. true for Ethernet.
        (0x0030, 0x0004) => {
            let mut buf = Vec::new();
            TlvWriter::new(&mut buf)
                .put_bool(Tag::Anonymous, true)
                .unwrap();
            buf
        }

        // ── NetworkCommissioning cluster (0x0031) ─────────────────────────────

        // FeatureMap (0x0031/0xFFFC): which network interfaces the device exposes.
        // Wire shape after extract_read_payload re-encoding:
        //   anonymous uint (what decode_feature_map expects).
        // ETHERNET only = bit 2 = value 4. No WIFI (bit 0) or THREAD (bit 1) bits.
        // With only ETHERNET set, the Commissioner skips Stage::NetworkSetup.
        (0x0031, 0xFFFC) => {
            let ethernet_feature: u32 =
                matter_commissioning::clusters::network_commissioning::NetworkCommissioningFeature::ETHERNET
                    .bits();
            let mut buf = Vec::new();
            TlvWriter::new(&mut buf)
                .put_uint(Tag::Anonymous, u64::from(ethernet_feature))
                .unwrap();
            buf
        }

        // ConnectMaxTimeSeconds (0x0031/0x0003): read alongside FeatureMap at
        // Stage::ReadNetworkCommissioningInfo (D7). int8u seconds (spec
        // §11.9.5.4); sizes the FailsafeBeforeNetworkEnable extension.
        // Value is irrelevant on the Ethernet loopback (AlreadyOnNetwork
        // skips network setup), but the device must answer the requested
        // path — return a plausible 30 s.
        (0x0031, 0x0003) => {
            let mut buf = Vec::new();
            TlvWriter::new(&mut buf)
                .put_uint(Tag::Anonymous, 30)
                .unwrap();
            buf
        }

        (c, a) => {
            panic!("respond_read_attribute: unrecognised (cluster=0x{c:04X}, attribute=0x{a:04X})")
        }
    }
}

// ── Task 10: Step 2 tests ─────────────────────────────────────────────────────

#[cfg(test)]
mod mock_device_response_table {
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::items_after_statements
    )]

    use matter_cert::time::MatterTime;
    use matter_codec::{Tag, TlvWriter};
    use matter_commissioning::clusters::network_commissioning::NetworkCommissioningFeature;
    use matter_commissioning::im::{AttributePath, CommandPath};
    use matter_commissioning::noc::{
        decode_attestation_response, decode_certificate_chain_response,
    };
    use matter_commissioning::state_machine::Expectation;

    use super::{build_mock_device_pki, respond, respond_read_attribute, DeviceReply};

    fn now() -> MatterTime {
        MatterTime::from_unix_secs(1_800_000_000)
    }

    /// Helper: build a `CommissioningErrorEnum`-shaped request fields TLV (ArmFailSafe
    /// has ctx(0)=expiry, ctx(1)=breadcrumb — we build the minimal form and call respond).
    fn arm_fail_safe_fields() -> Vec<u8> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_uint(Tag::Context(0), 60_u64).unwrap(); // ExpiryLengthSeconds
        w.put_uint(Tag::Context(1), 1_u64).unwrap(); // Breadcrumb
        w.end_container().unwrap();
        buf
    }

    /// Helper: build a CertificateChainRequest fields TLV with the given type byte.
    fn cert_chain_request_fields(cert_type: u8) -> Vec<u8> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_uint(Tag::Context(0), u64::from(cert_type)).unwrap();
        w.end_container().unwrap();
        buf
    }

    /// Helper: build an AttestationRequest fields TLV with a fixed nonce.
    fn attestation_request_fields(nonce: [u8; 32]) -> Vec<u8> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(0), &nonce).unwrap();
        w.end_container().unwrap();
        buf
    }

    /// Helper: build a CSRRequest fields TLV with a fixed nonce.
    fn csr_request_fields(nonce: [u8; 32]) -> Vec<u8> {
        let mut buf = Vec::new();
        let mut w = TlvWriter::new(&mut buf);
        w.start_structure(Tag::Anonymous).unwrap();
        w.put_bytes(Tag::Context(0), &nonce).unwrap();
        w.end_container().unwrap();
        buf
    }

    // ── ArmFailSafe ───────────────────────────────────────────────────────────

    /// respond(0x0030/0x00) produces command-fields the Commissioner's
    /// `decode_arm_fail_safe_response` decodes as `error_code=0, debug_text=Some("")`.
    #[test]
    fn arm_fail_safe_returns_ok_response_accepted_by_commissioner_decoder() {
        let pki = build_mock_device_pki(now());
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x0030,
            command: 0x00,
        };
        let fields = arm_fail_safe_fields();
        let challenge = [0u8; 16];

        let reply = respond(path, &fields, challenge, &pki);
        let fields_tlv = match reply {
            DeviceReply::Command(v) => v,
            DeviceReply::Status(s) => panic!("expected Command, got Status({s})"),
        };

        // Feed the fields_tlv directly to the Commissioner's decoder.
        use matter_commissioning::clusters::general_commissioning::decode_arm_fail_safe_response;
        let decoded =
            decode_arm_fail_safe_response(&fields_tlv).expect("ArmFailSafeResponse decodes");
        assert_eq!(decoded.error_code, 0, "error_code must be 0 (OK)");
        // debug_text is allowed to be Some("") or None; both are success.
        let text = decoded.debug_text.as_deref().unwrap_or("");
        assert_eq!(text, "", "debug_text must be empty on success");
    }

    /// The same fields_tlv round-trips through the Commissioner's full
    /// `on_response(Expectation::ArmFailsafeResponse, …)` without error.
    #[test]
    fn arm_fail_safe_fields_accepted_by_on_response() {
        use std::sync::Arc;

        use matter_cert::time::MatterTime;
        use matter_commissioning::attestation::CdSigningRoots;
        use matter_commissioning::noc::{FabricRecord, NocRng, SystemNocRng};
        use matter_commissioning::setup::{
            CommissioningFlow, DiscoveryCapabilities, Discriminator, Passcode, SetupPayload,
        };
        use matter_commissioning::state_machine::CommissionerConfig;
        use matter_crypto::{RingSigner, Signer};

        let pki = build_mock_device_pki(now());

        // Build a minimal Commissioner (same pattern as commissioner.rs unit tests).
        let (signer, _) = RingSigner::generate().unwrap();
        let signer: Arc<dyn Signer> = Arc::new(signer);
        let fabric = FabricRecord::new_root_only(
            0x0000_0000_0000_0001,
            signer,
            MatterTime::from_unix_secs(1_704_067_200),
            MatterTime::from_unix_secs(1_735_689_600),
            42,
            &SystemNocRng,
        )
        .unwrap();

        let setup = SetupPayload {
            version: 0,
            vendor_id: Some(super::VID),
            product_id: Some(super::PID),
            commissioning_flow: CommissioningFlow::Standard,
            discovery_capabilities: DiscoveryCapabilities::ON_NETWORK,
            discriminator: Discriminator::new(0x0F00).unwrap(),
            passcode: Passcode::new(20_202_021).unwrap(),
        };
        let paa = pki.paa_trust_store.clone();
        let cd = CdSigningRoots::with_example_device_roots();
        let rng: Arc<dyn NocRng> = Arc::new(SystemNocRng);

        let mut sm = matter_commissioning::state_machine::Commissioner::new(CommissionerConfig {
            pase_attestation_challenge: [0u8; 16],
            fabric: &fabric,
            setup_payload: &setup,
            paa_trust_store: &paa,
            cd_signing_roots: &cd,
            commissioner_node_id: 0x1,
            assigned_node_id: 0x2,
            ipk_epoch_key: [0x42_u8; 16],
            case_admin_subject: 0x1,
            admin_vendor_id: 0xFFF1,
            now: MatterTime::from_unix_secs(1_800_000_000),
            rng,
            network: matter_commissioning::NetworkCredentials::AlreadyOnNetwork,
        })
        .unwrap();

        // Drive to ArmFailsafe: poll (ReadCommissioningInfo) → respond CommissioningInfo
        // → poll (ArmFailSafe) → feed our mock device's ArmFailSafeResponse.
        let _ = sm.poll().unwrap(); // ReadCommissioningInfo

        // Provide a minimal well-formed CommissioningInfo response.
        let commissioning_info_tlv = respond_read_attribute(AttributePath {
            endpoint: 0,
            cluster: 0x0030,
            attribute: 0x0001, // BasicCommissioningInfo
        });
        // The driver re-encodes the struct value; simulate by building a
        // well-formed anonymous struct directly (the state machine's CommissioningInfo
        // handler calls decode_basic_commissioning_info which is best-effort).
        sm.on_response(Expectation::CommissioningInfo, &commissioning_info_tlv)
            .expect("CommissioningInfo accepted");

        let _ = sm.poll().unwrap(); // ArmFailSafe

        // Now feed the mock device's ArmFailSafeResponse.
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x0030,
            command: 0x00,
        };
        let fields = arm_fail_safe_fields();
        let reply = respond(path, &fields, [0u8; 16], &pki);
        let fields_tlv = match reply {
            DeviceReply::Command(v) => v,
            DeviceReply::Status(s) => panic!("expected Command, got Status({s})"),
        };

        sm.on_response(Expectation::ArmFailsafeResponse, &fields_tlv)
            .expect("Commissioner accepts ArmFailSafeResponse from mock device");
        assert_eq!(
            sm.stage(),
            matter_commissioning::state_machine::Stage::ConfigRegulatory,
            "state machine advanced past ArmFailsafe"
        );
    }

    // ── AttestationRequest ────────────────────────────────────────────────────

    /// respond(0x003E/0x00) produces command-fields the Commissioner's
    /// `decode_attestation_response` decodes correctly (elements present, sig 64 bytes).
    #[test]
    fn attestation_request_returns_fields_accepted_by_noc_decoder() {
        let pki = build_mock_device_pki(now());
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E,
            command: 0x00,
        };
        let nonce = [0xAB_u8; 32];
        let challenge = [0x01_u8; 16];
        let fields = attestation_request_fields(nonce);

        let reply = respond(path, &fields, challenge, &pki);
        let fields_tlv = match reply {
            DeviceReply::Command(v) => v,
            DeviceReply::Status(s) => panic!("expected Command, got Status({s})"),
        };

        // Feed to the Commissioner's decoder (noc::decode_attestation_response).
        let decoded =
            decode_attestation_response(&fields_tlv).expect("AttestationResponse decodes");
        assert_eq!(
            decoded.signature.len(),
            64,
            "ECDSA signature must be 64 bytes (IEEE P1363)"
        );
        assert!(
            !decoded.attestation_elements.is_empty(),
            "attestation_elements must be non-empty"
        );
    }

    /// respond(0x003E/0x00) nonce echo: the emitted AttestationElements
    /// contain the nonce we sent (verified via extract_attestation_elements_fields).
    #[test]
    fn attestation_response_elements_echo_the_nonce() {
        use matter_commissioning::attestation::extract_attestation_elements_fields;

        let pki = build_mock_device_pki(now());
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E,
            command: 0x00,
        };
        let nonce = [0xCC_u8; 32];
        let challenge = [0x02_u8; 16];
        let fields = attestation_request_fields(nonce);

        let reply = respond(path, &fields, challenge, &pki);
        let DeviceReply::Command(fields_tlv) = reply else {
            panic!("expected Command");
        };

        let att_resp = decode_attestation_response(&fields_tlv).unwrap();
        let att_fields = extract_attestation_elements_fields(&att_resp.attestation_elements)
            .expect("elements parse");
        assert_eq!(
            att_fields.attestation_nonce, nonce,
            "nonce must be echoed back"
        );
    }

    // ── CertificateChainRequest ────────────────────────────────────────────────

    /// respond(0x003E/0x02) with type=PAI (0x01) returns pai_der.
    #[test]
    fn cert_chain_request_pai_returns_pai_der() {
        let pki = build_mock_device_pki(now());
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E,
            command: 0x02,
        };
        let fields = cert_chain_request_fields(0x02); // PAI (spec §11.18.5.2: 2 = PAI)
        let reply = respond(path, &fields, [0u8; 16], &pki);
        let fields_tlv = match reply {
            DeviceReply::Command(v) => v,
            DeviceReply::Status(s) => panic!("expected Command, got Status({s})"),
        };

        let decoded =
            decode_certificate_chain_response(&fields_tlv).expect("CertChainResponse decodes");
        assert_eq!(decoded.certificate, pki.pai_der, "PAI DER mismatch");
    }

    /// respond(0x003E/0x02) with type=DAC (0x01) returns dac_der.
    #[test]
    fn cert_chain_request_dac_returns_dac_der() {
        let pki = build_mock_device_pki(now());
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E,
            command: 0x02,
        };
        let fields = cert_chain_request_fields(0x01); // DAC (spec §11.18.5.2: 1 = DAC)
        let reply = respond(path, &fields, [0u8; 16], &pki);
        let fields_tlv = match reply {
            DeviceReply::Command(v) => v,
            DeviceReply::Status(s) => panic!("expected Command, got Status({s})"),
        };

        let decoded =
            decode_certificate_chain_response(&fields_tlv).expect("CertChainResponse decodes");
        assert_eq!(decoded.certificate, pki.dac_der, "DAC DER mismatch");
    }

    // ── AddTrustedRoot ─────────────────────────────────────────────────────────

    /// respond(0x003E/0x0B) emits Status(0) so Task 11 uses build_invoke_status_response.
    #[test]
    fn add_trusted_root_emits_status_zero() {
        let pki = build_mock_device_pki(now());
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E,
            command: 0x0B,
        };
        let fields = vec![0x15u8, 0x18]; // empty anonymous struct
        let reply = respond(path, &fields, [0u8; 16], &pki);
        match reply {
            DeviceReply::Status(0) => {}
            DeviceReply::Status(s) => panic!("expected Status(0), got Status({s})"),
            DeviceReply::Command(_) => panic!("expected Status, got Command"),
        }
    }

    // ── NetworkCommissioning FeatureMap read ──────────────────────────────────

    /// respond_read_attribute(0x0031/0xFFFC) returns ETHERNET-only FeatureMap (value=4).
    /// This causes the Commissioner to skip the Wi-Fi path (Stage::EvictPreviousCaseSessions).
    #[test]
    fn feature_map_is_ethernet_only() {
        use matter_commissioning::clusters::network_commissioning::decode_feature_map;

        let value_tlv = respond_read_attribute(AttributePath {
            endpoint: 0,
            cluster: 0x0031,
            attribute: 0xFFFC,
        });
        let features = decode_feature_map(&value_tlv).expect("FeatureMap decodes");
        assert!(
            features.contains(NetworkCommissioningFeature::ETHERNET),
            "ETHERNET bit must be set"
        );
        assert!(
            !features.contains(NetworkCommissioningFeature::WIFI),
            "WIFI bit must NOT be set (Ethernet path)"
        );
        assert!(
            !features.contains(NetworkCommissioningFeature::THREAD),
            "THREAD bit must NOT be set (Ethernet path)"
        );
    }

    // ── BasicCommissioningInfo read ────────────────────────────────────────────

    /// respond_read_attribute(0x0030/0x0001) returns an anonymous struct that
    /// decode_basic_commissioning_info parses as failsafe=60, max_cumulative=900.
    #[test]
    fn basic_commissioning_info_decodes_correctly() {
        use matter_commissioning::clusters::general_commissioning::decode_basic_commissioning_info;

        let value_tlv = respond_read_attribute(AttributePath {
            endpoint: 0,
            cluster: 0x0030,
            attribute: 0x0001, // BasicCommissioningInfo
        });
        let info =
            decode_basic_commissioning_info(&value_tlv).expect("BasicCommissioningInfo decodes");
        assert_eq!(
            info.failsafe_expiry_length_seconds, 60,
            "failsafe_expiry_length_seconds must be 60"
        );
        assert_eq!(
            info.max_cumulative_failsafe_seconds, 900,
            "max_cumulative_failsafe_seconds must be 900"
        );
    }

    // ── CSRRequest ────────────────────────────────────────────────────────────

    /// respond(0x003E/0x04) produces command-fields the Commissioner's
    /// `decode_csr_response` decodes correctly (nocsr_elements present, sig 64 bytes).
    #[test]
    fn csr_request_returns_fields_accepted_by_noc_decoder() {
        use matter_commissioning::noc::decode_csr_response;

        let pki = build_mock_device_pki(now());
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E,
            command: 0x04,
        };
        let nonce = [0xDD_u8; 32];
        let fields = csr_request_fields(nonce);
        let reply = respond(path, &fields, [0u8; 16], &pki);
        let fields_tlv = match reply {
            DeviceReply::Command(v) => v,
            DeviceReply::Status(s) => panic!("expected Command, got Status({s})"),
        };

        let decoded = decode_csr_response(&fields_tlv).expect("CSRResponse decodes");
        assert_eq!(decoded.attestation_signature.len(), 64);
        assert!(!decoded.nocsr_elements.is_empty());
    }

    // ── NOCResponse ───────────────────────────────────────────────────────────

    /// respond(0x003E/0x06) produces fields decode_noc_response parses as status=0, fabric_index=1.
    #[test]
    fn add_noc_returns_success_noc_response() {
        use matter_commissioning::noc::decode_noc_response;

        let pki = build_mock_device_pki(now());
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E,
            command: 0x06,
        };
        let fields = vec![0x15u8, 0x18]; // AddNOC payload is opaque to mock device
        let reply = respond(path, &fields, [0u8; 16], &pki);
        let fields_tlv = match reply {
            DeviceReply::Command(v) => v,
            DeviceReply::Status(s) => panic!("expected Command, got Status({s})"),
        };

        let decoded = decode_noc_response(&fields_tlv).expect("NOCResponse decodes");
        assert_eq!(decoded.status, 0, "status must be 0 (OK)");
        assert_eq!(decoded.fabric_index, Some(1), "fabric_index must be 1");
    }
}

// ── Task 9: roundtrip tests ───────────────────────────────────────────────────

#[cfg(test)]
mod device_im_roundtrip {
    #![allow(clippy::unwrap_used, clippy::expect_used)]

    use matter_codec::{Tag, TlvWriter, Value};
    use matter_commissioning::im::{
        build_invoke_request, build_read_request, parse_invoke_response, parse_report_data,
        AttributePath, CommandPath, InvokeResponse,
    };

    use super::{
        build_invoke_response, build_invoke_status_response, build_report_data,
        parse_invoke_request, parse_read_request,
    };

    /// (a) Controller builds InvokeRequest → device parses it back.
    #[test]
    fn parse_invoke_request_is_inverse_of_build_invoke_request() {
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x0030,
            command: 0x00,
        };
        // ArmFailSafe fields: { ctx(0): 60u16, ctx(1): 1u64 }
        let mut fields_buf = Vec::new();
        {
            let mut w = TlvWriter::new(&mut fields_buf);
            w.start_structure(Tag::Anonymous).unwrap();
            w.put_uint(Tag::Context(0), 60).unwrap(); // ExpiryLengthSeconds
            w.put_uint(Tag::Context(1), 1).unwrap(); // Breadcrumb
            w.end_container().unwrap();
        }

        let request_bytes = build_invoke_request(path, &fields_buf);
        let decoded = parse_invoke_request(&request_bytes);

        assert_eq!(decoded.path, path);
        assert_eq!(
            decoded.fields_tlv, fields_buf,
            "fields_tlv mismatch: got {:02X?}, expected {fields_buf:02X?}",
            decoded.fields_tlv
        );
    }

    /// (b) Device builds InvokeResponse → controller's im::parse_invoke_response recovers it.
    #[test]
    fn parse_invoke_response_accepts_build_invoke_response() {
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x0030,
            command: 0x01, // ArmFailSafeResponse
        };
        // ArmFailSafeResponse fields: { ctx(0): 0u8 (errorCode = OK) }
        let mut fields_buf = Vec::new();
        {
            let mut w = TlvWriter::new(&mut fields_buf);
            w.start_structure(Tag::Anonymous).unwrap();
            w.put_uint(Tag::Context(0), 0).unwrap(); // errorCode = OK
            w.end_container().unwrap();
        }

        let response_bytes = build_invoke_response(path, &fields_buf);
        let parsed = parse_invoke_response(&response_bytes).unwrap();

        match parsed {
            InvokeResponse::Command {
                path: p,
                fields_tlv,
            } => {
                assert_eq!(p, path);
                assert_eq!(
                    fields_tlv, fields_buf,
                    "fields_tlv mismatch: got {fields_tlv:02X?}, expected {fields_buf:02X?}",
                );
            }
            InvokeResponse::Status(s) => panic!("expected Command, got Status({s:?})"),
        }
    }

    /// (b-status) Device builds InvokeStatusResponse → controller's
    /// im::parse_invoke_response decodes it as InvokeResponse::Status.
    #[test]
    fn parse_invoke_response_accepts_build_invoke_status_response() {
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E, // OperationalCredentials
            command: 0x0B,   // AddTrustedRootCertificate (no command response, only status)
        };

        let response_bytes = build_invoke_status_response(path, 0x00); // 0x00 = Success
        let parsed = parse_invoke_response(&response_bytes).unwrap();

        assert!(
            matches!(parsed, InvokeResponse::Status(_)),
            "expected Status, got {parsed:?}"
        );
        if let InvokeResponse::Status(status) = parsed {
            // ImStatus::Success maps from 0x00; confirm it does not carry a failure code.
            let status_name = format!("{status:?}");
            assert!(
                status_name.contains("Success") || status_name.contains('0'),
                "expected Success status, got {status_name}"
            );
        }
    }

    /// (c) Controller builds ReadRequest → device parses it back.
    #[test]
    fn parse_read_request_is_inverse_of_build_read_request() {
        let paths = vec![
            AttributePath {
                endpoint: 0,
                cluster: 0x0030,
                attribute: 0x0001, // BasicCommissioningInfo
            },
            AttributePath {
                endpoint: 0,
                cluster: 0x0031,
                attribute: 0xFFFC, // FeatureMap
            },
        ];

        let request_bytes = build_read_request(&paths);
        let decoded = parse_read_request(&request_bytes);

        assert_eq!(decoded.len(), paths.len());
        assert_eq!(decoded, paths);
    }

    /// (d) Device builds ReportData → controller's im::parse_report_data recovers it.
    #[test]
    fn parse_report_data_accepts_build_report_data() {
        let path1 = AttributePath {
            endpoint: 0,
            cluster: 0x0030,
            attribute: 0x0004,
        };
        // Value: a u64 scalar (FeatureMap style).
        let mut val1_buf = Vec::new();
        {
            let mut w = TlvWriter::new(&mut val1_buf);
            w.put_uint(Tag::Anonymous, 0x01).unwrap();
        }

        let path2 = AttributePath {
            endpoint: 0,
            cluster: 0x0031,
            attribute: 0xFFFC,
        };
        // Value: a u64 scalar = 3 (WIFI | THREAD).
        let mut val2_buf = Vec::new();
        {
            let mut w = TlvWriter::new(&mut val2_buf);
            w.put_uint(Tag::Anonymous, 3).unwrap();
        }

        let report_bytes =
            build_report_data(&[(path1, val1_buf.as_slice()), (path2, val2_buf.as_slice())]);
        let parsed = parse_report_data(&report_bytes).unwrap();

        let attrs: Vec<_> = parsed.attributes().collect();
        assert_eq!(attrs.len(), 2);

        let (p0, v0) = attrs[0];
        assert_eq!(*p0, path1);
        assert_eq!(*v0, Value::Uint(0x01));

        let (p1, v1) = attrs[1];
        assert_eq!(*p1, path2);
        assert_eq!(*v1, Value::Uint(3));
    }

    /// Edge case: roundtrip with an empty anonymous struct as CommandFields.
    #[test]
    fn roundtrip_empty_command_fields() {
        let path = CommandPath {
            endpoint: 0,
            cluster: 0x003E,
            command: 0x0B, // AddTrustedRootCertificate (fields is just an empty struct)
        };
        // Empty anonymous struct: [0x15, 0x18]
        let empty_fields = vec![0x15u8, 0x18];

        // (a) build→parse direction.
        let req = build_invoke_request(path, &empty_fields);
        let decoded = parse_invoke_request(&req);
        assert_eq!(decoded.path, path);
        assert_eq!(decoded.fields_tlv, empty_fields);

        // (b) build-response→parse direction.
        let resp = build_invoke_response(path, &empty_fields);
        match parse_invoke_response(&resp).unwrap() {
            InvokeResponse::Command {
                path: p,
                fields_tlv,
            } => {
                assert_eq!(p, path);
                assert_eq!(fields_tlv, empty_fields);
            }
            InvokeResponse::Status(s) => panic!("expected Command, got {s:?}"),
        }
    }
}

// ── Task 11: mock-device responder loop (PASE → secured IM → CASE → IM) ───────
//
// `run_mock_device` is the device twin of the controller's `commission()`
// (src/driver/commission.rs). Where `commission()` runs the *initiator* side of
// PASE, drives the IM command/read loop over the PASE session, then switches to
// the CASE *initiator* and finishes commissioning, `run_mock_device` runs the
// matching *responder* side: PASE `PaseVerifier`, the IM `respond`/`build_*`
// reply path, then the CASE `CaseResponder`, then resumes the IM loop on the
// CASE session until `CommissioningComplete`.
//
// Both halves run under one `tokio::join!` in Task 12's loopback gate, over a
// single `InMemoryDatagram` pair. The device listens on its end for every
// phase; `commission()` reuses the same transport for PASE, the secured IM
// round-trips, and CASE (operational discovery is stubbed so the resolved
// operational address is the same loopback endpoint).
//
// Wire/opcode map (mirrors the M6.6.3b loopback device tasks and the Task 3/4/5
// device-reply tasks in commission.rs):
//   - PASE (unsecured, SecureChannel): in 0x20 PBKDFParamRequest / 0x22 Pake1 /
//     0x24 Pake3; out 0x21 PBKDFParamResponse / 0x23 Pake2.
//   - Secured IM (INTERACTION_MODEL): in 0x08 InvokeRequest / 0x02 ReadRequest;
//     out 0x09 InvokeResponse (Command or bare Status) / 0x05 ReportData.
//   - CASE (unsecured, SecureChannel): in 0x30 Sigma1 / 0x32 Sigma3;
//     out 0x31 Sigma2.

/// Device-side handshake opcodes (Matter Core Spec §4.14.1). Inbound opcodes
/// the controller sends; outbound opcodes the device replies with.
mod op {
    // SecureChannel — PASE.
    pub const PBKDF_PARAM_REQUEST: u8 = 0x20;
    pub const PBKDF_PARAM_RESPONSE: u8 = 0x21;
    pub const PASE_PAKE1: u8 = 0x22;
    pub const PASE_PAKE2: u8 = 0x23;
    pub const PASE_PAKE3: u8 = 0x24;
    // SecureChannel — CASE.
    pub const CASE_SIGMA1: u8 = 0x30;
    pub const CASE_SIGMA2: u8 = 0x31;
    pub const CASE_SIGMA3: u8 = 0x32;
    // SecureChannel — MRP / status.
    pub const MRP_STANDALONE_ACK: u8 = 0x10;
    pub const STATUS_REPORT: u8 = 0x40;
    // Interaction Model.
    pub const IM_INVOKE_REQUEST: u8 = 0x08;
    pub const IM_INVOKE_RESPONSE: u8 = 0x09;
    pub const IM_READ_REQUEST: u8 = 0x02;
    pub const IM_REPORT_DATA: u8 = 0x05;
}

/// GeneralCommissioning `CommissioningComplete` command id (cluster 0x0030,
/// command 0x04). When the device services this on the CASE session, the
/// commissioning flow is done and the loop returns.
const CMD_COMMISSIONING_COMPLETE: u32 = 0x04;

/// Close a PASE/CASE handshake the way a real device does: send a reliable
/// success `StatusReport` on the exchange, then consume the controller's MRP
/// standalone ack for it (observed: Tapo P110M, M6.6.5 validation).
async fn close_handshake_with_status_report<T: AsyncDatagram>(
    dev_io: &T,
    peer: std::net::SocketAddr,
    counter: u32,
    exchange_id: u16,
    ack_counter: u32,
) -> Result<(), DriverError> {
    let mut body = Vec::with_capacity(8);
    body.extend_from_slice(&0u16.to_le_bytes()); // general code: SUCCESS
    body.extend_from_slice(&0u32.to_le_bytes()); // protocol id: SecureChannel
    body.extend_from_slice(&0u16.to_le_bytes()); // SessionEstablishmentSuccess
    let report = encode_unsecured(
        counter,
        exchange_id,
        op::STATUS_REPORT,
        ProtocolId::SECURE_CHANNEL,
        false,
        true,
        Some(ack_counter),
        None,
        &body,
    );
    dev_io.send_to(&report, peer).await?;
    let (p, _) = dev_io.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::MRP_STANDALONE_ACK);
    debug_assert_eq!(m.ack_counter, Some(counter));
    Ok(())
}

/// Inputs the mock device needs to play the device side of one commissioning run.
///
/// Built by Task 12 from the *same* fabric the controller commissions under, so
/// the device's CASE `CaseResponder` validates against the controller's
/// `TrustedRoots` and vice versa (the device's NOC and the controller's NOC are
/// both issued under the fabric RCAC). See `commission()`'s "CASE credential
/// sourcing" note: the controller mints its own operational NOC under the
/// fabric RCAC; Task 12 mints the *device's* operational NOC under the same
/// RCAC and hands the resulting [`CaseCredentials`] + [`TrustedRoots`] here.
pub struct MockDeviceCaseSetup {
    /// The device's operational identity for the CASE responder — a NOC issued
    /// under the fabric RCAC for the device's assigned node id, on the
    /// commissioner's fabric id, carrying the fabric IPK.
    pub credentials: CaseCredentials,
    /// Trusted roots (the fabric RCAC) the responder validates the controller's
    /// NOC against.
    pub trusted_roots: TrustedRoots,
    /// The non-zero secured-session id the device advertises in Sigma2 for the
    /// controller to address the operational session by. Mirrors the `0x00D2`
    /// the `run_case` loopback uses.
    pub responder_session_id: u16,
    /// Wall-clock instant the responder validates the controller's NOC chain
    /// at. Must fall within the controller NOC's validity window (the
    /// commissioner mints it with `not_before = config.now`), so callers set
    /// this to the same `now` the commissioner uses.
    pub now: MatterTime,
}

/// Run the full device side of one commissioning run against `commission()`.
///
/// Sequence (each step is the responder counterpart of the matching
/// `commission()` step):
///
/// 1. **PASE.** Drive a [`PaseVerifier`] over the unsecured (session-id 0)
///    `SecureChannel` path: recv PBKDFParamRequest → send PBKDFParamResponse →
///    recv Pake1 → send Pake2 → recv Pake3 → `finish()`. Register the derived
///    keys via [`SessionManager::register_pase`] as
///    `SessionRole::Responder`. The PASE-derived `attestation_key` (see
///    [`matter_crypto::pase::PaseSessionKeys`]) is the 16-byte attestation
///    challenge: both sides derive it from the same SPAKE2+ secret, so it
///    equals the controller's `CommissionerConfig::pase_attestation_challenge`.
///    It is fed straight into [`respond`].
/// 2. **Secured IM loop (PASE session).** Loop: receive a secured packet,
///    `decode_inbound`, then branch on the decrypted payload shape — an
///    InvokeRequest goes to [`parse_invoke_request`] + [`respond`]; a
///    ReadRequest goes to [`parse_read_request`] + [`respond_read_attribute`].
///    Build the reply via [`build_invoke_response`] /
///    [`build_invoke_status_response`] / [`build_report_data`], then
///    `encode_outbound` on the SAME exchange id with the IM reply opcode. The
///    loop exits this phase when an *unsecured* (session-id 0) packet arrives —
///    that is the controller's CASE Sigma1, signalling the PASE→CASE transition.
/// 3. **CASE.** Drive a [`CaseResponder`] over the unsecured path: handle
///    Sigma1 (the already-received packet) → send Sigma2 → recv Sigma3 →
///    `finish()`. Register via [`SessionManager::register_case`] as
///    [`SessionRole::Responder`].
/// 4. **Secured IM loop (CASE session).** Same IM loop, now decoding on the
///    CASE session, until the device services `CommissioningComplete`
///    (GeneralCommissioning 0x0030 / 0x04). After replying to that, return
///    `Ok(())` so the `tokio::join!` in Task 12 completes.
///
/// `peer` is the address replies are sent to; with [`InMemoryDatagram`] the
/// peer arg is ignored (the channel is point-to-point) and the controller's
/// source address is also learned from the first inbound packet — `peer` is
/// kept for signature symmetry with the controller helpers.
///
/// # PASE→CASE transition detection
///
/// `decode_inbound` only handles *secured* (non-zero session-id) packets. CASE
/// Sigma1 arrives *unsecured* (session-id 0). The loop therefore peeks the
/// 2-byte little-endian session id at offset 1 of the secured message header
/// (`framing::encode_header` layout): id 0 → unsecured CASE Sigma1 (transition);
/// id != 0 → secured IM message routed through `decode_inbound`.
///
/// # Errors
///
/// - [`DriverError::Io`] if a datagram recv/send fails or the channel closes.
/// - [`DriverError::Crypto`] / [`DriverError::Transport`] if a PASE/CASE step or
///   secured framing fails.
///
/// # Panics
///
/// Panics (via the test-support `respond`/`build_*` helpers and `.unwrap()`) on
/// any malformed controller message or unrecognised command — acceptable in
/// test support code where the input is always `commission()`-generated.
// One async block covering PASE + two IM loops + CASE; splitting would scatter
// the shared SessionManager/transport state. The nested `service_secured_im`
// helper is defined after the PASE statements so it closes over nothing and
// stays next to its only call sites.
#[allow(clippy::too_many_lines, clippy::items_after_statements)]
pub async fn run_mock_device(
    dev_io: &InMemoryDatagram,
    peer: std::net::SocketAddr,
    pki: &MockDevicePki,
    pase_pin: u32,
    pase_params: PasePbkdfParams,
    pase_responder_session_id: u16,
    case_setup: MockDeviceCaseSetup,
) -> Result<(), DriverError> {
    use std::time::Instant;

    // The controller registers PASE first against a fresh SessionManager whose
    // allocator starts at 1, so its local (initiator) session id is 1. The
    // device registers its PASE/CASE sessions with that as `peer_session_id` so
    // the controller's secured replies demux. (PASE's PBKDFParamRequest carries
    // the controller's `initiator_session_id`, but `messages` is pub(crate) and
    // not reachable from the integration-test crate; the loopback's
    // deterministic allocation makes 1 correct — mirrors `paired_pase_sessions`
    // in exchange.rs/commission.rs.)
    const CTRL_PASE_SESSION_ID: u16 = 1;

    let mut sessions = SessionManager::new();

    // ── 1. PASE (device = verifier, unsecured path) ──────────────────────────
    let mut verifier = PaseVerifier::new_from_pin(pase_pin, pase_params, pase_responder_session_id)
        .map_err(DriverError::Crypto)?;
    let mut unsecured_ctr: u32 = 100;

    // PBKDFParamRequest → PBKDFParamResponse.
    let (p, _) = dev_io.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PBKDF_PARAM_REQUEST);
    verifier
        .handle_pbkdf_request(&m.payload)
        .map_err(DriverError::Crypto)?;
    let resp = verifier.next_message().map_err(DriverError::Crypto)?;
    let wire = encode_unsecured(
        unsecured_ctr,
        m.exchange_id,
        op::PBKDF_PARAM_RESPONSE,
        ProtocolId::SECURE_CHANNEL,
        false,
        true,
        Some(m.message_counter),
        None,
        &resp,
    );
    unsecured_ctr += 1;
    dev_io.send_to(&wire, peer).await?;

    // Pake1 → Pake2.
    let (p, _) = dev_io.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PASE_PAKE1);
    verifier
        .handle_pake1(&m.payload)
        .map_err(DriverError::Crypto)?;
    let pake2 = verifier.next_message().map_err(DriverError::Crypto)?;
    let wire = encode_unsecured(
        unsecured_ctr,
        m.exchange_id,
        op::PASE_PAKE2,
        ProtocolId::SECURE_CHANNEL,
        false,
        true,
        Some(m.message_counter),
        None,
        &pake2,
    );
    dev_io.send_to(&wire, peer).await?;

    // Pake3 → success StatusReport (acked by the controller) → finish.
    let (p, _) = dev_io.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PASE_PAKE3);
    verifier
        .handle_pake3(&m.payload)
        .map_err(DriverError::Crypto)?;
    unsecured_ctr += 1;
    close_handshake_with_status_report(
        dev_io,
        peer,
        unsecured_ctr,
        m.exchange_id,
        m.message_counter,
    )
    .await?;
    let pase_keys = verifier.finish().map_err(DriverError::Crypto)?;

    // The attestation challenge is the PASE-derived attestation_key — identical
    // on both sides (same SPAKE2+ secret), so it matches the controller's
    // `CommissionerConfig::pase_attestation_challenge`.
    let attestation_challenge: [u8; 16] = pase_keys.attestation_key;

    // Register under the device's OWN advertised id (`pase_responder_session_id`)
    // as the local id — the controller addresses its secured PASE-session
    // messages TO that id (it captured it from `prover.responder_session_id()`),
    // so `decode_inbound` must find the session keyed under it. The peer
    // (controller) session id is `CTRL_PASE_SESSION_ID` (its fresh-allocator
    // local id 1). This mirrors the controller's own
    // `register_pase_with_local_id(local, …, peer_session_id)` in pase.rs.
    let pase_sid = SessionId(pase_responder_session_id);
    sessions.register_pase_with_local_id(
        pase_sid,
        pase_keys,
        SessionRole::Responder,
        CTRL_PASE_SESSION_ID,
        PeerHint::default(),
    );

    // ── 2./4. Secured IM service handler, shared by the PASE and CASE loops ──
    //
    // Handle one inbound secured IM packet: decode, build the reply, send it on
    // the same exchange id. Returns the (cluster, command) of a serviced Invoke
    // (None for reads / acks) so the CASE-phase loop can detect
    // CommissioningComplete and stop.
    //
    // NOTE on opcode: the IM protocol opcode (0x08 InvokeRequest / 0x02
    // ReadRequest) lives in the *encrypted* protocol header, which
    // `decode_inbound` consumes internally — it is NOT in the cleartext packet,
    // and `DecodeInboundOutput` does not surface it. We therefore discriminate
    // on the decrypted IM payload's top-level TLV shape (see `is_read_request`):
    // a ReadRequestMessage exposes its AttributeRequests array at context tag 0,
    // whereas an InvokeRequestMessage exposes its InvokeRequests array at
    // context tag 2 (tag 0 there is the SuppressResponse bool). Unambiguous for
    // the two message kinds the commissioning flow sends over a secured session.
    async fn service_secured_im(
        dev_io: &InMemoryDatagram,
        peer: std::net::SocketAddr,
        sessions: &mut SessionManager,
        session_id: SessionId,
        packet: &[u8],
        challenge: [u8; 16],
        pki: &MockDevicePki,
    ) -> Result<Option<(u32, u32)>, DriverError> {
        let decoded = sessions.decode_inbound(packet, Instant::now())?;
        let (exchange_id, payload) = match decoded {
            DecodeInboundOutput::AppMessage {
                exchange_id,
                payload,
                ..
            } => (exchange_id, payload),
            DecodeInboundOutput::DuplicateReliableAckResent { ack_packet, .. } => {
                dev_io.send_to(&ack_packet, peer).await?;
                return Ok(None);
            }
            // AckOnly (no app payload), and — `DecodeInboundOutput` being
            // `#[non_exhaustive]` — any future outcome: nothing to reply to.
            _ => return Ok(None),
        };

        if is_read_request(&payload) {
            let paths = parse_read_request(&payload);
            let values: Vec<Vec<u8>> = paths.iter().map(|p| respond_read_attribute(*p)).collect();
            let pairs: Vec<(matter_commissioning::im::AttributePath, &[u8])> = paths
                .iter()
                .zip(values.iter())
                .map(|(p, v)| (*p, v.as_slice()))
                .collect();
            let reply_bytes = build_report_data(&pairs);
            let out = sessions.encode_outbound(
                session_id,
                Some(exchange_id),
                op::IM_REPORT_DATA,
                ProtocolId::INTERACTION_MODEL,
                &reply_bytes,
                MrpFlags { reliable: true },
                Instant::now(),
            )?;
            dev_io.send_to(&out.wire_bytes, peer).await?;
            Ok(None)
        } else {
            let decoded_req = parse_invoke_request(&payload);
            let path = decoded_req.path;
            let serviced = (path.cluster, path.command);
            let reply_bytes = match respond(path, &decoded_req.fields_tlv, challenge, pki) {
                DeviceReply::Command(fields_tlv) => build_invoke_response(path, &fields_tlv),
                DeviceReply::Status(code) => build_invoke_status_response(path, code),
            };
            let out = sessions.encode_outbound(
                session_id,
                Some(exchange_id),
                op::IM_INVOKE_RESPONSE,
                ProtocolId::INTERACTION_MODEL,
                &reply_bytes,
                MrpFlags { reliable: true },
                Instant::now(),
            )?;
            dev_io.send_to(&out.wire_bytes, peer).await?;
            Ok(Some(serviced))
        }
    }

    // PASE-phase IM loop: run until an *unsecured* packet (CASE Sigma1) arrives.
    // That first CASE packet is captured and handed to the CASE responder below.
    let sigma1_packet: Vec<u8> = loop {
        let (packet, _from) = dev_io.recv_from().await?;
        // Peek the 2-byte LE session id at header offset 1 (framing::encode_header
        // layout). Session id 0 ⇒ unsecured ⇒ the controller's CASE Sigma1, i.e.
        // the PASE→CASE transition. Non-zero ⇒ secured IM over the PASE session.
        let session_id_field = u16::from_le_bytes([packet[1], packet[2]]);
        if session_id_field == 0 {
            break packet;
        }
        service_secured_im(
            dev_io,
            peer,
            &mut sessions,
            pase_sid,
            &packet,
            attestation_challenge,
            pki,
        )
        .await?;
    };

    // ── 3. CASE (device = responder, unsecured path) ─────────────────────────
    let MockDeviceCaseSetup {
        credentials,
        trusted_roots,
        responder_session_id,
        now: case_now,
    } = case_setup;

    let mut responder =
        CaseResponder::new(credentials, trusted_roots, responder_session_id, case_now)
            .map_err(DriverError::Crypto)?;
    let m = decode_unsecured(&sigma1_packet)?;
    debug_assert_eq!(m.opcode, op::CASE_SIGMA1);
    match responder
        .handle_sigma1(&m.payload)
        .map_err(DriverError::Crypto)?
    {
        Sigma1Outcome::NewSession => {}
        // Resumption is not exercised in the M6 loopback.
        Sigma1Outcome::ResumptionRequested { .. } => {
            return Err(DriverError::Handshake(
                "mock device CASE Sigma1 was not a fresh session",
            ));
        }
    }
    let sigma2 = responder.next_message().map_err(DriverError::Crypto)?;
    let wire = encode_unsecured(
        unsecured_ctr,
        m.exchange_id,
        op::CASE_SIGMA2,
        ProtocolId::SECURE_CHANNEL,
        false,
        true,
        Some(m.message_counter),
        None,
        &sigma2,
    );
    dev_io.send_to(&wire, peer).await?;

    let (p, _) = dev_io.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::CASE_SIGMA3);
    responder
        .handle_sigma3(&m.payload)
        .map_err(DriverError::Crypto)?;
    unsecured_ctr += 1;
    close_handshake_with_status_report(
        dev_io,
        peer,
        unsecured_ctr,
        m.exchange_id,
        m.message_counter,
    )
    .await?;
    let case_output = responder.finish().map_err(DriverError::Crypto)?;
    let case_sid = sessions.register_case(&case_output, SessionRole::Responder);

    // ── 4. Secured IM loop on the CASE session until CommissioningComplete ───
    loop {
        let (packet, _from) = dev_io.recv_from().await?;
        let serviced = service_secured_im(
            dev_io,
            peer,
            &mut sessions,
            case_sid,
            &packet,
            attestation_challenge,
            pki,
        )
        .await?;
        if let Some((cluster, command)) = serviced {
            if cluster == matter_commissioning::clusters::general_commissioning::CLUSTER_ID
                && command == CMD_COMMISSIONING_COMPLETE
            {
                // Serviced CommissioningComplete on CASE — the controller's
                // commission() will return Action::Done next; we're finished.
                return Ok(());
            }
        }
    }
}

/// Which network interface the dual-transport mock advertises (via
/// `NetworkCommissioning.FeatureMap`) and therefore which provisioning path the
/// controller drives. Additive selector so one mock serves both the Wi-Fi
/// (M9-C1) and Thread (M9-C2) loopbacks without diverging the wire behavior of
/// the shared PASE/CASE scaffolding.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum MockNetworkKind {
    /// Advertise `FeatureMap = WIFI`; answer `AddOrUpdateWiFiNetwork` (0x02).
    WiFi,
    /// Advertise `FeatureMap = THREAD`; answer `AddOrUpdateThreadNetwork` (0x03).
    Thread,
}

/// One `InvokeRequest` command the mock decoded on a secured IM session,
/// captured so a test can assert *what the controller sent* — the on-wire bytes
/// are AES-CCM-encrypted under the session key, so the decoded command fields
/// are only observable here inside the device half.
#[derive(Clone, Debug)]
pub struct CapturedCommand {
    /// Cluster id of the invoked command (e.g. `0x0031` NetworkCommissioning).
    pub cluster: u32,
    /// Command id (e.g. `0x03` AddOrUpdateThreadNetwork, `0x06` ConnectNetwork).
    pub command: u32,
    /// The anonymous-tagged command-fields TLV as decoded from the request.
    pub fields_tlv: Vec<u8>,
}

/// Shared, cloneable capture sink the dual-transport mock appends every
/// serviced [`CapturedCommand`] to. Tests hold a clone and inspect it after the
/// run completes.
pub type CommandLog = std::sync::Arc<std::sync::Mutex<Vec<CapturedCommand>>>;

/// Service one inbound secured IM packet on a given session/transport (the
/// module-level twin of `run_mock_device`'s nested `service_secured_im`, so the
/// dual-transport harness can reuse it without disturbing the single-transport
/// harness). Decodes, builds the reply, sends it on the same exchange id;
/// returns the `(cluster, command)` of a serviced Invoke (None for reads/acks).
///
/// `network` selects the FeatureMap the read responder advertises (WIFI vs
/// THREAD) and thus which provisioning path the controller takes. Every
/// serviced Invoke's `(cluster, command, fields_tlv)` is appended to
/// `command_log` so tests can assert the controller's decrypted commands.
///
/// # Errors
///
/// Propagates datagram / framing failures as [`DriverError`].
#[allow(clippy::too_many_arguments)]
async fn service_secured_im_dual(
    dev_io: &InMemoryDatagram,
    peer: std::net::SocketAddr,
    sessions: &mut SessionManager,
    session_id: SessionId,
    packet: &[u8],
    challenge: [u8; 16],
    pki: &MockDevicePki,
    network: MockNetworkKind,
    command_log: &CommandLog,
) -> Result<Option<(u32, u32)>, DriverError> {
    use std::time::Instant;

    let decoded = sessions.decode_inbound(packet, Instant::now())?;
    let (exchange_id, payload) = match decoded {
        DecodeInboundOutput::AppMessage {
            exchange_id,
            payload,
            ..
        } => (exchange_id, payload),
        DecodeInboundOutput::DuplicateReliableAckResent { ack_packet, .. } => {
            dev_io.send_to(&ack_packet, peer).await?;
            return Ok(None);
        }
        _ => return Ok(None),
    };

    if is_read_request(&payload) {
        let paths = parse_read_request(&payload);
        // Flavor the FeatureMap by the requested network kind so the controller
        // drives the matching provisioning path.
        let values: Vec<Vec<u8>> = paths
            .iter()
            .map(|p| match network {
                MockNetworkKind::WiFi => respond_read_attribute_wifi(*p),
                MockNetworkKind::Thread => respond_read_attribute_thread(*p),
            })
            .collect();
        let pairs: Vec<(matter_commissioning::im::AttributePath, &[u8])> = paths
            .iter()
            .zip(values.iter())
            .map(|(p, v)| (*p, v.as_slice()))
            .collect();
        let reply_bytes = build_report_data(&pairs);
        let out = sessions.encode_outbound(
            session_id,
            Some(exchange_id),
            op::IM_REPORT_DATA,
            ProtocolId::INTERACTION_MODEL,
            &reply_bytes,
            MrpFlags { reliable: true },
            Instant::now(),
        )?;
        dev_io.send_to(&out.wire_bytes, peer).await?;
        Ok(None)
    } else {
        let decoded_req = parse_invoke_request(&payload);
        let path = decoded_req.path;
        // Record the decrypted command so tests can assert the controller sent
        // exactly the expected provisioning commands + payloads.
        command_log.lock().unwrap().push(CapturedCommand {
            cluster: path.cluster,
            command: path.command,
            fields_tlv: decoded_req.fields_tlv.clone(),
        });
        let serviced = (path.cluster, path.command);
        let reply_bytes = match respond(path, &decoded_req.fields_tlv, challenge, pki) {
            DeviceReply::Command(fields_tlv) => build_invoke_response(path, &fields_tlv),
            DeviceReply::Status(code) => build_invoke_status_response(path, code),
        };
        let out = sessions.encode_outbound(
            session_id,
            Some(exchange_id),
            op::IM_INVOKE_RESPONSE,
            ProtocolId::INTERACTION_MODEL,
            &reply_bytes,
            MrpFlags { reliable: true },
            Instant::now(),
        )?;
        dev_io.send_to(&out.wire_bytes, peer).await?;
        Ok(Some(serviced))
    }
}

/// Two-transport device twin for `driver::commission_ble`: PASE and every
/// pre-operational IM stage run over `btp_dev`; CASE and the operational IM run
/// over `udp_dev`.
///
/// The single-transport [`run_mock_device`] detects the PASE→CASE transition by
/// an unsecured packet arriving on its one socket. Here the transition is
/// implicit in the transport split — the controller stops sending on `btp_dev`
/// and sends CASE Sigma1 on `udp_dev` — so the PASE-phase IM loop watches BOTH
/// sockets and breaks to CASE handling when Sigma1 lands on `udp_dev`.
///
/// The BTP path runs under `TransportReliability::TransportProvides`, so the
/// controller neither sets the R-flag nor sends a standalone ack for the PASE
/// closing `StatusReport`; the device therefore sends that report and does NOT
/// wait for an ack. The CASE handshake on `udp_dev` runs under MRP exactly as in
/// `run_mock_device` (the controller acks the closing report).
///
/// # Errors
///
/// - [`DriverError::Io`] if a datagram recv/send fails or a channel closes.
/// - [`DriverError::Crypto`] / [`DriverError::Transport`] on a PASE/CASE step or
///   secured-framing failure.
///
/// # Panics
///
/// Panics (via the `respond`/`build_*`/`parse_*` helpers) on any malformed
/// controller message — acceptable in test-support code.
#[allow(clippy::too_many_lines, clippy::too_many_arguments)]
pub async fn run_mock_device_dual(
    btp_dev: &InMemoryDatagram,
    udp_dev: &InMemoryDatagram,
    btp_peer: std::net::SocketAddr,
    udp_peer: std::net::SocketAddr,
    pki: &MockDevicePki,
    pase_pin: u32,
    pase_params: PasePbkdfParams,
    pase_responder_session_id: u16,
    case_setup: MockDeviceCaseSetup,
    network: MockNetworkKind,
    command_log: &CommandLog,
) -> Result<(), DriverError> {
    const CTRL_PASE_SESSION_ID: u16 = 1;

    let mut sessions = SessionManager::new();

    // ── 1. PASE over btp_dev (device = verifier, unsecured, MRP off) ──────────
    let mut verifier = PaseVerifier::new_from_pin(pase_pin, pase_params, pase_responder_session_id)
        .map_err(DriverError::Crypto)?;
    let mut unsecured_ctr: u32 = 100;

    // PBKDFParamRequest → PBKDFParamResponse.
    let (p, _) = btp_dev.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PBKDF_PARAM_REQUEST);
    verifier
        .handle_pbkdf_request(&m.payload)
        .map_err(DriverError::Crypto)?;
    let resp = verifier.next_message().map_err(DriverError::Crypto)?;
    btp_dev
        .send_to(
            &encode_unsecured(
                unsecured_ctr,
                m.exchange_id,
                op::PBKDF_PARAM_RESPONSE,
                ProtocolId::SECURE_CHANNEL,
                false,
                false, // MRP off on BTP
                Some(m.message_counter),
                None,
                &resp,
            ),
            btp_peer,
        )
        .await?;
    unsecured_ctr += 1;

    // Pake1 → Pake2.
    let (p, _) = btp_dev.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PASE_PAKE1);
    verifier
        .handle_pake1(&m.payload)
        .map_err(DriverError::Crypto)?;
    let pake2 = verifier.next_message().map_err(DriverError::Crypto)?;
    btp_dev
        .send_to(
            &encode_unsecured(
                unsecured_ctr,
                m.exchange_id,
                op::PASE_PAKE2,
                ProtocolId::SECURE_CHANNEL,
                false,
                false,
                Some(m.message_counter),
                None,
                &pake2,
            ),
            btp_peer,
        )
        .await?;
    unsecured_ctr += 1;

    // Pake3 → success StatusReport (NOT acked on a reliable transport) → finish.
    let (p, _) = btp_dev.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PASE_PAKE3);
    verifier
        .handle_pake3(&m.payload)
        .map_err(DriverError::Crypto)?;
    let mut body = Vec::with_capacity(8);
    body.extend_from_slice(&0u16.to_le_bytes()); // SUCCESS
    body.extend_from_slice(&0u32.to_le_bytes()); // SecureChannel
    body.extend_from_slice(&0u16.to_le_bytes()); // SessionEstablishmentSuccess
    btp_dev
        .send_to(
            &encode_unsecured(
                unsecured_ctr,
                m.exchange_id,
                op::STATUS_REPORT,
                ProtocolId::SECURE_CHANNEL,
                false,
                false, // not reliable — controller sends no ack under TransportProvides
                Some(m.message_counter),
                None,
                &body,
            ),
            btp_peer,
        )
        .await?;
    unsecured_ctr += 1;
    let pase_keys = verifier.finish().map_err(DriverError::Crypto)?;
    let attestation_challenge: [u8; 16] = pase_keys.attestation_key;

    let pase_sid = SessionId(pase_responder_session_id);
    sessions.register_pase_with_local_id(
        pase_sid,
        pase_keys,
        SessionRole::Responder,
        CTRL_PASE_SESSION_ID,
        PeerHint::default(),
    );

    // ── 2. PASE-phase secured IM over btp_dev, until CASE Sigma1 on udp_dev ───
    let sigma1_packet: Vec<u8> = loop {
        tokio::select! {
            // Pre-operational IM on the PASE session over BTP.
            r = btp_dev.recv_from() => {
                let (packet, _from) = r?;
                service_secured_im_dual(
                    btp_dev,
                    btp_peer,
                    &mut sessions,
                    pase_sid,
                    &packet,
                    attestation_challenge,
                    pki,
                    network,
                    command_log,
                )
                .await?;
            }
            // The controller's CASE Sigma1 lands on the UDP transport — the
            // PASE→CASE transition. Capture it and hand it to the responder.
            r = udp_dev.recv_from() => {
                let (packet, _from) = r?;
                break packet;
            }
        }
    };

    // ── 3. CASE over udp_dev (device = responder, unsecured, MRP on) ──────────
    let MockDeviceCaseSetup {
        credentials,
        trusted_roots,
        responder_session_id,
        now: case_now,
    } = case_setup;

    let mut responder =
        CaseResponder::new(credentials, trusted_roots, responder_session_id, case_now)
            .map_err(DriverError::Crypto)?;
    let m = decode_unsecured(&sigma1_packet)?;
    debug_assert_eq!(m.opcode, op::CASE_SIGMA1);
    match responder
        .handle_sigma1(&m.payload)
        .map_err(DriverError::Crypto)?
    {
        Sigma1Outcome::NewSession => {}
        Sigma1Outcome::ResumptionRequested { .. } => {
            return Err(DriverError::Handshake(
                "mock dual device CASE Sigma1 was not a fresh session",
            ));
        }
    }
    let sigma2 = responder.next_message().map_err(DriverError::Crypto)?;
    udp_dev
        .send_to(
            &encode_unsecured(
                unsecured_ctr,
                m.exchange_id,
                op::CASE_SIGMA2,
                ProtocolId::SECURE_CHANNEL,
                false,
                true, // MRP on over UDP
                Some(m.message_counter),
                None,
                &sigma2,
            ),
            udp_peer,
        )
        .await?;

    let (p, _) = udp_dev.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::CASE_SIGMA3);
    responder
        .handle_sigma3(&m.payload)
        .map_err(DriverError::Crypto)?;
    unsecured_ctr += 1;
    close_handshake_with_status_report(
        udp_dev,
        udp_peer,
        unsecured_ctr,
        m.exchange_id,
        m.message_counter,
    )
    .await?;
    let case_output = responder.finish().map_err(DriverError::Crypto)?;
    let case_sid = sessions.register_case(&case_output, SessionRole::Responder);

    // ── 4. Operational secured IM over udp_dev until CommissioningComplete ────
    loop {
        let (packet, _from) = udp_dev.recv_from().await?;
        let serviced = service_secured_im_dual(
            udp_dev,
            udp_peer,
            &mut sessions,
            case_sid,
            &packet,
            attestation_challenge,
            pki,
            network,
            command_log,
        )
        .await?;
        if let Some((cluster, command)) = serviced {
            if cluster == matter_commissioning::clusters::general_commissioning::CLUSTER_ID
                && command == CMD_COMMISSIONING_COMPLETE
            {
                return Ok(());
            }
        }
    }
}

/// Dual-transport device twin that answers PASE and the first
/// `service_pre_op_stages` pre-operational IM round-trips over `btp_dev`, then
/// **goes silent** — it holds both endpoints open (so neither channel closes)
/// but never sends another datagram.
///
/// This is the D11.3 coverage twin of [`run_mock_device_dual`]: it forces the
/// controller's poll loop to hit its response deadline on a stalled BTP session
/// (a `TransportProvides` dispatch that never gets a reply → the loop exits with
/// `DriverError::Timeout`), and then keeps stalling so the *failure-exit rollback*
/// (`ArmFailSafe(0)` over the same dead BTP session) also gets no reply. It never
/// reaches the CASE phase, so `udp_dev`/`udp_peer` are unused past the signature;
/// they are kept for parity with [`run_mock_device_dual`].
///
/// Because the tail never resolves, drive this under a `tokio::select!` against
/// `commission_ble` (not `tokio::join!`): when `commission_ble` returns, the
/// device future — and its borrowed endpoints — is dropped. Run the test under
/// `#[tokio::test(start_paused = true)]` so the two 30 s response deadlines
/// (loop + rollback) elapse in ~60 s of *virtual* time rather than wall time.
///
/// The PASE ping-pong is duplicated verbatim from [`run_mock_device_dual`] rather
/// than shared, to keep this addition strictly non-invasive to the existing
/// dual-transport harness.
///
/// # Errors
///
/// - [`DriverError::Io`] if a datagram recv/send fails.
/// - [`DriverError::Crypto`] on a PASE step or secured-framing failure.
///
/// # Panics
///
/// Panics (via the `respond`/`build_*`/`parse_*` helpers) on any malformed
/// controller message — acceptable in test-support code.
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
pub async fn run_mock_device_dual_silent_after(
    btp_dev: &InMemoryDatagram,
    _udp_dev: &InMemoryDatagram,
    btp_peer: std::net::SocketAddr,
    _udp_peer: std::net::SocketAddr,
    pki: &MockDevicePki,
    pase_pin: u32,
    pase_params: PasePbkdfParams,
    pase_responder_session_id: u16,
    service_pre_op_stages: usize,
) -> Result<(), DriverError> {
    const CTRL_PASE_SESSION_ID: u16 = 1;

    let mut sessions = SessionManager::new();

    // ── 1. PASE over btp_dev (device = verifier, unsecured, MRP off) ──────────
    let mut verifier = PaseVerifier::new_from_pin(pase_pin, pase_params, pase_responder_session_id)
        .map_err(DriverError::Crypto)?;
    let mut unsecured_ctr: u32 = 100;

    // PBKDFParamRequest → PBKDFParamResponse.
    let (p, _) = btp_dev.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PBKDF_PARAM_REQUEST);
    verifier
        .handle_pbkdf_request(&m.payload)
        .map_err(DriverError::Crypto)?;
    let resp = verifier.next_message().map_err(DriverError::Crypto)?;
    btp_dev
        .send_to(
            &encode_unsecured(
                unsecured_ctr,
                m.exchange_id,
                op::PBKDF_PARAM_RESPONSE,
                ProtocolId::SECURE_CHANNEL,
                false,
                false, // MRP off on BTP
                Some(m.message_counter),
                None,
                &resp,
            ),
            btp_peer,
        )
        .await?;
    unsecured_ctr += 1;

    // Pake1 → Pake2.
    let (p, _) = btp_dev.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PASE_PAKE1);
    verifier
        .handle_pake1(&m.payload)
        .map_err(DriverError::Crypto)?;
    let pake2 = verifier.next_message().map_err(DriverError::Crypto)?;
    btp_dev
        .send_to(
            &encode_unsecured(
                unsecured_ctr,
                m.exchange_id,
                op::PASE_PAKE2,
                ProtocolId::SECURE_CHANNEL,
                false,
                false,
                Some(m.message_counter),
                None,
                &pake2,
            ),
            btp_peer,
        )
        .await?;
    unsecured_ctr += 1;

    // Pake3 → success StatusReport (NOT acked on a reliable transport) → finish.
    let (p, _) = btp_dev.recv_from().await?;
    let m = decode_unsecured(&p)?;
    debug_assert_eq!(m.opcode, op::PASE_PAKE3);
    verifier
        .handle_pake3(&m.payload)
        .map_err(DriverError::Crypto)?;
    let mut body = Vec::with_capacity(8);
    body.extend_from_slice(&0u16.to_le_bytes()); // SUCCESS
    body.extend_from_slice(&0u32.to_le_bytes()); // SecureChannel
    body.extend_from_slice(&0u16.to_le_bytes()); // SessionEstablishmentSuccess
    btp_dev
        .send_to(
            &encode_unsecured(
                unsecured_ctr,
                m.exchange_id,
                op::STATUS_REPORT,
                ProtocolId::SECURE_CHANNEL,
                false,
                false, // not reliable — controller sends no ack under TransportProvides
                Some(m.message_counter),
                None,
                &body,
            ),
            btp_peer,
        )
        .await?;
    let pase_keys = verifier.finish().map_err(DriverError::Crypto)?;
    let attestation_challenge: [u8; 16] = pase_keys.attestation_key;

    let pase_sid = SessionId(pase_responder_session_id);
    sessions.register_pase_with_local_id(
        pase_sid,
        pase_keys,
        SessionRole::Responder,
        CTRL_PASE_SESSION_ID,
        PeerHint::default(),
    );

    // ── 2. Service `service_pre_op_stages` pre-op IM round-trips, then STALL ───
    // Each iteration answers exactly one secured IM request over the PASE session
    // on BTP (an Invoke or a Read). After the configured count, the device stops
    // replying but keeps both endpoints alive by parking on a never-ready future.
    // The next controller dispatch (still a `TransportProvides` PASE-session
    // dispatch) then hits its response deadline, and so does the rollback.
    // This stall twin never reaches network provisioning; advertise Wi-Fi and
    // discard the capture (a local throwaway sink) so the public signature stays
    // unchanged for the D11.3 rollback test that calls it.
    let discard_log: CommandLog = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    for _ in 0..service_pre_op_stages {
        let (packet, _from) = btp_dev.recv_from().await?;
        service_secured_im_dual(
            btp_dev,
            btp_peer,
            &mut sessions,
            pase_sid,
            &packet,
            attestation_challenge,
            pki,
            MockNetworkKind::WiFi,
            &discard_log,
        )
        .await?;
    }

    // Go silent. Parking this future keeps every borrowed endpoint (including
    // `_udp_dev`) alive for the duration of the caller's `select!`. `pending`
    // never resolves; the caller drops us when `commission_ble` returns.
    std::future::pending::<()>().await;
    unreachable!("run_mock_device_dual_silent_after parks forever until dropped")
}

/// Discriminate a decrypted IM message payload: `true` for a
/// `ReadRequestMessage`, `false` for an `InvokeRequestMessage`.
///
/// Both are top-level anonymous structs. A `ReadRequestMessage` carries its
/// `AttributeRequests` **array at context tag 0** (`im::build_read_request`); an
/// `InvokeRequestMessage` carries `SuppressResponse` (a bool) at context tag 0
/// and its `InvokeRequests` array at context tag 2 (`im::build_invoke_request`).
/// We scan the top-level struct members: if context tag 0 is an array, it is a
/// read; otherwise an invoke. This is unambiguous for the two message kinds the
/// commissioning flow ever sends over a secured session — the opcode itself is
/// inside the encrypted protocol header and not otherwise recoverable here.
///
/// # Panics
///
/// Panics if `payload` is not a valid top-level TLV struct — acceptable in test
/// support code where the input is always `commission()`-generated.
fn is_read_request(payload: &[u8]) -> bool {
    use matter_codec::{ContainerKind, Element, Tag, TlvReader};

    let mut r = TlvReader::new(payload);
    match r.next().expect("IM payload: first element") {
        Some(Element::ContainerStart {
            tag: Tag::Anonymous,
            kind: ContainerKind::Structure,
        }) => {}
        other => panic!("IM payload: expected top-level anon struct, got {other:?}"),
    }
    // Inspect the immediate member at context tag 0.
    match r.next().expect("IM payload: first member") {
        // ReadRequestMessage: AttributeRequests array at ctx(0).
        Some(Element::ContainerStart {
            tag: Tag::Context(0),
            kind: ContainerKind::Array,
        }) => true,
        // InvokeRequestMessage: SuppressResponse bool at ctx(0) (or any non-array).
        _ => false,
    }
}