a3s-code-core 9.0.0

A3S Code Core - Embeddable AI agent library with tool execution
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
use super::*;
use crate::config::{SearchConfig, SearchEngineConfig};
use a3s_search::RetrievalHealth;
use std::collections::HashMap;
use std::path::PathBuf;

#[cfg(feature = "headless-search")]
#[tokio::test]
async fn headless_browser_pool_is_scoped_to_one_tool_execution() {
    let config = HeadlessConfig::default();
    let first = WebSearchTool::create_pool(&config);
    let second = WebSearchTool::create_pool(&config);

    assert!(
        !Arc::ptr_eq(&first, &second),
        "parallel or cancelled tool calls must not retain one shared Chrome lifecycle"
    );
    first.shutdown().await;
    second.shutdown().await;
}

#[cfg(feature = "headless-search")]
#[tokio::test]
async fn dropped_cleanup_guard_schedules_background_shutdown() {
    let config = HeadlessConfig::default();
    let pool = WebSearchTool::create_pool(&config);
    drop(BrowserPoolCleanup::new(Some(Arc::clone(&pool))));

    tokio::task::yield_now().await;
    let error = pool.warm_up().await.unwrap_err();
    assert!(
        error.message.contains("shut down"),
        "dropping a cancelled tool future must schedule pool shutdown: {error:?}"
    );
}

#[cfg(feature = "headless-search")]
#[test]
fn default_headless_backend_is_bundled_moli() {
    assert_eq!(HeadlessConfig::default().backend, BrowserBackend::Moli);
    assert!(HeadlessConfig::default().auto_download_moli);
}

#[cfg(feature = "headless-search")]
#[test]
fn request_proxy_is_applied_to_the_headless_tier() {
    let configured = HeadlessConfig {
        proxy_url: Some("http://configured.example:8080".to_string()),
        ..HeadlessConfig::default()
    };

    let effective =
        effective_headless_config(Some(&configured), Some("socks5://request.example:1080"))
            .expect("configured headless runtime");

    assert_eq!(effective.backend, BrowserBackend::Moli);
    assert_eq!(
        effective.proxy_url.as_deref(),
        Some("socks5://request.example:1080")
    );
}

#[cfg(feature = "headless-search")]
#[test]
fn managed_headless_discovery_uses_lightpanda_when_chrome_is_unavailable() {
    use crate::search_runtime::{BrowserInstallSource, BrowserRuntimeStatus, ManagedBrowser};

    let statuses = [
        BrowserRuntimeStatus {
            browser: ManagedBrowser::Chrome,
            available: false,
            source: BrowserInstallSource::Missing,
            path: None,
            version: None,
            cache_dir: None,
            detail: "not installed".to_string(),
        },
        BrowserRuntimeStatus {
            browser: ManagedBrowser::Lightpanda,
            available: true,
            source: BrowserInstallSource::System,
            path: Some(PathBuf::from("/diagnostic/lightpanda")),
            version: None,
            cache_dir: None,
            detail: "ready".to_string(),
        },
    ];

    let config = managed_headless_config_from_statuses(&statuses)
        .expect("Lightpanda should satisfy automatic headless discovery");
    assert_eq!(config.backend, BrowserBackend::Lightpanda);
    assert_eq!(
        config.browser_path.as_deref(),
        Some("/diagnostic/lightpanda")
    );
}

#[test]
fn latest_search_metrics_are_exposed_as_stable_metadata() {
    let snapshot = MetricsSnapshot {
        successes: 3,
        failures: 1,
        transient_failures: 1,
        permanent_failures: 0,
        error_counts: HashMap::from([("timeout".to_string(), 1)]),
        latency_p50_ms: 10,
        latency_p95_ms: 20,
        latency_p99_ms: 30,
    };
    let metadata = search_metrics_json(&snapshot);
    assert_eq!(metadata["total_requests"], 4);
    assert_eq!(metadata["success_rate"], 75.0);
    assert_eq!(metadata["transient_failure_rate"], 100.0);
    assert_eq!(metadata["error_counts"]["timeout"], 1);
    assert_eq!(metadata["latency_p99_ms"], 30);
}

#[test]
fn latest_request_coalescing_state_is_exposed_as_stable_metadata() {
    let mut snapshot = a3s_search::SearchCoalescerSnapshot::default();
    snapshot.max_in_flight = 128;
    snapshot.in_flight = 2;
    snapshot.leader_requests = 7;
    snapshot.shared_requests = 5;
    snapshot.bypassed_requests = 1;
    snapshot.abandoned_requests = 1;

    let metadata = search_coalescer_json(&snapshot);

    assert_eq!(metadata["max_in_flight"], 128);
    assert_eq!(metadata["in_flight"], 2);
    assert_eq!(metadata["leader_requests"], 7);
    assert_eq!(metadata["shared_requests"], 5);
    assert_eq!(metadata["bypassed_requests"], 1);
    assert_eq!(metadata["abandoned_requests"], 1);
}

struct CoalescingProbeEngine {
    config: a3s_search::EngineConfig,
    calls: Arc<std::sync::atomic::AtomicUsize>,
}

#[async_trait::async_trait]
impl a3s_search::Engine for CoalescingProbeEngine {
    fn config(&self) -> &a3s_search::EngineConfig {
        &self.config
    }

    async fn search(
        &self,
        query: &a3s_search::SearchQuery,
    ) -> a3s_search::Result<Vec<a3s_search::SearchResult>> {
        self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        tokio::time::sleep(std::time::Duration::from_millis(25)).await;
        Ok(vec![a3s_search::SearchResult::new(
            "https://example.test/coalesced",
            query.query.clone(),
            "shared result",
        )])
    }
}

#[tokio::test]
async fn tier_searches_share_the_session_request_coalescer() {
    let context = ToolContext::new(PathBuf::from("."));
    let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let mut first = tier_search(&context, Arc::new(Metrics::new()));
    let mut second = tier_search(&context, Arc::new(Metrics::new()));
    for search in [&mut first, &mut second] {
        search.add_engine(CoalescingProbeEngine {
            config: a3s_search::EngineConfig {
                name: "Coalescing Probe".to_string(),
                shortcut: "coalescing_probe".to_string(),
                ..a3s_search::EngineConfig::default()
            },
            calls: Arc::clone(&calls),
        });
    }
    let query = SearchQuery::new("same concurrent request");

    let (first_result, second_result) =
        tokio::join!(first.search(query.clone()), second.search(query));

    assert!(first_result.is_ok());
    assert!(second_result.is_ok());
    assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
}

fn search_config(engines: HashMap<String, SearchEngineConfig>) -> SearchConfig {
    SearchConfig {
        timeout: 10,
        cascade_order: None,
        health: None,
        engines,
        headless: None,
    }
}

#[test]
fn default_engine_selection_uses_builtin_defaults_without_engine_configuration() {
    let (engines, source) = default_engine_selection(None);
    assert_eq!(engines, ["anysearch", "tavily", "ddg", "wiki"]);
    assert_eq!(source, "builtin_default");

    let config = search_config(HashMap::new());
    let (engines, source) = default_engine_selection(Some(&config));
    assert_eq!(engines, ["anysearch", "tavily", "ddg", "wiki"]);
    assert_eq!(source, "builtin_default");
}

#[test]
fn configured_default_engine_selection_can_enable_anysearch_explicitly() {
    let config = search_config(HashMap::from([(
        "anysearch".to_string(),
        SearchEngineConfig {
            enabled: true,
            weight: 1.0,
            timeout: None,
            api_key: None,
            project: None,
            endpoint: None,
        },
    )]));

    let (engines, source) = default_engine_selection(Some(&config));

    assert_eq!(engines, ["anysearch"]);
    assert_eq!(source, "config");
}

#[test]
fn config_acl_controls_the_default_engine_selection() {
    let config = crate::config::CodeConfig::from_acl(
        r#"
search {
  engine {
    anysearch {
      enabled = true
      weight = 1.0
    }
  }
}
"#,
    )
    .expect("valid search config");
    let search = config.search.as_ref().expect("search config");

    let (engines, source) = default_engine_selection(Some(search));

    assert_eq!(engines, ["anysearch"]);
    assert_eq!(source, "config");
}

#[test]
fn automatic_tier_plan_is_stable_and_deduplicated() {
    let plan = tiered_engine_plan(&["anysearch", "duckduckgo"], None, true);

    assert_eq!(plan.api, ["anysearch"]);
    assert_eq!(plan.http, ["ddg", "brave", "bing", "wiki"]);
    #[cfg(feature = "headless-search")]
    assert_eq!(
        plan.headless,
        ["g", "baidu", "brave_browser", "bing_browser"]
    );
    #[cfg(not(feature = "headless-search"))]
    assert!(plan.headless.is_empty());
}

#[cfg(feature = "headless-search")]
#[test]
fn automatic_search_route_tries_api_before_http_and_headless() {
    use super::engines::EngineTier;
    use super::fallback::automatic_tier_order;

    assert_eq!(
        automatic_tier_order(),
        [EngineTier::Api, EngineTier::Http, EngineTier::Headless]
    );
}

#[test]
fn tier_plan_normalizes_aliases_and_respects_disabled_configuration() {
    let config = search_config(HashMap::from([
        (
            "duckduckgo".to_string(),
            SearchEngineConfig {
                enabled: false,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: None,
            },
        ),
        (
            "wikipedia".to_string(),
            SearchEngineConfig {
                enabled: false,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: None,
            },
        ),
    ]));

    let automatic = tiered_engine_plan(&["AnySearch"], Some(&config), true);
    assert_eq!(automatic.api, ["anysearch"]);
    assert_eq!(automatic.http, ["brave", "bing"]);
    #[cfg(feature = "headless-search")]
    assert_eq!(
        automatic.headless,
        ["g", "baidu", "brave_browser", "bing_browser"]
    );
    #[cfg(not(feature = "headless-search"))]
    assert!(automatic.headless.is_empty());

    let explicit = tiered_engine_plan(&["duckduckgo", "wikipedia"], None, false);
    assert!(explicit.api.is_empty());
    assert_eq!(explicit.http, ["ddg", "wiki"]);
    assert!(explicit.headless.is_empty());
}

#[test]
fn fallback_notice_uses_structured_failure_kinds_for_every_provider() {
    let failures = vec![
        EngineFailure::new("AnySearch", "provider_quota", "redacted").with_provider("anysearch"),
        EngineFailure::new("Tavily", "provider_rate_limited", "redacted")
            .with_provider("tavily")
            .with_transient(true),
    ];

    assert_eq!(
        failure_summary(&failures),
        "AnySearch quota is exhausted; Tavily was rate limited"
    );
    assert_eq!(failure_metadata(&failures)[0]["kind"], "provider_quota");
    assert_eq!(failure_metadata(&failures)[1]["provider"], "tavily");
}

#[test]
fn tool_error_kind_uses_structured_failure_kinds_instead_of_messages() {
    let rate_limited = [
        EngineFailure::new("Provider A", "provider_rate_limited", "opaque"),
        EngineFailure::new("Provider B", "rate_limited", "unrelated text"),
    ];
    assert_eq!(
        tool_error_kind_for_failures(&rate_limited, Duration::from_secs(10)),
        Some(ToolErrorKind::RateLimited {
            retry_after_ms: None,
        })
    );

    let timed_out = [
        EngineFailure::new("Provider A", "timeout", "opaque"),
        EngineFailure::new("Provider B", "http_timeout", "unrelated text"),
    ];
    assert_eq!(
        tool_error_kind_for_failures(&timed_out, Duration::from_secs(10)),
        Some(ToolErrorKind::Timeout {
            op: "web_search".to_string(),
            duration_ms: 10_000,
        })
    );

    let mixed = [
        EngineFailure::new("Provider A", "provider_quota", "rate limit"),
        EngineFailure::new("Provider B", "provider_rate_limited", "rate limit"),
    ];
    assert_eq!(
        tool_error_kind_for_failures(&mixed, Duration::from_secs(10)),
        None,
        "quota exhaustion must remain distinguishable in engine_failures metadata"
    );
}

#[test]
fn tier_timeout_preserves_a_share_for_each_remaining_tier() {
    assert_eq!(
        tier_timeout(Duration::from_secs(12), 0),
        Duration::from_secs(12)
    );
    assert_eq!(
        tier_timeout(Duration::from_secs(12), 1),
        Duration::from_secs(6)
    );
    assert_eq!(
        tier_timeout(Duration::from_secs(12), 2),
        Duration::from_secs(4)
    );
    assert_eq!(
        tier_timeout(Duration::from_millis(2), 2),
        Duration::from_millis(1)
    );
}

#[test]
fn default_engine_selection_respects_explicit_configuration() {
    let config = search_config(HashMap::from([
        (
            "enabled".to_string(),
            SearchEngineConfig {
                enabled: true,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: None,
            },
        ),
        (
            "disabled".to_string(),
            SearchEngineConfig {
                enabled: false,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: None,
            },
        ),
    ]));
    let (engines, source) = default_engine_selection(Some(&config));
    assert_eq!(engines, ["enabled"]);
    assert_eq!(source, "config");

    let config = search_config(HashMap::from([(
        "disabled".to_string(),
        SearchEngineConfig {
            enabled: false,
            weight: 1.0,
            timeout: None,
            api_key: None,
            project: None,
            endpoint: None,
        },
    )]));
    let (engines, source) = default_engine_selection(Some(&config));
    assert!(engines.is_empty());
    assert_eq!(source, "config");
}

#[test]
fn configured_default_engine_selection_deduplicates_aliases() {
    let config = search_config(HashMap::from([
        (
            "ddg".to_string(),
            SearchEngineConfig {
                enabled: true,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: None,
            },
        ),
        (
            "duckduckgo".to_string(),
            SearchEngineConfig {
                enabled: true,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: None,
            },
        ),
    ]));

    let (engines, source) = default_engine_selection(Some(&config));
    assert_eq!(engines, ["ddg"]);
    assert_eq!(source, "config");
}

#[test]
fn configured_engine_aliases_are_executable() {
    let mut search = Search::new();
    assert!(add_http_engine(&mut search, "duckduckgo", None, None).expect("engine setup"));
    assert!(add_http_engine(&mut search, "wikipedia", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 2);
}

#[test]
fn provider_setup_failures_remain_typed_for_the_cascade() {
    let error = a3s_search::SearchError::Other("provider setup failed".to_string());
    let failure = super::engines::provider_setup_failure(
        a3s_search::providers::BuiltinProvider::AnySearch,
        &error,
    );

    assert_eq!(failure.engine, "anysearch");
    assert_eq!(failure.provider.as_deref(), Some("anysearch"));
    assert_eq!(failure.kind, "other");
    assert!(!failure.transient);
}

#[tokio::test]
async fn test_web_search_missing_query() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));

    let result = tool.execute(&serde_json::json!({}), &ctx).await.unwrap();
    assert!(!result.success);
}

#[tokio::test]
async fn test_web_search_empty_query() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));

    let result = tool
        .execute(&serde_json::json!({"query": ""}), &ctx)
        .await
        .unwrap();
    assert!(!result.success);
}

#[tokio::test]
async fn test_web_search_no_valid_engines() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));

    let result = tool
        .execute(
            &serde_json::json!({"query": "test", "engines": ["nonexistent"]}),
            &ctx,
        )
        .await
        .unwrap();
    assert!(!result.success);
    assert!(result.content.contains("No valid engines"));
    let metadata = result.metadata.expect("search selection metadata");
    assert_eq!(metadata["status"], "failed");
    assert_eq!(metadata["engine_selection_source"], "request");
    assert_eq!(
        metadata["selected_engines"],
        serde_json::json!(["nonexistent"])
    );
}

#[tokio::test]
async fn configured_engine_selection_is_identified_in_failure_metadata() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp")).with_search_config(SearchConfig {
        timeout: 10,
        cascade_order: None,
        health: None,
        engines: HashMap::from([(
            "private-search".to_string(),
            SearchEngineConfig {
                enabled: true,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: None,
            },
        )]),
        headless: None,
    });

    let result = tool
        .execute(&serde_json::json!({"query": "test"}), &ctx)
        .await
        .unwrap();

    assert!(!result.success);
    let metadata = result.metadata.expect("search selection metadata");
    assert_eq!(metadata["status"], "failed");
    assert_eq!(metadata["engine_selection_source"], "config");
    assert_eq!(
        metadata["selected_engines"],
        serde_json::json!(["private-search"])
    );
}

#[tokio::test]
#[ignore = "requires external network"]
async fn real_builtin_default_search_uses_external_probe_query() {
    let query = std::env::var("A3S_WEB_SEARCH_PROBE_QUERY")
        .expect("set A3S_WEB_SEARCH_PROBE_QUERY for an external diagnostic query");
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));
    let result = tool
        .execute(
            &serde_json::json!({
                "query": query,
                "limit": 10,
                "timeout": 30,
                "format": "json",
                "full_text_bytes": 8192
            }),
            &ctx,
        )
        .await
        .unwrap();

    assert!(result.success, "{}", result.content);
    let items: serde_json::Value = serde_json::from_str(&result.content)
        .unwrap_or_else(|error| panic!("JSON search results ({error}): {}", result.content));
    assert!(
        items.as_array().is_some_and(|items| !items.is_empty()),
        "{}",
        result.content
    );
    let metadata = result.metadata.expect("default search metadata");
    assert_eq!(metadata["engine_selection_source"], "builtin_default");
    assert!(metadata["selected_engines"]
        .as_array()
        .is_some_and(|engines| !engines.is_empty()));
    let summaries = items
        .as_array()
        .expect("search result array")
        .iter()
        .map(|item| {
            let full_text = item
                .get("full_text")
                .and_then(serde_json::Value::as_str)
                .unwrap_or_default();
            let content = item
                .get("content")
                .and_then(serde_json::Value::as_str)
                .unwrap_or_default();
            serde_json::json!({
                "title": item.get("title"),
                "url": item.get("url"),
                "engines": item.get("engines"),
                "published_date": item.get("published_date"),
                "content_preview": crate::text::truncate_utf8(content, 480),
                "full_text_bytes": full_text.len(),
                "full_text_preview": crate::text::truncate_utf8(full_text, 240),
            })
        })
        .collect::<Vec<_>>();
    eprintln!(
        "{}",
        serde_json::to_string_pretty(&serde_json::json!({
            "metadata": metadata,
            "full_text_result_count": summaries.iter().filter(|item| {
                item["full_text_bytes"].as_u64().unwrap_or_default() > 0
            }).count(),
            "results": summaries,
        }))
        .unwrap()
    );
}

#[tokio::test]
#[ignore = "requires external network"]
async fn real_system_proxy_search_returns_traceable_results() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));
    let result = tool
        .execute(
            &serde_json::json!({
                "query": "Tokio Rust async runtime official documentation",
                "engines": ["ddg", "brave", "wiki"],
                "limit": 5,
                "timeout": 15,
                "format": "json"
            }),
            &ctx,
        )
        .await
        .unwrap();
    assert!(result.success, "{}", result.content);
    eprintln!("{}", result.content);
    let items: serde_json::Value = serde_json::from_str(&result.content)
        .unwrap_or_else(|error| panic!("JSON search results ({error}): {}", result.content));
    assert!(
        items.as_array().is_some_and(|items| !items.is_empty()),
        "{}",
        result.content
    );
}

#[tokio::test]
#[ignore = "requires external network"]
async fn real_bing_rss_search_works_without_headless_config() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));
    let started = std::time::Instant::now();

    let result = tool
        .execute(
            &serde_json::json!({
                "query": "Typhoon Bavi 2020 NOAA -2026",
                "engines": ["bing_cn"],
                "limit": 5,
                "timeout": 10,
                "format": "json"
            }),
            &ctx,
        )
        .await
        .unwrap();

    assert!(result.success, "{}", result.content);
    assert!(
        started.elapsed() < std::time::Duration::from_secs(12),
        "Bing RSS exceeded its convergence budget: {:?}",
        started.elapsed()
    );
    let items: serde_json::Value = serde_json::from_str(&result.content)
        .unwrap_or_else(|error| panic!("JSON Bing results ({error}): {}", result.content));
    assert!(
        items.as_array().is_some_and(|items| !items.is_empty()),
        "{}",
        result.content
    );
}

#[test]
fn bing_china_is_http_only() {
    assert_eq!(
        super::engines::engine_tier("bing_cn"),
        Some(super::engines::EngineTier::Http)
    );
    #[cfg(feature = "headless-search")]
    assert_eq!(
        super::engines::engine_tier("google"),
        Some(super::engines::EngineTier::Headless)
    );
    #[cfg(not(feature = "headless-search"))]
    assert_eq!(super::engines::engine_tier("google"), None);
}

#[cfg(feature = "headless-search")]
#[test]
fn explicit_headless_selection_does_not_invent_earlier_tiers() {
    let plan = tiered_engine_plan(&["google"], None, false);
    assert!(plan.api.is_empty());
    assert!(plan.http.is_empty());
    assert_eq!(plan.headless, ["g"]);
}

#[cfg(not(feature = "headless-search"))]
#[test]
fn headless_selection_is_unavailable_without_the_feature() {
    let plan = tiered_engine_plan(&["google", "baidu"], None, false);
    assert!(plan.is_empty());
}

#[cfg(not(feature = "headless-search"))]
fn browser_process_ids() -> std::collections::BTreeSet<(String, u32)> {
    let mut command = if cfg!(windows) {
        let mut command = std::process::Command::new("tasklist");
        command.args(["/FO", "CSV", "/NH"]);
        command
    } else {
        let mut command = std::process::Command::new("ps");
        command.args(["-A", "-o", "pid=,comm="]);
        command
    };
    let output = command.output().expect("list processes");
    let text = String::from_utf8_lossy(&output.stdout);
    let mut ids = std::collections::BTreeSet::new();
    for line in text.lines() {
        let (name, pid) = if cfg!(windows) {
            let mut parts = line.split("\",\"");
            let Some(name) = parts.next() else { continue };
            let Some(pid) = parts.next() else { continue };
            (
                name.trim_matches('"').to_ascii_lowercase(),
                pid.trim_matches('"').parse::<u32>().ok(),
            )
        } else {
            let mut parts = line.split_whitespace();
            let Some(pid) = parts.next() else { continue };
            let Some(name) = parts.next() else { continue };
            (name.to_ascii_lowercase(), pid.parse::<u32>().ok())
        };
        let Some(pid) = pid else { continue };
        if name.contains("moli") || name.contains("lightpanda") || name.contains("chrom") {
            ids.insert((name, pid));
        }
    }
    ids
}

#[cfg(not(feature = "headless-search"))]
#[tokio::test]
async fn headless_engine_request_does_not_spawn_moli() {
    let before = browser_process_ids();
    let tool = WebSearchTool::new();
    let context = ToolContext::new(PathBuf::from("."));
    let output = tool
        .execute(
            &serde_json::json!({
                "query": "kernel bound",
                "engines": "google"
            }),
            &context,
        )
        .await
        .expect("web_search returns a typed result");

    assert!(!output.success);
    assert!(
        matches!(
            output.error_kind,
            Some(crate::tools::ToolErrorKind::InvalidArgument { .. })
        ),
        "headless engines must be a typed rejection without the feature: {output:?}"
    );
    assert!(
        output.content.contains("No valid engines"),
        "rejection must name the missing engine set: {}",
        output.content
    );
    let spawned = browser_process_ids()
        .difference(&before)
        .cloned()
        .collect::<Vec<_>>();
    assert!(
        spawned.is_empty(),
        "a headless request spawned a browser process: {spawned:?}"
    );
}

#[tokio::test]
async fn test_web_search_unknown_parameter_engine_returns_error() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));

    // Using `engine` (singular) instead of `engines` (plural) should return an error
    let result = tool
        .execute(
            &serde_json::json!({"query": "test", "engine": "google"}),
            &ctx,
        )
        .await
        .unwrap();

    assert!(
        !result.success,
        "Expected error when using 'engine' instead of 'engines'"
    );
    assert!(
        result.content.contains("unknown parameter 'engine'"),
        "Error message should mention the unknown parameter"
    );
    assert!(
        result.content.contains("'engines' (plural)"),
        "Error message should clarify to use 'engines' (plural)"
    );
}

#[tokio::test]
async fn test_web_search_multiple_unknown_parameters() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));

    let result = tool
        .execute(
            &serde_json::json!({
                "query": "test",
                "engine": "ddg",
                "source": "web"
            }),
            &ctx,
        )
        .await
        .unwrap();

    assert!(!result.success);
    assert!(
        result.content.contains("unknown parameter"),
        "Error should mention unknown parameters"
    );
}

#[tokio::test]
async fn test_web_search_engines_param_works() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));

    let result = tool
        .execute(
            &serde_json::json!({"query": "test", "engines": ["ddg"]}),
            &ctx,
        )
        .await
        .unwrap();

    // May succeed or fail depending on network, but should NOT have unknown param error
    if !result.success {
        assert!(
            !result.content.contains("unknown parameter"),
            "Should not complain about 'engines' being unknown"
        );
    }
}

#[test]
fn test_web_search_schema_is_canonical() {
    let tool = WebSearchTool::new();
    let params = tool.parameters();
    assert_eq!(params["additionalProperties"], false);
    assert_eq!(params["required"], serde_json::json!(["query"]));
    // engines should be an array type
    assert_eq!(params["properties"]["engines"]["type"], "array");
    let examples = params["examples"].as_array().unwrap();
    assert_eq!(examples[0]["query"], "Rust async trait");
    assert!(examples[0].get("q").is_none());
    // Example with engines should use array format
    assert!(examples[1]["engines"].is_array());
    assert_eq!(examples[1]["engines"].as_array().unwrap(), &["ddg", "wiki"]);
    assert!(params["properties"]["engines"]["description"]
        .as_str()
        .is_some_and(|description| {
            description.contains("bing (Bing RSS)")
                && description.contains("anysearch")
                && description.contains("tavily")
        }));
    #[cfg(feature = "headless-search")]
    assert!(params["properties"]["engines"]["description"]
        .as_str()
        .is_some_and(|description| description.contains("Google, headless")));
    #[cfg(feature = "headless-search")]
    assert!(params["properties"]["engines"]["description"]
        .as_str()
        .is_some_and(|description| {
            description.contains("bing_browser")
                && description.contains("brave_browser")
                && description.contains("Moli")
        }));
    #[cfg(not(feature = "headless-search"))]
    assert!(params["properties"]["engines"]["description"]
        .as_str()
        .is_some_and(|description| !description.contains("Google")));
}

#[test]
fn test_parse_proxy_url_http() {
    let config = parse_proxy_url("http://127.0.0.1:8080").unwrap();
    assert_eq!(config.host, "127.0.0.1");
    assert_eq!(config.port, 8080);
}

#[test]
fn test_parse_proxy_url_socks5() {
    let config = parse_proxy_url("socks5://proxy.example.com:1080").unwrap();
    assert_eq!(config.host, "proxy.example.com");
    assert_eq!(config.port, 1080);
}

#[test]
fn test_parse_proxy_url_no_port() {
    assert!(parse_proxy_url("http://127.0.0.1").is_none());
}

#[test]
fn test_parse_proxy_url_empty() {
    assert!(parse_proxy_url("").is_none());
}

#[test]
fn test_add_http_engine_valid() {
    let mut search = Search::new();
    assert!(add_http_engine(&mut search, "ddg", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 1);

    assert!(add_http_engine(&mut search, "wiki", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 2);

    assert!(add_http_engine(&mut search, "brave", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 3);

    assert!(add_http_engine(&mut search, "bing", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 4);

    assert!(add_http_engine(&mut search, "bing_cn", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 5);

    assert!(add_http_engine(&mut search, "anysearch", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 6);

    assert!(add_http_engine(&mut search, "tavily", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 7);

    for provider in ["tinyfish", "bocha", "aliyun", "tencent", "firecrawl"] {
        assert!(
            add_http_engine(&mut search, provider, None, None).expect("engine setup"),
            "{provider} must be constructible when named"
        );
    }
    assert_eq!(search.engine_count(), 12);
}

#[test]
fn test_add_http_engine_unknown() {
    let mut search = Search::new();
    assert!(!add_http_engine(&mut search, "nonexistent", None, None).expect("engine setup"));
    assert_eq!(search.engine_count(), 0);
}

#[cfg(feature = "headless-search")]
#[test]
fn test_add_headless_engine_valid() {
    let mut search = Search::new();
    let pool_config = BrowserPoolConfig::default();
    let pool = Arc::new(BrowserPool::new(pool_config));

    let retry_budget = a3s_search::RetryBudget::default();
    assert!(add_headless_engine(
        &mut search,
        "google",
        pool.clone(),
        BrowserBackend::Chrome,
        &retry_budget,
    ));
    assert_eq!(search.engine_count(), 1);

    assert!(add_headless_engine(
        &mut search,
        "baidu",
        pool.clone(),
        BrowserBackend::Chrome,
        &retry_budget,
    ));
    assert_eq!(search.engine_count(), 2);

    assert!(!add_headless_engine(
        &mut search,
        "bing_cn",
        pool.clone(),
        BrowserBackend::Chrome,
        &retry_budget,
    ));
    assert_eq!(search.engine_count(), 2);
}

#[cfg(feature = "headless-search")]
#[test]
fn test_add_headless_engine_aliases() {
    let mut search = Search::new();
    let pool_config = BrowserPoolConfig::default();
    let pool = Arc::new(BrowserPool::new(pool_config));

    let retry_budget = a3s_search::RetryBudget::default();
    assert!(add_headless_engine(
        &mut search,
        "g",
        pool.clone(),
        BrowserBackend::Chrome,
        &retry_budget,
    ));
    assert_eq!(search.engine_count(), 1);
}

#[cfg(feature = "headless-search")]
#[test]
fn test_add_headless_engine_unknown() {
    let mut search = Search::new();
    let pool_config = BrowserPoolConfig::default();
    let pool = Arc::new(BrowserPool::new(pool_config));

    let retry_budget = a3s_search::RetryBudget::default();
    assert!(!add_headless_engine(
        &mut search,
        "ddg",
        pool.clone(),
        BrowserBackend::Chrome,
        &retry_budget,
    ));
    assert!(!add_headless_engine(
        &mut search,
        "nonexistent",
        pool.clone(),
        BrowserBackend::Chrome,
        &retry_budget,
    ));
}

#[tokio::test]
async fn test_web_search_all_valid_parameters_accepted() {
    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(PathBuf::from("/tmp"));

    // All valid parameters should be accepted without unknown param error
    let result = tool
        .execute(
            &serde_json::json!({
                "query": "test",
                "engines": ["ddg", "wiki"],
                "limit": 5,
                "timeout": 30,
                "proxy": "http://127.0.0.1:8080",
                "format": "json"
            }),
            &ctx,
        )
        .await
        .unwrap();

    // Should not have unknown parameter error
    // May fail for other reasons (e.g., network), but not param validation
    if !result.success {
        assert!(
            !result.content.contains("unknown parameter"),
            "All listed parameters should be valid: {}",
            result.content
        );
    }
}

#[test]
fn test_web_search_schema_has_all_valid_fields() {
    let tool = WebSearchTool::new();
    let params = tool.parameters();

    // Verify all valid fields are documented
    let valid_fields = [
        "query",
        "engines",
        "limit",
        "timeout",
        "proxy",
        "format",
        "full_text_bytes",
    ];
    for field in valid_fields {
        assert!(
            params["properties"]
                .as_object()
                .unwrap()
                .contains_key(field),
            "Schema should document '{}' as a valid field",
            field
        );
    }

    // Verify additionalProperties is false (no extra fields allowed)
    assert_eq!(params["additionalProperties"], false);
}

#[test]
fn json_search_result_preserves_published_date() {
    let result = SearchResult::new(
            "https://result-user:result-password@example.com/release?tracking=secret#fragment",
            "Release notes https://title-user:title-password@example.com/title?title_token=secret#title-fragment",
            "Current release evidence at https://content-user:content-password@example.com/evidence?content_token=secret#content-fragment.",
        )
        .with_engine("ddg", 2)
        .with_engine("brave", 1)
        .with_published_date("2026-07-11");
    let json = search_result_json(&result, None);

    assert_eq!(json["published_date"], "2026-07-11");
    assert!(json.get("query_match_score").is_none());
    assert_eq!(json["url"], "https://example.com/release");
    assert_eq!(json["title"], "Release notes https://example.com/title");
    assert_eq!(
        json["content"],
        "Current release evidence at https://example.com/evidence."
    );
    assert_eq!(json["engines"], serde_json::json!(["brave", "ddg"]));
    let serialized = json.to_string();
    for secret in [
        "result-user",
        "result-password",
        "tracking",
        "fragment",
        "title-user",
        "title-password",
        "title_token",
        "content-user",
        "content-password",
        "content_token",
    ] {
        assert!(
            !serialized.contains(secret),
            "leaked {secret}: {serialized}"
        );
    }
}

#[test]
fn json_search_payload_preserves_the_array_contract_when_requirements_are_met() {
    let results = vec![serde_json::json!({
        "title": "Portable evidence",
        "url": "https://example.test/evidence"
    })];

    let payload = json_search_payload(results.clone());

    assert_eq!(payload, serde_json::Value::Array(results));
}

#[test]
fn json_search_payload_keeps_the_array_contract_below_retrieval_requirements() {
    let health = RetrievalHealth::default();
    let requirements = RetrievalRequirements::for_limit(1);
    assert!(
        !requirements.is_met(&health),
        "an empty health snapshot cannot pass the structural gate"
    );

    let results = vec![serde_json::json!({
        "title": "Weak candidate",
        "url": "https://example.test/candidate"
    })];
    let payload = json_search_payload(results.clone());

    assert_eq!(payload, serde_json::Value::Array(results));
}

#[test]
fn json_search_result_includes_only_requested_bounded_sanitized_full_text() {
    let mut result = SearchResult::new("https://example.com/source", "Source", "Summary");
    result.full_text = Some(format!(
        "Evidence at https://reader:password@example.com/private?token=secret#fragment {}",
        "x".repeat(2_000)
    ));

    let omitted = search_result_json(&result, None);
    assert!(omitted.get("full_text").is_none());

    let included = search_result_json(&result, Some(MIN_FULL_TEXT_BYTES));
    let full_text = included["full_text"].as_str().unwrap();
    assert!(full_text.len() <= MIN_FULL_TEXT_BYTES);
    assert!(full_text.contains("https://example.com/private"));
    for secret in ["reader", "password", "token", "secret", "fragment"] {
        assert!(!full_text.contains(secret), "leaked {secret}: {full_text}");
    }
}

#[test]
fn json_search_result_bounds_provider_title_and_summary_fields() {
    let result = SearchResult::new(
        "https://example.com/source",
        "t".repeat(MAX_JSON_TITLE_BYTES * 2),
        "c".repeat(MAX_JSON_CONTENT_BYTES * 2),
    );

    let json = search_result_json(&result, None);

    assert!(json["title"].as_str().unwrap().len() <= MAX_JSON_TITLE_BYTES);
    assert!(json["content"].as_str().unwrap().len() <= MAX_JSON_CONTENT_BYTES);
}

#[test]
fn json_search_result_collection_stays_valid_below_tool_transport_limit() {
    let results = (0..16)
        .map(|index| {
            let mut result = SearchResult::new(
                format!("https://example.com/source-{index}"),
                "title".repeat(600),
                "summary".repeat(1_200),
            );
            result.full_text = Some("evidence".repeat(8_000));
            result
        })
        .collect::<Vec<_>>();
    let references = results.iter().collect::<Vec<_>>();

    let bounded = bounded_json_search_results(&references, Some(MAX_FULL_TEXT_BYTES));
    let encoded = serde_json::to_vec(&bounded).unwrap();

    assert!(!bounded.is_empty());
    assert!(bounded.len() < results.len());
    assert!(encoded.len() <= MAX_JSON_OUTPUT_BYTES);
    assert!(serde_json::from_slice::<serde_json::Value>(&encoded).is_ok());
}

#[test]
fn text_search_result_preserves_optional_date_and_stable_engines() {
    let dated = SearchResult::new(
            "https://result-user:result-password@example.com/release?tracking=secret#fragment",
            "Release notes https://title-user:title-password@example.com/title?title_token=secret#title-fragment",
            "Current release evidence at https://content-user:content-password@example.com/evidence?content_token=secret#content-fragment.",
        )
        .with_engine("ddg", 2)
        .with_engine("brave", 1)
        .with_published_date(" 2026-07-11 ");
    let text = text_search_result(0, &dated);
    assert!(text.contains("Published: 2026-07-11\n"), "{text}");
    assert!(text.contains("(via brave, ddg)"), "{text}");
    assert!(
        text.contains("URL: https://example.com/release\n"),
        "{text}"
    );
    assert!(
        text.contains("Release notes https://example.com/title"),
        "{text}"
    );
    assert!(
        text.contains("Current release evidence at https://example.com/evidence."),
        "{text}"
    );
    for secret in [
        "result-user",
        "result-password",
        "tracking",
        "fragment",
        "title-user",
        "title-password",
        "title_token",
        "content-user",
        "content-password",
        "content_token",
    ] {
        assert!(!text.contains(secret), "leaked {secret}: {text}");
    }

    let undated = SearchResult::new(
        "https://example.com/reference",
        "Reference",
        "Undated evidence",
    );
    let text = text_search_result(1, &undated);
    assert!(text.starts_with("2. Reference\n"), "{text}");
    assert!(!text.contains("Published:"), "{text}");

    let unsafe_result = SearchResult::new("javascript:alert(1)", "Unsafe", "Unsafe");
    assert!(safe_search_result_url(&unsafe_result).is_empty());
}

#[test]
fn search_query_urls_drop_credentials_query_and_fragment() {
    let query = sanitize_http_urls(
        "compare https://query-user:query-password@example.com/release?api_key=secret#private, now",
    );

    assert_eq!(query, "compare https://example.com/release, now");
    for secret in [
        "query-user",
        "query-password",
        "api_key",
        "secret",
        "private",
    ] {
        assert!(!query.contains(secret), "leaked {secret}: {query}");
    }
}

#[test]
fn configured_api_endpoint_rejects_non_loopback_http() {
    let mut search = Search::new();
    let config = SearchConfig {
        timeout: 5,
        cascade_order: None,
        health: None,
        engines: HashMap::from([(
            "tavily".to_string(),
            SearchEngineConfig {
                enabled: true,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: Some("http://example.com/search".to_string()),
            },
        )]),
        headless: None,
    };

    let failure = add_http_engine(&mut search, "tavily", None, Some(&config))
        .expect_err("non-loopback http endpoint must fail closed");
    assert_eq!(failure.engine, "tavily");
    assert_eq!(failure.provider.as_deref(), Some("tavily"));
    assert!(
        failure.kind.contains("invalid") || failure.message.to_lowercase().contains("https"),
        "{failure:?}"
    );
}

#[test]
fn configured_api_endpoint_accepts_loopback_http() {
    let mut search = Search::new();
    let config = SearchConfig {
        timeout: 5,
        cascade_order: None,
        health: None,
        engines: HashMap::from([(
            "tavily".to_string(),
            SearchEngineConfig {
                enabled: true,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: Some("http://127.0.0.1:9/search".to_string()),
            },
        )]),
        headless: None,
    };

    assert!(
        add_http_engine(&mut search, "tavily", None, Some(&config)).expect("loopback ok"),
        "loopback fixture endpoint must register"
    );
}

/// S-WS-01: 100 searches against a loopback Tavily fixture stay connection-
/// and byte-bounded. Endpoint override stays SSRF-safe (loopback HTTP only).
#[tokio::test]
#[ignore = "S-WS-01 soak: loopback search fixture connection and byte caps"]
async fn soak_web_search_loopback_fixture_stays_connection_and_byte_capped() {
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::sync::Arc;
    use std::time::Duration;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    const CYCLES: usize = 100;
    const POOL_IDLE_CAP: usize = 4;

    let listener = TcpListener::bind("127.0.0.1:0").await.expect("listener");
    let addr = listener.local_addr().expect("addr");
    let endpoint = format!("http://127.0.0.1:{}/search", addr.port());

    let open = Arc::new(AtomicUsize::new(0));
    let peak = Arc::new(AtomicUsize::new(0));
    let hits = Arc::new(AtomicUsize::new(0));
    let stop = Arc::new(AtomicBool::new(false));

    let serve_open = Arc::clone(&open);
    let serve_peak = Arc::clone(&peak);
    let serve_hits = Arc::clone(&hits);
    let serve_stop = Arc::clone(&stop);
    let server = tokio::spawn(async move {
        loop {
            if serve_stop.load(Ordering::SeqCst) {
                break;
            }
            let accept = tokio::time::timeout(Duration::from_millis(50), listener.accept()).await;
            let Ok(Ok((mut stream, _))) = accept else {
                continue;
            };
            let open = Arc::clone(&serve_open);
            let peak = Arc::clone(&serve_peak);
            let hits = Arc::clone(&serve_hits);
            tokio::spawn(async move {
                let current = open.fetch_add(1, Ordering::SeqCst) + 1;
                peak.fetch_max(current, Ordering::SeqCst);
                let mut buf = vec![0u8; 16 * 1024];
                let _ = stream.read(&mut buf).await;
                hits.fetch_add(1, Ordering::SeqCst);
                let body = serde_json::json!({
                    "results": [{
                        "title": "Fixture",
                        "url": "https://example.com/fixture",
                        "content": "X".repeat(64 * 1024)
                    }]
                })
                .to_string();
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                    body.len(),
                    body
                );
                let _ = stream.write_all(response.as_bytes()).await;
                let _ = stream.shutdown().await;
                open.fetch_sub(1, Ordering::SeqCst);
            });
        }
    });

    let tool = WebSearchTool::new();
    let ctx = ToolContext::new(std::env::temp_dir()).with_search_config(SearchConfig {
        timeout: 10,
        cascade_order: None,
        health: None,
        engines: HashMap::from([(
            "tavily".to_string(),
            SearchEngineConfig {
                enabled: true,
                weight: 1.0,
                timeout: None,
                api_key: None,
                project: None,
                endpoint: Some(endpoint),
            },
        )]),
        headless: None,
    });

    for index in 0..CYCLES {
        let output = tool
            .execute(
                &serde_json::json!({
                    "query": format!("soak-{index}"),
                    "engines": ["tavily"],
                    "format": "json",
                    "limit": 1
                }),
                &ctx,
            )
            .await
            .expect("typed result");
        assert!(output.success, "cycle {index} failed: {}", output.content);
        assert!(
            output.content.len() <= MAX_JSON_OUTPUT_BYTES,
            "cycle {index} exceeded output cap: {} > {}",
            output.content.len(),
            MAX_JSON_OUTPUT_BYTES
        );
        assert!(
            open.load(Ordering::SeqCst) <= POOL_IDLE_CAP,
            "open connections exceeded pool cap mid-soak: {}",
            open.load(Ordering::SeqCst)
        );
    }

    tokio::time::sleep(Duration::from_millis(250)).await;
    let idle_open = open.load(Ordering::SeqCst);
    stop.store(true, Ordering::SeqCst);
    let _ = server.await;

    assert_eq!(hits.load(Ordering::SeqCst), CYCLES, "fixture hit count");
    assert!(
        idle_open <= POOL_IDLE_CAP,
        "idle open connections {idle_open} exceeded pool cap {POOL_IDLE_CAP} (fail if == {CYCLES})"
    );
    assert_ne!(
        idle_open, CYCLES,
        "each search must not leave a live socket"
    );
    assert!(
        peak.load(Ordering::SeqCst) <= POOL_IDLE_CAP,
        "peak open {} exceeded pool cap",
        peak.load(Ordering::SeqCst)
    );
}