switchyard-server 0.2.0

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

//! Integration tests for the libsy Rust server.

use std::collections::{BTreeMap, HashSet};
use std::convert::Infallible;
use std::error::Error;
use std::io::Write;
use std::sync::Arc;

use axum::body::{Body, Bytes};
use axum::extract::{DefaultBodyLimit, State};
use axum::http::{Request as HttpRequest, StatusCode};
use axum::response::sse::{Event, Sse};
use axum::response::{IntoResponse, Response as HttpResponse};
use axum::routing::post;
use axum::{Json, Router};
use http_body_util::BodyExt;
use libsy::{Algorithm, LlmTarget, LlmTargetSet, Random};
use serde_json::{Value, json};
use switchyard_llm_client::{Backend, HttpBackendConfig, ModelConfig, TranslatingLlmClient};
use switchyard_protocol::RoutedLlmClient;
use switchyard_server::config::load_server_state;
use switchyard_server::{ServerState, build_switchyard_router};
use tokio::net::TcpListener;
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tower::ServiceExt;

type TestError = Box<dyn Error + Send + Sync>;
type TestResult<T = ()> = Result<T, TestError>;

const ROUTE_MODEL: &str = "switchyard/random";
const VERSION: &str = env!("CARGO_PKG_VERSION");

struct MockUpstream {
    base_url: String,
    calls: Arc<Mutex<Vec<Value>>>,
    task: JoinHandle<()>,
}

impl MockUpstream {
    async fn start() -> TestResult<Self> {
        let calls = Arc::new(Mutex::new(Vec::new()));
        let app = Router::new()
            .route("/v1/chat/completions", post(upstream_chat))
            .route("/v1/messages/count_tokens", post(upstream_count_tokens))
            .layer(DefaultBodyLimit::disable())
            .with_state(Arc::clone(&calls));
        let listener = TcpListener::bind("127.0.0.1:0").await?;
        let addr = listener.local_addr()?;
        let task = tokio::spawn(async move {
            if let Err(error) = axum::serve(listener, app).await {
                tracing::error!(error = %error, "mock upstream stopped");
            }
        });
        Ok(Self {
            base_url: format!("http://{addr}/v1"),
            calls,
            task,
        })
    }
}

impl Drop for MockUpstream {
    fn drop(&mut self) {
        self.task.abort();
    }
}

async fn upstream_chat(
    State(calls): State<Arc<Mutex<Vec<Value>>>>,
    Json(body): Json<Value>,
) -> HttpResponse {
    calls.lock().await.push(body.clone());
    if body["messages"][0]["content"] == "fail" {
        return (
            StatusCode::IM_A_TEAPOT,
            Json(json!({"error": {"message": "upstream rejected request"}})),
        )
            .into_response();
    }
    if body["messages"][0]["content"] == "auth-fail" {
        return (
            StatusCode::UNAUTHORIZED,
            Json(json!({"error": {"message": "upstream authentication failed"}})),
        )
            .into_response();
    }

    let model = body["model"].as_str().unwrap_or("unknown").to_string();
    let prompt = body["messages"][0]["content"].as_str().unwrap_or("");
    if (model == "model/weak" && prompt == "unavailable") || prompt == "all-unavailable" {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(json!({"error": {"message": "upstream is unavailable"}})),
        )
            .into_response();
    }
    if model == "model/weak" && body["messages"][0]["content"] == "overflow" {
        return (
            StatusCode::BAD_REQUEST,
            Json(json!({
                "error": {
                    "code": "context_length_exceeded",
                    "message": "request exceeds this model's context window"
                }
            })),
        )
            .into_response();
    }
    if body["stream"].as_bool() == Some(true) {
        if body["messages"][0]["content"] == "stream-error" {
            let events = [
                json!({"id": "chatcmpl-stream-error", "model": model, "choices": [{"index": 0, "delta": {"role": "assistant"}}]}).to_string(),
                json!({"id": "chatcmpl-stream-error", "model": model, "choices": [{"index": 0, "delta": {"content": "before"}}]}).to_string(),
                json!({"id": "chatcmpl-stream-error", "model": model, "choices": [{"index": 0, "delta": {"content": "still here"}}], "usage": {"prompt_tokens": 6, "completion_tokens": 2, "total_tokens": 8}}).to_string(),
                json!({"error": {"message": "upstream stream failed", "type": "server_error"}}).to_string(),
            ];
            let stream = futures_util::stream::iter(
                events
                    .into_iter()
                    .map(|data| Ok::<Event, Infallible>(Event::default().data(data))),
            );
            return Sse::new(stream).into_response();
        }
        let events = [
            json!({"id": "chatcmpl-stream", "model": model, "choices": [{"index": 0, "delta": {"role": "assistant"}}]}).to_string(),
            json!({"id": "chatcmpl-stream", "model": model, "choices": [{"index": 0, "delta": {"content": "hello"}}]}).to_string(),
            json!({"id": "chatcmpl-stream", "model": model, "choices": [{"index": 0, "delta": {"content": "-partial"}}], "usage": {"prompt_tokens": 5, "completion_tokens": 1, "total_tokens": 6, "prompt_tokens_details": {"cached_tokens": 2, "cache_creation_tokens": 1}}}).to_string(),
            json!({"id": "chatcmpl-stream", "model": model, "choices": [{"index": 0, "delta": {"content": "-final"}}], "usage": {"prompt_tokens": 12, "completion_tokens": 5, "total_tokens": 17, "prompt_tokens_details": {"cached_tokens": 7, "cache_creation_tokens": 2}, "completion_tokens_details": {"reasoning_tokens": 3}}}).to_string(),
            json!({"id": "chatcmpl-stream", "model": model, "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}).to_string(),
            "[DONE]".to_string(),
        ];
        let stream = futures_util::stream::iter(
            events
                .into_iter()
                .map(|data| Ok::<Event, Infallible>(Event::default().data(data))),
        );
        return Sse::new(stream).into_response();
    }

    let custom_target_schema = body
        .pointer("/response_format/json_schema/schema/properties/decision/properties/target")
        .is_some();
    let requests_invalid_verdict = body["messages"].as_array().is_some_and(|messages| {
        messages.iter().any(|message| {
            message["content"]
                .as_str()
                .is_some_and(|content| content.contains("invalid verdict"))
        })
    });
    let content = if model == "model/classifier" && custom_target_schema {
        if requests_invalid_verdict {
            r#"{"decision":{"target":"unknown"}}"#
        } else {
            r#"{"decision":{"target":"premium"}}"#
        }
    } else if model == "model/classifier"
        && body
            .pointer("/response_format/json_schema/schema/properties/escalate")
            .is_some()
    {
        r#"{"escalate":false,"reason":"making progress"}"#
    } else if model == "model/classifier" {
        r#"{"crux":"bounded task","primary_rule":"SUP-1","capability_boundary":"supported","p_solve":0.9}"#
    } else {
        "ok"
    };
    Json(json!({
        "id": "chatcmpl-test",
        "object": "chat.completion",
        "model": model,
        "choices": [{
            "index": 0,
            "message": {"role": "assistant", "content": content},
            "finish_reason": "stop"
        }],
        "usage": {
            "prompt_tokens": 10,
            "completion_tokens": 2,
            "total_tokens": 12,
            "prompt_tokens_details": {"cached_tokens": 7}
        }
    }))
    .into_response()
}

async fn upstream_count_tokens(
    State(calls): State<Arc<Mutex<Vec<Value>>>>,
    Json(body): Json<Value>,
) -> HttpResponse {
    calls.lock().await.push(body.clone());
    Json(json!({"input_tokens": 7})).into_response()
}

fn random_state(base_url: &str, routes: &[(&str, &[&str])]) -> TestResult<ServerState> {
    let backend = Backend::OpenAiChat(HttpBackendConfig {
        base_url: base_url.to_string(),
        api_key: Some("test-key".to_string()),
        extra_headers: BTreeMap::new(),
        extra_body: BTreeMap::new(),
        max_retries: 0,
    });
    let target_models = routes
        .iter()
        .flat_map(|(_, targets)| targets.iter().copied())
        .collect::<HashSet<_>>();
    let model_configs = target_models
        .into_iter()
        .map(|model| ModelConfig::new(model, backend.clone(), None))
        .collect::<Vec<_>>();
    let client: Arc<dyn RoutedLlmClient> = Arc::new(TranslatingLlmClient::new(&model_configs)?);
    let entries = routes
        .iter()
        .map(|(route_model, targets)| {
            let target_set = LlmTargetSet::new(
                targets
                    .iter()
                    .map(|model| LlmTarget {
                        semantic_name: (*model).to_string(),
                        llm_client: Some(Arc::clone(&client)),
                    })
                    .collect(),
            );
            let algorithm: Arc<dyn Algorithm> = Arc::new(Random::new(target_set, None, None)?);
            Ok(((*route_model).to_string(), algorithm))
        })
        .collect::<TestResult<Vec<_>>>()?;
    Ok(ServerState::new(entries)?)
}

async fn test_app(routes: &[(&str, &[&str])]) -> TestResult<(MockUpstream, Router)> {
    let upstream = MockUpstream::start().await?;
    let app = build_switchyard_router(random_state(&upstream.base_url, routes)?);
    Ok((upstream, app))
}

fn empty_token_totals() -> Value {
    json!({
        "prompt": 0,
        "completion": 0,
        "cached": 0,
        "cache_creation": 0,
        "reasoning": 0,
        "total": 0
    })
}

#[tokio::test]
async fn stats_exposes_the_exact_empty_schema_and_no_legacy_alias() -> TestResult {
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;
    let response = send(&app, "GET", "/v1/stats", None).await?;
    assert_eq!(response.status, StatusCode::OK);
    assert_eq!(
        response.json()?,
        json!({
            "total_requests": 0,
            "total_errors": 0,
            "total_tokens": empty_token_totals(),
            "models": {},
            "tiers": {},
            "routing_overhead": {
                "count": 0,
                "total_ms": 0.0,
                "min_ms": 0.0,
                "max_ms": 0.0,
                "avg_ms": 0.0,
                "p50_ms": 0.0,
                "p99_ms": 0.0
            },
            "routing_fallbacks": {
                "context_window": 0,
                "unavailable": 0
            },
            "classifier": {
                "total_requests": 0,
                "total_errors": 0,
                "total_tokens": empty_token_totals(),
                "models": {},
            },
        })
    );
    assert_eq!(
        send(&app, "GET", "/v1/routing/stats", None).await?.status,
        StatusCode::NOT_FOUND
    );
    Ok(())
}

#[tokio::test]
async fn stats_accumulates_buffered_success_error_and_shared_routes() -> TestResult {
    let (_upstream, app) = test_app(&[
        ("switchyard/one", &["gemini-3.5-flash"]),
        ("switchyard/two", &["model/unknown"]),
    ])
    .await?;
    for route in ["switchyard/one", "switchyard/two"] {
        assert_eq!(
            send(
                &app,
                "POST",
                "/v1/chat/completions",
                Some(json!({
                    "model": route,
                    "messages": [{"role": "user", "content": "hello"}]
                })),
            )
            .await?
            .status,
            StatusCode::OK
        );
    }
    assert_eq!(
        send(
            &app,
            "POST",
            "/v1/chat/completions",
            Some(json!({
                "model": "switchyard/one",
                "messages": [{"role": "user", "content": "fail"}]
            })),
        )
        .await?
        .status,
        StatusCode::IM_A_TEAPOT
    );

    let stats = send(&app, "GET", "/v1/stats", None).await?.json()?;
    assert_eq!(stats["total_requests"], 3);
    assert_eq!(stats["total_errors"], 1);
    assert_eq!(
        stats["total_tokens"],
        json!({
            "prompt": 20,
            "completion": 4,
            "cached": 14,
            "cache_creation": 0,
            "reasoning": 0,
            "total": 24
        })
    );
    assert_eq!(stats["models"]["gemini-3.5-flash"]["calls"], 1);
    assert_eq!(stats["models"]["gemini-3.5-flash"]["errors"], 1);
    assert_eq!(stats["models"]["model/unknown"]["calls"], 1);
    assert_eq!(stats["routing_overhead"]["count"], 2);
    Ok(())
}

#[tokio::test]
async fn stats_reset_returns_confirmation_and_clears_all_stats() -> TestResult {
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;
    assert_eq!(
        send(
            &app,
            "POST",
            "/v1/chat/completions",
            Some(json!({
                "model": ROUTE_MODEL,
                "messages": [{"role": "user", "content": "hello"}]
            })),
        )
        .await?
        .status,
        StatusCode::OK
    );

    let reset = send(&app, "POST", "/v1/stats/reset", None).await?;
    assert_eq!(reset.status, StatusCode::OK);
    assert_eq!(reset.json()?, json!({"status": "reset"}));

    let stats = send(&app, "GET", "/v1/stats", None).await?.json()?;
    assert_eq!(stats["total_requests"], 0);
    assert_eq!(stats["total_errors"], 0);
    assert_eq!(stats["total_tokens"], empty_token_totals());
    assert_eq!(stats["models"], json!({}));
    assert_eq!(stats["tiers"], json!({}));
    assert_eq!(stats["routing_overhead"]["count"], 0);
    assert_eq!(stats["classifier"]["total_requests"], 0);
    assert_eq!(stats["classifier"]["models"], json!({}));
    Ok(())
}

#[tokio::test]
async fn metrics_exposes_switchyard_otel_instruments() -> TestResult {
    const MODEL: &str = "model/metrics-buffered";
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &[MODEL])]).await?;

    let before = send(&app, "GET", "/metrics", None).await?;
    assert_eq!(before.status, StatusCode::OK);
    assert_eq!(
        before
            .headers
            .get("content-type")
            .and_then(|value| value.to_str().ok()),
        Some("text/plain; version=0.0.4; charset=utf-8")
    );
    let seeded = before.text()?;
    for expected in [
        "# TYPE switchyard_client_responses_total counter",
        "switchyard_client_responses_total{outcome=\"success\",",
        "switchyard_client_responses_total{outcome=\"retryable_error\",",
        "switchyard_client_responses_total{outcome=\"other_error\",",
        "# TYPE switchyard_upstream_attempts_total counter",
        "switchyard_upstream_attempts_total{code=\"200\",outcome=\"success\",",
        "switchyard_upstream_attempts_total{code=\"429\",outcome=\"retryable_error\",",
        "switchyard_upstream_attempts_total{code=\"500\",outcome=\"retryable_error\",",
        "switchyard_upstream_attempts_total{code=\"504\",outcome=\"retryable_error\",",
        "switchyard_upstream_attempts_total{code=\"none\",outcome=\"retryable_error\",",
        "# TYPE switchyard_router_retry_recovered_total counter",
        "switchyard_router_retry_recovered_total{otel_scope_name=\"switchyard\"} 0",
    ] {
        assert!(
            seeded.contains(expected),
            "missing seeded {expected:?} in metrics:\n{seeded}"
        );
    }

    let response = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": "hello"}]
        })),
    )
    .await?;
    assert_eq!(response.status, StatusCode::OK);

    let after = send(&app, "GET", "/metrics", None).await?;
    let metrics = after.text()?;
    for expected in [
        "# TYPE switchyard_build_info gauge",
        &format!("switchyard_build_info{{version=\"{VERSION}\""),
        "# TYPE switchyard_total_requests gauge",
        "# TYPE switchyard_total_errors gauge",
        "# TYPE switchyard_requests_total counter",
        "# TYPE switchyard_model_call_latency_ms histogram",
        "switchyard_client_responses_total{outcome=\"success\",",
        "switchyard_upstream_attempts_total{code=\"200\",outcome=\"success\",",
        "# TYPE switchyard_runs_total counter",
        "# TYPE switchyard_llm_calls_total counter",
        "# TYPE switchyard_run_duration_ms histogram",
        "# TYPE switchyard_llm_call_duration_ms histogram",
        "# TYPE switchyard_prompt_tokens_total counter",
        "# TYPE switchyard_completion_tokens_total counter",
        "# TYPE switchyard_cached_tokens_total counter",
        "# TYPE switchyard_total_latency_ms histogram",
        "# TYPE switchyard_routing_overhead_ms histogram",
        "algorithm=\"random\"",
        &format!("selected_model=\"{MODEL}\""),
    ] {
        assert!(
            metrics.contains(expected),
            "missing {expected:?} in metrics:\n{metrics}"
        );
    }
    for (name, expected_delta) in [
        ("switchyard_prompt_tokens_total", 10.0),
        ("switchyard_completion_tokens_total", 2.0),
        ("switchyard_cached_tokens_total", 7.0),
        ("switchyard_total_latency_ms_count", 1.0),
    ] {
        assert_eq!(
            metric_delta(seeded, metrics, name, &[("model", MODEL)]),
            Some(expected_delta),
            "unexpected delta for {name}"
        );
    }
    // A sub-millisecond boundary exists only because of the server's bucket view.
    assert!(
        metric_line(
            metrics,
            "switchyard_routing_overhead_ms_bucket",
            &[("algorithm", "random"), ("le", "0.1")]
        )
        .is_some()
    );
    assert!(
        metric_line(
            metrics,
            "switchyard_cache_creation_tokens_total",
            &[("model", MODEL)]
        )
        .is_none()
    );
    assert!(
        metric_line(
            metrics,
            "switchyard_reasoning_tokens_total",
            &[("model", MODEL)]
        )
        .is_none()
    );
    for metric in [
        "switchyard_prompt_tokens_total",
        "switchyard_completion_tokens_total",
        "switchyard_cached_tokens_total",
        "switchyard_total_latency_ms_count",
    ] {
        let line = metric_line(metrics, metric, &[("model", MODEL)])
            .ok_or_else(|| format!("missing {metric} series for {MODEL}"))?;
        assert!(!line.contains("tier="), "unexpected tier label in {line}");
    }
    Ok(())
}

#[tokio::test]
async fn accepts_requests_larger_than_the_axum_default_body_limit() -> TestResult {
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;
    let content = "x".repeat(2 * 1024 * 1024);

    let response = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": content}]
        })),
    )
    .await?;

    assert_eq!(response.status, StatusCode::OK);
    Ok(())
}

fn load_test_config(toml: &str) -> TestResult<ServerState> {
    let mut config = tempfile::Builder::new()
        .prefix("switchyard-server-config-")
        .suffix(".toml")
        .tempfile()?;
    config.write_all(toml.as_bytes())?;
    config.flush()?;
    Ok(load_server_state(config.path())?)
}

/// A `random` route that selects `first` before any request-local fallback.
fn fallback_state(base_url: &str) -> TestResult<ServerState> {
    load_test_config(&format!(
        r#"
schema_version = 1

[llm_clients.mock]
format = "openai_chat"
base_url = "{base_url}"
max_retries = 0

[targets.first]
id = "{first}"
llm_client = "mock"

[targets.second]
id = "{second}"
llm_client = "mock"

[routes.random]
id = "{ROUTE_MODEL}"
type = "random"
targets = ["first", "second"]
weights = [1, 0]
"#,
        first = "model/weak",
        second = "model/strong",
    ))
}

async fn send(app: &Router, method: &str, path: &str, body: Option<Value>) -> TestResult<Response> {
    send_with_headers(app, method, path, body, &[]).await
}

async fn send_with_headers(
    app: &Router,
    method: &str,
    path: &str,
    body: Option<Value>,
    headers: &[(&str, &str)],
) -> TestResult<Response> {
    let mut builder = HttpRequest::builder().method(method).uri(path);
    for (name, value) in headers {
        builder = builder.header(*name, *value);
    }
    let request_body = if let Some(body) = body {
        builder = builder.header("content-type", "application/json");
        Body::from(serde_json::to_vec(&body)?)
    } else {
        Body::empty()
    };
    let response = app.clone().oneshot(builder.body(request_body)?).await?;
    let status = response.status();
    let headers = response.headers().clone();
    let bytes = response.into_body().collect().await?.to_bytes();
    Ok(Response {
        status,
        headers,
        bytes,
    })
}

struct Response {
    status: StatusCode,
    headers: axum::http::HeaderMap,
    bytes: Bytes,
}

impl Response {
    fn json(&self) -> TestResult<Value> {
        Ok(serde_json::from_slice(&self.bytes)?)
    }

    fn text(&self) -> TestResult<&str> {
        Ok(std::str::from_utf8(&self.bytes)?)
    }
}

fn metric_line<'a>(metrics: &'a str, name: &str, labels: &[(&str, &str)]) -> Option<&'a str> {
    metrics.lines().find(|line| {
        line.starts_with(name)
            && labels
                .iter()
                .all(|(key, value)| line.contains(&format!("{key}=\"{value}\"")))
    })
}

fn metric_value(metrics: &str, name: &str, labels: &[(&str, &str)]) -> Option<f64> {
    metric_line(metrics, name, labels)?
        .split_whitespace()
        .last()?
        .parse()
        .ok()
}

fn metric_delta(before: &str, after: &str, name: &str, labels: &[(&str, &str)]) -> Option<f64> {
    metric_value(after, name, labels)
        .map(|after| after - metric_value(before, name, labels).unwrap_or_default())
}

fn assert_in_order(haystack: &str, needles: &[&str]) {
    let mut remainder = haystack;
    for needle in needles {
        let offset = remainder
            .find(needle)
            .unwrap_or_else(|| panic!("missing {needle:?} after prior events in:\n{haystack}"));
        remainder = &remainder[offset + needle.len()..];
    }
}

/// A critical tool error must reach the stage router's signal scorer, which reads
/// the decoded conversation. The endpoint records no inbound wire format, so a
/// scorer that parsed the raw body instead would find nothing and route every turn
/// as if the conversation had no signals at all.
#[tokio::test]
async fn stage_route_escalates_on_a_signal_in_the_conversation() -> TestResult {
    let upstream = MockUpstream::start().await?;
    let state = load_test_config(&format!(
        r#"
schema_version = 1

[llm_clients.upstream]
format = "openai_chat"
base_url = "{base_url}"

[targets.strong]
id = "model/strong"
llm_client = "upstream"

[targets.weak]
id = "model/weak"
llm_client = "upstream"

[routes.stage]
id = "switchyard/stage"
type = "stage_router"
capable_target = "strong"
efficient_target = "weak"
picker = "efficient_first"
confidence_threshold = 0.5
"#,
        base_url = upstream.base_url
    ))?;
    let app = build_switchyard_router(state);

    let response = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": "switchyard/stage",
            "messages": [
                {"role": "user", "content": "fix the build"},
                {"role": "assistant", "tool_calls": [{
                    "id": "call_1",
                    "type": "function",
                    "function": {"name": "Bash", "arguments": "{\"command\": \"cargo test\"}"}
                }]},
                {"role": "tool", "tool_call_id": "call_1", "content": "fatal runtime error: out of memory"},
            ]
        })),
    )
    .await?;

    assert_eq!(response.status, StatusCode::OK);
    assert_eq!(
        response
            .headers
            .get("x-model-router-selected-model")
            .and_then(|value| value.to_str().ok()),
        Some("model/strong"),
        "a critical error should escalate on the signals alone"
    );
    Ok(())
}

#[tokio::test]
async fn toml_config_constructs_and_serves_multiple_algorithms() -> TestResult {
    let upstream = MockUpstream::start().await?;
    let state = load_test_config(&format!(
        r#"
schema_version = 1

[llm_clients.upstream]
format = "openai_chat"
base_url = "{base_url}"

[targets.classifier]
id = "model/classifier"
llm_client = "upstream"

[targets.strong]
id = "model/strong"
llm_client = "upstream"

[targets.weak]
id = "model/weak"
llm_client = "upstream"

[routes.random]
id = "switchyard/random"
type = "random"
targets = ["weak"]

[routes.classifier]
id = "switchyard/classifier"
type = "llm_classifier"
classifier_target = "classifier"
strong_target = "strong"
weak_target = "weak"
base_threshold = 0.5

[routes.passthrough]
id = "switchyard/passthrough"
type = "passthrough"
target = "weak"

[routes.stage]
id = "switchyard/stage"
type = "stage_router"
capable_target = "strong"
efficient_target = "weak"
picker = "efficient_first"
confidence_threshold = 0.5
recent_turn_window = 3
capable_system_prompt = "diagnose before you edit"
efficient_system_prompt = "follow the settled plan"

[routes.stage.handoff_notes]
escalation_note = "the previous model was stalling"

[routes.stage.classifier]
target = "classifier"
base_threshold = 0.5
"#,
        base_url = upstream.base_url
    ))?;
    let app = build_switchyard_router(state);

    for (route, selected) in [
        ("switchyard/random", "model/weak"),
        ("switchyard/classifier", "model/weak"),
        ("switchyard/passthrough", "model/weak"),
    ] {
        let response = send(
            &app,
            "POST",
            "/v1/chat/completions",
            Some(json!({
                "model": route,
                "messages": [{"role": "user", "content": "hi"}]
            })),
        )
        .await?;
        assert_eq!(response.status, StatusCode::OK);
        assert_eq!(
            response
                .headers
                .get("x-model-router-selected-model")
                .and_then(|value| value.to_str().ok()),
            Some(selected)
        );
    }

    let calls = upstream.calls.lock().await;
    assert_eq!(calls.len(), 4);
    assert_eq!(calls[0]["model"], "model/weak");
    assert_eq!(calls[1]["model"], "model/classifier");
    assert_eq!(calls[2]["model"], "model/weak");
    assert_eq!(calls[3]["model"], "model/weak");
    drop(calls);

    let stats = send(&app, "GET", "/v1/stats", None).await?.json()?;
    assert_eq!(stats["total_requests"], 3);
    assert_eq!(stats["models"]["model/weak"]["calls"], 3);
    assert_eq!(stats["tiers"]["weak"]["calls"], 1);
    assert_eq!(stats["classifier"]["total_requests"], 1);
    assert_eq!(
        stats["classifier"]["models"]["model/classifier"]["calls"],
        1
    );
    assert_eq!(stats["classifier"]["total_tokens"]["prompt"], 10);
    Ok(())
}

#[tokio::test]
async fn custom_classifier_routes_four_targets_and_falls_back_on_an_invalid_verdict() -> TestResult
{
    let upstream = MockUpstream::start().await?;
    let state = load_test_config(&format!(
        r#"
schema_version = 1

[llm_clients.upstream]
format = "openai_chat"
base_url = "{base_url}"

[targets.classifier]
id = "model/classifier"
llm_client = "upstream"

[targets.strong]
id = "model/strong"
llm_client = "upstream"

[targets.middle]
id = "model/middle"
llm_client = "upstream"

[targets.premium]
id = "model/premium"
llm_client = "upstream"

[targets.weak]
id = "model/weak"
llm_client = "upstream"

[routes.custom]
id = "switchyard/custom"
type = "llm_classifier"
mode = "custom"
classifier_target = "classifier"
targets = ["weak", "middle", "strong", "premium"]
default_target = "strong"
prompt = "CUSTOM MULTI TARGET"
response_schema = '''
{{
  "type": "object",
  "properties": {{
    "decision": {{
      "type": "object",
      "properties": {{
        "target": {{"type": "string", "enum": ["weak", "middle", "strong", "premium"]}}
      }},
      "required": ["target"],
      "additionalProperties": false
    }}
  }},
  "required": ["decision"],
  "additionalProperties": false
}}
'''

[routes.custom.policy]
type = "target_selector"
selector = "/decision/target"
"#,
        base_url = upstream.base_url
    ))?;
    let app = build_switchyard_router(state);

    for (task, selected) in [
        ("route this task", "model/premium"),
        ("return an invalid verdict", "model/strong"),
    ] {
        let response = send(
            &app,
            "POST",
            "/v1/chat/completions",
            Some(json!({
                "model": "switchyard/custom",
                "messages": [{"role": "user", "content": task}]
            })),
        )
        .await?;
        assert_eq!(response.status, StatusCode::OK);
        assert_eq!(
            response
                .headers
                .get("x-model-router-selected-model")
                .and_then(|value| value.to_str().ok()),
            Some(selected)
        );
    }

    let calls = upstream.calls.lock().await;
    let judge_call = calls
        .iter()
        .find(|call| call["model"] == "model/classifier")
        .ok_or("custom classifier target was not called")?;
    let prompt = judge_call["messages"][0]["content"]
        .as_str()
        .ok_or("custom classifier prompt was not text")?;
    assert_eq!(prompt, "CUSTOM MULTI TARGET");
    assert_eq!(judge_call["response_format"]["type"], "json_schema");
    assert_eq!(
        judge_call["response_format"]["json_schema"]["name"],
        "switchyard_classifier_response"
    );
    assert_eq!(judge_call["response_format"]["json_schema"]["strict"], true);
    assert_eq!(
        judge_call["response_format"]["json_schema"]["schema"]["properties"]["decision"]["properties"]
            ["target"]["enum"],
        json!(["weak", "middle", "strong", "premium"])
    );
    Ok(())
}

#[tokio::test]
async fn classifier_prompt_overrides_reach_every_server_mode() -> TestResult {
    let upstream = MockUpstream::start().await?;
    let state = load_test_config(&format!(
        r#"
schema_version = 1

[llm_clients.upstream]
format = "openai_chat"
base_url = "{base_url}"

[targets.classifier]
id = "model/classifier"
llm_client = "upstream"

[targets.strong]
id = "model/strong"
llm_client = "upstream"

[targets.weak]
id = "model/weak"
llm_client = "upstream"

[routes.capability]
id = "switchyard/capability"
type = "llm_classifier"
mode = "capability"
classifier_target = "classifier"
strong_target = "strong"
weak_target = "weak"
base_threshold = 0.5
prompt = "CUSTOM CAPABILITY"

[routes.escalation]
id = "switchyard/escalation"
type = "llm_classifier"
mode = "escalation"
classifier_target = "classifier"
strong_target = "strong"
weak_target = "weak"
prompt = "CUSTOM ESCALATION"
escalation = {{ confirmations = 1 }}

[routes.stage]
id = "switchyard/stage"
type = "stage_router"
capable_target = "strong"
efficient_target = "weak"
picker = "efficient_first"
confidence_threshold = 1.0

[routes.stage.classifier]
target = "classifier"
base_threshold = 0.5
prompt = "CUSTOM STAGE"
"#,
        base_url = upstream.base_url
    ))?;
    let app = build_switchyard_router(state);

    for (route, prompt_prefix, schema_field) in [
        ("switchyard/capability", "CUSTOM CAPABILITY", "p_solve"),
        ("switchyard/escalation", "CUSTOM ESCALATION", "escalate"),
        ("switchyard/stage", "CUSTOM STAGE", "p_solve"),
    ] {
        upstream.calls.lock().await.clear();
        let response = send(
            &app,
            "POST",
            "/v1/chat/completions",
            Some(json!({
                "model": route,
                "messages": [{"role": "user", "content": "bounded task"}]
            })),
        )
        .await?;

        assert_eq!(response.status, StatusCode::OK);
        let calls = upstream.calls.lock().await;
        let judge_call = calls
            .iter()
            .find(|call| call["model"] == "model/classifier")
            .ok_or("classifier target was not called")?;
        let prompt = judge_call["messages"][0]["content"]
            .as_str()
            .ok_or("classifier prompt was not text")?;
        assert!(prompt.starts_with(prompt_prefix), "{route}: {prompt}");
        assert!(
            judge_call["response_format"]["json_schema"]["schema"]["properties"]
                .get(schema_field)
                .is_some(),
            "{route}: missing {schema_field} in {judge_call}"
        );
    }
    Ok(())
}

#[tokio::test]
async fn count_tokens_forwards_to_configured_anthropic_target() -> TestResult {
    let upstream = MockUpstream::start().await?;
    let state = load_test_config(&format!(
        r#"
schema_version = 1

[llm_clients.claude]
format = "anthropic_messages"
base_url = "{base_url}"

[targets.strong]
id = "real/opus"
llm_client = "claude"

[targets.other]
id = "real/sonnet"
llm_client = "claude"

[routes.random]
id = "switchyard/random"
type = "random"
targets = ["other", "strong"]
"#,
        base_url = upstream.base_url
    ))?;
    let app = build_switchyard_router(state);

    let response = send(
        &app,
        "POST",
        "/v1/messages/count_tokens",
        Some(json!({
            "model": "switchyard/random",
            "messages": [{"role": "user", "content": "hi"}]
        })),
    )
    .await?;
    assert_eq!(response.status, StatusCode::OK);
    assert_eq!(response.json()?["input_tokens"], 7);

    let calls = upstream.calls.lock().await;
    assert_eq!(calls.len(), 1);
    // The inbound route name is rewritten to the real upstream model.
    assert_eq!(calls[0]["model"], "real/opus");
    Ok(())
}

#[tokio::test]
async fn count_tokens_without_anthropic_target_returns_bad_request() -> TestResult {
    let upstream = MockUpstream::start().await?;
    let state = load_test_config(&format!(
        r#"
schema_version = 1

[llm_clients.upstream]
format = "openai_chat"
base_url = "{base_url}"

[targets.weak]
id = "model/weak"
llm_client = "upstream"

[routes.random]
id = "switchyard/random"
type = "random"
targets = ["weak"]
"#,
        base_url = upstream.base_url
    ))?;
    let app = build_switchyard_router(state);

    let response = send(
        &app,
        "POST",
        "/v1/messages/count_tokens",
        Some(json!({
            "model": "switchyard/random",
            "messages": [{"role": "user", "content": "hi"}]
        })),
    )
    .await?;
    assert_eq!(response.status, StatusCode::BAD_REQUEST);
    // This route has no Anthropic-format target.
    assert_eq!(
        response.json()?,
        json!({
            "type": "error",
            "error": {
                "type": "invalid_request_error",
                "message": "route has no Anthropic target for token counting"
            }
        })
    );
    Ok(())
}

#[tokio::test]
async fn routes_dispatch_and_discovery_endpoints_are_stable() -> TestResult {
    let (upstream, app) = test_app(&[
        ("switchyard/coding", &["model/code"]),
        ("switchyard/general", &["model/general"]),
    ])
    .await?;

    let health = send(&app, "GET", "/health", None).await?;
    assert_eq!(health.status, StatusCode::OK);
    assert_eq!(health.json()?, json!({"status": "ok"}));

    let models = send(&app, "GET", "/v1/models", None).await?;
    assert_eq!(models.status, StatusCode::OK);
    assert_eq!(
        models.json()?["model_pool"],
        json!(["switchyard/coding", "switchyard/general"])
    );

    let missing = send(&app, "GET", "/missing", None).await?;
    assert_eq!(missing.status, StatusCode::NOT_FOUND);
    assert_eq!(missing.json()?["error"]["code"], "endpoint_not_found");

    for (route_model, target_model) in [
        ("switchyard/general", "model/general"),
        ("switchyard/coding", "model/code"),
    ] {
        let response = send(
            &app,
            "POST",
            "/v1/chat/completions",
            Some(json!({
                "model": route_model,
                "messages": [{"role": "user", "content": "hi"}]
            })),
        )
        .await?;
        assert_eq!(response.status, StatusCode::OK);
        assert_eq!(
            response
                .headers
                .get("x-model-router-selected-model")
                .and_then(|value| value.to_str().ok()),
            Some(target_model)
        );
    }

    let calls = upstream.calls.lock().await;
    assert_eq!(calls[0]["model"], "model/general");
    assert_eq!(calls[1]["model"], "model/code");
    Ok(())
}

#[tokio::test]
async fn models_endpoint_reports_declared_route_capabilities_and_null_when_undeclared() -> TestResult
{
    const CONFIG: &str = r#"
schema_version = 1

[llm_clients.primary]
format = "openai_chat"
base_url = "https://example.test/v1"

[targets.shared]
id = "nvidia/deepseek-ai/deepseek-v4-pro"
llm_client = "primary"

[routes.declared]
id = "declared"
type = "passthrough"
target = "shared"
context_window = 1000000
tool_calling = true

[routes.restricted]
id = "restricted"
type = "passthrough"
target = "shared"
context_window = 262000
tool_calling = false

[routes.reasoning]
id = "reasoning"
type = "passthrough"
target = "shared"
reasoning = true

[routes.undeclared]
id = "undeclared"
type = "passthrough"
target = "shared"
"#;
    let app = build_switchyard_router(load_test_config(CONFIG)?);
    let models = send(&app, "GET", "/v1/models", None).await?;
    assert_eq!(models.status, StatusCode::OK);
    let body = models.json()?;
    let data = body["data"].as_array().cloned().unwrap_or_default();
    let capabilities = data
        .iter()
        .filter_map(|entry| entry["id"].as_str().map(|id| (id, &entry["capabilities"])))
        .collect::<BTreeMap<_, _>>();

    assert_eq!(capabilities["declared"]["context_window"], json!(1_000_000));
    assert_eq!(capabilities["declared"]["tool_calling"], json!(true));
    assert_eq!(capabilities["restricted"]["context_window"], json!(262_000));
    assert_eq!(capabilities["restricted"]["tool_calling"], json!(false));
    assert_eq!(capabilities["undeclared"]["context_window"], json!(null));
    assert_eq!(capabilities["undeclared"]["tool_calling"], json!(null));

    let codex_models = body["models"].as_array().cloned().unwrap_or_default();
    let codex_metadata = codex_models
        .iter()
        .filter_map(|entry| entry["slug"].as_str().map(|slug| (slug, entry)))
        .collect::<BTreeMap<_, _>>();
    // This checks the shape the server emits. That Codex 0.144.5 actually decodes it
    // (context_window: null included) is verified by a live Codex run in SWITCH-1225.
    assert_eq!(codex_metadata.len(), 4);
    assert_eq!(
        codex_metadata["declared"]["context_window"],
        json!(1_000_000)
    );
    assert_eq!(codex_metadata["declared"]["shell_type"], "shell_command");
    assert_eq!(
        codex_metadata["declared"]["apply_patch_tool_type"],
        "freeform"
    );
    // Constant fields Codex requires: a typo here would fail its decode, so pin them.
    assert_eq!(codex_metadata["declared"]["visibility"], "list");
    assert_eq!(codex_metadata["declared"]["supported_in_api"], json!(true));
    assert_eq!(codex_metadata["declared"]["web_search_tool_type"], "text");
    assert_eq!(
        codex_metadata["declared"]["input_modalities"],
        json!(["text"])
    );
    assert_eq!(
        codex_metadata["declared"]["truncation_policy"],
        json!({"mode": "tokens", "limit": 10_000})
    );
    assert_eq!(
        codex_metadata["restricted"]["context_window"],
        json!(262_000)
    );
    assert_eq!(codex_metadata["restricted"]["shell_type"], "disabled");
    assert_eq!(
        codex_metadata["restricted"]["apply_patch_tool_type"],
        json!(null)
    );
    // A reasoning route advertises the effort presets and reasoning controls.
    assert_eq!(
        codex_metadata["reasoning"]["default_reasoning_level"],
        "xhigh"
    );
    assert_eq!(
        codex_metadata["reasoning"]["supported_reasoning_levels"]
            .as_array()
            .map(Vec::len),
        Some(4)
    );
    assert_eq!(
        codex_metadata["reasoning"]["supports_reasoning_summaries"],
        json!(true)
    );
    assert_eq!(
        codex_metadata["reasoning"]["support_verbosity"],
        json!(true)
    );
    assert_eq!(codex_metadata["reasoning"]["default_verbosity"], "low");
    // An undeclared route: null context window, non-reasoning, but tools default on
    // so `switchyard launch codex` stays usable out of the box.
    assert_eq!(codex_metadata["undeclared"]["context_window"], json!(null));
    assert_eq!(
        codex_metadata["undeclared"]["supported_reasoning_levels"],
        json!([])
    );
    assert_eq!(
        codex_metadata["undeclared"]["default_reasoning_level"],
        json!(null)
    );
    assert_eq!(
        codex_metadata["undeclared"]["supports_reasoning_summaries"],
        json!(false)
    );
    assert_eq!(codex_metadata["undeclared"]["shell_type"], "shell_command");
    assert_eq!(
        codex_metadata["undeclared"]["apply_patch_tool_type"],
        "freeform"
    );
    assert_eq!(
        codex_metadata["undeclared"]["supports_parallel_tool_calls"],
        json!(true)
    );
    Ok(())
}

#[tokio::test]
async fn all_inbound_formats_run_libsy_and_return_the_caller_format() -> TestResult {
    let (upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;

    let cases = [
        (
            "/v1/chat/completions",
            json!({
                "model": ROUTE_MODEL,
                "messages": [{"role": "user", "content": "hi"}]
            }),
        ),
        (
            "/v1/messages",
            json!({
                "model": ROUTE_MODEL,
                "max_tokens": 16,
                "messages": [{"role": "user", "content": "hi"}]
            }),
        ),
        (
            "/v1/responses",
            json!({"model": ROUTE_MODEL, "input": "hi"}),
        ),
    ];

    let mut responses = Vec::new();
    for (path, body) in cases {
        responses.push(send(&app, "POST", path, Some(body)).await?);
    }

    assert!(
        responses
            .iter()
            .all(|response| response.status == StatusCode::OK)
    );
    assert_eq!(
        responses[0].json()?["choices"][0]["message"]["content"],
        "ok"
    );
    assert_eq!(responses[1].json()?["content"][0]["text"], "ok");
    assert_eq!(
        responses[2].json()?["output"][0]["content"][0]["text"],
        "ok"
    );
    assert_eq!(responses[0].json()?["usage"]["prompt_tokens"], 10);
    assert_eq!(
        responses[0].json()?["usage"]["prompt_tokens_details"]["cached_tokens"],
        7
    );
    assert_eq!(responses[1].json()?["usage"]["input_tokens"], 3);
    assert_eq!(responses[1].json()?["usage"]["cache_read_input_tokens"], 7);
    assert_eq!(responses[2].json()?["usage"]["input_tokens"], 10);
    assert_eq!(
        responses[2].json()?["usage"]["input_tokens_details"]["cached_tokens"],
        7
    );
    for response in &responses {
        assert_eq!(
            response
                .headers
                .get("x-model-router-selected-model")
                .and_then(|value| value.to_str().ok()),
            Some("model/a")
        );
        // The body names the model that answered, not the route id the caller
        // addressed, so it agrees with the routing header above.
        assert_eq!(response.json()?["model"], "model/a");
    }

    let calls = upstream.calls.lock().await;
    assert_eq!(calls.len(), 3);
    assert!(calls.iter().all(|call| call["model"] == "model/a"));
    Ok(())
}

#[tokio::test]
async fn routing_log_exposes_session_stats() -> TestResult {
    let upstream = MockUpstream::start().await?;
    let temp_dir = tempfile::tempdir()?;
    let log_path = temp_dir.path().join("routing.jsonl");
    let state = random_state(&upstream.base_url, &[(ROUTE_MODEL, &["model/a"])])?
        .with_routing_log(&log_path)?;
    let app = build_switchyard_router(state);

    let request = HttpRequest::builder()
        .method("POST")
        .uri("/v1/chat/completions")
        .header("content-type", "application/json")
        .header("proxy_x_session_id", "session-1")
        .body(Body::from(serde_json::to_vec(&json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": "hello"}]
        }))?))?;
    let response = app.clone().oneshot(request).await?;
    assert_eq!(response.status(), StatusCode::OK);

    let stats = send(
        &app,
        "GET",
        "/v1/routing/session-stats?session_id=session-1",
        None,
    )
    .await?
    .json()?;
    assert_eq!(stats["total_calls"], 1);
    assert_eq!(stats["total_prompt_tokens"], 10);
    assert_eq!(stats["total_cached_tokens"], 7);
    assert_eq!(stats["models"]["model/a"]["completion_tokens"], 2);

    let records = std::fs::read_to_string(log_path)?;
    let first: Value =
        serde_json::from_str(records.lines().next().ok_or("routing log was empty")?)?;
    assert!(
        first["ts"]
            .as_str()
            .is_some_and(|value| value.ends_with('Z'))
    );
    Ok(())
}

// Overflow history is isolated per child, cleared with the session, and not retained when a
// child lacks an agent ID.
#[tokio::test]
async fn overflow_history_is_scoped_to_agent_and_session_lifetime() -> TestResult {
    let upstream = MockUpstream::start().await?;
    let state = fallback_state(&upstream.base_url)?;
    let app = build_switchyard_router(state);
    let child_a = [
        ("x-switchyard-session-id", "shared-session"),
        ("x-switchyard-agent-id", "child-a"),
        ("x-switchyard-is-subagent", "true"),
    ];
    let root = [
        ("x-switchyard-session-id", "shared-session"),
        ("x-switchyard-agent-id", "root"),
        ("x-switchyard-is-subagent", "false"),
    ];
    let child_b = [
        ("x-switchyard-session-id", "shared-session"),
        ("x-switchyard-agent-id", "child-b"),
        ("x-switchyard-is-subagent", "true"),
    ];
    let child_without_agent_id = [
        ("x-switchyard-session-id", "shared-session"),
        ("x-switchyard-is-subagent", "true"),
    ];
    let final_root = [
        ("x-switchyard-session-id", "shared-session"),
        ("x-switchyard-agent-id", "root"),
        ("x-switchyard-is-subagent", "false"),
        ("x-switchyard-session-final", "true"),
    ];
    type Case<'a> = (&'a str, &'a [(&'a str, &'a str)], &'a [&'a str]);
    let cases: [Case<'_>; 8] = [
        (
            "overflow",
            child_a.as_slice(),
            &["model/weak", "model/strong"],
        ),
        ("fits", child_a.as_slice(), &["model/strong"]),
        ("fits", root.as_slice(), &["model/weak"]),
        ("fits", child_b.as_slice(), &["model/weak"]),
        ("fits", final_root.as_slice(), &["model/weak"]),
        ("fits", child_a.as_slice(), &["model/weak"]),
        (
            "overflow",
            child_without_agent_id.as_slice(),
            &["model/weak", "model/strong"],
        ),
        (
            "overflow",
            child_without_agent_id.as_slice(),
            &["model/weak", "model/strong"],
        ),
    ];

    for (content, headers, expected_calls) in cases {
        let previous_call_count = upstream.calls.lock().await.len();
        let response = send_with_headers(
            &app,
            "POST",
            "/v1/chat/completions",
            Some(json!({
                "model": ROUTE_MODEL,
                "messages": [{"role": "user", "content": content}]
            })),
            headers,
        )
        .await?;
        assert_eq!(response.status, StatusCode::OK);
        let expected_model = expected_calls.last().copied();
        assert_eq!(
            response
                .headers
                .get("x-model-router-selected-model")
                .and_then(|value| value.to_str().ok()),
            expected_model
        );
        let calls = upstream.calls.lock().await;
        assert_eq!(
            calls[previous_call_count..]
                .iter()
                .map(|call| call["model"].as_str().unwrap_or(""))
                .collect::<Vec<_>>(),
            expected_calls
        );
    }
    Ok(())
}

#[tokio::test]
async fn unavailable_target_fails_over_across_endpoints_and_stops_when_exhausted() -> TestResult {
    let upstream = MockUpstream::start().await?;
    let temp_dir = tempfile::tempdir()?;
    let log_path = temp_dir.path().join("routing.jsonl");
    let state = fallback_state(&upstream.base_url)?.with_routing_log(&log_path)?;
    let app = build_switchyard_router(state);
    let cases = [
        (
            "/v1/chat/completions",
            json!({
                "model": ROUTE_MODEL,
                "messages": [{"role": "user", "content": "unavailable"}]
            }),
        ),
        (
            "/v1/messages",
            json!({
                "model": ROUTE_MODEL,
                "max_tokens": 16,
                "messages": [{"role": "user", "content": "unavailable"}]
            }),
        ),
        (
            "/v1/responses",
            json!({"model": ROUTE_MODEL, "input": "unavailable"}),
        ),
    ];

    for (path, body) in cases {
        let previous_call_count = upstream.calls.lock().await.len();
        let response = send(&app, "POST", path, Some(body)).await?;
        assert_eq!(response.status, StatusCode::OK);
        assert_eq!(
            response
                .headers
                .get("x-model-router-selected-model")
                .and_then(|value| value.to_str().ok()),
            Some("model/strong")
        );
        assert_eq!(
            response
                .headers
                .get("x-model-router-rationale")
                .and_then(|value| value.to_str().ok()),
            Some("model/weak was unavailable; fell back to model/strong")
        );
        let calls = upstream.calls.lock().await;
        assert_eq!(
            calls[previous_call_count..]
                .iter()
                .map(|call| call["model"].as_str().unwrap_or(""))
                .collect::<Vec<_>>(),
            ["model/weak", "model/strong"]
        );
    }

    let stats = send(&app, "GET", "/v1/stats", None).await?.json()?;
    assert_eq!(stats["routing_fallbacks"]["unavailable"], 3);
    assert_eq!(stats["routing_fallbacks"]["context_window"], 0);

    let records = std::fs::read_to_string(&log_path)?;
    let records = records
        .lines()
        .map(serde_json::from_str::<Value>)
        .collect::<Result<Vec<_>, _>>()?;
    assert_eq!(records.len(), 3);
    assert!(records.iter().all(|record| {
        record["model"] == "model/strong" && record["fallback_reason"] == "unavailable"
    }));

    let previous_call_count = upstream.calls.lock().await.len();
    let response = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": "all-unavailable"}]
        })),
    )
    .await?;
    assert_eq!(response.status, StatusCode::SERVICE_UNAVAILABLE);
    let error = response.json()?;
    assert_eq!(error["error"]["type"], "upstream_error");
    assert_eq!(error["error"]["code"], "upstream_error");
    let calls = upstream.calls.lock().await;
    assert_eq!(
        calls[previous_call_count..]
            .iter()
            .map(|call| call["model"].as_str().unwrap_or(""))
            .collect::<Vec<_>>(),
        ["model/weak", "model/strong"]
    );
    Ok(())
}

#[tokio::test]
async fn streaming_response_is_framed_for_the_inbound_api() -> TestResult {
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;

    let response = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": "hi"}],
            "stream": true
        })),
    )
    .await?;

    assert_eq!(response.status, StatusCode::OK);
    assert!(response.text()?.contains("hello"));
    assert!(response.text()?.contains("data: [DONE]"));
    Ok(())
}

// SWITCH-922: every streaming codec must report the routed target, not the route
// id the caller addressed — the route id is meaningless to anything reading the
// trajectory (a Bench UI, a spend log, the client's own display).
#[tokio::test]
async fn streamed_response_model_names_the_served_model_not_the_route() -> TestResult {
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;

    // Each case names the JSON pointer to the model on that format's first event.
    let cases = [
        (
            "/v1/chat/completions",
            json!({
                "model": ROUTE_MODEL,
                "messages": [{"role": "user", "content": "hi"}],
                "stream": true
            }),
            vec!["model"],
        ),
        (
            "/v1/messages",
            json!({
                "model": ROUTE_MODEL,
                "max_tokens": 16,
                "messages": [{"role": "user", "content": "hi"}],
                "stream": true
            }),
            vec!["message", "model"],
        ),
        (
            "/v1/responses",
            json!({"model": ROUTE_MODEL, "input": "hi", "stream": true}),
            vec!["response", "model"],
        ),
    ];

    for (path, body, pointer) in cases {
        let response = send(&app, "POST", path, Some(body)).await?;
        assert_eq!(response.status, StatusCode::OK, "{path}");

        let first = first_sse_event(response.text()?)
            .ok_or_else(|| format!("{path} produced no SSE data frames"))?;
        let model = pointer
            .iter()
            .try_fold(&first, |value, key| value.get(key))
            .and_then(Value::as_str);
        assert_eq!(model, Some("model/a"), "{path}");
    }
    Ok(())
}

// Returns the first `data:` frame of an SSE body as JSON, skipping `[DONE]`.
fn first_sse_event(body: &str) -> Option<Value> {
    body.lines()
        .filter_map(|line| line.strip_prefix("data: "))
        .filter(|data| *data != "[DONE]")
        .find_map(|data| serde_json::from_str(data).ok())
}

#[tokio::test]
async fn streaming_success_records_only_final_usage_and_one_latency() -> TestResult {
    const MODEL: &str = "model/stream-success";
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &[MODEL])]).await?;
    let before = send(&app, "GET", "/metrics", None).await?;
    let before = before.text()?;

    let response = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": "stream-success"}],
            "stream": true
        })),
    )
    .await?;

    assert_eq!(response.status, StatusCode::OK);
    assert_in_order(
        response.text()?,
        &[
            "hello",
            "-partial",
            "-final",
            "\"finish_reason\":\"stop\"",
            "[DONE]",
        ],
    );

    let after = send(&app, "GET", "/metrics", None).await?;
    let after = after.text()?;
    for (name, expected_delta) in [
        ("switchyard_prompt_tokens_total", 12.0),
        ("switchyard_completion_tokens_total", 5.0),
        ("switchyard_cached_tokens_total", 7.0),
        ("switchyard_cache_creation_tokens_total", 2.0),
        ("switchyard_reasoning_tokens_total", 3.0),
        ("switchyard_total_latency_ms_count", 1.0),
    ] {
        assert_eq!(
            metric_delta(before, after, name, &[("model", MODEL)]),
            Some(expected_delta),
            "unexpected delta for {name}"
        );
    }
    let stats = send(&app, "GET", "/v1/stats", None).await?.json()?;
    assert_eq!(stats["total_requests"], 1);
    assert_eq!(
        stats["total_tokens"],
        json!({
            "prompt": 12, "completion": 5, "cached": 7,
            "cache_creation": 2, "reasoning": 3, "total": 17
        })
    );
    assert_eq!(stats["models"][MODEL]["model_call_latency"]["count"], 1);
    assert_eq!(stats["models"][MODEL]["total_latency"]["count"], 1);
    Ok(())
}

#[tokio::test]
// A terminal stream failure records errors without usage or terminal latency.
async fn streaming_error_records_error_without_usage_or_latency() -> TestResult {
    const MODEL: &str = "model/stream-error";
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &[MODEL])]).await?;
    let before = send(&app, "GET", "/metrics", None).await?;
    let before = before.text()?;

    let response = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": "stream-error"}],
            "stream": true
        })),
    )
    .await?;

    assert_eq!(response.status, StatusCode::OK);
    assert_in_order(
        response.text()?,
        &["before", "still here", "upstream stream failed"],
    );

    let after = send(&app, "GET", "/metrics", None).await?;
    let after = after.text()?;
    for name in [
        "switchyard_prompt_tokens_total",
        "switchyard_completion_tokens_total",
        "switchyard_cached_tokens_total",
        "switchyard_cache_creation_tokens_total",
        "switchyard_reasoning_tokens_total",
        "switchyard_total_latency_ms_count",
    ] {
        assert_eq!(
            metric_value(after, name, &[("model", MODEL)]),
            metric_value(before, name, &[("model", MODEL)]),
            "{name} changed after a failed stream"
        );
    }
    assert_eq!(
        metric_delta(
            before,
            after,
            "switchyard_errors_total",
            &[("model", MODEL)]
        ),
        Some(1.0)
    );
    let stats = send(&app, "GET", "/v1/stats", None).await?.json()?;
    assert_eq!(stats["total_requests"], 1);
    assert_eq!(stats["total_errors"], 1);
    assert_eq!(stats["total_tokens"], empty_token_totals());
    assert_eq!(stats["models"][MODEL]["calls"], 1);
    assert_eq!(stats["models"][MODEL]["errors"], 1);
    assert_eq!(stats["models"][MODEL]["total_latency"]["count"], 0);
    assert_eq!(stats["routing_overhead"]["count"], 1);
    Ok(())
}

#[tokio::test]
async fn responses_stream_error_does_not_emit_success_terminal_events() -> TestResult {
    // A distinct target keeps this test's error-counter increments off the shared
    // model/stream-error metric that streaming_error_records_... asserts an exact delta on.
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/responses-stream-error"])]).await?;

    let response = send(
        &app,
        "POST",
        "/v1/responses",
        Some(json!({
            "model": ROUTE_MODEL,
            "input": "stream-error",
            "stream": true
        })),
    )
    .await?;

    assert_eq!(response.status, StatusCode::OK);
    let body = response.text()?;
    assert_in_order(body, &["before", "upstream stream failed"]);
    for event_type in [
        "response.content_part.done",
        "response.output_item.done",
        "response.completed",
    ] {
        assert!(
            !body.contains(event_type),
            "{event_type} followed an upstream stream error"
        );
    }
    Ok(())
}

#[tokio::test]
async fn chat_stream_error_does_not_emit_success_terminal_chunk() -> TestResult {
    // A distinct target keeps this test's error-counter increments off the shared
    // model/stream-error metric that streaming_error_records_... asserts an exact delta on.
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/chat-stream-error"])]).await?;

    let response = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": "stream-error"}],
            "stream": true
        })),
    )
    .await?;

    assert_eq!(response.status, StatusCode::OK);
    let body = response.text()?;
    assert_in_order(body, &["before", "still here", "upstream stream failed"]);
    // The finalizer must not synthesize a `finish_reason: stop` completion chunk after the error.
    let after_error = body
        .split_once("upstream stream failed")
        .map(|(_, rest)| rest)
        .unwrap_or_default();
    assert!(
        !after_error.contains(r#""finish_reason":"stop""#),
        "a finish_reason=stop chunk followed an upstream stream error:\n{body}"
    );
    Ok(())
}

#[tokio::test]
async fn anthropic_stream_error_does_not_emit_success_terminal_events() -> TestResult {
    // A distinct target keeps this test's error-counter increments off the shared
    // model/stream-error metric that streaming_error_records_... asserts an exact delta on.
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/anthropic-stream-error"])]).await?;

    let response = send(
        &app,
        "POST",
        "/v1/messages",
        Some(json!({
            "model": ROUTE_MODEL,
            "messages": [{"role": "user", "content": "stream-error"}],
            "max_tokens": 16,
            "stream": true
        })),
    )
    .await?;

    assert_eq!(response.status, StatusCode::OK);
    let body = response.text()?;
    assert_in_order(body, &["before", "upstream stream failed"]);
    // The finalizer must not close the turn with message_delta/message_stop after the error.
    let after_error = body
        .split_once("upstream stream failed")
        .map(|(_, rest)| rest)
        .unwrap_or_default();
    for event_type in ["message_delta", "message_stop"] {
        assert!(
            !after_error.contains(event_type),
            "{event_type} followed an upstream stream error:\n{body}"
        );
    }
    Ok(())
}

#[tokio::test]
async fn request_and_upstream_errors_use_the_inbound_wire_format() -> TestResult {
    let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;

    let unknown = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({
            "model": "other",
            "messages": [{"role": "user", "content": "hi"}]
        })),
    )
    .await?;
    assert_eq!(unknown.status, StatusCode::NOT_FOUND);
    assert_eq!(unknown.json()?["error"]["code"], "model_not_found");

    let missing_model = send(
        &app,
        "POST",
        "/v1/chat/completions",
        Some(json!({"messages": [{"role": "user", "content": "hi"}]})),
    )
    .await?;
    assert_eq!(missing_model.status, StatusCode::BAD_REQUEST);
    assert_eq!(
        missing_model.json()?["error"]["code"],
        "invalid_request_error"
    );

    let upstream_cases = [
        (
            "/v1/chat/completions",
            json!({
                "model": ROUTE_MODEL,
                "messages": [{"role": "user", "content": "auth-fail"}]
            }),
            json!({
                "error": {
                    "message": "upstream authentication failed",
                    "type": "upstream_error",
                    "code": "upstream_error"
                }
            }),
        ),
        (
            "/v1/responses",
            json!({"model": ROUTE_MODEL, "input": "auth-fail"}),
            json!({
                "error": {
                    "message": "upstream authentication failed",
                    "type": "upstream_error",
                    "code": "upstream_error"
                }
            }),
        ),
        (
            "/v1/messages",
            json!({
                "model": ROUTE_MODEL,
                "max_tokens": 64,
                "messages": [{"role": "user", "content": "auth-fail"}]
            }),
            json!({
                "type": "error",
                "error": {
                    "type": "authentication_error",
                    "message": "upstream authentication failed"
                }
            }),
        ),
    ];
    for (path, body, expected) in upstream_cases {
        let response = send(&app, "POST", path, Some(body)).await?;
        assert_eq!(response.status, StatusCode::UNAUTHORIZED, "{path}");
        assert_eq!(response.json()?, expected, "{path}");
    }

    let anthropic_unknown = send(
        &app,
        "POST",
        "/v1/messages",
        Some(json!({
            "model": "other",
            "max_tokens": 64,
            "messages": [{"role": "user", "content": "hi"}]
        })),
    )
    .await?;
    assert_eq!(anthropic_unknown.status, StatusCode::NOT_FOUND);
    assert_eq!(
        anthropic_unknown.json()?,
        json!({
            "type": "error",
            "error": {
                "type": "not_found_error",
                "message": "No route registered for model other"
            }
        })
    );
    Ok(())
}