stateset-http 1.22.0

HTTP service layer (REST + SSE) for the StateSet commerce engine
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
//! Server builder for configuring and running the HTTP service.

use std::{
    collections::HashSet,
    fmt,
    net::{IpAddr, SocketAddr},
    path::PathBuf,
};

use axum::Router;
use stateset_authz::AuthzEngine;
use stateset_embedded::Commerce;
use uuid::Uuid;

use crate::error::HttpError;
use crate::middleware::{self, AuthzConfig, BearerAuthBinding, RateLimitConfig};
use crate::routes::{self, DEFAULT_REQUEST_BODY_LIMIT_BYTES};
use crate::state::{AppState, IpCidr, MetricsHeaderLimits};

/// Default bind address.
const DEFAULT_ADDR: ([u8; 4], u16) = ([127, 0, 0, 1], 3000);
const METRICS_IP_ALLOWLIST_ENV: &str = "STATESET_HTTP_METRICS_IP_ALLOWLIST";
const METRICS_IP_CIDR_ALLOWLIST_ENV: &str = "STATESET_HTTP_METRICS_IP_CIDR_ALLOWLIST";
const METRICS_TRUSTED_PROXIES_ENV: &str = "STATESET_HTTP_METRICS_TRUSTED_PROXIES";
const METRICS_FORWARDED_HEADER_MAX_BYTES_ENV: &str = "STATESET_HTTP_METRICS_FORWARDED_MAX_BYTES";
const METRICS_X_FORWARDED_FOR_HEADER_MAX_BYTES_ENV: &str =
    "STATESET_HTTP_METRICS_X_FORWARDED_FOR_MAX_BYTES";
const METRICS_X_REAL_IP_HEADER_MAX_BYTES_ENV: &str = "STATESET_HTTP_METRICS_X_REAL_IP_MAX_BYTES";
const METRICS_AUTHORIZATION_HEADER_MAX_BYTES_ENV: &str =
    "STATESET_HTTP_METRICS_AUTHORIZATION_MAX_BYTES";
const REQUEST_BODY_MAX_BYTES_ENV: &str = "STATESET_HTTP_MAX_BODY_BYTES";
const ALLOW_UNAUTHENTICATED_ENV: &str = "STATESET_HTTP_ALLOW_UNAUTHENTICATED";
const REQUIRE_IDEMPOTENCY_KEYS_ENV: &str = "STATESET_HTTP_REQUIRE_IDEMPOTENCY_KEYS";

#[derive(Clone, Debug, PartialEq, Eq)]
struct AdditionalApiBearerBinding {
    token: String,
    bound_tenant_id: Option<String>,
    bound_actor_id: Option<String>,
}

impl AdditionalApiBearerBinding {
    const fn new(
        token: String,
        bound_tenant_id: Option<String>,
        bound_actor_id: Option<String>,
    ) -> Self {
        Self { token, bound_tenant_id, bound_actor_id }
    }
}

fn parse_ip_allowlist_csv(env_var: &str, value: &str) -> Result<Vec<IpAddr>, HttpError> {
    let mut ips = value
        .split(',')
        .map(str::trim)
        .filter(|entry| !entry.is_empty())
        .map(|entry| {
            entry.parse::<IpAddr>().map_err(|error| {
                HttpError::BadRequest(format!("invalid IP '{entry}' in {env_var}: {error}"))
            })
        })
        .collect::<Result<Vec<_>, _>>()?;
    ips.sort_unstable();
    ips.dedup();
    Ok(ips)
}

fn parse_ip_cidr_allowlist_csv(env_var: &str, value: &str) -> Result<Vec<IpCidr>, HttpError> {
    let mut cidrs = value
        .split(',')
        .map(str::trim)
        .filter(|entry| !entry.is_empty())
        .map(|entry| {
            entry.parse::<IpCidr>().map_err(|error| {
                HttpError::BadRequest(format!("invalid CIDR '{entry}' in {env_var}: {error}"))
            })
        })
        .collect::<Result<Vec<_>, _>>()?;
    cidrs.sort_unstable();
    cidrs.dedup();
    Ok(cidrs)
}

fn parse_positive_usize_env(env_var: &str, value: &str) -> Result<usize, HttpError> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return Err(HttpError::BadRequest(format!(
            "{env_var} must be a positive integer greater than zero"
        )));
    }
    let parsed = trimmed.parse::<usize>().map_err(|error| {
        HttpError::BadRequest(format!("invalid value '{trimmed}' in {env_var}: {error}"))
    })?;
    if parsed == 0 {
        return Err(HttpError::BadRequest(format!("{env_var} must be greater than zero")));
    }
    Ok(parsed)
}

fn parse_bool_env(env_var: &str, value: &str) -> Result<bool, HttpError> {
    match value.trim().to_ascii_lowercase().as_str() {
        "1" | "true" | "yes" => Ok(true),
        "0" | "false" | "no" | "" => Ok(false),
        other => Err(HttpError::BadRequest(format!(
            "invalid value '{other}' in {env_var}: expected true/false"
        ))),
    }
}

/// Builder for configuring and launching the HTTP server.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::Commerce;
/// use stateset_http::ServerBuilder;
///
/// let commerce = Commerce::new(":memory:")?;
///
/// ServerBuilder::new_from_env(commerce)?
///     .bind("0.0.0.0:8080".parse()?)
///     .with_cors()
///     .with_request_id()
///     .with_bearer_auth("replace-me-with-a-secret")
///     .serve()
///     .await?;
/// ```
pub struct ServerBuilder {
    state: AppState,
    addr: SocketAddr,
    enable_cors: bool,
    enable_request_id: bool,
    api_bearer_token: Option<String>,
    bound_tenant_id: Option<String>,
    bound_actor_id: Option<String>,
    additional_api_bearer_bindings: Vec<AdditionalApiBearerBinding>,
    generated_default_token: bool,
    max_request_body_bytes: usize,
    authz_config: Option<AuthzConfig>,
    trust_actor_headers_for_authz: bool,
    authz_strict: bool,
    allow_unauthenticated: bool,
    rate_limit: Option<RateLimitConfig>,
    require_idempotency_keys: bool,
}

impl fmt::Debug for ServerBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ServerBuilder")
            .field("state", &"AppState { .. }")
            .field("addr", &self.addr)
            .field("enable_cors", &self.enable_cors)
            .field("enable_request_id", &self.enable_request_id)
            .field("api_bearer_token", &self.api_bearer_token.as_ref().map(|_| "<redacted>"))
            .field(
                "metrics_bearer_token",
                &self.state.metrics_bearer_auth_token().map(|_| "<redacted>"),
            )
            .field("bound_tenant_id", &self.bound_tenant_id.as_ref().map(|_| "<redacted>"))
            .field("bound_actor_id", &self.bound_actor_id.as_ref().map(|_| "<redacted>"))
            .field("additional_api_bearer_bindings", &self.additional_api_bearer_bindings.len())
            .field("generated_default_token", &self.generated_default_token)
            .field("max_request_body_bytes", &self.max_request_body_bytes)
            .field("authz_enabled", &self.authz_config.is_some())
            .field("trust_actor_headers_for_authz", &self.trust_actor_headers_for_authz)
            .field("authz_strict", &self.authz_strict)
            .field("allow_unauthenticated", &self.allow_unauthenticated)
            .field("rate_limit", &self.rate_limit)
            .field("require_idempotency_keys", &self.require_idempotency_keys)
            .finish()
    }
}

impl ServerBuilder {
    fn api_bearer_bindings(&self) -> Vec<AdditionalApiBearerBinding> {
        let mut bindings = Vec::with_capacity(
            usize::from(self.api_bearer_token.is_some())
                + self.additional_api_bearer_bindings.len(),
        );
        if let Some(token) = self.api_bearer_token.clone() {
            bindings.push(AdditionalApiBearerBinding::new(
                token,
                self.bound_tenant_id.clone(),
                self.bound_actor_id.clone(),
            ));
        }
        bindings.extend(self.additional_api_bearer_bindings.iter().cloned());
        bindings
    }

    fn tenant_routing_auth_error(&self) -> Option<&'static str> {
        self.state.tenant_db_dir()?;
        let bindings = self.api_bearer_bindings();
        if bindings.is_empty() {
            return Some("per-tenant database routing requires API auth to remain enabled");
        }
        if bindings.iter().any(|binding| binding.bound_tenant_id.is_none()) {
            return Some(
                "per-tenant database routing requires binding every API bearer token to a single tenant",
            );
        }
        None
    }

    fn api_auth_error(&self) -> Option<String> {
        if let Some(message) = self.tenant_routing_auth_error() {
            return Some(message.to_string());
        }
        let bindings = self.api_bearer_bindings();
        if bindings.is_empty() {
            if self.authz_config.is_some() && !self.trust_actor_headers_for_authz {
                return Some(
                    "request authorization requires actor-bound API authentication or explicitly trusted x-actor-id headers"
                        .to_string(),
                );
            }
            if self.bound_actor_id.is_some() {
                return Some(
                    "actor-bound API authentication requires API auth to remain enabled"
                        .to_string(),
                );
            }
            return None;
        }

        let mut seen_tokens = HashSet::with_capacity(bindings.len());
        for binding in &bindings {
            if !seen_tokens.insert(binding.token.clone()) {
                return Some("duplicate API bearer tokens are not allowed".to_string());
            }
            if let Some(bound_actor_id) = binding.bound_actor_id.as_deref() {
                if !middleware::is_valid_actor_id(bound_actor_id) {
                    return Some("bound actor ID is invalid".to_string());
                }
            }
        }
        if self.authz_config.is_some()
            && !self.trust_actor_headers_for_authz
            && bindings.iter().any(|binding| binding.bound_actor_id.is_none())
        {
            return Some(
                "request authorization requires binding every API bearer token to a single actor or explicitly trusting x-actor-id headers"
                    .to_string(),
            );
        }
        None
    }

    /// Create a new server builder wrapping a [`Commerce`] instance.
    #[must_use]
    pub fn new(commerce: Commerce) -> Self {
        let generated_token = Uuid::new_v4().to_string();
        Self {
            state: AppState::new(commerce).with_metrics_bearer_auth(generated_token.clone()),
            addr: SocketAddr::from(DEFAULT_ADDR),
            enable_cors: false,
            enable_request_id: false,
            // Secure-by-default: API routes require an auth token unless
            // explicitly disabled.
            api_bearer_token: Some(generated_token),
            bound_tenant_id: None,
            bound_actor_id: None,
            additional_api_bearer_bindings: Vec::new(),
            generated_default_token: true,
            max_request_body_bytes: DEFAULT_REQUEST_BODY_LIMIT_BYTES,
            authz_config: None,
            trust_actor_headers_for_authz: false,
            authz_strict: false,
            allow_unauthenticated: false,
            rate_limit: None,
            // Secure-by-default: money-moving create endpoints require an
            // Idempotency-Key header (HTTP 428 when missing).
            require_idempotency_keys: true,
        }
    }

    /// Create a new server builder and apply `/metrics` network policy from environment.
    ///
    /// This is useful for startup wiring where operators configure network policy via:
    /// - `STATESET_HTTP_METRICS_IP_ALLOWLIST`
    /// - `STATESET_HTTP_METRICS_IP_CIDR_ALLOWLIST`
    /// - `STATESET_HTTP_METRICS_TRUSTED_PROXIES`
    /// - `STATESET_HTTP_METRICS_FORWARDED_MAX_BYTES`
    /// - `STATESET_HTTP_METRICS_X_FORWARDED_FOR_MAX_BYTES`
    /// - `STATESET_HTTP_METRICS_X_REAL_IP_MAX_BYTES`
    /// - `STATESET_HTTP_METRICS_AUTHORIZATION_MAX_BYTES`
    /// - `STATESET_HTTP_ALLOW_UNAUTHENTICATED`
    pub fn new_from_env(commerce: Commerce) -> Result<Self, HttpError> {
        Self::new(commerce)
            .with_metrics_network_policy_from_env()?
            .with_metrics_header_limits_from_env()?
            .with_request_body_limit_from_env()?
            .with_allow_unauthenticated_from_env()?
            .with_require_idempotency_keys_from_env()
    }

    /// Apply the unauthenticated opt-out from `STATESET_HTTP_ALLOW_UNAUTHENTICATED`.
    ///
    /// Accepts `true`/`false` (also `1`/`0`, `yes`/`no`). Unset or empty leaves
    /// the secure default (`false`) in place.
    pub fn with_allow_unauthenticated_from_env(mut self) -> Result<Self, HttpError> {
        if let Ok(raw) = std::env::var(ALLOW_UNAUTHENTICATED_ENV) {
            self.allow_unauthenticated = parse_bool_env(ALLOW_UNAUTHENTICATED_ENV, &raw)?;
        }
        Ok(self)
    }

    /// Require an `Idempotency-Key` header on money-moving create endpoints
    /// (`POST /orders`, `/payments`, `/payments/{id}/refund`, `/ap/payments`),
    /// returning HTTP 428 (Precondition Required) when it is missing.
    ///
    /// Defaults to `true`; pass `false` to let existing deployments opt out.
    #[must_use]
    pub const fn require_idempotency_keys(mut self, require: bool) -> Self {
        self.require_idempotency_keys = require;
        self
    }

    /// Apply the required-idempotency-key opt-out from
    /// `STATESET_HTTP_REQUIRE_IDEMPOTENCY_KEYS`.
    ///
    /// Accepts `true`/`false` (also `1`/`0`, `yes`/`no`). Unset or empty leaves
    /// the secure default (`true`) in place.
    pub fn with_require_idempotency_keys_from_env(self) -> Result<Self, HttpError> {
        let raw = std::env::var(REQUIRE_IDEMPOTENCY_KEYS_ENV).ok();
        self.with_require_idempotency_keys_from_value(raw.as_deref())
    }

    /// Apply the required-idempotency-key setting from an optional raw value.
    fn with_require_idempotency_keys_from_value(
        mut self,
        raw: Option<&str>,
    ) -> Result<Self, HttpError> {
        if let Some(raw) = raw {
            self.require_idempotency_keys = parse_bool_env(REQUIRE_IDEMPOTENCY_KEYS_ENV, raw)?;
        }
        Ok(self)
    }

    /// Set the bind address.
    #[must_use]
    pub const fn bind(mut self, addr: SocketAddr) -> Self {
        self.addr = addr;
        self
    }

    /// Enable CORS middleware with explicit defaults and optional env override.
    ///
    /// Set `STATESET_HTTP_ALLOWED_ORIGINS` to a comma-separated list of origins
    /// to override the localhost-only defaults.
    #[must_use]
    pub const fn with_cors(mut self) -> Self {
        self.enable_cors = true;
        self
    }

    /// Enable request-ID middleware (generates UUID, propagates in headers).
    #[must_use]
    pub const fn with_request_id(mut self) -> Self {
        self.enable_request_id = true;
        self
    }

    /// Configure bearer authentication for `/api/v1/*` endpoints.
    ///
    /// Requests to API routes must include `Authorization: Bearer <token>`.
    #[must_use]
    pub fn with_bearer_auth(mut self, token: impl Into<String>) -> Self {
        self.api_bearer_token = Some(token.into());
        self.generated_default_token = false;
        self
    }

    fn push_bearer_auth_binding(
        &mut self,
        token: impl Into<String>,
        bound_tenant_id: Option<String>,
        bound_actor_id: Option<String>,
    ) {
        self.additional_api_bearer_bindings.push(AdditionalApiBearerBinding::new(
            token.into(),
            bound_tenant_id,
            bound_actor_id,
        ));
    }

    /// Configure bearer authentication for `/metrics`.
    #[must_use]
    pub fn with_metrics_bearer_auth(mut self, token: impl Into<String>) -> Self {
        self.state = self.state.with_metrics_bearer_auth(token);
        self
    }

    /// Disable authentication for `/metrics`.
    #[must_use]
    pub fn without_metrics_auth(mut self) -> Self {
        self.state = self.state.without_metrics_auth();
        self
    }

    /// Configure a client IP allowlist for `/metrics`.
    #[must_use]
    pub fn with_metrics_ip_allowlist<I>(mut self, ips: I) -> Self
    where
        I: IntoIterator<Item = IpAddr>,
    {
        self.state = self.state.with_metrics_ip_allowlist(ips);
        self
    }

    /// Disable `/metrics` IP allowlist checks.
    #[must_use]
    pub fn without_metrics_ip_allowlist(mut self) -> Self {
        self.state = self.state.without_metrics_ip_allowlist();
        self
    }

    /// Configure a CIDR-based client IP allowlist for `/metrics`.
    #[must_use]
    pub fn with_metrics_ip_cidr_allowlist<I>(mut self, cidrs: I) -> Self
    where
        I: IntoIterator<Item = IpCidr>,
    {
        self.state = self.state.with_metrics_ip_cidr_allowlist(cidrs);
        self
    }

    /// Disable CIDR-based `/metrics` IP allowlist checks.
    #[must_use]
    pub fn without_metrics_ip_cidr_allowlist(mut self) -> Self {
        self.state = self.state.without_metrics_ip_cidr_allowlist();
        self
    }

    /// Configure trusted proxy CIDRs for `/metrics`.
    #[must_use]
    pub fn with_metrics_trusted_proxies<I>(mut self, cidrs: I) -> Self
    where
        I: IntoIterator<Item = IpCidr>,
    {
        self.state = self.state.with_metrics_trusted_proxies(cidrs);
        self
    }

    /// Configure max accepted forwarding header lengths for `/metrics`.
    #[must_use]
    pub fn with_metrics_header_limits(mut self, limits: MetricsHeaderLimits) -> Self {
        self.state = self.state.with_metrics_header_limits(limits);
        self
    }

    /// Apply `/metrics` network policy from environment variables.
    ///
    /// Supported variables (comma-separated entries):
    /// - `STATESET_HTTP_METRICS_IP_ALLOWLIST` (exact IPs)
    /// - `STATESET_HTTP_METRICS_IP_CIDR_ALLOWLIST` (CIDRs and/or IPs)
    /// - `STATESET_HTTP_METRICS_TRUSTED_PROXIES` (CIDRs and/or IPs)
    ///
    /// If a variable is present but empty, the corresponding config is disabled.
    pub fn with_metrics_network_policy_from_env(self) -> Result<Self, HttpError> {
        let ip_allowlist = std::env::var(METRICS_IP_ALLOWLIST_ENV).ok();
        let ip_cidr_allowlist = std::env::var(METRICS_IP_CIDR_ALLOWLIST_ENV).ok();
        let trusted_proxies = std::env::var(METRICS_TRUSTED_PROXIES_ENV).ok();
        self.with_metrics_network_policy_from_values(
            ip_allowlist.as_deref(),
            ip_cidr_allowlist.as_deref(),
            trusted_proxies.as_deref(),
        )
    }

    /// Apply `/metrics` forwarding header limits from environment variables.
    ///
    /// Supported variables (positive integers in bytes):
    /// - `STATESET_HTTP_METRICS_FORWARDED_MAX_BYTES`
    /// - `STATESET_HTTP_METRICS_X_FORWARDED_FOR_MAX_BYTES`
    /// - `STATESET_HTTP_METRICS_X_REAL_IP_MAX_BYTES`
    /// - `STATESET_HTTP_METRICS_AUTHORIZATION_MAX_BYTES`
    pub fn with_metrics_header_limits_from_env(self) -> Result<Self, HttpError> {
        let forwarded = std::env::var(METRICS_FORWARDED_HEADER_MAX_BYTES_ENV).ok();
        let x_forwarded_for = std::env::var(METRICS_X_FORWARDED_FOR_HEADER_MAX_BYTES_ENV).ok();
        let x_real_ip = std::env::var(METRICS_X_REAL_IP_HEADER_MAX_BYTES_ENV).ok();
        let authorization = std::env::var(METRICS_AUTHORIZATION_HEADER_MAX_BYTES_ENV).ok();
        self.with_metrics_header_limits_from_values(
            forwarded.as_deref(),
            x_forwarded_for.as_deref(),
            x_real_ip.as_deref(),
            authorization.as_deref(),
        )
    }

    fn with_metrics_network_policy_from_values(
        mut self,
        ip_allowlist: Option<&str>,
        ip_cidr_allowlist: Option<&str>,
        trusted_proxies: Option<&str>,
    ) -> Result<Self, HttpError> {
        if let Some(raw) = ip_allowlist {
            let ips = parse_ip_allowlist_csv(METRICS_IP_ALLOWLIST_ENV, raw)?;
            self = if ips.is_empty() {
                self.without_metrics_ip_allowlist()
            } else {
                self.with_metrics_ip_allowlist(ips)
            };
        }

        if let Some(raw) = ip_cidr_allowlist {
            let cidrs = parse_ip_cidr_allowlist_csv(METRICS_IP_CIDR_ALLOWLIST_ENV, raw)?;
            self = if cidrs.is_empty() {
                self.without_metrics_ip_cidr_allowlist()
            } else {
                self.with_metrics_ip_cidr_allowlist(cidrs)
            };
        }

        if let Some(raw) = trusted_proxies {
            let cidrs = parse_ip_cidr_allowlist_csv(METRICS_TRUSTED_PROXIES_ENV, raw)?;
            self = if cidrs.is_empty() {
                self.without_metrics_trusted_proxies()
            } else {
                self.with_metrics_trusted_proxies(cidrs)
            };
        }

        Ok(self)
    }

    fn with_metrics_header_limits_from_values(
        self,
        forwarded: Option<&str>,
        x_forwarded_for: Option<&str>,
        x_real_ip: Option<&str>,
        authorization: Option<&str>,
    ) -> Result<Self, HttpError> {
        let current = self.metrics_header_limits();
        let forwarded = if let Some(raw) = forwarded {
            parse_positive_usize_env(METRICS_FORWARDED_HEADER_MAX_BYTES_ENV, raw)?
        } else {
            current.forwarded_header_value_bytes()
        };
        let x_forwarded_for = if let Some(raw) = x_forwarded_for {
            parse_positive_usize_env(METRICS_X_FORWARDED_FOR_HEADER_MAX_BYTES_ENV, raw)?
        } else {
            current.x_forwarded_for_header_value_bytes()
        };
        let x_real_ip = if let Some(raw) = x_real_ip {
            parse_positive_usize_env(METRICS_X_REAL_IP_HEADER_MAX_BYTES_ENV, raw)?
        } else {
            current.x_real_ip_header_value_bytes()
        };
        let authorization = if let Some(raw) = authorization {
            parse_positive_usize_env(METRICS_AUTHORIZATION_HEADER_MAX_BYTES_ENV, raw)?
        } else {
            current.authorization_header_value_bytes()
        };
        let limits = MetricsHeaderLimits::new_with_authorization(
            forwarded,
            x_forwarded_for,
            x_real_ip,
            authorization,
        )?;
        Ok(self.with_metrics_header_limits(limits))
    }

    /// Apply the request body size limit from `STATESET_HTTP_MAX_BODY_BYTES`.
    pub fn with_request_body_limit_from_env(self) -> Result<Self, HttpError> {
        let max_body_bytes = std::env::var(REQUEST_BODY_MAX_BYTES_ENV).ok();
        self.with_request_body_limit_from_value(max_body_bytes.as_deref())
    }

    fn with_request_body_limit_from_value(
        mut self,
        max_body_bytes: Option<&str>,
    ) -> Result<Self, HttpError> {
        if let Some(raw) = max_body_bytes {
            self.max_request_body_bytes =
                parse_positive_usize_env(REQUEST_BODY_MAX_BYTES_ENV, raw)?;
        }
        Ok(self)
    }

    /// Disable trusted proxy checks for `/metrics`.
    #[must_use]
    pub fn without_metrics_trusted_proxies(mut self) -> Self {
        self.state = self.state.without_metrics_trusted_proxies();
        self
    }

    /// Bind the configured bearer token to a single tenant.
    ///
    /// Requests using this token must present the same `x-tenant-id`.
    #[must_use]
    pub fn bind_auth_tenant(mut self, tenant_id: impl Into<String>) -> Self {
        self.bound_tenant_id = Some(tenant_id.into());
        self
    }

    /// Bind the configured bearer token to a single actor.
    ///
    /// When set, authenticated API requests are treated as this actor during
    /// authorization checks, and any conflicting `x-actor-id` header is rejected.
    #[must_use]
    pub fn bind_auth_actor(mut self, actor_id: impl Into<String>) -> Self {
        self.bound_actor_id = Some(actor_id.into());
        self
    }

    /// Configure bearer authentication and bind it to a tenant in one call.
    #[must_use]
    pub fn with_bearer_auth_for_tenant(
        self,
        token: impl Into<String>,
        tenant_id: impl Into<String>,
    ) -> Self {
        self.with_bearer_auth(token).bind_auth_tenant(tenant_id)
    }

    /// Configure bearer authentication and bind it to an actor in one call.
    #[must_use]
    pub fn with_bearer_auth_for_actor(
        self,
        token: impl Into<String>,
        actor_id: impl Into<String>,
    ) -> Self {
        self.with_bearer_auth(token).bind_auth_actor(actor_id)
    }

    /// Add an additional bearer token for `/api/v1/*` endpoints.
    ///
    /// This leaves the primary configured API token unchanged.
    #[must_use]
    pub fn add_bearer_auth(mut self, token: impl Into<String>) -> Self {
        self.push_bearer_auth_binding(token, None, None);
        self
    }

    /// Add an additional bearer token bound to a tenant.
    #[must_use]
    pub fn add_bearer_auth_for_tenant(
        mut self,
        token: impl Into<String>,
        tenant_id: impl Into<String>,
    ) -> Self {
        self.push_bearer_auth_binding(token, Some(tenant_id.into()), None);
        self
    }

    /// Add an additional bearer token bound to an actor.
    #[must_use]
    pub fn add_bearer_auth_for_actor(
        mut self,
        token: impl Into<String>,
        actor_id: impl Into<String>,
    ) -> Self {
        self.push_bearer_auth_binding(token, None, Some(actor_id.into()));
        self
    }

    /// Add an additional bearer token bound to both an actor and a tenant.
    #[must_use]
    pub fn add_bearer_auth_for_actor_and_tenant(
        mut self,
        token: impl Into<String>,
        actor_id: impl Into<String>,
        tenant_id: impl Into<String>,
    ) -> Self {
        self.push_bearer_auth_binding(token, Some(tenant_id.into()), Some(actor_id.into()));
        self
    }

    /// Enable per-tenant storage using `<base_dir>/<tenant>.db`.
    #[must_use]
    pub fn with_tenant_db_dir(mut self, base_dir: impl Into<PathBuf>) -> Self {
        self.state = self.state.with_tenant_db_dir(base_dir);
        self
    }

    /// Ignore `x-tenant-id` headers when per-tenant routing is disabled.
    ///
    /// By default such requests are rejected with `400` instead of silently
    /// being served shared data. Use this escape hatch only for deployments
    /// that intentionally front a multi-tenant proxy which handles tenant
    /// isolation upstream.
    #[must_use]
    pub fn with_ignore_tenant_header(mut self) -> Self {
        self.state = self.state.with_ignore_tenant_header();
        self
    }

    /// Set the maximum number of lazily created tenant databases kept in-memory.
    #[must_use]
    pub fn with_max_tenant_dbs(mut self, max_tenant_dbs: usize) -> Self {
        self.state = self.state.with_max_tenant_dbs(max_tenant_dbs);
        self
    }

    /// Set the maximum accepted request body size for extractor-based endpoints.
    #[must_use]
    pub const fn with_max_request_body_bytes(mut self, max_request_body_bytes: usize) -> Self {
        self.max_request_body_bytes =
            if max_request_body_bytes == 0 { 1 } else { max_request_body_bytes };
        self
    }

    /// Enable request authorization for `/api/v1/*` using a provided authz engine.
    ///
    /// By default, authorization expects actor identity to come from actor-bound
    /// bearer tokens. If you want to trust `x-actor-id` request headers instead,
    /// also call [`Self::trust_actor_headers_for_authz`].
    #[must_use]
    pub fn with_authz_engine(mut self, engine: AuthzEngine) -> Self {
        self.authz_config = Some(AuthzConfig::new(engine));
        self
    }

    /// Explicitly trust `x-actor-id` request headers for authorization.
    ///
    /// Use this only behind a trusted upstream that authenticates callers and
    /// strips or overwrites any client-supplied actor header.
    #[must_use]
    pub const fn trust_actor_headers_for_authz(mut self) -> Self {
        self.trust_actor_headers_for_authz = true;
        self
    }

    /// Explicitly opt out of the fail-closed authentication startup check.
    ///
    /// By default the server refuses to start on a non-loopback bind address
    /// when no API bearer tokens are configured. Call this (or set
    /// `STATESET_HTTP_ALLOW_UNAUTHENTICATED=true`) to acknowledge the risk and
    /// serve unauthenticated API traffic anyway.
    #[must_use]
    pub const fn allow_unauthenticated(mut self) -> Self {
        self.allow_unauthenticated = true;
        self
    }

    /// Fail closed on authorization for unmapped API paths.
    ///
    /// By default, `/api/v1` paths that the authorization layer cannot map to
    /// a resource/action pair bypass authorization (authentication still
    /// applies). When strict mode is enabled, such requests are denied with
    /// HTTP 403 instead. Only takes effect when authorization is configured
    /// via [`Self::with_authz`].
    #[must_use]
    pub const fn with_strict_authz(mut self) -> Self {
        self.authz_strict = true;
        self
    }

    /// Disable API authentication (not recommended for untrusted networks).
    #[must_use]
    pub fn without_auth(mut self) -> Self {
        self.api_bearer_token = None;
        self.bound_tenant_id = None;
        self.bound_actor_id = None;
        self.additional_api_bearer_bindings.clear();
        self.generated_default_token = false;
        self
    }

    /// Enable global rate limiting with a token bucket.
    ///
    /// `requests_per_second` controls steady-state throughput; `burst_size`
    /// controls how many requests can be served in a burst before throttling.
    /// Requests exceeding the limit receive HTTP 429.
    #[must_use]
    pub const fn with_rate_limit(mut self, requests_per_second: u64, burst_size: u64) -> Self {
        self.rate_limit = Some(RateLimitConfig { requests_per_second, burst_size });
        self
    }

    /// Return the configured bearer token, if auth is enabled.
    #[must_use]
    pub fn bearer_auth_token(&self) -> Option<&str> {
        self.api_bearer_token.as_deref()
    }

    /// Return the configured bearer token for `/metrics`, if enabled.
    #[must_use]
    pub fn metrics_bearer_auth_token(&self) -> Option<&str> {
        self.state.metrics_bearer_auth_token()
    }

    /// Return configured metrics IP allowlist entries, if any.
    #[must_use]
    pub fn metrics_ip_allowlist(&self) -> Option<Vec<IpAddr>> {
        self.state.metrics_ip_allowlist()
    }

    /// Return configured CIDR-based metrics IP allowlist entries, if any.
    #[must_use]
    pub fn metrics_ip_cidr_allowlist(&self) -> Option<Vec<IpCidr>> {
        self.state.metrics_ip_cidr_allowlist()
    }

    /// Return configured trusted proxy CIDRs for `/metrics`, if any.
    #[must_use]
    pub fn metrics_trusted_proxies(&self) -> Option<Vec<IpCidr>> {
        self.state.metrics_trusted_proxies()
    }

    /// Return configured max accepted forwarding header lengths for `/metrics`.
    #[must_use]
    pub const fn metrics_header_limits(&self) -> MetricsHeaderLimits {
        self.state.metrics_header_limits()
    }

    /// Build the axum [`Router`] without starting the server.
    ///
    /// Useful for testing or embedding in a larger application.
    pub fn build(self) -> Router {
        let misconfigured_auth_message = self.api_auth_error();
        let auth_bindings = self
            .api_bearer_bindings()
            .into_iter()
            .map(|binding| {
                BearerAuthBinding::new(
                    binding.token,
                    binding.bound_tenant_id,
                    binding.bound_actor_id,
                )
            })
            .collect::<Vec<_>>();
        let auth_config = if auth_bindings.is_empty() { None } else { Some(auth_bindings) };
        let trust_actor_headers_for_authz = self.trust_actor_headers_for_authz;
        let authz_strict = self.authz_strict;
        let authz_config = self.authz_config.map(|config| {
            let config = if trust_actor_headers_for_authz {
                config.with_trusted_actor_headers()
            } else {
                config
            };
            if authz_strict { config.with_strict_path_mapping() } else { config }
        });
        // Durable, database-backed idempotency store (with the in-memory map as
        // a read-through cache) plus the required-key gate for money-moving
        // create endpoints.
        let mut idempotency_layer = crate::idempotency::IdempotencyLayer::new()
            .with_required_keys(self.require_idempotency_keys);
        if let Ok(commerce) = self.state.commerce_for_tenant(None) {
            idempotency_layer = idempotency_layer.with_durable_store(commerce);
        }
        let router =
            routes::api_router_with_idempotency(self.max_request_body_bytes, idempotency_layer)
                .with_state(self.state);
        middleware::apply_middleware(
            router,
            self.enable_cors,
            self.enable_request_id,
            auth_config,
            authz_config,
            misconfigured_auth_message,
            self.rate_limit,
        )
    }

    /// Build the router and start serving HTTP requests.
    ///
    /// This method will block until the server is shut down.
    pub async fn serve(self) -> Result<(), HttpError> {
        let token = self.api_bearer_token.clone();
        let api_token_count = self.api_bearer_bindings().len();
        let metrics_token = self.state.metrics_bearer_auth_token().map(ToOwned::to_owned);
        let trusted_proxy_count = self.state.metrics_trusted_proxies().map_or(0, |v| v.len());
        let metrics_header_limits = self.state.metrics_header_limits();
        let bound_tenant_id = self.bound_tenant_id.clone();
        let bound_actor_id = self.bound_actor_id.clone();
        let generated_default_token = self.generated_default_token;
        let authz_enabled = self.authz_config.is_some();
        let rate_limit_enabled = self.rate_limit.is_some();
        let trust_actor_headers_for_authz = self.trust_actor_headers_for_authz;
        let addr = self.addr;

        if api_token_count == 0 && !self.allow_unauthenticated {
            if addr.ip().is_loopback() {
                tracing::warn!(
                    "No API authentication configured on a loopback bind. Configure a bearer \
                     token with ServerBuilder::with_bearer_auth before exposing this server."
                );
            } else {
                return Err(HttpError::BadRequest(format!(
                    "Refusing to start without API authentication on non-loopback address \
                     {addr}. Configure a bearer token with ServerBuilder::with_bearer_auth (or \
                     add_bearer_auth_for_actor/add_bearer_auth_for_tenant), or explicitly opt \
                     out with ServerBuilder::allow_unauthenticated() or \
                     {ALLOW_UNAUTHENTICATED_ENV}=true."
                )));
            }
        }

        if let Some(message) = self.api_auth_error() {
            return Err(HttpError::BadRequest(format!("Refusing to start: {message}")));
        }

        let app = self.build();

        tracing::info!("StateSet HTTP listening on {addr}");
        if let Some(token) = token.as_deref() {
            tracing::info!("API bearer authentication is enabled for /api/v1/*");
            if api_token_count > 1 {
                tracing::info!(api_token_count, "Multiple API bearer tokens are configured");
            }
            if let Some(bound_tenant_id) = bound_tenant_id.as_deref() {
                tracing::info!(
                    tenant_id = %bound_tenant_id,
                    "API token is bound to a specific tenant"
                );
            }
            if let Some(bound_actor_id) = bound_actor_id.as_deref() {
                tracing::info!(
                    actor_id = %bound_actor_id,
                    "API token is bound to a specific actor"
                );
            }
            if generated_default_token {
                tracing::warn!(
                    "Using generated bearer token. Persist it and rotate for production deployments."
                );
                let preview: String = token.chars().take(8).collect();
                tracing::info!(
                    token_preview = %preview,
                    token_length = token.len(),
                    "Generated API bearer token (redacted preview)"
                );
            }
        } else if api_token_count > 0 {
            tracing::info!("API bearer authentication is enabled for /api/v1/*");
            tracing::info!(api_token_count, "Multiple API bearer tokens are configured");
        } else {
            tracing::warn!("API authentication is disabled for /api/v1/*");
        }
        if authz_enabled && bound_actor_id.is_none() && trust_actor_headers_for_authz {
            tracing::warn!(
                "Request authorization is enabled for /api/v1/*; ensure x-actor-id is set by a trusted upstream"
            );
        }
        if !addr.ip().is_loopback() {
            // Production baseline: authentication alone leaves every valid
            // token with full API access, and no throttle at all.
            if !authz_enabled {
                tracing::warn!(
                    "No authorization (RBAC) configured on a non-loopback bind: any valid \
                     bearer token has full access to every /api/v1 route. Configure \
                     ServerBuilder::with_authz for production deployments."
                );
            }
            if !rate_limit_enabled {
                tracing::warn!(
                    "No rate limiting configured on a non-loopback bind. Configure \
                     ServerBuilder::with_rate_limit for production deployments."
                );
            }
        }

        if metrics_token.is_some() {
            tracing::info!("Metrics bearer authentication is enabled for /metrics");
        } else if !addr.ip().is_loopback() {
            tracing::warn!(
                "Metrics authentication is disabled for /metrics on a non-loopback bind"
            );
        }
        if trusted_proxy_count > 0 {
            tracing::info!(
                trusted_proxy_count,
                "Metrics forwarded headers are trusted only for configured proxy CIDRs"
            );
        }
        tracing::info!(
            forwarded_header_max_bytes = metrics_header_limits.forwarded_header_value_bytes(),
            x_forwarded_for_header_max_bytes =
                metrics_header_limits.x_forwarded_for_header_value_bytes(),
            x_real_ip_header_max_bytes = metrics_header_limits.x_real_ip_header_value_bytes(),
            authorization_header_max_bytes =
                metrics_header_limits.authorization_header_value_bytes(),
            "Metrics header byte limits configured"
        );

        let listener = tokio::net::TcpListener::bind(addr)
            .await
            .map_err(|e| HttpError::InternalError(format!("Failed to bind: {e}")))?;

        axum::serve(listener, app.into_make_service_with_connect_info::<SocketAddr>())
            .await
            .map_err(|e| HttpError::InternalError(format!("Server error: {e}")))?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use stateset_authz::{AuthzEngineBuilder, Role};
    use tower::ServiceExt;
    use uuid::Uuid;

    fn test_commerce() -> Commerce {
        Commerce::new(":memory:").expect("in-memory Commerce")
    }

    #[test]
    fn builder_default_addr() {
        let builder = ServerBuilder::new(test_commerce());
        assert_eq!(builder.addr, SocketAddr::from(DEFAULT_ADDR));
        assert!(!builder.enable_cors);
        assert!(!builder.enable_request_id);
        assert!(builder.api_bearer_token.is_some());
        assert!(builder.metrics_bearer_auth_token().is_some());
        assert_eq!(builder.metrics_header_limits(), MetricsHeaderLimits::default());
        assert!(builder.bound_tenant_id.is_none());
        assert!(builder.bound_actor_id.is_none());
        assert!(builder.additional_api_bearer_bindings.is_empty());
        assert_eq!(builder.max_request_body_bytes, DEFAULT_REQUEST_BODY_LIMIT_BYTES);
        assert!(builder.authz_config.is_none());
        assert!(!builder.trust_actor_headers_for_authz);
    }

    #[test]
    fn builder_with_bind() {
        let addr: SocketAddr = "0.0.0.0:8080".parse().unwrap();
        let builder = ServerBuilder::new(test_commerce()).bind(addr);
        assert_eq!(builder.addr, addr);
    }

    #[test]
    fn builder_with_cors() {
        let builder = ServerBuilder::new(test_commerce()).with_cors();
        assert!(builder.enable_cors);
    }

    #[test]
    fn builder_with_request_id() {
        let builder = ServerBuilder::new(test_commerce()).with_request_id();
        assert!(builder.enable_request_id);
    }

    #[test]
    fn builder_with_bearer_auth() {
        let builder = ServerBuilder::new(test_commerce());
        let default_metrics_token = builder
            .metrics_bearer_auth_token()
            .expect("builder should start with metrics auth")
            .to_string();
        let builder = builder.with_bearer_auth("test-token");
        assert_eq!(builder.bearer_auth_token(), Some("test-token"));
        assert_eq!(builder.metrics_bearer_auth_token(), Some(default_metrics_token.as_str()));
        assert!(builder.bound_tenant_id.is_none());
        assert!(builder.bound_actor_id.is_none());
    }

    #[test]
    fn builder_with_metrics_bearer_auth() {
        let builder = ServerBuilder::new(test_commerce()).with_metrics_bearer_auth("metrics-token");
        assert_eq!(builder.metrics_bearer_auth_token(), Some("metrics-token"));
    }

    #[test]
    fn builder_with_bearer_auth_keeps_explicit_metrics_token() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_bearer_auth("metrics-token")
            .with_bearer_auth("api-token");
        assert_eq!(builder.bearer_auth_token(), Some("api-token"));
        assert_eq!(builder.metrics_bearer_auth_token(), Some("metrics-token"));
    }

    #[test]
    fn builder_with_metrics_ip_allowlist() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_ip_allowlist(["127.0.0.1".parse().unwrap(), "10.0.0.1".parse().unwrap()]);

        let allowlist = builder.metrics_ip_allowlist().unwrap();
        assert_eq!(allowlist.len(), 2);
    }

    #[test]
    fn parse_ip_allowlist_csv_parses_and_sorts_unique_values() {
        let parsed =
            parse_ip_allowlist_csv(METRICS_IP_ALLOWLIST_ENV, "127.0.0.1, 10.0.0.1,127.0.0.1")
                .unwrap();
        assert_eq!(
            parsed,
            vec!["10.0.0.1".parse::<IpAddr>().unwrap(), "127.0.0.1".parse::<IpAddr>().unwrap()]
        );
    }

    #[test]
    fn parse_ip_cidr_allowlist_csv_rejects_invalid_entries() {
        let err = parse_ip_cidr_allowlist_csv(METRICS_IP_CIDR_ALLOWLIST_ENV, "10.0.0.0/8,bogus")
            .expect_err("should reject invalid CIDR");
        assert!(err.to_string().contains(METRICS_IP_CIDR_ALLOWLIST_ENV));
    }

    #[test]
    fn builder_with_metrics_ip_cidr_allowlist() {
        let builder = ServerBuilder::new(test_commerce()).with_metrics_ip_cidr_allowlist([
            "10.0.0.0/8".parse().unwrap(),
            "127.0.0.1".parse().unwrap(),
        ]);

        let cidrs = builder.metrics_ip_cidr_allowlist().unwrap();
        assert_eq!(cidrs, vec!["10.0.0.0/8".parse().unwrap(), "127.0.0.1/32".parse().unwrap()]);
    }

    #[test]
    fn builder_with_metrics_trusted_proxies() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_trusted_proxies(["10.0.0.0/8".parse().unwrap()]);
        let proxies = builder.metrics_trusted_proxies().unwrap();
        assert_eq!(proxies, vec!["10.0.0.0/8".parse().unwrap()]);
    }

    #[test]
    fn builder_with_metrics_header_limits() {
        let limits = MetricsHeaderLimits::new(1024, 1536, 256).unwrap();
        let builder = ServerBuilder::new(test_commerce()).with_metrics_header_limits(limits);
        assert_eq!(builder.metrics_header_limits(), limits);
    }

    #[test]
    fn builder_with_metrics_network_policy_from_env() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_network_policy_from_values(
                Some("127.0.0.1"),
                Some("10.0.0.0/8"),
                Some("10.1.0.0/16"),
            )
            .expect("policy should parse");

        assert_eq!(builder.metrics_ip_allowlist(), Some(vec!["127.0.0.1".parse().unwrap()]));
        assert_eq!(builder.metrics_ip_cidr_allowlist(), Some(vec!["10.0.0.0/8".parse().unwrap()]));
        assert_eq!(builder.metrics_trusted_proxies(), Some(vec!["10.1.0.0/16".parse().unwrap()]));
    }

    #[test]
    fn builder_with_metrics_network_policy_from_env_can_disable_lists_with_empty_values() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_ip_allowlist(["127.0.0.1".parse().unwrap()])
            .with_metrics_ip_cidr_allowlist(["10.0.0.0/8".parse().unwrap()])
            .with_metrics_trusted_proxies(["10.1.0.0/16".parse().unwrap()])
            .with_metrics_network_policy_from_values(Some("   "), Some(","), Some("  ,  "))
            .expect("empty policy values should disable lists");

        assert!(builder.metrics_ip_allowlist().is_none());
        assert!(builder.metrics_ip_cidr_allowlist().is_none());
        assert!(builder.metrics_trusted_proxies().is_none());
    }

    #[test]
    fn parse_positive_usize_env_rejects_zero_and_empty_values() {
        let zero =
            parse_positive_usize_env(METRICS_X_REAL_IP_HEADER_MAX_BYTES_ENV, "0").unwrap_err();
        assert!(zero.to_string().contains("greater than zero"));

        let empty =
            parse_positive_usize_env(METRICS_X_REAL_IP_HEADER_MAX_BYTES_ENV, "   ").unwrap_err();
        assert!(empty.to_string().contains("positive integer"));
    }

    #[test]
    fn builder_with_metrics_header_limits_from_values() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_header_limits_from_values(
                Some("1024"),
                Some("1536"),
                Some("256"),
                Some("768"),
            )
            .expect("limits should parse");
        let limits = builder.metrics_header_limits();
        assert_eq!(limits.forwarded_header_value_bytes(), 1024);
        assert_eq!(limits.x_forwarded_for_header_value_bytes(), 1536);
        assert_eq!(limits.x_real_ip_header_value_bytes(), 256);
        assert_eq!(limits.authorization_header_value_bytes(), 768);
    }

    #[test]
    fn builder_with_metrics_header_limits_from_values_applies_partial_overrides() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_header_limits_from_values(None, Some("4096"), None, None)
            .expect("partial limits should parse");
        let limits = builder.metrics_header_limits();
        assert_eq!(
            limits.forwarded_header_value_bytes(),
            MetricsHeaderLimits::DEFAULT_FORWARDED_HEADER_VALUE_BYTES
        );
        assert_eq!(limits.x_forwarded_for_header_value_bytes(), 4096);
        assert_eq!(
            limits.x_real_ip_header_value_bytes(),
            MetricsHeaderLimits::DEFAULT_X_REAL_IP_HEADER_VALUE_BYTES
        );
        assert_eq!(
            limits.authorization_header_value_bytes(),
            MetricsHeaderLimits::DEFAULT_AUTHORIZATION_HEADER_VALUE_BYTES
        );
    }

    #[test]
    fn builder_with_request_body_limit() {
        let builder = ServerBuilder::new(test_commerce()).with_max_request_body_bytes(4096);
        assert_eq!(builder.max_request_body_bytes, 4096);
    }

    #[test]
    fn builder_with_request_body_limit_from_value() {
        let builder = ServerBuilder::new(test_commerce())
            .with_request_body_limit_from_value(Some("8192"))
            .expect("request body limit should parse");
        assert_eq!(builder.max_request_body_bytes, 8192);
    }

    #[test]
    fn builder_with_authz_engine() {
        let engine = AuthzEngineBuilder::new()
            .add_role(Role::viewer())
            .assign_role("viewer-1", "viewer")
            .build();
        let builder = ServerBuilder::new(test_commerce()).with_authz_engine(engine);
        assert!(builder.authz_config.is_some());
    }

    #[test]
    fn builder_trusts_actor_headers_for_authz() {
        let builder = ServerBuilder::new(test_commerce()).trust_actor_headers_for_authz();
        assert!(builder.trust_actor_headers_for_authz);
    }

    #[test]
    fn builder_with_bearer_auth_for_actor() {
        let builder = ServerBuilder::new(test_commerce())
            .with_bearer_auth_for_actor("actor-token", "admin-1");
        assert_eq!(builder.bearer_auth_token(), Some("actor-token"));
        assert_eq!(builder.bound_actor_id.as_deref(), Some("admin-1"));
    }

    #[test]
    fn builder_adds_additional_actor_bound_token() {
        let builder = ServerBuilder::new(test_commerce())
            .without_auth()
            .add_bearer_auth_for_actor("viewer-token", "viewer-1");
        assert!(builder.bearer_auth_token().is_none());
        assert_eq!(builder.additional_api_bearer_bindings.len(), 1);
        assert_eq!(builder.additional_api_bearer_bindings[0].token, "viewer-token");
        assert_eq!(
            builder.additional_api_bearer_bindings[0].bound_actor_id.as_deref(),
            Some("viewer-1")
        );
    }

    #[test]
    fn builder_with_bearer_auth_for_tenant() {
        let builder = ServerBuilder::new(test_commerce())
            .with_bearer_auth_for_tenant("tenant-token", "tenant-1");
        assert_eq!(builder.bearer_auth_token(), Some("tenant-token"));
        assert_eq!(builder.bound_tenant_id.as_deref(), Some("tenant-1"));
    }

    #[test]
    fn builder_without_auth() {
        let builder = ServerBuilder::new(test_commerce())
            .with_bearer_auth_for_tenant("token", "tenant-1")
            .bind_auth_actor("admin-1")
            .without_auth();
        assert!(builder.bearer_auth_token().is_none());
        assert!(builder.metrics_bearer_auth_token().is_some());
        assert!(builder.bound_tenant_id.is_none());
        assert!(builder.bound_actor_id.is_none());
    }

    #[test]
    fn builder_allow_unauthenticated_flag() {
        let builder = ServerBuilder::new(test_commerce());
        assert!(!builder.allow_unauthenticated, "secure default must be false");
        let builder = builder.allow_unauthenticated();
        assert!(builder.allow_unauthenticated);
    }

    #[test]
    fn builder_require_idempotency_keys_flag() {
        let builder = ServerBuilder::new(test_commerce());
        assert!(builder.require_idempotency_keys, "secure default must require keys");
        let builder = builder.require_idempotency_keys(false);
        assert!(!builder.require_idempotency_keys);
    }

    #[test]
    fn builder_require_idempotency_keys_from_value() {
        let builder = ServerBuilder::new(test_commerce())
            .with_require_idempotency_keys_from_value(Some("false"))
            .expect("false should parse");
        assert!(!builder.require_idempotency_keys);

        let builder = ServerBuilder::new(test_commerce())
            .with_require_idempotency_keys_from_value(None)
            .expect("unset keeps default");
        assert!(builder.require_idempotency_keys, "unset must keep the secure default");

        assert!(
            ServerBuilder::new(test_commerce())
                .with_require_idempotency_keys_from_value(Some("banana"))
                .is_err(),
            "invalid values must be rejected"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn built_server_returns_428_on_keyless_payment_create_by_default() {
        use tower::ServiceExt as _;
        let app = ServerBuilder::new(test_commerce()).without_auth().build();
        let response = app
            .oneshot(
                axum::http::Request::post("/api/v1/payments")
                    .header("content-type", "application/json")
                    .body(axum::body::Body::from("{}"))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), axum::http::StatusCode::PRECONDITION_REQUIRED);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn built_server_allows_keyless_payment_create_when_opted_out() {
        use tower::ServiceExt as _;
        let app = ServerBuilder::new(test_commerce())
            .without_auth()
            .require_idempotency_keys(false)
            .build();
        let response = app
            .oneshot(
                axum::http::Request::post("/api/v1/payments")
                    .header("content-type", "application/json")
                    .body(axum::body::Body::from("{}"))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_ne!(
            response.status(),
            axum::http::StatusCode::PRECONDITION_REQUIRED,
            "opt-out must disable the 428 gate"
        );
    }

    #[test]
    fn builder_strict_authz_flag() {
        let builder = ServerBuilder::new(test_commerce());
        assert!(!builder.authz_strict, "default must preserve existing behavior");
        let builder = builder.with_strict_authz();
        assert!(builder.authz_strict);
    }

    #[test]
    fn parse_bool_env_accepts_expected_values() {
        for raw in ["1", "true", "TRUE", "yes"] {
            assert!(matches!(parse_bool_env("TEST_ENV", raw), Ok(true)), "raw={raw}");
        }
        for raw in ["0", "false", "no", "", "  "] {
            assert!(matches!(parse_bool_env("TEST_ENV", raw), Ok(false)), "raw={raw}");
        }
        assert!(parse_bool_env("TEST_ENV", "maybe").is_err());
    }

    #[tokio::test]
    async fn serve_refuses_non_loopback_bind_without_auth() {
        let err = ServerBuilder::new(test_commerce())
            .without_auth()
            .bind("192.0.2.1:0".parse().expect("socket addr"))
            .serve()
            .await
            .expect_err("must refuse to start without auth on a non-loopback bind");
        match err {
            HttpError::BadRequest(message) => {
                assert!(message.contains("Refusing to start"), "message: {message}");
                assert!(message.contains("allow_unauthenticated"), "message: {message}");
                assert!(message.contains("STATESET_HTTP_ALLOW_UNAUTHENTICATED"));
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[tokio::test]
    async fn allow_unauthenticated_bypasses_non_loopback_refusal() {
        // 192.0.2.1 (TEST-NET-1) is not locally assigned, so binding fails —
        // reaching the bind step proves the fail-closed check was opted out of.
        let err = ServerBuilder::new(test_commerce())
            .without_auth()
            .allow_unauthenticated()
            .bind("192.0.2.1:0".parse().expect("socket addr"))
            .serve()
            .await
            .expect_err("bind to TEST-NET-1 must fail");
        match err {
            HttpError::InternalError(message) => {
                assert!(message.contains("Failed to bind"), "message: {message}");
            }
            other => panic!("expected bind failure, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn serve_allows_loopback_bind_without_auth() {
        let serve_future = ServerBuilder::new(test_commerce())
            .without_auth()
            .bind("127.0.0.1:0".parse().expect("socket addr"))
            .serve();
        // The server must start (and then block serving); a quick return would
        // be a startup refusal.
        let result =
            tokio::time::timeout(std::time::Duration::from_millis(500), serve_future).await;
        assert!(result.is_err(), "loopback bind without auth must start and keep serving");
    }

    #[test]
    fn builder_without_metrics_auth() {
        let builder = ServerBuilder::new(test_commerce()).without_metrics_auth();
        assert!(builder.bearer_auth_token().is_some());
        assert!(builder.metrics_bearer_auth_token().is_none());
    }

    #[test]
    fn builder_without_metrics_ip_allowlist() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_ip_allowlist(["127.0.0.1".parse().unwrap()])
            .without_metrics_ip_allowlist();
        assert!(builder.metrics_ip_allowlist().is_none());
    }

    #[test]
    fn builder_without_metrics_ip_cidr_allowlist() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_ip_cidr_allowlist(["10.0.0.0/8".parse().unwrap()])
            .without_metrics_ip_cidr_allowlist();
        assert!(builder.metrics_ip_cidr_allowlist().is_none());
    }

    #[test]
    fn builder_without_metrics_trusted_proxies() {
        let builder = ServerBuilder::new(test_commerce())
            .with_metrics_trusted_proxies(["10.0.0.0/8".parse().unwrap()])
            .without_metrics_trusted_proxies();
        assert!(builder.metrics_trusted_proxies().is_none());
    }

    #[test]
    fn builder_chaining() {
        let addr: SocketAddr = "0.0.0.0:9090".parse().unwrap();
        let builder = ServerBuilder::new(test_commerce())
            .bind(addr)
            .with_cors()
            .with_request_id()
            .with_bearer_auth("chain-token")
            .bind_auth_tenant("chain-tenant")
            .bind_auth_actor("chain-actor");
        assert_eq!(builder.addr, addr);
        assert!(builder.enable_cors);
        assert!(builder.enable_request_id);
        assert_eq!(builder.bearer_auth_token(), Some("chain-token"));
        assert_eq!(builder.bound_tenant_id.as_deref(), Some("chain-tenant"));
        assert_eq!(builder.bound_actor_id.as_deref(), Some("chain-actor"));
    }

    #[test]
    fn builder_with_max_tenant_dbs() {
        let tenant_dir =
            std::env::temp_dir().join(format!("stateset-http-builder-{}", Uuid::new_v4()));
        let builder = ServerBuilder::new(test_commerce())
            .with_tenant_db_dir(tenant_dir.clone())
            .with_max_tenant_dbs(1);

        let tenant_a = builder.state.commerce_for_tenant(Some("tenant-a")).unwrap();
        let second_while_in_use = builder.state.commerce_for_tenant(Some("tenant-b"));
        assert!(matches!(second_while_in_use, Err(HttpError::TooManyRequests(_))));

        drop(tenant_a);
        let second_after_release = builder.state.commerce_for_tenant(Some("tenant-b"));
        assert!(second_after_release.is_ok());

        let _ = std::fs::remove_dir_all(tenant_dir);
    }

    #[test]
    fn builder_builds_router() {
        let _router = ServerBuilder::new(test_commerce()).build();
    }

    #[tokio::test]
    async fn built_router_fails_closed_for_unbound_tenant_routing() {
        let tenant_dir =
            std::env::temp_dir().join(format!("stateset-http-misconfig-{}", Uuid::new_v4()));
        let builder = ServerBuilder::new(test_commerce()).with_tenant_db_dir(tenant_dir.clone());
        let token =
            builder.bearer_auth_token().expect("default auth token should be present").to_string();
        let router = builder.build();

        let resp = router
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", format!("Bearer {token}"))
                    .header("x-tenant-id", "tenant-1")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let _ = std::fs::remove_dir_all(tenant_dir);
    }

    #[tokio::test]
    async fn built_router_fails_closed_for_invalid_bound_actor() {
        let builder = ServerBuilder::new(test_commerce()).bind_auth_actor("invalid actor");
        let token =
            builder.bearer_auth_token().expect("default auth token should be present").to_string();
        let router = builder.build();

        let resp = router
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", format!("Bearer {token}"))
                    .header("x-tenant-id", "tenant-1")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[tokio::test]
    async fn built_router_fails_closed_for_actor_binding_without_auth() {
        let router =
            ServerBuilder::new(test_commerce()).without_auth().bind_auth_actor("admin-1").build();

        let resp = router
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("x-tenant-id", "tenant-1")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[tokio::test]
    async fn built_router_fails_closed_for_duplicate_api_tokens() {
        let builder = ServerBuilder::new(test_commerce())
            .with_bearer_auth("duplicate-token")
            .add_bearer_auth_for_actor("duplicate-token", "viewer-1");
        let router = builder.build();

        let resp = router
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", "Bearer duplicate-token")
                    .header("x-tenant-id", "tenant-1")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[tokio::test]
    async fn built_router_fails_closed_for_authz_without_actor_bound_tokens_or_trusted_headers() {
        let engine = AuthzEngineBuilder::new()
            .add_role(Role::viewer())
            .assign_role("viewer-1", "viewer")
            .build();
        let builder = ServerBuilder::new(test_commerce()).with_authz_engine(engine);
        let token =
            builder.bearer_auth_token().expect("default auth token should be present").to_string();
        let router = builder.build();

        let resp = router
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", format!("Bearer {token}"))
                    .header("x-tenant-id", "tenant-1")
                    .header("x-actor-id", "viewer-1")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[tokio::test]
    async fn built_router_fails_closed_for_tenant_routing_with_unbound_additional_token() {
        let tenant_dir =
            std::env::temp_dir().join(format!("stateset-http-extra-token-{}", Uuid::new_v4()));
        let builder = ServerBuilder::new(test_commerce())
            .with_bearer_auth_for_tenant("primary-token", "tenant-1")
            .add_bearer_auth_for_actor("viewer-token", "viewer-1")
            .with_tenant_db_dir(tenant_dir.clone());
        let router = builder.build();

        let resp = router
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", "Bearer viewer-token")
                    .header("x-tenant-id", "tenant-1")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);

        let _ = std::fs::remove_dir_all(tenant_dir);
    }

    #[tokio::test]
    async fn built_router_serves_health() {
        let router = ServerBuilder::new(test_commerce()).with_cors().with_request_id().build();

        let resp =
            router.oneshot(Request::get("/health").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn built_router_serves_api_orders() {
        let router = ServerBuilder::new(test_commerce()).without_auth().build();

        let resp = router
            .oneshot(Request::get("/api/v1/orders").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn built_router_serves_api_customers() {
        let router = ServerBuilder::new(test_commerce()).without_auth().build();

        let resp = router
            .oneshot(Request::get("/api/v1/customers").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn built_router_serves_api_products() {
        let router = ServerBuilder::new(test_commerce()).without_auth().build();

        let resp = router
            .oneshot(Request::get("/api/v1/products").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn built_router_404_for_unknown_path() {
        let router = ServerBuilder::new(test_commerce()).without_auth().build();

        let resp = router
            .oneshot(Request::get("/api/v1/nonexistent").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[test]
    fn builder_debug_impl() {
        let builder = ServerBuilder::new(test_commerce());
        let dbg = format!("{builder:?}");
        assert!(dbg.contains("ServerBuilder"));
        assert!(dbg.contains("<redacted>"));
    }

    #[tokio::test]
    async fn built_router_blocks_api_without_token_by_default() {
        let router = ServerBuilder::new(test_commerce()).build();

        let resp = router
            .oneshot(Request::get("/api/v1/orders").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn built_router_allows_api_with_token() {
        // Sends `x-tenant-id` without a tenant DB dir; opt into the explicit
        // escape hatch now that silent fallthrough is rejected.
        let builder = ServerBuilder::new(test_commerce()).with_ignore_tenant_header();
        let token =
            builder.bearer_auth_token().expect("default auth token should be present").to_string();
        let router = builder.build();

        let resp = router
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", format!("Bearer {token}"))
                    .header("x-tenant-id", "tenant-1")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn built_router_blocks_metrics_without_token_by_default() {
        let router = ServerBuilder::new(test_commerce()).build();

        let resp =
            router.oneshot(Request::get("/metrics").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn built_router_allows_metrics_with_token() {
        let builder = ServerBuilder::new(test_commerce());
        let token = builder
            .metrics_bearer_auth_token()
            .expect("default metrics token should be present")
            .to_string();
        let router = builder.build();

        let resp = router
            .oneshot(
                Request::get("/metrics")
                    .header("authorization", format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn built_router_allows_metrics_without_token_when_disabled() {
        let router = ServerBuilder::new(test_commerce()).without_metrics_auth().build();

        let resp =
            router.oneshot(Request::get("/metrics").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn built_router_keeps_metrics_protected_after_without_auth() {
        let router = ServerBuilder::new(test_commerce()).without_auth().build();

        let resp =
            router.oneshot(Request::get("/metrics").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn built_router_rejects_mismatched_tenant_for_bound_token() {
        let router = ServerBuilder::new(test_commerce())
            .with_bearer_auth_for_tenant("bound-token", "tenant-1")
            .build();

        let resp = router
            .oneshot(
                Request::get("/api/v1/orders")
                    .header("authorization", "Bearer bound-token")
                    .header("x-tenant-id", "tenant-2")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
    }

    #[tokio::test]
    async fn serve_refuses_public_bind_without_auth() {
        let addr: SocketAddr = "0.0.0.0:0".parse().unwrap();
        let err = ServerBuilder::new(test_commerce())
            .bind(addr)
            .without_auth()
            .serve()
            .await
            .expect_err("should reject public bind without auth");

        assert!(err.to_string().contains("Refusing to start without API auth"));
    }

    #[tokio::test]
    async fn serve_refuses_tenant_routing_without_tenant_binding() {
        let tenant_dir =
            std::env::temp_dir().join(format!("stateset-http-serve-misconfig-{}", Uuid::new_v4()));
        let err = ServerBuilder::new(test_commerce())
            .with_tenant_db_dir(tenant_dir.clone())
            .serve()
            .await
            .expect_err("should reject unbound tenant routing");

        assert!(err.to_string().contains("binding every API bearer token"));
        let _ = std::fs::remove_dir_all(tenant_dir);
    }

    #[tokio::test]
    async fn serve_refuses_tenant_routing_without_auth() {
        let tenant_dir =
            std::env::temp_dir().join(format!("stateset-http-serve-no-auth-{}", Uuid::new_v4()));
        let err = ServerBuilder::new(test_commerce())
            .with_tenant_db_dir(tenant_dir.clone())
            .without_auth()
            .serve()
            .await
            .expect_err("should reject tenant routing without auth");

        assert!(err.to_string().contains("requires API auth"));
        let _ = std::fs::remove_dir_all(tenant_dir);
    }

    #[tokio::test]
    async fn serve_refuses_authz_without_actor_bound_tokens_or_trusted_headers() {
        let engine = AuthzEngineBuilder::new()
            .add_role(Role::viewer())
            .assign_role("viewer-1", "viewer")
            .build();
        let err = ServerBuilder::new(test_commerce())
            .with_authz_engine(engine)
            .serve()
            .await
            .expect_err("should reject authz configuration without actor identity source");

        assert!(err.to_string().contains("binding every API bearer token"));
    }
}