nautilus-coinbase 0.56.0

Coinbase Advanced Trade integration adapter for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Integration tests for the Coinbase execution-side HTTP reconciliation path.
//!
//! Uses an axum mock server to feed canned responses and capture the outgoing
//! query strings so pagination, URL encoding, and filter-key regressions stay
//! locked in. The mock server accepts any Bearer JWT (no verification), so
//! tests pass bogus credentials that still sign with a valid EC key pair.

use std::{
    collections::{HashMap, HashSet, VecDeque},
    net::SocketAddr,
    path::PathBuf,
    sync::{Arc, Mutex},
    time::Duration,
};

use aws_lc_rs::{
    encoding::AsDer,
    rand as lc_rand,
    signature::{ECDSA_P256_SHA256_FIXED_SIGNING, EcdsaKeyPair},
};
use axum::{
    Router,
    extract::State,
    http::Uri,
    response::{IntoResponse, Json},
    routing::{get, post},
};
use chrono::{TimeZone, Utc};
use nautilus_coinbase::{
    common::{consts::COINBASE_VENUE, enums::CoinbaseEnvironment},
    config::CoinbaseExecClientConfig,
    execution::CoinbaseExecutionClient,
    http::client::CoinbaseHttpClient,
};
use nautilus_common::{
    cache::Cache,
    clients::ExecutionClient,
    live::runner::replace_exec_event_sender,
    messages::{
        ExecutionEvent,
        execution::{GeneratePositionStatusReports, GeneratePositionStatusReportsBuilder},
    },
};
use nautilus_core::UnixNanos;
use nautilus_live::ExecutionClientCore;
use nautilus_model::{
    enums::{AccountType, OmsType, OrderSide, OrderType, PositionSideSpecified, TimeInForce},
    identifiers::{AccountId, ClientId, ClientOrderId, InstrumentId, TraderId, VenueOrderId},
    instruments::InstrumentAny,
    types::{Price, Quantity},
};
use nautilus_network::retry::RetryConfig;
use rstest::rstest;
use rust_decimal_macros::dec;
use serde_json::{Value, json};

fn data_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_data")
}

fn load_json(filename: &str) -> Value {
    let content = std::fs::read_to_string(data_path().join(filename))
        .unwrap_or_else(|_| panic!("failed to read {filename}"));
    serde_json::from_str(&content).expect("invalid json")
}

// Generates an ES256 EC private key in SEC1 PEM format. Coinbase's credential
// loader accepts both SEC1 and PKCS#8; SEC1 matches production.
fn test_pem_key() -> String {
    let rng = lc_rand::SystemRandom::new();
    let pkcs8 = EcdsaKeyPair::generate_pkcs8(&ECDSA_P256_SHA256_FIXED_SIGNING, &rng).unwrap();
    let key_pair =
        EcdsaKeyPair::from_pkcs8(&ECDSA_P256_SHA256_FIXED_SIGNING, pkcs8.as_ref()).unwrap();
    let sec1_der = key_pair.private_key().as_der().unwrap();
    let pem_obj = pem::Pem::new("EC PRIVATE KEY", sec1_der.as_ref().to_vec());
    pem::encode(&pem_obj)
}

fn test_api_key() -> String {
    "organizations/test-org/apiKeys/test-key".to_string()
}

#[derive(Debug, Clone)]
struct RequestRecord {
    path: String,
    raw_query: String,
    body: Option<Value>,
}

#[derive(Default)]
struct TestStateInner {
    requests: Vec<RequestRecord>,
    queues: HashMap<String, VecDeque<Value>>,
    // Paths listed here respond with HTTP 503 so retry-guard tests can
    // observe whether the client issues a second attempt.
    fail_paths: HashSet<String>,
}

#[derive(Clone, Default)]
struct TestState {
    inner: Arc<Mutex<TestStateInner>>,
}

impl TestState {
    fn enqueue(&self, path: &str, response: Value) {
        self.inner
            .lock()
            .unwrap()
            .queues
            .entry(path.to_string())
            .or_default()
            .push_back(response);
    }

    fn next_response(&self, path: &str, raw_query: String) -> Value {
        let mut state = self.inner.lock().unwrap();
        state.requests.push(RequestRecord {
            path: path.to_string(),
            raw_query,
            body: None,
        });
        state
            .queues
            .get_mut(path)
            .and_then(|q| q.pop_front())
            .unwrap_or_else(|| json!({}))
    }

    fn next_response_with_body(&self, path: &str, body: Value) -> Value {
        let mut state = self.inner.lock().unwrap();
        state.requests.push(RequestRecord {
            path: path.to_string(),
            raw_query: String::new(),
            body: Some(body),
        });
        state
            .queues
            .get_mut(path)
            .and_then(|q| q.pop_front())
            .unwrap_or_else(|| json!({}))
    }

    fn requests(&self) -> Vec<RequestRecord> {
        self.inner.lock().unwrap().requests.clone()
    }

    fn mark_failing(&self, path: &str) {
        self.inner
            .lock()
            .unwrap()
            .fail_paths
            .insert(path.to_string());
    }

    fn is_failing(&self, path: &str) -> bool {
        self.inner.lock().unwrap().fail_paths.contains(path)
    }

    fn record_failure(&self, path: &str, raw_query: String, body: Option<Value>) {
        self.inner.lock().unwrap().requests.push(RequestRecord {
            path: path.to_string(),
            raw_query,
            body,
        });
    }

    fn requests_for(&self, path: &str) -> Vec<RequestRecord> {
        self.inner
            .lock()
            .unwrap()
            .requests
            .iter()
            .filter(|r| r.path == path)
            .cloned()
            .collect()
    }
}

async fn handle_orders_batch(State(state): State<TestState>, uri: Uri) -> impl IntoResponse {
    let raw_query = uri.query().unwrap_or("").to_string();
    let response = state.next_response("/orders/historical/batch", raw_query);
    Json(response)
}

async fn handle_order_by_id(
    State(state): State<TestState>,
    axum::extract::Path(order_id): axum::extract::Path<String>,
    uri: Uri,
) -> impl IntoResponse {
    let raw_query = uri.query().unwrap_or("").to_string();
    let path = format!("/orders/historical/{order_id}");
    let response = state.next_response(&path, raw_query);
    Json(response)
}

async fn handle_fills(State(state): State<TestState>, uri: Uri) -> impl IntoResponse {
    let raw_query = uri.query().unwrap_or("").to_string();
    let response = state.next_response("/orders/historical/fills", raw_query);
    Json(response)
}

async fn handle_accounts(State(state): State<TestState>, uri: Uri) -> impl IntoResponse {
    let raw_query = uri.query().unwrap_or("").to_string();
    let response = state.next_response("/accounts", raw_query);
    Json(response)
}

async fn handle_products(State(state): State<TestState>) -> axum::response::Response {
    if state.is_failing("/market/products") {
        state.record_failure("/market/products", String::new(), None);
        return (
            axum::http::StatusCode::SERVICE_UNAVAILABLE,
            Json(json!({"error": "unavailable"})),
        )
            .into_response();
    }

    // If the test enqueued a custom products response use that; otherwise
    // fall back to the shared spot fixture so instrument resolution works
    // out of the box.
    let inner = state.inner.clone();
    let have_queue = inner
        .lock()
        .unwrap()
        .queues
        .get("/market/products")
        .is_some_and(|q| !q.is_empty());

    if have_queue {
        let response = state.next_response("/market/products", String::new());
        Json(response).into_response()
    } else {
        state.next_response("/market/products", String::new());
        Json(load_json("http_products.json")).into_response()
    }
}

async fn handle_product(
    State(state): State<TestState>,
    axum::extract::Path(product_id): axum::extract::Path<String>,
) -> impl IntoResponse {
    state.next_response(&format!("/market/products/{product_id}"), String::new());

    if product_id == "BTC-USD" {
        Json(load_json("http_product.json"))
    } else if product_id == "BIP-20DEC30-CDE" {
        // Return the first (perpetual) entry from the futures fixture.
        let payload = load_json("http_products_future.json");
        let product = payload["products"][0].clone();
        Json(product)
    } else {
        Json(json!({"error": "not found"}))
    }
}

async fn handle_cfm_balance_summary(State(state): State<TestState>) -> impl IntoResponse {
    state.next_response("/cfm/balance_summary", String::new());
    Json(load_json("http_cfm_balance_summary.json"))
}

async fn handle_cfm_positions(State(state): State<TestState>) -> axum::response::Response {
    if state.is_failing("/cfm/positions") {
        state.record_failure("/cfm/positions", String::new(), None);
        return (
            axum::http::StatusCode::SERVICE_UNAVAILABLE,
            Json(json!({"error": "unavailable"})),
        )
            .into_response();
    }
    state.next_response("/cfm/positions", String::new());
    Json(load_json("http_cfm_positions.json")).into_response()
}

async fn handle_cfm_position(
    State(state): State<TestState>,
    axum::extract::Path(product_id): axum::extract::Path<String>,
) -> axum::response::Response {
    let path = format!("/cfm/positions/{product_id}");
    if state.is_failing(&path) {
        state.record_failure(&path, String::new(), None);
        return (
            axum::http::StatusCode::SERVICE_UNAVAILABLE,
            Json(json!({"error": "unavailable"})),
        )
            .into_response();
    }
    state.next_response(&path, String::new());
    Json(load_json("http_cfm_position.json")).into_response()
}

async fn handle_create_order(
    State(state): State<TestState>,
    Json(body): Json<Value>,
) -> axum::response::Response {
    if state.is_failing("/orders") {
        state.record_failure("/orders", String::new(), Some(body));
        return (
            axum::http::StatusCode::SERVICE_UNAVAILABLE,
            Json(json!({"error": "unavailable"})),
        )
            .into_response();
    }

    let response = state.next_response_with_body("/orders", body);
    Json(response).into_response()
}

async fn handle_cancel_orders(
    State(state): State<TestState>,
    Json(body): Json<Value>,
) -> impl IntoResponse {
    let response = state.next_response_with_body("/orders/batch_cancel", body);
    Json(response)
}

async fn handle_edit_order(
    State(state): State<TestState>,
    Json(body): Json<Value>,
) -> impl IntoResponse {
    let response = state.next_response_with_body("/orders/edit", body);
    Json(response)
}

const API_PREFIX: &str = "/api/v3/brokerage";

fn create_router(state: TestState) -> Router {
    Router::new()
        .route(
            &format!("{API_PREFIX}/orders/historical/batch"),
            get(handle_orders_batch),
        )
        .route(
            &format!("{API_PREFIX}/orders/historical/fills"),
            get(handle_fills),
        )
        .route(
            &format!("{API_PREFIX}/orders/historical/{{order_id}}"),
            get(handle_order_by_id),
        )
        .route(&format!("{API_PREFIX}/accounts"), get(handle_accounts))
        .route(
            &format!("{API_PREFIX}/market/products"),
            get(handle_products),
        )
        .route(
            &format!("{API_PREFIX}/market/products/{{product_id}}"),
            get(handle_product),
        )
        .route(
            &format!("{API_PREFIX}/cfm/balance_summary"),
            get(handle_cfm_balance_summary),
        )
        .route(
            &format!("{API_PREFIX}/cfm/positions"),
            get(handle_cfm_positions),
        )
        .route(
            &format!("{API_PREFIX}/cfm/positions/{{product_id}}"),
            get(handle_cfm_position),
        )
        .route(&format!("{API_PREFIX}/orders"), post(handle_create_order))
        .route(
            &format!("{API_PREFIX}/orders/batch_cancel"),
            post(handle_cancel_orders),
        )
        .route(
            &format!("{API_PREFIX}/orders/edit"),
            post(handle_edit_order),
        )
        .with_state(state)
}

async fn start_mock_server(state: TestState) -> SocketAddr {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let router = create_router(state);

    tokio::spawn(async move {
        axum::serve(listener, router).await.unwrap();
    });

    let start = std::time::Instant::now();

    loop {
        if tokio::net::TcpStream::connect(addr).await.is_ok() {
            break;
        }

        assert!(
            start.elapsed() <= Duration::from_secs(5),
            "Mock server did not start within timeout"
        );
        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    addr
}

fn create_http_client(addr: SocketAddr) -> CoinbaseHttpClient {
    create_http_client_with_retry(addr, None)
}

fn create_http_client_with_retry(
    addr: SocketAddr,
    retry_config: Option<RetryConfig>,
) -> CoinbaseHttpClient {
    let client = CoinbaseHttpClient::from_credentials(
        &test_api_key(),
        &test_pem_key(),
        CoinbaseEnvironment::Live,
        10,
        None,
        retry_config,
    )
    .unwrap();
    client.set_base_url(format!("http://{addr}"));
    client
}

// Keeps retry-loop tests fast.
fn fast_retry_config(max_retries: u32) -> RetryConfig {
    RetryConfig {
        max_retries,
        initial_delay_ms: 5,
        max_delay_ms: 5,
        backoff_factor: 1.0,
        jitter_ms: 0,
        operation_timeout_ms: Some(2_000),
        immediate_first: false,
        max_elapsed_ms: None,
    }
}

fn account_id() -> AccountId {
    AccountId::new("COINBASE-001")
}

fn order_json(order_id: &str, product_id: &str, client_order_id: &str, status: &str) -> Value {
    json!({
        "order_id": order_id,
        "product_id": product_id,
        "user_id": "user-1",
        "order_configuration": {
            "limit_limit_gtc": {
                "base_size": "0.001",
                "limit_price": "50000.00",
                "post_only": false
            }
        },
        "side": "BUY",
        "client_order_id": client_order_id,
        "status": status,
        "time_in_force": "GOOD_UNTIL_CANCELLED",
        "created_time": "2024-01-15T10:00:00Z",
        "completion_percentage": "0",
        "filled_size": "0",
        "average_filled_price": "0",
        "fee": "0",
        "number_of_fills": "0",
        "filled_value": "0",
        "pending_cancel": false,
        "size_in_quote": false,
        "total_fees": "0",
        "size_inclusive_of_fees": false,
        "total_value_after_fees": "0",
        "trigger_status": "INVALID_ORDER_TYPE",
        "order_type": "LIMIT",
        "reject_reason": "",
        "settled": false,
        "product_type": "SPOT",
        "reject_message": "",
        "cancel_message": "",
        "order_placement_source": "RETAIL_ADVANCED",
        "outstanding_hold_amount": "0",
        "is_liquidation": false,
        "last_fill_time": null,
        "leverage": "",
        "margin_type": "",
        "retail_portfolio_id": "",
        "originating_order_id": "",
        "attached_order_id": ""
    })
}

fn fill_json(trade_id: &str, order_id: &str, product_id: &str) -> Value {
    json!({
        "entry_id": format!("entry-{trade_id}"),
        "trade_id": trade_id,
        "order_id": order_id,
        "trade_time": "2024-01-15T10:30:00Z",
        "trade_type": "FILL",
        "price": "45000.00",
        "size": "0.001",
        "commission": "0.50",
        "product_id": product_id,
        "sequence_timestamp": "2024-01-15T10:30:00.000Z",
        "liquidity_indicator": "MAKER",
        "size_in_quote": false,
        "user_id": "user-1",
        "side": "BUY",
        "retail_portfolio_id": ""
    })
}

fn account_json(currency: &str, available: &str, hold: &str, uuid: &str) -> Value {
    json!({
        "uuid": uuid,
        "name": format!("{currency} wallet"),
        "currency": currency,
        "available_balance": {"value": available, "currency": currency},
        "default": false,
        "active": true,
        "created_at": "2024-01-15T10:00:00Z",
        "updated_at": "2024-01-15T10:00:00Z",
        "deleted_at": null,
        "type": "FIAT",
        "ready": true,
        "hold": {"value": hold, "currency": currency},
        "retail_portfolio_id": "portfolio-1"
    })
}

fn btc_usd_instrument_id() -> InstrumentId {
    InstrumentId::from("BTC-USD.COINBASE")
}

fn query_pairs(raw_query: &str) -> Vec<(String, String)> {
    url::form_urlencoded::parse(raw_query.as_bytes())
        .into_owned()
        .collect()
}

async fn bootstrap_btc_usd_instrument(client: &CoinbaseHttpClient) -> InstrumentAny {
    client
        .request_instrument("BTC-USD")
        .await
        .expect("instrument bootstrap")
}

#[rstest]
#[tokio::test]
async fn test_exec_client_request_order_status_reports_paginates_cursor() {
    let state = TestState::default();
    // Page 1: two orders, has_next=true, cursor="page2"
    state.enqueue(
        "/orders/historical/batch",
        json!({
            "orders": [
                order_json("venue-1", "BTC-USD", "client-1", "OPEN"),
                order_json("venue-2", "BTC-USD", "client-2", "OPEN"),
            ],
            "sequence": "0",
            "has_next": true,
            "cursor": "page2"
        }),
    );
    // Page 2: one order, has_next=false
    state.enqueue(
        "/orders/historical/batch",
        json!({
            "orders": [order_json("venue-3", "BTC-USD", "client-3", "FILLED")],
            "sequence": "0",
            "has_next": false,
            "cursor": ""
        }),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);
    bootstrap_btc_usd_instrument(&client).await;

    let reports = client
        .request_order_status_reports(account_id(), None, false, None, None, None)
        .await
        .unwrap();

    assert_eq!(reports.len(), 3);
    assert_eq!(reports[0].venue_order_id.as_str(), "venue-1");
    assert_eq!(reports[2].venue_order_id.as_str(), "venue-3");

    let batch_requests = state.requests_for("/orders/historical/batch");
    assert_eq!(batch_requests.len(), 2);
    assert!(
        !batch_requests[0].raw_query.contains("cursor="),
        "first request must not send a cursor, query={}",
        batch_requests[0].raw_query
    );
    let second_pairs = query_pairs(&batch_requests[1].raw_query);
    assert!(
        second_pairs
            .iter()
            .any(|(k, v)| k == "cursor" && v == "page2"),
        "second request must carry cursor=page2, query={}",
        batch_requests[1].raw_query
    );
}

#[rstest]
#[tokio::test]
async fn test_exec_client_request_order_status_reports_sends_plural_product_ids() {
    let state = TestState::default();
    state.enqueue(
        "/orders/historical/batch",
        json!({
            "orders": [order_json("venue-1", "BTC-USD", "client-1", "OPEN")],
            "sequence": "0",
            "has_next": false,
            "cursor": ""
        }),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);
    bootstrap_btc_usd_instrument(&client).await;

    let _ = client
        .request_order_status_reports(
            account_id(),
            Some(btc_usd_instrument_id()),
            false,
            None,
            None,
            None,
        )
        .await
        .unwrap();

    let batch_requests = state.requests_for("/orders/historical/batch");
    assert_eq!(batch_requests.len(), 1);
    let pairs = query_pairs(&batch_requests[0].raw_query);
    assert!(
        pairs
            .iter()
            .any(|(k, v)| k == "product_ids" && v == "BTC-USD"),
        "expected product_ids=BTC-USD, query={}",
        batch_requests[0].raw_query
    );
    assert!(
        !pairs.iter().any(|(k, _)| k == "product_id"),
        "must not send singular product_id, query={}",
        batch_requests[0].raw_query
    );
}

#[rstest]
#[tokio::test]
async fn test_exec_client_request_order_status_reports_honors_hard_limit() {
    let state = TestState::default();

    // Three pages of 10 orders each. With limit=25 the client should stop
    // after page 3 and truncate the collected vector to 25.
    for page in 0..3 {
        let orders: Vec<Value> = (0..10)
            .map(|i| {
                order_json(
                    &format!("venue-{page}-{i}"),
                    "BTC-USD",
                    &format!("client-{page}-{i}"),
                    "OPEN",
                )
            })
            .collect();
        let (cursor, has_next) = if page < 2 {
            (format!("page{}", page + 1), true)
        } else {
            (String::new(), false)
        };
        state.enqueue(
            "/orders/historical/batch",
            json!({
                "orders": orders,
                "sequence": "0",
                "has_next": has_next,
                "cursor": cursor
            }),
        );
    }

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);
    bootstrap_btc_usd_instrument(&client).await;

    let reports = client
        .request_order_status_reports(account_id(), None, false, None, None, Some(25))
        .await
        .unwrap();

    assert_eq!(reports.len(), 25);
    // The client fetched three pages before truncating at 30 collected items.
    let batch_requests = state.requests_for("/orders/historical/batch");
    assert_eq!(batch_requests.len(), 3);
}

#[rstest]
#[tokio::test]
async fn test_exec_client_request_order_status_reports_encodes_rfc3339_start_date() {
    let state = TestState::default();
    state.enqueue(
        "/orders/historical/batch",
        json!({
            "orders": [],
            "sequence": "0",
            "has_next": false,
            "cursor": ""
        }),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);
    bootstrap_btc_usd_instrument(&client).await;

    let start = Utc.with_ymd_and_hms(2024, 1, 15, 10, 0, 0).unwrap();
    let _ = client
        .request_order_status_reports(account_id(), None, false, Some(start), None, None)
        .await
        .unwrap();

    let batch_requests = state.requests_for("/orders/historical/batch");
    assert_eq!(batch_requests.len(), 1);
    let raw_query = &batch_requests[0].raw_query;
    // RFC 3339 UTC serializes as `+00:00`, which must arrive percent-encoded.
    assert!(
        raw_query.contains("%2B00%3A00"),
        "timestamp plus sign must be percent-encoded, raw_query={raw_query}"
    );
    assert!(
        !raw_query.contains("+00:00"),
        "raw plus sign must not leak into query, raw_query={raw_query}"
    );
}

#[rstest]
#[tokio::test]
async fn test_exec_client_request_order_status_report_by_client_order_id_fallback() {
    let state = TestState::default();
    // When the caller provides only a client_order_id, the client falls back
    // to /orders/historical/batch and filters in-process.
    state.enqueue(
        "/orders/historical/batch",
        json!({
            "orders": [
                order_json("venue-1", "BTC-USD", "wrong-1", "OPEN"),
                order_json("venue-2", "BTC-USD", "target-client", "OPEN"),
                order_json("venue-3", "BTC-USD", "wrong-2", "OPEN"),
            ],
            "sequence": "0",
            "has_next": false,
            "cursor": ""
        }),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);
    bootstrap_btc_usd_instrument(&client).await;

    let report = client
        .request_order_status_report(
            account_id(),
            Some(ClientOrderId::new("target-client")),
            None,
        )
        .await
        .unwrap();

    assert_eq!(report.venue_order_id.as_str(), "venue-2");
    assert_eq!(report.client_order_id.unwrap().as_str(), "target-client");
    // Must hit the batch endpoint, not the single-order endpoint.
    assert_eq!(state.requests_for("/orders/historical/batch").len(), 1);
    let single_hits = state
        .requests()
        .iter()
        .filter(|r| {
            r.path.starts_with("/orders/historical/")
                && r.path != "/orders/historical/batch"
                && r.path != "/orders/historical/fills"
        })
        .count();
    assert_eq!(single_hits, 0);
}

#[rstest]
#[tokio::test]
async fn test_exec_client_request_fill_reports_sends_plural_keys_and_paginates() {
    let state = TestState::default();
    state.enqueue(
        "/orders/historical/fills",
        json!({
            "fills": [fill_json("trade-1", "venue-1", "BTC-USD")],
            "cursor": "fillpage2"
        }),
    );
    state.enqueue(
        "/orders/historical/fills",
        json!({
            "fills": [fill_json("trade-2", "venue-1", "BTC-USD")],
            "cursor": ""
        }),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);
    bootstrap_btc_usd_instrument(&client).await;

    let reports = client
        .request_fill_reports(
            account_id(),
            Some(btc_usd_instrument_id()),
            Some(VenueOrderId::new("venue-1")),
            None,
            None,
            None,
        )
        .await
        .unwrap();

    assert_eq!(reports.len(), 2);
    assert_eq!(reports[0].trade_id.as_str(), "trade-1");
    assert_eq!(reports[1].trade_id.as_str(), "trade-2");

    let fill_requests = state.requests_for("/orders/historical/fills");
    assert_eq!(fill_requests.len(), 2);

    let first_pairs = query_pairs(&fill_requests[0].raw_query);
    assert!(
        first_pairs
            .iter()
            .any(|(k, v)| k == "product_ids" && v == "BTC-USD"),
        "expected product_ids=BTC-USD, query={}",
        fill_requests[0].raw_query
    );
    assert!(
        first_pairs
            .iter()
            .any(|(k, v)| k == "order_ids" && v == "venue-1"),
        "expected order_ids=venue-1, query={}",
        fill_requests[0].raw_query
    );
    assert!(
        !first_pairs
            .iter()
            .any(|(k, _)| k == "product_id" || k == "order_id"),
        "must not send singular filter keys, query={}",
        fill_requests[0].raw_query
    );

    let second_pairs = query_pairs(&fill_requests[1].raw_query);
    assert!(
        second_pairs
            .iter()
            .any(|(k, v)| k == "cursor" && v == "fillpage2"),
        "second request must carry cursor=fillpage2, query={}",
        fill_requests[1].raw_query
    );
}

#[rstest]
#[tokio::test]
async fn test_exec_client_request_account_state_paginates_and_aggregates() {
    let state = TestState::default();
    // Two pages of accounts; USD appears on both pages and must be summed.
    state.enqueue(
        "/accounts",
        json!({
            "accounts": [
                account_json("USD", "1000.00", "50.00", "uuid-1"),
                account_json("BTC", "0.5", "0.1", "uuid-2"),
            ],
            "has_next": true,
            "cursor": "acct-page2",
            "size": 2
        }),
    );
    state.enqueue(
        "/accounts",
        json!({
            "accounts": [
                account_json("USD", "2500.00", "25.00", "uuid-3"),
            ],
            "has_next": false,
            "cursor": "",
            "size": 1
        }),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let account = client.request_account_state(account_id()).await.unwrap();

    // Two unique currencies even though three wallets came back across pages.
    assert_eq!(account.balances.len(), 2);

    let usd = account
        .balances
        .iter()
        .find(|b| b.currency.code.as_str() == "USD")
        .expect("USD aggregated across pages");
    assert_eq!(usd.free.as_decimal(), dec!(3500.00));
    assert_eq!(usd.locked.as_decimal(), dec!(75.00));
    assert_eq!(usd.total.as_decimal(), dec!(3575.00));

    let btc = account
        .balances
        .iter()
        .find(|b| b.currency.code.as_str() == "BTC")
        .expect("BTC present");
    assert_eq!(btc.free.as_decimal(), dec!(0.5));
    assert_eq!(btc.locked.as_decimal(), dec!(0.1));

    // Cursor followed, so the second request carried cursor=acct-page2.
    let account_requests = state.requests_for("/accounts");
    assert_eq!(account_requests.len(), 2);
    let second_pairs = query_pairs(&account_requests[1].raw_query);
    assert!(
        second_pairs
            .iter()
            .any(|(k, v)| k == "cursor" && v == "acct-page2"),
        "second accounts request must carry cursor, query={}",
        account_requests[1].raw_query
    );
}

#[rstest]
#[tokio::test]
async fn test_exec_client_get_or_fetch_instrument_lazy_fetches_missing_product() {
    let state = TestState::default();
    // Two identical order pages so we can issue two independent calls.
    for _ in 0..2 {
        state.enqueue(
            "/orders/historical/batch",
            json!({
                "orders": [order_json("venue-1", "BTC-USD", "client-1", "OPEN")],
                "sequence": "0",
                "has_next": false,
                "cursor": ""
            }),
        );
    }

    let addr = start_mock_server(state.clone()).await;
    // Fresh client, instrument cache empty. The first call must lazy-fetch
    // the product definition via /products/{id} before parsing the order.
    let client = create_http_client(addr);

    let reports = client
        .request_order_status_reports(account_id(), None, false, None, None, None)
        .await
        .unwrap();

    assert_eq!(reports.len(), 1);
    assert_eq!(state.requests_for("/market/products/BTC-USD").len(), 1);

    // A follow-up call must not re-fetch the product: it now hits cache.
    let reports2 = client
        .request_order_status_reports(account_id(), None, false, None, None, None)
        .await
        .unwrap();
    assert_eq!(reports2.len(), 1);
    assert_eq!(
        state.requests_for("/market/products/BTC-USD").len(),
        1,
        "cached instrument must not trigger a second /products fetch"
    );
}

fn create_order_success_response(order_id: &str, client_order_id: &str) -> Value {
    json!({
        "success": true,
        "failure_reason": "",
        "order_id": order_id,
        "success_response": {
            "order_id": order_id,
            "product_id": "BTC-USD",
            "side": "BUY",
            "client_order_id": client_order_id,
        }
    })
}

fn create_order_failure_response() -> Value {
    json!({
        "success": false,
        "failure_reason": "INSUFFICIENT_FUND",
        "order_id": "",
        "error_response": {
            "error": "INSUFFICIENT_FUND",
            "message": "Insufficient balance",
            "error_details": "available=0",
            "preview_failure_reason": "",
            "new_order_failure_reason": "",
        }
    })
}

#[rstest]
#[tokio::test]
async fn test_http_submit_order_limit_gtc_serializes_typed_body() {
    let state = TestState::default();
    state.enqueue(
        "/orders",
        create_order_success_response("venue-100", "client-100"),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let response = client
        .submit_order(
            ClientOrderId::new("client-100"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Limit,
            Quantity::from("0.5"),
            TimeInForce::Gtc,
            Some(Price::from("50000.00")),
            None,
            None,
            true, // post_only
            false,
            None,
            None,
            false,
            None,
        )
        .await
        .unwrap();

    assert!(response.success);
    assert_eq!(response.order_id, "venue-100");

    // Verify the typed body was serialized correctly to limit_limit_gtc shape.
    let requests = state.requests_for("/orders");
    assert_eq!(requests.len(), 1);
    let body = requests[0].body.as_ref().expect("POST body captured");
    assert_eq!(body["client_order_id"], "client-100");
    assert_eq!(body["product_id"], "BTC-USD");
    assert_eq!(body["side"], "BUY");
    let cfg = &body["order_configuration"]["limit_limit_gtc"];
    assert_eq!(cfg["base_size"], "0.5");
    assert_eq!(cfg["limit_price"], "50000.00");
    assert_eq!(cfg["post_only"], true);
    // retail_portfolio_id was None; the field must be omitted so the venue
    // routes the order to the key's default portfolio.
    assert!(body.get("retail_portfolio_id").is_none());
}

#[rstest]
#[tokio::test]
async fn test_http_submit_order_threads_retail_portfolio_id_when_set() {
    let state = TestState::default();
    state.enqueue(
        "/orders",
        create_order_success_response("venue-portfolio", "client-portfolio"),
    );
    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let response = client
        .submit_order(
            ClientOrderId::new("client-portfolio"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Limit,
            Quantity::from("0.5"),
            TimeInForce::Gtc,
            Some(Price::from("50000.00")),
            None,
            None,
            true, // post_only
            false,
            None,
            None,
            false,
            Some("portfolio-uuid-123".to_string()),
        )
        .await
        .unwrap();

    assert!(response.success);
    let requests = state.requests_for("/orders");
    let body = requests[0].body.as_ref().expect("POST body captured");
    assert_eq!(
        body["retail_portfolio_id"], "portfolio-uuid-123",
        "retail_portfolio_id must reach the wire when configured"
    );
}

#[rstest]
#[tokio::test]
async fn test_http_submit_order_market_uses_base_size_when_not_quote_qty() {
    let state = TestState::default();
    state.enqueue(
        "/orders",
        create_order_success_response("venue-200", "client-200"),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let _ = client
        .submit_order(
            ClientOrderId::new("client-200"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Market,
            Quantity::from("0.001"),
            TimeInForce::Ioc,
            None,
            None,
            None,
            false,
            false, // is_quote_quantity = false → base_size
            None,
            None,
            false,
            None,
        )
        .await
        .unwrap();

    let requests = state.requests_for("/orders");
    assert_eq!(requests.len(), 1);
    let body = requests[0].body.as_ref().unwrap();
    let cfg = &body["order_configuration"]["market_market_ioc"];
    assert_eq!(cfg["base_size"], "0.001");
    assert!(cfg.get("quote_size").is_none());
}

#[rstest]
#[tokio::test]
async fn test_http_submit_order_returns_failure_response() {
    let state = TestState::default();
    state.enqueue("/orders", create_order_failure_response());

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let response = client
        .submit_order(
            ClientOrderId::new("client-300"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Limit,
            Quantity::from("0.5"),
            TimeInForce::Gtc,
            Some(Price::from("50000.00")),
            None,
            None,
            false,
            false,
            None,
            None,
            false,
            None,
        )
        .await
        .unwrap();

    assert!(!response.success);
    let err = response
        .error_response
        .as_ref()
        .expect("error_response set");
    assert_eq!(err.error, "INSUFFICIENT_FUND");
    assert_eq!(err.message, "Insufficient balance");
}

#[rstest]
#[tokio::test]
async fn test_http_submit_order_rejects_unsupported_market_tif() {
    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    // DAY on MARKET is unsupported; should fail before HTTP. (IOC and FOK
    // both map to valid `market_market_*` configurations.)
    let result = client
        .submit_order(
            ClientOrderId::new("client-400"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Market,
            Quantity::from("0.001"),
            TimeInForce::Day,
            None,
            None,
            None,
            false,
            false,
            None,
            None,
            false,
            None,
        )
        .await;

    assert!(result.is_err());
    assert!(
        state.requests_for("/orders").is_empty(),
        "no HTTP call made"
    );
}

#[rstest]
#[tokio::test]
async fn test_http_cancel_orders_serializes_order_ids_and_returns_results() {
    let state = TestState::default();
    state.enqueue(
        "/orders/batch_cancel",
        json!({
            "results": [
                {"success": true, "failure_reason": "", "order_id": "venue-1"},
                {"success": false, "failure_reason": "UNKNOWN_CANCEL_FAILURE_REASON", "order_id": "venue-2"},
            ]
        }),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let response = client
        .cancel_orders(&[VenueOrderId::new("venue-1"), VenueOrderId::new("venue-2")])
        .await
        .unwrap();

    assert_eq!(response.results.len(), 2);
    assert!(response.results[0].success);
    assert!(!response.results[1].success);
    assert_eq!(
        response.results[1].failure_reason,
        "UNKNOWN_CANCEL_FAILURE_REASON"
    );

    let requests = state.requests_for("/orders/batch_cancel");
    assert_eq!(requests.len(), 1);
    let body = requests[0].body.as_ref().unwrap();
    assert_eq!(body["order_ids"][0], "venue-1");
    assert_eq!(body["order_ids"][1], "venue-2");
}

#[rstest]
#[tokio::test]
async fn test_http_modify_order_forwards_price_size_and_stop_price() {
    let state = TestState::default();
    state.enqueue("/orders/edit", json!({"success": true, "errors": []}));

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let response = client
        .modify_order(
            VenueOrderId::new("venue-99"),
            Some(Price::from("55000.00")),
            Some(Quantity::from("0.75")),
            Some(Price::from("54000.00")),
        )
        .await
        .unwrap();

    assert!(response.success);
    let requests = state.requests_for("/orders/edit");
    assert_eq!(requests.len(), 1);
    let body = requests[0].body.as_ref().unwrap();
    assert_eq!(body["order_id"], "venue-99");
    assert_eq!(body["price"], "55000.00");
    assert_eq!(body["size"], "0.75");
    assert_eq!(body["stop_price"], "54000.00");
}

#[rstest]
#[tokio::test]
async fn test_http_modify_order_omits_unset_fields() {
    let state = TestState::default();
    state.enqueue("/orders/edit", json!({"success": true, "errors": []}));

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let _ = client
        .modify_order(
            VenueOrderId::new("venue-99"),
            Some(Price::from("55000.00")),
            None,
            None,
        )
        .await
        .unwrap();

    let body = state.requests_for("/orders/edit")[0]
        .body
        .as_ref()
        .unwrap()
        .clone();
    assert_eq!(body["price"], "55000.00");
    assert!(body.get("size").is_none());
    assert!(body.get("stop_price").is_none());
}

#[rstest]
#[tokio::test]
async fn test_http_modify_order_returns_typed_failure_reason() {
    let state = TestState::default();
    state.enqueue(
        "/orders/edit",
        json!({
            "success": false,
            "errors": [{
                "edit_failure_reason": "ORDER_ALREADY_FILLED",
                "preview_failure_reason": "",
            }]
        }),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let response = client
        .modify_order(
            VenueOrderId::new("venue-99"),
            Some(Price::from("55000.00")),
            None,
            None,
        )
        .await
        .unwrap();

    assert!(!response.success);
    assert_eq!(response.errors.len(), 1);
    assert_eq!(
        response.errors[0].edit_failure_reason,
        "ORDER_ALREADY_FILLED"
    );
}

// GET is idempotent so transient 503s retry up to `max_retries` times,
// giving `1 + max_retries` attempts.
#[rstest]
#[tokio::test]
async fn test_http_get_retries_transient_failure_up_to_budget() {
    let state = TestState::default();
    state.mark_failing("/market/products");

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client_with_retry(addr, Some(fast_retry_config(3)));

    let result = client.get_products().await;
    assert!(
        result.is_err(),
        "expected 503 to surface after retry budget"
    );

    let attempts = state.requests_for("/market/products").len();
    assert_eq!(
        attempts, 4,
        "GET should run once plus 3 retries; saw {attempts}"
    );
}

// POSTs to order endpoints mutate live state; replaying could place, edit,
// or cancel twice, so the retry gate must keep them single-shot.
#[rstest]
#[tokio::test]
async fn test_http_post_does_not_retry_transient_failure() {
    let state = TestState::default();
    state.mark_failing("/orders");

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client_with_retry(addr, Some(fast_retry_config(3)));

    let result = client
        .submit_order(
            ClientOrderId::new("client-retry-guard"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Limit,
            Quantity::from("0.1"),
            TimeInForce::Gtc,
            Some(Price::from("50000.00")),
            None,
            None,
            false,
            false,
            None,
            None,
            false,
            None,
        )
        .await;

    assert!(result.is_err(), "expected 503 to propagate from POST");

    let attempts = state.requests_for("/orders").len();
    assert_eq!(
        attempts, 1,
        "POST must run exactly once regardless of retry budget; saw {attempts}"
    );
}

#[rstest]
#[tokio::test]
async fn test_http_request_cfm_balance_summary_returns_parsed_summary() {
    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let summary = client
        .request_cfm_balance_summary()
        .await
        .expect("CFM balance summary should deserialize");

    assert_eq!(
        summary.total_usd_balance.value,
        dec!(10000.00),
        "USD balance mirrors the fixture"
    );
    assert_eq!(summary.available_margin.value, dec!(7500.00));
    assert_eq!(
        summary
            .intraday_margin_window_measure
            .as_ref()
            .unwrap()
            .initial_margin
            .value,
        dec!(500.00)
    );

    let requests = state.requests_for("/cfm/balance_summary");
    assert_eq!(requests.len(), 1);
}

#[rstest]
#[tokio::test]
async fn test_http_request_cfm_margin_balances_picks_stricter_window() {
    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let margins = client
        .request_cfm_margin_balances()
        .await
        .expect("margin balances should build from summary");

    // `MarginAccount::split_event_margins` keys account-level margins by
    // Currency alone, so we collapse to a single MarginBalance. We pick the
    // whole window with the larger `initial_margin` (overnight here, 1000 vs
    // 500) so the emitted pair matches a real venue window verbatim.
    assert_eq!(margins.len(), 1);
    assert_eq!(margins[0].initial.as_decimal(), dec!(1000.00));
    assert_eq!(margins[0].maintenance.as_decimal(), dec!(500.00));
}

#[rstest]
#[tokio::test]
async fn test_http_request_cfm_account_state_produces_margin_account() {
    use nautilus_model::enums::AccountType;

    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let account_state = client
        .request_cfm_account_state(account_id())
        .await
        .expect("account state should build");

    assert_eq!(account_state.account_type, AccountType::Margin);
    assert_eq!(account_state.balances.len(), 1);
    // total = total_usd_balance (venue equity); free = available_margin;
    // locked = total - free captures both working-orders hold and margin
    // consumed by open positions.
    assert_eq!(account_state.balances[0].total.as_decimal(), dec!(10000.00));
    assert_eq!(account_state.balances[0].free.as_decimal(), dec!(7500.00));
    assert_eq!(account_state.balances[0].locked.as_decimal(), dec!(2500.00));
    assert_eq!(
        account_state.margins.len(),
        1,
        "intraday + overnight windows collapse to one account-level entry"
    );
}

#[rstest]
#[tokio::test]
async fn test_http_request_position_status_reports_for_cfm() {
    use nautilus_model::enums::PositionSideSpecified;

    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let reports = client
        .request_position_status_reports(account_id())
        .await
        .expect("position reports should build");

    assert_eq!(reports.len(), 1);
    let report = &reports[0];
    assert_eq!(report.position_side, PositionSideSpecified::Long);
    assert_eq!(report.quantity, Quantity::from("2"));
    assert_eq!(report.avg_px_open, Some(dec!(49000.00)));
    assert_eq!(report.instrument_id.symbol.as_str(), "BIP-20DEC30-CDE");

    // `get_or_fetch_instrument` may trigger a single /market/products/{id}
    // lookup to bootstrap the BIP instrument, which is fine: the CFM
    // endpoint itself must only be hit once.
    let positions_requests = state.requests_for("/cfm/positions");
    assert_eq!(positions_requests.len(), 1);
}

#[rstest]
#[tokio::test]
async fn test_http_request_position_status_report_single_product() {
    use nautilus_model::enums::PositionSideSpecified;

    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let instrument_id = InstrumentId::from("BIP-20DEC30-CDE.COINBASE");
    let report = client
        .request_position_status_report(account_id(), instrument_id)
        .await
        .expect("position report should build")
        .expect("fixture provides a non-flat position");

    assert_eq!(report.position_side, PositionSideSpecified::Short);
    assert_eq!(report.quantity, Quantity::from("3"));
    assert_eq!(report.avg_px_open, Some(dec!(51000.00)));

    let single_requests = state.requests_for("/cfm/positions/BIP-20DEC30-CDE");
    assert_eq!(single_requests.len(), 1);
}

#[rstest]
#[tokio::test]
async fn test_http_submit_order_threads_leverage_margin_type_reduce_only() {
    use nautilus_coinbase::common::enums::CoinbaseMarginType;

    let state = TestState::default();
    state.enqueue(
        "/orders",
        create_order_success_response("venue-500", "client-500"),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let _ = client
        .submit_order(
            ClientOrderId::new("client-500"),
            btc_usd_instrument_id(),
            OrderSide::Sell,
            OrderType::Limit,
            Quantity::from("0.5"),
            TimeInForce::Gtc,
            Some(Price::from("50000.00")),
            None,
            None,
            false,
            false,
            Some(dec!(5)),
            Some(CoinbaseMarginType::Cross),
            true,
            None,
        )
        .await
        .expect("submit should succeed");

    let requests = state.requests_for("/orders");
    assert_eq!(requests.len(), 1);
    let body = requests[0].body.as_ref().expect("POST body captured");
    assert_eq!(body["leverage"], "5");
    assert_eq!(body["margin_type"], "CROSS");
    assert_eq!(body["reduce_only"], true);
}

// reduce_only must not leak into the wire payload when the order is not
// flagged; Coinbase would otherwise reject the field on spot accounts.
#[rstest]
#[tokio::test]
async fn test_http_submit_order_omits_derivatives_fields_for_spot_defaults() {
    let state = TestState::default();
    state.enqueue(
        "/orders",
        create_order_success_response("venue-501", "client-501"),
    );

    let addr = start_mock_server(state.clone()).await;
    let client = create_http_client(addr);

    let _ = client
        .submit_order(
            ClientOrderId::new("client-501"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Limit,
            Quantity::from("0.5"),
            TimeInForce::Gtc,
            Some(Price::from("50000.00")),
            None,
            None,
            false,
            false,
            None,
            None,
            false,
            None,
        )
        .await
        .expect("submit should succeed");

    let requests = state.requests_for("/orders");
    assert_eq!(requests.len(), 1);
    let body = requests[0].body.as_ref().unwrap();
    assert!(body.get("leverage").is_none());
    assert!(body.get("margin_type").is_none());
    assert!(body.get("reduce_only").is_none());
}

// Builds a `CoinbaseExecutionClient` against the mock server with explicit
// account type so exec-client-level dispatch (Margin vs Cash) is exercised
// without going through `connect()`. The returned client is constructed but
// not connected; HTTP-backed methods still hit the mock because the base URL
// is threaded through the config.
fn make_exec_client(
    addr: std::net::SocketAddr,
    account_type: AccountType,
) -> CoinbaseExecutionClient {
    // The emitter inside the exec client tries to publish on the global
    // runner sender; install a drop-through channel so constructing it is
    // safe in tests that only call report-generation methods.
    let (sender, _rx) = tokio::sync::mpsc::unbounded_channel::<ExecutionEvent>();
    replace_exec_event_sender(sender);

    let cache = std::rc::Rc::new(std::cell::RefCell::new(Cache::default()));
    let core = ExecutionClientCore::new(
        TraderId::from("TRADER-001"),
        ClientId::from("COINBASE-TEST"),
        *COINBASE_VENUE,
        OmsType::Netting,
        AccountId::from("COINBASE-001"),
        account_type,
        None,
        cache,
    );

    let config = CoinbaseExecClientConfig {
        api_key: Some(test_api_key()),
        api_secret: Some(test_pem_key()),
        base_url_rest: Some(format!("http://{addr}")),
        account_type,
        ..CoinbaseExecClientConfig::default()
    };

    CoinbaseExecutionClient::new(core, config).expect("exec client construction")
}

fn position_status_reports_cmd(
    instrument_id: Option<InstrumentId>,
) -> GeneratePositionStatusReports {
    GeneratePositionStatusReportsBuilder::default()
        .ts_init(UnixNanos::default())
        .instrument_id(instrument_id)
        .build()
        .expect("cmd build")
}

#[rstest]
#[tokio::test]
async fn test_exec_client_position_reports_margin_list_hits_cfm_positions() {
    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = make_exec_client(addr, AccountType::Margin);

    let reports = client
        .generate_position_status_reports(&position_status_reports_cmd(None))
        .await
        .expect("position reports");

    assert_eq!(reports.len(), 1);
    assert_eq!(reports[0].position_side, PositionSideSpecified::Long);
    assert_eq!(reports[0].instrument_id.symbol.as_str(), "BIP-20DEC30-CDE");

    // Exec client must route to the list endpoint, not the single-product one.
    assert_eq!(state.requests_for("/cfm/positions").len(), 1);
    assert!(
        state
            .requests_for("/cfm/positions/BIP-20DEC30-CDE")
            .is_empty()
    );
}

#[rstest]
#[tokio::test]
async fn test_exec_client_position_reports_margin_single_hits_scoped_endpoint() {
    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = make_exec_client(addr, AccountType::Margin);

    let instrument_id = InstrumentId::from("BIP-20DEC30-CDE.COINBASE");
    let reports = client
        .generate_position_status_reports(&position_status_reports_cmd(Some(instrument_id)))
        .await
        .expect("position reports");

    assert_eq!(reports.len(), 1);
    assert_eq!(reports[0].position_side, PositionSideSpecified::Short);
    assert_eq!(reports[0].instrument_id, instrument_id);

    // Exec client must target the single-product endpoint; the list
    // endpoint should not be touched.
    assert_eq!(
        state.requests_for("/cfm/positions/BIP-20DEC30-CDE").len(),
        1
    );
    assert!(state.requests_for("/cfm/positions").is_empty());
}

// Cash clients have no positions: the exec client's fast-path must return
// empty without hitting the venue so a Cash/Margin factory mix-up does not
// leak CFM traffic onto a spot account.
#[rstest]
#[tokio::test]
async fn test_exec_client_position_reports_cash_returns_empty_without_http() {
    let state = TestState::default();
    let addr = start_mock_server(state.clone()).await;
    let client = make_exec_client(addr, AccountType::Cash);

    let reports = client
        .generate_position_status_reports(&position_status_reports_cmd(None))
        .await
        .expect("position reports");

    assert!(reports.is_empty());
    assert!(state.requests_for("/cfm/positions").is_empty());
    assert!(
        state
            .requests_for("/cfm/positions/BIP-20DEC30-CDE")
            .is_empty()
    );
}

// A 5xx from /cfm/positions must surface as an error rather than collapse
// to an empty Ok(vec![]); otherwise `generate_mass_status` would return a
// snapshot with zero positions and the live manager's reconciliation path
// would treat a venue outage as "no positions open". Retry budgets may
// retry the GET internally before the error propagates.
#[rstest]
#[tokio::test]
async fn test_exec_client_position_reports_margin_list_propagates_http_failure() {
    let state = TestState::default();
    state.mark_failing("/cfm/positions");
    let addr = start_mock_server(state.clone()).await;
    let client = make_exec_client(addr, AccountType::Margin);

    let result = client
        .generate_position_status_reports(&position_status_reports_cmd(None))
        .await;
    assert!(
        result.is_err(),
        "503 from /cfm/positions must propagate, was {:?}",
        result.as_ref().map(Vec::len)
    );
    assert!(!state.requests_for("/cfm/positions").is_empty());
}

#[rstest]
#[tokio::test]
async fn test_exec_client_position_reports_margin_single_propagates_http_failure() {
    let state = TestState::default();
    state.mark_failing("/cfm/positions/BIP-20DEC30-CDE");
    let addr = start_mock_server(state.clone()).await;
    let client = make_exec_client(addr, AccountType::Margin);

    let result = client
        .generate_position_status_reports(&position_status_reports_cmd(Some(InstrumentId::from(
            "BIP-20DEC30-CDE.COINBASE",
        ))))
        .await;
    assert!(
        result.is_err(),
        "503 from /cfm/positions/BIP-20DEC30-CDE must propagate, was {:?}",
        result.as_ref().map(Vec::len)
    );
}

// Mass-status on a Margin client must route position reports through the
// CFM endpoint, not treat the Margin account as spot.
#[rstest]
#[tokio::test]
async fn test_exec_client_mass_status_margin_includes_cfm_positions() {
    let state = TestState::default();
    // Mass status also fetches orders/fills; enqueue empty pages.
    state.enqueue(
        "/orders/historical/batch",
        json!({"orders": [], "sequence": "0", "has_next": false, "cursor": ""}),
    );
    state.enqueue(
        "/orders/historical/fills",
        json!({"fills": [], "cursor": ""}),
    );
    let addr = start_mock_server(state.clone()).await;
    let client = make_exec_client(addr, AccountType::Margin);

    let mass_status = client
        .generate_mass_status(Some(60))
        .await
        .expect("mass status")
        .expect("mass status populated");

    // One position was expected through /cfm/positions; mass_status's
    // position_reports map is keyed by instrument_id with a Vec of reports.
    let position_reports = mass_status.position_reports();
    let instrument_id = InstrumentId::from("BIP-20DEC30-CDE.COINBASE");
    assert_eq!(
        position_reports.get(&instrument_id).map(Vec::len),
        Some(1),
        "Margin mass status must carry the CFM position"
    );
}

// HTTP error-path tests. Each spins up an ad-hoc router with a single failure
// behaviour so the assertion is unambiguous: the test name names the failure
// mode, and a regression in retry/parse handling fails exactly the right test.

async fn start_failure_server(router: Router) -> SocketAddr {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    tokio::spawn(async move {
        axum::serve(listener, router).await.unwrap();
    });

    let start = std::time::Instant::now();

    loop {
        if tokio::net::TcpStream::connect(addr).await.is_ok() {
            break;
        }
        assert!(
            start.elapsed() <= std::time::Duration::from_secs(5),
            "failure server did not start within timeout"
        );
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
    }
    addr
}

#[rstest]
#[tokio::test]
async fn test_http_submit_surfaces_error_on_500_status() {
    let attempts = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let attempts_clone = Arc::clone(&attempts);
    let router = Router::new().route(
        "/api/v3/brokerage/orders",
        post(move || {
            let attempts = Arc::clone(&attempts_clone);
            async move {
                attempts.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                (
                    axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                    Json(json!({"error": "internal error"})),
                )
            }
        }),
    );
    let addr = start_failure_server(router).await;
    let client = create_http_client_with_retry(addr, Some(fast_retry_config(3)));

    let result = client
        .submit_order(
            ClientOrderId::new("client-500"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Limit,
            Quantity::from("0.1"),
            TimeInForce::Gtc,
            Some(Price::from("50000.00")),
            None,
            None,
            false,
            false,
            None,
            None,
            false,
            None,
        )
        .await;
    assert!(result.is_err(), "expected 500 to surface as error");
    // POSTs are non-idempotent; a 500 should never trigger the retry loop.
    assert_eq!(attempts.load(std::sync::atomic::Ordering::SeqCst), 1);
}

#[rstest]
#[tokio::test]
async fn test_http_submit_surfaces_error_on_429_status_without_retry() {
    let attempts = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let attempts_clone = Arc::clone(&attempts);
    let router = Router::new().route(
        "/api/v3/brokerage/orders",
        post(move || {
            let attempts = Arc::clone(&attempts_clone);
            async move {
                attempts.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                (
                    axum::http::StatusCode::TOO_MANY_REQUESTS,
                    Json(json!({"error": "rate limited"})),
                )
            }
        }),
    );
    let addr = start_failure_server(router).await;
    let client = create_http_client_with_retry(addr, Some(fast_retry_config(3)));

    let result = client
        .submit_order(
            ClientOrderId::new("client-429"),
            btc_usd_instrument_id(),
            OrderSide::Buy,
            OrderType::Limit,
            Quantity::from("0.1"),
            TimeInForce::Gtc,
            Some(Price::from("50000.00")),
            None,
            None,
            false,
            false,
            None,
            None,
            false,
            None,
        )
        .await;
    assert!(result.is_err(), "expected 429 to surface as error");
    assert_eq!(attempts.load(std::sync::atomic::Ordering::SeqCst), 1);
}

#[rstest]
#[tokio::test]
async fn test_http_get_products_surfaces_error_on_malformed_body() {
    // Server returns 200 with a non-JSON body. The deserializer must surface
    // the parse failure rather than swallow it silently. Track route hits so
    // the assertion would not pass on an off-route 404.
    let hits = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let hits_clone = Arc::clone(&hits);
    let router = Router::new().route(
        "/api/v3/brokerage/market/products",
        get(move || {
            let hits = Arc::clone(&hits_clone);
            async move {
                hits.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                "not json{{{"
            }
        }),
    );
    let addr = start_failure_server(router).await;
    let client = create_http_client(addr);

    let result = client.get_products().await;
    assert_eq!(
        hits.load(std::sync::atomic::Ordering::SeqCst),
        1,
        "expected the malformed-body route to be hit exactly once"
    );
    let err = result.expect_err("malformed body must surface as a parse error");
    let msg = err.to_string().to_lowercase();
    assert!(
        msg.contains("parse")
            || msg.contains("decode")
            || msg.contains("expected")
            || msg.contains("json")
            || msg.contains("deserialize"),
        "expected a parse/decode-style error, was: {err}"
    );
}

#[rstest]
#[tokio::test]
async fn test_http_get_products_surfaces_error_on_404() {
    let hits = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let hits_clone = Arc::clone(&hits);
    let router = Router::new().route(
        "/api/v3/brokerage/market/products",
        get(move || {
            let hits = Arc::clone(&hits_clone);
            async move {
                hits.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                (
                    axum::http::StatusCode::NOT_FOUND,
                    Json(json!({"error": "not found"})),
                )
            }
        }),
    );
    let addr = start_failure_server(router).await;
    let client = create_http_client(addr);

    let result = client.get_products().await;
    assert_eq!(
        hits.load(std::sync::atomic::Ordering::SeqCst),
        1,
        "expected the 404 route to be hit exactly once"
    );
    let err = result.expect_err("404 must surface as error");
    let msg = err.to_string();
    assert!(
        msg.contains("404") || msg.contains("not found") || msg.contains("Not Found"),
        "expected the error to reference the 404 status, was: {err}"
    );
}