searchwala 8.3.0

Meta-search engine and zero-dependency MCP server in Rust: cross-engine consensus ranking, trafilatura + Readability extraction, domain controls, scholarly sources, and mode-specific synthesis with optional BYOK LLM
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
// ══════════════════════════════════════════════════════════════════════════════
// Generic HTML search engine driver
//
// v8.0 changes:
//   * `Box::leak` removed — specs own their template via Cow (P0-3). The old
//     code leaked a String per Google alias per variation per search, forever.
//   * Pagination is explicit via `PageParam` (P0-6). v7.0 had three bugs:
//       - ~25 engines declared pages:2 with no {offset}/{page} placeholder, so
//         they fetched the identical URL twice (~120 wasted requests/search).
//       - `page_idx + 1` skipped page 1 on 0-indexed engines (yandex, ecosia).
//       - Brave's `offset` is a PAGE index, not a row offset.
//   * HTTP status is checked before parsing (P0-2). v7.0 parsed 429/403/202
//     CAPTCHA pages as if they were result pages.
//   * `rank_position` is global across pages (P0-7) — it previously reset to 1
//     on every page, destroying the rank signal RRF depends on.
//   * Regional aliases carry real market/locale params (P0-8a) instead of all
//     resolving to one identical URL.
// ══════════════════════════════════════════════════════════════════════════════

use std::borrow::Cow;

use scraper::{Html, Selector};

use crate::fetcher::Fetcher;
use crate::models::RawSearchResult;

/// How an engine expresses "page N" in its query string.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum PageParam {
    /// No pagination support — exactly one request.
    None,
    /// Row offset in tens, 0-based: 0, 10, 20 (google, ddg html).
    Offset10,
    /// Row offset in tens, 1-based: 1, 11, 21 (bing, yahoo, mojeek).
    Offset1Based,
    /// Page index, 0-based: 0, 1, 2 (yandex, ecosia, brave).
    PageZero,
    /// Page index, 1-based: 1, 2, 3 (sogou, startpage).
    PageOne,
}

#[derive(Clone, Debug)]
pub struct GenericEngineSpec {
    pub endpoint_template: Cow<'static, str>,
    pub pages: usize,
    pub page_param: PageParam,
    /// Engine sits behind a WAF — fetch with TLS impersonation.
    pub stealth: bool,
}

impl GenericEngineSpec {
    fn stat(template: &'static str, pages: usize, page_param: PageParam) -> Self {
        Self {
            endpoint_template: Cow::Borrowed(template),
            pages,
            page_param,
            stealth: false,
        }
    }

    fn stat_stealth(template: &'static str, pages: usize, page_param: PageParam) -> Self {
        Self {
            endpoint_template: Cow::Borrowed(template),
            pages,
            page_param,
            stealth: true,
        }
    }

    fn owned_stealth(template: String, pages: usize, page_param: PageParam) -> Self {
        Self {
            endpoint_template: Cow::Owned(template),
            pages,
            page_param,
            stealth: true,
        }
    }

    /// A template with no placeholder cannot paginate — forcing 1 here is what
    /// eliminates the duplicate-request class of bug.
    pub fn effective_pages(&self) -> usize {
        if self.page_param == PageParam::None {
            1
        } else {
            self.pages.max(1)
        }
    }

    pub fn url_for_page(&self, encoded_query: &str, page_idx: usize) -> String {
        let value = match self.page_param {
            PageParam::None => 0,
            PageParam::Offset10 => page_idx * 10,
            PageParam::Offset1Based => page_idx * 10 + 1,
            PageParam::PageZero => page_idx,
            PageParam::PageOne => page_idx + 1,
        };
        let v = value.to_string();
        self.endpoint_template
            .replace("{query}", encoded_query)
            .replace("{offset}", &v)
            .replace("{page}", &v)
    }
}

pub struct GenericEngine {
    name: String,
    spec: GenericEngineSpec,
}

impl GenericEngine {
    pub fn new(name: &str, spec: GenericEngineSpec) -> Self {
        Self {
            name: name.to_string(),
            spec,
        }
    }
}

#[async_trait::async_trait]
impl super::SearchEngine for GenericEngine {
    fn name(&self) -> &str {
        &self.name
    }

    fn needs_stealth(&self) -> bool {
        self.spec.stealth
    }

    async fn search(&self, f: &Fetcher, query: &str) -> Vec<RawSearchResult> {
        let mut results = Vec::new();
        let mut seen = std::collections::HashSet::new();
        let encoded = urlencoding::encode(query).to_string();

        // Global, 1-based rank across all pages (P0-7).
        let mut rank_base = 0usize;

        for page_idx in 0..super::page_budget(self.spec.effective_pages()) {
            let url = self.spec.url_for_page(&encoded, page_idx);
            super::health::record_attempt(&self.name);

            let page = match f.get(&url, &[]).await {
                Ok(p) => p,
                Err(e) => {
                    tracing::debug!("{} transport error for {}: {}", self.name, url, e);
                    super::health::record_transport_err(&self.name);
                    break; // host unreachable — later pages will fail too
                }
            };

            if page.status != 200 {
                tracing::warn!("{}: HTTP {} — not parsing", self.name, page.status);
                super::health::record_block(&self.name, page.status);
                break;
            }
            if super::health::looks_like_challenge(&page.body) {
                tracing::warn!("{}: bot challenge in 200 response — not parsing", self.name);
                super::health::record_block(&self.name, 0);
                break;
            }

            let page_results =
                parse_generic_html(&page.body, &self.name, &page.final_url, rank_base);
            super::health::record_parsed(&self.name, page_results.len());
            if page_results.is_empty() && page_idx == 0 {
                super::health::dump_zero_result_body(&self.name, &url, &page.body);
            }

            if page_results.is_empty() {
                // 200 OK with nothing parsed => selector rot or end of results.
                break;
            }
            rank_base += page_results.len();

            for r in page_results {
                if seen.insert(r.url.clone()) {
                    results.push(r);
                }
            }
        }

        tracing::info!("{} [{}]: {} results", self.name, f.kind(), results.len());
        results
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// Parsing
// ══════════════════════════════════════════════════════════════════════════════

/// Pre-compiled selectors. v7.0 called `Selector::parse(..).unwrap()` on every
/// page of every engine — thousands of compilations per search.
static SELECTORS: std::sync::LazyLock<Vec<Selector>> = std::sync::LazyLock::new(|| {
    [
        // Engine-specific result containers first — far higher precision than
        // the generic fallbacks, which pick up nav/footer/related links.
        "li.b_algo h2 a",                 // Bing
        ".result__a",                     // DuckDuckGo html
        ".result__title a",               // DuckDuckGo html (alt)
        "a.result-link",                  // DuckDuckGo lite / Startpage
        "#results .snippet a[href]",      // Brave
        ".serp-item a.OrganicTitle-Link", // Yandex
        "ul.results-standard li a.title", // Mojeek
        "h3 a[href]",
        "h2 a[href]",
        // Generic fallbacks, lowest precision.
        "#search a[href]",
        "article a[href]",
        "main a[href]",
    ]
    .iter()
    .filter_map(|s| Selector::parse(s).ok())
    .collect()
});

pub fn parse_generic_html(
    html: &str,
    engine_name: &str,
    engine_url: &str,
    rank_base: usize,
) -> Vec<RawSearchResult> {
    let doc = Html::parse_document(html);
    let mut out = Vec::new();
    let mut seen = std::collections::HashSet::new();

    let host = url::Url::parse(engine_url)
        .ok()
        .and_then(|u| u.host_str().map(|h| h.to_string()))
        .unwrap_or_default();
    let host_root = registrable_root(&host);

    // Try selectors in precision order and stop at the first that produces a
    // plausible result set. v7.0 unioned ALL selectors, so a page that matched
    // a precise selector still had nav links from `li a[href]` appended.
    for sel in SELECTORS.iter() {
        let mut batch: Vec<RawSearchResult> = Vec::new();
        for link in doc.select(sel) {
            let Some(href) = link.value().attr("href") else {
                continue;
            };
            let Some(resolved) = resolve_target_url(href, engine_url) else {
                continue;
            };

            if !resolved.starts_with("http://") && !resolved.starts_with("https://") {
                continue;
            }
            // Drop self-links by registrable root, not substring-of-URL.
            if !host_root.is_empty() && url_root(&resolved) == host_root {
                continue;
            }
            if !seen.insert(resolved.clone()) {
                continue;
            }

            let title = link.text().collect::<String>().trim().to_string();
            if crate::util::char_len(&title) < 3 {
                continue;
            }

            let snippet = link
                .value()
                .attr("aria-label")
                .map(|s| s.to_string())
                .unwrap_or_default();

            batch.push(RawSearchResult {
                url: resolved,
                title,
                snippet,
                engine: engine_name.to_string(),
                rank_position: rank_base + batch.len() + 1,
                published: None,
                full_text: None,
            });

            if batch.len() >= 40 {
                break;
            }
        }

        if batch.len() >= 3 {
            // Good enough — a precise selector matched.
            out = batch;
            break;
        }
        // Keep the best partial result in case nothing better matches.
        if batch.len() > out.len() {
            out = batch;
        }
        seen.clear();
        for r in &out {
            seen.insert(r.url.clone());
        }
    }

    out
}

/// Registrable-ish root: last two labels of the host ("www.bing.com" ->
/// "bing.com"). Good enough to detect self-links without a PSL dependency.
fn registrable_root(host: &str) -> String {
    let h = host.trim_end_matches('.').to_lowercase();
    let parts: Vec<&str> = h.split('.').collect();
    if parts.len() <= 2 {
        return h;
    }
    parts[parts.len() - 2..].join(".")
}

fn url_root(url: &str) -> String {
    url::Url::parse(url)
        .ok()
        .and_then(|u| u.host_str().map(registrable_root))
        .unwrap_or_default()
}

fn resolve_target_url(href: &str, base: &str) -> Option<String> {
    if href.starts_with("http://") || href.starts_with("https://") {
        return Some(href.to_string());
    }
    if href.starts_with("//") {
        return Some(format!("https:{href}"));
    }

    // Google-style redirect: /url?q=<target>
    if href.starts_with("/url?") {
        if let Ok(parsed) = url::Url::parse(&format!("https://www.google.com{href}")) {
            for (k, v) in parsed.query_pairs() {
                if k == "q" || k == "url" {
                    return Some(v.to_string());
                }
            }
        }
    }

    // DuckDuckGo redirect: /l/?uddg=<percent-encoded target>
    if href.contains("uddg=") {
        let abs = if href.starts_with("http") {
            href.to_string()
        } else {
            format!("https://duckduckgo.com{href}")
        };
        if let Ok(parsed) = url::Url::parse(&abs) {
            for (k, v) in parsed.query_pairs() {
                if k == "uddg" {
                    return Some(
                        urlencoding::decode(&v)
                            .map(|d| d.to_string())
                            .unwrap_or_else(|_| v.to_string()),
                    );
                }
            }
        }
    }

    // Yahoo redirect: .../RU=<encoded>/RK=...
    if let Some(pos) = href.find("RU=") {
        let start = pos + 3;
        let end = href[start..]
            .find('/')
            .map(|e| start + e)
            .unwrap_or(href.len());
        if let Ok(decoded) = urlencoding::decode(&href[start..end]) {
            if decoded.starts_with("http") {
                return Some(decoded.to_string());
            }
        }
    }

    // Relative path — resolve against the engine URL.
    if href.starts_with('/') {
        if let Ok(b) = url::Url::parse(base) {
            if let Ok(joined) = b.join(href) {
                return Some(joined.to_string());
            }
        }
    }

    None
}

// ══════════════════════════════════════════════════════════════════════════════
// Engine specs
// ══════════════════════════════════════════════════════════════════════════════

pub fn spec_for(name: &str) -> Option<GenericEngineSpec> {
    let n = name.to_lowercase();

    // ── Google family ──────────────────────────────────────────────────────
    if n.starts_with("google") {
        let domain = google_domain_for_engine(&n);
        let hl = google_locale_for_engine(&n);

        if n.contains("_news") {
            return Some(GenericEngineSpec::owned_stealth(
                format!("https://{domain}/search?q={{query}}&tbm=nws&start={{offset}}&hl={hl}"),
                2,
                PageParam::Offset10,
            ));
        }
        if n.contains("_scholar") {
            return Some(GenericEngineSpec::stat_stealth(
                "https://scholar.google.com/scholar?q={query}&start={offset}",
                2,
                PageParam::Offset10,
            ));
        }
        // Image/video verticals return no usable text sources — dropped.
        if n.contains("_images") || n.contains("_videos") {
            return None;
        }
        // Regional aliases fetch ONE page: they share google.com-family hosts
        // under the per-host gate, and they exist for diversity, not depth.
        let pages = if n == "google" { 3 } else { 1 };
        return Some(GenericEngineSpec::owned_stealth(
            format!("https://{domain}/search?q={{query}}&start={{offset}}&hl={hl}"),
            pages,
            PageParam::Offset10,
        ));
    }

    // ── Bing family — regional aliases now carry a real market ─────────────
    if n.starts_with("bing") {
        if n.contains("_news") {
            return Some(GenericEngineSpec::stat_stealth(
                "https://www.bing.com/news/search?q={query}&first={offset}",
                2,
                PageParam::Offset1Based,
            ));
        }
        if n.contains("_images") || n.contains("_videos") {
            return None;
        }
        let market = bing_market_for_engine(&n);
        return Some(match market {
            Some(m) => GenericEngineSpec::owned_stealth(
                format!(
                    "https://www.bing.com/search?q={{query}}&first={{offset}}&mkt={m}&setlang={m}"
                ),
                3,
                PageParam::Offset1Based,
            ),
            None => GenericEngineSpec::stat_stealth(
                "https://www.bing.com/search?q={query}&first={offset}",
                3,
                PageParam::Offset1Based,
            ),
        });
    }

    // ── DuckDuckGo: only the html endpoint is scrapable. The _news/_images/
    //    _videos verticals need a vqd token, and in v7.0 all four aliases
    //    resolved to the SAME html URL — four identical requests.
    if n.starts_with("duckduckgo") {
        if n == "duckduckgo" || n == "duckduckgo_html" {
            return Some(GenericEngineSpec::stat_stealth(
                "https://html.duckduckgo.com/html/?q={query}&s={offset}",
                2,
                PageParam::Offset10,
            ));
        }
        return None;
    }

    // ── Brave — `offset` is a PAGE index (0,1,2), not a row offset ─────────
    if n.starts_with("brave") {
        let endpoint = if n.contains("_news") {
            "https://search.brave.com/news?q={query}&offset={offset}"
        } else {
            "https://search.brave.com/search?q={query}&source=web&offset={offset}"
        };
        return Some(GenericEngineSpec::stat_stealth(
            endpoint,
            3,
            PageParam::PageZero,
        ));
    }

    if n.starts_with("yahoo") {
        let endpoint = if n.contains("_news") {
            "https://news.search.yahoo.com/search?p={query}&b={offset}"
        } else {
            "https://search.yahoo.com/search?p={query}&b={offset}"
        };
        return Some(GenericEngineSpec::stat_stealth(
            endpoint,
            3,
            PageParam::Offset1Based,
        ));
    }

    // Yandex `p` is 0-based — v7.0 started at p=1 and never fetched page one.
    if n.starts_with("yandex") {
        return Some(GenericEngineSpec::stat_stealth(
            "https://yandex.com/search/?text={query}&p={page}",
            2,
            PageParam::PageZero,
        ));
    }

    // Ecosia `p` is 0-based — same off-by-one as Yandex in v7.0.
    if n.starts_with("ecosia") {
        return Some(GenericEngineSpec::stat_stealth(
            "https://www.ecosia.org/search?q={query}&p={page}",
            2,
            PageParam::PageZero,
        ));
    }

    if n.starts_with("mwmbl") {
        return Some(GenericEngineSpec::stat(
            "https://mwmbl.org/?q={query}",
            1,
            PageParam::None,
        ));
    }

    if n.starts_with("4get") {
        return Some(GenericEngineSpec::stat(
            "https://4get.ca/web?s={query}",
            1,
            PageParam::None,
        ));
    }

    if n.starts_with("yacy") {
        return Some(GenericEngineSpec::stat(
            "https://yacy.searchlab.eu/yacysearch.html?query={query}",
            1,
            PageParam::None,
        ));
    }

    if n.starts_with("sogou") {
        return Some(GenericEngineSpec::stat(
            "https://www.sogou.com/web?query={query}&page={page}",
            2,
            PageParam::PageOne,
        ));
    }

    if n.starts_with("baidu") {
        return Some(GenericEngineSpec::stat(
            "https://www.baidu.com/s?wd={query}&pn={offset}",
            2,
            PageParam::Offset10,
        ));
    }

    if n.starts_with("naver") {
        return Some(GenericEngineSpec::stat(
            "https://search.naver.com/search.naver?query={query}",
            1,
            PageParam::None,
        ));
    }

    if n.starts_with("seznam") {
        return Some(GenericEngineSpec::stat(
            "https://search.seznam.cz/?q={query}",
            1,
            PageParam::None,
        ));
    }

    if n.starts_with("swisscows") {
        return Some(GenericEngineSpec::stat(
            "https://swisscows.com/web?query={query}",
            1,
            PageParam::None,
        ));
    }

    if n.starts_with("metager") {
        return Some(GenericEngineSpec::stat(
            "https://metager.org/meta/meta.ger3?eingabe={query}",
            1,
            PageParam::None,
        ));
    }

    // ── REMOVED in v8.0 — measured dead or walled on 2026-10-01 ────────────
    // No DNS / connection refused: alexandria.org, results.excite.com,
    //   search.lycos.com, leta.mullvad.net, pipilika.com, searchencrypt.com
    // HTTP errors: whoogle.io 502, librex.devol.it 404, rightdao.com 503,
    //   ask.com/web 404, stract.com/search 404
    // JS/token walls with no HTML path: dogpile, webcrawler, info, searchalot,
    //   aol, gibiru, kiddle, presearch (403), yep (403)
    // `marginalia` is now served by engines::marginalia_api (JSON API).

    None
}

fn google_domain_for_engine(name: &str) -> &'static str {
    match name.rsplit('_').next().unwrap_or("") {
        "uk" => "www.google.co.uk",
        "in" => "www.google.co.in",
        "de" => "www.google.de",
        "fr" => "www.google.fr",
        "es" => "www.google.es",
        "it" => "www.google.it",
        "br" => "www.google.com.br",
        "jp" => "www.google.co.jp",
        "ca" => "www.google.ca",
        "au" => "www.google.com.au",
        "nl" => "www.google.nl",
        "se" => "www.google.se",
        "no" => "www.google.no",
        "fi" => "www.google.fi",
        _ => "www.google.com",
    }
}

/// `hl` makes a regional alias actually different. Without it every Google
/// alias produced an identical request (v7.0).
fn google_locale_for_engine(name: &str) -> &'static str {
    match name.rsplit('_').next().unwrap_or("") {
        "uk" => "en-GB",
        "in" => "en-IN",
        "de" => "de",
        "fr" => "fr",
        "es" => "es",
        "it" => "it",
        "br" => "pt-BR",
        "jp" => "ja",
        "ca" => "en-CA",
        "au" => "en-AU",
        "nl" => "nl",
        "se" => "sv",
        "no" => "no",
        "fi" => "fi",
        _ => "en",
    }
}

fn bing_market_for_engine(name: &str) -> Option<&'static str> {
    match name.rsplit('_').next().unwrap_or("") {
        "uk" => Some("en-GB"),
        "in" => Some("en-IN"),
        "de" => Some("de-DE"),
        "fr" => Some("fr-FR"),
        "es" => Some("es-ES"),
        "it" => Some("it-IT"),
        "jp" => Some("ja-JP"),
        "ca" => Some("en-CA"),
        "au" => Some("en-AU"),
        "nl" => Some("nl-NL"),
        "se" => Some("sv-SE"),
        "no" => Some("nb-NO"),
        "fi" => Some("fi-FI"),
        "us" => Some("en-US"),
        _ => None,
    }
}

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

    #[test]
    fn no_placeholder_means_exactly_one_request() {
        // The v7.0 bug: pages:2 with no {offset}/{page} fetched the same URL
        // twice. ~25 engines did this, wasting ~120 requests per search.
        let spec = GenericEngineSpec::stat("https://example.test/?q={query}", 2, PageParam::None);
        assert_eq!(spec.effective_pages(), 1);
        assert_eq!(
            spec.url_for_page("cats", 0),
            "https://example.test/?q=cats",
            "no placeholder, no pagination"
        );
    }

    #[test]
    fn page_zero_engines_start_at_zero() {
        // v7.0 used page_idx+1, so yandex/ecosia never fetched page one.
        let spec = GenericEngineSpec::stat(
            "https://y.test/?text={query}&p={page}",
            2,
            PageParam::PageZero,
        );
        assert_eq!(spec.url_for_page("x", 0), "https://y.test/?text=x&p=0");
        assert_eq!(spec.url_for_page("x", 1), "https://y.test/?text=x&p=1");
    }

    #[test]
    fn offset10_and_offset1based_differ_correctly() {
        let g = GenericEngineSpec::stat("g?start={offset}", 3, PageParam::Offset10);
        assert_eq!(g.url_for_page("q", 0), "g?start=0");
        assert_eq!(g.url_for_page("q", 2), "g?start=20");

        let b = GenericEngineSpec::stat("b?first={offset}", 3, PageParam::Offset1Based);
        assert_eq!(b.url_for_page("q", 0), "b?first=1");
        assert_eq!(b.url_for_page("q", 2), "b?first=21");
    }

    #[test]
    fn brave_offset_is_a_page_index() {
        // v7.0 computed offset = page*10 -> 0,10,20, which is far past the
        // result set. Brave's offset is the page number.
        let spec = spec_for("brave").unwrap();
        assert_eq!(spec.page_param, PageParam::PageZero);
        let urls: Vec<String> = (0..3).map(|i| spec.url_for_page("q", i)).collect();
        assert!(urls[1].ends_with("offset=1"), "got {}", urls[1]);
        assert!(urls[2].ends_with("offset=2"), "got {}", urls[2]);
    }

    #[test]
    fn bing_regional_aliases_build_distinct_urls() {
        // v7.0: 15 Bing aliases -> one identical URL.
        let base = spec_for("bing").unwrap().url_for_page("q", 0);
        let uk = spec_for("bing_uk").unwrap().url_for_page("q", 0);
        let de = spec_for("bing_de").unwrap().url_for_page("q", 0);
        assert_ne!(base, uk);
        assert_ne!(uk, de);
        assert!(uk.contains("mkt=en-GB"), "got {uk}");
        assert!(de.contains("mkt=de-DE"), "got {de}");
    }

    #[test]
    fn google_regional_aliases_build_distinct_urls() {
        let com = spec_for("google").unwrap().url_for_page("q", 0);
        let uk = spec_for("google_uk").unwrap().url_for_page("q", 0);
        let in_ = spec_for("google_in").unwrap().url_for_page("q", 0);
        assert!(com.contains("www.google.com") && com.contains("hl=en"));
        assert!(uk.contains("www.google.co.uk") && uk.contains("hl=en-GB"));
        assert!(in_.contains("www.google.co.in") && in_.contains("hl=en-IN"));
        assert_ne!(uk, in_);
    }

    #[test]
    fn duplicate_ddg_verticals_have_no_spec() {
        // All four resolved to the same html URL in v7.0.
        assert!(spec_for("duckduckgo").is_some());
        assert!(spec_for("duckduckgo_news").is_none());
        assert!(spec_for("duckduckgo_images").is_none());
        assert!(spec_for("duckduckgo_videos").is_none());
    }

    #[test]
    fn dead_engines_have_no_spec() {
        for dead in [
            "alexandria",
            "excite",
            "lycos",
            "mullvad_leta",
            "pipilika",
            "searchencrypt",
            "whoogle",
            "librex",
            "right_dao",
            "ask",
            "stract",
            "dogpile",
            "webcrawler",
            "info",
            "gibiru",
            "kiddle",
            "presearch",
            "yep",
            "aol",
            "searchalot",
        ] {
            assert!(spec_for(dead).is_none(), "{dead} should have been removed");
        }
    }

    #[test]
    fn waf_engines_are_marked_stealth() {
        for e in [
            "google",
            "bing",
            "duckduckgo",
            "brave",
            "yahoo",
            "yandex",
            "ecosia",
        ] {
            assert!(
                spec_for(e).unwrap().stealth,
                "{e} must use TLS impersonation"
            );
        }
        // Indie engines do not need it.
        assert!(!spec_for("mwmbl").unwrap().stealth);
        assert!(!spec_for("4get").unwrap().stealth);
    }

    #[test]
    fn rank_position_is_global_across_pages() {
        // v7.0 reset rank to 1 on every page, so position 21 scored like
        // position 1 in RRF.
        let html = r#"<html><body><div id="search">
            <h3><a href="https://a.test/1">Result One Title</a></h3>
            <h3><a href="https://b.test/2">Result Two Title</a></h3>
            <h3><a href="https://c.test/3">Result Three Title</a></h3>
        </div></body></html>"#;
        let p1 = parse_generic_html(html, "test", "https://engine.test/s", 0);
        assert_eq!(p1.len(), 3);
        assert_eq!(p1[0].rank_position, 1);
        assert_eq!(p1[2].rank_position, 3);

        let p2 = parse_generic_html(html, "test", "https://engine.test/s", 3);
        assert_eq!(p2[0].rank_position, 4, "page 2 must continue, not restart");
        assert_eq!(p2[2].rank_position, 6);
    }

    #[test]
    fn self_links_filtered_by_host_root_not_substring() {
        // "https://news.site.com/why-bing.com-lost" must NOT be dropped when
        // scraping bing.com — v7.0's `resolved.contains(&host)` dropped it.
        let html = r#"<html><body><div id="search">
            <h3><a href="https://news.site.com/why-bing.com-lost">Why Bing Lost The Race</a></h3>
            <h3><a href="https://www.bing.com/settings">Bing Settings Page</a></h3>
            <h3><a href="https://other.test/article">Another Real Article</a></h3>
            <h3><a href="https://third.test/page">Third Real Article</a></h3>
        </div></body></html>"#;
        let r = parse_generic_html(html, "bing", "https://www.bing.com/search?q=x", 0);
        let urls: Vec<&str> = r.iter().map(|x| x.url.as_str()).collect();
        assert!(
            urls.iter().any(|u| u.contains("news.site.com")),
            "legit article dropped: {urls:?}"
        );
        assert!(
            !urls.iter().any(|u| u.contains("bing.com/settings")),
            "self-link kept: {urls:?}"
        );
    }

    #[test]
    fn resolves_ddg_and_google_redirects() {
        assert_eq!(
            resolve_target_url(
                "/l/?uddg=https%3A%2F%2Fexample.com%2Fa",
                "https://html.duckduckgo.com/html/"
            ),
            Some("https://example.com/a".to_string())
        );
        assert_eq!(
            resolve_target_url(
                "/url?q=https://example.com/b&sa=U",
                "https://www.google.com/search"
            ),
            Some("https://example.com/b".to_string())
        );
        assert_eq!(
            resolve_target_url("//cdn.example.com/x", "https://e.test/"),
            Some("https://cdn.example.com/x".to_string())
        );
    }

    #[test]
    fn registrable_root_collapses_subdomains() {
        assert_eq!(registrable_root("www.bing.com"), "bing.com");
        assert_eq!(registrable_root("news.search.yahoo.com"), "yahoo.com");
        assert_eq!(registrable_root("bing.com"), "bing.com");
        assert_eq!(registrable_root("localhost"), "localhost");
    }

    /// Direct regression test for the P0-3 unbounded leak.
    ///
    /// v7.0's `spec_for` built Google templates with `format!` and then called
    /// `Box::leak` to coerce them to `&'static str`. Because `get_engines` was
    /// invoked inside every spawned dispatch task, each Google alias leaked a
    /// String on every query variation of every search, forever.
    ///
    /// This measures LIVE ALLOCATED BYTES via a counting allocator rather than
    /// process RSS. An earlier version of this test read /proc/self/status,
    /// which is process-wide and therefore polluted by the other tests running
    /// in parallel (the extractor suite loads an 890 KB fixture) — it reported
    /// a 15 MB "leak" that was really a neighbouring test's working set.
    #[test]
    fn spec_for_does_not_leak_across_many_calls() {
        use crate::testalloc;

        // Warm up: first-touch allocations and any lazy statics.
        for _ in 0..1_000 {
            std::hint::black_box(spec_for("google_uk"));
        }

        let before = testalloc::live_bytes();
        for _ in 0..100_000 {
            std::hint::black_box(spec_for("google"));
            std::hint::black_box(spec_for("google_uk"));
            std::hint::black_box(spec_for("google_news"));
            std::hint::black_box(spec_for("bing_de"));
        }
        let after = testalloc::live_bytes();

        // 400k spec_for calls. Under the old code every Google-variant call
        // leaked a ~60-byte String: ~18 MB retained. With Cow the allocations
        // are owned and dropped, so live bytes must be ~unchanged.
        let growth = after.saturating_sub(before);
        assert!(
            growth < 256 * 1024,
            "live allocations grew {growth} bytes over 400k spec_for calls \
             — Box::leak may be back"
        );
    }

    #[test]
    fn spec_templates_are_owned_or_borrowed_never_leaked() {
        use std::borrow::Cow;
        // Static templates stay Borrowed (zero alloc); dynamic ones are Owned
        // (freed on drop). Neither form can leak.
        assert!(matches!(
            spec_for("duckduckgo").unwrap().endpoint_template,
            Cow::Borrowed(_)
        ));
        assert!(matches!(
            spec_for("google_uk").unwrap().endpoint_template,
            Cow::Owned(_)
        ));
    }

    #[test]
    fn regional_aliases_fetch_one_page_only() {
        // Keeps the per-host gate from serializing deep pagination across
        // every alias of the same origin.
        assert_eq!(spec_for("google").unwrap().pages, 3);
        assert_eq!(spec_for("google_uk").unwrap().pages, 1);
        assert_eq!(spec_for("google_in").unwrap().pages, 1);
    }

    #[test]
    fn parsing_is_utf8_safe_on_devanagari_titles() {
        let html = format!(
            r#"<html><body><div id="search"><h3><a href="https://x.test/a">{}</a></h3></div></body></html>"#,
            "भारत और पाकिस्तान के बीच विश्व कप मुकाबला"
        );
        let r = parse_generic_html(&html, "test", "https://e.test/", 0);
        assert_eq!(r.len(), 1);
        assert!(r[0].title.contains("भारत"));
    }
}