File size: 114,122 Bytes
ef28ed2
 
 
 
 
 
 
336a9be
ef28ed2
 
6567a38
ef28ed2
24463b6
ef28ed2
 
070294b
ef28ed2
 
3e92b27
24463b6
ef28ed2
 
3e92b27
ef28ed2
 
3e92b27
cae32d7
ef28ed2
 
 
3e92b27
ef28ed2
 
3e92b27
 
 
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24463b6
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cae32d7
 
 
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
cae32d7
ef28ed2
 
 
 
 
 
336a9be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d78e3fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
070294b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6023bcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e92b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6567a38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24463b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef28ed2
 
 
 
 
 
 
 
24463b6
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24463b6
ef28ed2
24463b6
ef28ed2
070294b
d78e3fd
ef28ed2
d78e3fd
6023bcd
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
d78e3fd
 
070294b
336a9be
ef28ed2
 
6023bcd
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
070294b
d78e3fd
336a9be
 
 
ef28ed2
6023bcd
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d78e3fd
ef28ed2
 
 
 
 
 
 
 
070294b
 
 
 
 
 
 
 
 
 
 
ef28ed2
 
 
 
 
 
 
 
 
6023bcd
 
 
 
ef28ed2
 
 
336a9be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef28ed2
 
 
 
 
070294b
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24463b6
ef28ed2
 
 
 
 
 
 
 
 
 
 
070294b
 
 
 
 
 
 
 
 
d78e3fd
 
 
 
 
 
 
 
 
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336a9be
 
 
 
 
 
6023bcd
 
 
 
 
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "browser/browser_app.hpp"
#include "core/arena.hpp"
#include "core/json.hpp"
#include "core/memory_governor.hpp"
#include "docs/doc_viewer.hpp"
#include "ext/extension_runtime.hpp"
#include "gfx/astra3d.hpp"
#include "gfx/font_engine.hpp"
#include "gfx/image_decoder.hpp"
#include "gfx/rasterizer.hpp"
#include "gfx/svg_engine.hpp"
#include "ipc/ipc_channel.hpp"
#include "ipc/sandbox.hpp"
#include "js/astra_js.hpp"
#include "media/media_engine.hpp"
#include "net/http2_client.hpp"
#include "net/http_cache.hpp"
#include "net/http_client.hpp"
#include "net/websocket_client.hpp"
#include "privacy/security_policy.hpp"
#include "privacy/shield.hpp"

#include <arpa/inet.h>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <netinet/in.h>
#include <sstream>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
#include <vector>

using namespace astra;

namespace {

int g_tests_passed = 0;
int g_tests_failed = 0;

void expect(bool cond, const std::string& label) {
    if (cond) {
        ++g_tests_passed;
        std::cout << "  [PASS] " << label << "\n";
    } else {
        ++g_tests_failed;
        std::cerr << "  [FAIL] " << label << "\n";
    }
}

int run_full_test_suite() {
    std::cout << "=== Astra Browser Core v9 — Full Subsystem Verification Suite ===\n\n";

    // 1. SlabArena + OS Reclamation
    {
        SlabArena arena(64 * 1024);
        for (int i = 0; i < 4000; ++i) (void)arena.create<int>(i);
        size_t before = arena.bytes_used();
        arena.reset(true);
        expect(before >= 16000 && arena.bytes_used() == 0 && arena.slab_count() == 0,
               "SlabArena allocates cleanly and releases OS slabs via munmap/madvise");
    }

    // 2. JSON Parser
    {
        JsonValue v;
        std::string err;
        bool ok = JsonValue::parse(R"({"name":"Astra","mv":3,"perms":["storage","declarativeNetRequest"],"active":true})", v, &err);
        expect(ok && v.get_string("name") == "Astra" && v.get_number("mv") == 3 &&
               v.get_bool("active") && v.get("perms")->as_array().size() == 2,
               "RFC 8259 JSON parser parses Manifest V3 structures");
    }

    // 3. AstraShield Privacy Engine
    {
        AstraShield shield;
        auto d1 = shield.inspect_request("http://example.com/article?id=42&utm_source=ads&fbclid=xyz999", "example.com", true);
        expect(!d1.blocked && d1.upgraded_to_https && d1.params_stripped == 2 &&
               d1.sanitized_url == "https://example.com/article?id=42",
               "AstraShield upgrades HTTP->HTTPS and strips utm_source/fbclid params");

        auto d2 = shield.inspect_request("https://stats.doubleclick.net/pixel.js", "example.com", false);
        expect(d2.blocked && d2.category == TrackerCategory::Advertising,
               "AstraShield suffix-trie blocks subdomain trackers (stats.doubleclick.net)");

        shield.store_cookie("site-a.com", "https://cdn.widget.net", "uid=ALPHA; Secure; HttpOnly");
        std::string on_a = shield.cookie_header_for("site-a.com", "https://cdn.widget.net", true);
        std::string on_b = shield.cookie_header_for("site-b.com", "https://cdn.widget.net", true);
        expect(on_a == "uid=ALPHA" && on_b.empty(),
               "Total Cookie Protection isolates third-party cookies across top-level sites");
    }

    // 4. Bounded RAM+Disk HttpCache
    {
        HttpCache cache("/tmp/astra_test_cache", 2048);
        cache.store({"https://astra.test/a", "text/css", "\"e1\"", std::string(1000, 'A'), 0, 600});
        cache.store({"https://astra.test/b", "text/css", "\"e2\"", std::string(1000, 'B'), 0, 600});
        cache.store({"https://astra.test/c", "text/css", "\"e3\"", std::string(1000, 'C'), 0, 600});
        CachedResource hit;
        bool found_c = cache.lookup("https://astra.test/c", hit);
        expect(found_c && hit.body.size() == 1000 && cache.stats().evictions >= 1 && cache.stats().current_bytes <= 2048,
               "HttpCache stores to RAM+Disk and enforces bounded LRU byte eviction");
    }

    // 5. Native PNG Encoder + Native PNG Decoder roundtrip (all zero external build deps)
    {
        Framebuffer fb(64, 48);
        fb.fill_rect(0, 0, 64, 48, 0xFF112233u);
        fb.fill_rect(10, 10, 20, 20, 0xFF38BDF8u);
        bool saved = fb.save_png("/tmp/astra_roundtrip_test.png");
        DecodedImage dec = ImageDecoder::decode_file("/tmp/astra_roundtrip_test.png");
        expect(saved && dec.valid && dec.width == 64 && dec.height == 48 &&
               dec.argb[15 * 64 + 15] == 0xFF38BDF8u && dec.argb[0] == 0xFF112233u,
               "AstraImage native PNG decoder roundtrips exact ARGB32 pixels cleanly");
    }

    // 6. HTML5 + CSS3 + Layout + HitRegions + Binary DisplayList v2
    {
        SlabArena arena;
        std::string html =
            "<!doctype html><html><head><title>Astra v2</title>"
            "<style>.box { background: #1e293b; padding: 12px; border-radius: 8px; }</style>"
            "</head><body><div class=\"box\"><a href=\"astra://3d\">Open 3D</a><button id=\"go\">Click</button></div></body></html>";
        HtmlDocument doc = HtmlDocument::parse(arena, html);
        CssStylesheet sheet;
        sheet.parse(doc.collect_stylesheets());
        LayoutFrame* root = LayoutEngine::build_layout(arena, doc, sheet, 1024, 600);
        DisplayList dl = LayoutEngine::build_display_list(root, 1024, 600);
        auto blob = dl.serialize();
        DisplayList rt;
        bool dec_ok = DisplayList::deserialize(blob, rt);
        expect(dec_ok && rt.items.size() == dl.items.size() && rt.hit_regions.size() == 2,
               "HTML5 -> CSS3 -> Layout -> ASD2 Binary DisplayList + HitRegions roundtrip");
    }

    // 7. AstraJS v2: Bytecode VM (functions, loops, arrays, objects, Mark-and-Sweep GC, Microtasks, Timers, DOM Events)
    {
        SlabArena arena;
        HtmlDocument doc = HtmlDocument::parse(
            arena, "<html><body><h1 id=\"t\">0</h1><button id=\"inc\">+1</button></body></html>");
        AstraJsEngine vm;
        std::map<std::string, std::string> store;
        vm.bind_storage([&](const std::string& k) { return store[k]; },
                        [&](const std::string& k, const std::string& v) { store[k] = v; });

        std::string script =
            "function fib(n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); }\n"
            "let sum = 0;\n"
            "let arr = [10, 20, 30];\n"
            "arr.push(40);\n"
            "for (let i = 0; i < arr.length; i = i + 1) { sum = sum + arr[i]; }\n"
            "let f8 = fib(8);\n"
            "let order = \"\";\n"
            "setTimeout(function() { order = order + \"T\"; }, 10);\n"
            "queueMicrotask(function() { order = order + \"M\"; });\n"
            "let garbage = { a: [1, 2, 3], b: { c: 9 } };\n"
            "garbage = null;\n"
            "let clicks = 0;\n"
            "let btn = document.querySelector(\"#inc\");\n"
            "btn.addEventListener(\"click\", function() {\n"
            "  clicks = clicks + 1;\n"
            "  document.querySelector(\"#t\").textContent = \"Clicks:\" + clicks + \" Fib8:\" + f8 + \" Sum:\" + sum;\n"
            "});\n"
            "console.log(\"fib8=\", f8, \"sum=\", sum);\n";

        auto rep = vm.execute(script, doc);
        size_t freed = vm.gc_collect();
        auto click1 = vm.dispatch_event(doc.query_selector("#inc"), "click", doc);
        auto click2 = vm.dispatch_event(doc.query_selector("#inc"), "click", doc);
        (void)click1; (void)click2;

        std::string order = vm.globals().count("order") ? vm.globals().at("order").to_string() : "";
        std::string dom_txt = doc.query_selector("#t")->text_content();
        expect(rep.ok && rep.bytecode_size > 40 && rep.microtasks_run == 1 && rep.timers_run == 1 &&
               order == "MT" && freed >= 3 && dom_txt == "Clicks:2 Fib8:21 Sum:100",
               "AstraJS v2 Bytecode VM executes recursion, loops, arrays, GC sweep, Microtask->Timer order, and DOM click events");
    }

    // 8. Astra3D v2 Smooth Shader Engine
    {
        Framebuffer fb(320, 200);
        Camera3D cam;
        cam.smooth_shading = true;
        cam.wireframe = false;
        auto st = Astra3D::render_scene_into(fb.pixels(), 320, 0, 0, 320, 200, "planet", 0.5f, cam);
        expect(st.vertices > 600 && st.triangles_rasterized > 400,
               "Astra3D v2 smooth-normal planetary shader engine rasterizes high-detail geometry");
    }

    // 9. Native PDF Viewer
    {
        std::string pdf =
            "%PDF-1.4\n"
            "1 0 obj << /Title (Astra Spec) /Author (Astra Team) >> endobj\n"
            "2 0 obj << /Length 44 >> stream\n"
            "BT /F1 12 Tf 72 720 Td (Astra Native PDF Works!) Tj ET\n"
            "endstream endobj\n%%EOF\n";
        ParsedDocument d = DocViewer::parse_pdf_bytes(pdf);
        expect(d.ok && d.title == "Astra Spec" && d.page_count == 1 &&
               d.pages[0].find("Astra Native PDF Works!") != std::string::npos,
               "AstraDocs parses real PDF header, metadata, and BT/ET/Tj text streams natively");
    }

    // 10. AstraMedia v2 Bounded Playback Session (multi-frame MP4 decode)
    {
        MediaPlaybackSession sess;
        std::string mp4 = "astra/examples/astra_demo.mp4";
        if (!std::ifstream(mp4)) mp4 = "examples/astra_demo.mp4";
        bool opened = sess.open(mp4);
        bool batched = opened && sess.decode_batch(0, 6);
        DecodedVideoFrame f0, f1;
        bool got2 = batched && sess.next_frame(f0) && sess.next_frame(f1);
        expect(got2 && f0.frame_index == 0 && f1.frame_index == 1 &&
               sess.buffered_bytes() <= MediaPlaybackSession::kMaxBufferBytes,
               "AstraMedia v2 decodes multi-frame H.264 MP4 sequence within 16 MiB ring buffer cap");
    }

    // 11. Multi-Process IPC + Strict TLS + Subresource Loader + Interactive Click/Scroll
    {
        std::error_code ec;
        std::filesystem::remove_all("/tmp/astra_http_cache", ec);

        BrowserApp app(2ULL * 1024 * 1024 * 1024);
        bool started = app.start_workers();
        expect(started && app.content_pid() > 0 && app.media_pid() > 0,
               "Multi-process architecture spawns isolated Content and RDD Media workers over AF_UNIX IPC");

        uint32_t tab1 = app.new_tab("astra://home");
        auto nav = app.navigate(tab1, "astra://home");
        expect(nav.ok && nav.rendered_via_ipc && nav.display_items > 10 && nav.hit_regions >= 2,
               "Content Process renders page into ASD2 DisplayList + HitRegions over IPC");

        // Test live strict-verified HTTPS + HttpCache hit on second fetch
        auto https1 = app.navigate(tab1, "https://example.com");
        auto https2 = app.navigate(tab1, "https://example.com");
        expect(https1.ok && https1.tls_verified && !https1.from_cache && https2.ok && https2.from_cache,
               "AstraNet enforces strict TLS certificate+hostname verification and hits HttpCache on repeat load");

        auto mem = app.memory_snapshot();
        expect(mem.total_rss_bytes > 0 && mem.total_rss_bytes < 256ULL * 1024 * 1024 && mem.processes.size() >= 3,
               "MemoryGovernor tracks real Linux /proc RSS across Main + Content + RDD workers");

        // 12. AstraFont Native TrueType Vector Typography + UTF-8 + Bounded LRU Cache
        {
            auto& fe = FontEngine::instance();
            float w_en = fe.measure_text("Astra Browser", 18.0f, true, false);
            float w_es = fe.measure_text("Navegación rápida — España", 16.0f, false, false);
            float w_mono = fe.measure_text("0xFF38BDF8", 13.0f, false, true);
            expect(fe.freetype_active() && w_en > 80.0f && w_es > 120.0f && w_mono > 50.0f &&
                   fe.cached_glyph_count() >= 15 && fe.cache_bytes() <= FontEngine::kMaxCacheBytes,
                   "AstraFont loads libfreetype.so.6 via dlopen, renders UTF-8 TrueType glyphs within 4 MiB LRU cap");
        }

        // 13. AstraImage v2 Native Pure-C++20 Baseline DCT JPEG Decoder
        {
            std::string jpg = "astra/examples/astra_nebula.jpg";
            if (!std::ifstream(jpg)) jpg = "examples/astra_nebula.jpg";
            DecodedImage jimg = ImageDecoder::decode_file(jpg);
            bool non_uniform = false;
            if (jimg.valid && jimg.argb.size() > 100) {
                uint32_t first = jimg.argb[0];
                for (size_t i = 1; i < jimg.argb.size(); i += 97) {
                    if (jimg.argb[i] != first) { non_uniform = true; break; }
                }
            }
            expect(jimg.valid && jimg.format == "JPEG" && jimg.width >= 300 && jimg.height >= 180 && non_uniform,
                   "AstraImage v2 pure-C++20 Baseline DCT JPEG decoder decodes real YCbCr JPEG images natively");
        }

        // 14. CSS Grid Multi-Column + Multi-Row Layout Engine
        {
            SlabArena ga;
            HtmlDocument gdoc = HtmlDocument::parse(
                ga,
                "<html><head><style>"
                ".g { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }"
                ".c { height: 60px; background: #1e293b; }"
                "</style></head><body><div class=\"g\">"
                "<div class=\"c\">1</div><div class=\"c\">2</div><div class=\"c\">3</div>"
                "<div class=\"c\">4</div><div class=\"c\">5</div><div class=\"c\">6</div>"
                "</div></body></html>");
            CssStylesheet gsheet;
            gsheet.parse(gdoc.collect_stylesheets());
            LayoutFrame* groot = LayoutEngine::build_layout(ga, gdoc, gsheet, 960.0f, 600.0f);
            LayoutFrame* grid = (groot && !groot->children.empty()) ? groot->children[0] : nullptr;
            bool ok_grid = grid && grid->children.size() == 6 &&
                           grid->children[0]->border_box.x < grid->children[1]->border_box.x &&
                           grid->children[1]->border_box.x < grid->children[2]->border_box.x &&
                           std::abs(grid->children[0]->border_box.y - grid->children[2]->border_box.y) < 1.0f &&
                           grid->children[3]->border_box.y > grid->children[0]->border_box.y + 60.0f;
            expect(ok_grid,
                   "CSS Grid (display: grid + repeat(3, 1fr) + gap) computes multi-column/multi-row geometry accurately");
        }

        // 15. Interactive <input> Focus, Keyboard Typing & AstraJS v3 input/submit Events
        {
            const std::string fpath = "/tmp/astra_input_test.html";
            {
                std::ofstream f(fpath);
                f << "<!doctype html><html><body>"
                  << "<input id=\"q\" placeholder=\"Search...\">"
                  << "<p id=\"out\">idle</p>"
                  << "<script>"
                  << "let inp = document.querySelector(\"#q\");"
                  << "inp.addEventListener(\"input\", function() { document.querySelector(\"#out\").textContent = \"typing:\" + inp.value; });"
                  << "inp.addEventListener(\"submit\", function() { document.querySelector(\"#out\").textContent = \"submitted:\" + inp.value.toUpperCase(); });"
                  << "</script></body></html>";
            }
            app.new_tab("file://" + fpath);
            const Tab* tptr = app.active_tab();
            int hit_x = 80, hit_y = Framebuffer::kChromeHeight + 40;
            if (tptr) {
                for (const auto& hr : tptr->cached_display_list.hit_regions) {
                    if (hr.kind == HitRegionKind::Input) {
                        hit_x = static_cast<int>(hr.rect.x + 20);
                        hit_y = Framebuffer::kChromeHeight + static_cast<int>(hr.rect.y + 16);
                    }
                }
            }
            auto cr = app.click_at(hit_x, hit_y);
            bool typed = app.type_text("astra v3");
            std::string mid_txt = app.active_tab()->live_doc->query_selector("#out")->text_content();
            bool submitted = app.press_enter();
            std::string end_txt = app.active_tab()->live_doc->query_selector("#out")->text_content();
            expect(cr.handled && cr.action == "focus_input" && typed && submitted &&
                   mid_txt == "typing:astra v3" && end_txt == "submitted:ASTRA V3",
                   "Interactive <input> focus, keyboard typing, and Enter dispatch real input/submit events in AstraJS v3");
        }

        // 16. AstraJS v3 Web APIs: fetch() via AstraShield + HttpCache, JSON.parse/stringify, localStorage
        {
            const std::string apath = "/tmp/astra_webapi_test.html";
            {
                std::ofstream f(apath);
                f << "<!doctype html><html><body><p id=\"res\">init</p><script>"
                  << "localStorage.setItem(\"engine\", \"AstraV3\");"
                  << "let obj = JSON.parse(\"{\\\"ver\\\":3,\\\"tags\\\":[\\\"cpp\\\",\\\"fast\\\"]}\");"
                  << "let ser = JSON.stringify({ ok: true, v: obj.ver });"
                  << "let blocked = fetch(\"https://doubleclick.net/pixel.js\");"
                  << "let live = fetch(\"https://example.com\");"
                  << "document.querySelector(\"#res\").textContent = localStorage.getItem(\"engine\") + \"|\" + obj.tags[1] + \"|\" + blocked.blockedByShield + \"|\" + live.status + \"|\" + ser;"
                  << "</script></body></html>";
            }
            app.new_tab("file://" + apath);
            std::string res_txt = app.active_tab()->live_doc->query_selector("#res")->text_content();
            expect(res_txt.find("AstraV3|fast|true|200|") == 0 && res_txt.find("\"v\":3") != std::string::npos,
                   "AstraJS v3 executes fetch() (with AstraShield blocking + live HTTPS), JSON.parse/stringify, and localStorage");
        }

        // 17. Interactive Browser Chrome v4: Editable Omnibox, History Back/Forward, [+] New Tab, [x] Close Tab
        {
            uint32_t t_home = app.new_tab("astra://home");
            (void)t_home;
            size_t tabs_before = app.tabs().size();
            auto c_omni = app.click_at(320, 60); // Click Omnibox
            bool typed_url = app.type_text("astra://3d");
            bool nav_enter = app.press_enter();
            std::string after_enter = app.active_tab()->url;
            auto c_back = app.click_at(28, 60); // Click Back [<]
            std::string after_back = app.active_tab()->url;
            auto c_fwd = app.click_at(62, 60); // Click Forward [>]
            std::string after_fwd = app.active_tab()->url;
            // Click [+] button after visible tabs
            size_t vis = std::min<size_t>(app.tabs().size(), 5);
            int plus_x = 92 + static_cast<int>(vis) * 213 + 12;
            auto c_new = app.click_at(plus_x, 20);
            size_t tabs_after_new = app.tabs().size();
            bool closed = app.close_tab(app.active_tab_id());
            size_t tabs_after_close = app.tabs().size();
            expect(c_omni.action == "focus_omnibox" && typed_url && nav_enter &&
                   after_enter == "astra://3d" && c_back.action == "back" &&
                   after_back == "astra://home" && c_fwd.action == "forward" &&
                   after_fwd == "astra://3d" && c_new.action == "new_tab" &&
                   tabs_after_new == tabs_before + 1 && closed && tabs_after_close == tabs_before,
                   "Chrome v4 interactive Omnibox, Back/Forward history stack, [+] new tab, and tab closing work end-to-end");
        }

        // 18. CSS Positioning (absolute/fixed), z-index Paint Ordering, box-shadow DropShadow, opacity & ASD3 Roundtrip
        {
            SlabArena pa;
            HtmlDocument pdoc = HtmlDocument::parse(
                pa,
                "<html><head><style>"
                ".back { position: relative; z-index: 1; background: #1e293b; height: 120px; box-shadow: 0 6px 14px rgba(0,0,0,0.5); }"
                ".top { position: fixed; top: 16px; right: 20px; width: 200px; z-index: 50; background: #0284c7; opacity: 0.9; }"
                ".abs { position: absolute; top: 10px; left: 30px; width: 140px; z-index: 20; background: #10b981; }"
                "</style></head><body>"
                "<div class=\"top\"><button id=\"pin\">Pinned</button></div>"
                "<div class=\"back\"><div class=\"abs\">Overlay</div></div>"
                "</body></html>");
            CssStylesheet psheet;
            psheet.parse(pdoc.collect_stylesheets());
            LayoutFrame* proot = LayoutEngine::build_layout(pa, pdoc, psheet, 1280.0f, 636.0f);
            DisplayList pdl = LayoutEngine::build_display_list(proot, 1280, 636);
            auto blob = pdl.serialize();
            DisplayList rt;
            bool rt_ok = DisplayList::deserialize(blob, rt);
            bool has_shadow = false, has_fixed = false, z_ordered = true;
            for (size_t i = 0; i < rt.items.size(); ++i) {
                if (rt.items[i].type == DisplayItemType::DropShadow) has_shadow = true;
                if (rt.items[i].fixed_viewport && rt.items[i].z_index == 50) has_fixed = true;
                if (i > 0 && rt.items[i].z_index < rt.items[i - 1].z_index) z_ordered = false;
            }
            // Fixed button at top:16, right:20 -> x in [1060, 1260], y in [16, 48] MUST still hit when scroll_y = 500!
            const HitRegion* fixed_hit = rt.hit_test(1120.0f, 24.0f, 500.0f);
            expect(rt_ok && has_shadow && has_fixed && z_ordered &&
                   fixed_hit && fixed_hit->fixed_viewport && fixed_hit->target == "#pin",
                   "CSS position:absolute/fixed, z-index layering, box-shadow DropShadow, opacity & ASD3 roundtrip work cleanly");
        }

        // 19. AstraNet v3: TLS 1.3 / ALPN Inspection + Bounded 4-Thread Parallel Subresource Fetcher
        {
            std::error_code ec;
            std::filesystem::remove_all("/tmp/astra_netv3_cache", ec);
            AstraShield sh;
            HttpCache hc("/tmp/astra_netv3_cache", 4 * 1024 * 1024);
            HttpClient net(sh, &hc);
            auto single = net.get("https://example.com", "example.com", true);
            auto batch = net.fetch_batch({
                "https://example.com/",
                "https://doubleclick.net/track.js",
                "https://example.com/?b=2"
            }, "example.com", 4);
            expect(single.status_code == 200 && single.tls_verified &&
                   !single.tls_version.empty() && batch.size() == 3 &&
                   batch[0].status_code == 200 && batch[1].blocked_by_privacy && batch[2].status_code == 200,
                   "AstraNet v3 negotiates TLS 1.3 + ALPN and executes bounded 4-thread parallel fetch_batch safely");
        }

        // 20. AstraJS v4: Real Promise Microtask Chaining (.then/.catch/.finally/Promise.all) + Disk-Persisted localStorage
        {
            std::error_code ec;
            std::filesystem::remove_all("/tmp/astra_origin_storage", ec);
            const std::string ppath = "/tmp/astra_promise_test.html";
            {
                std::ofstream f(ppath);
                f << "<!doctype html><html><body><p id=\"pout\">init</p><script>"
                  << "let prev = localStorage.getItem(\"persist_key\");"
                  << "localStorage.setItem(\"persist_key\", prev ? (prev + \"_2\") : \"v1\");"
                  << "let log = \"S\";"
                  << "setTimeout(function() { log = log + \"_T\"; document.querySelector(\"#pout\").textContent = log + \"|\" + localStorage.getItem(\"persist_key\"); }, 5);"
                  << "Promise.resolve(21).then(function(x) { return x * 2; }).then(function(y) { log = log + \"_P\" + y; });"
                  << "Promise.reject(\"boom\").catch(function(e) { log = log + \"_C:\" + e; }).finally(function() { log = log + \"_F\"; });"
                  << "</script></body></html>";
            }
            app.new_tab("file://" + ppath);
            std::string run1 = app.active_tab()->live_doc->query_selector("#pout")->text_content();
            app.reload_active_tab();
            std::string run2 = app.active_tab()->live_doc->query_selector("#pout")->text_content();
            expect(run1 == "S_C:boom_P42_F_T|v1" && run2 == "S_C:boom_P42_F_T|v1_2",
                   "AstraJS v4 executes Promise .then/.catch/.finally microtasks before timers and persists per-origin localStorage to disk");
        }

        // 21. Multi-Extension MV3 Simultaneous Isolation (2 Extensions + Combined DNR + chrome.storage.local)
        {
            BrowserApp mapp(512ULL * 1024 * 1024);
            std::string e1 = "astra/examples/astra-privacy-guard";
            if (!std::ifstream(e1 + "/manifest.json")) e1 = "examples/astra-privacy-guard";
            std::string e2 = "astra/examples/astra-reader-boost";
            if (!std::ifstream(e2 + "/manifest.json")) e2 = "examples/astra-reader-boost";
            bool ok1 = mapp.load_extension(e1);
            bool ok2 = mapp.load_extension(e2);
            auto dnr1 = mapp.extensions().evaluate_dnr("https://tracker.evil-ads.test/banner", "example.com");
            auto dnr2 = mapp.extensions().evaluate_dnr("https://popup-annoyance.test/modal.js", "example.com");
            const std::string mhtml = "/tmp/astra_multi_ext.html";
            {
                std::ofstream f(mhtml);
                f << "<!doctype html><html><body><div id=\"ext-boost-slot\">none</div></body></html>";
            }
            mapp.new_tab("file://" + mhtml);
            std::string slot_txt = mapp.active_tab()->live_doc->query_selector("#ext-boost-slot")->text_content();
            expect(ok1 && ok2 && mapp.extensions().extensions().size() == 2 &&
                   dnr1.block && dnr2.block && slot_txt == "MV3 ReaderBoost: enabled",
                   "Multi-Extension MV3 runtime loads 2 extensions simultaneously with combined DNR + chrome.storage.local");
        }

        // 22. AstraMedia v3: Native WAV Audio Probe + Bounded PCM Decode (Non-Zero RMS) & Video Scrubber Seek
        {
            std::string wav = "astra/examples/astra_chime.wav";
            if (!std::ifstream(wav)) wav = "examples/astra_chime.wav";
            auto ainfo = MediaEngine::probe_container(wav);
            DecodedAudioBuffer abuf;
            bool decoded = MediaEngine::decode_audio_pcm(wav, 1.0, abuf);
            expect(ainfo.valid && ainfo.has_audio && ainfo.audio_sample_rate == 44100 &&
                   decoded && abuf.valid && abuf.pcm_s16.size() > 10000 && abuf.rms_level > 0.04,
                   "AstraMedia v3 probes WAV audio container and decodes bounded PCM s16le waveform with real non-zero RMS");
        }

        // 23. AstraNet v4: Native HTTP/2 (RFC 7540 Framing + RFC 7541 HPACK + Huffman) & Live TLS 1.3 ALPN
        {
            Http2Frame hdr_f;
            hdr_f.type = Http2FrameType::Headers;
            hdr_f.flags = 0x05;
            hdr_f.stream_id = 1;
            hdr_f.payload = {0x88}; // :status 200 (HPACK static index 8)
            auto packed = Http2Client::pack_frame(hdr_f);
            Http2Frame unpacked;
            size_t consumed = 0;
            bool f_ok = Http2Client::unpack_frame(packed.data(), packed.size(), unpacked, consumed) &&
                        unpacked.type == Http2FrameType::Headers && unpacked.stream_id == 1;
            // Huffman decode test: "www.example.com" = f1 e3 c2 e5 f2 3a 6b a0 ab 90 f4 ff (RFC 7541 C.4.1)
            const uint8_t huff_in[] = {0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff};
            std::string huff_out;
            bool huff_ok = HpackCodec::huffman_decode(huff_in, sizeof(huff_in), huff_out);
            HpackCodec hdec;
            std::vector<std::pair<std::string, std::string>> hlist;
            bool hpack_ok = hdec.decode_headers(unpacked.payload.data(), unpacked.payload.size(), hlist) &&
                            !hlist.empty() && hlist[0].first == ":status" && hlist[0].second == "200";
            // Live HTTPS test (negotiates h2 or http/1.1 cleanly)
            AstraShield h2sh;
            HttpClient h2net(h2sh, nullptr);
            auto live_r = h2net.get("https://example.com", "example.com", true);
            auto live_r2 = h2net.get("https://example.com/", "example.com", true);
            expect(f_ok && huff_ok && huff_out == "www.example.com" && hpack_ok &&
                   live_r.status_code == 200 && live_r.tls_verified && live_r2.status_code == 200 &&
                   (h2net.http2_requests() >= 1 || h2net.keepalive_reuses() >= 1),
                   "AstraNet v4 HTTP/2 RFC 7540 framing + RFC 7541 HPACK/Huffman + live TLS 1.3 ALPN/pool work end-to-end");
        }

        // 24. AstraJS v5: async/await, Arrow Functions (=>), and Shared Mutable Lexical Closures
        {
            SlabArena ja;
            HtmlDocument jdoc = HtmlDocument::parse(ja, "<html><body><div id=\"out\">init</div></body></html>");
            AstraJsEngine jvm;
            std::string js =
                "const makeCounter = (start) => {\n"
                "  let n = start;\n"
                "  return () => { n = n + 1; return n; };\n"
                "};\n"
                "const c1 = makeCounter(10);\n"
                "const c2 = makeCounter(100);\n"
                "const v1 = c1();\n"
                "const v2 = c1();\n"
                "const v3 = c2();\n"
                "async function compute() {\n"
                "  const a = await Promise.resolve(v1 + v2);\n"
                "  const b = await Promise.resolve(v3);\n"
                "  document.querySelector(\"#out\").textContent = a + \":\" + b + \":\" + c1();\n"
                "}\n"
                "compute();\n";
            auto rep = jvm.execute(js, jdoc);
            std::string got = jdoc.query_selector("#out")->text_content();
            expect(rep.ok && got == "23:101:13",
                   "AstraJS v5 executes async/await, arrow functions (=>), and independent mutable lexical closures");
        }

        // 25. AstraJS v5 Dynamic DOM: innerHTML fragment parser, classList, setAttribute/removeAttribute
        {
            SlabArena da;
            HtmlDocument ddoc = HtmlDocument::parse(da, "<html><body><div id=\"box\" class=\"card\" data-x=\"1\"></div></body></html>");
            AstraJsEngine dvm;
            std::string js =
                "let b = document.querySelector(\"#box\");\n"
                "b.classList.add(\"active\");\n"
                "let hasA = b.classList.contains(\"active\");\n"
                "b.classList.toggle(\"card\");\n"
                "b.removeAttribute(\"data-x\");\n"
                "b.setAttribute(\"data-ok\", \"yes\");\n"
                "b.innerHTML = \"<span id='child'>Hello <b>Astra5</b></span>\";\n"
                "let inner = b.innerHTML;\n"
                "let txt = document.querySelector(\"#child\").textContent;\n"
                "let summary = hasA + \"|\" + b.className + \"|\" + b.getAttribute(\"data-x\") + \"|\" + b.getAttribute(\"data-ok\") + \"|\" + txt;\n";
            auto rep = dvm.execute(js, ddoc);
            std::string sum = dvm.globals().count("summary") ? dvm.globals().at("summary").to_string() : "";
            std::string in_h = dvm.globals().count("inner") ? dvm.globals().at("inner").to_string() : "";
            expect(rep.ok && sum == "true|active||yes|Hello Astra5" && in_h.find("<b>Astra5</b>") != std::string::npos,
                   "AstraJS v5 dynamic DOM supports innerHTML getter/setter, classList, and removeAttribute cleanly");
        }

        // 26. CSS @media Responsive Queries + WebRender Per-Item overflow:hidden Clipping (ASD4)
        {
            SlabArena ma;
            HtmlDocument mdoc = HtmlDocument::parse(
                ma,
                "<html><head><style>"
                "body { margin: 0; padding: 0; }"
                ".box { background: #111827; height: 60px; overflow: hidden; }"
                ".child { background: #ef4444; height: 140px; }"
                "@media (max-width: 700px) { .box { background: #0284c7; } }"
                "</style></head><body><div class=\"box\"><div class=\"child\"></div></div></body></html>");
            CssStylesheet msheet;
            msheet.parse(mdoc.collect_stylesheets());
            LayoutFrame* wide_root = LayoutEngine::build_layout(ma, mdoc, msheet, 1280.0f, 600.0f);
            LayoutFrame* narrow_root = LayoutEngine::build_layout(ma, mdoc, msheet, 600.0f, 600.0f);
            uint32_t wide_bg = wide_root->children[0]->style.background.to_argb();
            uint32_t narrow_bg = narrow_root->children[0]->style.background.to_argb();
            DisplayList dl = LayoutEngine::build_display_list(wide_root, 1280, 600);
            auto blob = dl.serialize();
            DisplayList rt;
            bool rt_ok = DisplayList::deserialize(blob, rt);
            bool has_clipped_child = false;
            for (const auto& it : rt.items) {
                if (it.has_clip && it.rect.h >= 120.0f && it.clip_rect.h <= 62.0f) has_clipped_child = true;
            }
            // Verify Framebuffer actually clips pixels outside clip_rect
            Framebuffer cfb(200, 200);
            cfb.composite_display_list(rt, 0, 0.0f);
            // At y=30 (inside 60px clipped box) -> red #ef4444; at y=100 (below 60px) -> clear_color_argb!
            uint32_t inside_px = cfb.pixels()[30 * 200 + 50];
            uint32_t below_px = cfb.pixels()[100 * 200 + 50];
            expect(wide_bg == 0xFF111827u && narrow_bg == 0xFF0284C7u && rt_ok &&
                   has_clipped_child && inside_px == 0xFFEF4444u && below_px == rt.clear_color_argb,
                   "CSS @media queries adapt to viewport width and WebRender ASD4 overflow:hidden clips pixels strictly");
        }

        // 27. Native Download Manager + FIPS 180-4 SHA-256 + astra://downloads Ledger
        {
            std::error_code ec;
            std::filesystem::remove_all("/tmp/astra_downloads_test", ec);
            std::string sha_abc = BrowserApp::compute_sha256_hex("abc");
            std::string data_file = "astra/examples/astra_data.json";
            if (!std::ifstream(data_file)) data_file = "examples/astra_data.json";
            auto rec = app.download_url("file://" + data_file, "/tmp/astra_downloads_test");
            uint32_t dtab = app.new_tab("astra://downloads");
            const Tab* dt = app.active_tab();
            bool has_sha_in_page = dt && dt->raw_html.find(rec.sha256_hex) != std::string::npos;
            expect(sha_abc == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" &&
                   rec.ok && rec.bytes > 20 && std::filesystem::exists(rec.saved_path) &&
                   dtab > 0 && has_sha_in_page,
                   "Native Download Manager saves files with exact FIPS 180-4 SHA-256 checksum and renders astra://downloads");
        }

        // 28. AstraMedia v4: PCM Waveform Peak Analyzer + Dynamic Linux ALSA Audio Probe
        {
            std::string wav = "astra/examples/astra_chime.wav";
            if (!std::ifstream(wav)) wav = "examples/astra_chime.wav";
            DecodedAudioBuffer abuf;
            bool dec = MediaEngine::decode_audio_pcm(wav, 1.0, abuf);
            auto peaks = MediaEngine::compute_waveform_peaks(abuf, 40);
            float max_peak = 0.0f;
            for (float p : peaks) max_peak = std::max(max_peak, p);
            auto alsa_rep = MediaEngine::play_audio_alsa(abuf);
            bool safe_status = (alsa_rep.status == "played" ||
                                alsa_rep.status == "headless_no_soundcard" ||
                                alsa_rep.status == "alsa_not_installed");
            expect(dec && peaks.size() == 40 && max_peak > 0.25f && safe_status,
                   "AstraMedia v4 computes 40-bar PCM waveform peaks and probes Linux ALSA output via dlopen safely");
        }

        // 29. AstraNet v5: True HTTP/2 Multi-Stream Multiplexing over a Single TLS 1.3 Socket
        {
            std::error_code ec;
            std::filesystem::remove_all("/tmp/astra_h2mux_test_cache", ec);
            AstraShield msh;
            HttpCache mhc("/tmp/astra_h2mux_test_cache", 4 * 1024 * 1024);
            HttpClient mnet(msh, &mhc);
            auto batch = mnet.fetch_batch({
                "https://example.com/",
                "https://example.com/?mux=2",
                "https://example.com/?mux=3"
            }, "example.com", 4);
            expect(batch.size() == 3 &&
                   batch[0].status_code == 200 && batch[1].status_code == 200 && batch[2].status_code == 200 &&
                   batch[0].multiplexed_h2 && batch[1].multiplexed_h2 && batch[2].multiplexed_h2 &&
                   mnet.http2_multiplexed_streams() >= 3,
                   "AstraNet v5 multiplexes 3 concurrent HTTP/2 streams over a single TLS 1.3 ALPN h2 connection");
        }

        // 30. AstraJS v6: try/catch/throw Multi-Frame Stack Unwinding + ES6 for..of (Array & String)
        {
            SlabArena ea;
            HtmlDocument edoc = HtmlDocument::parse(ea, "<html><body><div id=\"e\"></div></body></html>");
            AstraJsEngine evm;
            std::string js =
                "function deepThrow(x) { throw \"err:\" + x; }\n"
                "function mid(x) { return deepThrow(x); }\n"
                "let caught = \"none\";\n"
                "let afterTry = false;\n"
                "try { mid(42); } catch (e) { caught = e; } finally { afterTry = true; }\n"
                "let sum = 0;\n"
                "for (let n of [3, 5, 7, 10]) { sum = sum + n; }\n"
                "let chars = \"\";\n"
                "for (let ch of \"ASTRA\") { chars = chars + ch + \"-\"; }\n";
            auto rep = evm.execute(js, edoc);
            std::string caught = evm.globals().count("caught") ? evm.globals().at("caught").to_string() : "";
            double sum = evm.globals().count("sum") ? evm.globals().at("sum").to_number() : 0.0;
            std::string chars = evm.globals().count("chars") ? evm.globals().at("chars").to_string() : "";
            bool fin = evm.globals().count("afterTry") && evm.globals().at("afterTry").truthy();
            expect(rep.ok && caught == "err:42" && fin && sum == 25.0 && chars == "A-S-T-R-A-",
                   "AstraJS v6 unwinds try/catch/finally/throw across call frames and iterates Arrays & Strings via for..of");
        }

        // 31. AstraJS v6: Native HTML5 <canvas> 2D API + requestAnimationFrame Queue
        {
            SlabArena ca;
            HtmlDocument cdoc = HtmlDocument::parse(
                ca, "<html><body><canvas id=\"c1\" width=\"120\" height=\"80\"></canvas><p id=\"st\">0</p></body></html>");
            AstraJsEngine cvm;
            std::string js =
                "let cv = document.querySelector(\"#c1\");\n"
                "let ctx = cv.getContext(\"2d\");\n"
                "ctx.fillStyle = \"#0f172a\"; ctx.fillRect(0, 0, 120, 80);\n"
                "ctx.fillStyle = \"#ef4444\"; ctx.fillRect(10, 10, 30, 30);\n"
                "ctx.fillStyle = \"#3b82f6\"; ctx.beginPath(); ctx.arc(75, 35, 14, 0, Math.PI * 2); ctx.fill();\n"
                "ctx.strokeStyle = \"#38bdf8\"; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(5, 70); ctx.lineTo(115, 70); ctx.stroke();\n"
                "ctx.font = \"bold 12px monospace\"; ctx.fillStyle = \"#ffffff\"; ctx.fillText(\"OK\", 8, 62);\n"
                "let frame = 0;\n"
                "function tick(ts) {\n"
                "  frame = frame + 1;\n"
                "  ctx.fillStyle = \"#22c55e\";\n"
                "  ctx.fillRect(10 + frame * 15, 50, 10, 10);\n"
                "  document.querySelector(\"#st\").textContent = \"frame:\" + frame;\n"
                "  requestAnimationFrame(tick);\n"
                "}\n"
                "requestAnimationFrame(tick);\n";
            auto rep0 = cvm.execute(js, cdoc);
            bool queued = (cvm.pending_raf_count() == 1);
            auto rep1 = cvm.run_animation_frame(16.67, cdoc);
            auto rep2 = cvm.run_animation_frame(33.34, cdoc);
            const CanvasSurface* surf = cvm.find_canvas("c1");
            uint32_t red_px = (surf && surf->argb.size() == 120u * 80u) ? surf->argb[20 * 120 + 20] : 0;
            uint32_t blue_px = (surf && surf->argb.size() == 120u * 80u) ? surf->argb[35 * 120 + 75] : 0;
            uint32_t green_px = (surf && surf->argb.size() == 120u * 80u) ? surf->argb[55 * 120 + 45] : 0;
            std::string st_txt = cdoc.query_selector("#st")->text_content();
            expect(rep0.ok && queued && rep1.raf_run == 1 && rep2.raf_run == 1 &&
                   surf && surf->width == 120 && surf->height == 80 &&
                   red_px == 0xFFEF4444u && blue_px == 0xFF3B82F6u && green_px == 0xFF22C55Eu &&
                   st_txt == "frame:2",
                   "AstraJS v6 renders native HTML5 <canvas> 2D shapes/text and steps requestAnimationFrame callbacks cleanly");
        }

        // 32. CSS Box Sizing Level 3: box-sizing: border-box vs content-box Geometry
        {
            SlabArena ba;
            HtmlDocument bdoc = HtmlDocument::parse(
                ba,
                "<html><head><style>"
                "body { margin: 0; padding: 0; }"
                ".bb { box-sizing: border-box; width: 240px; height: 100px; padding: 20px; border: 4px solid #38bdf8; }"
                ".cb { box-sizing: content-box; width: 240px; height: 100px; padding: 20px; border: 4px solid #38bdf8; }"
                "</style></head><body><div class=\"bb\"></div><div class=\"cb\"></div></body></html>");
            CssStylesheet bsheet;
            bsheet.parse(bdoc.collect_stylesheets());
            LayoutFrame* broot = LayoutEngine::build_layout(ba, bdoc, bsheet, 800.0f, 600.0f);
            bool ok_bs = broot && broot->children.size() == 2 &&
                         std::abs(broot->children[0]->border_box.w - 240.0f) < 0.5f &&
                         std::abs(broot->children[0]->border_box.h - 100.0f) < 0.5f &&
                         std::abs(broot->children[0]->content_box.w - 192.0f) < 0.5f &&
                         std::abs(broot->children[0]->content_box.h - 52.0f) < 0.5f &&
                         std::abs(broot->children[1]->border_box.w - 288.0f) < 0.5f &&
                         std::abs(broot->children[1]->border_box.h - 148.0f) < 0.5f &&
                         std::abs(broot->children[1]->content_box.w - 240.0f) < 0.5f &&
                         std::abs(broot->children[1]->content_box.h - 100.0f) < 0.5f;
            expect(ok_bs,
                   "CSS box-sizing: border-box vs content-box computes exact border_box and content_box dimensions");
        }

        // 33. CSS linear-gradient(...) + ASD5 DisplayList Roundtrip + Per-Pixel Gradient Interpolation
        {
            SlabArena ga;
            HtmlDocument gdoc = HtmlDocument::parse(
                ga,
                "<html><head><style>"
                "body { margin: 0; padding: 0; }"
                ".grad { width: 200px; height: 60px; background: linear-gradient(90deg, #ef4444, #3b82f6); }"
                "</style></head><body><div class=\"grad\"></div></body></html>");
            CssStylesheet gsheet;
            gsheet.parse(gdoc.collect_stylesheets());
            LayoutFrame* groot = LayoutEngine::build_layout(ga, gdoc, gsheet, 400.0f, 200.0f);
            DisplayList gdl = LayoutEngine::build_display_list(groot, 400, 200);
            auto blob = gdl.serialize();
            size_t moff = 0;
            uint32_t magic = 0;
            bool is_asd5 = read_u32(blob, moff, magic) && magic == 0x41534435u;
            DisplayList rt;
            bool rt_ok = DisplayList::deserialize(blob, rt);
            bool has_grad = false;
            for (const auto& it : rt.items) {
                if (it.type == DisplayItemType::FillGradient &&
                    it.color_argb == 0xFFEF4444u && it.color2_argb == 0xFF3B82F6u &&
                    std::abs(it.angle_deg - 90.0f) < 0.5f) {
                    has_grad = true;
                }
            }
            Framebuffer gfb(220, 80);
            gfb.composite_display_list(rt, 0, 0.0f);
            uint32_t left_px = gfb.pixels()[30 * 220 + 5];
            uint32_t mid_px = gfb.pixels()[30 * 220 + 100];
            uint32_t right_px = gfb.pixels()[30 * 220 + 195];
            auto r_ch = [](uint32_t c) { return (c >> 16) & 0xFFu; };
            auto b_ch = [](uint32_t c) { return c & 0xFFu; };
            bool grad_interp = (r_ch(left_px) > 220 && b_ch(left_px) < 85 &&
                                r_ch(mid_px) > 120 && b_ch(mid_px) > 120 &&
                                b_ch(right_px) > 220 && r_ch(right_px) < 85);
            expect(is_asd5 && rt_ok && has_grad && grad_interp,
                   "CSS linear-gradient(90deg) roundtrips in ASD5 DisplayList and rasterizes smooth per-pixel RGB interpolation");
        }

        // 34. AstraNet v6: RFC 6455 WebSocket SHA-1/Base64 Handshake + Real Loopback Echo Server + JS new WebSocket()
        {
            // Canonical RFC 6455 Section 1.3 vector
            std::string canonical_accept = WebSocketClient::compute_accept_key("dGhlIHNhbXBsZSBub25jZQ==");
            bool rfc_vector_ok = (canonical_accept == "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");

            int listen_fd = ::socket(AF_INET, SOCK_STREAM, 0);
            int opt = 1;
            if (listen_fd >= 0) setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
            sockaddr_in addr{};
            addr.sin_family = AF_INET;
            addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
            addr.sin_port = 0; // ephemeral port
            bool bound = (listen_fd >= 0 &&
                          ::bind(listen_fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == 0 &&
                          ::listen(listen_fd, 4) == 0);
            socklen_t alen = sizeof(addr);
            uint16_t port = 0;
            if (bound && ::getsockname(listen_fd, reinterpret_cast<sockaddr*>(&addr), &alen) == 0) {
                port = ntohs(addr.sin_port);
            }

            bool srv_saw_masked_frame = false;
            std::thread srv_thread([&]() {
                if (!bound) return;
                int cfd = ::accept(listen_fd, nullptr, nullptr);
                if (cfd < 0) return;
                std::string req;
                char buf[1024];
                while (req.find("\r\n\r\n") == std::string::npos && req.size() < 4096) {
                    ssize_t n = ::recv(cfd, buf, sizeof(buf), 0);
                    if (n <= 0) break;
                    req.append(buf, static_cast<size_t>(n));
                }
                std::string ws_key;
                size_t kp = req.find("Sec-WebSocket-Key:");
                if (kp != std::string::npos) {
                    size_t vs = kp + 18;
                    while (vs < req.size() && req[vs] == ' ') ++vs;
                    size_t ve = req.find("\r\n", vs);
                    if (ve != std::string::npos) ws_key = req.substr(vs, ve - vs);
                }
                std::string accept_val = WebSocketClient::compute_accept_key(ws_key);
                std::string resp =
                    "HTTP/1.1 101 Switching Protocols\r\n"
                    "Upgrade: websocket\r\n"
                    "Connection: Upgrade\r\n"
                    "Sec-WebSocket-Accept: " + accept_val + "\r\n\r\n";
                (void)::send(cfd, resp.data(), resp.size(), MSG_NOSIGNAL);

                // Serve up to 3 masked client frames (Text -> Echo Text, Ping -> Pong, Close -> exit)
                for (int iter = 0; iter < 4; ++iter) {
                    uint8_t fh[2];
                    if (::recv(cfd, fh, 2, MSG_WAITALL) != 2) break;
                    uint8_t op = fh[0] & 0x0Fu;
                    bool masked = (fh[1] & 0x80u) != 0;
                    size_t plen = fh[1] & 0x7Fu;
                    if (plen >= 126) break;
                    uint8_t mask[4] = {0, 0, 0, 0};
                    if (masked && ::recv(cfd, mask, 4, MSG_WAITALL) != 4) break;
                    std::string pl(plen, '\0');
                    if (plen > 0 && ::recv(cfd, pl.data(), plen, MSG_WAITALL) != static_cast<ssize_t>(plen)) break;
                    if (masked) {
                        srv_saw_masked_frame = true;
                        for (size_t i = 0; i < plen; ++i) pl[i] = static_cast<char>(static_cast<uint8_t>(pl[i]) ^ mask[i & 3u]);
                    }
                    if (op == 0x8) break; // Close
                    std::vector<uint8_t> out;
                    if (op == 0x1) { // Text -> echo prefixed
                        std::string reply = "echo:" + pl;
                        out.push_back(0x81u);
                        out.push_back(static_cast<uint8_t>(reply.size()));
                        out.insert(out.end(), reply.begin(), reply.end());
                    } else if (op == 0x9) { // Ping -> Pong
                        out.push_back(0x8Au);
                        out.push_back(static_cast<uint8_t>(pl.size()));
                        out.insert(out.end(), pl.begin(), pl.end());
                    }
                    if (!out.empty()) (void)::send(cfd, out.data(), out.size(), MSG_NOSIGNAL);
                }
                ::close(cfd);
            });

            SlabArena wsa;
            HtmlDocument wsdoc = HtmlDocument::parse(wsa, "<html><body><p id=\"wsout\">init</p></body></html>");
            AstraJsEngine wsvm;
            std::string ws_script =
                "let sock = new WebSocket(\"ws://127.0.0.1:" + std::to_string(port) + "/stream\");\n"
                "let stateOpen = sock.readyState;\n"
                "let cbMsg = \"\";\n"
                "let closedFlag = false;\n"
                "sock.onmessage = (ev) => { cbMsg = ev.data; };\n"
                "sock.onclose = () => { closedFlag = true; };\n"
                "let sent = sock.send(\"AstraV7\");\n"
                "let r1 = sock.recv(2000);\n"
                "sock.ping(\"hb\");\n"
                "let pong = sock.recv(2000);\n"
                "sock.close();\n"
                "let stateClosed = sock.readyState;\n"
                "document.querySelector(\"#wsout\").textContent = stateOpen + \"|\" + sent + \"|\" + r1 + \"|\" + cbMsg + \"|\" + pong + \"|\" + closedFlag + \"|\" + stateClosed;\n";
            auto wsrep = wsvm.execute(ws_script, wsdoc);
            if (srv_thread.joinable()) srv_thread.join();
            if (listen_fd >= 0) ::close(listen_fd);
            std::string wsout = wsdoc.query_selector("#wsout")->text_content();
            expect(rfc_vector_ok && bound && wsrep.ok && srv_saw_masked_frame &&
                   wsout == "1|true|echo:AstraV7|echo:AstraV7|hb|true|3",
                   "AstraNet v6 RFC 6455 WebSocket handshake, masked framing, ping/pong & JS new WebSocket() work end-to-end");
        }

        // 35. CSS3 Custom Properties (--*/var()), :root Inheritance, Fallback & Direct-Child Combinator (>)
        {
            SlabArena va;
            HtmlDocument vdoc = HtmlDocument::parse(
                va,
                "<html><head><style>"
                ":root { --accent: #38bdf8; --pad: 18px; }"
                ".parent > .item { color: var(--accent); padding: var(--pad); background: var(--missing, #1e293b); }"
                ".scope { --accent: #10b981; }"
                ".scope > .item { color: var(--accent); }"
                "</style></head><body>"
                "<div class=\"parent\"><div class=\"item\" id=\"direct\">Direct</div>"
                "<div class=\"wrap\"><div class=\"item\" id=\"nested\">Nested</div></div></div>"
                "<div class=\"scope\"><div class=\"item\" id=\"scoped\">Scoped</div></div>"
                "</body></html>");
            CssStylesheet vsheet;
            vsheet.parse(vdoc.collect_stylesheets());
            LayoutFrame* vroot = LayoutEngine::build_layout(va, vdoc, vsheet, 800.0f, 400.0f);
            const LayoutFrame* f_direct = nullptr;
            const LayoutFrame* f_nested = nullptr;
            const LayoutFrame* f_scoped = nullptr;
            std::function<void(const LayoutFrame*)> find_frames = [&](const LayoutFrame* f) {
                if (!f) return;
                if (f->node) {
                    std::string id = f->node->get_attr("id");
                    if (id == "direct") f_direct = f;
                    else if (id == "nested") f_nested = f;
                    else if (id == "scoped") f_scoped = f;
                }
                for (const LayoutFrame* c : f->children) find_frames(c);
            };
            find_frames(vroot);
            bool ok_vars = f_direct && f_nested && f_scoped &&
                           f_direct->style.color.to_argb() == 0xFF38BDF8u &&
                           f_direct->style.background.to_argb() == 0xFF1E293Bu &&
                           std::abs(f_direct->style.padding.left - 18.0f) < 0.5f &&
                           f_nested->style.color.to_argb() != 0xFF38BDF8u &&
                           f_scoped->style.color.to_argb() == 0xFF10B981u;
            expect(ok_vars,
                   "CSS3 :root variables (--*/var() with fallback), scoped cascade, and direct-child combinator (>) resolve accurately");
        }

        // 36. CSS3 Flexbox justify-content (space-between/center), align-items:center, text-align & min/max-width
        {
            SlabArena fa;
            HtmlDocument fdoc = HtmlDocument::parse(
                fa,
                "<html><head><style>"
                "body { margin: 0; padding: 0; }"
                ".bar { display: flex; justify-content: space-between; align-items: center; width: 600px; height: 100px; background: #0f172a; }"
                ".a { width: 100px; height: 40px; background: #0ea5e9; }"
                ".b { width: 100px; height: 80px; background: #10b981; }"
                ".clamped { width: 500px; max-width: 260px; min-width: 120px; height: 30px; text-align: center; }"
                "</style></head><body>"
                "<div class=\"bar\"><div class=\"a\">A</div><div class=\"b\">B</div></div>"
                "<div class=\"clamped\">Centered</div>"
                "</body></html>");
            CssStylesheet fsheet;
            fsheet.parse(fdoc.collect_stylesheets());
            LayoutFrame* froot = LayoutEngine::build_layout(fa, fdoc, fsheet, 800.0f, 400.0f);
            bool ok_flex = froot && froot->children.size() == 2 &&
                           froot->children[0]->children.size() == 2 &&
                           std::abs(froot->children[0]->children[0]->border_box.x - 0.0f) < 1.0f &&
                           std::abs(froot->children[0]->children[1]->border_box.x - 500.0f) < 1.0f &&
                           std::abs(froot->children[0]->children[0]->border_box.y - 30.0f) < 1.5f &&
                           std::abs(froot->children[1]->border_box.w - 260.0f) < 1.0f &&
                           froot->children[1]->style.text_align == TextAlign::Center;
            expect(ok_flex,
                   "CSS3 Flexbox justify-content:space-between, align-items:center, text-align & min/max-width compute exact geometry");
        }

        // 37. AstraJS v7: ES6 class / constructor / this / new Instantiation & Template Literals (`...${expr}...`)
        {
            SlabArena cla;
            HtmlDocument cldoc = HtmlDocument::parse(cla, "<html><body><p id=\"cl\">init</p></body></html>");
            AstraJsEngine clvm;
            std::string js =
                "class EngineNode {\n"
                "  constructor(name, threads) {\n"
                "    this.name = name;\n"
                "    this.threads = threads;\n"
                "  }\n"
                "  summary(ver) {\n"
                "    return `${this.name}:v${ver}(${this.threads * 2}q)`;\n"
                "  }\n"
                "}\n"
                "const n1 = new EngineNode(\"AstraNet\", 4);\n"
                "const n2 = new EngineNode(\"AstraJS\", 2);\n"
                "const tag = `${n1.summary(7)} | ${n2.summary(7)}`;\n"
                "document.querySelector(\"#cl\").textContent = tag;\n";
            auto rep = clvm.execute(js, cldoc);
            std::string got = cldoc.query_selector("#cl")->text_content();
            expect(rep.ok && got == "AstraNet:v7(8q) | AstraJS:v7(4q)",
                   "AstraJS v7 compiles and executes ES6 class/constructor/this/new and template literal interpolation cleanly");
        }

        // 38. AstraJS v7: Dynamic element.style.* Inline CSS Proxy -> Layout Engine Reflection
        {
            SlabArena sa;
            HtmlDocument sdoc = HtmlDocument::parse(
                sa, "<html><body><div id=\"dyn\">Dynamic</div></body></html>");
            AstraJsEngine svm;
            std::string js =
                "const el = document.querySelector(\"#dyn\");\n"
                "el.style.backgroundColor = \"#0ea5e9\";\n"
                "el.style.borderRadius = \"14px\";\n"
                "el.style.width = \"320px\";\n"
                "el.style.color = \"#ffffff\";\n"
                "const readBack = el.style.backgroundColor + \"|\" + el.style.width;\n";
            auto rep = svm.execute(js, sdoc);
            std::string rb = svm.globals().count("readBack") ? svm.globals().at("readBack").to_string() : "";
            CssStylesheet ssheet;
            LayoutFrame* sroot = LayoutEngine::build_layout(sa, sdoc, ssheet, 800.0f, 300.0f);
            bool ok_style = rep.ok && rb == "#0ea5e9|320px" &&
                            sroot && !sroot->children.empty() &&
                            sroot->children[0]->style.background.to_argb() == 0xFF0EA5E9u &&
                            sroot->children[0]->style.color.to_argb() == 0xFFFFFFFFu &&
                            std::abs(sroot->children[0]->style.border_radius - 14.0f) < 0.5f &&
                            std::abs(sroot->children[0]->border_box.w - 320.0f) < 1.0f;
            expect(ok_style,
                   "AstraJS v7 element.style.* camelCase proxy mutates inline CSS and reflects directly in LayoutEngine");
        }

        // 39. AstraSVG v1: Native Inline <svg> Vector Parser, 2x2 AA Rasterizer & DisplayList ASD5 Pipeline
        {
            const std::string svg_xml =
                "<svg width=\"120\" height=\"120\" viewBox=\"0 0 120 120\">"
                "<rect x=\"4\" y=\"4\" width=\"112\" height=\"112\" rx=\"12\" fill=\"#0f172a\" stroke=\"#38bdf8\" stroke-width=\"4\"/>"
                "<circle cx=\"60\" cy=\"60\" r=\"28\" fill=\"#10b981\"/>"
                "<path d=\"M20 96 Q60 66 100 96\" stroke=\"#f59e0b\" stroke-width=\"4\" fill=\"none\"/>"
                "</svg>";
            SvgSurface surf = SvgEngine::render_svg(svg_xml, 120, 120);
            uint32_t center_px = surf.valid ? surf.argb[60 * 120 + 60] : 0;
            uint32_t bg_px = surf.valid ? surf.argb[20 * 120 + 20] : 0;
            uint32_t corner_px = surf.valid ? surf.argb[0] : 0xFFFFFFFFu;
            bool direct_ok = surf.valid && surf.width == 120 && surf.height == 120 &&
                             center_px == 0xFF10B981u && bg_px == 0xFF0F172Au && (corner_px >> 24) == 0;

            SlabArena svga;
            HtmlDocument svgdoc = HtmlDocument::parse(
                svga,
                "<html><body style=\"margin:0;padding:0;background:#070b14;\">" + svg_xml + "</body></html>");
            CssStylesheet svgsheet;
            LayoutFrame* svgroot = LayoutEngine::build_layout(svga, svgdoc, svgsheet, 320.0f, 200.0f);
            DisplayList dl = LayoutEngine::build_display_list(svgroot, 320, 200);
            auto blob = dl.serialize();
            DisplayList dl2;
            bool deser_ok = DisplayList::deserialize(blob, dl2);
            bool has_svg_item = false;
            for (const auto& it : dl2.items) {
                if (it.type == DisplayItemType::EmbedSvg && it.text.find("<circle") != std::string::npos) {
                    has_svg_item = true;
                }
            }
            Framebuffer fb(320, 200);
            fb.composite_display_list(dl2, 0.0f);
            uint32_t fb_center = fb.pixels()[60 * 320 + 60];
            expect(direct_ok && deser_ok && has_svg_item && fb_center == 0xFF10B981u,
                   "AstraSVG v1 renders <rect rx>, <circle>, and Bezier <path> through DisplayList ASD5 into Framebuffer");
        }

        // 40. AstraJS v8: ES6 Destructuring, Default Params, Array Spread, Object Shorthand & Functional Stdlib
        {
            SlabArena j8a;
            HtmlDocument j8doc = HtmlDocument::parse(j8a, "<html><body><p id=\"j8\">init</p></body></html>");
            AstraJsEngine j8vm;
            std::string js =
                "const cfg = { engine: \"Astra\", ver: 8, threads: 4 };\n"
                "const { engine, ver: version } = cfg;\n"
                "const nums = [1, 2, 3, 4, 5];\n"
                "const [first, second] = nums;\n"
                "const spread = [0, ...nums, 6];\n"
                "function scale(x, factor = 10) { return x * factor; }\n"
                "const evens = spread.filter((n) => n % 2 === 0 && n > 0);\n"
                "const sum = evens.reduce((acc, n) => acc + n, 0);\n"
                "const found = spread.find((n) => n > 4);\n"
                "const sliced = spread.slice(1, 4).join(\"-\");\n"
                "const rev = [\"a\", \"b\", \"c\"].reverse().join(\"\");\n"
                "const shortObj = { engine, version };\n"
                "const merged = Object.assign({}, shortObj, { ok: true });\n"
                "const keysCount = Object.keys(merged).length;\n"
                "const label = \"astra-core\".replace(\"core\", \"v8\");\n"
                "const strChecks = label.startsWith(\"astra\") && label.endsWith(\"v8\") && (\"x\".repeat(3) === \"xxx\");\n"
                "document.querySelector(\"#j8\").textContent = `${engine}:${version}|${first}+${second}|sum=${sum}|find=${found}|sl=${sliced}|rev=${rev}|sc=${scale(3)}|k=${keysCount}|${label}|${strChecks}`;\n";
            auto rep = j8vm.execute(js, j8doc);
            std::string got = j8doc.query_selector("#j8")->text_content();
            expect(rep.ok && got == "Astra:8|1+2|sum=12|find=5|sl=1-2-3|rev=cba|sc=30|k=3|astra-v8|true",
                   "AstraJS v8 executes ES6 destructuring, default params, array spread, shorthand objects, Object.* & Array/String stdlib");
        }

        // 41. AstraNet v7 + JS fetch(): General REST Methods (POST/PUT/DELETE) & Custom Request Headers
        {
            int listen_fd = ::socket(AF_INET, SOCK_STREAM, 0);
            int opt = 1;
            if (listen_fd >= 0) ::setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
            sockaddr_in addr{};
            addr.sin_family = AF_INET;
            addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
            addr.sin_port = 0;
            bool bound = (listen_fd >= 0 &&
                          ::bind(listen_fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == 0 &&
                          ::listen(listen_fd, 8) == 0);
            socklen_t alen = sizeof(addr);
            uint16_t port = 0;
            if (bound && ::getsockname(listen_fd, reinterpret_cast<sockaddr*>(&addr), &alen) == 0) {
                port = ntohs(addr.sin_port);
            }

            std::thread srv([&]() {
                if (!bound) return;
                // Handle 3 HTTP requests: JS fetch POST, HttpClient PUT, HttpClient DELETE
                for (int i = 0; i < 3; ++i) {
                    int cfd = ::accept(listen_fd, nullptr, nullptr);
                    if (cfd < 0) break;
                    std::string raw;
                    char buf[2048];
                    size_t header_end = std::string::npos;
                    size_t content_len = 0;
                    while (raw.size() < 16384) {
                        ssize_t n = ::recv(cfd, buf, sizeof(buf), 0);
                        if (n <= 0) break;
                        raw.append(buf, static_cast<size_t>(n));
                        if (header_end == std::string::npos) {
                            header_end = raw.find("\r\n\r\n");
                            if (header_end != std::string::npos) {
                                std::string low = raw.substr(0, header_end);
                                for (char& c : low) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
                                size_t clp = low.find("content-length:");
                                if (clp != std::string::npos) {
                                    content_len = static_cast<size_t>(std::strtoull(low.c_str() + clp + 15, nullptr, 10));
                                }
                            }
                        }
                        if (header_end != std::string::npos && raw.size() >= header_end + 4 + content_len) break;
                    }
                    std::string method = raw.substr(0, raw.find(' '));
                    std::string body = (header_end != std::string::npos) ? raw.substr(header_end + 4) : "";
                    std::string sess_hdr;
                    size_t hp = raw.find("X-Astra-Session:");
                    if (hp != std::string::npos) {
                        size_t vs = hp + 16;
                        while (vs < raw.size() && raw[vs] == ' ') ++vs;
                        size_t ve = raw.find("\r\n", vs);
                        if (ve != std::string::npos) sess_hdr = raw.substr(vs, ve - vs);
                    }
                    std::string json = "{\"method\":\"" + method + "\",\"session\":\"" + sess_hdr + "\",\"body\":\"" + body + "\"}";
                    std::string resp =
                        "HTTP/1.1 200 OK\r\n"
                        "Content-Type: application/json\r\n"
                        "Connection: close\r\n"
                        "Content-Length: " + std::to_string(json.size()) + "\r\n\r\n" + json;
                    (void)::send(cfd, resp.data(), resp.size(), MSG_NOSIGNAL);
                    ::close(cfd);
                }
            });

            std::string endpoint = "http://127.0.0.1:" + std::to_string(port) + "/api/items";
            std::string page_path = "/tmp/astra_v8_rest_test.html";
            {
                std::ofstream pf(page_path);
                pf << "<html><body><p id=\"rest\">init</p><script>"
                   << "const r = fetch(\"" << endpoint << "\", {"
                   << "  method: \"POST\","
                   << "  headers: { \"X-Astra-Session\": \"v8-alpha\", \"Content-Type\": \"text/plain\" },"
                   << "  body: \"hello-v8\""
                   << "});"
                   << "const data = r.json();"
                   << "document.querySelector(\"#rest\").textContent = `${r.status}|${data.method}|${data.session}|${data.body}`;"
                   << "</script></body></html>";
            }
            uint32_t rest_tid = app.new_tab("file://" + page_path);
            (void)rest_tid;
            std::string rest_out;
            for (const auto& t : app.tabs()) {
                if (t.id == rest_tid && t.live_doc) {
                    const DomNode* p = t.live_doc->query_selector("#rest");
                    if (p) rest_out = p->text_content();
                }
            }

            AstraShield rest_shield;
            HttpClient raw_http(rest_shield);
            auto put_resp = raw_http.request("PUT", endpoint, "upd=1", {{"X-Astra-Session", "v8-put"}}, "text/plain");
            auto del_resp = raw_http.request("DELETE", endpoint, "", {{"X-Astra-Session", "v8-del"}}, "text/plain");

            if (srv.joinable()) srv.join();
            if (listen_fd >= 0) ::close(listen_fd);

            bool ok_rest = bound &&
                           rest_out == "200|POST|v8-alpha|hello-v8" &&
                           put_resp.status_code == 200 && put_resp.body.find("\"method\":\"PUT\"") != std::string::npos &&
                           put_resp.body.find("\"session\":\"v8-put\"") != std::string::npos &&
                           del_resp.status_code == 200 && del_resp.body.find("\"method\":\"DELETE\"") != std::string::npos &&
                           del_resp.body.find("\"session\":\"v8-del\"") != std::string::npos;
            expect(ok_rest,
                   "AstraNet v7 & JS fetch() deliver POST/PUT/DELETE methods, custom headers & request bodies end-to-end");
        }

        // 42. CSS3 Level 8: :first-child/:last-child/:focus, flex-wrap:wrap, border-left & transform:translate()
        {
            SlabArena c8a;
            HtmlDocument c8doc = HtmlDocument::parse(
                c8a,
                "<html><head><style>"
                "body { margin: 0; padding: 0; }"
                ".wrap { display: flex; flex-wrap: wrap; gap: 10px; width: 250px; }"
                ".chip { width: 110px; height: 40px; background: #1e293b; color: #cbd5e1; }"
                ".chip:first-child { background: #0284c7; border-left: 4px solid #38bdf8; }"
                ".chip:last-child { background: #065f46; transform: translate(15px, 10px); }"
                "</style></head><body>"
                "<div class=\"wrap\">"
                "<div class=\"chip\">One</div>"
                "<div class=\"chip\">Two</div>"
                "<div class=\"chip\">Three</div>"
                "</div></body></html>");
            CssStylesheet c8sheet;
            c8sheet.parse(c8doc.collect_stylesheets());
            LayoutFrame* c8root = LayoutEngine::build_layout(c8a, c8doc, c8sheet, 400.0f, 300.0f);
            bool ok_c8 = false;
            if (c8root && !c8root->children.empty() && c8root->children[0]->children.size() == 3) {
                const LayoutFrame* f0 = c8root->children[0]->children[0];
                const LayoutFrame* f1 = c8root->children[0]->children[1];
                const LayoutFrame* f2 = c8root->children[0]->children[2];
                // Row 1: f0 (x=0, y=0) + f1 (x=120, y=0) = 230px <= 250px.
                // Row 2: f2 wraps to y=50px, then transform:translate(15px, 10px) -> x=15px, y=60px.
                DisplayList c8dl = LayoutEngine::build_display_list(c8root, 400, 300);
                bool has_accent_bar = false;
                for (const auto& it : c8dl.items) {
                    if (it.type == DisplayItemType::FillRect && it.color_argb == 0xFF38BDF8u &&
                        std::abs(it.rect.w - 4.0f) < 0.5f) {
                        has_accent_bar = true;
                    }
                }
                ok_c8 = f0->style.background.to_argb() == 0xFF0284C7u &&
                        f1->style.background.to_argb() == 0xFF1E293Bu &&
                        f2->style.background.to_argb() == 0xFF065F46u &&
                        std::abs(f0->border_box.x - 0.0f) < 1.0f &&
                        std::abs(f1->border_box.x - 120.0f) < 1.0f &&
                        std::abs(f2->border_box.x - 15.0f) < 1.0f &&
                        std::abs(f2->border_box.y - 60.0f) < 1.5f &&
                        has_accent_bar;
            }
            expect(ok_c8,
                   "CSS3 Level 8 resolves :first-child/:last-child, flex-wrap multi-row layout, border-left accent & transform:translate()");
        }

        // 43. End-to-End Session 8 Showcase (astra_session8.html): Inline SVG + Flex-Wrap + ES6 JS + Render
        {
            std::string showcase_path = "astra/examples/astra_session8.html";
            if (!std::filesystem::exists(showcase_path)) showcase_path = "examples/astra_session8.html";
            uint32_t s8_tid = app.new_tab("file://" + showcase_path);
            bool has_svg = false;
            std::string js_summary;
            for (const auto& t : app.tabs()) {
                if (t.id == s8_tid) {
                    for (const auto& it : t.cached_display_list.items) {
                        if (it.type == DisplayItemType::EmbedSvg) has_svg = true;
                    }
                    if (t.live_doc) {
                        if (const DomNode* el = t.live_doc->query_selector("#s8-js-out")) {
                            js_summary = el->text_content();
                        }
                    }
                }
            }
            expect(has_svg && js_summary.find("v8") != std::string::npos && js_summary.find("48") != std::string::npos,
                   "End-to-end astra_session8.html renders inline <svg>, flex-wrap CSS3 & ES6 AstraJS v8 pipeline");
        }

        // 44. Hito A — Real Web Layout v9: <table> + colspan, float/clear, inline-block, calc(), :nth-child, ::before/::after, <iframe srcdoc>
        {
            SlabArena a9;
            HtmlDocument d9 = HtmlDocument::parse(
                a9,
                "<html><head><style>"
                "body { margin: 0; padding: 0; }"
                ".side { float: right; width: 200px; height: 80px; background: #1e293b; }"
                ".main { width: calc(100% - 200px); background: #0f172a; }"
                ".hdr::before { content: \"PRE \"; color: #38bdf8; }"
                ".hdr::after { content: \" POST\"; color: #34d399; }"
                ".ib { display: inline-block; width: 90px; height: 24px; background: #1d4ed8; }"
                "table { width: 400px; }"
                "tr:nth-child(even) { background: #132038; }"
                ".after { clear: both; height: 20px; }"
                "</style></head><body>"
                "<div class=\"side\">FloatBox</div>"
                "<div class=\"main\">"
                "  <div class=\"hdr\">Title</div>"
                "  <span class=\"ib\">A</span><span class=\"ib\">B</span>"
                "  <table><tr><th>H1</th><th>H2</th></tr>"
                "         <tr><td>R1C1</td><td>R1C2</td></tr>"
                "         <tr><td colspan=\"2\">Span2</td></tr></table>"
                "</div>"
                "<div class=\"after\">Cleared</div>"
                "<iframe srcdoc=\"<html><body><p>NestedDoc</p></body></html>\" style=\"width:240px;height:60px;\"></iframe>"
                "</body></html>");
            CssStylesheet s9;
            s9.parse(d9.collect_stylesheets());
            LayoutFrame* r9 = LayoutEngine::build_layout(a9, d9, s9, 800.0f, 600.0f);
            DisplayList dl9 = LayoutEngine::build_display_list(r9, 800, 600);

            bool ok_float_x = false, ok_calc_w = false, ok_clear_y = false;
            bool ok_inline_block = false, ok_table_cols = false, ok_nth_even = false;
            bool ok_before = false, ok_after = false, ok_iframe = false;

            if (r9 && r9->children.size() >= 4) {
                const LayoutFrame* f_side = r9->children[0];
                const LayoutFrame* f_main = r9->children[1];
                const LayoutFrame* f_after = r9->children[2];
                ok_float_x = (f_side->border_box.x >= 595.0f && f_side->border_box.w >= 195.0f);
                ok_calc_w = (std::abs(f_main->border_box.w - 600.0f) < 2.0f);
                ok_clear_y = (f_after->border_box.y >= f_side->border_box.y + f_side->border_box.h - 1.0f);

                // Check inline-block siblings side-by-side and table columns
                const LayoutFrame* ib1 = nullptr;
                const LayoutFrame* ib2 = nullptr;
                const LayoutFrame* tbl = nullptr;
                for (const LayoutFrame* ch : f_main->children) {
                    if (ch->style.display == DisplayType::InlineBlock) {
                        if (!ib1) ib1 = ch;
                        else if (!ib2) ib2 = ch;
                    }
                    if (ch->node && ch->node->tag == "table") tbl = ch;
                }
                if (ib1 && ib2) {
                    ok_inline_block = (ib2->border_box.x > ib1->border_box.x + 50.0f &&
                                       std::abs(ib1->border_box.y - ib2->border_box.y) < 2.0f);
                }
                if (tbl) {
                    std::vector<const LayoutFrame*> rows;
                    std::function<void(const LayoutFrame*)> gather_rows = [&](const LayoutFrame* n) {
                        if (!n) return;
                        if (n->style.display == DisplayType::TableRow) rows.push_back(n);
                        for (const LayoutFrame* c : n->children) gather_rows(c);
                    };
                    gather_rows(tbl);
                    if (rows.size() == 3 && rows[1]->children.size() == 2 && rows[2]->children.size() == 1) {
                        const LayoutFrame* c0 = rows[1]->children[0];
                        const LayoutFrame* c1 = rows[1]->children[1];
                        const LayoutFrame* span2 = rows[2]->children[0];
                        ok_table_cols = (c1->border_box.x > c0->border_box.x + 80.0f) &&
                                        (span2->border_box.w > c0->border_box.w * 1.5f);
                        ok_nth_even = (rows[1]->style.background.to_argb() == 0xFF132038u);
                    }
                }
            }
            for (const auto& it : dl9.items) {
                if (it.type == DisplayItemType::DrawText) {
                    if (it.text.find("PRE") != std::string::npos) ok_before = true;
                    if (it.text.find("POST") != std::string::npos) ok_after = true;
                    if (it.text.find("NestedDoc") != std::string::npos) ok_iframe = true;
                }
            }
            bool all44 = ok_float_x && ok_calc_w && ok_clear_y && ok_inline_block &&
                         ok_table_cols && ok_nth_even && ok_before && ok_after && ok_iframe;
            if (!all44) {
                std::cerr << "    [diag44] float=" << ok_float_x << " calc=" << ok_calc_w
                          << " clear=" << ok_clear_y << " ib=" << ok_inline_block
                          << " tbl=" << ok_table_cols << " nth=" << ok_nth_even
                          << " bef=" << ok_before << " aft=" << ok_after << " ifr=" << ok_iframe << "\n";
            }
            expect(all44,
                   "Hito A: CSS3 Layout v9 handles <table>+colspan, float/clear, inline-block, calc(), :nth-child, ::before/::after & <iframe srcdoc>");
        }

        // 45. Hito A — AstraImage v3: Pure-C++20 GIF89a LZW Decoder (with Transparency) & WebP RIFF Probe
        {
            // Build a spec-valid 4x2 GIF89a image with 4-color GCT, transparent index=3, min_code_size=4 (5-bit codes)
            std::vector<uint8_t> gif = {
                'G','I','F','8','9','a',
                4, 0, 2, 0, 0x91, 0, 0,
                255, 0, 0,     // 0: Red
                0, 255, 0,     // 1: Green
                0, 0, 255,     // 2: Blue
                255, 255, 0,   // 3: Transparent
                0x21, 0xF9, 0x04, 0x01, 0x00, 0x00, 0x03, 0x00,
                0x2C, 0, 0, 0, 0, 4, 0, 2, 0, 0x00,
                4 // LZW minimum code size = 4 (clear=16, EOI=17, code width=5 bits)
            };
            const int codes[] = {16, 0, 1, 2, 3, 2, 1, 0, 3, 17};
            std::vector<uint8_t> lzw_bytes;
            uint32_t bitbuf = 0;
            int bitcnt = 0;
            for (int c : codes) {
                bitbuf |= (static_cast<uint32_t>(c) << bitcnt);
                bitcnt += 5;
                while (bitcnt >= 8) {
                    lzw_bytes.push_back(static_cast<uint8_t>(bitbuf & 0xFFu));
                    bitbuf >>= 8;
                    bitcnt -= 8;
                }
            }
            if (bitcnt > 0) lzw_bytes.push_back(static_cast<uint8_t>(bitbuf & 0xFFu));
            gif.push_back(static_cast<uint8_t>(lzw_bytes.size()));
            gif.insert(gif.end(), lzw_bytes.begin(), lzw_bytes.end());
            gif.push_back(0x00); // block terminator
            gif.push_back(0x3B); // GIF trailer

            DecodedImage gimg = ImageDecoder::decode_bytes(gif.data(), gif.size());
            bool ok_gif = gimg.valid && gimg.width == 4 && gimg.height == 2 && gimg.argb.size() == 8 &&
                          gimg.argb[0] == 0xFFFF0000u &&
                          gimg.argb[1] == 0xFF00FF00u &&
                          gimg.argb[2] == 0xFF0000FFu &&
                          (gimg.argb[3] >> 24) == 0u &&
                          gimg.argb[4] == 0xFF0000FFu &&
                          gimg.argb[5] == 0xFF00FF00u &&
                          gimg.argb[6] == 0xFFFF0000u &&
                          (gimg.argb[7] >> 24) == 0u;

            // Minimal RIFF WEBP VP8X header probe (16x8 canvas)
            std::vector<uint8_t> webp = {
                'R','I','F','F', 22, 0, 0, 0,
                'W','E','B','P',
                'V','P','8','X', 10, 0, 0, 0,
                0, 0, 0, 0,
                15, 0, 0, // width - 1 = 15 -> 16
                7, 0, 0   // height - 1 = 7 -> 8
            };
            DecodedImage wimg = ImageDecoder::decode_bytes(webp.data(), webp.size());
            bool ok_webp = wimg.format == "WEBP" && wimg.width == 16 && wimg.height == 8;
            if (!ok_gif || !ok_webp) {
                std::cerr << "    [diag45] gif=" << ok_gif << " (" << gimg.valid << "," << gimg.width << "x" << gimg.height << "," << gimg.error << ") webp=" << ok_webp << " (" << wimg.width << "x" << wimg.height << "," << wimg.error << ")\n";
            }
            expect(ok_gif && ok_webp,
                   "Hito A: AstraImage v3 decodes GIF89a LZW (with transparency) & RIFF WebP frames");
        }

        // 46. Hito A — Native HTML <form method="GET"> Submission + URL-Encoding
        {
            std::string target_path = "/tmp/astra_v9_form_target.html";
            std::string form_path = "/tmp/astra_v9_form_page.html";
            {
                std::ofstream tf(target_path);
                tf << "<html><head><title>Search Results</title></head><body><h1>Results</h1></body></html>";
            }
            {
                std::ofstream ff(form_path);
                ff << "<html><head><title>Search Form</title></head><body>"
                   << "<form action=\"file://" << target_path << "\" method=\"GET\">"
                   << "<input id=\"q\" name=\"q\" value=\"\" style=\"width:240px;height:32px;\" />"
                   << "<button id=\"sub\" type=\"submit\">Go</button>"
                   << "</form></body></html>";
            }
            uint32_t ftid = app.new_tab("file://" + form_path);
            app.switch_tab(ftid);
            bool clicked_input = false;
            if (const Tab* t = app.active_tab()) {
                for (const auto& hr : t->cached_display_list.hit_regions) {
                    if (hr.kind == HitRegionKind::Input) {
                        int cx = static_cast<int>(hr.rect.x + hr.rect.w * 0.5f);
                        int cy = Framebuffer::kChromeHeight + static_cast<int>(hr.rect.y + hr.rect.h * 0.5f);
                        auto cr = app.click_at(cx, cy);
                        clicked_input = cr.handled && cr.action == "focus_input";
                        break;
                    }
                }
            }
            app.type_text("astra v9");
            bool entered = app.press_enter();
            std::string final_u = app.active_tab() ? app.active_tab()->url : "";
            expect(clicked_input && entered &&
                   final_u == ("file://" + target_path + "?q=astra%20v9"),
                   "Hito A: HTML <form method=\"GET\"> collects <input name>, URL-encodes query & navigates on Enter");
        }

        // 47. Hito B — AstraJS v9: Real [[Prototype]] Chain, class extends, instanceof, RegExp, Date, Map & Set
        {
            SlabArena j9a;
            HtmlDocument j9doc = HtmlDocument::parse(j9a, "<html><body><div id=\"out\"></div></body></html>");
            AstraJsEngine vm9;
            auto rep = vm9.execute(
                "function Animal(n) { this.name = n; }\n"
                "Animal.prototype.speak = function() { return this.name + '!'; };\n"
                "const a = new Animal('Fox');\n"
                "const child = Object.create(a);\n"
                "const pOk = (Object.getPrototypeOf(child) === a) && (child.speak() === 'Fox!') && (a instanceof Animal);\n"
                "class Base { constructor(x) { this.x = x; } baseVal() { return this.x * 2; } }\n"
                "class Sub extends Base { constructor(x) { super(x); } subVal() { return this.baseVal() + 7; } }\n"
                "const s = new Sub(10);\n"
                "const cOk = (s instanceof Sub) && (s instanceof Base) && (s.subVal() === 27);\n"
                "const re = /([a-z]+)-(\\d+)/i;\n"
                "const m = re.exec('Core ASTRA-900 ok');\n"
                "const rOk = re.test('foo-42') && m[1] === 'ASTRA' && m[2] === '900' && "
                "  'x1y2z3'.replace(/\\d/g, '_') === 'x_y_z_' && 'a,b;c'.split(/[,;]/).join('|') === 'a|b|c';\n"
                "const dt = new Date('2026-11-25T10:30:45Z');\n"
                "const dOk = dt.getUTCFullYear() === 2026 && dt.getUTCMonth() === 10 && dt.getUTCDate() === 25 && "
                "  dt.toISOString() === '2026-11-25T10:30:45.000Z' && Date.now() > 1700000000000;\n"
                "const mp = new Map();\n"
                "mp.set('k1', 10); mp.set('k2', 20); mp.set('k1', 30);\n"
                "let msum = 0; for (const v of mp.values()) { msum = msum + v; }\n"
                "const st = new Set(); st.add('a'); st.add('b'); st.add('a');\n"
                "let sjoin = ''; for (const v of st) { sjoin = sjoin + v; }\n"
                "const collOk = mp.size === 2 && mp.get('k1') === 30 && msum === 50 && st.size === 2 && st.has('b') && sjoin === 'ab';\n"
                "document.querySelector('#out').textContent = `${pOk}|${cOk}|${rOk}|${dOk}|${collOk}`;\n",
                j9doc);
            std::string out = j9doc.query_selector("#out") ? j9doc.query_selector("#out")->text_content() : "";
            if (!rep.ok || out != "true|true|true|true|true") {
                std::cerr << "    [diag47] rep.ok=" << rep.ok << " err=" << rep.error << " out='" << out << "'\n";
            }
            expect(rep.ok && out == "true|true|true|true|true",
                   "Hito B: AstraJS v9 executes [[Prototype]] chains, class extends, instanceof, RegExp, Date, Map & Set");
        }

        // 48. Hito B — SPA Runtime: history.pushState/replaceState, location.* & sessionStorage
        {
            std::string spa_path = "/tmp/astra_v9_spa_test.html";
            {
                std::ofstream sf(spa_path);
                sf << "<html><body><div id=\"spa\">init</div><script>"
                   << "sessionStorage.setItem('token', 'spa-ok');"
                   << "history.pushState({ page: 2 }, '', '/docs/v9?tab=api#top');"
                   << "const st = history.state;"
                   << "document.querySelector('#spa').textContent = "
                   << "  location.pathname + '|' + location.search + '|' + location.hash + '|' + "
                   << "  history.length + '|' + st.page + '|' + sessionStorage.getItem('token');"
                   << "</script></body></html>";
            }
            uint32_t stid = app.new_tab("file://" + spa_path);
            app.switch_tab(stid);
            std::string spa_out, tab_url;
            if (const Tab* t = app.active_tab()) {
                tab_url = t->url;
                if (t->live_doc && t->live_doc->query_selector("#spa")) {
                    spa_out = t->live_doc->query_selector("#spa")->text_content();
                }
            }
            expect(spa_out == "/docs/v9|?tab=api|#top|2|2|spa-ok" &&
                   tab_url.find("/docs/v9?tab=api#top") != std::string::npos,
                   "Hito B: SPA history.pushState, location.*, and sessionStorage update VM and BrowserApp tab URL");
        }

        // 49. Hito C — Real Kernel seccomp-BPF Sandbox + CSP + Mixed Content + SOP/CORS + SameSite Cookies + Site Isolation
        {
            std::string seccomp_detail;
            bool ok_seccomp = ProcessSandbox::self_verify_seccomp(&seccomp_detail);

            // CSP inline-script & connect-src enforcement
            ContentSecurityPolicy csp;
            csp.parse("default-src 'self'; script-src 'self'; connect-src 'self' https://*.trusted.example");
            SecurityOrigin doc_org = SecurityOrigin::from_url("https://app.example.com/index.html");
            bool ok_csp = !csp.allows_inline_script() &&
                          csp.allows_url(ResourceKind::Connect, "https://app.example.com/api", doc_org) &&
                          csp.allows_url(ResourceKind::Connect, "https://api.trusted.example/v1", doc_org) &&
                          !csp.allows_url(ResourceKind::Connect, "https://evil.attacker.org/exfil", doc_org);

            // CSP <meta> enforcement inside BrowserApp tab (blocks inline script)
            std::string csp_page = "/tmp/astra_v9_csp_test.html";
            {
                std::ofstream cf(csp_page);
                cf << "<html><head><meta http-equiv=\"Content-Security-Policy\" content=\"script-src 'none'\"></head>"
                   << "<body><p id=\"v\">SAFE</p><script>document.querySelector('#v').textContent='PWNED';</script></body></html>";
            }
            uint32_t csp_tid = app.new_tab("file://" + csp_page);
            std::string csp_text;
            size_t csp_blocks = 0;
            for (const auto& t : app.tabs()) {
                if (t.id == csp_tid) {
                    csp_blocks = t.csp_blocks;
                    if (t.live_doc && t.live_doc->query_selector("#v")) {
                        csp_text = t.live_doc->query_selector("#v")->text_content();
                    }
                }
            }
            bool ok_csp_dom = (csp_text == "SAFE" && csp_blocks >= 1);

            // Mixed Content blocking (blocks http:// & ws:// active resources on https://)
            bool ok_mixed = MixedContentBlocker::is_blocked(doc_org, "http://insecure.example/script.js", ResourceKind::Script) &&
                            MixedContentBlocker::is_blocked(doc_org, "ws://insecure.example/live", ResourceKind::Connect) &&
                            !MixedContentBlocker::is_blocked(doc_org, "https://cdn.example/script.js", ResourceKind::Script);

            // SOP / CORS Access-Control-Allow-Origin enforcement
            std::map<std::string, std::string> no_cors_hdrs{{"content-type", "application/json"}};
            std::map<std::string, std::string> ok_cors_hdrs{{"Access-Control-Allow-Origin", "https://app.example.com"}};
            bool ok_cors = !CorsEnforcer::allows_response(doc_org, "https://other.example.net/data", no_cors_hdrs) &&
                           CorsEnforcer::allows_response(doc_org, "https://other.example.net/data", ok_cors_hdrs) &&
                           CorsEnforcer::allows_response(doc_org, "https://app.example.com/same", no_cors_hdrs);

            // SameSite=Strict / Lax / None cookie enforcement in AstraShield
            AstraShield ss_shield;
            ss_shield.store_cookie("bank.example", "https://bank.example", "sid=strict1; SameSite=Strict");
            ss_shield.store_cookie("bank.example", "https://bank.example", "bad=none_no_sec; SameSite=None");
            ss_shield.store_cookie("evil.example", "https://bank.example", "sid=strict1; SameSite=Strict");
            ss_shield.store_cookie("evil.example", "https://bank.example", "lax=lax1; SameSite=Lax");
            ss_shield.store_cookie("evil.example", "https://bank.example", "none=ok1; SameSite=None; Secure");
            std::string same_site_hdr = ss_shield.cookie_header_for("bank.example", "https://bank.example", true, true);
            std::string cross_top_hdr = ss_shield.cookie_header_for("evil.example", "https://bank.example", true, true);
            std::string cross_sub_hdr = ss_shield.cookie_header_for("evil.example", "https://bank.example", true, false);
            bool ok_samesite = (same_site_hdr.find("sid=strict1") != std::string::npos) &&
                               (same_site_hdr.find("bad=") == std::string::npos) &&
                               (cross_top_hdr.find("sid=strict1") == std::string::npos) &&
                               (cross_top_hdr.find("lax=lax1") != std::string::npos) &&
                               (cross_sub_hdr.find("lax=lax1") == std::string::npos) &&
                               (cross_sub_hdr.find("none=ok1") != std::string::npos);

            // Per-origin Site Isolation worker spawning for distinct sites
            WorkerProcess* w_a = app.acquire_content_worker_for_origin(SecurityOrigin::from_url("https://alpha.example/"));
            WorkerProcess* w_b = app.acquire_content_worker_for_origin(SecurityOrigin::from_url("https://beta.example/"));
            bool ok_site_iso = (w_a != nullptr && w_b != nullptr && w_a->pid() != w_b->pid() &&
                                app.site_isolated_worker_count() >= 3);

            bool all49 = ok_seccomp && ok_csp && ok_csp_dom && ok_mixed && ok_cors && ok_samesite && ok_site_iso;
            if (!all49) {
                std::cerr << "    [diag49] seccomp=" << ok_seccomp << "(" << seccomp_detail << ")"
                          << " csp=" << ok_csp << " csp_dom=" << ok_csp_dom
                          << " mixed=" << ok_mixed << " cors=" << ok_cors
                          << " samesite=" << ok_samesite << " site_iso=" << ok_site_iso << "\n";
            }
            expect(all49,
                   "Hito C: Kernel seccomp-BPF sandbox (EPERM verified), CSP, Mixed Content, CORS/SOP, SameSite & Site Isolation");
        }

        // 50. Hito C — Adversarial Parser Fuzz-Resistance Harness + End-to-End astra_session9.html
        {
            // Hammer HTML, CSS, Image (PNG/JPEG/GIF/WebP), SVG, and HPACK parsers with hostile/truncated payloads
            const std::vector<std::string> hostile_inputs = {
                "",
                std::string(2048, '<'),
                "<table><tr><td colspan=\"999999999\"><div><span><iframe srcdoc=\"<iframe srcdoc='<iframe srcdoc=x>'>\">",
                "@media ((((((( { .a:nth-child(999999n-999999)::before { width: calc((((100% / 0) + 1e38px)); content: \"unterminated",
                std::string("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\xff\xff\xff\xff\xff\xff\xff\xff", 24),
                std::string("\xff\xd8\xff\xc0\x00\x11\x08\xff\xff\xff\xff\x03\x01\x22\x00", 15),
                std::string("GIF89a\xff\xff\xff\xff\xf7\x00\x00\x2c\x00\x00\x00\x00\xff\xff\xff\xff\x00\x0c\x04\xff\xff\xff\xff", 29),
                std::string("RIFF\x10\x00\x00\x00WEBPVP8X\x0a\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff", 30),
                "<svg viewBox=\"0 0 0 0\"><path d=\"M 1e99 1e99 C 0 0 0 0 0 0 Q Z L H V 9999999\" /><circle r=\"-50\"/></svg>"
            };
            uint32_t rng = 0xDEADBEEFu;
            for (int iter = 0; iter < 40; ++iter) {
                std::string rnd(96, '\0');
                for (char& c : rnd) {
                    rng = rng * 1664525u + 1013904223u;
                    c = static_cast<char>(rng >> 24);
                }
                SlabArena fa(16 * 1024);
                (void)HtmlDocument::parse(fa, rnd);
                CssStylesheet fs;
                fs.parse(rnd);
                (void)ImageDecoder::decode_bytes(reinterpret_cast<const uint8_t*>(rnd.data()), rnd.size());
                (void)SvgEngine::render_svg(rnd, 32, 32);
                HpackCodec hp;
                std::vector<std::pair<std::string, std::string>> h_out;
                (void)hp.decode_headers(reinterpret_cast<const uint8_t*>(rnd.data()), rnd.size(), h_out);
            }
            for (const auto& payload : hostile_inputs) {
                SlabArena fa(32 * 1024);
                HtmlDocument fd = HtmlDocument::parse(fa, payload);
                CssStylesheet fs;
                fs.parse(payload);
                (void)LayoutEngine::build_layout(fa, fd, fs, 800.0f, 600.0f);
                (void)ImageDecoder::decode_bytes(reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
                (void)SvgEngine::render_svg(payload, 64, 64);
                HpackCodec hp;
                std::vector<std::pair<std::string, std::string>> h_out;
                (void)hp.decode_headers(reinterpret_cast<const uint8_t*>(payload.data()), payload.size(), h_out);
            }

            // End-to-end verification of astra_session9.html
            std::string s9_path = "astra/examples/astra_session9.html";
            if (!std::filesystem::exists(s9_path)) s9_path = "examples/astra_session9.html";
            uint32_t s9_tid = app.new_tab("file://" + s9_path);
            std::string s9_proto, s9_regex, s9_colls, s9_route;
            bool has_pseudo = false, has_iframe_txt = false;
            for (const auto& t : app.tabs()) {
                if (t.id == s9_tid) {
                    if (t.live_doc) {
                        if (auto* n = t.live_doc->query_selector("#spa-proto")) s9_proto = n->text_content();
                        if (auto* n = t.live_doc->query_selector("#spa-regex")) s9_regex = n->text_content();
                        if (auto* n = t.live_doc->query_selector("#spa-colls")) s9_colls = n->text_content();
                        if (auto* n = t.live_doc->query_selector("#spa-route")) s9_route = n->text_content();
                    }
                    for (const auto& it : t.cached_display_list.items) {
                        if (it.type == DisplayItemType::DrawText) {
                            if (it.text.find("SECCOMP-BPF") != std::string::npos) has_pseudo = true;
                            if (it.text.find("Embedded srcdoc") != std::string::npos) has_iframe_txt = true;
                        }
                    }
                }
            }
            bool ok_s9 = s9_proto.find("astra-core@v9") != std::string::npos &&
                         s9_proto.find("instanceof=true") != std::string::npos &&
                         s9_regex.find("v9 (a#b#c#)") != std::string::npos &&
                         s9_regex.find("2026-11-25") != std::string::npos &&
                         s9_colls.find("Map(3)") != std::string::npos &&
                         s9_colls.find("Set(2)=true") != std::string::npos &&
                         s9_route.find("/wiki/astra-v9?milestone=all") != std::string::npos &&
                         s9_route.find("v9-verified") != std::string::npos &&
                         has_pseudo && has_iframe_txt;

            if (!ok_s9) {
                std::cerr << "    [diag50] proto='" << s9_proto << "' regex='" << s9_regex
                          << "' colls='" << s9_colls << "' route='" << s9_route
                          << "' pseudo=" << has_pseudo << " ifr=" << has_iframe_txt << "\n";
            }
            expect(ok_s9,
                   "Hito C + E2E: Fuzz harness survives 49 hostile HTML/CSS/Image/SVG/HPACK payloads & astra_session9.html renders cleanly");
        }

        app.stop_workers();
    }

    std::cout << "\nSummary: " << g_tests_passed << " passed, " << g_tests_failed << " failed.\n";
    return g_tests_failed == 0 ? 0 : 1;
}

int run_stress_test(int tab_count, uint64_t ceiling_mb) {
    std::cout << "=== Astra v9 Multi-Tab Stress Test (" << tab_count << " tabs, ceiling " << ceiling_mb << " MB) ===\n";
    BrowserApp app(ceiling_mb * 1024ULL * 1024ULL);
    app.start_workers();

    auto t0 = std::chrono::steady_clock::now();

    std::ostringstream payload;
    payload << "<!doctype html><html><head><title>Stress Tab</title>"
            << "<style>.item{background:#131c2e;padding:10px;margin:6px;border-radius:6px;}</style></head><body>"
            << "<h1>Astra Heavy Tab Stress Benchmark</h1>";
    for (int i = 0; i < 160; ++i) {
        payload << "<div class=\"item\"><h3>Section " << i << "</h3>"
                << "<p>Astra SlabArena + MemoryGovernor stress payload verifying sub-2GB RAM across concurrent tabs.</p></div>";
    }
    payload << "</body></html>";

    const std::string stress_path = "/tmp/astra_stress_page.html";
    {
        std::ofstream f(stress_path);
        f << payload.str();
    }

    for (int i = 0; i < tab_count; ++i) {
        app.new_tab("file://" + stress_path);
        if (i >= 8) app.dehydrate_lru_background_tab();
    }

    auto t1 = std::chrono::steady_clock::now();
    double elapsed_ms = std::chrono::duration<double, std::milli>(t1 - t0).count();
    auto mem = app.memory_snapshot();

    size_t active_or_warm = 0, dehydrated = 0;
    for (const auto& t : app.tabs()) {
        if (t.state == TabState::Dehydrated) ++dehydrated;
        else ++active_or_warm;
    }

    std::cout << "  Tabs created:       " << app.tabs().size() << "\n";
    std::cout << "  Active/Warm tabs:   " << active_or_warm << "\n";
    std::cout << "  Dehydrated tabs:    " << dehydrated << "\n";
    std::cout << "  Total elapsed:      " << std::fixed << std::setprecision(1) << elapsed_ms << " ms ("
              << (elapsed_ms / std::max(1, tab_count)) << " ms/tab)\n";
    std::cout << "  Total OS RSS:       " << std::setprecision(2)
              << (static_cast<double>(mem.total_rss_bytes) / (1024.0 * 1024.0)) << " MB / "
              << ceiling_mb << " MB hard cap\n";
    for (const auto& p : mem.processes) {
        std::cout << "    - PID " << p.pid << " [" << p.role << "]: "
                  << (static_cast<double>(p.rss_bytes) / (1024.0 * 1024.0)) << " MB RSS\n";
    }

    bool switched = app.switch_tab(1);
    std::cout << "  Rehydrate Tab #1:   " << (switched ? "OK" : "FAILED") << "\n";

    app.stop_workers();
    return (mem.total_rss_bytes < ceiling_mb * 1024ULL * 1024ULL && switched) ? 0 : 1;
}

void print_help() {
    std::cout
        << "Astra Browser Core v9 (Native C++20 Multi-Process Engine)\n"
        << "Usage:\n"
        << "  astra --test                                Run full Session 9 subsystem suite (55 tests)\n"
        << "  astra --url <URL> [--out <file.png>]        Load URL & render PNG\n"
        << "        [--download <URL>]                    Download URL to /tmp/astra_downloads/ with SHA-256\n"
        << "        [--ext <extension_dir>]               Load Manifest V3 WebExtension (repeatable)\n"
        << "        [--scroll <px>] [--click <x,y>]       Simulate APZ scroll or mouse click before render\n"
        << "        [--type <text>] [--enter]             Type into Omnibox or focused <input> and press Enter\n"
        << "        [--raf-step <N>]                      Step N requestAnimationFrame frames before render\n"
        << "        [--video-frame <N>] [--wireframe]     Select video frame or 3D wireframe mode\n"
        << "        [--gui]                               Open interactive Linux X11 desktop window\n"
        << "  astra --stress-tabs <N> [--cap <MB>]        Stress-test N tabs with real /proc RSS check\n"
        << "  astra --repl                                Launch interactive Astra shell\n";
}

} // namespace

int main(int argc, char** argv) {
    if (argc < 2) {
        print_help();
        return 0;
    }

    std::string mode_url, out_png;
    std::vector<std::string> ext_dirs;
    std::vector<std::string> download_urls;
    std::vector<std::pair<std::string, std::string>> cli_actions;
    bool run_tests = false, open_gui = false, run_repl = false, wireframe = false;
    int stress_tabs = 0;
    int raf_steps = 0;
    float scroll_px = 0.0f;
    uint32_t video_frame = 12;
    uint64_t cap_mb = 2048;

    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        if (arg == "--test") run_tests = true;
        else if (arg == "--gui") open_gui = true;
        else if (arg == "--repl") run_repl = true;
        else if (arg == "--wireframe") wireframe = true;
        else if (arg == "--url" && i + 1 < argc) mode_url = argv[++i];
        else if (arg == "--out" && i + 1 < argc) out_png = argv[++i];
        else if (arg == "--download" && i + 1 < argc) download_urls.emplace_back(argv[++i]);
        else if (arg == "--ext" && i + 1 < argc) ext_dirs.emplace_back(argv[++i]);
        else if (arg == "--click" && i + 1 < argc) cli_actions.emplace_back("click", argv[++i]);
        else if (arg == "--type" && i + 1 < argc) cli_actions.emplace_back("type", argv[++i]);
        else if (arg == "--enter") cli_actions.emplace_back("enter", "");
        else if (arg == "--scroll" && i + 1 < argc) scroll_px = std::strtof(argv[++i], nullptr);
        else if (arg == "--raf-step" && i + 1 < argc) raf_steps = std::atoi(argv[++i]);
        else if (arg == "--video-frame" && i + 1 < argc) video_frame = static_cast<uint32_t>(std::atoi(argv[++i]));
        else if (arg == "--stress-tabs" && i + 1 < argc) stress_tabs = std::atoi(argv[++i]);
        else if (arg == "--cap" && i + 1 < argc) cap_mb = static_cast<uint64_t>(std::atoll(argv[++i]));
        else if (arg == "--help" || arg == "-h") { print_help(); return 0; }
    }

    if (run_tests) return run_full_test_suite();
    if (stress_tabs > 0) return run_stress_test(stress_tabs, cap_mb);

    BrowserApp app(cap_mb * 1024ULL * 1024ULL);
    app.start_workers();

    if (wireframe) {
        Camera3D cam;
        cam.wireframe = true;
        app.set_3d_camera(cam);
    }
    app.set_video_frame(video_frame);

    for (const auto& ext_dir : ext_dirs) {
        std::string err;
        if (app.load_extension(ext_dir, &err)) {
            std::cout << "[AstraExt] Loaded extension from " << ext_dir << "\n";
        } else {
            std::cerr << "[AstraExt] Failed to load extension: " << err << "\n";
        }
    }

    for (const auto& dl_u : download_urls) {
        auto rec = app.download_url(dl_u);
        if (rec.ok) {
            std::cout << "[AstraDL] Saved:  " << rec.saved_path << " (" << rec.bytes << " B, "
                      << rec.protocol << ", " << std::fixed << std::setprecision(1) << rec.elapsed_ms << " ms)\n"
                      << "          SHA256: " << rec.sha256_hex << "\n";
        } else {
            std::cerr << "[AstraDL] Failed: " << dl_u << " -> " << rec.error << "\n";
        }
    }

    if (!mode_url.empty()) {
        uint32_t tid = app.new_tab("");
        auto nav = app.navigate(tid, mode_url);
        if (!nav.ok) {
            std::cerr << "[Astra] Navigation failed: " << nav.error << "\n";
            app.stop_workers();
            return 1;
        }

        if (raf_steps > 0) {
            app.step_animation_frames(raf_steps);
            std::cout << "[Astra] Stepped " << raf_steps << " requestAnimationFrame frame(s)\n";
        }
        if (scroll_px != 0.0f) {
            app.scroll_active_tab(scroll_px);
        }
        for (const auto& [act, val] : cli_actions) {
            if (act == "click") {
                size_t comma = val.find(',');
                if (comma != std::string::npos) {
                    int cx = std::atoi(val.substr(0, comma).c_str());
                    int cy = std::atoi(val.substr(comma + 1).c_str());
                    auto cr = app.click_at(cx, cy);
                    std::cout << "[Astra] Click(" << cx << "," << cy << "): "
                              << (cr.handled ? (cr.action + " -> " + cr.detail) : "no hit") << "\n";
                }
            } else if (act == "type") {
                bool ok = app.type_text(val);
                std::cout << "[Astra] Type(\"" << val << "\"): " << (ok ? "OK" : "no focused input") << "\n";
            } else if (act == "enter") {
                bool ok = app.press_enter();
                std::cout << "[Astra] Enter: " << (ok ? "submitted" : "no focused input") << "\n";
            }
        }

        std::cout << "[Astra] Loaded:       " << nav.final_url << "\n"
                  << "        Title:        " << nav.title << "\n"
                  << "        Protocol:     " << (nav.protocol.empty() ? "local" : nav.protocol) << "\n"
                  << "        TLS Verified: " << (nav.tls_verified ? "YES (OS CA Store)" : "Local/N-A") << "\n"
                  << "        Cache Hit:    " << (nav.from_cache ? "YES" : "NO") << "\n"
                  << "        Ext CSS:      " << nav.external_css_loaded << " stylesheet(s)\n"
                  << "        IPC Render:   " << (nav.rendered_via_ipc ? "YES (Content Process)" : "In-Process (JS/Ext mutated)") << "\n"
                  << "        DisplayItems: " << nav.display_items << " | HitRegions: " << nav.hit_regions << "\n"
                  << "        Arena Used:   " << (nav.arena_bytes / 1024) << " KiB | Time: "
                  << std::fixed << std::setprecision(2) << nav.elapsed_ms << " ms\n";

        auto mem = app.memory_snapshot();
        std::cout << "        Total OS RSS: " << std::setprecision(2)
                  << (static_cast<double>(mem.total_rss_bytes) / (1024.0 * 1024.0)) << " MB / "
                  << (mem.ceiling_bytes / (1024 * 1024)) << " MB\n";

        if (!out_png.empty() || open_gui) {
            bool ok = app.render_active_tab(out_png, 1280, 720, open_gui);
            if (!out_png.empty()) {
                std::cout << "        PNG Output:   " << (ok ? out_png : "FAILED") << "\n";
            }
        }
        app.stop_workers();
        return 0;
    }

    if (run_repl) {
        std::cout << "Astra v9 Interactive Shell (open <url>, download <url>, downloads, back, forward, reload, close <id>, tabs, switch <id>, scroll <px>, click <x> <y>, type <text>, enter, raf [N], mem, shield, cache, render <file.png>, quit)\n";
        app.new_tab("astra://home");
        std::string line;
        while (std::cout << "astra> " && std::getline(std::cin, line)) {
            std::istringstream iss(line);
            std::string cmd;
            iss >> cmd;
            if (cmd == "quit" || cmd == "exit") break;
            else if (cmd == "open") {
                std::string u; iss >> u;
                uint32_t id = app.new_tab(u);
                std::cout << "Opened tab #" << id << " -> " << u << "\n";
            } else if (cmd == "download") {
                std::string u; iss >> u;
                auto rec = app.download_url(u);
                if (rec.ok) std::cout << "Saved " << rec.saved_path << " (" << rec.bytes << " B) SHA256=" << rec.sha256_hex << "\n";
                else std::cout << "Download failed: " << rec.error << "\n";
            } else if (cmd == "downloads") {
                for (const auto& d : app.downloads()) {
                    std::cout << "  " << d.filename << " [" << d.protocol << "] " << d.bytes << " B sha256:" << d.sha256_hex.substr(0, 16) << "...\n";
                }
            } else if (cmd == "back") {
                std::cout << (app.go_back() ? "Back OK.\n" : "No previous page.\n");
            } else if (cmd == "forward") {
                std::cout << (app.go_forward() ? "Forward OK.\n" : "No forward page.\n");
            } else if (cmd == "reload") {
                std::cout << (app.reload_active_tab() ? "Reloaded.\n" : "Reload failed.\n");
            } else if (cmd == "close") {
                uint32_t id = 0; iss >> id;
                std::cout << (app.close_tab(id ? id : app.active_tab_id()) ? "Closed.\n" : "Not found.\n");
            } else if (cmd == "tabs") {
                for (const auto& t : app.tabs()) {
                    std::cout << "  #" << t.id << (t.id == app.active_tab_id() ? " * " : "   ")
                              << "[" << (t.state == TabState::Dehydrated ? "DEHYDRATED" : "LIVE") << "] "
                              << t.title << " (" << t.url << ")\n";
                }
            } else if (cmd == "switch") {
                uint32_t id = 0; iss >> id;
                std::cout << (app.switch_tab(id) ? "Switched.\n" : "Not found.\n");
            } else if (cmd == "scroll") {
                float dy = 0; iss >> dy;
                app.scroll_active_tab(dy);
                std::cout << "Scrolled by " << dy << " px.\n";
            } else if (cmd == "click") {
                int x = 0, y = 0; iss >> x >> y;
                auto cr = app.click_at(x, y);
                std::cout << (cr.handled ? (cr.action + ": " + cr.detail + "\n") : "No interactive hit.\n");
            } else if (cmd == "type") {
                std::string txt;
                std::getline(iss >> std::ws, txt);
                std::cout << (app.type_text(txt) ? "Typed.\n" : "No focused input.\n");
            } else if (cmd == "enter") {
                std::cout << (app.press_enter() ? "Submitted.\n" : "No focused input.\n");
            } else if (cmd == "raf") {
                int n = 1;
                if (!(iss >> n) || n < 1) n = 1;
                std::cout << (app.step_animation_frames(n) ? ("Stepped " + std::to_string(n) + " rAF frame(s).\n")
                                                           : "No pending rAF callbacks.\n");
            } else if (cmd == "mem") {
                auto m = app.memory_snapshot();
                std::cout << "Total RSS: " << (m.total_rss_bytes / (1024.0 * 1024.0)) << " MB / "
                          << (m.ceiling_bytes / (1024 * 1024)) << " MB\n";
            } else if (cmd == "cache") {
                const auto& c = app.cache().stats();
                std::cout << "Hits: " << c.hits << " | Misses: " << c.misses
                          << " | Bytes: " << (c.current_bytes / 1024) << " KiB\n";
            } else if (cmd == "shield") {
                const auto& st = app.shield().stats();
                std::cout << "Inspected: " << st.requests_inspected
                          << " | Blocked: " << st.trackers_blocked
                          << " | Params stripped: " << st.params_stripped
                          << " | HTTPS upgrades: " << st.https_upgrades << "\n";
            } else if (cmd == "render") {
                std::string out; iss >> out;
                if (out.empty()) out = "astra_frame.png";
                std::cout << (app.render_active_tab(out) ? ("Saved " + out + "\n") : "Render failed.\n");
            }
        }
    }

    app.stop_workers();
    return 0;
}