selfware 0.6.3

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

use std::collections::HashMap;
use std::sync::Arc;

use super::coordinator::{ConflictStrategy, Swarm};
use super::memory::SharedMemory;
use super::types::{Agent, AgentRole, AgentStatus, DecisionStatus, SwarmTask, TaskStatus, Vote};

// ============================================================================
// Agent Tests
// ============================================================================

#[test]
fn test_agent_role_default() {
    assert_eq!(AgentRole::default(), AgentRole::General);
}

#[test]
fn test_agent_role_name() {
    assert_eq!(AgentRole::Architect.name(), "Architect");
    assert_eq!(AgentRole::Coder.name(), "Coder");
}

#[test]
fn test_agent_role_priority() {
    assert!(AgentRole::Security.priority() > AgentRole::Coder.priority());
    assert!(AgentRole::Architect.priority() > AgentRole::General.priority());
}

#[test]
fn test_agent_role_name_all_variants() {
    assert_eq!(AgentRole::Architect.name(), "Architect");
    assert_eq!(AgentRole::Coder.name(), "Coder");
    assert_eq!(AgentRole::Tester.name(), "Tester");
    assert_eq!(AgentRole::Reviewer.name(), "Reviewer");
    assert_eq!(AgentRole::Documenter.name(), "Documenter");
    assert_eq!(AgentRole::DevOps.name(), "DevOps");
    assert_eq!(AgentRole::Security.name(), "Security");
    assert_eq!(AgentRole::Performance.name(), "Performance");
    assert_eq!(AgentRole::VisualCritic.name(), "VisualCritic");
    assert_eq!(AgentRole::General.name(), "General");
}

#[test]
fn test_agent_role_system_prompt_all_variants() {
    assert!(AgentRole::Architect.system_prompt().contains("architect"));
    assert!(AgentRole::Coder.system_prompt().contains("programmer"));
    assert!(AgentRole::Tester.system_prompt().contains("testing"));
    assert!(AgentRole::Reviewer.system_prompt().contains("reviewer"));
    assert!(AgentRole::Documenter
        .system_prompt()
        .contains("documentation"));
    assert!(AgentRole::DevOps.system_prompt().contains("DevOps"));
    assert!(AgentRole::Security.system_prompt().contains("security"));
    assert!(AgentRole::Performance
        .system_prompt()
        .contains("performance"));
    assert!(AgentRole::VisualCritic
        .system_prompt()
        .contains("visual design"));
    assert!(AgentRole::General
        .system_prompt()
        .contains("general-purpose"));
}

#[test]
fn test_agent_role_priority_all_variants() {
    assert_eq!(AgentRole::Security.priority(), 10);
    assert_eq!(AgentRole::Architect.priority(), 8);
    assert_eq!(AgentRole::Reviewer.priority(), 7);
    assert_eq!(AgentRole::Tester.priority(), 6);
    assert_eq!(AgentRole::Performance.priority(), 5);
    assert_eq!(AgentRole::Coder.priority(), 4);
    assert_eq!(AgentRole::DevOps.priority(), 4);
    assert_eq!(AgentRole::VisualCritic.priority(), 6);
    assert_eq!(AgentRole::Documenter.priority(), 3);
    assert_eq!(AgentRole::General.priority(), 2);
}

#[test]
fn test_agent_creation() {
    let agent = Agent::new("TestAgent", AgentRole::Coder)
        .with_expertise("Rust")
        .with_expertise("Python");

    assert_eq!(agent.name, "TestAgent");
    assert_eq!(agent.role, AgentRole::Coder);
    assert_eq!(agent.expertise.len(), 2);
}

#[test]
fn test_agent_custom_prompt() {
    let agent = Agent::new("Test", AgentRole::General).with_prompt("Custom prompt here");
    assert_eq!(agent.system_prompt(), "Custom prompt here");
}

#[test]
fn test_agent_system_prompt_uses_role_default() {
    let agent = Agent::new("Test", AgentRole::Security);
    assert_eq!(agent.system_prompt(), AgentRole::Security.system_prompt());
}

#[test]
fn test_agent_with_model() {
    let agent = Agent::new("TestAgent", AgentRole::Coder).with_model("gpt-4");
    assert_eq!(agent.model_id, Some("gpt-4".to_string()));
}

#[test]
fn test_agent_task_completion() {
    let mut agent = Agent::new("Test", AgentRole::Coder);
    let initial_trust = agent.trust_score;

    agent.complete_task(true);
    assert!(agent.trust_score > initial_trust);
    assert_eq!(agent.tasks_completed, 1);

    agent.complete_task(false);
    assert_eq!(agent.tasks_failed, 1);
}

#[test]
fn test_agent_success_rate() {
    let mut agent = Agent::new("Test", AgentRole::Coder);
    agent.tasks_completed = 8;
    agent.tasks_failed = 2;

    assert!((agent.success_rate() - 0.8).abs() < 0.01);
}

#[test]
fn test_agent_success_rate_zero_tasks() {
    let agent = Agent::new("Test", AgentRole::Coder);
    assert!((agent.success_rate() - 1.0).abs() < f32::EPSILON);
}

#[test]
fn test_agent_trust_score_floor() {
    let mut agent = Agent::new("Test", AgentRole::Coder);
    // Drive trust down with repeated failures
    for _ in 0..20 {
        agent.complete_task(false);
    }
    // Trust should never go below the 0.05 floor
    assert!(agent.trust_score >= 0.05);
}

#[test]
fn test_agent_trust_score_ceiling() {
    let mut agent = Agent::new("Test", AgentRole::Coder);
    // Drive trust up with repeated successes
    for _ in 0..20 {
        agent.complete_task(true);
    }
    // Trust should be capped at 1.0
    assert!((agent.trust_score - 1.0).abs() < f32::EPSILON);
}

#[test]
fn test_agent_set_idle() {
    let mut agent = Agent::new("Test", AgentRole::Coder);
    agent.start_working();
    assert_eq!(agent.status, AgentStatus::Working);
    agent.set_idle();
    assert_eq!(agent.status, AgentStatus::Idle);
}

#[test]
fn test_agent_set_error() {
    let mut agent = Agent::new("Test", AgentRole::Coder);
    agent.set_error();
    assert_eq!(agent.status, AgentStatus::Error);
}

#[test]
fn test_agent_full_builder_chain() {
    let agent = Agent::new("Full", AgentRole::VisualCritic)
        .with_prompt("Custom visual critic prompt")
        .with_expertise("UI/UX")
        .with_expertise("Accessibility")
        .with_model("vision-model-v2");

    assert_eq!(agent.role, AgentRole::VisualCritic);
    assert_eq!(agent.system_prompt(), "Custom visual critic prompt");
    assert_eq!(agent.expertise.len(), 2);
    assert_eq!(agent.model_id, Some("vision-model-v2".to_string()));
}

#[test]
fn test_agent_start_working_updates_status() {
    let mut agent = Agent::new("Test", AgentRole::Coder);
    let before = agent.last_active;
    std::thread::sleep(std::time::Duration::from_millis(10));
    agent.start_working();
    assert_eq!(agent.status, AgentStatus::Working);
    assert!(agent.last_active >= before);
}

#[test]
fn test_agent_complete_task_updates_last_active() {
    let mut agent = Agent::new("Test", AgentRole::Coder);
    let before = agent.last_active;
    std::thread::sleep(std::time::Duration::from_millis(10));
    agent.complete_task(true);
    assert!(agent.last_active >= before);
}

// ============================================================================
// Vote Tests
// ============================================================================

#[test]
fn test_vote_creation() {
    let vote = Vote::new(
        "agent1",
        AgentRole::Reviewer,
        "option_a",
        0.9,
        "Good choice",
    );

    assert_eq!(vote.choice, "option_a");
    assert_eq!(vote.confidence, 0.9);
}

#[test]
fn test_vote_weighted_value() {
    let vote = Vote::new("agent1", AgentRole::Security, "opt", 1.0, "reason");
    let value = vote.weighted_value(1.0);

    // Security has priority 10, so weight = 1.0
    assert!((value - 1.0).abs() < 0.01);
}

#[test]
fn test_vote_weighted_value_general_role() {
    // General has priority 2, so role_weight = 0.2
    let vote = Vote::new("a1", AgentRole::General, "opt", 1.0, "r");
    let value = vote.weighted_value(1.0);
    assert!((value - 0.2).abs() < 0.01);
}

#[test]
fn test_vote_weighted_value_zero_trust() {
    let vote = Vote::new("a1", AgentRole::Security, "opt", 1.0, "r");
    let value = vote.weighted_value(0.0);
    assert!(value.abs() < f32::EPSILON);
}

#[test]
fn test_vote_confidence_clamped_above_1() {
    let vote = Vote::new("a1", AgentRole::Coder, "opt", 5.0, "reason");
    assert!((vote.confidence - 1.0).abs() < f32::EPSILON);
}

#[test]
fn test_vote_confidence_clamped_below_0() {
    let vote = Vote::new("a1", AgentRole::Coder, "opt", -3.0, "reason");
    assert!(vote.confidence.abs() < f32::EPSILON);
}

#[test]
fn test_vote_timestamp_set() {
    let before = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs();
    let vote = Vote::new("a1", AgentRole::Coder, "opt", 0.5, "reason");
    let after = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs();
    assert!(vote.timestamp >= before && vote.timestamp <= after);
}

// ============================================================================
// Decision Tests
// ============================================================================

#[test]
fn test_decision_creation() {
    let decision = super::types::Decision::new("Which approach?", vec!["A".into(), "B".into()]);

    assert!(decision.is_pending());
    assert_eq!(decision.options.len(), 2);
}

#[test]
fn test_decision_add_vote() {
    let mut decision = super::types::Decision::new("Test?", vec!["Yes".into(), "No".into()]);
    decision.add_vote(Vote::new("a1", AgentRole::Coder, "Yes", 0.8, "reason"));

    assert_eq!(decision.votes.len(), 1);
    assert_eq!(decision.votes_for("Yes").len(), 1);
}

#[test]
fn test_decision_resolve() {
    let mut decision = super::types::Decision::new("Test?", vec!["A".into(), "B".into()]);
    decision.add_vote(Vote::new("a1", AgentRole::Coder, "A", 0.9, "r1"));
    decision.add_vote(Vote::new("a2", AgentRole::Tester, "A", 0.8, "r2"));
    decision.add_vote(Vote::new("a3", AgentRole::Reviewer, "B", 0.5, "r3"));

    let trust_scores: HashMap<String, f32> = [
        ("a1".to_string(), 0.8),
        ("a2".to_string(), 0.7),
        ("a3".to_string(), 0.6),
    ]
    .into_iter()
    .collect();

    let outcome = decision.resolve(&trust_scores);
    assert!(outcome.is_some());
    assert_eq!(outcome.unwrap(), "A");
}

#[test]
fn test_decision_resolve_empty_options() {
    let mut decision = super::types::Decision::new("Empty?", vec![]);
    let trust: HashMap<String, f32> = HashMap::new();
    let result = decision.resolve(&trust);
    assert!(result.is_none());
}

#[test]
fn test_decision_resolve_conflict_scores_close() {
    let mut decision = super::types::Decision::new("Close?", vec!["A".into(), "B".into()]);
    // Both votes with the same role, same confidence, same trust => scores identical
    decision.add_vote(Vote::new("a1", AgentRole::Coder, "A", 0.8, "r1"));
    decision.add_vote(Vote::new("a2", AgentRole::Coder, "B", 0.8, "r2"));

    let trust: HashMap<String, f32> = [("a1".to_string(), 0.5), ("a2".to_string(), 0.5)]
        .into_iter()
        .collect();

    let result = decision.resolve(&trust);
    assert!(result.is_none());
    assert_eq!(decision.status, DecisionStatus::Conflict);
}

#[test]
fn test_decision_weighted_score_missing_trust_defaults() {
    let mut decision = super::types::Decision::new("Test?", vec!["A".into()]);
    decision.add_vote(Vote::new("unknown_agent", AgentRole::Coder, "A", 0.8, "r"));

    // Empty trust map => defaults to 0.5
    let trust: HashMap<String, f32> = HashMap::new();
    let score = decision.weighted_score("A", &trust);
    // Coder priority=4, role_weight=0.4, confidence=0.8, trust=0.5
    // 0.8 * 0.4 * 0.5 = 0.16
    assert!((score - 0.16).abs() < 0.01);
}

#[test]
fn test_decision_votes_for_empty() {
    let decision = super::types::Decision::new("Test?", vec!["A".into(), "B".into()]);
    assert!(decision.votes_for("A").is_empty());
    assert!(decision.votes_for("nonexistent").is_empty());
}

#[test]
fn test_decision_is_pending_false_when_resolved() {
    let mut decision = super::types::Decision::new("Test?", vec!["A".into()]);
    decision.add_vote(Vote::new("a1", AgentRole::Security, "A", 1.0, "r"));
    let trust: HashMap<String, f32> = [("a1".to_string(), 1.0)].into_iter().collect();

    assert!(decision.is_pending());
    decision.resolve(&trust);
    assert!(!decision.is_pending());
}

#[test]
fn test_decision_resolve_sets_resolved_at() {
    let mut decision = super::types::Decision::new("Test?", vec!["A".into()]);
    decision.add_vote(Vote::new("a1", AgentRole::Security, "A", 1.0, "r"));
    let trust: HashMap<String, f32> = [("a1".to_string(), 1.0)].into_iter().collect();

    assert!(decision.resolved_at.is_none());
    decision.resolve(&trust);
    assert!(decision.resolved_at.is_some());
}

// ============================================================================
// SharedMemory Tests
// ============================================================================

#[test]
fn test_shared_memory_write_read() {
    let mut memory = SharedMemory::new();

    memory.write("key1", "value1", "agent1");
    let value = memory.read("key1", "agent2");

    assert_eq!(value, Some("value1".to_string()));
}

#[test]
fn test_shared_memory_peek() {
    let mut memory = SharedMemory::new();
    memory.write("key1", "value1", "agent1");

    let value = memory.peek("key1");
    assert_eq!(value, Some("value1"));
}

#[test]
fn test_shared_memory_delete() {
    let mut memory = SharedMemory::new();
    memory.write("key1", "value1", "agent1");

    let deleted = memory.delete("key1", "agent1");
    assert_eq!(deleted, Some("value1".to_string()));
    assert!(memory.peek("key1").is_none());
}

#[test]
fn test_shared_memory_delete_missing_key() {
    let mut memory = SharedMemory::new();
    let result = memory.delete("nonexistent", "agent1");
    assert!(result.is_none());
}

#[test]
fn test_shared_memory_tags() {
    let mut memory = SharedMemory::new();
    memory.write("key1", "value1", "agent1");
    memory.tag("key1", "important");

    let tagged = memory.find_by_tag("important");
    assert_eq!(tagged.len(), 1);
}

#[test]
fn test_shared_memory_find_by_tag_no_match() {
    let mut memory = SharedMemory::new();
    memory.write("key1", "value1", "agent1");
    let results = memory.find_by_tag("nonexistent_tag");
    assert!(results.is_empty());
}

#[test]
fn test_shared_memory_tag_nonexistent_key() {
    let mut memory = SharedMemory::new();
    memory.tag("nonexistent", "sometag"); // should not panic
    assert!(memory.find_by_tag("sometag").is_empty());
}

#[test]
fn test_shared_memory_access_log() {
    let mut memory = SharedMemory::new();
    memory.write("key1", "value1", "agent1");
    memory.read("key1", "agent2");

    assert_eq!(memory.access_log().len(), 2);
}

#[test]
fn test_shared_memory_write_update_existing() {
    let mut memory = SharedMemory::new();
    memory.write("key1", "value1", "agent1");
    memory.write("key1", "value2", "agent2");

    assert_eq!(memory.peek("key1"), Some("value2"));

    // Verify modified_by is set
    let entries = memory.entries();
    let entry = entries.iter().find(|e| e.key == "key1").unwrap();
    assert_eq!(entry.modified_by, Some("agent2".to_string()));
    assert!(entry.modified_at.is_some());
}

#[test]
fn test_shared_memory_read_missing_key() {
    let mut memory = SharedMemory::new();
    let result = memory.read("nonexistent", "agent1");
    assert!(result.is_none());
}

#[test]
fn test_shared_memory_read_increments_access_count() {
    let mut memory = SharedMemory::new();
    memory.write("key1", "val", "agent1");

    memory.read("key1", "agent2");
    memory.read("key1", "agent3");
    memory.read("key1", "agent2");

    let entries = memory.entries();
    let entry = entries.iter().find(|e| e.key == "key1").unwrap();
    assert_eq!(entry.access_count, 3);
}

#[test]
fn test_shared_memory_keys() {
    let mut memory = SharedMemory::new();
    memory.write("k1", "v1", "a1");
    memory.write("k2", "v2", "a1");

    let keys = memory.keys();
    assert_eq!(keys.len(), 2);
}

#[test]
fn test_shared_memory_entries() {
    let mut memory = SharedMemory::new();
    memory.write("k1", "v1", "a1");
    memory.write("k2", "v2", "a1");
    memory.write("k3", "v3", "a2");

    let entries = memory.entries();
    assert_eq!(entries.len(), 3);
}

#[test]
fn test_shared_memory_clear() {
    let mut memory = SharedMemory::new();
    memory.write("k1", "v1", "a1");
    memory.clear();

    assert!(memory.keys().is_empty());
    assert!(memory.access_log().is_empty());
}

#[test]
fn test_shared_memory_multiple_tags() {
    let mut memory = SharedMemory::new();
    memory.write("key1", "value1", "agent1");
    memory.tag("key1", "tag1");
    memory.tag("key1", "tag2");

    let entries1 = memory.find_by_tag("tag1");
    let entries2 = memory.find_by_tag("tag2");
    assert_eq!(entries1.len(), 1);
    assert_eq!(entries2.len(), 1);
}

#[test]
fn test_shared_memory_default() {
    let memory: SharedMemory = Default::default();
    assert!(memory.keys().is_empty());
}

// ============================================================================
// Swarm Tests
// ============================================================================

#[test]
fn test_swarm_creation() {
    let swarm = Swarm::new();
    assert_eq!(swarm.list_agents().len(), 0);
}

#[test]
fn test_swarm_default() {
    let swarm = Swarm::default();
    assert_eq!(swarm.list_agents().len(), 0);
}

#[test]
fn test_swarm_add_agent() {
    let mut swarm = Swarm::new();
    let agent = Agent::new("Test", AgentRole::Coder);
    let id = swarm.add_agent(agent);

    assert!(swarm.get_agent(&id).is_some());
}

#[test]
fn test_swarm_remove_agent() {
    let mut swarm = Swarm::new();
    let agent = Agent::new("Test", AgentRole::Coder);
    let id = swarm.add_agent(agent);

    let removed = swarm.remove_agent(&id);
    assert!(removed.is_some());
    assert!(swarm.get_agent(&id).is_none());
}

#[test]
fn test_swarm_remove_agent_not_found() {
    let mut swarm = Swarm::new();
    assert!(swarm.remove_agent("nonexistent").is_none());
}

#[test]
fn test_swarm_agents_by_role() {
    let mut swarm = Swarm::new();
    swarm.add_agent(Agent::new("C1", AgentRole::Coder));
    swarm.add_agent(Agent::new("C2", AgentRole::Coder));
    swarm.add_agent(Agent::new("T1", AgentRole::Tester));

    assert_eq!(swarm.agents_by_role(AgentRole::Coder).len(), 2);
    assert_eq!(swarm.agents_by_role(AgentRole::Tester).len(), 1);
}

#[test]
fn test_swarm_idle_agents() {
    let mut swarm = Swarm::new();
    let id1 = swarm.add_agent(Agent::new("A1", AgentRole::Coder));
    swarm.add_agent(Agent::new("A2", AgentRole::Coder));

    swarm.get_agent_mut(&id1).unwrap().start_working();

    assert_eq!(swarm.idle_agents().len(), 1);
}

#[test]
fn test_swarm_create_decision() {
    let mut swarm = Swarm::new();
    let decision_id = swarm.create_decision("Which?", vec!["A".into(), "B".into()]);

    assert!(!decision_id.is_empty());
}

#[test]
fn test_swarm_vote() {
    let mut swarm = Swarm::new();
    let agent_id = swarm.add_agent(Agent::new("Test", AgentRole::Coder));
    let decision_id = swarm.create_decision("Which?", vec!["A".into(), "B".into()]);

    let result = swarm.vote(&decision_id, &agent_id, "A", 0.9, "Looks good");
    assert!(result.is_ok());
}

#[test]
fn test_swarm_vote_agent_not_found() {
    let mut swarm = Swarm::new();
    let decision_id = swarm.create_decision("Q?", vec!["A".into()]);

    let result = swarm.vote(&decision_id, "nonexistent_agent", "A", 0.9, "r");
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Agent not found"));
}

#[test]
fn test_swarm_vote_decision_not_found() {
    let mut swarm = Swarm::new();
    let agent_id = swarm.add_agent(Agent::new("A", AgentRole::Coder));

    let result = swarm.vote("nonexistent_decision", &agent_id, "A", 0.9, "r");
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Decision not found"));
}

#[test]
fn test_swarm_vote_decision_already_resolved() {
    let mut swarm = Swarm::new();
    let a1 = swarm.add_agent(Agent::new("A1", AgentRole::Coder));
    let a2 = swarm.add_agent(Agent::new("A2", AgentRole::Tester));
    let id = swarm.create_decision("Q?", vec!["X".into()]);
    swarm.vote(&id, &a1, "X", 0.9, "r").unwrap();
    swarm.resolve_decision(&id).unwrap();

    let result = swarm.vote(&id, &a2, "X", 0.8, "r2");
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("already resolved"));
}

#[test]
fn test_swarm_resolve_decision() {
    let mut swarm = Swarm::new();
    let a1 = swarm.add_agent(Agent::new("A1", AgentRole::Architect));
    let a2 = swarm.add_agent(Agent::new("A2", AgentRole::Coder));

    let decision_id = swarm.create_decision("Which?", vec!["X".into(), "Y".into()]);

    swarm.vote(&decision_id, &a1, "X", 0.9, "r1").unwrap();
    swarm.vote(&decision_id, &a2, "X", 0.8, "r2").unwrap();

    let outcome = swarm.resolve_decision(&decision_id).unwrap();
    assert_eq!(outcome, Some("X".to_string()));
}

#[test]
fn test_swarm_resolve_decision_not_found() {
    let mut swarm = Swarm::new();
    let result = swarm.resolve_decision("nonexistent");
    assert!(result.is_err());
}

#[test]
fn test_swarm_list_decisions() {
    let mut swarm = Swarm::new();
    swarm.create_decision("Q1?", vec!["A".into()]);
    swarm.create_decision("Q2?", vec!["B".into()]);

    assert_eq!(swarm.list_decisions().len(), 2);
}

#[test]
fn test_swarm_get_decision_found() {
    let mut swarm = Swarm::new();
    let id = swarm.create_decision("Q?", vec!["A".into()]);
    assert!(swarm.get_decision(&id).is_some());
}

#[test]
fn test_swarm_get_decision_not_found() {
    let swarm = Swarm::new();
    assert!(swarm.get_decision("nonexistent").is_none());
}

#[test]
fn test_swarm_list_tasks() {
    let mut swarm = Swarm::new();
    swarm.queue_task(SwarmTask::new("T1")).unwrap();
    swarm.queue_task(SwarmTask::new("T2")).unwrap();

    assert_eq!(swarm.list_tasks().len(), 2);
}

#[test]
fn test_swarm_get_task_found() {
    let mut swarm = Swarm::new();
    let task = SwarmTask::new("Find me");
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    assert!(swarm.get_task(&task_id).is_some());
}

#[test]
fn test_swarm_get_task_not_found() {
    let swarm = Swarm::new();
    assert!(swarm.get_task("nonexistent").is_none());
}

#[test]
fn test_swarm_queue_task() {
    let mut swarm = Swarm::new();

    swarm
        .queue_task(SwarmTask::new("Task 1").with_priority(5))
        .unwrap();
    swarm
        .queue_task(SwarmTask::new("Task 2").with_priority(8))
        .unwrap();

    // Higher priority should come first
    let task_id = swarm.next_task().unwrap();
    let task = swarm.get_task(&task_id).unwrap();
    assert_eq!(task.priority, 8);
}

#[test]
fn test_swarm_queue_task_ordering() {
    let mut swarm = Swarm::new();
    swarm
        .queue_task(SwarmTask::new("Low").with_priority(1))
        .unwrap();
    swarm
        .queue_task(SwarmTask::new("High").with_priority(10))
        .unwrap();
    swarm
        .queue_task(SwarmTask::new("Mid").with_priority(5))
        .unwrap();

    // pop() returns from the end; sorted ascending, so highest is last
    let t1_id = swarm.next_task().unwrap();
    let t1 = swarm.get_task(&t1_id).unwrap();
    assert_eq!(t1.priority, 10);
    let t2_id = swarm.next_task().unwrap();
    let t2 = swarm.get_task(&t2_id).unwrap();
    assert_eq!(t2.priority, 5);
    let t3_id = swarm.next_task().unwrap();
    let t3 = swarm.get_task(&t3_id).unwrap();
    assert_eq!(t3.priority, 1);
}

#[test]
fn test_swarm_next_task_empty() {
    let mut swarm = Swarm::new();
    assert!(swarm.next_task().is_none());
}

#[test]
fn test_swarm_assign_task() {
    let mut swarm = Swarm::new();
    let coder_id = swarm.add_agent(Agent::new("Cody", AgentRole::Coder));
    let tester_id = swarm.add_agent(Agent::new("Tessa", AgentRole::Tester));

    let task = SwarmTask::new("Build it")
        .with_role(AgentRole::Coder)
        .with_role(AgentRole::Tester);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    let assigned = swarm.assign_task(&task_id);
    assert_eq!(assigned.len(), 2);
    assert!(assigned.contains(&coder_id));
    assert!(assigned.contains(&tester_id));

    // Assigned agents should now be Working
    assert_eq!(
        swarm.get_agent(&coder_id).unwrap().status,
        AgentStatus::Working
    );
    assert_eq!(
        swarm.get_agent(&tester_id).unwrap().status,
        AgentStatus::Working
    );

    // Task should be InProgress
    let task = swarm.get_task(&task_id).unwrap();
    assert_eq!(task.status, TaskStatus::InProgress);
}

#[test]
fn test_swarm_assign_task_not_found() {
    let mut swarm = Swarm::new();
    let assigned = swarm.assign_task("nonexistent");
    assert!(assigned.is_empty());
}

#[test]
fn test_swarm_assign_task_no_idle_agents() {
    let mut swarm = Swarm::new();
    let coder_id = swarm.add_agent(Agent::new("Cody", AgentRole::Coder));
    swarm.get_agent_mut(&coder_id).unwrap().start_working();

    let task = SwarmTask::new("Build it").with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    let assigned = swarm.assign_task(&task_id);
    assert!(assigned.is_empty());
}

#[test]
fn test_swarm_complete_task() {
    let mut swarm = Swarm::new();
    let coder_id = swarm.add_agent(Agent::new("Cody", AgentRole::Coder));

    let task = SwarmTask::new("Build it").with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    let assigned = swarm.assign_task(&task_id);
    assert_eq!(assigned.len(), 1);

    swarm.complete_task(&task_id, &coder_id, "Done!", true);

    // Task should be Completed since all assigned agents submitted
    let task = swarm.get_task(&task_id).unwrap();
    assert_eq!(task.status, TaskStatus::Completed);
    assert_eq!(task.results.get(&coder_id).unwrap(), "Done!");

    // Agent bookkeeping is updated and the agent returns to Idle so it
    // can be assigned to subsequent tasks.
    let agent = swarm.get_agent(&coder_id).unwrap();
    assert_eq!(agent.status, AgentStatus::Idle);
    assert_eq!(agent.tasks_completed, 1);
}

#[test]
fn test_swarm_complete_task_partial() {
    let mut swarm = Swarm::new();
    let c1 = swarm.add_agent(Agent::new("C1", AgentRole::Coder));
    let c2 = swarm.add_agent(Agent::new("C2", AgentRole::Coder));

    let task = SwarmTask::new("Build it")
        .with_role(AgentRole::Coder)
        .with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();
    swarm.assign_task(&task_id);

    // Only one agent completes
    swarm.complete_task(&task_id, &c1, "Partial", true);

    let task = swarm.get_task(&task_id).unwrap();
    // Not all agents submitted, so task should still be InProgress
    assert_eq!(task.status, TaskStatus::InProgress);

    // Second agent completes
    swarm.complete_task(&task_id, &c2, "Full", true);
    let task = swarm.get_task(&task_id).unwrap();
    assert_eq!(task.status, TaskStatus::Completed);
}

#[test]
fn test_swarm_complete_task_not_found() {
    let mut swarm = Swarm::new();
    let coder_id = swarm.add_agent(Agent::new("Cody", AgentRole::Coder));
    // Completing a nonexistent task should not panic
    swarm.complete_task("nonexistent", &coder_id, "result", true);
}

#[test]
fn test_swarm_complete_task_returns_agent_to_idle_for_reassignment() {
    // Regression: agents must return to Idle after completing a task,
    // otherwise every task after the first assigns 0 agents and tasks
    // stay Pending forever.
    let mut swarm = Swarm::new();
    let coder_id = swarm.add_agent(Agent::new("Cody", AgentRole::Coder));

    // First task runs to completion.
    let task1 = SwarmTask::new("Task 1").with_role(AgentRole::Coder);
    let task1_id = task1.id.clone();
    swarm.queue_task(task1).unwrap();
    let assigned1 = swarm.assign_task(&task1_id);
    assert_eq!(assigned1, vec![coder_id.clone()]);
    swarm.complete_task(&task1_id, &coder_id, "done", true);
    assert_eq!(
        swarm.get_agent(&coder_id).unwrap().status,
        AgentStatus::Idle
    );

    // The same agent must be assignable again for the next task.
    let task2 = SwarmTask::new("Task 2").with_role(AgentRole::Coder);
    let task2_id = task2.id.clone();
    swarm.queue_task(task2).unwrap();
    let assigned2 = swarm.assign_task(&task2_id);
    assert_eq!(
        assigned2,
        vec![coder_id.clone()],
        "agent must be assignable for the next task after completing one"
    );
}

#[test]
fn test_swarm_complete_task_failure_records_failure() {
    // The real per-agent success flag must drive trust and failure counters
    // (previously hardcoded `true`, so `tasks_failed` could never increment).
    let mut swarm = Swarm::new();
    let coder_id = swarm.add_agent(Agent::new("Cody", AgentRole::Coder));
    let initial_trust = swarm.get_agent(&coder_id).unwrap().trust_score;

    let task = SwarmTask::new("Task").with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();
    swarm.assign_task(&task_id);
    swarm.complete_task(&task_id, &coder_id, "boom", false);

    let agent = swarm.get_agent(&coder_id).unwrap();
    assert_eq!(agent.tasks_completed, 0);
    assert_eq!(agent.tasks_failed, 1);
    assert!(agent.trust_score < initial_trust);
    // Even a failed agent returns to the idle pool for the next task.
    assert_eq!(agent.status, AgentStatus::Idle);
}

#[test]
fn test_swarm_fail_task_settles_status_and_releases_agents() {
    let mut swarm = Swarm::new();
    let coder_id = swarm.add_agent(Agent::new("Cody", AgentRole::Coder));

    let task = SwarmTask::new("Task").with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();
    swarm.assign_task(&task_id);
    assert_eq!(
        swarm.get_agent(&coder_id).unwrap().status,
        AgentStatus::Working
    );

    swarm.fail_task(&task_id);

    assert_eq!(swarm.get_task(&task_id).unwrap().status, TaskStatus::Failed);
    assert_eq!(
        swarm.get_agent(&coder_id).unwrap().status,
        AgentStatus::Idle
    );
}

#[test]
fn test_swarm_fail_task_unknown_task_does_not_panic() {
    let mut swarm = Swarm::new();
    swarm.fail_task("nonexistent");
}

#[test]
fn test_swarm_stats_empty() {
    let swarm = Swarm::new();
    let stats = swarm.stats();
    assert_eq!(stats.total_agents, 0);
    assert!(stats.average_trust.abs() < f32::EPSILON);
    assert_eq!(stats.pending_decisions, 0);
    assert_eq!(stats.queued_tasks, 0);
}

#[test]
fn test_swarm_stats_detailed() {
    let mut swarm = Swarm::new();
    swarm.add_agent(Agent::new("A1", AgentRole::Coder));
    swarm.add_agent(Agent::new("A2", AgentRole::Coder));
    swarm.add_agent(Agent::new("A3", AgentRole::Tester));
    swarm.create_decision("Q?", vec!["A".into()]);
    swarm.queue_task(SwarmTask::new("T1")).unwrap();

    let stats = swarm.stats();
    assert_eq!(stats.total_agents, 3);
    assert_eq!(*stats.agents_by_role.get(&AgentRole::Coder).unwrap(), 2);
    assert_eq!(*stats.agents_by_role.get(&AgentRole::Tester).unwrap(), 1);
    assert_eq!(stats.pending_decisions, 1);
    assert_eq!(stats.queued_tasks, 1);
    // All agents start with 0.5 trust, so average = 0.5
    assert!((stats.average_trust - 0.5).abs() < 0.01);
}

#[test]
fn test_swarm_memory_shared() {
    let swarm = Swarm::new();
    let m1 = swarm.memory();
    let m2 = swarm.memory();
    // Both should point to the same allocation
    assert!(Arc::ptr_eq(&m1, &m2));
}

#[test]
fn test_swarm_with_settings() {
    let swarm = Swarm::new()
        .with_conflict_strategy(ConflictStrategy::MajorityWins)
        .with_consensus_threshold(0.7);

    // Verify settings were applied through behavior
    let thresholded = Swarm::new()
        .with_consensus_threshold(0.5)
        .with_consensus_threshold(2.0);
    let _ = thresholded; // Threshold clamping verified in other tests
    let _ = swarm;
}

#[test]
fn test_swarm_consensus_threshold_clamping() {
    // Test that threshold values are clamped to 0.0-1.0 range
    let swarm = Swarm::new().with_consensus_threshold(2.0);
    let _ = swarm;

    let swarm2 = Swarm::new().with_consensus_threshold(-1.0);
    let _ = swarm2;

    // Verify through behavior - these shouldn't panic
}

#[test]
fn test_conflict_strategy_default() {
    assert_eq!(ConflictStrategy::default(), ConflictStrategy::PriorityWins);
}

#[test]
fn test_resolve_conflict_priority_wins() {
    let mut swarm = Swarm::new().with_conflict_strategy(ConflictStrategy::PriorityWins);

    let sec_id = swarm.add_agent(Agent::new("Sec", AgentRole::Security));
    let cod_id = swarm.add_agent(Agent::new("Cod", AgentRole::Coder));

    let did = swarm.create_decision("Strategy?", vec!["A".into(), "B".into()]);
    swarm.vote(&did, &sec_id, "A", 0.8, "r").unwrap();
    swarm.vote(&did, &cod_id, "B", 0.8, "r").unwrap();

    // Force conflict status through decision resolution
    swarm.resolve_decision(&did).ok();

    let result = swarm.resolve_conflict(&did).unwrap();
    // Result may vary based on decision state
    assert!(result.is_some() || result.is_none());
}

#[test]
fn test_resolve_conflict_confidence_wins() {
    let mut swarm = Swarm::new().with_conflict_strategy(ConflictStrategy::ConfidenceWins);

    let a1 = swarm.add_agent(Agent::new("A1", AgentRole::Coder));
    let a2 = swarm.add_agent(Agent::new("A2", AgentRole::Coder));

    let did = swarm.create_decision("Pick?", vec!["X".into(), "Y".into()]);
    swarm.vote(&did, &a1, "X", 0.6, "r").unwrap();
    swarm.vote(&did, &a2, "Y", 0.95, "r").unwrap();

    let result = swarm.resolve_conflict(&did).unwrap();
    // If not in conflict, returns current outcome
    assert!(result.is_some() || result.is_none());
}

#[test]
fn test_resolve_conflict_majority_wins() {
    let mut swarm = Swarm::new().with_conflict_strategy(ConflictStrategy::MajorityWins);

    let a1 = swarm.add_agent(Agent::new("A1", AgentRole::Coder));
    let a2 = swarm.add_agent(Agent::new("A2", AgentRole::Coder));
    let a3 = swarm.add_agent(Agent::new("A3", AgentRole::Coder));

    let did = swarm.create_decision("Vote?", vec!["A".into(), "B".into()]);
    swarm.vote(&did, &a1, "A", 0.8, "r").unwrap();
    swarm.vote(&did, &a2, "A", 0.7, "r").unwrap();
    swarm.vote(&did, &a3, "B", 0.9, "r").unwrap();

    let result = swarm.resolve_conflict(&did).unwrap();
    // Result depends on decision state
    let _ = result;
}

#[test]
fn test_resolve_conflict_human_intervention() {
    let mut swarm = Swarm::new().with_conflict_strategy(ConflictStrategy::HumanIntervention);

    let a1 = swarm.add_agent(Agent::new("A1", AgentRole::Coder));

    let did = swarm.create_decision("Help?", vec!["A".into()]);
    swarm.vote(&did, &a1, "A", 0.8, "r").unwrap();

    let result = swarm.resolve_conflict(&did).unwrap();
    // If not in conflict, returns outcome; if in conflict with HumanIntervention, returns None
    let _ = result;
}

#[test]
fn test_resolve_conflict_accept_all() {
    let mut swarm = Swarm::new().with_conflict_strategy(ConflictStrategy::AcceptAll);

    let a1 = swarm.add_agent(Agent::new("A1", AgentRole::Coder));
    let a2 = swarm.add_agent(Agent::new("A2", AgentRole::Tester));

    let did = swarm.create_decision("Merge?", vec!["X".into(), "Y".into()]);
    swarm.vote(&did, &a1, "X", 0.8, "r").unwrap();
    swarm.vote(&did, &a2, "Y", 0.7, "r").unwrap();

    let result = swarm.resolve_conflict(&did).unwrap();
    // If in conflict, combines all unique choices
    let _ = result;
}

#[test]
fn test_resolve_conflict_not_in_conflict_returns_outcome() {
    let mut swarm = Swarm::new();
    let a1 = swarm.add_agent(Agent::new("A1", AgentRole::Coder));
    let did = swarm.create_decision("Q?", vec!["A".into()]);
    swarm.vote(&did, &a1, "A", 0.9, "r").unwrap();
    swarm.resolve_decision(&did).unwrap();

    // Decision is Resolved, not Conflict
    let result = swarm.resolve_conflict(&did).unwrap();
    assert_eq!(result, Some("A".to_string()));
}

#[test]
fn test_resolve_conflict_decision_not_found() {
    let mut swarm = Swarm::new();
    let result = swarm.resolve_conflict("nonexistent");
    assert!(result.is_err());
}

#[test]
fn test_resolve_conflict_updates_decision_state() {
    // Verify that resolve_conflict actually mutates the decision's
    // status and outcome (not just computing and discarding the result).
    let mut swarm = Swarm::new()
        .with_conflict_strategy(ConflictStrategy::PriorityWins)
        .with_consensus_threshold(0.9); // high threshold to force Conflict

    let sec_id = swarm.add_agent(Agent::new("Sec", AgentRole::Security));
    let cod_id = swarm.add_agent(Agent::new("Cod", AgentRole::Coder));

    let did = swarm.create_decision("Strategy?", vec!["A".into(), "B".into()]);
    swarm.vote(&did, &sec_id, "A", 0.8, "r").unwrap();
    swarm.vote(&did, &cod_id, "B", 0.8, "r").unwrap();

    // This should produce a Conflict (scores are close, threshold is 0.9)
    swarm.resolve_decision(&did).ok();
    assert_eq!(
        swarm.get_decision(&did).unwrap().status,
        DecisionStatus::Conflict
    );

    // Now resolve the conflict — Security has higher priority, so "A" wins.
    let result = swarm.resolve_conflict(&did).unwrap();
    assert_eq!(result, Some("A".to_string()));

    // The decision's state must be updated to reflect the resolution.
    let decision = swarm.get_decision(&did).unwrap();
    assert_eq!(decision.status, DecisionStatus::Resolved);
    assert_eq!(decision.outcome, Some("A".to_string()));
    assert!(decision.resolved_at.is_some());
}

// ============================================================================
// Swarm Factory Tests
// ============================================================================

#[test]
fn test_create_dev_swarm() {
    let swarm = super::factory::create_dev_swarm();
    assert_eq!(swarm.list_agents().len(), 4);
}

#[test]
fn test_create_dev_swarm_has_all_roles() {
    let swarm = super::factory::create_dev_swarm();
    assert_eq!(swarm.agents_by_role(AgentRole::Architect).len(), 1);
    assert_eq!(swarm.agents_by_role(AgentRole::Coder).len(), 1);
    assert_eq!(swarm.agents_by_role(AgentRole::Tester).len(), 1);
    assert_eq!(swarm.agents_by_role(AgentRole::Reviewer).len(), 1);
}

// ============================================================================
// Coordinator Routing Tests
// ============================================================================
//
// These tests verify the swarm coordinator flow that `--coordinator` activates:
// queue_task → next_task → assign_task → complete_task → consensus decision.

#[test]
fn test_coordinator_full_flow_queue_assign_complete_consensus() {
    // Build a dev swarm (Architect, Coder, Tester, Reviewer).
    let mut swarm = super::factory::create_dev_swarm()
        .with_conflict_strategy(ConflictStrategy::ConfidenceWins)
        .with_consensus_threshold(0.5);

    // 1. Queue a task requiring all four roles.
    let mut task = SwarmTask::new("Implement feature X with tests and review");
    task = task.with_role(AgentRole::Architect);
    task = task.with_role(AgentRole::Coder);
    task = task.with_role(AgentRole::Tester);
    task = task.with_role(AgentRole::Reviewer);
    swarm.queue_task(task).expect("queue_task should succeed");

    assert_eq!(swarm.list_tasks().len(), 1);

    // 2. Pop the next task.
    let task_id = swarm
        .next_task()
        .expect("next_task should return a task id");
    assert!(swarm.get_task(&task_id).is_some());

    // 3. Assign the task to role-matched idle agents.
    let assigned = swarm.assign_task(&task_id);
    assert_eq!(
        assigned.len(),
        4,
        "all four roles should be assigned to idle agents"
    );

    // Verify all assigned agents are now Working.
    for agent_id in &assigned {
        let agent = swarm.get_agent(agent_id).expect("agent should exist");
        assert_eq!(agent.status, AgentStatus::Working);
    }

    // 4. Complete the task for each agent with a result.
    for agent_id in &assigned {
        swarm.complete_task(
            &task_id,
            agent_id,
            format!("Result from agent {}", agent_id),
            true,
        );
    }

    // Verify the task is now Completed.
    let task = swarm.get_task(&task_id).expect("task should exist");
    assert_eq!(task.status, TaskStatus::Completed);
    assert_eq!(task.results.len(), 4);

    // 5. Create a consensus decision and have agents vote.
    let agent_names: Vec<String> = assigned
        .iter()
        .map(|id| swarm.get_agent(id).unwrap().name.clone())
        .collect();

    let decision_id = swarm.create_decision(
        "Which agent's response best addresses the task?",
        agent_names.clone(),
    );

    // Each agent votes for a different option with varying confidence.
    for (i, agent_id) in assigned.iter().enumerate() {
        let choice = &agent_names[i];
        let confidence = 0.5 + (i as f32 * 0.1); // 0.5, 0.6, 0.7, 0.8
        swarm
            .vote(
                &decision_id,
                agent_id,
                choice.clone(),
                confidence,
                "reasoning",
            )
            .expect("vote should succeed");
    }

    // 6. Resolve the decision. With 4 agents voting for 4 different options,
    //    the consensus threshold (0.5) may not be reached, resulting in a
    //    Conflict. In that case, resolve_conflict applies the ConfidenceWins
    //    strategy to pick the highest-confidence vote.
    let outcome = swarm
        .resolve_decision(&decision_id)
        .expect("resolve_decision should not error");

    let decision = swarm.get_decision(&decision_id).expect("decision exists");

    let final_outcome = if decision.status == DecisionStatus::Conflict {
        // Conflict — use the conflict resolution strategy (ConfidenceWins).
        swarm
            .resolve_conflict(&decision_id)
            .expect("resolve_conflict should not error")
    } else {
        outcome
    };

    assert!(
        final_outcome.is_some(),
        "decision should have an outcome after resolution"
    );

    let decision = swarm.get_decision(&decision_id).expect("decision exists");
    assert!(
        decision.status == DecisionStatus::Resolved || decision.status == DecisionStatus::Conflict,
        "decision should be Resolved or Conflict after resolution attempt"
    );
}

#[test]
fn test_coordinator_assign_no_idle_agents() {
    let mut swarm = Swarm::new();
    // No agents in the swarm — assign should return empty.
    let mut task = SwarmTask::new("Test task");
    task = task.with_role(AgentRole::Coder);
    swarm.queue_task(task).unwrap();
    let task_id = swarm.next_task().expect("should get task");
    let assigned = swarm.assign_task(&task_id);
    assert!(assigned.is_empty(), "no agents should be assigned");
}

#[test]
fn test_coordinator_flag_selects_swarm_path() {
    // This test verifies the logic that determines whether the coordinator
    // (swarm) path is taken. The `--coordinator` flag maps to `coordinator: bool`
    // on the Cli struct. When true, interactive_swarm() is called instead of
    // interactive(). Here we verify the swarm infrastructure works correctly
    // so the routing is meaningful.

    let swarm = super::factory::create_dev_swarm();

    // Verify the swarm has the expected agents for coordinator mode.
    assert_eq!(
        swarm.list_agents().len(),
        4,
        "dev swarm should have 4 agents"
    );
    assert!(!swarm.agents_by_role(AgentRole::Architect).is_empty());
    assert!(!swarm.agents_by_role(AgentRole::Coder).is_empty());
    assert!(!swarm.agents_by_role(AgentRole::Tester).is_empty());
    assert!(!swarm.agents_by_role(AgentRole::Reviewer).is_empty());

    // All agents should start Idle (ready for assignment).
    for agent in swarm.list_agents() {
        assert_eq!(agent.status, AgentStatus::Idle);
    }
}

// ============================================================================
// Decision Timeout Tests
// ============================================================================

#[test]
fn test_sweep_no_timeouts() {
    let mut swarm = Swarm::new();
    swarm.create_decision("Fresh?", vec!["A".into(), "B".into()]);

    let timed_out = swarm.sweep_timed_out_decisions();
    assert!(timed_out.is_empty());
}

#[test]
fn test_sweep_marks_old_pending() {
    let mut swarm = Swarm::new().with_decision_timeout(0); // instant timeout
    let id = swarm.create_decision("Old?", vec!["A".into(), "B".into()]);

    let timed_out = swarm.sweep_timed_out_decisions();
    assert_eq!(timed_out, vec![id.clone()]);

    let d = swarm.get_decision(&id).unwrap();
    assert_eq!(d.status, DecisionStatus::TimedOut);
    assert!(d.resolved_at.is_some());
}

#[test]
fn test_sweep_skips_resolved() {
    let mut swarm = Swarm::new().with_decision_timeout(0);
    let a1 = swarm.add_agent(Agent::new("A1", AgentRole::Coder));
    let id = swarm.create_decision("Resolved?", vec!["X".into()]);
    swarm.vote(&id, &a1, "X", 0.9, "r").unwrap();
    swarm.resolve_decision(&id).unwrap();

    let timed_out = swarm.sweep_timed_out_decisions();
    assert!(timed_out.is_empty());
}

#[test]
fn test_custom_decision_timeout() {
    let swarm = Swarm::new().with_decision_timeout(600);
    // Verify through sweep behavior with timeout
    let mut swarm2 = Swarm::new().with_decision_timeout(600);
    let id = swarm2.create_decision("Test?", vec!["A".into()]);
    // With 600s timeout, should not timeout immediately
    let timed_out = swarm2.sweep_timed_out_decisions();
    assert!(timed_out.is_empty());
    let _ = id;
    let _ = swarm;
}

// ============================================================================
// Resource Gating Tests
// ============================================================================

#[test]
fn test_queue_task_no_pressure() {
    let mut swarm = Swarm::new();
    let result = swarm.queue_task(SwarmTask::new("Test task"));
    assert!(result.is_ok());
}

#[test]
fn test_queue_task_high_pressure_rejected() {
    use crate::resource::ResourcePressure;

    let pressure = Arc::new(std::sync::RwLock::new(ResourcePressure::High));
    let mut swarm = Swarm::new();
    swarm.set_resource_pressure(Arc::clone(&pressure));

    let result = swarm.queue_task(SwarmTask::new("Blocked task"));
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("resource pressure"));
}

#[test]
fn test_queue_task_critical_pressure_rejected() {
    use crate::resource::ResourcePressure;

    let pressure = Arc::new(std::sync::RwLock::new(ResourcePressure::Critical));
    let mut swarm = Swarm::new();
    swarm.set_resource_pressure(Arc::clone(&pressure));

    let result = swarm.queue_task(SwarmTask::new("Blocked task"));
    assert!(result.is_err());
}

#[test]
fn test_queue_task_low_pressure_allowed() {
    use crate::resource::ResourcePressure;

    let pressure = Arc::new(std::sync::RwLock::new(ResourcePressure::Low));
    let mut swarm = Swarm::new();
    swarm.set_resource_pressure(Arc::clone(&pressure));

    let result = swarm.queue_task(SwarmTask::new("Allowed task"));
    assert!(result.is_ok());
}

#[test]
fn test_queue_task_medium_allowed() {
    use crate::resource::ResourcePressure;

    let pressure = Arc::new(std::sync::RwLock::new(ResourcePressure::Medium));
    let mut swarm = Swarm::new();
    swarm.set_resource_pressure(Arc::clone(&pressure));

    let result = swarm.queue_task(SwarmTask::new("Medium pressure task"));
    assert!(result.is_ok());
}

#[test]
fn test_queue_task_none_allowed() {
    use crate::resource::ResourcePressure;

    let pressure = Arc::new(std::sync::RwLock::new(ResourcePressure::None));
    let mut swarm = Swarm::new();
    swarm.set_resource_pressure(Arc::clone(&pressure));

    let result = swarm.queue_task(SwarmTask::new("No pressure task"));
    assert!(result.is_ok());
}

// ============================================================================
// Swarm Coordinator Advanced Tests
// ============================================================================

#[test]
fn test_swarm_agent_spawning_lifecycle() {
    let mut swarm = Swarm::new();

    // Create agents with different roles
    let architect = Agent::new("Archie", AgentRole::Architect);
    let coder = Agent::new("Cody", AgentRole::Coder);
    let tester = Agent::new("Tessa", AgentRole::Tester);

    let arch_id = swarm.add_agent(architect);
    let cod_id = swarm.add_agent(coder);
    let test_id = swarm.add_agent(tester);

    // Verify all agents created
    assert_eq!(swarm.list_agents().len(), 3);

    // Verify each agent can be retrieved
    assert!(swarm.get_agent(&arch_id).is_some());
    assert!(swarm.get_agent(&cod_id).is_some());
    assert!(swarm.get_agent(&test_id).is_some());

    // Verify initial status
    assert_eq!(swarm.get_agent(&arch_id).unwrap().status, AgentStatus::Idle);

    // Test lifecycle: Idle -> Working -> Completed
    swarm.get_agent_mut(&arch_id).unwrap().start_working();
    assert_eq!(
        swarm.get_agent(&arch_id).unwrap().status,
        AgentStatus::Working
    );

    swarm.get_agent_mut(&arch_id).unwrap().complete_task(true);
    assert_eq!(
        swarm.get_agent(&arch_id).unwrap().status,
        AgentStatus::Completed
    );
    assert_eq!(swarm.get_agent(&arch_id).unwrap().tasks_completed, 1);

    // Remove agent
    let removed = swarm.remove_agent(&arch_id);
    assert!(removed.is_some());
    assert_eq!(swarm.list_agents().len(), 2);
}

#[test]
fn test_swarm_task_distribution_with_trust() {
    let mut swarm = Swarm::new();

    // Create two coders with different trust scores
    let coder1 = Agent::new("Coder1", AgentRole::Coder);
    let coder2 = Agent::new("Coder2", AgentRole::Coder);

    let _id1 = swarm.add_agent(coder1);
    let id2 = swarm.add_agent(coder2);

    // Give coder2 higher trust through successful tasks
    for _ in 0..5 {
        swarm.get_agent_mut(&id2).unwrap().complete_task(true);
    }

    // Reset status to idle for task assignment
    swarm.get_agent_mut(&id2).unwrap().set_idle();

    // Create task requiring coder
    let task = SwarmTask::new("Important task").with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    // Assign task
    let assigned = swarm.assign_task(&task_id);

    // Should assign to one of the idle agents
    assert!(!assigned.is_empty());
}

#[test]
fn test_swarm_task_distribution_multiple_roles() {
    let mut swarm = Swarm::new();

    // Create agents for different roles
    swarm.add_agent(Agent::new("Archie", AgentRole::Architect));
    swarm.add_agent(Agent::new("Cody", AgentRole::Coder));
    swarm.add_agent(Agent::new("Tessa", AgentRole::Tester));
    swarm.add_agent(Agent::new("Rex", AgentRole::Reviewer));

    // Create task requiring multiple roles
    let task = SwarmTask::new("Complex task")
        .with_role(AgentRole::Architect)
        .with_role(AgentRole::Coder)
        .with_role(AgentRole::Tester);

    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    // Assign task
    let assigned = swarm.assign_task(&task_id);

    // Should assign to agents matching required roles
    assert_eq!(assigned.len(), 3);
}

#[test]
fn test_swarm_consensus_with_multiple_votes() {
    let mut swarm = Swarm::new();

    // Create agents with different roles and trust
    let architect = swarm.add_agent(Agent::new("Archie", AgentRole::Architect));
    let coder1 = swarm.add_agent(Agent::new("Coder1", AgentRole::Coder));
    let coder2 = swarm.add_agent(Agent::new("Coder2", AgentRole::Coder));
    let tester = swarm.add_agent(Agent::new("Tessa", AgentRole::Tester));

    // Create decision
    let decision_id = swarm.create_decision(
        "Which architecture?",
        vec!["Microservices".into(), "Monolith".into(), "Hybrid".into()],
    );

    // Vote with different confidences
    swarm
        .vote(
            &decision_id,
            &architect,
            "Microservices",
            0.9,
            "Better scalability",
        )
        .unwrap();
    swarm
        .vote(&decision_id, &coder1, "Monolith", 0.6, "Simpler deployment")
        .unwrap();
    swarm
        .vote(&decision_id, &coder2, "Microservices", 0.7, "Team autonomy")
        .unwrap();
    swarm
        .vote(
            &decision_id,
            &tester,
            "Microservices",
            0.8,
            "Independent testing",
        )
        .unwrap();

    // Resolve decision
    let outcome = swarm.resolve_decision(&decision_id).unwrap();

    // Should resolve to the option with highest weighted votes
    assert!(outcome.is_some());
}

#[test]
fn test_swarm_state_management() {
    let mut swarm = Swarm::new();

    // Add agents in different states
    let _idle = swarm.add_agent(Agent::new("Idle", AgentRole::Coder));
    let working = swarm.add_agent(Agent::new("Working", AgentRole::Tester));
    let error = swarm.add_agent(Agent::new("Error", AgentRole::Reviewer));

    swarm.get_agent_mut(&working).unwrap().start_working();
    swarm.get_agent_mut(&error).unwrap().set_error();

    // Get stats
    let stats = swarm.stats();

    assert_eq!(stats.total_agents, 3);
    assert_eq!(
        *stats.agents_by_status.get(&AgentStatus::Idle).unwrap_or(&0),
        1
    );
    assert_eq!(
        *stats
            .agents_by_status
            .get(&AgentStatus::Working)
            .unwrap_or(&0),
        1
    );
    assert_eq!(
        *stats
            .agents_by_status
            .get(&AgentStatus::Error)
            .unwrap_or(&0),
        1
    );
}

#[test]
fn test_swarm_memory_shared_scratchpad() {
    let swarm = Swarm::new();

    // Get memory handle
    let memory = swarm.memory();

    // Write from one agent
    {
        let mut mem = memory.write().unwrap();
        mem.write("shared_key", "shared_value", "agent1");
    }

    // Read from another agent
    {
        let mut mem = memory.write().unwrap();
        let value = mem.read("shared_key", "agent2");
        assert_eq!(value, Some("shared_value".to_string()));
    }

    // Verify access log tracked both operations
    let mem = memory.read().unwrap();
    assert!(mem.access_log().len() >= 2);
}

#[test]
fn test_swarm_worker_timeout_handling() {
    let mut swarm = Swarm::new().with_decision_timeout(1); // 1 second timeout

    // Create decision
    let decision_id = swarm.create_decision("Test?", vec!["A".into(), "B".into()]);

    // Initially should not be timed out
    let timed_out = swarm.sweep_timed_out_decisions();
    assert!(timed_out.is_empty());

    // Wait a bit and check again (with 1s timeout)
    std::thread::sleep(std::time::Duration::from_millis(1100));

    let timed_out = swarm.sweep_timed_out_decisions();
    assert!(timed_out.contains(&decision_id));

    let decision = swarm.get_decision(&decision_id).unwrap();
    assert_eq!(decision.status, DecisionStatus::TimedOut);
}

#[test]
fn test_swarm_error_handling_recovery() {
    let mut swarm = Swarm::new();

    // Test error on nonexistent agent operations
    let result = swarm.get_agent("nonexistent");
    assert!(result.is_none());

    let removed = swarm.remove_agent("nonexistent");
    assert!(removed.is_none());

    // Test error on nonexistent decision operations
    let result = swarm.vote("nonexistent", "agent", "choice", 0.5, "reason");
    assert!(result.is_err());

    let result = swarm.resolve_decision("nonexistent");
    assert!(result.is_err());

    let result = swarm.resolve_conflict("nonexistent");
    assert!(result.is_err());

    // Test completing nonexistent task (should not panic)
    swarm.complete_task("nonexistent", "agent", "result", true);
}

#[test]
fn test_swarm_task_priority_ordering() {
    let mut swarm = Swarm::new();

    // Queue tasks with different priorities
    let low = SwarmTask::new("Low priority").with_priority(1);
    let medium = SwarmTask::new("Medium priority").with_priority(5);
    let high = SwarmTask::new("High priority").with_priority(10);
    let very_high = SwarmTask::new("Very high priority").with_priority(10);

    swarm.queue_task(low).unwrap();
    swarm.queue_task(medium).unwrap();
    swarm.queue_task(high).unwrap();
    swarm.queue_task(very_high).unwrap();

    // Should dequeue in priority order (highest first)
    let first_id = swarm.next_task().unwrap();
    let first = swarm.get_task(&first_id).unwrap();
    assert_eq!(first.priority, 10);

    let second_id = swarm.next_task().unwrap();
    let second = swarm.get_task(&second_id).unwrap();
    assert_eq!(second.priority, 10);

    let third_id = swarm.next_task().unwrap();
    let third = swarm.get_task(&third_id).unwrap();
    assert_eq!(third.priority, 5);

    let fourth_id = swarm.next_task().unwrap();
    let fourth = swarm.get_task(&fourth_id).unwrap();
    assert_eq!(fourth.priority, 1);
}

#[test]
fn test_swarm_task_assignment_with_busy_agents() {
    let mut swarm = Swarm::new();

    // Add two coders, make one busy
    let coder1 = swarm.add_agent(Agent::new("Coder1", AgentRole::Coder));
    let coder2 = swarm.add_agent(Agent::new("Coder2", AgentRole::Coder));

    swarm.get_agent_mut(&coder1).unwrap().start_working();

    // Create task
    let task = SwarmTask::new("Coding task").with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    // Should only assign to idle coder
    let assigned = swarm.assign_task(&task_id);
    assert_eq!(assigned.len(), 1);
    assert!(assigned.contains(&coder2));
}

#[test]
fn test_swarm_concurrent_decisions() {
    let mut swarm = Swarm::new();

    let agent = swarm.add_agent(Agent::new("Agent", AgentRole::Coder));

    // Create multiple decisions
    let decision1 = swarm.create_decision("Q1?", vec!["A".into(), "B".into()]);
    let decision2 = swarm.create_decision("Q2?", vec!["X".into(), "Y".into()]);
    let decision3 = swarm.create_decision("Q3?", vec!["1".into(), "2".into()]);

    // Vote on different decisions
    swarm.vote(&decision1, &agent, "A", 0.8, "reason1").unwrap();
    swarm.vote(&decision2, &agent, "Y", 0.7, "reason2").unwrap();
    swarm.vote(&decision3, &agent, "1", 0.9, "reason3").unwrap();

    // Resolve each independently
    let outcome1 = swarm.resolve_decision(&decision1).unwrap();
    let outcome2 = swarm.resolve_decision(&decision2).unwrap();
    let outcome3 = swarm.resolve_decision(&decision3).unwrap();

    assert_eq!(outcome1, Some("A".to_string()));
    assert_eq!(outcome2, Some("Y".to_string()));
    assert_eq!(outcome3, Some("1".to_string()));
}

#[test]
fn test_swarm_agent_get_mut_operations() {
    let mut swarm = Swarm::new();
    let id = swarm.add_agent(Agent::new("Test", AgentRole::Coder));

    // Test mutable operations
    {
        let agent = swarm.get_agent_mut(&id).unwrap();
        agent.start_working();
        agent.trust_score = 0.8;
    }

    // Verify changes persisted
    let agent = swarm.get_agent(&id).unwrap();
    assert_eq!(agent.status, AgentStatus::Working);
    assert!((agent.trust_score - 0.8).abs() < f32::EPSILON);
}

#[test]
fn test_swarm_memory_entries_retrieval() {
    let swarm = Swarm::new();
    let memory = swarm.memory();

    // Write multiple entries
    {
        let mut mem = memory.write().unwrap();
        mem.write("key1", "value1", "agent1");
        mem.write("key2", "value2", "agent2");
        mem.write("key3", "value3", "agent1");
    }

    // Retrieve and verify entries
    let mem = memory.read().unwrap();
    let entries = mem.entries();
    assert_eq!(entries.len(), 3);

    let keys = mem.keys();
    assert_eq!(keys.len(), 3);
}

#[test]
fn test_swarm_memory_tag_and_find() {
    let swarm = Swarm::new();
    let memory = swarm.memory();

    {
        let mut mem = memory.write().unwrap();
        mem.write("config", "app settings", "agent1");
        mem.write("data", "user data", "agent2");
        mem.tag("config", "system");
        mem.tag("data", "user");
        mem.tag("config", "important");
    }

    let mem = memory.read().unwrap();
    let system_entries = mem.find_by_tag("system");
    assert_eq!(system_entries.len(), 1);

    let user_entries = mem.find_by_tag("user");
    assert_eq!(user_entries.len(), 1);

    let important_entries = mem.find_by_tag("important");
    assert_eq!(important_entries.len(), 1);
}

#[test]
fn test_conflict_strategy_variants() {
    // Test all conflict strategies can be created
    let _ = ConflictStrategy::PriorityWins;
    let _ = ConflictStrategy::ConfidenceWins;
    let _ = ConflictStrategy::MajorityWins;
    let _ = ConflictStrategy::HumanIntervention;
    let _ = ConflictStrategy::AcceptAll;

    // Test default
    assert_eq!(ConflictStrategy::default(), ConflictStrategy::PriorityWins);

    // Test with_swarm builder
    let swarm1 = Swarm::new().with_conflict_strategy(ConflictStrategy::MajorityWins);
    let swarm2 = Swarm::new().with_conflict_strategy(ConflictStrategy::ConfidenceWins);
    let _ = (swarm1, swarm2);
}

#[test]
fn test_decision_status_transitions() {
    use super::types::Decision;

    let mut decision = Decision::new("Test?", vec!["A".into(), "B".into()]);

    // Initially pending
    assert_eq!(decision.status, DecisionStatus::Pending);
    assert!(decision.is_pending());

    // Add vote and resolve
    decision.add_vote(Vote::new("a1", AgentRole::Security, "A", 1.0, "reason"));
    let trust: HashMap<String, f32> = [("a1".to_string(), 1.0)].into_iter().collect();

    decision.resolve(&trust);

    // Now resolved
    assert_eq!(decision.status, DecisionStatus::Resolved);
    assert!(!decision.is_pending());
    assert_eq!(decision.outcome, Some("A".to_string()));
}

#[test]
fn test_task_status_lifecycle() {
    let mut task = SwarmTask::new("Test task").with_role(AgentRole::Coder);

    // Initial status
    assert_eq!(task.status, TaskStatus::Pending);

    // After assignment (simulated)
    task.status = TaskStatus::InProgress;
    assert_eq!(task.status, TaskStatus::InProgress);

    // After completion
    task.status = TaskStatus::Completed;
    assert_eq!(task.status, TaskStatus::Completed);
}

#[test]
fn test_swarm_complete_task_not_in_assigned() {
    let mut swarm = Swarm::new();
    let coder = swarm.add_agent(Agent::new("Coder", AgentRole::Coder));

    // Create and queue task
    let task = SwarmTask::new("Task").with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    // Try to complete without assigning (task is in queue, not active_tasks)
    // This should log a warning but not panic
    swarm.complete_task(&task_id, &coder, "result", true);

    // Task should still be in queue
    assert_eq!(swarm.list_tasks().len(), 1);
}

#[test]
fn test_swarm_stats_with_varied_trust() {
    let mut swarm = Swarm::new();

    // Add agents with different trust levels
    let high_trust = swarm.add_agent(Agent::new("High", AgentRole::Coder));
    let low_trust = swarm.add_agent(Agent::new("Low", AgentRole::Tester));

    // Build up trust for high_trust agent
    for _ in 0..10 {
        swarm
            .get_agent_mut(&high_trust)
            .unwrap()
            .complete_task(true);
    }

    // Reduce trust for low_trust agent
    for _ in 0..5 {
        swarm
            .get_agent_mut(&low_trust)
            .unwrap()
            .complete_task(false);
    }

    // Reset statuses
    swarm.get_agent_mut(&high_trust).unwrap().set_idle();
    swarm.get_agent_mut(&low_trust).unwrap().set_idle();

    let stats = swarm.stats();
    assert_eq!(stats.total_agents, 2);
    assert!(stats.average_trust > 0.0);
    assert!(stats.average_trust <= 1.0);
}

#[test]
fn test_swarm_complex_workflow() {
    let mut swarm = Swarm::new()
        .with_conflict_strategy(ConflictStrategy::PriorityWins)
        .with_consensus_threshold(0.6)
        .with_decision_timeout(300);

    // Create a development team
    let architect = swarm.add_agent(Agent::new("Archie", AgentRole::Architect));
    let coder = swarm.add_agent(Agent::new("Cody", AgentRole::Coder));
    let tester = swarm.add_agent(Agent::new("Tessa", AgentRole::Tester));
    let _reviewer = swarm.add_agent(Agent::new("Rex", AgentRole::Reviewer));

    // Step 1: Make an architectural decision
    let arch_decision = swarm.create_decision(
        "Which database?",
        vec!["PostgreSQL".into(), "MongoDB".into(), "SQLite".into()],
    );

    swarm
        .vote(
            &arch_decision,
            &architect,
            "PostgreSQL",
            0.9,
            "Best for relational data",
        )
        .unwrap();
    swarm
        .vote(
            &arch_decision,
            &coder,
            "PostgreSQL",
            0.8,
            "Familiar with it",
        )
        .unwrap();
    swarm
        .vote(&arch_decision, &tester, "SQLite", 0.6, "Simpler setup")
        .unwrap();

    let db_choice = swarm.resolve_decision(&arch_decision).unwrap();
    assert!(db_choice.is_some());

    // Step 2: Create and assign implementation task
    let impl_task = SwarmTask::new("Implement database layer")
        .with_role(AgentRole::Coder)
        .with_role(AgentRole::Tester)
        .with_priority(8);

    let task_id = impl_task.id.clone();
    swarm.queue_task(impl_task).unwrap();

    let assigned = swarm.assign_task(&task_id);
    assert_eq!(assigned.len(), 2);

    // Step 3: Complete the task
    for agent_id in &assigned {
        swarm.complete_task(&task_id, agent_id, "Task completed successfully", true);
    }

    let task = swarm.get_task(&task_id).unwrap();
    assert_eq!(task.status, TaskStatus::Completed);
    assert_eq!(task.results.len(), 2);

    // Step 4: Review task
    let review_task = SwarmTask::new("Review implementation")
        .with_role(AgentRole::Reviewer)
        .with_priority(9);

    let review_id = review_task.id.clone();
    swarm.queue_task(review_task).unwrap();

    let reviewers = swarm.assign_task(&review_id);
    assert!(!reviewers.is_empty());

    // Verify final stats
    let stats = swarm.stats();
    assert_eq!(stats.total_agents, 4);
    assert_eq!(stats.queued_tasks, 0); // Both tasks moved to active
}

#[test]
fn test_swarm_list_agents_filtering() {
    let mut swarm = Swarm::new();

    // Add agents of different roles
    let _arch = swarm.add_agent(Agent::new("Arch", AgentRole::Architect));
    let _coder1 = swarm.add_agent(Agent::new("Coder1", AgentRole::Coder));
    let _coder2 = swarm.add_agent(Agent::new("Coder2", AgentRole::Coder));
    let _tester = swarm.add_agent(Agent::new("Tester", AgentRole::Tester));

    // Test list_agents
    let all_agents = swarm.list_agents();
    assert_eq!(all_agents.len(), 4);

    // Test agents_by_role
    let coders = swarm.agents_by_role(AgentRole::Coder);
    assert_eq!(coders.len(), 2);

    let architects = swarm.agents_by_role(AgentRole::Architect);
    assert_eq!(architects.len(), 1);

    let security = swarm.agents_by_role(AgentRole::Security);
    assert!(security.is_empty());
}

#[test]
fn test_swarm_get_task_from_active() {
    let mut swarm = Swarm::new();
    swarm.add_agent(Agent::new("Coder", AgentRole::Coder));

    let task = SwarmTask::new("Test").with_role(AgentRole::Coder);
    let task_id = task.id.clone();
    swarm.queue_task(task).unwrap();

    // Task initially in queue
    assert!(swarm.get_task(&task_id).is_some());

    // Move to active
    swarm.next_task();

    // Should still be findable
    assert!(swarm.get_task(&task_id).is_some());
}

#[test]
fn test_decision_votes_for_with_multiple() {
    let mut decision =
        super::types::Decision::new("Vote?", vec!["A".into(), "B".into(), "C".into()]);

    decision.add_vote(Vote::new("a1", AgentRole::Coder, "A", 0.8, "r1"));
    decision.add_vote(Vote::new("a2", AgentRole::Coder, "A", 0.7, "r2"));
    decision.add_vote(Vote::new("a3", AgentRole::Tester, "B", 0.9, "r3"));
    decision.add_vote(Vote::new("a4", AgentRole::Security, "A", 1.0, "r4"));

    let votes_for_a = decision.votes_for("A");
    assert_eq!(votes_for_a.len(), 3);

    let votes_for_b = decision.votes_for("B");
    assert_eq!(votes_for_b.len(), 1);

    let votes_for_c = decision.votes_for("C");
    assert!(votes_for_c.is_empty());
}

#[test]
fn test_memory_entry_metadata() {
    let mut memory = SharedMemory::new();

    memory.write("test_key", "test_value", "creator_agent");

    let entries = memory.entries();
    let entry = entries.iter().find(|e| e.key == "test_key").unwrap();

    assert_eq!(entry.value, "test_value");
    assert_eq!(entry.created_by, "creator_agent");
    assert!(entry.created_at > 0);
    assert_eq!(entry.access_count, 0);
    assert!(entry.tags.is_empty());
}

#[test]
fn test_memory_write_updates_metadata() {
    let mut memory = SharedMemory::new();

    memory.write("key", "original", "agent1");

    // Read to increment access count
    memory.read("key", "agent2");

    let entries = memory.entries();
    let entry = entries.iter().find(|e| e.key == "key").unwrap();
    assert_eq!(entry.access_count, 1);

    // Write update
    memory.write("key", "updated", "agent3");

    let entries = memory.entries();
    let entry = entries.iter().find(|e| e.key == "key").unwrap();
    assert_eq!(entry.value, "updated");
    assert_eq!(entry.modified_by, Some("agent3".to_string()));
    assert!(entry.modified_at.is_some());
}