Skip to main content

seer_core/
lookup.rs

1use std::collections::HashMap;
2use std::net::Ipv6Addr;
3use std::str::FromStr;
4use std::sync::{Arc, Mutex, Weak};
5use std::time::Duration;
6
7use chrono::{DateTime, Utc};
8use serde::{Deserialize, Serialize};
9use std::sync::LazyLock;
10use tokio::sync::Notify;
11use tracing::{debug, instrument, warn};
12
13use tokio::time::timeout as tokio_timeout;
14
15use crate::availability::{
16    classify_fallback, AvailabilityChecker, AvailabilityResult, Fallback, PriorRdap, PriorWhois,
17};
18use crate::cache::TtlCache;
19use crate::dns::{DnsPresence, DnsResolver};
20use crate::error::{Result, SeerError};
21use crate::rdap::{RdapClient, RdapResponse};
22use crate::whois::{get_registry_url, get_tld, WhoisClient, WhoisResponse};
23
24/// Cache TTL for lookup results (5 minutes).
25const LOOKUP_CACHE_TTL: Duration = Duration::from_secs(5 * 60);
26
27/// Grace period for the second protocol after the first one finishes *with
28/// usable data*. If RDAP answers and WHOIS hasn't responded within this
29/// window (or vice versa), we use the answer in hand rather than waiting the
30/// loser's full timeout. A winner that failed — or returned an unusable
31/// body — grants no such truncation: the other leg is then the only possible
32/// source of registry data and runs to its own bounded completion (see
33/// [`race_with_grace`]).
34const PROTOCOL_GRACE_PERIOD: Duration = Duration::from_secs(5);
35
36/// Maximum length for public-facing error strings.
37const MAX_PUBLIC_ERROR_LEN: usize = 256;
38
39/// Upper bound on one wait of a coalesced waiter on the owner's in-flight
40/// lookup. When it elapses the waiter re-checks the cache and then the
41/// in-flight map: an owner still running keeps its entry, so the waiter just
42/// waits another round, and only a vanished owner (its guard dropped without a
43/// cached result) lets it take over. The bound therefore need not cover a full
44/// lookup — which, with WHOIS/RDAP retries, referral hops and the
45/// availability fallback, can run well past a minute; it only caps how long a
46/// lost notification can delay a waiter.
47const DEFAULT_INFLIGHT_WAIT: Duration = Duration::from_secs(30);
48
49/// Global cache for lookup results to avoid redundant network calls.
50static LOOKUP_CACHE: LazyLock<TtlCache<String, LookupResult>> =
51    LazyLock::new(|| TtlCache::new(LOOKUP_CACHE_TTL));
52
53/// In-flight lookup coalescing map: normalized-domain -> `Weak<Notify>`.
54/// Only one network race runs per unique domain at a time; concurrent callers
55/// wait on the shared Notify and then read the result from LOOKUP_CACHE.
56static LOOKUP_INFLIGHT: LazyLock<Mutex<HashMap<String, Weak<Notify>>>> =
57    LazyLock::new(|| Mutex::new(HashMap::new()));
58
59// Regex patterns for stripping IP literals from public error messages.
60static_regex! {
61    IPV4_RE = r"\b(?:\d{1,3}\.){3}\d{1,3}\b";
62
63    /// Candidate pattern for IPv6 literals: a hex/colon token containing either
64    /// a `::` compression or at least three colons. This catches plausible IPv6
65    /// addresses cheaply; each match is then validated by `Ipv6Addr::from_str`
66    /// before redaction, so MAC fragments, hex hashes, and similar colon-laden
67    /// tokens are left alone.
68    IPV6_CANDIDATE_RE = r"\b[0-9a-fA-F:]*(?:::|(?:[0-9a-fA-F]{1,4}:){3,})[0-9a-fA-F:]*\b";
69}
70
71/// Redact substrings that parse as valid IPv6 addresses, leaving non-IPv6
72/// tokens (e.g. `af:ba:12`) untouched.
73fn strip_ipv6(msg: &str) -> String {
74    IPV6_CANDIDATE_RE
75        .replace_all(msg, |caps: &regex::Captures| {
76            let candidate = &caps[0];
77            if Ipv6Addr::from_str(candidate).is_ok() {
78                "[ip-redacted]".to_string()
79            } else {
80                candidate.to_string()
81            }
82        })
83        .into_owned()
84}
85
86/// Test-only hook: counts the number of times `lookup_concurrent` is actually
87/// invoked (i.e., the underlying network race runs). Used to verify request
88/// coalescing. Not exposed outside the crate.
89#[cfg(test)]
90static LOOKUP_CONCURRENT_CALLS: LazyLock<std::sync::atomic::AtomicUsize> =
91    LazyLock::new(|| std::sync::atomic::AtomicUsize::new(0));
92
93/// TTL for *degraded* lookup results: verdicts derived from DNS presence or
94/// a registry refusal, and WHOIS records without registration data, rather
95/// than from registry data (see [`is_degraded_result`]). Short so a transient rate limit or outage isn't
96/// served back for the full [`LOOKUP_CACHE_TTL`], yet non-zero so coalesced
97/// waiters (which read the owner's result from the cache) and bulk
98/// duplicates still share one network race.
99const DEGRADED_LOOKUP_CACHE_TTL: Duration = Duration::from_secs(30);
100
101/// Whether a lookup result is a stand-in for registry data we failed to get:
102/// an inconclusive verdict, one inferred from DNS presence (`dns_present` —
103/// "retry shortly for full detail" — and `dns_nxdomain`), or a WHOIS record
104/// with no registration data (a thin body kept because RDAP failed or
105/// returned an empty 200). Such results are cached only briefly.
106fn is_degraded_result(result: &LookupResult) -> bool {
107    match result {
108        LookupResult::Available { data, .. } => matches!(
109            data.method.as_str(),
110            "inconclusive" | "dns_present" | "dns_nxdomain"
111        ),
112        LookupResult::Whois { data, .. } => data.is_thin(),
113        LookupResult::Rdap { .. } => false,
114    }
115}
116
117/// Builds the `Available` variant for the routes of
118/// [`SmartLookup::lookup_concurrent`] that hold a WHOIS response. Every such
119/// route goes through here so none can forget to sanitize the public RDAP
120/// error string.
121fn available_with_whois(
122    avail: AvailabilityResult,
123    rdap_error: &str,
124    whois_data: WhoisResponse,
125) -> LookupResult {
126    LookupResult::Available {
127        data: Box::new(avail),
128        rdap_error: sanitize_error_for_public(rdap_error),
129        whois_error: String::new(),
130        whois_data: Some(whois_data),
131    }
132}
133
134/// Returns true if an RDAP response carries enough data to serve as the
135/// primary lookup result: the domain name plus at least one other piece of
136/// information (dates, entities, nameservers, or status).
137fn rdap_response_is_useful(response: &RdapResponse) -> bool {
138    let has_name = response.ldh_name.is_some() || response.unicode_name.is_some();
139    let has_dates = response
140        .events
141        .iter()
142        .any(|e| e.event_action == "registration" || e.event_action == "expiration");
143    let has_entities = !response.entities.is_empty();
144    let has_nameservers = !response.nameservers.is_empty();
145    let has_status = !response.status.is_empty();
146
147    has_name && (has_dates || has_entities || has_nameservers || has_status)
148}
149
150/// Race predicate for the WHOIS leg of [`race_with_grace`]: only a response
151/// carrying real registration data counts as "data in hand" for
152/// grace-truncation purposes. A thin body — e.g. an Identity-Digital-style
153/// "no WHOIS service" sentinel, or a "no match" banner — must not cut a
154/// viable in-flight RDAP query down to the grace period: RDAP is then the
155/// only possible source of registry data, and for "no match" bodies a late
156/// RDAP 200 must remain able to veto the availability claim (v0.26.6 rule).
157/// Mirrors the RDAP-side [`rdap_response_is_useful`] gate; thinness is
158/// [`WhoisResponse::is_thin`], the same signal the fallback ladders use.
159fn whois_leg_has_data(w: &Result<WhoisResponse>) -> bool {
160    matches!(w, Ok(data) if !data.is_thin())
161}
162
163/// The availability reading of a WHOIS answer when RDAP did not answer
164/// usefully. An RDAP HTTP 200 — even with a thin body — proves the domain
165/// object exists, so it vetoes every WHOIS- or DNS-derived "available"
166/// (WHOIS lags freshly provisioned domains; v0.26.6 rule) and the record is
167/// kept. Otherwise the answer is [`classify_fallback`]'s — the same ladder
168/// [`AvailabilityChecker`] uses, so the two paths cannot diverge.
169/// `rdap_err` is `None` for a grace-truncated RDAP leg.
170fn whois_leg_fallback(
171    domain: &str,
172    rdap_returned_200: bool,
173    rdap_err: Option<&SeerError>,
174    whois: &WhoisResponse,
175) -> Fallback {
176    if rdap_returned_200 {
177        return Fallback::Registered;
178    }
179    classify_fallback(domain, rdap_err, Ok(whois))
180}
181
182/// Progress line for an availability verdict reached with WHOIS in hand.
183fn verdict_progress(avail: &AvailabilityResult) -> &'static str {
184    match avail.method.as_str() {
185        "registrable_parent" => "Name is below a registrable domain (checked its parent)",
186        "inconclusive" => "Registry gave no usable answer (availability inconclusive)",
187        "dns_present" => "Domain is registered (registry detail unavailable)",
188        "dns_nxdomain" => "Domain appears unregistered (no DNS presence)",
189        _ if avail.available => "Domain appears unregistered",
190        _ => "Domain is registered",
191    }
192}
193
194/// Sanitizes an error message for inclusion in a public-facing response.
195///
196/// Strips IPv4 and IPv6 literals (to avoid leaking internal addresses when
197/// an SSRF guard rejects a resolved URL) and caps the total length to
198/// [`MAX_PUBLIC_ERROR_LEN`] characters.
199fn sanitize_error_for_public(msg: &str) -> String {
200    let s = IPV4_RE.replace_all(msg, "[ip-redacted]");
201    let s = strip_ipv6(&s);
202    if s.chars().count() > MAX_PUBLIC_ERROR_LEN {
203        let mut trunc: String = s.chars().take(MAX_PUBLIC_ERROR_LEN).collect();
204        trunc.push('…');
205        trunc
206    } else {
207        s
208    }
209}
210
211/// RAII guard for the in-flight-lookup slot. On drop, removes the entry
212/// from `LOOKUP_INFLIGHT` and notifies any waiters so they can read the
213/// freshly-populated cache.
214///
215/// NOTE on failed-owner retry semantics:
216/// When the owning task's lookup fails, `InflightGuard::drop` runs, the
217/// `HashMap` entry is removed, and `notify_waiters()` fires. Waiters wake,
218/// observe an empty cache, and one of them becomes the new owner — triggering
219/// a fresh network race. This means transient failures are automatically
220/// retried by any concurrent waiter. Callers that observe a timeout error
221/// should not assume no work is in flight; another concurrent caller may
222/// already be retrying.
223struct InflightGuard {
224    key: String,
225    notify: Arc<Notify>,
226}
227
228impl Drop for InflightGuard {
229    fn drop(&mut self) {
230        // Always remove the entry before notifying. The earlier `try_lock`
231        // design skipped removal under contention, but that left a stale
232        // `Weak<Notify>` in the map: a caller arriving in the brief window
233        // between `notify_waiters()` firing and the owner's `Arc<Notify>`
234        // dropping could upgrade the Weak, register as a waiter on the
235        // already-fired Notify, and block forever (notify_waiters only
236        // wakes currently-registered waiters; it does not accumulate
237        // permits for later registrations).
238        //
239        // Contention windows on this `std::sync::Mutex<HashMap>` are
240        // microseconds — the brief block here is safer than the stale-entry
241        // hazard. Poisoned-mutex recovery is preserved.
242        let mut inflight = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
243        inflight.remove(&self.key);
244        drop(inflight);
245        self.notify.notify_waiters();
246    }
247}
248
249/// Outcome of one leg of the concurrent RDAP/WHOIS race.
250///
251/// Tracks whether the leg completed naturally or was truncated by the grace
252/// period, so downstream error messages can distinguish a true timeout from a
253/// loser-truncation.
254enum LegOutcome<T> {
255    /// The leg ran to its own completion (success or error).
256    Completed(T),
257    /// The leg was abandoned because the other protocol answered first with
258    /// usable data and this leg did not finish within
259    /// [`PROTOCOL_GRACE_PERIOD`] of that answer.
260    GraceTruncated,
261}
262
263/// Races the RDAP and WHOIS legs of a smart lookup.
264///
265/// Whichever leg finishes first is inspected with its `*_has_data` predicate:
266///
267/// * Winner brought usable data → the still-running leg is merely
268///   supplementary, so it gets [`PROTOCOL_GRACE_PERIOD`] to finish before
269///   being truncated.
270/// * Winner came back empty-handed (an error, or a response the caller's
271///   predicate rejects) → the other leg is now the only possible source of
272///   registry data, so it runs to its own (already timeout-bounded)
273///   completion. This matters for RDAP-less TLDs such as .ru: the bootstrap
274///   miss fails in microseconds and must not shave the WHOIS budget down to
275///   the grace period. It also avoids pure waste — a truncated leg was
276///   re-queried in full by the availability fallback anyway.
277async fn race_with_grace<R, W>(
278    rdap_fut: impl std::future::Future<Output = R>,
279    whois_fut: impl std::future::Future<Output = W>,
280    rdap_has_data: impl Fn(&R) -> bool,
281    whois_has_data: impl Fn(&W) -> bool,
282) -> (LegOutcome<R>, LegOutcome<W>) {
283    tokio::pin!(rdap_fut);
284    tokio::pin!(whois_fut);
285
286    tokio::select! {
287        rdap_res = &mut rdap_fut => {
288            let whois_leg = if rdap_has_data(&rdap_res) {
289                match tokio_timeout(PROTOCOL_GRACE_PERIOD, whois_fut).await {
290                    Ok(res) => LegOutcome::Completed(res),
291                    Err(_) => LegOutcome::GraceTruncated,
292                }
293            } else {
294                LegOutcome::Completed(whois_fut.await)
295            };
296            (LegOutcome::Completed(rdap_res), whois_leg)
297        }
298        whois_res = &mut whois_fut => {
299            let rdap_leg = if whois_has_data(&whois_res) {
300                match tokio_timeout(PROTOCOL_GRACE_PERIOD, rdap_fut).await {
301                    Ok(res) => LegOutcome::Completed(res),
302                    Err(_) => LegOutcome::GraceTruncated,
303                }
304            } else {
305                LegOutcome::Completed(rdap_fut.await)
306            };
307            (rdap_leg, LegOutcome::Completed(whois_res))
308        }
309    }
310}
311
312/// Public-facing error string for a grace-truncated leg. Truncation only
313/// happens when the other protocol answered first with usable data (see
314/// [`race_with_grace`]), so the wording says the winner "answered" — the old
315/// "after RDAP won" phrasing misled when the winner had merely finished first
316/// with an error.
317fn grace_truncated_error(truncated: &str, winner: &str) -> String {
318    format!(
319        "{} did not respond within the {}s grace period after {} answered",
320        truncated,
321        PROTOCOL_GRACE_PERIOD.as_secs(),
322        winner
323    )
324}
325
326/// Internal classification of the RDAP leg of a concurrent lookup.
327///
328/// Distinguishing `NoData` (HTTP 200 but response was missing useful fields)
329/// from `Error` lets the orchestrator prefer a thin WHOIS result over the
330/// availability fallback when RDAP silently returned nothing.
331enum RdapOutcome {
332    Useful(RdapResponse),
333    NoData(RdapResponse),
334    Error(SeerError),
335    /// RDAP future did not complete within the grace period after WHOIS
336    /// answered with a response.
337    GraceTimeout,
338}
339
340/// Progress callback for smart lookup operations.
341/// Called with a message describing the current phase of the lookup.
342pub type LookupProgressCallback = Arc<dyn Fn(&str) + Send + Sync>;
343
344#[derive(Debug, Clone, Serialize, Deserialize)]
345#[serde(tag = "source", rename_all = "lowercase")]
346pub enum LookupResult {
347    Rdap {
348        data: Box<RdapResponse>,
349        #[serde(skip_serializing_if = "Option::is_none")]
350        whois_fallback: Option<WhoisResponse>,
351    },
352    Whois {
353        data: WhoisResponse,
354        rdap_error: Option<String>,
355        #[serde(skip_serializing_if = "Option::is_none")]
356        rdap_fallback: Option<Box<RdapResponse>>,
357    },
358    Available {
359        data: Box<AvailabilityResult>,
360        rdap_error: String,
361        whois_error: String,
362        /// Raw WHOIS response, when one was available at routing time
363        /// (Cases A and B in the design spec). `None` preserves the
364        /// pre-existing "both protocols errored" semantics.
365        #[serde(default, skip_serializing_if = "Option::is_none")]
366        whois_data: Option<WhoisResponse>,
367    },
368}
369
370impl LookupResult {
371    /// The protocol whose record this result carries — `"RDAP"` or
372    /// `"WHOIS"` — or `None` for an availability verdict (neither registry
373    /// had the domain). Callers pick their own fallback text.
374    pub fn protocol(&self) -> Option<&'static str> {
375        match self {
376            LookupResult::Rdap { .. } => Some("RDAP"),
377            LookupResult::Whois { .. } => Some("WHOIS"),
378            LookupResult::Available { .. } => None,
379        }
380    }
381
382    /// Returns the domain name from the lookup result, in seer's normalized
383    /// form (lowercase A-labels) whichever protocol answered: registries
384    /// spell RDAP's `ldhName` in any case (`EXAMPLE.COM`) and some send only
385    /// a `unicodeName`, while WHOIS and availability results already carry
386    /// the normalized queried name.
387    pub fn domain_name(&self) -> Option<String> {
388        match self {
389            LookupResult::Rdap { data, .. } => data.domain_name().map(normalize_rdap_name),
390            LookupResult::Whois { data, .. } => Some(data.domain.clone()),
391            LookupResult::Available { data, .. } => Some(data.domain.clone()),
392        }
393    }
394
395    /// Reads one registration field: from RDAP with the attached WHOIS record
396    /// as fallback, from WHOIS alone, or `None` for an availability verdict.
397    fn rdap_or_whois<T>(
398        &self,
399        rdap: impl FnOnce(&RdapResponse) -> Option<T>,
400        whois: impl FnOnce(&WhoisResponse) -> Option<T>,
401    ) -> Option<T> {
402        match self {
403            LookupResult::Rdap {
404                data,
405                whois_fallback,
406            } => rdap(data).or_else(|| whois_fallback.as_ref().and_then(whois)),
407            LookupResult::Whois { data, .. } => whois(data),
408            LookupResult::Available { .. } => None,
409        }
410    }
411
412    /// Returns the registrar name, preferring RDAP data with WHOIS fallback.
413    pub fn registrar(&self) -> Option<String> {
414        self.rdap_or_whois(RdapResponse::get_registrar, |w| w.registrar.clone())
415    }
416
417    /// Returns the registrant organization, preferring RDAP data with WHOIS fallback.
418    pub fn organization(&self) -> Option<String> {
419        self.rdap_or_whois(RdapResponse::get_registrant_organization, |w| {
420            w.organization.clone()
421        })
422    }
423
424    /// Returns the creation date, preferring RDAP data with WHOIS fallback.
425    pub fn creation_date(&self) -> Option<DateTime<Utc>> {
426        self.rdap_or_whois(RdapResponse::creation_date, |w| w.creation_date)
427    }
428
429    /// Returns the expiration date, preferring RDAP data with WHOIS fallback.
430    pub fn expiration_date(&self) -> Option<DateTime<Utc>> {
431        self.rdap_or_whois(RdapResponse::expiration_date, |w| w.expiration_date)
432    }
433
434    /// Returns true if the result came from RDAP.
435    pub fn is_rdap(&self) -> bool {
436        matches!(self, LookupResult::Rdap { .. })
437    }
438
439    /// Returns true if the result came from WHOIS.
440    pub fn is_whois(&self) -> bool {
441        matches!(self, LookupResult::Whois { .. })
442    }
443
444    /// Returns true if the result is an availability check fallback.
445    pub fn is_available(&self) -> bool {
446        matches!(self, LookupResult::Available { .. })
447    }
448
449    /// Returns the expiration date and registrar info from the lookup result.
450    pub fn expiration_info(&self) -> (Option<DateTime<Utc>>, Option<String>) {
451        (self.expiration_date(), self.registrar())
452    }
453}
454
455/// Normalizes a registry-reported RDAP domain name to seer's canonical
456/// form: no trailing root dot, lowercase, A-labels.
457fn normalize_rdap_name(name: &str) -> String {
458    let name = name.trim_end_matches('.').to_lowercase();
459    crate::validation::domain_to_ascii(&name).unwrap_or(name)
460}
461
462/// Truncates `s` to at most `max` bytes, backing up to the nearest UTF-8 char
463/// boundary at or below `max`. `String::truncate` panics if the byte offset is
464/// not a char boundary, and WHOIS `raw_response` is server-controlled and may
465/// preserve multi-byte UTF-8, so we must not truncate blindly at a fixed byte
466/// offset. (`str::floor_char_boundary` would do this, but it is unstable on
467/// stable Rust, so we walk backwards manually.)
468fn truncate_on_char_boundary(s: &mut String, max: usize) {
469    if s.len() > max {
470        let mut end = max;
471        while end > 0 && !s.is_char_boundary(end) {
472            end -= 1;
473        }
474        s.truncate(end);
475    }
476}
477
478/// Trims an oversized WHOIS `raw_response` in place (char-boundary safe),
479/// appending a truncation marker. A raw body can be up to 1 MB (the WHOIS
480/// client's response cap); 32 KB is plenty for the parsed fields while
481/// bounding memory wherever responses are retained — the lookup cache here
482/// and the bulk executor's buffered results.
483pub(crate) fn trim_whois_raw(whois: &mut WhoisResponse) {
484    const MAX_RAW: usize = 32 * 1024;
485    if whois.raw_response.len() > MAX_RAW {
486        truncate_on_char_boundary(&mut whois.raw_response, MAX_RAW);
487        whois.raw_response.push_str("\n... [truncated]");
488    }
489}
490
491/// Trims any retained raw WHOIS body inside a [`LookupResult`] via
492/// [`trim_whois_raw`]. Applied before caching lookups and by the bulk
493/// executor before buffering results.
494pub(crate) fn trim_raw_response(mut result: LookupResult) -> LookupResult {
495    match result {
496        LookupResult::Whois { ref mut data, .. } => trim_whois_raw(data),
497        LookupResult::Rdap {
498            ref mut whois_fallback,
499            ..
500        } => {
501            if let Some(w) = whois_fallback {
502                trim_whois_raw(w);
503            }
504        }
505        LookupResult::Available {
506            ref mut whois_data, ..
507        } => {
508            if let Some(w) = whois_data {
509                trim_whois_raw(w);
510            }
511        }
512    }
513
514    result
515}
516
517#[derive(Debug, Clone, Default)]
518pub struct SmartLookup {
519    rdap_client: RdapClient,
520    whois_client: WhoisClient,
521    availability_checker: AvailabilityChecker,
522    dns_resolver: DnsResolver,
523}
524
525impl SmartLookup {
526    /// Creates a new SmartLookup that runs RDAP and WHOIS concurrently,
527    /// falling back to an availability check if both fail.
528    pub fn new() -> Self {
529        Self::default()
530    }
531
532    /// Builds a SmartLookup whose RDAP/WHOIS/DNS sub-clients honor the
533    /// per-protocol timeouts in `config`.
534    pub fn from_config(config: &crate::config::SeerConfig) -> Self {
535        Self {
536            rdap_client: RdapClient::from_config(config),
537            whois_client: WhoisClient::from_config(config),
538            availability_checker: AvailabilityChecker::from_config(config),
539            dns_resolver: DnsResolver::from_config(config),
540        }
541    }
542
543    /// Performs a smart lookup for a domain, trying both RDAP and WHOIS concurrently.
544    /// Falls back to an availability check if both fail.
545    /// Results are cached for 5 minutes to avoid redundant network calls
546    /// (30 seconds for degraded, DNS-inferred or inconclusive verdicts).
547    #[instrument(skip(self), fields(domain = %domain))]
548    pub async fn lookup(&self, domain: &str) -> Result<LookupResult> {
549        self.lookup_with_progress(domain, None).await
550    }
551
552    /// Performs a lookup with an optional progress callback.
553    /// The callback is called with messages describing the current phase.
554    /// Results are cached for 5 minutes (30 seconds for degraded verdicts).
555    /// Concurrent lookups for the same domain are coalesced — only one
556    /// network race runs per domain at a time.
557    #[instrument(skip(self, progress), fields(domain = %domain))]
558    pub async fn lookup_with_progress(
559        &self,
560        domain: &str,
561        progress: Option<LookupProgressCallback>,
562    ) -> Result<LookupResult> {
563        let normalized = crate::validation::normalize_domain(domain)?;
564
565        // Check cache first
566        if let Some(cached) = LOOKUP_CACHE.get(&normalized) {
567            debug!(domain = %normalized, "Returning cached lookup result");
568            return Ok(cached);
569        }
570
571        // Coalesce in-flight lookups: if another task is already running a
572        // race for this domain, wait on its Notify rather than starting a
573        // second race. Two branches:
574        //   - Waiter: another task owns the slot; await its notify, then
575        //     read the cache. If the cache is still empty (owner failed),
576        //     loop and re-contend for ownership.
577        //   - Owner: no entry exists; insert a Weak handle, hold the Arc
578        //     for the duration of the work, then remove and notify on drop.
579        //
580        // Each iteration takes the map lock, then either claims ownership or
581        // subscribes as a waiter *before* releasing it. The `MutexGuard` is
582        // always dropped before any `.await`.
583        let _guard = loop {
584            let existing: Arc<Notify>;
585            let notified = {
586                // Recover from poisoning rather than panicking: a prior
587                // owner's panic should not permanently wedge the in-flight
588                // tracker for every future lookup.
589                let mut inflight = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
590                match inflight.get(&normalized).and_then(|w| w.upgrade()) {
591                    Some(n) => existing = n,
592                    None => {
593                        let n = Arc::new(Notify::new());
594                        inflight.insert(normalized.clone(), Arc::downgrade(&n));
595                        break InflightGuard {
596                            key: normalized.clone(),
597                            notify: n,
598                        };
599                    }
600                }
601                // Subscribe while still holding the map lock. The owner's
602                // `InflightGuard::drop` takes this same lock to remove the
603                // entry before calling `notify_waiters()`, and a `Notified`
604                // future receives `notify_waiters()` from the moment it is
605                // created — so the notification cannot fire between us
606                // finding the entry and subscribing. Subscribing after the
607                // unlock left a gap in which a cancelled owner's
608                // notification was lost and this waiter slept the full
609                // `DEFAULT_INFLIGHT_WAIT`.
610                existing.notified()
611            };
612            tokio::pin!(notified);
613            // Also register eagerly as a waiter (before any await).
614            notified.as_mut().enable();
615            debug!(domain = %normalized, "Waiting for in-flight lookup to complete");
616
617            // Re-check the cache now that we're subscribed: the owner may
618            // already have populated it.
619            if let Some(cached) = LOOKUP_CACHE.get(&normalized) {
620                return Ok(cached);
621            }
622
623            // Bounded wait: if the owner's future hangs, or a notification is
624            // otherwise lost, fall through and re-contend for ownership rather
625            // than blocking forever.
626            let _ = tokio_timeout(DEFAULT_INFLIGHT_WAIT, notified.as_mut()).await;
627
628            if let Some(cached) = LOOKUP_CACHE.get(&normalized) {
629                return Ok(cached);
630            }
631            // Owner finished without populating the cache (failed or
632            // errored), or the wait timed out. Re-contend for ownership.
633        };
634
635        // Re-check the cache now that we own the slot: a previous owner may
636        // have finished (populated the cache and released the slot) between
637        // our first cache check and taking the lock, and running a second
638        // full lookup for it would be pure waste. Returning drops `_guard`,
639        // which releases any waiters to read the same cached value.
640        if let Some(cached) = LOOKUP_CACHE.get(&normalized) {
641            debug!(domain = %normalized, "Returning result cached by previous owner");
642            return Ok(cached);
643        }
644
645        let result = self.lookup_concurrent(&normalized, progress).await?;
646
647        // Cache a trimmed copy to limit memory usage before releasing
648        // waiters (via guard drop) so they observe the cached value. A
649        // degraded verdict (no registry data) only gets a short TTL so the
650        // next lookup soon retries the registries.
651        let ttl = if is_degraded_result(&result) {
652            DEGRADED_LOOKUP_CACHE_TTL
653        } else {
654            LOOKUP_CACHE_TTL
655        };
656        LOOKUP_CACHE.insert_with_ttl(normalized.clone(), trim_raw_response(result.clone()), ttl);
657
658        Ok(result)
659    }
660
661    #[instrument(skip(self, progress), fields(domain = %domain))]
662    async fn lookup_concurrent(
663        &self,
664        domain: &str,
665        progress: Option<LookupProgressCallback>,
666    ) -> Result<LookupResult> {
667        #[cfg(test)]
668        LOOKUP_CONCURRENT_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
669
670        debug!(domain = %domain, "Attempting RDAP and WHOIS concurrently");
671
672        if let Some(ref cb) = progress {
673            cb("Querying RDAP and WHOIS concurrently");
674        }
675
676        let rdap_fut = self.rdap_client.lookup_domain(domain);
677        let whois_fut = self.whois_client.lookup(domain);
678
679        // Race: a winner with usable data grants the loser only a grace
680        // period; a winner that failed (or returned an unusable body) leaves
681        // the loser as the sole possible data source, so it runs to its own
682        // bounded completion. Both predicates gate on usefulness — RDAP via
683        // `rdap_response_is_useful`, WHOIS via `whois_leg_has_data`
684        // (non-thin) — so neither side's empty answer can truncate the
685        // other. See `race_with_grace`.
686        let (rdap_leg, whois_leg) = race_with_grace(
687            rdap_fut,
688            whois_fut,
689            |r| matches!(r, Ok(data) if rdap_response_is_useful(data)),
690            whois_leg_has_data,
691        )
692        .await;
693
694        if matches!(whois_leg, LegOutcome::GraceTruncated) {
695            debug!("WHOIS did not finish within grace period after RDAP answered, proceeding with RDAP only");
696        }
697        if matches!(rdap_leg, LegOutcome::GraceTruncated) {
698            debug!("RDAP did not finish within grace period after WHOIS answered, proceeding with WHOIS only");
699        }
700
701        // Classify the RDAP leg.
702        let rdap_outcome = match rdap_leg {
703            LegOutcome::Completed(Ok(data)) => {
704                if rdap_response_is_useful(&data) {
705                    RdapOutcome::Useful(data)
706                } else {
707                    RdapOutcome::NoData(data)
708                }
709            }
710            LegOutcome::Completed(Err(e)) => RdapOutcome::Error(e),
711            LegOutcome::GraceTruncated => RdapOutcome::GraceTimeout,
712        };
713
714        // Phase 1: If RDAP returned useful data, use it as primary.
715        if let RdapOutcome::Useful(rdap_data) = rdap_outcome {
716            debug!("RDAP lookup successful");
717            let whois_fallback = match whois_leg {
718                LegOutcome::Completed(Ok(w)) => Some(w),
719                _ => None,
720            };
721            return Ok(LookupResult::Rdap {
722                data: Box::new(rdap_data),
723                whois_fallback,
724            });
725        }
726
727        // RDAP was not useful (NoData, Error, or GraceTimeout). Prefer WHOIS
728        // if it returned any response, even a thin one — this is safer than
729        // falling back to the availability heuristic when we have actual
730        // registry data in hand.
731        //
732        // We separately track whether RDAP returned an HTTP 200 (NoData):
733        // even a thin RDAP 200 is positive evidence the domain object
734        // exists. In that case we must NOT reclassify a WHOIS "no match"
735        // signal as availability — WHOIS lag against a freshly-provisioned
736        // domain would otherwise produce a false "available" verdict.
737        let rdap_returned_200 = matches!(rdap_outcome, RdapOutcome::NoData(_));
738        let (rdap_error_str, rdap_fallback_data, rdap_seer_error) = match rdap_outcome {
739            RdapOutcome::Useful(_) => {
740                // Unreachable in this branch (we returned above), but handle
741                // defensively rather than panicking across the FFI boundary.
742                debug!("Unexpected RdapOutcome::Useful in fallback branch");
743                (String::from("RDAP ok"), None, None)
744            }
745            RdapOutcome::NoData(data) => (
746                "RDAP response incomplete".to_string(),
747                Some(Box::new(data)),
748                None,
749            ),
750            RdapOutcome::Error(e) => (e.to_string(), None, Some(e)),
751            RdapOutcome::GraceTimeout => (grace_truncated_error("RDAP", "WHOIS"), None, None),
752        };
753
754        if let LegOutcome::Completed(Ok(whois_data)) = whois_leg {
755            // Read the WHOIS answer through the shared availability ladder.
756            // The DNS probe runs only when the registry signals are silent,
757            // so the common paths never pay for it.
758            let fallback = whois_leg_fallback(
759                domain,
760                rdap_returned_200,
761                rdap_seer_error.as_ref(),
762                &whois_data,
763            );
764            let verdict = match fallback {
765                Fallback::Registered => None,
766                Fallback::Verdict(avail) => Some(avail),
767                Fallback::NeedsDns(tie_break) => {
768                    Some(tie_break.decide(self.dns_resolver.presence(domain).await))
769                }
770            };
771            if let Some(avail) = verdict {
772                debug!(
773                    domain = %domain,
774                    method = %avail.method,
775                    confidence = %avail.confidence,
776                    "Reading WHOIS leg as an availability verdict"
777                );
778                // A registry "no such domain" for a name below its
779                // registrable domain (mail.google.com) is not availability.
780                let avail = self.availability_checker.guard_subdomain_claim(avail).await;
781                if let Some(ref cb) = progress {
782                    cb(verdict_progress(&avail));
783                }
784                return Ok(available_with_whois(avail, &rdap_error_str, whois_data));
785            }
786            debug!("Using WHOIS result (RDAP not useful)");
787            if let Some(ref cb) = progress {
788                cb("RDAP not available (using WHOIS)");
789            }
790            return Ok(LookupResult::Whois {
791                data: whois_data,
792                rdap_error: Some(sanitize_error_for_public(&rdap_error_str)),
793                rdap_fallback: rdap_fallback_data,
794            });
795        }
796
797        // Both sides failed to provide useful data. Craft a precise WHOIS
798        // error string that distinguishes true errors from grace-period
799        // truncation. Borrow the leg here so we can move it into the reuse
800        // token below.
801        let whois_error_str = match &whois_leg {
802            LegOutcome::Completed(Err(e)) => e.to_string(),
803            LegOutcome::Completed(Ok(_)) => {
804                // Already handled above; treat defensively.
805                debug!("Unexpected completed-Ok WHOIS in availability fallback branch");
806                "WHOIS returned but was not used".to_string()
807            }
808            // Unreachable under `race_with_grace` semantics: WHOIS is only
809            // truncated behind a useful RDAP answer, and that path returned
810            // early above. Kept as a defensive arm.
811            LegOutcome::GraceTruncated => grace_truncated_error("WHOIS", "RDAP"),
812        };
813
814        // Reuse what the race already learned rather than re-querying the same
815        // registries: a thin HTTP 200 still proves the object exists (feed it
816        // back), a concrete error is reused as-is, and only a grace-truncated
817        // leg (genuinely missing) is re-queried by the checker.
818        let prior_rdap = if rdap_returned_200 {
819            match rdap_fallback_data {
820                // `rdap_fallback_data` is already the boxed NoData response.
821                Some(resp) => PriorRdap::Response(resp),
822                None => PriorRdap::Missing,
823            }
824        } else if let Some(e) = rdap_seer_error {
825            PriorRdap::Failed(e)
826        } else {
827            PriorRdap::Missing
828        };
829        let prior_whois = match whois_leg {
830            LegOutcome::Completed(Err(e)) => PriorWhois::Failed(e),
831            // Handled above; the availability path never reuses an Ok WHOIS.
832            LegOutcome::Completed(Ok(_)) => PriorWhois::Missing,
833            LegOutcome::GraceTruncated => PriorWhois::Missing,
834        };
835
836        self.availability_fallback(
837            domain,
838            prior_rdap,
839            prior_whois,
840            rdap_error_str,
841            whois_error_str,
842            progress,
843        )
844        .await
845    }
846
847    async fn availability_fallback(
848        &self,
849        domain: &str,
850        prior_rdap: PriorRdap,
851        prior_whois: PriorWhois,
852        rdap_error: String,
853        whois_error: String,
854        progress: Option<LookupProgressCallback>,
855    ) -> Result<LookupResult> {
856        if let Some(ref cb) = progress {
857            cb("RDAP and WHOIS unavailable (checking availability)");
858        }
859        warn!(
860            domain = %domain,
861            rdap_error = %rdap_error,
862            whois_error = %whois_error,
863            "Both RDAP and WHOIS failed, falling back to availability check"
864        );
865
866        match self
867            .availability_checker
868            .check_with_prior(domain, prior_rdap, prior_whois)
869            .await
870        {
871            Ok(avail) => Ok(LookupResult::Available {
872                data: Box::new(avail),
873                rdap_error: sanitize_error_for_public(&rdap_error),
874                whois_error: sanitize_error_for_public(&whois_error),
875                whois_data: None,
876            }),
877            Err(avail_err) => Err(SeerError::LookupFailed {
878                domain: domain.to_string(),
879                details: format!(
880                    "RDAP failed ({}), WHOIS failed ({}), availability check failed ({})",
881                    rdap_error, whois_error, avail_err
882                ),
883                registry_url: get_registry_url(get_tld(domain)),
884            }),
885        }
886    }
887
888    /// DNS presence passthrough over this lookup's private resolver, exposing
889    /// the same cheap availability pre-signal the fallback ladder uses. The
890    /// confusables scan uses it to skip NXDOMAIN candidates before paying for a
891    /// full RDAP+WHOIS race on each one.
892    pub(crate) async fn presence(&self, domain: &str) -> DnsPresence {
893        self.dns_resolver.presence(domain).await
894    }
895}
896
897#[cfg(test)]
898mod tests {
899    use super::*;
900    use crate::rdap::rdap_error_is_404;
901
902    /// Global serialization mutex for the tests that share `LOOKUP_INFLIGHT`
903    /// or `LOOKUP_CACHE` state (map coalescing, waiter coalescing, cache
904    /// clear, poison recovery, drop recovery).
905    /// Running them in parallel creates two races:
906    ///   1. Guard drop uses `try_lock`; if another test holds the mutex, the
907    ///      Drop path skips cleanup → stale entries fail later assertions.
908    ///   2. Poisoning one test leaves the mutex poisoned for the next test,
909    ///      which is handled by `unwrap_or_else` but still disturbs state.
910    ///
911    /// Per-test unique keys (see `unique_test_key`) prevent entry-level
912    /// collisions; this mutex prevents lock-contention races on Drop.
913    static INFLIGHT_TEST_SERIAL: Mutex<()> = Mutex::new(());
914
915    #[test]
916    fn test_lookup_result_domain_name_whois() {
917        let result = LookupResult::Whois {
918            data: WhoisResponse {
919                domain: "example.com".to_string(),
920                registrar: Some("Test Registrar".to_string()),
921                whois_server: "whois.example.com".to_string(),
922                ..Default::default()
923            },
924            rdap_error: None,
925            rdap_fallback: None,
926        };
927
928        assert_eq!(result.domain_name(), Some("example.com".to_string()));
929        assert_eq!(result.registrar(), Some("Test Registrar".to_string()));
930        assert!(result.is_whois());
931        assert!(!result.is_rdap());
932        assert!(!result.is_available());
933    }
934
935    #[test]
936    fn rdap_domain_name_is_normalized_like_the_other_protocols() {
937        for (ldh, unicode) in [
938            (Some("EXAMPLE.COM"), None),
939            (Some("Example.Com."), None),
940            (None, Some("пример.рф")),
941        ] {
942            let result = LookupResult::Rdap {
943                data: Box::new(RdapResponse {
944                    ldh_name: ldh.map(str::to_string),
945                    unicode_name: unicode.map(str::to_string),
946                    ..Default::default()
947                }),
948                whois_fallback: None,
949            };
950            let expected = if unicode.is_some() {
951                "xn--e1afmkfd.xn--p1ai"
952            } else {
953                "example.com"
954            };
955            assert_eq!(result.domain_name().as_deref(), Some(expected));
956        }
957    }
958
959    #[test]
960    fn test_lookup_result_serialization() {
961        let result = LookupResult::Whois {
962            data: WhoisResponse {
963                domain: "test.com".to_string(),
964                ..Default::default()
965            },
966            rdap_error: Some("RDAP failed".to_string()),
967            rdap_fallback: None,
968        };
969
970        let json = serde_json::to_string(&result).unwrap();
971        assert!(json.contains("\"source\":\"whois\""));
972        assert!(json.contains("RDAP failed"));
973    }
974
975    #[test]
976    fn test_lookup_result_available_serialization() {
977        let result = LookupResult::Available {
978            data: Box::new(
979                AvailabilityResult::new("test123.xyz", true, "medium", "dns_nxdomain")
980                    .with_details("Registry lookups failed; domain has no DNS presence (NXDOMAIN)"),
981            ),
982            rdap_error: "RDAP failed".to_string(),
983            whois_error: "WHOIS failed".to_string(),
984            whois_data: None,
985        };
986
987        let json = serde_json::to_string(&result).unwrap();
988        assert!(json.contains("\"source\":\"available\""));
989        assert!(json.contains("\"available\":true"));
990        assert!(json.contains("test123.xyz"));
991
992        assert_eq!(result.domain_name(), Some("test123.xyz".to_string()));
993        assert!(result.is_available());
994        assert!(!result.is_rdap());
995        assert!(!result.is_whois());
996        assert!(result.registrar().is_none());
997        assert_eq!(result.expiration_info(), (None, None));
998    }
999
1000    // ---------------- trim_raw_response char-boundary safety ----------------
1001
1002    #[test]
1003    fn truncate_on_char_boundary_does_not_panic_on_multibyte_straddle() {
1004        const MAX_RAW: usize = 32 * 1024;
1005        // Build a string longer than MAX_RAW with a 3-byte char straddling the
1006        // MAX_RAW byte offset: fill up to MAX_RAW-1 bytes of ASCII, then a
1007        // multi-byte char so byte MAX_RAW lands mid-character.
1008        let mut s = "a".repeat(MAX_RAW - 1);
1009        s.push('€'); // 3 bytes (E2 82 AC) — byte MAX_RAW is NOT a boundary
1010        s.push_str(&"b".repeat(100));
1011        assert!(!s.is_char_boundary(MAX_RAW));
1012
1013        // Must not panic.
1014        truncate_on_char_boundary(&mut s, MAX_RAW);
1015        assert!(s.len() <= MAX_RAW);
1016        // Result is valid UTF-8 (String invariant upheld) — backed up below
1017        // the straddling char.
1018        assert_eq!(s.len(), MAX_RAW - 1);
1019    }
1020
1021    #[test]
1022    fn trim_raw_response_truncates_multibyte_whois_without_panic() {
1023        const MAX_RAW: usize = 32 * 1024;
1024        let mut raw = "a".repeat(MAX_RAW - 1);
1025        raw.push('€');
1026        raw.push_str(&"b".repeat(1000));
1027
1028        let mut w = empty_whois("example.com");
1029        w.raw_response = raw;
1030        let result = trim_raw_response(LookupResult::Whois {
1031            data: w,
1032            rdap_error: None,
1033            rdap_fallback: None,
1034        });
1035        if let LookupResult::Whois { data, .. } = result {
1036            assert!(data.raw_response.ends_with("[truncated]"));
1037        } else {
1038            panic!("expected Whois variant");
1039        }
1040    }
1041
1042    // ---------------- sanitize_error_for_public ----------------
1043
1044    #[test]
1045    fn test_sanitize_strips_ipv4() {
1046        let msg = "RDAP URL resolves to reserved IP 10.0.0.1 which is forbidden";
1047        let sanitized = sanitize_error_for_public(msg);
1048        assert!(
1049            !sanitized.contains("10.0.0.1"),
1050            "IPv4 should be stripped, got: {}",
1051            sanitized
1052        );
1053        assert!(sanitized.contains("[ip-redacted]"));
1054    }
1055
1056    #[test]
1057    fn test_sanitize_strips_multiple_ipv4() {
1058        let msg = "Could not connect to 192.168.1.1 after trying 127.0.0.1";
1059        let sanitized = sanitize_error_for_public(msg);
1060        assert!(!sanitized.contains("192.168.1.1"));
1061        assert!(!sanitized.contains("127.0.0.1"));
1062        // Two redactions expected.
1063        assert_eq!(sanitized.matches("[ip-redacted]").count(), 2);
1064    }
1065
1066    #[test]
1067    fn test_sanitize_strips_ipv6() {
1068        let msg = "RDAP URL resolves to reserved IP fe80::1 which is forbidden";
1069        let sanitized = sanitize_error_for_public(msg);
1070        assert!(!sanitized.contains("fe80::1"));
1071        assert!(sanitized.contains("[ip-redacted]"));
1072    }
1073
1074    #[test]
1075    fn sanitize_leaves_mac_address_like_tokens_alone() {
1076        let msg = "error code af:ba:12 at line 5";
1077        let out = sanitize_error_for_public(msg);
1078        assert!(
1079            out.contains("af:ba:12"),
1080            "MAC fragment should not be stripped: {}",
1081            out
1082        );
1083    }
1084
1085    #[test]
1086    fn sanitize_strips_real_ipv6() {
1087        let msg = "cannot reach 2001:db8::1 — timeout";
1088        let out = sanitize_error_for_public(msg);
1089        assert!(!out.contains("2001:db8::1"));
1090        assert!(out.contains("[ip-redacted]"));
1091    }
1092
1093    #[test]
1094    fn sanitize_strips_fe80_link_local() {
1095        let msg = "peer at fe80::1 unreachable";
1096        let out = sanitize_error_for_public(msg);
1097        assert!(out.contains("[ip-redacted]"));
1098    }
1099
1100    #[test]
1101    fn test_sanitize_truncates_long_message() {
1102        // Build a 500-char message with no IPs.
1103        let long = "a".repeat(500);
1104        let sanitized = sanitize_error_for_public(&long);
1105        // Should cap at MAX_PUBLIC_ERROR_LEN chars + ellipsis.
1106        let char_count = sanitized.chars().count();
1107        assert_eq!(char_count, MAX_PUBLIC_ERROR_LEN + 1);
1108        assert!(sanitized.ends_with('…'));
1109    }
1110
1111    #[test]
1112    fn test_sanitize_preserves_short_messages() {
1113        let msg = "RDAP timed out after 15s";
1114        let sanitized = sanitize_error_for_public(msg);
1115        assert_eq!(sanitized, msg);
1116    }
1117
1118    // ---------------- RdapOutcome classification ----------------
1119
1120    #[test]
1121    fn test_is_rdap_response_useful_detects_no_data() {
1122        use crate::rdap::RdapResponse;
1123        // Construct a response with a name but no events, entities, NS, or status
1124        // — this is the "200 OK but no useful fields" case that should be
1125        // classified as RdapOutcome::NoData (not Useful, not Error).
1126        let resp = RdapResponse {
1127            ldh_name: Some("example.com".to_string()),
1128            ..Default::default()
1129        };
1130        assert!(
1131            !rdap_response_is_useful(&resp),
1132            "Response with only a name should be classified as NoData"
1133        );
1134
1135        // And one with a name + status IS useful (sanity check).
1136        let useful = RdapResponse {
1137            ldh_name: Some("example.com".to_string()),
1138            status: vec!["active".to_string()],
1139            ..Default::default()
1140        };
1141        assert!(rdap_response_is_useful(&useful));
1142    }
1143
1144    // ---------------- Coalescing ----------------
1145
1146    // Verifies that when multiple concurrent lookups hit the in-flight map
1147    // for the same domain, later arrivals observe the existing Weak<Notify>
1148    // and become waiters rather than racing a second lookup. We test the
1149    // map-level primitive here because the full SmartLookup pipeline
1150    // requires network access to exercise.
1151    // INFLIGHT_TEST_SERIAL is a std Mutex held across awaits on purpose: it
1152    // serializes whole async tests that share process-global state; an
1153    // async-aware mutex would defeat that.
1154    #[allow(clippy::await_holding_lock)]
1155    #[tokio::test]
1156    async fn test_inflight_coalescing_map() {
1157        // Serialize with sibling poisoning tests: we share LOOKUP_INFLIGHT
1158        // state, and `InflightGuard::drop` uses `try_lock` — if a sibling
1159        // holds the mutex during drop, cleanup is skipped and assertions
1160        // fail.
1161        let _serial = INFLIGHT_TEST_SERIAL
1162            .lock()
1163            .unwrap_or_else(|p| p.into_inner());
1164        // Poison-tolerant: the sibling poisoning regression tests may run
1165        // earlier under `cargo test` parallelism and leave LOOKUP_INFLIGHT
1166        // poisoned. The production code recovers via `unwrap_or_else`,
1167        // so this test does the same.
1168        //
1169        // Use a per-run unique key so this test cannot race with the other
1170        // tests that touch LOOKUP_INFLIGHT. Previously we `clear()`ed the
1171        // whole map, which raced with peer tests' entries.
1172        let domain = unique_test_key("__coalesce");
1173
1174        // Defensive: ensure our specific key is not present.
1175        {
1176            let mut m = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1177            m.remove(&domain);
1178        }
1179
1180        // First caller: no entry → becomes owner.
1181        let owner_notify = Arc::new(Notify::new());
1182        {
1183            let mut m = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1184            assert!(m.get(&domain).and_then(|w| w.upgrade()).is_none());
1185            m.insert(domain.clone(), Arc::downgrade(&owner_notify));
1186        }
1187
1188        // Second caller: sees the existing Weak and upgrades.
1189        let waiter = {
1190            let m = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1191            m.get(&domain)
1192                .and_then(|w| w.upgrade())
1193                .expect("Second caller must observe in-flight entry")
1194        };
1195
1196        // Waiter listens in the background.
1197        let waiter_clone = waiter.clone();
1198        let handle = tokio::spawn(async move {
1199            waiter_clone.notified().await;
1200        });
1201
1202        // Simulate owner completing.
1203        tokio::time::sleep(Duration::from_millis(20)).await;
1204        {
1205            let mut m = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1206            m.remove(&domain);
1207        }
1208        owner_notify.notify_waiters();
1209
1210        // Waiter should unblock quickly.
1211        tokio::time::timeout(Duration::from_secs(1), handle)
1212            .await
1213            .expect("waiter must unblock after notify")
1214            .expect("waiter task joined cleanly");
1215
1216        // After owner removes entry and drops its Arc, the Weak is dead.
1217        drop(owner_notify);
1218        drop(waiter);
1219        let m = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1220        assert!(m.get(&domain).and_then(|w| w.upgrade()).is_none());
1221    }
1222
1223    // Executes the real waiter branch of `lookup_with_progress` (not just the
1224    // map primitive above): two concurrent `lookup_with_progress` calls find
1225    // an in-flight entry for their domain, subscribe to its Notify, and — once
1226    // the owner populates the cache, removes the entry, and notifies (the
1227    // exact `InflightGuard::drop` sequence) — both return the cached result
1228    // WITHOUT running their own network race. `LOOKUP_CONCURRENT_CALLS` is the
1229    // coalescing proof: it is incremented at the top of `lookup_concurrent`
1230    // before any I/O, so if a waiter ever falls through to ownership the
1231    // counter moves and this test fails deterministically.
1232    //
1233    // The owner itself is simulated (cache insert + entry removal + notify)
1234    // rather than a third real lookup: a real owner's `lookup_concurrent`
1235    // needs RDAP bootstrap + WHOIS server discovery against live endpoints,
1236    // and those clients expose no in-scope hermetic seam for full-path
1237    // injection.
1238    // Deliberately holds INFLIGHT_TEST_SERIAL across awaits — see
1239    // test_inflight_coalescing_map.
1240    #[allow(clippy::await_holding_lock)]
1241    #[tokio::test]
1242    async fn waiters_coalesce_on_inflight_lookup_and_read_owners_cache() {
1243        use std::sync::atomic::Ordering;
1244
1245        let _serial = INFLIGHT_TEST_SERIAL
1246            .lock()
1247            .unwrap_or_else(|p| p.into_inner());
1248
1249        let raw = unique_test_key("coalesce-waiter");
1250        let normalized = crate::validation::normalize_domain(&raw).expect("test key normalizes");
1251        LOOKUP_CACHE.remove(&normalized);
1252
1253        // Simulated owner claims the in-flight slot before the waiters arrive.
1254        let owner_notify = Arc::new(Notify::new());
1255        {
1256            let mut m = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1257            m.insert(normalized.clone(), Arc::downgrade(&owner_notify));
1258        }
1259
1260        let calls_before = LOOKUP_CONCURRENT_CALLS.load(Ordering::SeqCst);
1261
1262        // Two real concurrent callers: both must take the Waiter branch.
1263        let lookup = SmartLookup::new();
1264        let spawn_waiter = |l: SmartLookup, d: String| {
1265            tokio::spawn(async move { l.lookup_with_progress(&d, None).await })
1266        };
1267        let h1 = spawn_waiter(lookup.clone(), raw.clone());
1268        let h2 = spawn_waiter(lookup, raw);
1269
1270        // Current-thread runtime: awaiting here runs both waiters up to their
1271        // `notified` await (their path to it is fully synchronous), so the
1272        // owner's completion below cannot slip in before they subscribe.
1273        tokio::time::sleep(Duration::from_millis(50)).await;
1274
1275        // Owner completes: populate the cache, then remove the entry and
1276        // notify — the same order `InflightGuard::drop` uses.
1277        let canned = LookupResult::Whois {
1278            data: empty_whois(&normalized),
1279            rdap_error: None,
1280            rdap_fallback: None,
1281        };
1282        LOOKUP_CACHE.insert(normalized.clone(), canned);
1283        {
1284            let mut m = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1285            m.remove(&normalized);
1286        }
1287        owner_notify.notify_waiters();
1288
1289        // Both waiters must wake promptly (well under DEFAULT_INFLIGHT_WAIT)
1290        // and observe the owner's result.
1291        for handle in [h1, h2] {
1292            let result = tokio::time::timeout(Duration::from_secs(5), handle)
1293                .await
1294                .expect("waiter must wake via notify, not the bounded-wait timeout")
1295                .expect("waiter task joined cleanly")
1296                .expect("waiter must read the owner's cached result");
1297            assert!(result.is_whois());
1298            assert_eq!(result.domain_name(), Some(normalized.clone()));
1299        }
1300
1301        // Coalescing proof: neither waiter ran its own `lookup_concurrent`.
1302        let calls_after = LOOKUP_CONCURRENT_CALLS.load(Ordering::SeqCst);
1303        assert_eq!(
1304            calls_before, calls_after,
1305            "coalesced waiters must not start a second network race"
1306        );
1307
1308        LOOKUP_CACHE.remove(&normalized);
1309    }
1310
1311    /// Builds a domain key guaranteed unique per test invocation, so that
1312    /// tests touching the shared LOOKUP_INFLIGHT static never collide when
1313    /// `cargo test` runs them in parallel. We include a nanosecond timestamp
1314    /// plus an atomic counter to defeat even hash-identical calls within the
1315    /// same nanosecond.
1316    fn unique_test_key(prefix: &str) -> String {
1317        use std::sync::atomic::{AtomicU64, Ordering};
1318        use std::time::{SystemTime, UNIX_EPOCH};
1319        static COUNTER: AtomicU64 = AtomicU64::new(0);
1320        let nanos = SystemTime::now()
1321            .duration_since(UNIX_EPOCH)
1322            .map(|d| d.as_nanos())
1323            .unwrap_or(0);
1324        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
1325        format!("{}_{}_{}.example.", prefix, nanos, n)
1326    }
1327
1328    // The public `rdap_error` / `whois_error` strings of every `Available`
1329    // result must be sanitized. These exercise the real construction paths
1330    // (not the sanitizer in isolation), so a construction site that stops
1331    // sanitizing fails here.
1332
1333    #[test]
1334    fn available_with_whois_sanitizes_rdap_error() {
1335        // The constructor every WHOIS-in-hand route of `lookup_concurrent`
1336        // returns through.
1337        let avail = AvailabilityResult::new("unreg.test", false, "none", "inconclusive");
1338        let result = available_with_whois(
1339            avail,
1340            "RDAP URL resolves to reserved IP 10.0.0.1",
1341            empty_whois("unreg.test"),
1342        );
1343        let LookupResult::Available {
1344            rdap_error,
1345            whois_error,
1346            whois_data,
1347            ..
1348        } = result
1349        else {
1350            panic!("expected Available variant");
1351        };
1352        assert!(!rdap_error.contains("10.0.0.1"), "{rdap_error}");
1353        assert!(rdap_error.contains("[ip-redacted]"));
1354        assert!(whois_error.is_empty());
1355        assert!(whois_data.is_some());
1356    }
1357
1358    #[tokio::test]
1359    async fn availability_fallback_sanitizes_both_error_fields() {
1360        // A prior RDAP 200 short-circuits the checker to `decide_from_rdap`,
1361        // so this runs the real fallback construction with no network.
1362        let lookup = SmartLookup::new();
1363        let prior = RdapResponse {
1364            ldh_name: Some("example.com".to_string()),
1365            status: vec!["active".to_string()],
1366            ..Default::default()
1367        };
1368        let result = lookup
1369            .availability_fallback(
1370                "example.com",
1371                PriorRdap::Response(Box::new(prior)),
1372                PriorWhois::Missing,
1373                "RDAP URL resolves to reserved IP 10.0.0.1".to_string(),
1374                "connection refused at 192.168.0.5 and fe80::1".to_string(),
1375                None,
1376            )
1377            .await
1378            .expect("prior-response fallback must not error");
1379        let LookupResult::Available {
1380            rdap_error,
1381            whois_error,
1382            ..
1383        } = result
1384        else {
1385            panic!("expected Available variant");
1386        };
1387        assert!(!rdap_error.contains("10.0.0.1"), "{rdap_error}");
1388        assert!(!whois_error.contains("192.168.0.5"), "{whois_error}");
1389        assert!(!whois_error.contains("fe80::1"), "{whois_error}");
1390        assert!(rdap_error.contains("[ip-redacted]"));
1391        assert!(whois_error.contains("[ip-redacted]"));
1392    }
1393
1394    // ---------------- degraded-result caching ----------------
1395
1396    fn available_via(method: &str, available: bool, confidence: &str) -> LookupResult {
1397        LookupResult::Available {
1398            data: Box::new(AvailabilityResult::new(
1399                "example.test",
1400                available,
1401                confidence,
1402                method,
1403            )),
1404            rdap_error: String::new(),
1405            whois_error: String::new(),
1406            whois_data: None,
1407        }
1408    }
1409
1410    #[test]
1411    fn degraded_results_are_identified_for_short_caching() {
1412        // Verdicts standing in for registry data we failed to get.
1413        assert!(is_degraded_result(&available_via(
1414            "inconclusive",
1415            false,
1416            "none"
1417        )));
1418        assert!(is_degraded_result(&available_via(
1419            "dns_present",
1420            false,
1421            "high"
1422        )));
1423        assert!(is_degraded_result(&available_via(
1424            "dns_nxdomain",
1425            true,
1426            "medium"
1427        )));
1428        // A WHOIS record without registration data (kept after an RDAP
1429        // failure or an empty RDAP 200) is a stand-in too.
1430        assert!(is_degraded_result(&LookupResult::Whois {
1431            data: empty_whois("example.test"),
1432            rdap_error: Some("RDAP response incomplete".to_string()),
1433            rdap_fallback: None,
1434        }));
1435        // Authoritative registry answers keep the full TTL.
1436        assert!(!is_degraded_result(&available_via("rdap", true, "high")));
1437        assert!(!is_degraded_result(&available_via("whois", true, "high")));
1438        assert!(!is_degraded_result(&LookupResult::Whois {
1439            data: denic_whois(),
1440            rdap_error: None,
1441            rdap_fallback: None,
1442        }));
1443        const { assert!(DEGRADED_LOOKUP_CACHE_TTL.as_secs() < LOOKUP_CACHE_TTL.as_secs()) };
1444    }
1445
1446    #[test]
1447    fn rdap_error_is_404_matches_standard_404() {
1448        let e = SeerError::RdapError("query failed with status 404 Not Found".to_string());
1449        assert!(rdap_error_is_404(&e));
1450    }
1451
1452    #[test]
1453    fn rdap_error_is_404_matches_without_reason_phrase() {
1454        let e = SeerError::RdapError("query failed with status 404".to_string());
1455        assert!(rdap_error_is_404(&e));
1456    }
1457
1458    #[test]
1459    fn rdap_error_is_404_rejects_other_statuses() {
1460        let e = SeerError::RdapError("query failed with status 500 Server Error".to_string());
1461        assert!(!rdap_error_is_404(&e));
1462        let e = SeerError::RdapError("query failed with status 400 Bad Request".to_string());
1463        assert!(!rdap_error_is_404(&e));
1464    }
1465
1466    #[test]
1467    fn rdap_error_is_404_rejects_non_http_errors() {
1468        let e = SeerError::RdapError("connection timeout".to_string());
1469        assert!(!rdap_error_is_404(&e));
1470        let e = SeerError::Timeout("rdap".to_string());
1471        assert!(!rdap_error_is_404(&e));
1472    }
1473
1474    #[test]
1475    fn rdap_error_is_404_rejects_incidental_404_in_message() {
1476        // A 404 substring inside a non-status context must not match.
1477        let e = SeerError::RdapError("error 40404: database corruption".to_string());
1478        assert!(!rdap_error_is_404(&e));
1479    }
1480
1481    // ---------------- WhoisResponse::is_thin ----------------
1482
1483    fn empty_whois(domain: &str) -> WhoisResponse {
1484        WhoisResponse {
1485            domain: domain.to_string(),
1486            ..Default::default()
1487        }
1488    }
1489
1490    #[test]
1491    fn whois_response_is_thin_when_all_key_fields_missing() {
1492        let w = empty_whois("example.com");
1493        assert!(w.is_thin());
1494    }
1495
1496    #[test]
1497    fn whois_response_is_not_thin_when_registrar_present() {
1498        let mut w = empty_whois("example.com");
1499        w.registrar = Some("Test Registrar".to_string());
1500        assert!(!w.is_thin());
1501    }
1502
1503    #[test]
1504    fn whois_response_is_not_thin_when_creation_date_present() {
1505        let mut w = empty_whois("example.com");
1506        w.creation_date = Some(Utc::now());
1507        assert!(!w.is_thin());
1508    }
1509
1510    #[test]
1511    fn whois_response_is_not_thin_when_expiration_date_present() {
1512        let mut w = empty_whois("example.com");
1513        w.expiration_date = Some(Utc::now());
1514        assert!(!w.is_thin());
1515    }
1516
1517    /// DENIC-shaped parsed WHOIS: nameservers, status and a changed date,
1518    /// but never a registrar or creation/expiry dates (and .de has no RDAP).
1519    fn denic_whois() -> WhoisResponse {
1520        let mut w = empty_whois("example.de");
1521        w.nameservers = vec!["ns1.example.net".to_string()];
1522        w.status = vec!["active".to_string()];
1523        w.updated_date = Some(Utc::now());
1524        w.raw_response = "Domain: example.de\nNserver: ns1.example.net\nStatus: connect\n".into();
1525        w
1526    }
1527
1528    #[test]
1529    fn whois_response_with_nameservers_is_not_thin() {
1530        let mut w = empty_whois("example.com");
1531        w.nameservers = vec!["ns1.example.net".to_string()];
1532        assert!(!w.is_thin());
1533    }
1534
1535    #[test]
1536    fn denic_whois_stays_on_the_whois_path() {
1537        // Regression: every registered .de domain used to become
1538        // `Available { method: "dns_present" }` ("WHOIS returned no data;
1539        // retry shortly") with its WHOIS data discarded by DomainInfo.
1540        let w = denic_whois();
1541        let bootstrap_miss =
1542            SeerError::RdapBootstrapError("no RDAP server for example.de".to_string());
1543        assert!(!w.is_thin());
1544        assert!(whois_leg_has_data(&Ok(w.clone())));
1545        assert!(matches!(
1546            whois_leg_fallback("example.de", false, Some(&bootstrap_miss), &w),
1547            Fallback::Registered
1548        ));
1549    }
1550
1551    // ---------------- whois_leg_fallback ----------------
1552    //
1553    // The ladder itself is pinned combination by combination in
1554    // `availability`'s tests; these cover what the lookup adds on top.
1555
1556    /// Reads a fallback as (available, method), `None` for Registered.
1557    fn verdict_of(fallback: Fallback, dns: DnsPresence) -> Option<(bool, String)> {
1558        let avail = match fallback {
1559            Fallback::Registered => return None,
1560            Fallback::Verdict(v) => v,
1561            Fallback::NeedsDns(t) => t.decide(dns),
1562        };
1563        Some((avail.available, avail.method))
1564    }
1565
1566    #[test]
1567    fn rdap_200_vetoes_whois_no_match_and_thin_bodies() {
1568        // Regression coverage for the v0.26.6 fix: an RDAP HTTP 200 (even a
1569        // thin one) proves the object exists; WHOIS propagation lag must not
1570        // flip it to "available", nor DNS be consulted.
1571        let mut no_match = empty_whois("freshly-registered.com");
1572        no_match.raw_response = "No match for \"FRESHLY-REGISTERED.COM\".".to_string();
1573        for w in [no_match, empty_whois("freshly-registered.com")] {
1574            assert!(matches!(
1575                whois_leg_fallback("freshly-registered.com", true, None, &w),
1576                Fallback::Registered
1577            ));
1578        }
1579    }
1580
1581    #[test]
1582    fn whois_no_match_routes_to_available_for_any_rdap_failure() {
1583        let mut w = empty_whois("genuinely-free.com");
1584        w.raw_response = "No match for \"GENUINELY-FREE.COM\".".to_string();
1585        let r404 = SeerError::RdapError("query failed with status 404".to_string());
1586        let bootstrap = SeerError::RdapBootstrapError("all registries failed".to_string());
1587        // 404, a non-404 failure, and a grace-truncated RDAP leg (None).
1588        for rdap in [Some(&r404), Some(&bootstrap), None] {
1589            assert_eq!(
1590                verdict_of(
1591                    whois_leg_fallback("genuinely-free.com", false, rdap, &w),
1592                    DnsPresence::Unknown
1593                ),
1594                Some((true, "whois".to_string()))
1595            );
1596        }
1597    }
1598
1599    #[test]
1600    fn thin_whois_with_rdap_404_is_available_via_rdap() {
1601        // Must read identically to `seer avail` for the same outcomes: the
1602        // registry's own RDAP 404 is authoritative ("high", "rdap").
1603        let r404 = SeerError::RdapError("query failed with status 404 Not Found".to_string());
1604        let fallback = whois_leg_fallback("example.xyz", false, Some(&r404), &empty_whois("x"));
1605        let Fallback::Verdict(v) = fallback else {
1606            panic!("expected a verdict");
1607        };
1608        assert_eq!(
1609            (v.available, v.confidence.as_str(), v.method.as_str()),
1610            (true, "high", "rdap")
1611        );
1612    }
1613
1614    #[test]
1615    fn thin_whois_with_silent_rdap_is_decided_by_dns() {
1616        // The zac.email / Identity-Digital case among them: thin/no-service
1617        // WHOIS with RDAP unavailable. A delegated apex reads as
1618        // likely-registered, NXDOMAIN as likely-available, and a failed probe
1619        // as inconclusive — never a bare WHOIS record cached for 5 minutes.
1620        let rdap = SeerError::RdapError("query failed with status 429".to_string());
1621        let w = empty_whois("zac.email");
1622        let read = |dns| verdict_of(whois_leg_fallback("zac.email", false, Some(&rdap), &w), dns);
1623        assert_eq!(
1624            read(DnsPresence::Present),
1625            Some((false, "dns_present".to_string()))
1626        );
1627        assert_eq!(
1628            read(DnsPresence::Absent),
1629            Some((true, "dns_nxdomain".to_string()))
1630        );
1631        assert_eq!(
1632            read(DnsPresence::Unknown),
1633            Some((false, "inconclusive".to_string()))
1634        );
1635    }
1636
1637    #[test]
1638    fn thin_whois_refusal_is_inconclusive_regardless_of_dns() {
1639        // issue #45: a registry refusal/throttle must never be guessed into
1640        // "available" (from NXDOMAIN) or "registered" (from delegation).
1641        let rdap = SeerError::RdapError("query failed with status 503".to_string());
1642        let mut w = empty_whois("example.test");
1643        w.raw_response = "Access rate limited; please try again later.\n".to_string();
1644        for dns in [
1645            DnsPresence::Absent,
1646            DnsPresence::Present,
1647            DnsPresence::Unknown,
1648        ] {
1649            assert_eq!(
1650                verdict_of(
1651                    whois_leg_fallback("example.test", false, Some(&rdap), &w),
1652                    dns
1653                ),
1654                Some((false, "inconclusive".to_string()))
1655            );
1656        }
1657    }
1658
1659    #[test]
1660    fn whois_with_registration_data_stays_a_whois_record() {
1661        let mut w = empty_whois("registered.com");
1662        w.registrar = Some("Example Registrar Ltd".to_string());
1663        let rdap = SeerError::RdapError("connection timeout".to_string());
1664        for rdap in [Some(&rdap), None] {
1665            assert!(matches!(
1666                whois_leg_fallback("registered.com", false, rdap, &w),
1667                Fallback::Registered
1668            ));
1669        }
1670    }
1671
1672    #[test]
1673    fn verdict_progress_names_the_deciding_signal() {
1674        let v =
1675            |available, method: &str| AvailabilityResult::new("x.test", available, "high", method);
1676        assert_eq!(
1677            verdict_progress(&v(true, "rdap")),
1678            "Domain appears unregistered"
1679        );
1680        assert!(verdict_progress(&v(false, "inconclusive")).contains("inconclusive"));
1681        assert!(verdict_progress(&v(false, "dns_present")).contains("registered"));
1682        assert!(verdict_progress(&v(false, "registrable_parent")).contains("parent"));
1683    }
1684
1685    // ---------------- race_with_grace ----------------
1686    //
1687    // Paused-clock tests of the RDAP/WHOIS race semantics: the losing leg is
1688    // grace-truncated ONLY when the winner brought usable data. A winner that
1689    // failed (or returned an unusable body) leaves the other leg as the sole
1690    // possible source of registry data, so it must run to its own completion.
1691    // This is the .ru case: the RDAP bootstrap miss fails in microseconds and
1692    // must not shave the WHOIS budget down to the grace period.
1693
1694    /// A leg that resolves to `value` after `secs` of (paused, auto-advanced)
1695    /// tokio time.
1696    async fn leg<T>(secs: u64, value: T) -> T {
1697        tokio::time::sleep(Duration::from_secs(secs)).await;
1698        value
1699    }
1700
1701    #[tokio::test(start_paused = true)]
1702    async fn race_awaits_whois_fully_when_rdap_errors_first() {
1703        let rdap = leg(0, Err::<&str, &str>("no RDAP server for example.ru"));
1704        // Well beyond the grace period, within the WHOIS client's own budget.
1705        let whois = leg(20, Ok::<&str, &str>("whois data"));
1706        let (r, w) = race_with_grace(rdap, whois, |r| r.is_ok(), |w| w.is_ok()).await;
1707        assert!(matches!(r, LegOutcome::Completed(Err(_))));
1708        assert!(
1709            matches!(w, LegOutcome::Completed(Ok("whois data"))),
1710            "WHOIS must not be grace-truncated behind an RDAP failure"
1711        );
1712    }
1713
1714    #[tokio::test(start_paused = true)]
1715    async fn race_awaits_rdap_fully_when_whois_errors_first() {
1716        let rdap = leg(20, Ok::<&str, &str>("rdap data"));
1717        let whois = leg(0, Err::<&str, &str>("connection refused"));
1718        let (r, w) = race_with_grace(rdap, whois, |r| r.is_ok(), |w| w.is_ok()).await;
1719        assert!(
1720            matches!(r, LegOutcome::Completed(Ok("rdap data"))),
1721            "RDAP must not be grace-truncated behind a WHOIS failure"
1722        );
1723        assert!(matches!(w, LegOutcome::Completed(Err(_))));
1724    }
1725
1726    #[tokio::test(start_paused = true)]
1727    async fn race_truncates_whois_when_rdap_answers_with_data() {
1728        let rdap = leg(0, Ok::<&str, &str>("useful rdap"));
1729        let whois = leg(20, Ok::<&str, &str>("whois data"));
1730        let (r, w) = race_with_grace(rdap, whois, |r| r.is_ok(), |w| w.is_ok()).await;
1731        assert!(matches!(r, LegOutcome::Completed(Ok(_))));
1732        assert!(
1733            matches!(w, LegOutcome::GraceTruncated),
1734            "a data-bearing RDAP winner only owes WHOIS the grace period"
1735        );
1736    }
1737
1738    #[tokio::test(start_paused = true)]
1739    async fn race_truncates_rdap_when_whois_answers_with_data() {
1740        let rdap = leg(20, Ok::<&str, &str>("rdap data"));
1741        let whois = leg(0, Ok::<&str, &str>("whois data"));
1742        let (r, w) = race_with_grace(rdap, whois, |r| r.is_ok(), |w| w.is_ok()).await;
1743        assert!(matches!(r, LegOutcome::GraceTruncated));
1744        assert!(matches!(w, LegOutcome::Completed(Ok(_))));
1745    }
1746
1747    #[tokio::test(start_paused = true)]
1748    async fn race_loser_inside_grace_period_still_completes() {
1749        let rdap = leg(0, Ok::<&str, &str>("useful rdap"));
1750        // Within the 5s grace period.
1751        let whois = leg(2, Ok::<&str, &str>("whois data"));
1752        let (r, w) = race_with_grace(rdap, whois, |r| r.is_ok(), |w| w.is_ok()).await;
1753        assert!(matches!(r, LegOutcome::Completed(Ok(_))));
1754        assert!(matches!(w, LegOutcome::Completed(Ok("whois data"))));
1755    }
1756
1757    #[tokio::test(start_paused = true)]
1758    async fn race_awaits_whois_fully_when_rdap_wins_with_unusable_data() {
1759        // An Ok the predicate rejects (e.g. a thin RDAP 200 with no useful
1760        // fields) is not "data in hand" — WHOIS still runs to completion.
1761        let rdap = leg(0, Ok::<&str, &str>("thin"));
1762        let whois = leg(20, Ok::<&str, &str>("whois data"));
1763        let (r, w) = race_with_grace(
1764            rdap,
1765            whois,
1766            |r| matches!(r, Ok(s) if *s == "useful"),
1767            |w| w.is_ok(),
1768        )
1769        .await;
1770        assert!(matches!(r, LegOutcome::Completed(Ok("thin"))));
1771        assert!(matches!(w, LegOutcome::Completed(Ok("whois data"))));
1772    }
1773
1774    #[tokio::test(start_paused = true)]
1775    async fn race_awaits_rdap_fully_when_whois_wins_with_thin_body() {
1776        // The Identity-Digital case from the WHOIS side: the registry's
1777        // port-43 endpoint answers Ok instantly but the body carries no
1778        // registration data (a "no service"/"not supported" sentinel). Under
1779        // the old bare `is_ok()` predicate that thin win grace-truncated the
1780        // only viable protocol — RDAP — guaranteeing a thin result. With the
1781        // production `whois_leg_has_data` predicate, RDAP runs to its own
1782        // bounded completion, mirroring the RDAP-side usefulness gate.
1783        let rdap = leg(20, Ok::<&str, &str>("rdap data"));
1784        let whois = leg(0, Ok::<_, SeerError>(empty_whois("zac.email")));
1785        let (r, w) = race_with_grace(rdap, whois, |r| r.is_ok(), whois_leg_has_data).await;
1786        assert!(
1787            matches!(r, LegOutcome::Completed(Ok("rdap data"))),
1788            "RDAP must not be grace-truncated behind a thin WHOIS answer"
1789        );
1790        assert!(matches!(w, LegOutcome::Completed(Ok(_))));
1791    }
1792
1793    #[tokio::test(start_paused = true)]
1794    async fn race_truncates_rdap_when_whois_wins_with_real_data() {
1795        // Complement: a WHOIS win that DOES carry registration data still
1796        // only owes RDAP the grace period.
1797        let mut whois_data = empty_whois("example.com");
1798        whois_data.registrar = Some("Mock Registrar".to_string());
1799        let rdap = leg(20, Ok::<&str, &str>("rdap data"));
1800        let whois = leg(0, Ok::<_, SeerError>(whois_data));
1801        let (r, w) = race_with_grace(rdap, whois, |r| r.is_ok(), whois_leg_has_data).await;
1802        assert!(
1803            matches!(r, LegOutcome::GraceTruncated),
1804            "a data-bearing WHOIS winner only owes RDAP the grace period"
1805        );
1806        assert!(matches!(w, LegOutcome::Completed(Ok(_))));
1807    }
1808
1809    // ---------------- whois_leg_has_data ----------------
1810
1811    #[test]
1812    fn whois_leg_has_data_rejects_thin_ok_and_errors() {
1813        // Thin Ok bodies — including "No match" availability sentinels — are
1814        // not data in hand: the race must keep RDAP alive so a late RDAP 200
1815        // can still veto a stale WHOIS "no match" (v0.26.6 rule).
1816        assert!(!whois_leg_has_data(&Ok(empty_whois("example.email"))));
1817        let mut no_match = empty_whois("example.com");
1818        no_match.raw_response = "No match for \"EXAMPLE.COM\".".to_string();
1819        assert!(!whois_leg_has_data(&Ok(no_match)));
1820        assert!(!whois_leg_has_data(&Err(SeerError::WhoisError(
1821            "connection refused".to_string()
1822        ))));
1823    }
1824
1825    #[test]
1826    fn whois_leg_has_data_accepts_registration_data() {
1827        // Stays in lockstep with `WhoisResponse::is_thin`: any of the three
1828        // key registration signals makes the leg a data-bearing winner.
1829        let mut w = empty_whois("example.com");
1830        w.registrar = Some("Mock Registrar".to_string());
1831        assert!(whois_leg_has_data(&Ok(w)));
1832        let mut w = empty_whois("example.com");
1833        w.expiration_date = Some(Utc::now());
1834        assert!(whois_leg_has_data(&Ok(w)));
1835    }
1836
1837    // ---------------- grace_truncated_error ----------------
1838
1839    #[test]
1840    fn grace_truncated_error_says_answered_not_won() {
1841        // Truncation only happens behind a data-bearing winner, so the message
1842        // must say the winner *answered* — the old "after RDAP won" wording
1843        // misled when the winner had merely finished first with an error.
1844        let msg = grace_truncated_error("WHOIS", "RDAP");
1845        assert_eq!(
1846            msg,
1847            format!(
1848                "WHOIS did not respond within the {}s grace period after RDAP answered",
1849                PROTOCOL_GRACE_PERIOD.as_secs()
1850            )
1851        );
1852        assert!(!msg.contains("won"));
1853    }
1854
1855    #[test]
1856    fn grace_truncated_error_is_symmetric() {
1857        let msg = grace_truncated_error("RDAP", "WHOIS");
1858        assert!(msg.starts_with("RDAP did not respond"));
1859        assert!(msg.ends_with("after WHOIS answered"));
1860    }
1861
1862    // ---------------- Mutex poisoning recovery ----------------
1863
1864    /// Regression: a panic inside `LOOKUP_INFLIGHT.lock()` must not wedge
1865    /// the tracker forever. After the mutex is poisoned, subsequent
1866    /// acquisition attempts must still succeed via `unwrap_or_else`.
1867    ///
1868    /// This isolates the lookup_with_progress acquisition site (formerly a
1869    /// `.expect("LOOKUP_INFLIGHT mutex poisoned")`) by exercising the same
1870    /// `.lock().unwrap_or_else(|p| p.into_inner())` pattern directly.
1871    #[test]
1872    fn lookup_inflight_recovers_from_poisoned_mutex() {
1873        use std::panic::{catch_unwind, AssertUnwindSafe};
1874
1875        // Serialize with sibling tests that also touch LOOKUP_INFLIGHT.
1876        let _serial = INFLIGHT_TEST_SERIAL
1877            .lock()
1878            .unwrap_or_else(|p| p.into_inner());
1879
1880        // Poison the real static by panicking while holding the guard.
1881        let _ = catch_unwind(AssertUnwindSafe(|| {
1882            let _guard = LOOKUP_INFLIGHT.lock().unwrap();
1883            panic!("poisoning LOOKUP_INFLIGHT for test");
1884        }));
1885
1886        // At this point LOOKUP_INFLIGHT is poisoned. Plain .lock() would
1887        // return Err(PoisonError). The recovery pattern used in
1888        // lookup_with_progress must still yield a usable guard.
1889        let mut guard = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1890        // Use a per-run unique canary so parallel tests cannot collide.
1891        let canary = unique_test_key("__poison_recovery");
1892        guard.insert(canary.clone(), Weak::new());
1893        assert!(guard.contains_key(&canary));
1894        guard.remove(&canary);
1895    }
1896
1897    /// Regression: InflightGuard::drop must also tolerate mutex poisoning
1898    /// without panicking — the Poisoned arm should still remove the entry.
1899    #[test]
1900    fn inflight_guard_drop_recovers_from_poisoned_mutex() {
1901        use std::panic::{catch_unwind, AssertUnwindSafe};
1902
1903        // Serialize with sibling tests that also touch LOOKUP_INFLIGHT —
1904        // the critical race was `InflightGuard::drop` using `try_lock`
1905        // and silently skipping cleanup when a parallel test held the
1906        // mutex, leaving this test's entry in the map and failing the
1907        // final assertion.
1908        let _serial = INFLIGHT_TEST_SERIAL
1909            .lock()
1910            .unwrap_or_else(|p| p.into_inner());
1911
1912        // Seed an entry and arm a guard for it. Use a per-run unique key
1913        // so this test can never collide with siblings under parallel
1914        // `cargo test` — previously a hard-coded key raced with the peer
1915        // coalescing test's `m.clear()` call.
1916        let key = unique_test_key("__drop_poison");
1917        let notify = Arc::new(Notify::new());
1918        {
1919            let mut map = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1920            map.insert(key.clone(), Arc::downgrade(&notify));
1921        }
1922        let guard = InflightGuard {
1923            key: key.clone(),
1924            notify: notify.clone(),
1925        };
1926
1927        // Poison the mutex.
1928        let _ = catch_unwind(AssertUnwindSafe(|| {
1929            let _g = LOOKUP_INFLIGHT.lock().unwrap();
1930            panic!("poisoning LOOKUP_INFLIGHT for drop test");
1931        }));
1932
1933        // Dropping the guard must not panic and must remove the entry via
1934        // the Poisoned branch of the new try_lock match.
1935        drop(guard);
1936
1937        let map = LOOKUP_INFLIGHT.lock().unwrap_or_else(|p| p.into_inner());
1938        assert!(
1939            !map.contains_key(&key),
1940            "poisoned-mutex drop path should still remove the in-flight entry"
1941        );
1942    }
1943}