apcore-cli 0.12.0

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

use std::path::{Path, PathBuf};
use std::process::{Command, Output};

use apcore::acl::{ACLRule, ApprovalRequirement, ACL};
use apcore::module::Module;
use apcore::{Config, Executor, ModuleAnnotations, Registry};
use apcore_cli::acl_cmd::{
    check_exit_code, status_exit_code, validate_exit_code, warn_if_strategy_bypasses_acl,
};
use apcore_cli::acl_loader::{
    acl_audit_enabled, acl_audit_include_denied, install_acl_audit_logger, load_cli_acl,
    load_cli_acl_with_audit, resolve_acl_file, resolve_acl_root, ACL_AUDIT_FIELDS, ACL_ROOT_ENV,
};
use apcore_cli::{AuditLogger, ConfigResolver};
use serde_json::{json, Value};

// ---------------------------------------------------------------------------
// Harness
// ---------------------------------------------------------------------------

/// A scratch project: an `extensions/` directory (the binary refuses to start
/// without one) plus whatever ACL fixture the test writes.
struct Project {
    dir: tempfile::TempDir,
}

impl Project {
    fn new() -> Self {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::create_dir(dir.path().join("extensions")).expect("mkdir extensions");
        Self { dir }
    }

    fn path(&self) -> &Path {
        self.dir.path()
    }

    fn write(&self, rel: &str, contents: &str) -> PathBuf {
        let path = self.dir.path().join(rel);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).expect("mkdir -p");
        }
        std::fs::write(&path, contents).expect("write fixture");
        path
    }

    /// Run the CLI with the project directory as the working directory, so
    /// tier-4 `./acl` resolution and `apcore.yaml` discovery behave as they
    /// would for a real user.
    fn run(&self, args: &[&str]) -> Output {
        self.run_with_env(args, &[])
    }

    fn run_with_env(&self, args: &[&str], env: &[(&str, &str)]) -> Output {
        let mut cmd = Command::new(env!("CARGO_BIN_EXE_apcore-cli"));
        cmd.current_dir(self.dir.path())
            .args(args)
            // Keep the shared audit log out of the way of a sandboxed run.
            .env("APCORE_CLI_AUDIT_DISABLE", "1")
            .env_remove(ACL_ROOT_ENV);
        for (k, v) in env {
            cmd.env(k, v);
        }
        cmd.output().expect("failed to spawn apcore-cli")
    }
}

fn stdout(out: &Output) -> String {
    String::from_utf8_lossy(&out.stdout).to_string()
}

fn stderr(out: &Output) -> String {
    String::from_utf8_lossy(&out.stderr).to_string()
}

fn json_stdout(out: &Output) -> Value {
    serde_json::from_str(&stdout(out))
        .unwrap_or_else(|e| panic!("stdout is not JSON ({e}):\n{}", stdout(out)))
}

const THREE_RULE_ACL: &str = "\
default_effect: deny
rules:
  - callers: ['@external']
    targets: ['system.control.*']
    effect: deny
    description: no external control
  - callers: ['*']
    targets: ['db.migrate']
    effect: allow
    approval: required
    description: migrations need a human
    conditions:
      roles: ['admin']
  - callers: ['*']
    targets: ['db.read']
    effect: allow
";

// ---------------------------------------------------------------------------
// T-ACL-01 / T-ACL-02 / T-ACL-05 -- attachment
// ---------------------------------------------------------------------------

#[test]
fn t_acl_01_no_acl_directory_changes_nothing() {
    let project = Project::new();
    let out = project.run(&["apcli", "acl", "status", "--format", "json"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    let payload = json_stdout(&out);
    assert_eq!(payload["acl_configured"], false);
    assert_eq!(payload["acl_source"], Value::Null);

    // And an ordinary command is unaffected.
    let listed = project.run(&["apcli", "list"]);
    assert_eq!(listed.status.code(), Some(0), "stderr: {}", stderr(&listed));
}

#[test]
fn t_acl_02_global_acl_in_cwd_is_attached() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&["apcli", "acl", "status", "--format", "json"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    let payload = json_stdout(&out);
    assert_eq!(payload["acl_configured"], true);
    assert!(payload["acl_source"]
        .as_str()
        .expect("acl_source is reported")
        .ends_with("global_acl.yaml"));
}

#[test]
fn t_acl_05_directory_without_global_acl_attaches_nothing() {
    let project = Project::new();
    std::fs::create_dir(project.path().join("acl")).expect("mkdir acl");
    let out = project.run(&["apcli", "acl", "status", "--format", "json"]);
    assert_eq!(
        out.status.code(),
        Some(0),
        "an absent conventional file is not an error; stderr: {}",
        stderr(&out)
    );
    assert_eq!(json_stdout(&out)["acl_configured"], false);
}

// ---------------------------------------------------------------------------
// T-ACL-03 / T-ACL-04 -- precedence
// ---------------------------------------------------------------------------

#[test]
fn t_acl_03_flag_beats_acl_root_in_yaml() {
    let project = Project::new();
    project.write("apcore.yaml", "acl:\n  root: ./from-yaml\n");
    project.write(
        "from-yaml/global_acl.yaml",
        "default_effect: allow\nrules: []\n",
    );
    project.write("custom.yaml", THREE_RULE_ACL);

    let out = project.run(&[
        "--acl",
        "./custom.yaml",
        "apcli",
        "acl",
        "list",
        "--format",
        "json",
    ]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    let payload = json_stdout(&out);
    assert_eq!(payload["default_effect"], "deny", "tier 1 must win");
    assert_eq!(payload["rules"].as_array().map(Vec::len), Some(3));
}

#[test]
fn t_acl_04_env_beats_acl_root_in_yaml() {
    let project = Project::new();
    project.write("apcore.yaml", "acl:\n  root: ./from-yaml\n");
    project.write(
        "from-yaml/global_acl.yaml",
        "default_effect: allow\nrules: []\n",
    );
    project.write("other/global_acl.yaml", THREE_RULE_ACL);

    let out = project.run_with_env(
        &["apcli", "acl", "list", "--format", "json"],
        &[(ACL_ROOT_ENV, "./other")],
    );
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    assert_eq!(
        json_stdout(&out)["default_effect"],
        "deny",
        "tier 2 must win"
    );
}

#[test]
fn tier3_yaml_is_used_when_no_flag_or_env_is_present() {
    let project = Project::new();
    project.write("apcore.yaml", "acl:\n  root: ./from-yaml\n");
    project.write("from-yaml/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&["apcli", "acl", "list", "--format", "json"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    assert_eq!(json_stdout(&out)["rules"].as_array().map(Vec::len), Some(3));
}

#[test]
fn resolve_acl_root_reports_the_tiers_directly() {
    // The precedence chain, exercised without a process so the tier boundary
    // is visible rather than inferred from a rendered document.
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        dir.path().join("apcore.yaml"),
        "acl:\n  root: ./from-yaml\n",
    )
    .expect("write");
    let resolver = ConfigResolver::new(None, Some(dir.path().join("apcore.yaml")));
    assert_eq!(
        resolve_acl_root(&resolver, Some("./flag")),
        Some("./flag".to_string())
    );
    assert_eq!(
        resolve_acl_root(&resolver, None),
        Some("./from-yaml".to_string())
    );
}

// ---------------------------------------------------------------------------
// T-ACL-06 / T-ACL-07 / T-ACL-08 -- structurally invalid files exit 47
// ---------------------------------------------------------------------------

fn assert_invalid_acl_exits_47(fixture: &str, label: &str) {
    let project = Project::new();
    project.write("acl/global_acl.yaml", fixture);
    let out = project.run(&["apcli", "acl", "list"]);
    assert_eq!(
        out.status.code(),
        Some(47),
        "{label} must exit 47 (CONFIG_INVALID), not the generic 1; stderr: {}",
        stderr(&out)
    );
    assert_ne!(
        out.status.code(),
        Some(77),
        "{label} is a configuration fault, never an access decision"
    );
    assert!(
        stderr(&out).contains("Invalid ACL configuration in "),
        "{label} stderr: {}",
        stderr(&out)
    );
}

#[test]
fn t_acl_06_unknown_rule_key_exits_47_and_names_the_rule_index() {
    let project = Project::new();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['*']\n    effect: allow\n    bogus_key: 1\n",
    );
    let out = project.run(&["apcli", "acl", "list"]);
    assert_eq!(out.status.code(), Some(47), "stderr: {}", stderr(&out));
    let err = stderr(&out);
    assert!(err.contains("Invalid ACL configuration in "), "{err}");
    assert!(
        err.contains('0') || err.to_lowercase().contains("rule"),
        "the message must locate the offending rule: {err}"
    );
}

#[test]
fn t_acl_07_effect_enum_is_closed() {
    assert_invalid_acl_exits_47(
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['*']\n    effect: permit\n",
        "effect: permit",
    );
}

#[test]
fn t_acl_08_empty_pattern_array_is_refused() {
    assert_invalid_acl_exits_47(
        "default_effect: deny\nrules:\n  - callers: []\n    targets: ['*']\n    effect: allow\n",
        "callers: []",
    );
}

#[test]
fn a_vanished_file_reports_not_found_rather_than_invalid() {
    // `resolve_acl_file` sees the file; `ACL::load` then fails to read it.
    // Simulated directly because the race cannot be staged through argv.
    let dir = tempfile::tempdir().expect("tempdir");
    let file = dir.path().join("acl.yaml");
    std::fs::write(&file, "default_effect: deny\nrules: []\n").expect("write");
    assert!(resolve_acl_file(file.to_str().unwrap()).is_some());
    std::fs::remove_file(&file).expect("remove");
    assert!(
        load_cli_acl(file.to_str().unwrap())
            .expect("a vanished path is the missing-path no-op")
            .is_none(),
        "a path that no longer exists attaches nothing rather than erroring"
    );
}

// ---------------------------------------------------------------------------
// T-ACL-09 / T-ACL-10 -- `acl list`
// ---------------------------------------------------------------------------

#[test]
fn t_acl_09_list_json_preserves_definition_order() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&["apcli", "acl", "list", "--format", "json"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    let payload = json_stdout(&out);
    let rules = payload["rules"].as_array().expect("rules");
    assert_eq!(rules.len(), 3);
    for (i, rule) in rules.iter().enumerate() {
        assert_eq!(rule["index"], i as u64);
    }
    assert_eq!(rules[0]["targets"][0], "system.control.*");
    assert_eq!(rules[1]["approval"], "required");
    assert_eq!(rules[1]["conditions"]["roles"][0], "admin");
    assert_eq!(rules[2]["targets"][0], "db.read");
}

#[test]
fn t_acl_10_list_with_no_acl_is_the_documented_empty_shape() {
    let project = Project::new();
    let out = project.run(&["apcli", "acl", "list", "--format", "json"]);
    assert_eq!(
        out.status.code(),
        Some(0),
        "listing nothing is not an error; stderr: {}",
        stderr(&out)
    );
    let payload = json_stdout(&out);
    assert_eq!(payload["source"], Value::Null);
    assert_eq!(payload["default_effect"], Value::Null);
    assert_eq!(payload["rules"].as_array().map(Vec::len), Some(0));
}

#[test]
fn list_table_with_no_acl_says_so_and_exits_0() {
    let project = Project::new();
    let out = project.run(&["apcli", "acl", "list", "--format", "table"]);
    assert_eq!(out.status.code(), Some(0));
    assert!(
        stdout(&out).contains("No ACL configured."),
        "{}",
        stdout(&out)
    );
}

// ---------------------------------------------------------------------------
// T-ACL-11 .. T-ACL-16 -- `acl check`
// ---------------------------------------------------------------------------

#[test]
fn t_acl_11_allow_rule_exits_0_and_reports_the_matched_rule() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&["apcli", "acl", "check", "db.read", "--format", "json"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    let payload = json_stdout(&out);
    assert_eq!(payload["access"], "allow");
    assert_eq!(payload["caller"], "@external");
    assert_eq!(payload["matched_rule_index"], 2);
}

#[test]
fn t_acl_12_deny_rule_exits_77() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&[
        "apcli",
        "acl",
        "check",
        "system.control.disable",
        "--format",
        "json",
    ]);
    assert_eq!(out.status.code(), Some(77), "stderr: {}", stderr(&out));
    assert_eq!(json_stdout(&out)["access"], "deny");
    assert!(
        stderr(&out).contains("Access denied: @external -> system.control.disable"),
        "stderr: {}",
        stderr(&out)
    );
}

#[test]
fn t_acl_13_allow_with_approval_exits_0_not_77() {
    // The discriminating case for section 4.5: `check()` fails closed on an
    // approval requirement, so a command built on it would report a denial for
    // a call the rule set in fact permits.
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&[
        "--role",
        "admin",
        "apcli",
        "acl",
        "check",
        "db.migrate",
        "--format",
        "json",
    ]);
    assert_eq!(
        out.status.code(),
        Some(0),
        "an allow-with-approval outcome must exit 0; stderr: {}",
        stderr(&out)
    );
    let payload = json_stdout(&out);
    assert_eq!(payload["access"], "allow");
    assert_eq!(payload["approval_required"], true);
}

#[test]
fn t_acl_14_role_flag_satisfies_a_roles_condition() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&[
        "--role",
        "admin",
        "apcli",
        "acl",
        "check",
        "db.migrate",
        "--format",
        "json",
    ]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    assert_eq!(json_stdout(&out)["matched_rule_index"], 1);
}

#[test]
fn t_acl_15_without_a_role_the_rule_does_not_match() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&["apcli", "acl", "check", "db.migrate", "--format", "json"]);
    assert_eq!(
        out.status.code(),
        Some(77),
        "falls through to default_effect: deny; stderr: {}",
        stderr(&out)
    );
    let payload = json_stdout(&out);
    assert_eq!(payload["access"], "deny");
    assert_eq!(payload["matched_rule_index"], Value::Null);
}

#[test]
fn t_acl_16_input_flag_feeds_the_arguments_condition() {
    let project = Project::new();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['git.push']\n    effect: allow\n    conditions:\n      arguments:\n        has_key: ['force']\n",
    );
    let forced = project.run(&[
        "apcli",
        "acl",
        "check",
        "git.push",
        "--input",
        r#"{"force": true}"#,
        "--format",
        "json",
    ]);
    assert_eq!(forced.status.code(), Some(0), "stderr: {}", stderr(&forced));
    assert_eq!(json_stdout(&forced)["access"], "allow");

    // Discriminator: without the key the rule must not grant.
    let plain = project.run(&[
        "apcli",
        "acl",
        "check",
        "git.push",
        "--input",
        r#"{"remote": "origin"}"#,
        "--format",
        "json",
    ]);
    assert_eq!(plain.status.code(), Some(77), "stderr: {}", stderr(&plain));
}

#[test]
fn depth_flag_feeds_the_max_call_depth_condition() {
    let project = Project::new();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['*']\n    effect: allow\n    conditions:\n      max_call_depth: 2\n",
    );
    let shallow = project.run(&[
        "apcli", "acl", "check", "x.y", "--depth", "1", "--format", "json",
    ]);
    assert_eq!(
        shallow.status.code(),
        Some(0),
        "stderr: {}",
        stderr(&shallow)
    );
    let deep = project.run(&[
        "apcli", "acl", "check", "x.y", "--depth", "5", "--format", "json",
    ]);
    assert_eq!(
        deep.status.code(),
        Some(77),
        "a chain deeper than the threshold must not match; stderr: {}",
        stderr(&deep)
    );
}

#[test]
fn check_with_no_acl_exits_47() {
    let project = Project::new();
    let out = project.run(&["apcli", "acl", "check", "db.read"]);
    assert_eq!(out.status.code(), Some(47), "stderr: {}", stderr(&out));
    assert!(
        stderr(&out).contains("No ACL configured; nothing to check."),
        "stderr: {}",
        stderr(&out)
    );
}

/// A rule that matches only when BOTH condition keys are satisfied, one
/// supplied at the root level and one at the subcommand level. A merge that
/// resolves the identity all-or-nothing drops whichever level it did not pick,
/// and the rule then fails to match.
const TWO_LEVEL_IDENTITY_ACL: &str = "\
default_effect: deny
rules:
  - callers: ['*']
    targets: ['db.read']
    effect: allow
    description: service accounts with the admin role
    conditions:
      identity_types: ['service']
      roles: ['admin']
";

#[test]
fn identity_flags_merge_per_field_across_root_and_subcommand() {
    // Spec 4.5 precedence, end to end. `--identity-type service` is given
    // ONLY at the root and `--role admin` ONLY at the subcommand, so the rule
    // matches just in case both survived the merge. This is the discriminating
    // case: an all-or-nothing merge silently discards a field the caller never
    // withdrew, and a happy-path test where both flags sit at one level would
    // not notice.
    let project = Project::new();
    project.write("acl/global_acl.yaml", TWO_LEVEL_IDENTITY_ACL);
    let out = project.run(&[
        "--identity-type",
        "service",
        "apcli",
        "acl",
        "check",
        "--role",
        "admin",
        "db.read",
        "--format",
        "json",
    ]);
    assert_eq!(
        out.status.code(),
        Some(0),
        "the root --identity-type must survive the subcommand --role; \
         stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
    let payload = json_stdout(&out);
    assert_eq!(payload["access"], "allow");
    assert_eq!(payload["matched_rule_index"], 0);
}

#[test]
fn a_subcommand_role_replaces_the_root_role_rather_than_appending() {
    // The other half of "per field": the restated field is overridden, not
    // merged with, its root counterpart.
    let project = Project::new();
    project.write("acl/global_acl.yaml", TWO_LEVEL_IDENTITY_ACL);
    let out = project.run(&[
        "--identity-type",
        "service",
        "--role",
        "admin",
        "apcli",
        "acl",
        "check",
        "--role",
        "guest",
        "db.read",
        "--format",
        "json",
    ]);
    assert_eq!(
        out.status.code(),
        Some(77),
        "the subcommand --role guest replaces the root --role admin, so the \
         rule no longer matches; stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
}

#[test]
fn a_root_identity_flag_still_applies_when_not_restated() {
    // Both fields at the root, nothing restated: the rule must match.
    let project = Project::new();
    project.write("acl/global_acl.yaml", TWO_LEVEL_IDENTITY_ACL);
    let out = project.run(&[
        "--identity-type",
        "service",
        "--role",
        "admin",
        "apcli",
        "acl",
        "check",
        "db.read",
        "--format",
        "json",
    ]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    assert_eq!(json_stdout(&out)["access"], "allow");
}

#[test]
fn roles_only_invocation_uses_the_pinned_identity_sentinel() {
    // Cross-SDK pin: `@cli`. apcore's Identity requires an id while spec 4.3
    // permits the roles-only form, so the CLI supplies one -- and it must be
    // the same literal in every SDK, since it is observable in Identity.id.
    assert_eq!(apcore_cli::DEFAULT_IDENTITY_ID, "@cli");
    let identity = apcore_cli::CliIdentity {
        id: None,
        identity_type: None,
        roles: vec!["admin".to_string()],
    };
    assert_eq!(identity.to_identity().id(), "@cli");

    // And it is not the caller: a real execution always presents @external.
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&[
        "--role",
        "admin",
        "apcli",
        "acl",
        "check",
        "db.migrate",
        "--format",
        "json",
    ]);
    assert_eq!(json_stdout(&out)["caller"], "@external");
}

#[test]
fn caller_defaults_to_external_without_a_clap_default_value() {
    // The pinned help text states the default inline; a clap `default_value`
    // would render it a second time and break the byte-match. The default is
    // therefore resolved at the use site, which this pins behaviourally.
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&["apcli", "acl", "check", "db.read", "--format", "json"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    assert_eq!(json_stdout(&out)["caller"], "@external");

    let help = project.run(&["apcli", "acl", "check", "--help"]);
    let rendered = stdout(&help);
    assert!(
        rendered.contains("Simulated caller ID (default: @external)"),
        "{rendered}"
    );
    assert!(
        !rendered.contains("[default: @external]"),
        "clap must not append a second copy of the default: {rendered}"
    );
    assert!(
        !rendered.contains("[default: user]"),
        "same for --identity-type: {rendered}"
    );
}

#[test]
fn caller_flag_is_honoured_because_nothing_runs() {
    let project = Project::new();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['agent.*']\n    targets: ['*']\n    effect: allow\n",
    );
    let as_agent = project.run(&[
        "apcli",
        "acl",
        "check",
        "db.read",
        "--caller",
        "agent.one",
        "--format",
        "json",
    ]);
    assert_eq!(
        as_agent.status.code(),
        Some(0),
        "stderr: {}",
        stderr(&as_agent)
    );
    assert_eq!(json_stdout(&as_agent)["caller"], "agent.one");

    let default_caller = project.run(&["apcli", "acl", "check", "db.read", "--format", "json"]);
    assert_eq!(
        default_caller.status.code(),
        Some(77),
        "the default caller is @external, which the rule does not cover"
    );
}

// ---------------------------------------------------------------------------
// T-ACL-17 / T-ACL-18 / T-ACL-19 -- `acl validate`
// ---------------------------------------------------------------------------

#[test]
fn t_acl_17_unregistered_condition_key_is_a_finding_and_exits_47() {
    let project = Project::new();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['db.migrate']\n    effect: deny\n    conditions:\n      mispelled: ['x']\n",
    );
    let out = project.run(&["apcli", "acl", "validate", "--format", "json"]);
    assert_eq!(out.status.code(), Some(47), "stderr: {}", stderr(&out));
    let payload = json_stdout(&out);
    assert_eq!(payload["count"], 1);
    let finding = &payload["findings"][0];
    assert_eq!(finding["rule_index"], 0);
    assert_eq!(finding["condition_key"], "mispelled");
    assert_eq!(finding["effect"], "deny");
    assert!(finding["condition_path"].is_string());
}

#[test]
fn t_acl_18_sync_and_async_columns_are_not_collapsed() {
    let project = Project::new();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['db.migrate']\n    effect: deny\n    conditions:\n      mispelled: ['x']\n",
    );
    let json_out = project.run(&["apcli", "acl", "validate", "--format", "json"]);
    let finding = &json_stdout(&json_out)["findings"][0];
    assert!(
        finding["sync_resolvable"].is_boolean() && finding["async_resolvable"].is_boolean(),
        "both axes must be reported separately: {finding}"
    );

    let table_out = project.run(&["apcli", "acl", "validate", "--format", "table"]);
    let rendered = stdout(&table_out);
    assert!(rendered.contains("Sync"), "{rendered}");
    assert!(rendered.contains("Async"), "{rendered}");
}

#[test]
fn t_acl_19_clean_rule_set_reports_zero_findings_and_exits_0() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&["apcli", "acl", "validate", "--format", "json"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    assert_eq!(json_stdout(&out)["count"], 0);
}

#[test]
fn validate_with_no_acl_exits_47() {
    let project = Project::new();
    let out = project.run(&["apcli", "acl", "validate"]);
    assert_eq!(out.status.code(), Some(47), "stderr: {}", stderr(&out));
    assert!(
        stderr(&out).contains("No ACL configured; nothing to check."),
        "stderr: {}",
        stderr(&out)
    );
}

// ---------------------------------------------------------------------------
// T-ACL-20 / T-ACL-21 -- `acl status`
// ---------------------------------------------------------------------------

/// A stand-in control module, so `governance_state()` has a control surface to
/// report on. The standalone binary registers no `system.control.*` modules,
/// so this half of the matrix is exercised against a hand-built executor.
struct ControlModule;

#[async_trait::async_trait]
impl Module for ControlModule {
    fn input_schema(&self) -> Value {
        json!({"type": "object"})
    }
    fn output_schema(&self) -> Value {
        json!({"type": "object"})
    }
    fn description(&self) -> &str {
        "Disable a module"
    }
    fn annotations(&self) -> ModuleAnnotations {
        ModuleAnnotations {
            requires_approval: false,
            ..Default::default()
        }
    }
    async fn execute(
        &self,
        _inputs: Value,
        _ctx: &apcore::Context<Value>,
    ) -> Result<Value, apcore::errors::ModuleError> {
        Ok(json!({}))
    }
}

fn control_descriptor(module_id: &str) -> apcore::registry::registry::ModuleDescriptor {
    apcore::registry::registry::ModuleDescriptor {
        module_id: module_id.to_string(),
        name: None,
        description: "Disable a module".to_string(),
        documentation: None,
        input_schema: json!({"type": "object"}),
        output_schema: json!({"type": "object"}),
        version: "1.0.0".to_string(),
        tags: vec![],
        annotations: Some(ModuleAnnotations {
            requires_approval: false,
            ..Default::default()
        }),
        examples: vec![],
        metadata: std::collections::HashMap::new(),
        display: None,
        sunset_date: None,
        dependencies: vec![],
        enabled: true,
    }
}

fn executor_with_control_module() -> Executor {
    let registry = Registry::new();
    // `system` is a reserved first segment, so a control module must go
    // through `register_internal` -- the same door `apcore::sys_modules` uses.
    registry
        .register_internal(
            "system.control.disable",
            Box::new(ControlModule),
            control_descriptor("system.control.disable"),
        )
        .expect("registers");
    Executor::new(std::sync::Arc::new(registry), Config::default())
}

#[test]
fn t_acl_20_control_modules_with_no_acl_are_an_unprotected_surface() {
    let executor = executor_with_control_module();
    let state = executor.governance_state();
    assert!(state.control_modules_registered);
    assert!(!state.acl_configured);
    assert!(
        state.unprotected_control_surface,
        "control modules registered with neither an ACL gate nor a fully \
         annotated approval gate is exactly PROTOCOL_SPEC 6.6.5.1"
    );
}

#[test]
fn t_acl_21_strict_exits_47_on_an_unprotected_surface() {
    let executor = executor_with_control_module();
    let state = executor.governance_state();
    assert_eq!(status_exit_code(&state, /*strict*/ true), 47);
    assert_eq!(
        status_exit_code(&state, /*strict*/ false),
        0,
        "without --strict, status always exits 0"
    );
}

#[test]
fn attaching_an_acl_closes_the_unprotected_surface() {
    // The discriminating half of T-ACL-20: if `unprotected_control_surface`
    // were hardcoded, this assertion would fail.
    let mut executor = executor_with_control_module();
    let mut rule = ACLRule::new(
        vec!["@external".to_string()],
        vec!["system.control.*".to_string()],
        "deny",
    );
    rule.description = Some("no external control".to_string());
    executor.set_acl(ACL::try_new(vec![rule], "deny", None).expect("well-formed"));

    let state = executor.governance_state();
    assert!(state.acl_configured);
    assert!(state.builtin_acl_gate_wired);
    assert!(!state.unprotected_control_surface);
    assert_eq!(status_exit_code(&state, /*strict*/ true), 0);
}

#[test]
fn status_renders_the_nine_observations_over_the_binary() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", THREE_RULE_ACL);
    let out = project.run(&["apcli", "acl", "status", "--format", "table"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    let rendered = stdout(&out);
    for label in [
        "Control modules registered:",
        "Read modules registered:",
        "ACL configured:",
        "Built-in ACL gate wired:",
        "Approval handler configured:",
        "Built-in approval gate wired:",
        "Policy strict:",
        "All control modules gated:",
        "Unprotected control surface:",
    ] {
        assert!(rendered.contains(label), "missing '{label}':\n{rendered}");
    }
}

// ---------------------------------------------------------------------------
// T-ACL-22 / T-ACL-23 -- the previously inert enforcement paths go live
// ---------------------------------------------------------------------------

/// Absolute path to the repo's example extensions, so the scratch project can
/// borrow a real module without copying one.
fn example_extensions() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/extensions")
}

const DENY_MATH_ADD: &str = "\
default_effect: allow
rules:
  - callers: ['*']
    targets: ['math.add']
    effect: deny
    description: math.add is off limits
";

#[test]
fn t_acl_22_and_33_exec_denied_by_acl_exits_77() {
    let project = Project::new();
    project.write("acl/global_acl.yaml", DENY_MATH_ADD);
    let ext = example_extensions();
    let out = project.run(&[
        "--extensions-dir",
        ext.to_str().expect("utf8 path"),
        "math.add",
        "--a",
        "3",
        "--b",
        "4",
    ]);
    assert_eq!(
        out.status.code(),
        Some(77),
        "an ACL denial must surface as 77; stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
    assert!(
        stderr(&out).contains("math.add"),
        "the error must name the module; stderr: {}",
        stderr(&out)
    );
}

#[test]
fn exec_allowed_by_acl_still_runs() {
    // Discriminator for the test above: with the same machinery and an allow
    // rule, the call must succeed. Without this, a broken exec path would make
    // the denial test pass for the wrong reason.
    let project = Project::new();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['*']\n    effect: allow\n",
    );
    let ext = example_extensions();
    let out = project.run(&[
        "--extensions-dir",
        ext.to_str().expect("utf8 path"),
        "math.add",
        "--a",
        "3",
        "--b",
        "4",
    ]);
    assert_eq!(
        out.status.code(),
        Some(0),
        "stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
}

#[tokio::test]
async fn t_acl_23_preflight_reports_a_failed_acl_check_row() {
    // Driven against the executor rather than the binary: `apcli validate`
    // routes through `executor.call("system.validate", ...)` and falls back to
    // a synthetic three-check result when no host has registered the system
    // modules, so the standalone binary has no `acl` row to report. The row
    // itself comes from `Executor::validate`, which is what the CLI forwards
    // verbatim once a host registers them -- so that is what is measured here.
    let registry = Registry::new();
    registry
        .register_module("math.add", Box::new(AddModule))
        .expect("registers");
    let mut executor = Executor::new(std::sync::Arc::new(registry), Config::default());

    let permitted = executor
        .validate("math.add", &json!({"a": 1, "b": 2}), None)
        .await
        .expect("preflight is non-throwing");
    assert!(
        permitted.valid,
        "with no ACL attached the call passes preflight: {permitted:?}"
    );

    let mut rule = ACLRule::new(vec!["*".to_string()], vec!["math.add".to_string()], "deny");
    rule.description = Some("math.add is off limits".to_string());
    executor.set_acl(ACL::try_new(vec![rule], "allow", None).expect("well-formed"));

    let denied = executor
        .validate("math.add", &json!({"a": 1, "b": 2}), None)
        .await
        .expect("preflight is non-throwing");
    assert!(!denied.valid, "an ACL denial must fail preflight");
    let acl_row = denied
        .checks
        .iter()
        .find(|c| c.check == "acl")
        .expect("an `acl` check row must be present once an ACL is attached");
    assert!(!acl_row.passed, "the acl row must report the denial");

    // The CLI's cascade maps the FIRST failing check to an exit code, so the
    // acl row must be the one it reaches -- otherwise a denial would be
    // reported under some other code.
    let first_failed = denied
        .checks
        .iter()
        .find(|c| !c.passed)
        .expect("at least one check failed");
    assert_eq!(
        first_failed.check,
        "acl",
        "the acl row must be the first failure, so the cascade reports {}",
        apcore_cli::EXIT_ACL_DENIED
    );
}

/// Minimal in-process stand-in for the `math.add` example extension.
struct AddModule;

#[async_trait::async_trait]
impl Module for AddModule {
    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {"a": {"type": "number"}, "b": {"type": "number"}},
            "required": ["a", "b"]
        })
    }
    fn output_schema(&self) -> Value {
        json!({"type": "object"})
    }
    fn description(&self) -> &str {
        "Add two numbers"
    }
    async fn execute(
        &self,
        inputs: Value,
        _ctx: &apcore::Context<Value>,
    ) -> Result<Value, apcore::errors::ModuleError> {
        let a = inputs.get("a").and_then(Value::as_f64).unwrap_or_default();
        let b = inputs.get("b").and_then(Value::as_f64).unwrap_or_default();
        Ok(json!({"sum": a + b}))
    }
}

// ---------------------------------------------------------------------------
// T-ACL-31 / T-ACL-32 / T-ACL-33 / T-ACL-34 -- section 4.10: every execution
// path is gated
// ---------------------------------------------------------------------------
//
// Attaching an ACL to the executor gates the calls that go through that
// executor and nothing else. Two CLI paths build their own: the `--sandbox`
// subprocess (whose runner constructs a fresh Registry + Executor with NO ACL)
// and filesystem script modules (spawned directly, never reaching
// `Executor::call`). Both are complete bypasses; the sandbox one inverts the
// user's intent, since `--sandbox` is a security flag.
//
// The observable used here is a sentinel file the module's own script creates.
// If the parent refuses before spawning, the sentinel cannot exist -- which is
// the property section 4.10 requires, and is not implied by the exit code
// alone.

/// Env var carrying the sentinel path. The `APCORE_` prefix matters: the
/// sandbox clears the child's environment except a narrow allowlist, and this
/// prefix is on it, so the value reaches the script on both paths.
const SENTINEL_ENV: &str = "APCORE_TEST_SENTINEL";

const TOUCH_MODULE_JSON: &str = r#"{
  "name": "demo.touch",
  "description": "Create a sentinel file, proving the process ran",
  "input_schema": {"type": "object", "properties": {"x": {"type": "integer"}}},
  "output_schema": {"type": "object"},
  "executable": "run.sh"
}"#;

const TOUCH_RUN_SH: &str = "#!/usr/bin/env bash\n\
                            : > \"${APCORE_TEST_SENTINEL:-/dev/null}\"\n\
                            echo '{\"ok\": true}'\n";

/// A project holding `demo.touch`, plus the absolute sentinel path its script
/// writes when -- and only when -- the process actually runs.
fn touch_project() -> (Project, PathBuf) {
    let project = Project::new();
    project.write("extensions/demo/touch/module.json", TOUCH_MODULE_JSON);
    let script = project.write("extensions/demo/touch/run.sh", TOUCH_RUN_SH);
    let mut perms = std::fs::metadata(&script).expect("stat").permissions();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        perms.set_mode(0o755);
    }
    std::fs::set_permissions(&script, perms).expect("chmod +x");
    let sentinel = project.path().join("SPAWNED");
    (project, sentinel)
}

const DENY_TOUCH: &str = "\
default_effect: allow
rules:
  - callers: ['*']
    targets: ['demo.touch']
    effect: deny
    description: demo.touch is off limits
";

const ALLOW_ALL: &str = "\
default_effect: deny
rules:
  - callers: ['*']
    targets: ['*']
    effect: allow
";

impl Project {
    /// Run with the sentinel env var pointing at `sentinel`.
    fn run_touch(&self, sentinel: &Path, args: &[&str]) -> Output {
        self.run_with_env(args, &[(SENTINEL_ENV, sentinel.to_str().expect("utf8"))])
    }
}

#[test]
fn t_acl_31_sandboxed_denied_module_exits_77_and_is_never_spawned() {
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", DENY_TOUCH);

    let sandboxed = project.run_touch(&sentinel, &["demo.touch", "--x", "1", "--sandbox"]);
    assert_eq!(
        sandboxed.status.code(),
        Some(77),
        "--sandbox must not disable access control; stdout: {}\nstderr: {}",
        stdout(&sandboxed),
        stderr(&sandboxed)
    );
    assert!(
        !sentinel.exists(),
        "the decision must be reached in the parent, BEFORE the subprocess is \
         spawned -- the sentinel proves it ran"
    );
    assert!(
        stderr(&sandboxed).contains("Permission denied for module 'demo.touch'"),
        "stderr: {}",
        stderr(&sandboxed)
    );
}

#[test]
fn t_acl_31_discriminator_same_call_without_sandbox_also_exits_77() {
    // Half one of the discriminator: the sandboxed and unsandboxed forms must
    // agree. A gate on only one path would show up as a divergence here.
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", DENY_TOUCH);

    let plain = project.run_touch(&sentinel, &["demo.touch", "--x", "1"]);
    assert_eq!(
        plain.status.code(),
        Some(77),
        "stdout: {}\nstderr: {}",
        stdout(&plain),
        stderr(&plain)
    );
    assert!(!sentinel.exists(), "the script must not have run");
}

#[test]
fn t_acl_31_discriminator_both_forms_succeed_once_the_rule_is_removed() {
    // Half two: with the deny rule gone, BOTH forms must run and create the
    // sentinel. Without this, a gate that refused everything unconditionally
    // would pass the two assertions above.
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", ALLOW_ALL);

    let plain = project.run_touch(&sentinel, &["demo.touch", "--x", "1"]);
    assert_eq!(
        plain.status.code(),
        Some(0),
        "stdout: {}\nstderr: {}",
        stdout(&plain),
        stderr(&plain)
    );
    assert!(sentinel.exists(), "the allowed call must actually run");
    std::fs::remove_file(&sentinel).expect("reset sentinel");

    let sandboxed = project.run_touch(&sentinel, &["demo.touch", "--x", "1", "--sandbox"]);
    assert_eq!(
        sandboxed.status.code(),
        Some(0),
        "stdout: {}\nstderr: {}",
        stdout(&sandboxed),
        stderr(&sandboxed)
    );
    assert!(
        sentinel.exists(),
        "the allowed sandboxed call must actually run"
    );
}

#[test]
fn t_acl_32_sandboxed_allowed_module_runs_normally() {
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", ALLOW_ALL);
    let out = project.run_touch(&sentinel, &["demo.touch", "--x", "1", "--sandbox"]);
    assert_eq!(
        out.status.code(),
        Some(0),
        "stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
    assert!(stdout(&out).contains("\"ok\""), "{}", stdout(&out));
    assert!(sentinel.exists());
}

#[test]
fn t_acl_33_script_module_denied_by_acl_is_never_spawned() {
    // The FsDiscoverer path, stated as its own case: the exit code alone does
    // not prove the process was not started.
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", DENY_TOUCH);
    let out = project.run_touch(&sentinel, &["demo.touch", "--x", "1"]);
    assert_eq!(out.status.code(), Some(77), "stderr: {}", stderr(&out));
    assert!(!sentinel.exists(), "the script must not have been spawned");
}

#[test]
fn t_acl_34_acl_sourced_approval_gates_a_subprocess_call() {
    // An ACL-sourced `approval: required` must compose with the annotation
    // before the CLI's approval gate on these paths too -- otherwise the same
    // rule would demand a human on an in-process call and wave through a
    // sandboxed or script one.
    //
    // `demo.touch` carries no `requires_approval` annotation, so only the ACL
    // can put it to a human. stdin is a pipe under `.output()`, so the handler
    // has no TTY and refuses deterministically.
    let (project, sentinel) = touch_project();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['demo.touch']\n    effect: allow\n    approval: required\n",
    );

    let refused = project.run_touch(&sentinel, &["demo.touch", "--x", "1"]);
    assert_eq!(
        refused.status.code(),
        Some(46),
        "the ACL requires a human and none is available; stdout: {}\nstderr: {}",
        stdout(&refused),
        stderr(&refused)
    );
    assert!(
        !sentinel.exists(),
        "a call awaiting approval must not have run"
    );

    // Discriminating counterpart: `--yes` bypasses the prompt, so the same
    // call runs. Without this, a gate that simply refused everything would
    // pass the assertion above.
    let approved = project.run_touch(&sentinel, &["demo.touch", "--x", "1", "--yes"]);
    assert_eq!(
        approved.status.code(),
        Some(0),
        "stdout: {}\nstderr: {}",
        stdout(&approved),
        stderr(&approved)
    );
    assert!(sentinel.exists());
}

/// A conditional deny. With no context the condition is a non-match
/// (PROTOCOL_SPEC 6.5), so the rule steps aside and `default_effect: allow`
/// lets the call through -- which is exactly the bypass a gate passing `None`
/// would reopen. An unconditional rule cannot catch this.
const DENY_TOUCH_CONDITIONAL: &str = "\
default_effect: allow
rules:
  - callers: ['*']
    targets: ['demo.touch']
    effect: deny
    description: external callers may not touch
    conditions:
      identity_types: ['external']
";

/// An `arguments`-scoped deny, which is inert unless the gate supplies the
/// governance projection of the call's arguments (PROTOCOL_SPEC 6.1.7).
const DENY_TOUCH_ARGUMENTS: &str = "\
default_effect: allow
rules:
  - callers: ['*']
    targets: ['demo.touch']
    effect: deny
    description: no calls carrying x
    conditions:
      arguments:
        has_key: ['x']
";

#[test]
fn t_acl_31_conditional_deny_fires_on_the_script_path() {
    // The gate must present a Context even when no identity flag was given:
    // apcore's pipeline creates one at Step 1 for every real call, so a gate
    // passing `None` leaves conditional deny rules inert on the delegated path
    // while they fire in-process.
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", DENY_TOUCH_CONDITIONAL);
    let out = project.run_touch(&sentinel, &["demo.touch", "--x", "1"]);
    assert_eq!(
        out.status.code(),
        Some(77),
        "a conditional deny must fire without any identity flag; stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
    assert!(!sentinel.exists(), "the script must not have been spawned");
}

#[test]
fn t_acl_31_conditional_deny_fires_on_the_sandbox_path() {
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", DENY_TOUCH_CONDITIONAL);
    let out = project.run_touch(&sentinel, &["demo.touch", "--x", "1", "--sandbox"]);
    assert_eq!(
        out.status.code(),
        Some(77),
        "stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
    assert!(!sentinel.exists());
}

#[test]
fn t_acl_31_conditional_deny_matches_the_in_process_verdict() {
    // The delegated paths must agree with what the same rule set says for an
    // in-process call. `demo.touch` is deliberately NOT a `system.*` id:
    // apcore's registry rejects that prefix as reserved, so the comparison
    // would fail with MODULE_NOT_FOUND before reaching the ACL check and
    // prove nothing.
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", DENY_TOUCH_CONDITIONAL);
    let simulated = project.run_touch(
        &sentinel,
        &[
            "apcli",
            "acl",
            "check",
            "demo.touch",
            "--identity-type",
            "external",
            "--format",
            "json",
        ],
    );
    assert_eq!(
        simulated.status.code(),
        Some(77),
        "stderr: {}",
        stderr(&simulated)
    );
    assert_eq!(json_stdout(&simulated)["access"], "deny");
}

#[test]
fn t_acl_31_conditional_deny_discriminator_condition_that_does_not_match() {
    // With a condition the default gate context cannot satisfy, the rule must
    // step aside and the call must run -- otherwise the two tests above would
    // pass against a gate that simply denied everything conditional.
    let (project, sentinel) = touch_project();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: allow\nrules:\n  - callers: ['*']\n    targets: ['demo.touch']\n    effect: deny\n    conditions:\n      identity_types: ['service']\n",
    );
    let out = project.run_touch(&sentinel, &["demo.touch", "--x", "1"]);
    assert_eq!(
        out.status.code(),
        Some(0),
        "the gate context is @external, not service; stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
    assert!(sentinel.exists());
}

#[test]
fn t_acl_31_arguments_scoped_deny_fires_on_the_delegated_paths() {
    // The other half of the gate's inputs: without the governance projection
    // an `arguments` rule is unevaluable and goes inert the same way.
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", DENY_TOUCH_ARGUMENTS);

    let script = project.run_touch(&sentinel, &["demo.touch", "--x", "1"]);
    assert_eq!(
        script.status.code(),
        Some(77),
        "stdout: {}\nstderr: {}",
        stdout(&script),
        stderr(&script)
    );
    assert!(!sentinel.exists());

    let sandboxed = project.run_touch(&sentinel, &["demo.touch", "--x", "1", "--sandbox"]);
    assert_eq!(
        sandboxed.status.code(),
        Some(77),
        "stdout: {}\nstderr: {}",
        stdout(&sandboxed),
        stderr(&sandboxed)
    );
    assert!(!sentinel.exists());
}

#[test]
fn t_acl_31_arguments_scoped_deny_discriminator_key_absent() {
    // `demo.touch` takes an optional `x`; omitting it means the projection
    // carries no `x` key, the rule does not match, and the call runs.
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", DENY_TOUCH_ARGUMENTS);
    let out = project.run_touch(&sentinel, &["demo.touch"]);
    assert_eq!(
        out.status.code(),
        Some(0),
        "stdout: {}\nstderr: {}",
        stdout(&out),
        stderr(&out)
    );
    assert!(sentinel.exists());
}

#[test]
fn t_acl_34_a_module_without_an_acl_approval_rule_is_not_gated() {
    // The other direction: the annotation-composition must not gate calls the
    // ACL said nothing about.
    let (project, sentinel) = touch_project();
    project.write("acl/global_acl.yaml", ALLOW_ALL);
    let out = project.run_touch(&sentinel, &["demo.touch", "--x", "1"]);
    assert_eq!(out.status.code(), Some(0), "stderr: {}", stderr(&out));
    assert!(sentinel.exists());
}

// ---------------------------------------------------------------------------
// T-ACL-25 -- strategy bypass warning
// ---------------------------------------------------------------------------

#[test]
fn t_acl_25_testing_strategy_warns_that_a_configured_acl_is_not_enforced() {
    let project = Project::new();
    project.write(
        "acl/global_acl.yaml",
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['*']\n    effect: allow\n",
    );
    let ext = example_extensions();
    let out = project.run(&[
        "--extensions-dir",
        ext.to_str().expect("utf8 path"),
        "math.add",
        "--strategy",
        "testing",
        "--a",
        "1",
        "--b",
        "2",
    ]);
    let err = stderr(&out);
    assert!(
        err.contains("testing") && err.contains("configured ACL is not enforced"),
        "the banner must name the strategy and say *configured*; stderr: {err}"
    );
}

#[test]
fn no_bypass_warning_without_an_attached_acl() {
    // The warning is about bypassing a *real* rule set; with none attached it
    // must stay silent, or it becomes noise on every testing run.
    let project = Project::new();
    let ext = example_extensions();
    let out = project.run(&[
        "--extensions-dir",
        ext.to_str().expect("utf8 path"),
        "math.add",
        "--strategy",
        "testing",
        "--a",
        "1",
        "--b",
        "2",
    ]);
    assert!(
        !stderr(&out).contains("configured ACL is not enforced"),
        "stderr: {}",
        stderr(&out)
    );
    // And the emitter itself is a no-op on both quiet paths.
    warn_if_strategy_bypasses_acl(Some("testing"), false);
    warn_if_strategy_bypasses_acl(Some("standard"), true);
    warn_if_strategy_bypasses_acl(None, true);
}

// ---------------------------------------------------------------------------
// T-ACL-24 -- ACL-sourced approval reaches the CLI handler
// ---------------------------------------------------------------------------
//
// The full discriminating case (handler refuses, non-TTY, call denied) lives
// in `tests/acl_argument_scoped_approval.rs`, which predates FE-14 and already
// drives an ACL rule carrying `approval: required` against a module annotated
// `requires_approval: false`. This test pins the composition FE-14 relies on:
// an ACL attached by the CLI loader routes such a call to the approval gate.

#[test]
fn t_acl_24_acl_sourced_approval_is_visible_in_preflight() {
    let dir = tempfile::tempdir().expect("tempdir");
    let acl_path = dir.path().join("global_acl.yaml");
    std::fs::write(
        &acl_path,
        "default_effect: deny\nrules:\n  - callers: ['*']\n    targets: ['git.push']\n    effect: allow\n    approval: required\n",
    )
    .expect("write");

    let acl = load_cli_acl(acl_path.to_str().unwrap())
        .expect("loads")
        .expect("attached");
    let decision = acl.check_access(Some("@external"), "git.push", None, None);
    assert_eq!(decision.access, "allow");
    assert!(
        decision.approval_required,
        "an ACL rule carrying approval: required must raise the approval axis \
         even for a module whose annotation says otherwise"
    );
    assert_eq!(check_exit_code(&decision), 0);
}

// ---------------------------------------------------------------------------
// Exit-code taxonomy
// ---------------------------------------------------------------------------

#[test]
fn exit_code_helpers_match_the_spec_table() {
    let allow = apcore::AccessDecision::new("allow", false, Some(0), "rule_match");
    let allow_with_approval = apcore::AccessDecision::new("allow", true, Some(0), "rule_match");
    let deny = apcore::AccessDecision::new("deny", false, Some(0), "rule_match");
    assert_eq!(check_exit_code(&allow), 0);
    assert_eq!(
        check_exit_code(&allow_with_approval),
        0,
        "the approval axis must not leak into the exit code"
    );
    assert_eq!(check_exit_code(&deny), 77);
    assert_eq!(validate_exit_code(0), 0);
    assert_eq!(validate_exit_code(1), 47);
    assert_eq!(validate_exit_code(9), 47);
}

#[test]
fn acl_rule_error_maps_to_47_not_77() {
    // T-ACL-30's Rust half: the exit map carries ACL_RULE_ERROR, and it is
    // distinct from the ACL_DENIED code.
    assert_eq!(apcore_cli::EXIT_ACL_RULE_ERROR, 47);
    assert_eq!(apcore_cli::EXIT_ACL_DENIED, 77);
    assert_ne!(apcore_cli::EXIT_ACL_RULE_ERROR, apcore_cli::EXIT_ACL_DENIED);
}

// ---------------------------------------------------------------------------
// Registration (spec 4.10)
// ---------------------------------------------------------------------------

#[test]
fn acl_is_registered_under_the_apcli_group() {
    let group = apcore_cli::register_apcli_subcommands(
        clap::Command::new("apcli"),
        &apcore_cli::ApcliGroup::from_yaml(None, /*registry_injected*/ false),
        "apcore-cli",
    );
    let acl = group
        .get_subcommands()
        .find(|c| c.get_name() == "acl")
        .expect("acl must be registered under apcli");
    let subs: Vec<&str> = acl.get_subcommands().map(|c| c.get_name()).collect();
    for expected in ["list", "check", "validate", "status"] {
        assert!(subs.contains(&expected), "missing '{expected}': {subs:?}");
    }
}

#[test]
fn acl_is_not_always_registered() {
    // Spec 4.10: `acl` is NOT in _ALWAYS_REGISTERED; under `mode: include` it
    // registers only when explicitly listed.
    assert!(!apcore_cli::APCLI_ALWAYS_REGISTERED.contains(&"acl"));
    let cfg = apcore_cli::ApcliGroup::from_cli_config(
        Some(apcore_cli::ApcliConfig {
            mode: apcore_cli::ApcliMode::Include(vec!["list".to_string()]),
            disable_env: true,
        }),
        /*registry_injected*/ false,
    );
    let group =
        apcore_cli::register_apcli_subcommands(clap::Command::new("apcli"), &cfg, "apcore-cli");
    let names: Vec<&str> = group.get_subcommands().map(|c| c.get_name()).collect();
    assert!(!names.contains(&"acl"), "got {names:?}");

    let listed_cfg = apcore_cli::ApcliGroup::from_cli_config(
        Some(apcore_cli::ApcliConfig {
            mode: apcore_cli::ApcliMode::Include(vec!["acl".to_string()]),
            disable_env: true,
        }),
        /*registry_injected*/ false,
    );
    let listed = apcore_cli::register_apcli_subcommands(
        clap::Command::new("apcli"),
        &listed_cfg,
        "apcore-cli",
    );
    let listed_names: Vec<&str> = listed.get_subcommands().map(|c| c.get_name()).collect();
    assert!(listed_names.contains(&"acl"), "got {listed_names:?}");
}

// ---------------------------------------------------------------------------
// Identity flags are assertions, not authentication (spec 4.3 / 7)
// ---------------------------------------------------------------------------

#[test]
fn identity_flag_help_states_it_is_not_authentication() {
    let project = Project::new();
    let out = project.run(&["--help"]);
    let rendered = stdout(&out);
    assert!(rendered.contains("--identity-id"), "{rendered}");
    assert!(rendered.contains("--identity-type"), "{rendered}");
    assert!(rendered.contains("--role"), "{rendered}");
    assert!(
        rendered.contains("not authentication"),
        "each identity flag must document the caveat: {rendered}"
    );
}

#[test]
fn no_flag_sets_a_caller_id() {
    // Spec 7 rule 2: real execution always presents @external, and the CLI
    // must expose no flag that sets Context::caller_id.
    let project = Project::new();
    let rendered = stdout(&project.run(&["--help", "--all-options"]));
    assert!(
        !rendered.contains("--caller-id"),
        "the CLI must not offer a caller_id flag: {rendered}"
    );
}

#[test]
fn approval_requirement_is_representable_only_on_allow_rules() {
    // Guard on the fixture's assumption: apcore refuses `approval: required`
    // on a deny rule, so the three-rule fixture cannot silently drift into an
    // unrepresentable state.
    let mut bad = ACLRule::new(vec!["*".to_string()], vec!["x".to_string()], "deny");
    bad.approval = Some(ApprovalRequirement::Required);
    assert!(
        ACL::try_new(vec![bad], "deny", None).is_err(),
        "approval: required on a deny rule must be refused at load"
    );
}

// ---------------------------------------------------------------------------
// T-ACL-26 / 27 / 27a / 27b / 27c -- section 4.8 audit wiring
// ---------------------------------------------------------------------------
//
// Driven in-process rather than through the binary, deliberately. The
// production audit path writes to `~/.apcore-cli/audit.jsonl`, and a test that
// spawns the real CLI with auditing on would append to the developer's own log
// -- so every other test in this file sets APCORE_CLI_AUDIT_DISABLE=1. These
// tests instead exercise the exact function `main.rs` calls
// (`load_cli_acl_with_audit`) with an `AuditLogger` pointed at a temp file,
// which is where all of section 4.8's observable behaviour lives.

/// An ACL denying `system.control.*` and allowing `db.read`, so one fixture
/// can produce both a deny and an allow decision.
const AUDIT_ACL: &str = "\
default_effect: deny
rules:
  - callers: ['@external']
    targets: ['system.control.*']
    effect: deny
    description: no external control
  - callers: ['*']
    targets: ['db.read']
    effect: allow
";

/// The same rules over a permissive default, for T-ACL-27b.
const AUDIT_ACL_DEFAULT_ALLOW: &str = "\
default_effect: allow
rules:
  - callers: ['@external']
    targets: ['system.control.*']
    effect: deny
    description: no external control
";

/// A scratch project holding an ACL file, an optional `apcore.yaml`, and the
/// audit log the section 4.8 callback writes to.
struct AuditProject {
    dir: tempfile::TempDir,
}

impl AuditProject {
    fn new(acl_yaml: &str, apcore_yaml: Option<&str>) -> Self {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(dir.path().join("global_acl.yaml"), acl_yaml).expect("write acl");
        if let Some(contents) = apcore_yaml {
            std::fs::write(dir.path().join("apcore.yaml"), contents).expect("write apcore.yaml");
        }
        Self { dir }
    }

    fn acl_path(&self) -> String {
        self.dir
            .path()
            .join("global_acl.yaml")
            .to_string_lossy()
            .to_string()
    }

    /// A resolver over this project's `apcore.yaml`, matching how `main.rs`
    /// builds one before calling `load_cli_acl_with_audit`.
    fn resolver(&self) -> ConfigResolver {
        let config = self.dir.path().join("apcore.yaml");
        if config.exists() {
            ConfigResolver::new(None, Some(config))
        } else {
            ConfigResolver::new(None, None)
        }
    }

    fn log_path(&self) -> PathBuf {
        self.dir.path().join("audit.jsonl")
    }

    fn logger(&self) -> AuditLogger {
        AuditLogger::new(Some(self.log_path()))
    }

    /// Every line of the audit log, parsed. Empty when nothing was written.
    fn entries(&self) -> Vec<Value> {
        let Ok(raw) = std::fs::read_to_string(self.log_path()) else {
            return Vec::new();
        };
        raw.lines()
            .filter(|l| !l.trim().is_empty())
            .map(|l| {
                serde_json::from_str(l).unwrap_or_else(|e| panic!("bad JSONL line ({e}): {l}"))
            })
            .collect()
    }

    /// Raw log lines, for the assertions that care about byte order.
    fn raw_lines(&self) -> Vec<String> {
        let Ok(raw) = std::fs::read_to_string(self.log_path()) else {
            return Vec::new();
        };
        raw.lines()
            .filter(|l| !l.trim().is_empty())
            .map(str::to_string)
            .collect()
    }
}

/// Whether an audit callback is installed on `acl`.
///
/// apcore keeps `ACL::audit_logger` private and offers no accessor, but its
/// `Debug` impl renders the slot as `audit_logger: None` or
/// `audit_logger: Some("...")`. T-ACL-27a asks for "no logger was installed",
/// not merely "no entries were written" -- the two differ, and only this
/// distinguishes them.
fn has_audit_logger(acl: &ACL) -> bool {
    let rendered = format!("{acl:?}");
    if rendered.contains("audit_logger: None") {
        return false;
    }
    assert!(
        rendered.contains(r#"audit_logger: Some("#),
        "apcore's ACL Debug impl no longer renders the audit_logger slot; \
         this helper is the only way the tests can see it:\n{rendered}"
    );
    true
}

/// Top-level object keys of one JSON line, in written order.
///
/// Section 4.8 pins key order, and `serde_json::Value` cannot report it
/// portably (its object map is a `BTreeMap` unless `preserve_order` happens to
/// be enabled somewhere in the dependency graph), so the order is read off the
/// raw text.
fn ordered_keys(line: &str) -> Vec<String> {
    let mut keys = Vec::new();
    let mut pending: Option<String> = None;
    let mut depth: i32 = 0;
    let mut chars = line.chars();
    while let Some(c) = chars.next() {
        match c {
            '"' => {
                // Consume the whole string, so a colon inside a value (an
                // RFC 3339 timestamp, say) is never read as a separator.
                let mut s = String::new();
                while let Some(ch) = chars.next() {
                    match ch {
                        '\\' => {
                            chars.next();
                        }
                        '"' => break,
                        _ => s.push(ch),
                    }
                }
                pending = Some(s);
            }
            ':' if depth == 1 => {
                if let Some(key) = pending.take() {
                    keys.push(key);
                }
            }
            '{' | '[' => {
                depth += 1;
                pending = None;
            }
            '}' | ']' => {
                depth -= 1;
                pending = None;
            }
            _ => {}
        }
    }
    keys
}

#[test]
fn t_acl_26_a_denied_call_writes_one_entry_with_all_13_fields() {
    // T-ACL-26: `acl.audit.enabled: true`, run a denied call -> one AuditEntry
    // in the audit log with `decision: deny`, all 13 fields.
    let project = AuditProject::new(AUDIT_ACL, None);
    let resolver = project.resolver();
    assert!(
        acl_audit_enabled(&resolver),
        "auditing is on by default; this test's premise"
    );

    let acl = load_cli_acl_with_audit(&project.acl_path(), &resolver, Some(project.logger()))
        .expect("loads")
        .expect("attached");
    assert!(has_audit_logger(&acl));

    let decision = acl.check_access(Some("@external"), "system.control.disable", None, None);
    assert_eq!(decision.access, "deny");

    let entries = project.entries();
    assert_eq!(entries.len(), 1, "one AuditEntry per check_access call");
    assert_eq!(entries[0]["decision"], "deny");
    assert_eq!(entries[0]["caller_id"], "@external");
    assert_eq!(entries[0]["target_id"], "system.control.disable");
    assert_eq!(entries[0]["reason"], "rule_match");
    assert_eq!(entries[0]["matched_rule_index"], 0);
    assert_eq!(entries[0]["matched_rule"], "no external control");

    // Field set, order and casing in one assertion: an equality check on the
    // ordered key list. Containment would miss a re-ordering, a renamed field,
    // and a CLI-added extra alike.
    let lines = project.raw_lines();
    assert_eq!(
        ordered_keys(&lines[0]),
        ACL_AUDIT_FIELDS,
        "the 13 section-4.8 fields, in apcore's AuditEntry declaration order"
    );

    // The optionals a `None` would have dropped are present as null, and the
    // two the spec calls out by name are present at all.
    assert!(entries[0]["handler_error"].is_null());
    assert_eq!(entries[0]["approval_required"], json!(false));
    assert!(entries[0]["identity_type"].is_null());
    assert!(entries[0]["trace_id"].is_null());
    assert_eq!(entries[0]["roles"], json!([]));
}

#[test]
fn t_acl_27_include_denied_false_drops_the_deny_and_keeps_the_allow() {
    // T-ACL-27: `acl.audit.include_denied: false`, run a denied call and an
    // allowed call -> the deny entry is absent; the allow entry is written.
    //
    // The discriminating half is the allow entry. A reading that inverted the
    // key ("include_denied: false means log denials only") would write exactly
    // the opposite line and still produce a one-entry log.
    let project = AuditProject::new(
        AUDIT_ACL,
        Some("acl:\n  audit:\n    enabled: true\n    include_denied: false\n"),
    );
    let resolver = project.resolver();
    assert!(acl_audit_enabled(&resolver));
    assert!(!acl_audit_include_denied(&resolver));

    let acl = load_cli_acl_with_audit(&project.acl_path(), &resolver, Some(project.logger()))
        .expect("loads")
        .expect("attached");

    let denied = acl.check_access(Some("@external"), "system.control.disable", None, None);
    assert_eq!(denied.access, "deny");
    let allowed = acl.check_access(Some("@external"), "db.read", None, None);
    assert_eq!(allowed.access, "allow");

    let entries = project.entries();
    assert_eq!(
        entries.len(),
        1,
        "the deny is suppressed and the allow is kept: {entries:?}"
    );
    assert_eq!(entries[0]["decision"], "allow");
    assert_eq!(entries[0]["target_id"], "db.read");
    assert!(
        !entries
            .iter()
            .any(|e| e["target_id"] == "system.control.disable"),
        "no deny entry may reach the log: {entries:?}"
    );
}

#[test]
fn t_acl_27a_disabled_installs_no_logger_and_does_not_rebuild() {
    // T-ACL-27a: `acl.audit.enabled: false` -> no audit callback installed and
    // no rebuild; the ACL from `ACL::load` is attached directly; no ACL entries
    // written for either outcome.
    let project = AuditProject::new(AUDIT_ACL, Some("acl:\n  audit:\n    enabled: false\n"));
    let resolver = project.resolver();
    assert!(!acl_audit_enabled(&resolver));

    let mut acl = load_cli_acl_with_audit(&project.acl_path(), &resolver, Some(project.logger()))
        .expect("loads")
        .expect("attached");

    // The point of the row: no *logger*, asserted directly. "No entries were
    // written" would also pass against a callback that silently dropped every
    // entry, which is a different (and worse) implementation.
    assert!(
        !has_audit_logger(&acl),
        "acl.audit.enabled: false must install no callback at all"
    );

    // "and no rebuild": `ACL::new` drops the `yaml_path` that only `ACL::load`
    // sets, and `reload()` is the observable that depends on it. A rebuilt ACL
    // would fail here with ACLRuleError("Cannot reload: ...").
    assert!(
        acl.reload().is_ok(),
        "the ACL from ACL::load must be attached directly, keeping reload()"
    );

    let denied = acl.check_access(Some("@external"), "system.control.disable", None, None);
    assert_eq!(denied.access, "deny");
    let allowed = acl.check_access(Some("@external"), "db.read", None, None);
    assert_eq!(allowed.access, "allow");
    assert!(
        project.entries().is_empty(),
        "no ACL entries for either outcome: {:?}",
        project.entries()
    );
}

#[test]
fn t_acl_27b_default_effect_allow_survives_audit_wiring() {
    // T-ACL-27b: an ACL file declaring `default_effect: allow`, with
    // `acl.audit.enabled: true` -> the attached ACL's default_effect is still
    // `allow`, and a call matching no rule is permitted.
    //
    // Discriminating for section 4.8 requirement 1. This SDK attaches via
    // `ACL::set_audit_logger`, so the file's default_effect is never re-stated
    // and the defect is structurally unreachable -- the test is written anyway,
    // to pin the behaviour against a future refactor to the rebuild form, where
    // a literal "deny" would invert the governing default silently and every
    // fixture using a deny-defaulted file would still pass.
    let project = AuditProject::new(AUDIT_ACL_DEFAULT_ALLOW, None);
    let resolver = project.resolver();
    assert!(
        acl_audit_enabled(&resolver),
        "the auditing path is the one under test"
    );

    let acl = load_cli_acl_with_audit(&project.acl_path(), &resolver, Some(project.logger()))
        .expect("loads")
        .expect("attached");
    assert!(
        has_audit_logger(&acl),
        "a rebuild-form regression would only be caught on the auditing path"
    );

    assert_eq!(
        acl.default_effect(),
        "allow",
        "the file's default_effect must reach the attached ACL unchanged"
    );

    let decision = acl.check_access(Some("@external"), "nothing.matches.this", None, None);
    assert_eq!(
        decision.access, "allow",
        "a call matching no rule falls through to the file's own default_effect"
    );
    assert_eq!(decision.reason, "default_effect");
    assert_eq!(decision.matched_rule_index, None);

    // The rules themselves survive too: the deny rule still denies.
    let denied = acl.check_access(Some("@external"), "system.control.disable", None, None);
    assert_eq!(denied.access, "deny");

    // And both decisions were audited, allow included.
    let entries = project.entries();
    assert_eq!(entries.len(), 2);
    assert_eq!(entries[0]["decision"], "allow");
    assert_eq!(entries[0]["reason"], "default_effect");
    assert_eq!(entries[1]["decision"], "deny");
}

#[test]
fn t_acl_27c_an_embedder_supplied_acl_is_attached_unchanged() {
    // T-ACL-27c: an embedder supplies its own ACL with auditing enabled -> the
    // supplied instance is attached unchanged, not rebuilt, so it retains
    // reload().
    //
    // Rust language note: this SDK has no `create_cli(acl=...)` factory (see
    // lib.rs on the embed-API parity gap), so an embedder attaches its own ACL
    // by calling `Executor::set_acl` directly -- a path that never reaches
    // `load_cli_acl_with_audit` and so can acquire neither a CLI callback nor a
    // rebuild. Pointer identity is also not expressible here: `set_acl` moves
    // the ACL and re-wraps it in a fresh `Arc`, so *every* implementation
    // changes the pointer. The observables section 8 names -- "not rebuilt",
    // "retains reload()" -- are asserted instead, on the very instance the
    // executor ends up holding.
    let project = AuditProject::new(AUDIT_ACL, None);
    let resolver = project.resolver();
    assert!(
        acl_audit_enabled(&resolver),
        "auditing is on: the config that would wire a callback is in force"
    );

    // The embedder's own ACL, built the way an embedder would.
    let embedder_acl = ACL::load(&project.acl_path()).expect("the embedder loads its own ACL");
    assert!(!has_audit_logger(&embedder_acl));

    let mut executor = Executor::new(std::sync::Arc::new(Registry::new()), Config::default());
    executor.set_acl(embedder_acl);

    // Recover the exact instance the executor holds. Nothing else cloned the
    // Arc, so this is that instance and not a copy of it.
    let held = executor.acl.take().expect("an ACL is attached");
    let mut held = std::sync::Arc::try_unwrap(held)
        .expect("the executor holds the only reference to the supplied ACL");

    assert!(
        !has_audit_logger(&held),
        "an embedder-supplied ACL must not acquire the CLI's audit callback"
    );
    assert_eq!(held.default_effect(), "deny");
    assert_eq!(held.rules().len(), 2);
    assert!(
        held.reload().is_ok(),
        "attached unchanged, so the yaml_path provenance -- and reload() -- survives"
    );
    assert!(
        project.entries().is_empty(),
        "nothing the embedder supplied may write to the CLI audit log"
    );

    // Discriminating half: the same config, taken through the CLI's own load
    // path, *does* get a callback. Without this the assertions above would
    // pass against an implementation that never wires anything at all.
    let cli_loaded =
        load_cli_acl_with_audit(&project.acl_path(), &resolver, Some(project.logger()))
            .expect("loads")
            .expect("attached");
    assert!(has_audit_logger(&cli_loaded));
}

#[test]
fn no_fe_05_logger_means_no_callback_and_no_write() {
    // Section 4.8, as amended: with no FE-05 logger installed the callback
    // writes nothing and fails silently. FE-05 auditing is off process-wide
    // (APCORE_CLI_AUDIT_DISABLE=1 in main.rs), so there is no log to write to
    // and no callback is installed either.
    let project = AuditProject::new(AUDIT_ACL, None);
    let acl = load_cli_acl_with_audit(&project.acl_path(), &project.resolver(), None)
        .expect("loads")
        .expect("attached");
    assert!(!has_audit_logger(&acl));
    let decision = acl.check_access(Some("@external"), "system.control.disable", None, None);
    assert_eq!(
        decision.access, "deny",
        "the decision is unaffected by having no audit sink"
    );
    assert!(project.entries().is_empty());
}

#[test]
fn an_installed_callback_over_an_unopenable_sink_stays_silent() {
    // The other half of the same rule: a callback IS installed, over an
    // AuditLogger whose path can never be opened (the empty path). It must
    // no-op silently rather than panic or propagate.
    let project = AuditProject::new(AUDIT_ACL, None);
    let mut acl = load_cli_acl(&project.acl_path())
        .expect("loads")
        .expect("attached");
    install_acl_audit_logger(&mut acl, AuditLogger::new(Some(PathBuf::new())), true);
    assert!(has_audit_logger(&acl));

    let decision = acl.check_access(Some("@external"), "system.control.disable", None, None);
    assert_eq!(decision.access, "deny");
    assert!(project.entries().is_empty());
}

#[test]
fn a_logging_fault_does_not_change_the_access_decision() {
    // Section 4.8, as amended: a logging fault MUST NOT change an access
    // decision. The callback swallows its own errors and the decision stands.
    //
    // Fault injection: point the AuditLogger at a path that is a directory, so
    // every append fails with EISDIR.
    let project = AuditProject::new(AUDIT_ACL, None);
    let unwritable = project.dir.path().join("audit-is-a-directory");
    std::fs::create_dir(&unwritable).expect("mkdir");

    let mut acl = load_cli_acl(&project.acl_path())
        .expect("loads")
        .expect("attached");
    install_acl_audit_logger(&mut acl, AuditLogger::new(Some(unwritable)), true);

    // Both axes still answer exactly as they would with auditing off.
    let denied = acl.check_access(Some("@external"), "system.control.disable", None, None);
    assert_eq!(denied.access, "deny");
    assert_eq!(check_exit_code(&denied), 77);
    let allowed = acl.check_access(Some("@external"), "db.read", None, None);
    assert_eq!(allowed.access, "allow");
    assert_eq!(check_exit_code(&allowed), 0);
}