a3s-search 0.5.4

Embeddable meta search engine library with CLI and proxy pool support
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
//! Search orchestration.

use std::sync::Arc;
use std::time::Instant;

use futures::future::join_all;
use tokio::time::{timeout, Duration};
use tracing::{debug, warn};

use crate::proxy::ProxyPool;
use crate::{Aggregator, Engine, Result, SearchError, SearchQuery, SearchResults};

/// Meta search engine that orchestrates searches across multiple engines.
pub struct Search {
    engines: Vec<Arc<dyn Engine>>,
    aggregator: Aggregator,
    default_timeout: Duration,
    proxy_pool: Option<Arc<ProxyPool>>,
}

impl Search {
    /// Creates a new search instance.
    pub fn new() -> Self {
        Self {
            engines: Vec::new(),
            aggregator: Aggregator::new(),
            default_timeout: Duration::from_secs(5),
            proxy_pool: None,
        }
    }

    /// Adds a search engine.
    pub fn add_engine<E: Engine + 'static>(&mut self, engine: E) {
        let config = engine.config();
        self.aggregator
            .set_engine_weight(&config.name, config.weight);
        self.engines.push(Arc::new(engine));
    }

    /// Sets the default timeout for searches.
    pub fn set_timeout(&mut self, timeout: Duration) {
        self.default_timeout = timeout;
    }

    /// Sets the proxy pool for anti-crawler protection.
    pub fn set_proxy_pool(&mut self, proxy_pool: ProxyPool) {
        self.proxy_pool = Some(Arc::new(proxy_pool));
    }

    /// Returns a reference to the proxy pool if configured.
    pub fn proxy_pool(&self) -> Option<&Arc<ProxyPool>> {
        self.proxy_pool.as_ref()
    }

    /// Returns the number of configured engines.
    pub fn engine_count(&self) -> usize {
        self.engines.len()
    }

    /// Performs a search across all configured engines.
    pub async fn search(&self, query: SearchQuery) -> Result<SearchResults> {
        if self.engines.is_empty() {
            return Err(SearchError::NoEngines);
        }

        if query.query.trim().is_empty() {
            return Err(SearchError::InvalidQuery("Query cannot be empty".into()));
        }

        let start = Instant::now();
        let query = Arc::new(query);

        let engines_to_use = self.select_engines(&query);
        debug!("Searching {} engines", engines_to_use.len());

        let futures: Vec<_> = engines_to_use
            .iter()
            .map(|engine| {
                let engine = Arc::clone(engine);
                let query = Arc::clone(&query);
                let timeout_duration = Duration::from_secs(engine.config().timeout);

                async move {
                    let name = engine.name().to_string();
                    match timeout(timeout_duration, engine.search(&query)).await {
                        Ok(Ok(results)) => {
                            debug!("Engine {} returned {} results", name, results.len());
                            Ok((name, results))
                        }
                        Ok(Err(e)) => {
                            warn!("Engine {} failed: {}", name, e);
                            Err((name, e.to_string()))
                        }
                        Err(_) => {
                            warn!("Engine {} timed out", name);
                            Err((name, "timed out".to_string()))
                        }
                    }
                }
            })
            .collect();

        let all_results: Vec<_> = join_all(futures).await;

        let mut engine_errors = Vec::new();
        let results: Vec<_> = all_results
            .into_iter()
            .filter_map(|r| match r {
                Ok(pair) => Some(pair),
                Err(err) => {
                    engine_errors.push(err);
                    None
                }
            })
            .collect();

        let mut search_results = self.aggregator.aggregate(results);
        for (engine, error) in engine_errors {
            search_results.add_error(engine, error);
        }
        search_results.set_duration(start.elapsed().as_millis() as u64);

        Ok(search_results)
    }

    /// Selects engines based on query parameters.
    fn select_engines(&self, query: &SearchQuery) -> Vec<Arc<dyn Engine>> {
        self.engines
            .iter()
            .filter(|engine| {
                if !engine.is_enabled() {
                    return false;
                }

                if !query.engines.is_empty() {
                    return query.engines.contains(&engine.shortcut().to_string());
                }

                let config = engine.config();
                query
                    .categories
                    .iter()
                    .any(|cat| config.categories.contains(cat))
            })
            .cloned()
            .collect()
    }
}

impl Default for Search {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{EngineCategory, EngineConfig, SearchResult};
    use async_trait::async_trait;

    struct MockEngine {
        config: EngineConfig,
        results: Vec<SearchResult>,
    }

    impl MockEngine {
        fn new(name: &str, results: Vec<SearchResult>) -> Self {
            Self {
                config: EngineConfig {
                    name: name.to_string(),
                    shortcut: name.to_string(),
                    categories: vec![EngineCategory::General],
                    ..Default::default()
                },
                results,
            }
        }

        fn with_category(mut self, category: EngineCategory) -> Self {
            self.config.categories = vec![category];
            self
        }

        fn with_shortcut(mut self, shortcut: &str) -> Self {
            self.config.shortcut = shortcut.to_string();
            self
        }

        fn disabled(mut self) -> Self {
            self.config.enabled = false;
            self
        }
    }

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

        async fn search(&self, _query: &SearchQuery) -> Result<Vec<SearchResult>> {
            Ok(self.results.clone())
        }
    }

    struct FailingEngine {
        config: EngineConfig,
    }

    impl FailingEngine {
        fn new(name: &str) -> Self {
            Self {
                config: EngineConfig {
                    name: name.to_string(),
                    shortcut: name.to_string(),
                    categories: vec![EngineCategory::General],
                    ..Default::default()
                },
            }
        }
    }

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

        async fn search(&self, _query: &SearchQuery) -> Result<Vec<SearchResult>> {
            Err(SearchError::Other("Engine failed".to_string()))
        }
    }

    #[tokio::test]
    async fn test_search_new() {
        let search = Search::new();
        assert_eq!(search.engine_count(), 0);
    }

    #[tokio::test]
    async fn test_search_default() {
        let search = Search::default();
        assert_eq!(search.engine_count(), 0);
    }

    #[tokio::test]
    async fn test_search_add_engine() {
        let mut search = Search::new();
        search.add_engine(MockEngine::new("test", vec![]));
        assert_eq!(search.engine_count(), 1);
    }

    #[tokio::test]
    async fn test_search_set_timeout() {
        let mut search = Search::new();
        search.set_timeout(Duration::from_secs(10));
        assert_eq!(search.default_timeout, Duration::from_secs(10));
    }

    #[tokio::test]
    async fn test_search_no_engines() {
        let search = Search::new();
        let query = SearchQuery::new("test");
        let result = search.search(query).await;
        assert!(matches!(result, Err(SearchError::NoEngines)));
    }

    #[tokio::test]
    async fn test_search_empty_query() {
        let mut search = Search::new();
        search.add_engine(MockEngine::new("test", vec![]));
        let query = SearchQuery::new("   ");
        let result = search.search(query).await;
        assert!(matches!(result, Err(SearchError::InvalidQuery(_))));
    }

    #[tokio::test]
    async fn test_search_whitespace_only_query() {
        let mut search = Search::new();
        search.add_engine(MockEngine::new("test", vec![]));
        let query = SearchQuery::new("\t\n  ");
        let result = search.search(query).await;
        assert!(matches!(result, Err(SearchError::InvalidQuery(_))));
    }

    #[tokio::test]
    async fn test_search_aggregates_results() {
        let mut search = Search::new();

        search.add_engine(MockEngine::new(
            "engine1",
            vec![SearchResult::new(
                "https://example.com",
                "Example",
                "Content",
            )],
        ));
        search.add_engine(MockEngine::new(
            "engine2",
            vec![
                SearchResult::new("https://example.com", "Example Site", "More content"),
                SearchResult::new("https://other.com", "Other", "Other content"),
            ],
        ));

        let query = SearchQuery::new("test");
        let results = search.search(query).await.unwrap();

        assert_eq!(results.items().len(), 2);

        let example = results
            .items()
            .iter()
            .find(|r| r.url == "https://example.com")
            .unwrap();
        assert_eq!(example.engines.len(), 2);
    }

    #[tokio::test]
    async fn test_search_records_duration() {
        let mut search = Search::new();
        search.add_engine(MockEngine::new("test", vec![]));

        let query = SearchQuery::new("test");
        let results = search.search(query).await.unwrap();

        // Duration should be recorded (u64 is always >= 0)
        let _ = results.duration_ms;
    }

    #[tokio::test]
    async fn test_search_filters_disabled_engines() {
        let mut search = Search::new();
        search.add_engine(MockEngine::new(
            "enabled",
            vec![SearchResult::new(
                "https://enabled.com",
                "Enabled",
                "Content",
            )],
        ));
        search.add_engine(
            MockEngine::new(
                "disabled",
                vec![SearchResult::new(
                    "https://disabled.com",
                    "Disabled",
                    "Content",
                )],
            )
            .disabled(),
        );

        let query = SearchQuery::new("test");
        let results = search.search(query).await.unwrap();

        assert_eq!(results.items().len(), 1);
        assert_eq!(results.items()[0].url, "https://enabled.com");
    }

    #[tokio::test]
    async fn test_search_filters_by_category() {
        let mut search = Search::new();
        search.add_engine(
            MockEngine::new(
                "general",
                vec![SearchResult::new(
                    "https://general.com",
                    "General",
                    "Content",
                )],
            )
            .with_category(EngineCategory::General),
        );
        search.add_engine(
            MockEngine::new(
                "images",
                vec![SearchResult::new("https://images.com", "Images", "Content")],
            )
            .with_category(EngineCategory::Images),
        );

        let query = SearchQuery::new("test").with_categories(vec![EngineCategory::Images]);
        let results = search.search(query).await.unwrap();

        assert_eq!(results.items().len(), 1);
        assert_eq!(results.items()[0].url, "https://images.com");
    }

    #[tokio::test]
    async fn test_search_filters_by_engine_shortcut() {
        let mut search = Search::new();
        search.add_engine(
            MockEngine::new(
                "engine1",
                vec![SearchResult::new("https://one.com", "One", "Content")],
            )
            .with_shortcut("e1"),
        );
        search.add_engine(
            MockEngine::new(
                "engine2",
                vec![SearchResult::new("https://two.com", "Two", "Content")],
            )
            .with_shortcut("e2"),
        );

        let query = SearchQuery::new("test").with_engines(vec!["e1".to_string()]);
        let results = search.search(query).await.unwrap();

        assert_eq!(results.items().len(), 1);
        assert_eq!(results.items()[0].url, "https://one.com");
    }

    #[tokio::test]
    async fn test_search_handles_engine_failure() {
        let mut search = Search::new();
        search.add_engine(MockEngine::new(
            "working",
            vec![SearchResult::new(
                "https://working.com",
                "Working",
                "Content",
            )],
        ));
        search.add_engine(FailingEngine::new("failing"));

        let query = SearchQuery::new("test");
        let results = search.search(query).await.unwrap();

        // Should still return results from working engine
        assert_eq!(results.items().len(), 1);
        assert_eq!(results.items()[0].url, "https://working.com");

        // Should record the engine error
        assert_eq!(results.errors().len(), 1);
        assert_eq!(results.errors()[0].0, "failing");
        assert!(results.errors()[0].1.contains("Engine failed"));
    }

    #[tokio::test]
    async fn test_search_all_engines_fail() {
        let mut search = Search::new();
        search.add_engine(FailingEngine::new("failing1"));
        search.add_engine(FailingEngine::new("failing2"));

        let query = SearchQuery::new("test");
        let results = search.search(query).await.unwrap();

        // Should return empty results, not error
        assert_eq!(results.items().len(), 0);

        // Should record both engine errors
        assert_eq!(results.errors().len(), 2);
    }

    #[tokio::test]
    async fn test_search_multiple_categories() {
        let mut search = Search::new();
        search.add_engine(
            MockEngine::new(
                "general",
                vec![SearchResult::new(
                    "https://general.com",
                    "General",
                    "Content",
                )],
            )
            .with_category(EngineCategory::General),
        );
        search.add_engine(
            MockEngine::new(
                "news",
                vec![SearchResult::new("https://news.com", "News", "Content")],
            )
            .with_category(EngineCategory::News),
        );
        search.add_engine(
            MockEngine::new(
                "images",
                vec![SearchResult::new("https://images.com", "Images", "Content")],
            )
            .with_category(EngineCategory::Images),
        );

        let query = SearchQuery::new("test")
            .with_categories(vec![EngineCategory::General, EngineCategory::News]);
        let results = search.search(query).await.unwrap();

        assert_eq!(results.items().len(), 2);
    }

    #[tokio::test]
    async fn test_search_set_proxy_pool() {
        use crate::proxy::{ProxyConfig, ProxyPool};

        let mut search = Search::new();
        assert!(search.proxy_pool().is_none());

        let proxy_pool = ProxyPool::with_proxies(vec![ProxyConfig::new("127.0.0.1", 8080)]);
        search.set_proxy_pool(proxy_pool);

        assert!(search.proxy_pool().is_some());
    }

    #[tokio::test]
    async fn test_search_proxy_pool_reference() {
        use crate::proxy::{ProxyConfig, ProxyPool};

        let mut search = Search::new();
        let proxy_pool = ProxyPool::with_proxies(vec![
            ProxyConfig::new("127.0.0.1", 8080),
            ProxyConfig::new("127.0.0.1", 8081),
        ]);
        search.set_proxy_pool(proxy_pool);

        let pool_ref = search.proxy_pool().unwrap();
        assert!(pool_ref.is_enabled());
    }
}