onnx-genai-server 0.1.0-dev.2

OpenAI-compatible HTTP server for onnx-genai
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
use crate::{
    AppState, ChatCompletionRequest, CompletionRequest, EmbeddingEncodingFormat, EmbeddingInput,
    EmbeddingRequest, EmbeddingResponse, EmbeddingUsage, EmbeddingVector, ServerConfig, app,
    build_generate_request,
    driver::{DriverCommand, EngineDriver},
    models_config::ModelSpec,
    routes::{CompletionGeneration, collect_generation_result, prepare_completion},
    sse::StopBoundaryBuffer,
};
use axum::{
    body::{Body, to_bytes},
    http::{Request, StatusCode, header},
};
use onnx_genai::{Engine, EngineConfig};
use serde_json::{Value, json};
use std::{io::Cursor, path::PathBuf, time::Duration};
use tokio::{sync::mpsc, time::timeout};
use tower::ServiceExt;

fn tiny_state() -> AppState {
    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    AppState::load(&model_dir, Some("tiny-llm".to_string())).expect("load fixture")
}

fn tiny_state_with_debug() -> AppState {
    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    AppState::load_with_config(
        &model_dir,
        Some("tiny-llm".to_string()),
        ServerConfig {
            enable_debug_endpoints: true,
            ..ServerConfig::default()
        },
    )
    .expect("load fixture with debug")
}

fn sse_json_events(body: &[u8]) -> Vec<Value> {
    std::str::from_utf8(body)
        .unwrap()
        .lines()
        .filter_map(|line| line.strip_prefix("data: "))
        .filter(|data| *data != "[DONE]")
        .map(|data| serde_json::from_str(data).unwrap())
        .collect()
}

fn tiny_png_data_uri() -> String {
    use base64::{Engine as _, engine::general_purpose::STANDARD};
    use image::{DynamicImage, ImageFormat, Rgb, RgbImage};

    let image = RgbImage::from_pixel(3, 4, Rgb([64, 128, 255]));
    let mut png = Cursor::new(Vec::new());
    DynamicImage::ImageRgb8(image)
        .write_to(&mut png, ImageFormat::Png)
        .unwrap();
    format!(
        "data:image/png;base64,{}",
        STANDARD.encode(png.into_inner())
    )
}

fn tiny_wav_bytes() -> Vec<u8> {
    let samples = [0_i16; 1_280];
    let data_len = (samples.len() * 2) as u32;
    let mut wav = Vec::with_capacity(44 + data_len as usize);
    wav.extend_from_slice(b"RIFF");
    wav.extend_from_slice(&(36 + data_len).to_le_bytes());
    wav.extend_from_slice(b"WAVEfmt ");
    wav.extend_from_slice(&16_u32.to_le_bytes());
    wav.extend_from_slice(&1_u16.to_le_bytes());
    wav.extend_from_slice(&1_u16.to_le_bytes());
    wav.extend_from_slice(&16_000_u32.to_le_bytes());
    wav.extend_from_slice(&32_000_u32.to_le_bytes());
    wav.extend_from_slice(&2_u16.to_le_bytes());
    wav.extend_from_slice(&16_u16.to_le_bytes());
    wav.extend_from_slice(b"data");
    wav.extend_from_slice(&data_len.to_le_bytes());
    for sample in samples {
        wav.extend_from_slice(&sample.to_le_bytes());
    }
    wav
}

fn tiny_wav_base64() -> String {
    use base64::{Engine as _, engine::general_purpose::STANDARD};
    STANDARD.encode(tiny_wav_bytes())
}

fn multipart_audio_body(response_format: &str) -> (String, Vec<u8>) {
    multipart_audio_body_for_model("tiny-whisper", response_format)
}

fn multipart_audio_body_for_model(model: &str, response_format: &str) -> (String, Vec<u8>) {
    let boundary = "onnx-genai-audio-boundary";
    let mut body = Vec::new();
    body.extend_from_slice(
        format!(
            "--{boundary}\r\nContent-Disposition: form-data; name=\"model\"\r\n\r\n{model}\r\n\
             --{boundary}\r\nContent-Disposition: form-data; name=\"response_format\"\r\n\r\n{response_format}\r\n\
             --{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"tiny.wav\"\r\nContent-Type: audio/wav\r\n\r\n"
        )
        .as_bytes(),
    );
    body.extend_from_slice(&tiny_wav_bytes());
    body.extend_from_slice(format!("\r\n--{boundary}--\r\n").as_bytes());
    (boundary.to_string(), body)
}

#[test]
fn multimodal_message_parses_text_and_data_image_parts() {
    let request: ChatCompletionRequest = serde_json::from_value(json!({
        "model": "tiny-vlm",
        "messages": [{
            "role": "user",
            "content": [
                {"type": "text", "text": "What is shown?"},
                {"type": "image_url", "image_url": {"url": tiny_png_data_uri()}}
            ]
        }]
    }))
    .unwrap();

    assert_eq!(
        request.messages[0]
            .content
            .as_ref()
            .expect("content")
            .text(),
        "What is shown?"
    );
    assert_eq!(request.image_urls().len(), 1);
    assert!(request.image_urls()[0].starts_with("data:image/png;base64,"));
}

#[test]
fn multimodal_message_parses_base64_wav_input_audio_part() {
    let request: ChatCompletionRequest = serde_json::from_value(json!({
        "model": "tiny-whisper",
        "messages": [{
            "role": "user",
            "content": [
                {"type": "text", "text": "Transcribe this"},
                {"type": "input_audio", "input_audio": {
                    "data": tiny_wav_base64(),
                    "format": "wav"
                }}
            ]
        }]
    }))
    .unwrap();

    assert_eq!(
        request.messages[0]
            .content
            .as_ref()
            .expect("content")
            .text(),
        "Transcribe this"
    );
    let audio = request.input_audio();
    assert_eq!(audio.len(), 1);
    assert_eq!(audio[0].format, "wav");
    assert!(!audio[0].data.is_empty());
}

#[test]
fn transcription_json_response_has_openai_shape() {
    let response = crate::types::AudioTranscriptionResponse {
        text: "hello".to_string(),
    };
    assert_eq!(
        serde_json::to_value(response).unwrap(),
        json!({"text": "hello"})
    );
}

#[test]
fn embedding_request_accepts_openai_input_variants_and_defaults_to_float() {
    let single: EmbeddingRequest = serde_json::from_value(json!({
        "model": "embedder",
        "input": "hello"
    }))
    .unwrap();
    assert!(matches!(single.input, EmbeddingInput::String(_)));
    assert_eq!(single.encoding_format, EmbeddingEncodingFormat::Float);

    let strings: EmbeddingRequest = serde_json::from_value(json!({
        "model": "embedder",
        "input": ["hello", "world"],
        "encoding_format": "base64",
        "dimensions": 64
    }))
    .unwrap();
    assert!(matches!(strings.input, EmbeddingInput::Strings(_)));
    assert_eq!(strings.encoding_format, EmbeddingEncodingFormat::Base64);
    assert_eq!(strings.dimensions, Some(64));

    let tokens: EmbeddingRequest = serde_json::from_value(json!({
        "model": "embedder",
        "input": [[1, 2], [3, 4]]
    }))
    .unwrap();
    assert!(matches!(tokens.input, EmbeddingInput::TokenArrays(_)));

    assert!(
        serde_json::from_value::<EmbeddingRequest>(json!({
            "model": "embedder",
            "input": "hello",
            "encoding_format": "hex"
        }))
        .is_err()
    );
}

#[tokio::test]
async fn chat_logprobs_match_openai_shape_and_are_opt_in() {
    let router = app(tiny_state());
    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "messages": [{"role": "user", "content": "hello"}],
                        "max_tokens": 2,
                        "temperature": 0.0,
                        "logprobs": true,
                        "top_logprobs": 2
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let body: Value =
        serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap()).unwrap();
    let content = body["choices"][0]["logprobs"]["content"]
        .as_array()
        .unwrap();
    assert_eq!(
        content.len(),
        body["usage"]["completion_tokens"].as_u64().unwrap() as usize
    );
    for token in content {
        let token_text = token["token"].as_str().unwrap();
        let bytes = token["bytes"].as_array().unwrap();
        assert_eq!(
            bytes
                .iter()
                .map(|byte| byte.as_u64().unwrap() as u8)
                .collect::<Vec<_>>(),
            token_text.as_bytes()
        );
        assert!(token["logprob"].is_number());
        let top_logprobs = token["top_logprobs"].as_array().unwrap();
        assert!(top_logprobs.len() <= 2);
        for alternative in top_logprobs {
            let token_text = alternative["token"].as_str().unwrap();
            assert_eq!(
                alternative["bytes"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|byte| byte.as_u64().unwrap() as u8)
                    .collect::<Vec<_>>(),
                token_text.as_bytes()
            );
        }
    }

    let response = router
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "messages": [{"role": "user", "content": "hello"}],
                        "max_tokens": 1,
                        "temperature": 0.0
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    let body: Value =
        serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap()).unwrap();
    assert!(body["choices"][0]["logprobs"].is_null());
}

#[tokio::test]
async fn completion_logprobs_match_legacy_openai_shape() {
    let response = app(tiny_state())
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "prompt": "hello",
                        "max_tokens": 3,
                        "temperature": 0.0,
                        "logprobs": 2
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let body: Value =
        serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap()).unwrap();
    let logprobs = &body["choices"][0]["logprobs"];
    let tokens = logprobs["tokens"].as_array().unwrap();
    let token_logprobs = logprobs["token_logprobs"].as_array().unwrap();
    let top_logprobs = logprobs["top_logprobs"].as_array().unwrap();
    let offsets = logprobs["text_offset"].as_array().unwrap();
    assert_eq!(tokens.len(), 3);
    assert_eq!(token_logprobs.len(), tokens.len());
    assert_eq!(top_logprobs.len(), tokens.len());
    assert_eq!(offsets.len(), tokens.len());
    let mut expected_offset = 0;
    for index in 0..tokens.len() {
        assert_eq!(offsets[index].as_u64().unwrap() as usize, expected_offset);
        expected_offset += tokens[index].as_str().unwrap().len();
        assert!(top_logprobs[index].as_object().unwrap().len() <= 2);
    }
}

#[tokio::test]
async fn streaming_chat_and_completion_chunks_include_logprobs() {
    let chat = app(tiny_state())
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "messages": [{"role": "user", "content": "hello"}],
                        "max_tokens": 2,
                        "temperature": 0.0,
                        "stream": true,
                        "logprobs": true,
                        "top_logprobs": 1
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    let events = sse_json_events(&to_bytes(chat.into_body(), usize::MAX).await.unwrap());
    let content_events = events
        .iter()
        .filter(|event| event["choices"][0]["delta"]["content"].is_string())
        .collect::<Vec<_>>();
    assert_eq!(content_events.len(), 2);
    for event in content_events {
        let record = &event["choices"][0]["logprobs"]["content"][0];
        assert_eq!(event["choices"][0]["delta"]["content"], record["token"]);
        assert_eq!(record["top_logprobs"].as_array().unwrap().len(), 1);
        assert!(record["bytes"].is_array());
    }

    let completion = app(tiny_state())
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "prompt": "hello",
                        "max_tokens": 2,
                        "temperature": 0.0,
                        "stream": true,
                        "logprobs": 1
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    let events = sse_json_events(&to_bytes(completion.into_body(), usize::MAX).await.unwrap());
    let token_events = events
        .iter()
        .filter(|event| {
            !event["choices"][0]["text"]
                .as_str()
                .unwrap_or_default()
                .is_empty()
        })
        .collect::<Vec<_>>();
    assert_eq!(token_events.len(), 2);
    for event in token_events {
        let logprobs = &event["choices"][0]["logprobs"];
        assert_eq!(logprobs["tokens"].as_array().unwrap().len(), 1);
        assert_eq!(logprobs["token_logprobs"].as_array().unwrap().len(), 1);
        assert_eq!(logprobs["top_logprobs"].as_array().unwrap().len(), 1);
        assert_eq!(logprobs["text_offset"].as_array().unwrap().len(), 1);
    }
}

#[tokio::test]
async fn logprobs_validation_enforces_openai_limits() {
    for body in [
        json!({
            "model": "tiny-llm",
            "messages": [{"role": "user", "content": "hello"}],
            "max_tokens": 1,
            "top_logprobs": 1
        }),
        json!({
            "model": "tiny-llm",
            "messages": [{"role": "user", "content": "hello"}],
            "max_tokens": 1,
            "logprobs": true,
            "top_logprobs": 21
        }),
    ] {
        let response = app(tiny_state())
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/v1/chat/completions")
                    .header(header::CONTENT_TYPE, "application/json")
                    .body(Body::from(body.to_string()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    let response = app(tiny_state())
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "prompt": "hello",
                        "max_tokens": 1,
                        "logprobs": 6
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}

#[test]
fn embedding_response_serializes_float_and_base64_vectors() {
    use base64::{Engine as _, engine::general_purpose::STANDARD};

    let float = EmbeddingResponse {
        object: "list",
        data: vec![crate::EmbeddingData {
            object: "embedding",
            embedding: EmbeddingVector::from_floats(
                vec![1.0, -2.0],
                EmbeddingEncodingFormat::Float,
            ),
            index: 0,
        }],
        model: "embedder".to_string(),
        usage: EmbeddingUsage {
            prompt_tokens: 2,
            total_tokens: 2,
        },
    };
    let float = serde_json::to_value(float).unwrap();
    assert_eq!(float["object"], "list");
    assert_eq!(float["data"][0]["object"], "embedding");
    assert_eq!(float["data"][0]["embedding"], json!([1.0, -2.0]));
    assert_eq!(float["data"][0]["index"], 0);
    assert_eq!(float["model"], "embedder");
    assert_eq!(
        float["usage"],
        json!({"prompt_tokens": 2, "total_tokens": 2})
    );

    let base64 = EmbeddingVector::from_floats(vec![1.0, -2.0], EmbeddingEncodingFormat::Base64);
    let encoded = serde_json::to_value(base64).unwrap();
    let expected = STANDARD.encode(
        [1.0_f32, -2.0_f32]
            .into_iter()
            .flat_map(f32::to_le_bytes)
            .collect::<Vec<_>>(),
    );
    assert_eq!(encoded, expected);
}

#[tokio::test]
async fn embeddings_valid_inputs_fail_on_logits_only_model() {
    let router = app(tiny_state());
    for input in [
        json!("hello"),
        json!(["hello", "world"]),
        json!([[1, 2], [3, 4]]),
    ] {
        let response = router
            .clone()
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/v1/embeddings")
                    .header(header::CONTENT_TYPE, "application/json")
                    .body(Body::from(
                        json!({
                            "model": "tiny-llm",
                            "input": input,
                            "encoding_format": "base64"
                        })
                        .to_string(),
                    ))
                    .unwrap(),
            )
            .await
            .unwrap();

        // tiny-llm is a logits-only model; the engine rejects embedding requests
        // with a descriptive error rather than NOT_IMPLEMENTED.
        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        let body: Value = serde_json::from_slice(&body).unwrap();
        assert!(
            body["error"]["message"]
                .as_str()
                .unwrap()
                .contains("hidden-state output"),
            "{body}"
        );
    }

    for (body, message) in [
        (
            json!({"model": "tiny-llm", "input": []}),
            "embedding input array must not be empty",
        ),
        (
            json!({"model": "tiny-llm", "input": "hello", "dimensions": 0}),
            "dimensions must be greater than zero",
        ),
    ] {
        let response = router
            .clone()
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/v1/embeddings")
                    .header(header::CONTENT_TYPE, "application/json")
                    .body(Body::from(body.to_string()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
        let body: Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(body["error"]["message"], message);
    }
}

#[tokio::test]
async fn embeddings_success_path_returns_openai_compatible_response() {
    let model_dir =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-mtp-full");
    let state = AppState::load(&model_dir, Some("tiny-mtp-full".to_string()))
        .expect("load tiny-mtp-full fixture");
    let router = app(state);

    // Single string input → one embedding entry
    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/embeddings")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-mtp-full",
                        "input": "hello"
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let body: Value =
        serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
            .unwrap();
    assert_eq!(body["object"], "list");
    assert_eq!(body["model"], "tiny-mtp-full");
    let data = body["data"].as_array().unwrap();
    assert_eq!(data.len(), 1);
    assert_eq!(data[0]["object"], "embedding");
    assert_eq!(data[0]["index"], 0);
    assert!(data[0]["embedding"].is_array());
    assert!(!data[0]["embedding"].as_array().unwrap().is_empty());
    let usage = &body["usage"];
    assert!(usage["prompt_tokens"].as_u64().unwrap() > 0);
    assert_eq!(usage["prompt_tokens"], usage["total_tokens"]);

    // Array of strings → one entry per input, indices in order, correct token count
    let response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/embeddings")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-mtp-full",
                        "input": ["hello", "world"]
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let body: Value =
        serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
            .unwrap();
    let data = body["data"].as_array().unwrap();
    assert_eq!(data.len(), 2);
    assert_eq!(data[0]["index"], 0);
    assert_eq!(data[1]["index"], 1);

    // base64 encoding format → embedding is a string, not an array
    let response = router
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/embeddings")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-mtp-full",
                        "input": "hello",
                        "encoding_format": "base64"
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    let body: Value =
        serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
            .unwrap();
    assert!(body["data"][0]["embedding"].is_string());
}

#[tokio::test]
async fn transcription_multipart_against_non_audio_model_returns_400() {
    // Send the correct model name (tiny-llm) so routing succeeds, then the handler
    // returns 400 because tiny-llm has no audio input spec.
    let (boundary, body) = multipart_audio_body_for_model("tiny-llm", "json");
    let response = app(tiny_state())
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/audio/transcriptions")
                .header(
                    header::CONTENT_TYPE,
                    format!("multipart/form-data; boundary={boundary}"),
                )
                .body(Body::from(body))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body: Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(
        body["error"]["message"],
        "this model does not support audio transcription"
    );
}

#[tokio::test]
async fn audio_chat_against_non_audio_model_returns_400() {
    let response = app(tiny_state())
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "messages": [{
                            "role": "user",
                            "content": [{
                                "type": "input_audio",
                                "input_audio": {
                                    "data": tiny_wav_base64(),
                                    "format": "wav"
                                }
                            }]
                        }],
                        "max_tokens": 1
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body: Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(
        body["error"]["message"],
        "this model does not support audio input"
    );
}

#[tokio::test]
#[ignore = "synthetic Whisper-contract smoke test; run explicitly for audio server validation"]
async fn audio_endpoints_route_through_tiny_whisper_pipeline() {
    let model_dir =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-whisper");
    let router =
        app(AppState::load(&model_dir, Some("tiny-whisper".to_string()))
            .expect("load Whisper fixture"));

    let chat_response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-whisper",
                        "messages": [{
                            "role": "user",
                            "content": [{
                                "type": "input_audio",
                                "input_audio": {
                                    "data": tiny_wav_base64(),
                                    "format": "wav"
                                }
                            }]
                        }],
                        "max_tokens": 2,
                        "temperature": 0.0
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(chat_response.status(), StatusCode::OK);
    let chat_body = to_bytes(chat_response.into_body(), usize::MAX)
        .await
        .unwrap();
    let chat_body: Value = serde_json::from_slice(&chat_body).unwrap();
    assert!(chat_body["choices"][0]["message"]["content"].is_string());

    let (boundary, body) = multipart_audio_body("json");
    let transcription_response = router
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/audio/transcriptions")
                .header(
                    header::CONTENT_TYPE,
                    format!("multipart/form-data; boundary={boundary}"),
                )
                .body(Body::from(body))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(transcription_response.status(), StatusCode::OK);
    let transcription_body = to_bytes(transcription_response.into_body(), usize::MAX)
        .await
        .unwrap();
    let transcription_body: Value = serde_json::from_slice(&transcription_body).unwrap();
    assert!(transcription_body["text"].is_string());

    let (boundary, body) = multipart_audio_body("text");
    let text_response = router
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/audio/transcriptions")
                .header(
                    header::CONTENT_TYPE,
                    format!("multipart/form-data; boundary={boundary}"),
                )
                .body(Body::from(body))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(text_response.status(), StatusCode::OK);
    assert_eq!(
        text_response.headers()[header::CONTENT_TYPE],
        "text/plain; charset=utf-8"
    );
    let text_body = to_bytes(text_response.into_body(), usize::MAX)
        .await
        .unwrap();
    assert!(!text_body.is_empty());
}

#[tokio::test]
async fn image_decode_and_preprocessing_use_pipeline_tensor_shape() {
    use onnx_genai_ort::{DataType, PipelineModels};

    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../models/tiny-vlm");
    if !model_dir.is_dir() {
        eprintln!("skipping image preprocessing test: tiny-vlm fixture is absent");
        return;
    }
    let models = PipelineModels::load(&model_dir).unwrap();
    let encoder = models.session("encoder").expect("encoder");
    let input = encoder
        .inputs()
        .iter()
        .find(|input| input.name == "pixel_values")
        .expect("pixel_values");
    assert_eq!(input.dtype, DataType::Float32);
    let spec = crate::image_input::VisionInputSpec::from_input(
        "encoder.pixel_values".to_string(),
        &input.shape,
    )
    .unwrap();
    let tensor = crate::image_input::load_and_preprocess(&[tiny_png_data_uri()], &spec)
        .await
        .unwrap();

    assert_eq!(tensor.shape, input.shape);
    assert_eq!(
        tensor.data.len(),
        input.shape.iter().product::<i64>() as usize
    );
    assert!(tensor.data.iter().all(|value| (0.0..=1.0).contains(value)));
}

#[tokio::test]
async fn vision_request_against_non_pipeline_model_returns_400() {
    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    let state = AppState::load(&model_dir, Some("tiny-llm".to_string())).expect("load fixture");
    let response = app(state)
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "messages": [{
                            "role": "user",
                            "content": [
                                {"type": "text", "text": "describe"},
                                {"type": "image_url", "image_url": {"url": tiny_png_data_uri()}}
                            ]
                        }],
                        "max_tokens": 1
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body: Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(
        body["error"]["message"],
        "this model does not support image input"
    );
}

#[tokio::test]
async fn status_reports_node_status_contract() {
    let response = app(tiny_state())
        .oneshot(
            Request::builder()
                .uri("/v1/status")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body: Value = serde_json::from_slice(&body).unwrap();

    // node_id is present, non-empty, and NOT the model id (model-agnostic).
    let node_id = body["node_id"].as_str().expect("node_id is a string");
    assert!(!node_id.is_empty(), "node_id must not be empty");
    assert_ne!(node_id, "tiny-llm", "node_id must not be the model id");

    assert_eq!(body["healthy"], true);
    // Real metrics serialize with the right JSON types.
    assert!(body["queue_depth"].is_u64());
    assert!(body["active_sessions"].is_u64());
    assert!(body["paused_sessions"].is_u64());
    assert!(body["kv_usage"].is_number());
    assert!(body["kv_pages_used"].is_u64());
    assert!(body["kv_pages_total"].is_u64());
    assert!(body["kv_pages_shared"].is_u64());
    assert!(body["tokens_per_second"].is_number());
    assert!(body["batch_utilization"].is_number());
    assert!(body["sessions"].is_array());
    assert!(body["prefix_hashes"].is_array());
}

#[tokio::test]
async fn status_node_id_reflects_configured_value() {
    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    let state = AppState::load_with_config(
        &model_dir,
        Some("tiny-llm".to_string()),
        ServerConfig {
            node_id: "gpu-7".to_string(),
            ..ServerConfig::default()
        },
    )
    .expect("load fixture with node id");

    let response = app(state)
        .oneshot(
            Request::builder()
                .uri("/v1/status")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body: Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(body["node_id"], "gpu-7");
}

#[tokio::test]
async fn status_active_sessions_reflect_real_state() {
    let state = tiny_state();
    let handle = state.registry.resolve("").unwrap();
    // Create a real engine session and register it, mirroring the session route.
    let engine_session = handle
        .engine
        .create_session()
        .await
        .expect("create engine session");
    let client_id = state.sessions.next_client_id().unwrap();
    state
        .sessions
        .insert(client_id, engine_session)
        .expect("register session");

    let response = app(state)
        .oneshot(
            Request::builder()
                .uri("/v1/status")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body: Value = serde_json::from_slice(&body).unwrap();
    assert!(
        body["active_sessions"].as_u64().unwrap() >= 1,
        "active_sessions must reflect the registered session"
    );
    let sessions = body["sessions"].as_array().unwrap();
    assert!(
        !sessions.is_empty(),
        "sessions list must include the registered session"
    );
    assert!(sessions[0]["id"].as_str().unwrap().starts_with("sess-"));
}

#[tokio::test]
async fn debug_endpoints_expose_config_sessions_cache_and_trace_state() {
    let router = app(tiny_state_with_debug());

    let config = router
        .clone()
        .oneshot(
            Request::builder()
                .uri("/v1/debug/config")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(config.status(), StatusCode::OK);
    let config: Value =
        serde_json::from_slice(&to_bytes(config.into_body(), usize::MAX).await.unwrap()).unwrap();
    assert_eq!(config["model_id"], "tiny-llm");
    assert_eq!(config["max_queue_depth"], 256);
    assert_eq!(config["pipeline"], false);

    let created = router
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/sessions")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(created.status(), StatusCode::OK);
    let created: Value =
        serde_json::from_slice(&to_bytes(created.into_body(), usize::MAX).await.unwrap()).unwrap();

    let sessions = router
        .clone()
        .oneshot(
            Request::builder()
                .uri("/v1/debug/sessions")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(sessions.status(), StatusCode::OK);
    let sessions: Value =
        serde_json::from_slice(&to_bytes(sessions.into_body(), usize::MAX).await.unwrap()).unwrap();
    // The list must contain a redacted entry for the created session, but must NOT
    // contain the raw bearer credential (full capability ID).
    let raw_id = created["id"].as_str().unwrap();
    let session_list = sessions["sessions"].as_array().unwrap();
    assert!(
        !session_list.iter().any(|v| v.as_str() == Some(raw_id)),
        "raw session ID must not appear in debug/sessions response"
    );
    // Redacted form starts with "sess-" and ends with "…"
    assert!(
        session_list
            .iter()
            .any(|v| v.as_str().is_some_and(|s| s.starts_with("sess-") && s.ends_with('…'))),
        "expected a redacted session entry (sess-<prefix>…) in debug/sessions"
    );

    let cache = router
        .clone()
        .oneshot(
            Request::builder()
                .uri("/v1/debug/kv")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(cache.status(), StatusCode::OK);
    let cache: Value =
        serde_json::from_slice(&to_bytes(cache.into_body(), usize::MAX).await.unwrap()).unwrap();
    assert!(cache["prefix_cache_hits"].is_u64());
    assert!(cache["pending_queue_depth"].is_u64());
    assert!(cache["available_admission_slots"].is_u64());

    let trace = router
        .oneshot(
            Request::builder()
                .uri("/v1/debug/trace")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(trace.status(), StatusCode::OK);
    let trace: Value =
        serde_json::from_slice(&to_bytes(trace.into_body(), usize::MAX).await.unwrap()).unwrap();
    assert_eq!(trace["tracing_span"], "http.request");
    assert!(trace["latest_trace_id"].is_string());
    assert_eq!(
        trace["perfetto_export"]["endpoint"],
        "/v1/debug/trace/perfetto"
    );
    assert!(trace["perfetto_export"]["recorded_events"].is_u64());
    assert!(trace["perfetto_export"]["collecting"].is_boolean());
    assert!(
        trace["otlp_export"].as_str().unwrap().contains("deferred"),
        "OTLP export must be reported as deferred"
    );
}

#[tokio::test]
async fn debug_trace_perfetto_returns_well_formed_chrome_trace() {
    let router = app(tiny_state_with_debug());

    let resp = router
        .oneshot(
            Request::builder()
                .uri("/v1/debug/trace/perfetto")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    assert_eq!(
        resp.headers()
            .get(header::CONTENT_TYPE)
            .and_then(|v| v.to_str().ok()),
        Some("application/json")
    );
    assert!(
        resp.headers()
            .get(header::CONTENT_DISPOSITION)
            .and_then(|v| v.to_str().ok())
            .is_some_and(|v| v.contains("onnx-genai-trace.json")),
        "Perfetto export must be served as a downloadable attachment"
    );
    let doc: Value =
        serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.unwrap()).unwrap();
    // Chrome Trace Event Format: an object with a `traceEvents` array. It may be
    // empty (no spans recorded in this process) but must be well-formed so it
    // opens directly in https://ui.perfetto.dev.
    assert!(
        doc["traceEvents"].is_array(),
        "Perfetto document must contain a traceEvents array"
    );
    assert_eq!(doc["displayTimeUnit"], "ms");
}

#[tokio::test]
async fn debug_endpoints_return_404_when_gate_is_off() {
    // Default state has enable_debug_endpoints = false; routes must not be registered.
    let router = app(tiny_state());
    for path in &[
        "/v1/debug/config",
        "/v1/debug/sessions",
        "/v1/debug/kv",
        "/v1/debug/trace",
        "/v1/debug/trace/perfetto",
    ] {
        let resp = router
            .clone()
            .oneshot(
                Request::builder()
                    .uri(*path)
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(
            resp.status(),
            StatusCode::NOT_FOUND,
            "{path} must return 404 when debug endpoints are disabled"
        );
    }
}

#[cfg(feature = "metrics")]
#[tokio::test]
async fn metrics_exposes_prometheus_families_and_request_counter_increments() {
    let router = app(tiny_state());
    let before = router
        .clone()
        .oneshot(
            Request::builder()
                .uri("/metrics")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    let before = to_bytes(before.into_body(), usize::MAX).await.unwrap();
    let before = String::from_utf8(before.to_vec()).unwrap();
    let before_health = prometheus_sample(
        &before,
        "onnx_genai_requests_total{endpoint=\"/health\",status=\"200\"}",
    );

    for _ in 0..2 {
        let response = router
            .clone()
            .oneshot(
                Request::builder()
                    .uri("/health")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    let response = router
        .oneshot(
            Request::builder()
                .uri("/metrics")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(
        response.headers()[header::CONTENT_TYPE],
        "text/plain; version=0.0.4; charset=utf-8"
    );
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body = String::from_utf8(body.to_vec()).unwrap();
    assert!(body.contains("# TYPE onnx_genai_requests_total counter"));
    assert!(body.contains("# TYPE onnx_genai_time_to_first_token_seconds histogram"));
    assert!(body.contains("# TYPE onnx_genai_e2e_request_latency_seconds histogram"));
    assert!(body.contains("onnx_genai_sessions_active"));
    assert!(body.contains("onnx_genai_requests_waiting"));
    assert!(body.contains("onnx_genai_batch_size_current"));
    assert!(body.contains("onnx_genai_prefix_cache_hit_rate"));
    assert!(body.contains("onnx_genai_rejections_total"));
    let after_health = prometheus_sample(
        &body,
        "onnx_genai_requests_total{endpoint=\"/health\",status=\"200\"}",
    );
    assert!(after_health >= before_health + 2);
}

#[cfg(feature = "metrics")]
fn prometheus_sample(body: &str, metric: &str) -> u64 {
    body.lines()
        .find_map(|line| {
            line.strip_prefix(metric)
                .and_then(|value| value.trim().parse().ok())
        })
        .unwrap_or(0)
}

#[tokio::test]
#[ignore = "requires gitignored models/tiny-vlm; run scripts/build_tiny_vlm.py first"]
async fn vision_request_routes_through_tiny_vlm_pipeline() {
    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../models/tiny-vlm");
    if !model_dir.is_dir() {
        eprintln!("skipping tiny VLM server test: fixture is absent");
        return;
    }
    let state = AppState::load(&model_dir, Some("tiny-vlm".to_string())).expect("load fixture");
    let response = app(state)
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-vlm",
                        "messages": [{
                            "role": "user",
                            "content": [
                                {"type": "text", "text": "describe"},
                                {"type": "image_url", "image_url": {"url": tiny_png_data_uri()}}
                            ]
                        }],
                        "max_tokens": 1,
                        "temperature": 0.0
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body: Value = serde_json::from_slice(&body).unwrap();
    assert!(body["choices"][0]["message"]["content"].is_string());
}

#[test]
fn completion_suffix_maps_to_fim_generation() {
    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    let state = AppState::load(&model_dir, Some("tiny-llm".to_string()))
        .expect("load fixture")
        .with_default_fim_config(Some(onnx_genai_engine::FimConfig {
            prefix_token: "<PRE>".to_string(),
            middle_token: "<MID>".to_string(),
            suffix_token: "<SUF>".to_string(),
            format: onnx_genai_engine::FimFormat::PSM,
        }));
    let handle = state.registry.resolve("").unwrap();
    let request: CompletionRequest = serde_json::from_value(json!({
        "model": "tiny-llm",
        "prompt": "prefix",
        "suffix": "suffix",
        "max_tokens": 7,
        "min_p": 0.2,
        "frequency_penalty": 0.3,
        "presence_penalty": 0.4
    }))
    .unwrap();

    let prepared = prepare_completion(&request, &handle).unwrap();
    match prepared.generation {
        CompletionGeneration::Fim {
            prefix,
            suffix,
            options,
        } => {
            assert_eq!(prefix, "prefix");
            assert_eq!(suffix, "suffix");
            assert_eq!(options.max_new_tokens, 7);
            assert_eq!(options.min_p, 0.2);
            assert_eq!(options.frequency_penalty, 0.3);
            assert_eq!(options.presence_penalty, 0.4);
        }
        CompletionGeneration::Plain(_) => panic!("suffix must route to FIM generation"),
    }
}

#[tokio::test]
async fn completion_suffix_uses_fim_and_returns_text_completion() {
    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    let state = AppState::load(&model_dir, Some("tiny-llm".to_string()))
        .expect("load fixture")
        .with_default_fim_config(Some(onnx_genai_engine::FimConfig {
            prefix_token: "<PRE>".to_string(),
            middle_token: "<MID>".to_string(),
            suffix_token: "<SUF>".to_string(),
            format: onnx_genai_engine::FimFormat::PSM,
        }));

    let response = app(state)
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "prompt": "prefix",
                        "suffix": "suffix",
                        "max_tokens": 1
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let json: Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(json["object"], "text_completion");
    assert!(json["choices"][0]["text"].is_string());
    assert!(json["choices"][0]["logprobs"].is_null());
}

#[test]
fn stop_boundary_buffer_holds_partial_stop_sequence() {
    let mut buffer = StopBoundaryBuffer::new(vec!["tok20".to_string()]);
    assert_eq!(buffer.push("to"), "");
    assert_eq!(buffer.push("k"), "");
    assert_eq!(buffer.push("2"), "");
    assert_eq!(buffer.push("1"), "tok21");
    assert_eq!(buffer.flush(), "");
}

#[test]
fn stop_boundary_buffer_suppresses_matched_stop_sequence() {
    let mut buffer = StopBoundaryBuffer::new(vec!["tok20".to_string()]);
    assert_eq!(buffer.push("hello tok"), "hello ");
    assert_eq!(buffer.push("20"), "");
    assert_eq!(buffer.flush(), "");
}

#[tokio::test]
async fn queue_depth_admission_limit_returns_429_with_retry_after() {
    let model_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    let state = AppState::load_with_config(
        &model_dir,
        Some("tiny-llm".to_string()),
        ServerConfig {
            max_output_tokens: 16,
            max_sessions: 8,
            max_queue_depth: 1,
            enable_debug_endpoints: false,
            ..ServerConfig::default()
        },
    )
    .unwrap();
    let handle = state.registry.resolve("").unwrap();
    let _occupied = handle
        .engine
        .generation_capacity
        .clone()
        .try_acquire_owned()
        .unwrap();

    let response = app(state)
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "messages": [{"role": "user", "content": "hello"}],
                        "max_tokens": 1
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
    assert_eq!(response.headers()[header::RETRY_AFTER], "1");
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let body: Value = serde_json::from_slice(&body).unwrap();
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("generation capacity exceeded")
    );
}

#[tokio::test]
async fn stalled_output_route_does_not_block_another_completion() {
    let model_dir =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm-scatter");
    let engine = Engine::from_dir(&model_dir, EngineConfig::default()).unwrap();
    let driver = EngineDriver::start(engine, 2, 2);
    let slow_request: ChatCompletionRequest = serde_json::from_value(json!({
        "model": "tiny-llm-scatter",
        "messages": [{"role": "user", "content": "hello"}],
        "max_tokens": 8
    }))
    .unwrap();
    let fast_request: ChatCompletionRequest = serde_json::from_value(json!({
        "model": "tiny-llm-scatter",
        "messages": [{"role": "user", "content": "world"}],
        "max_tokens": 2
    }))
    .unwrap();
    let (slow_tx, _slow_rx) = mpsc::channel(1);
    let slow_permit = driver
        .generation_capacity
        .clone()
        .try_acquire_owned()
        .unwrap();
    driver
        .commands
        .send(DriverCommand::Generate {
            session_id: None,
            request: Box::new(build_generate_request(&slow_request)),
            events: slow_tx,
            permit: slow_permit,
        })
        .await
        .unwrap();
    let fast_rx = driver
        .generate(None, build_generate_request(&fast_request))
        .await
        .unwrap();

    let fast_result = timeout(Duration::from_secs(5), collect_generation_result(fast_rx))
        .await
        .expect("fast request timed out behind stalled consumer")
        .expect("fast request failed");
    assert_eq!(fast_result.token_ids.len(), 2);
}

#[test]
fn pipeline_input_tensor_carries_num_tiles_for_image() {
    use crate::driver::PipelineInputTensor;

    let tensor = PipelineInputTensor {
        endpoint: "encoder.pixel_values".to_string(),
        data: vec![0.0; 12],
        shape: vec![1, 3, 2, 2],
        num_tiles: Some(4),
    };
    assert_eq!(tensor.num_tiles, Some(4));
}

#[test]
fn pipeline_input_tensor_num_tiles_none_for_audio() {
    use crate::driver::PipelineInputTensor;

    let tensor = PipelineInputTensor {
        endpoint: "encoder.audio_features".to_string(),
        data: vec![0.0; 8],
        shape: vec![1, 2, 4],
        num_tiles: None,
    };
    assert_eq!(tensor.num_tiles, None);
}

#[tokio::test]
async fn image_load_and_preprocess_populates_num_tiles() {
    let spec = crate::image_input::VisionInputSpec::from_input(
        "encoder.pixel_values".to_string(),
        // shape [N, C, H, W] — N is the batch/tile dimension
        &[1, 3, 4, 4],
    )
    .expect("valid spec");
    let tensor = crate::image_input::load_and_preprocess(&[tiny_png_data_uri()], &spec)
        .await
        .expect("preprocess succeeds");
    // The preprocessor always produces at least one tile.
    assert!(tensor.num_tiles >= 1, "num_tiles must be at least 1");
}

// ── KV cache dtype CLI/env surface tests ─────────────────────────────────────

/// Parse each accepted KV-cache dtype string using the same function that the
/// server binary uses (`parse_kv_cache_dtype`), and verify that the result is
/// threaded through `ServerConfig.engine_config.kv_cache_dtype`.
#[test]
fn kv_cache_dtype_parses_all_accepted_values() {
    use crate::state::parse_kv_cache_dtype;
    use onnx_genai_engine::KvDType;

    for (input, expected) in [
        ("f32", KvDType::F32),
        ("fp32", KvDType::F32),
        ("float32", KvDType::F32),
        ("int8", KvDType::Int8),
        ("fp8_e4m3fn", KvDType::Fp8E4M3Fn),
        ("float8_e4m3fn", KvDType::Fp8E4M3Fn),
        ("fp8_e5m2", KvDType::Fp8E5M2),
        ("float8_e5m2", KvDType::Fp8E5M2),
    ] {
        let parsed = parse_kv_cache_dtype(input)
            .unwrap_or_else(|_| panic!("expected '{input}' to parse successfully"));
        assert_eq!(
            parsed, expected,
            "'{input}' should parse to {expected:?}"
        );
    }
}

#[test]
fn kv_cache_dtype_rejects_garbage_values() {
    use crate::state::parse_kv_cache_dtype;

    for bad in ["fp4", "nope", "", "int4", "float64"] {
        assert!(
            parse_kv_cache_dtype(bad).is_err(),
            "'{bad}' should be rejected as an invalid KV dtype"
        );
    }
}

#[test]
fn server_config_engine_config_kv_cache_dtype_defaults_to_f32() {
    use onnx_genai_engine::KvDType;
    let config = ServerConfig::default();
    assert_eq!(
        config.engine_config.kv_cache_dtype,
        KvDType::F32,
        "default ServerConfig must use F32 KV storage"
    );
}

#[test]
fn server_config_engine_config_kv_cache_dtype_can_be_set() {
    use onnx_genai_engine::KvDType;
    let config = ServerConfig {
        engine_config: EngineConfig {
            kv_cache_dtype: KvDType::Fp8E4M3Fn,
            ..EngineConfig::default()
        },
        ..ServerConfig::default()
    };
    assert_eq!(config.engine_config.kv_cache_dtype, KvDType::Fp8E4M3Fn);
}

// ── M2: multi-model routing tests ────────────────────────────────────────────

/// Load the tiny-llm fixture twice under two different ids to exercise
/// multi-model routing without requiring a second distinct fixture.
fn two_model_state() -> AppState {
    let path =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    let specs = vec![
        ModelSpec { id: "model-a".to_string(), path: path.clone(), eager: true },
        ModelSpec { id: "model-b".to_string(), path: path.clone(), eager: true },
    ];
    AppState::load_from_specs(specs, ServerConfig::default()).expect("load two tiny-llm fixtures")
}

#[tokio::test]
async fn named_model_routes_to_the_correct_handle() {
    let router = app(two_model_state());
    // Request for model-a returns 200 and echoes model-a in the response.
    let resp = router
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "model-a",
                        "messages": [{"role": "user", "content": "hello"}],
                        "max_tokens": 1,
                        "temperature": 0.0
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body: Value =
        serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.unwrap()).unwrap();
    assert_eq!(body["model"], "model-a");
}

#[tokio::test]
async fn unknown_named_model_returns_404() {
    let router = app(two_model_state());
    let resp = router
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "does-not-exist",
                        "messages": [{"role": "user", "content": "hello"}],
                        "max_tokens": 1
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    let body: Value =
        serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.unwrap()).unwrap();
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("does-not-exist"),
        "error should name the unknown model: {body}"
    );
}

#[tokio::test]
async fn empty_model_field_falls_back_to_default() {
    let router = app(two_model_state());
    // Sending an empty string for model should resolve to the first loaded model.
    let resp = router
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "",
                        "messages": [{"role": "user", "content": "hello"}],
                        "max_tokens": 1,
                        "temperature": 0.0
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    // Should succeed (200) – empty model falls back to the default, not 404.
    assert_eq!(resp.status(), StatusCode::OK);
}

#[tokio::test]
async fn models_endpoint_lists_all_loaded_models() {
    let router = app(two_model_state());
    let resp = router
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/v1/models")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body: Value =
        serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.unwrap()).unwrap();
    let ids: Vec<&str> = body["data"]
        .as_array()
        .unwrap()
        .iter()
        .map(|obj| obj["id"].as_str().unwrap())
        .collect();
    assert!(ids.contains(&"model-a"), "model-a not in /v1/models: {body}");
    assert!(ids.contains(&"model-b"), "model-b not in /v1/models: {body}");
    assert_eq!(ids.len(), 2);
}

#[tokio::test]
async fn unknown_model_returns_404_on_completions_endpoint() {
    let router = app(tiny_state());
    let resp = router
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "no-such-model",
                        "prompt": "hello",
                        "max_tokens": 1
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn single_model_startup_still_works_via_load_with_config() {
    // Regression guard: the existing load_with_config / single-model path must
    // behave identically to M1.
    let model_dir =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    let state =
        AppState::load_with_config(&model_dir, Some("tiny-llm".to_string()), ServerConfig::default())
            .expect("single-model load must still work");
    // Registry has exactly one entry with the expected id.
    assert_eq!(state.registry.ids().len(), 1);
    assert_eq!(state.registry.default_id().as_deref(), Some("tiny-llm"));

    let resp = app(state)
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "tiny-llm",
                        "messages": [{"role": "user", "content": "ping"}],
                        "max_tokens": 1,
                        "temperature": 0.0
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
}

// ── M3: runtime load/unload + LRU eviction + lazy load tests ─────────────────

/// Build a two-model state where `model-a` is eager and `model-b` is lazy.
/// Both are backed by the tiny-llm fixture. `config` lets callers toggle admin
/// endpoints and the loaded-model cap.
fn lazy_state(config: ServerConfig) -> AppState {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-llm");
    let specs = vec![
        ModelSpec { id: "model-a".to_string(), path: path.clone(), eager: true },
        ModelSpec { id: "model-b".to_string(), path: path.clone(), eager: false },
    ];
    AppState::load_from_specs(specs, config).expect("load lazy two-model state")
}

fn chat_request(model: &str) -> Request<Body> {
    Request::builder()
        .method("POST")
        .uri("/v1/chat/completions")
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(
            json!({
                "model": model,
                "messages": [{"role": "user", "content": "hello"}],
                "max_tokens": 1,
                "temperature": 0.0
            })
            .to_string(),
        ))
        .unwrap()
}

async fn json_body(resp: axum::response::Response) -> Value {
    serde_json::from_slice(&to_bytes(resp.into_body(), usize::MAX).await.unwrap()).unwrap()
}

#[tokio::test]
async fn lazy_model_is_loaded_on_first_request() {
    let state = lazy_state(ServerConfig::default());
    // Only the eager model is loaded at startup.
    assert_eq!(state.registry.ids(), vec!["model-a"]);
    assert!(state.registry.contains_available("model-b"));

    // Routing to the lazy model triggers a load and succeeds.
    let resp = app(state.clone())
        .oneshot(chat_request("model-b"))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = json_body(resp).await;
    assert_eq!(body["model"], "model-b");

    // The shared registry now has both models loaded.
    let mut ids = state.registry.ids();
    ids.sort();
    assert_eq!(ids, vec!["model-a", "model-b"]);
}

#[tokio::test]
async fn admin_load_then_route_to_lazy_model() {
    let state = lazy_state(ServerConfig {
        enable_admin_endpoints: true,
        ..ServerConfig::default()
    });

    // Admin-load the lazy model.
    let resp = app(state.clone())
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/admin/models/model-b/load")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    assert!(state.registry.resolve("model-b").is_some());

    // Subsequent routing works without re-loading.
    let resp = app(state.clone())
        .oneshot(chat_request("model-b"))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
}

#[tokio::test]
async fn admin_unload_then_lazy_reload() {
    let state = lazy_state(ServerConfig {
        enable_admin_endpoints: true,
        ..ServerConfig::default()
    });
    // Unload the eager, default model.
    let resp = app(state.clone())
        .oneshot(
            Request::builder()
                .method("DELETE")
                .uri("/v1/admin/models/model-a")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NO_CONTENT);
    assert!(state.registry.resolve("model-a").is_none());
    // The spec is retained for lazy reload.
    assert!(state.registry.contains_available("model-a"));

    // A subsequent request for the default (empty model) lazily reloads it.
    let resp = app(state.clone())
        .oneshot(chat_request(""))
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    assert!(state.registry.resolve("model-a").is_some());
}

#[tokio::test]
async fn admin_unload_unknown_model_returns_404() {
    let state = lazy_state(ServerConfig {
        enable_admin_endpoints: true,
        ..ServerConfig::default()
    });
    // model-b is available but not loaded → unload is a 404.
    let resp = app(state)
        .oneshot(
            Request::builder()
                .method("DELETE")
                .uri("/v1/admin/models/model-b")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn admin_load_unknown_model_returns_404() {
    let state = lazy_state(ServerConfig {
        enable_admin_endpoints: true,
        ..ServerConfig::default()
    });
    let resp = app(state)
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/admin/models/no-such-model/load")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn max_loaded_models_evicts_least_recently_used() {
    let state = lazy_state(ServerConfig {
        enable_admin_endpoints: true,
        max_loaded_models: Some(1),
        ..ServerConfig::default()
    });
    // model-a is loaded at startup (cap = 1).
    assert_eq!(state.registry.ids(), vec!["model-a"]);

    // Loading model-b must evict model-a to respect the cap.
    let resp = app(state.clone())
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/admin/models/model-b/load")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    assert_eq!(state.registry.ids(), vec!["model-b"], "model-a should be evicted");
    assert!(state.registry.contains_available("model-a"));
}

#[tokio::test]
async fn admin_list_reports_loaded_and_available() {
    let state = lazy_state(ServerConfig {
        enable_admin_endpoints: true,
        ..ServerConfig::default()
    });
    let resp = app(state)
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/v1/admin/models")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    let body = json_body(resp).await;
    let entries = body["data"].as_array().unwrap();
    assert_eq!(entries.len(), 2);
    let a = entries.iter().find(|e| e["id"] == "model-a").unwrap();
    let b = entries.iter().find(|e| e["id"] == "model-b").unwrap();
    assert_eq!(a["loaded"], true);
    assert_eq!(a["is_default"], true);
    assert!(a["last_request_at"].is_number());
    assert_eq!(b["loaded"], false);
    assert_eq!(b["is_default"], false);
    assert!(b["last_request_at"].is_null());
}

#[tokio::test]
async fn admin_endpoints_return_404_when_gate_is_off() {
    // Admin endpoints disabled (default): routes are not mounted.
    let state = lazy_state(ServerConfig::default());
    for (method, uri) in [
        ("GET", "/v1/admin/models"),
        ("POST", "/v1/admin/models/model-b/load"),
        ("DELETE", "/v1/admin/models/model-a"),
    ] {
        let resp = app(state.clone())
            .oneshot(
                Request::builder()
                    .method(method)
                    .uri(uri)
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(
            resp.status(),
            StatusCode::NOT_FOUND,
            "{method} {uri} must 404 when admin endpoints are disabled"
        );
    }
}

#[tokio::test]
async fn empty_model_field_falls_back_to_default_on_embeddings() {
    // An empty `model` field on /v1/embeddings must resolve to the registry's
    // default model and return 200, not 400.
    let model_dir =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-mtp-full");
    let state = AppState::load(&model_dir, Some("tiny-mtp-full".to_string()))
        .expect("load tiny-mtp-full fixture");
    let resp = app(state)
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/embeddings")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "",
                        "input": "hello"
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
}

#[tokio::test]
async fn unknown_model_returns_404_on_embeddings_endpoint() {
    let model_dir =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/tiny-mtp-full");
    let state = AppState::load(&model_dir, Some("tiny-mtp-full".to_string()))
        .expect("load tiny-mtp-full fixture");
    let resp = app(state)
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/embeddings")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(
                    json!({
                        "model": "no-such-model",
                        "input": "hello"
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn concurrent_lazy_loads_of_same_id_load_once() {
    let state = lazy_state(ServerConfig::default());
    // Fire many concurrent requests for the same lazy model.
    let mut handles = Vec::new();
    for _ in 0..8 {
        let state = state.clone();
        handles.push(tokio::spawn(async move {
            app(state).oneshot(chat_request("model-b")).await.unwrap().status()
        }));
    }
    for handle in handles {
        assert_eq!(handle.await.unwrap(), StatusCode::OK);
    }
    // Exactly one loaded instance of model-b exists in the registry.
    assert!(state.registry.resolve("model-b").is_some());
    let mut ids = state.registry.ids();
    ids.sort();
    assert_eq!(ids, vec!["model-a", "model-b"]);
}