domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
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
//! HTTP response handling.

use anyhow::{Error, Result};
use log::debug;

use crate::fetch::dns::fetch_all_dns_data;
use crate::fetch::record::prepare_record_for_insertion;
use crate::fetch::response::{extract_response_data, parse_html_content, HtmlData, ResponseData};
use crate::fetch::ProcessingContext;
use crate::fetch::UrlProcessOutcome;
use crate::fingerprint::DetectedTechnology;
use crate::storage::insert::insert_persisted_url_record;
use crate::utils::{duration_to_us, UrlTimingMetrics};
use std::sync::Arc;
use std::time::Instant;

/// Serializes a response header map into newline-separated `Name: value` lines
/// for secret scanning. Repeated headers (e.g. multiple `Set-Cookie`) each get
/// their own line; non-UTF-8 header values are skipped.
fn serialize_headers(headers: &reqwest::header::HeaderMap) -> String {
    let mut out = String::new();
    for (name, value) in headers {
        if let Ok(v) = value.to_str() {
            out.push_str(name.as_str());
            out.push_str(": ");
            out.push_str(v);
            out.push('\n');
        }
    }
    out
}

struct ExtractedResponse {
    resp_data: ResponseData,
    html_data: HtmlData,
    html_parsing_us: u64,
}

/// Extract response bytes/headers and parse HTML on a blocking thread.
async fn extract_and_parse(
    response: reqwest::Response,
    original_url: &str,
    final_url_str: &str,
    ctx: &ProcessingContext,
) -> Result<Option<ExtractedResponse>, Error> {
    let html_parse_start = Instant::now();
    let Some(resp_data) = extract_response_data(response, original_url, final_url_str).await?
    else {
        return Ok(None);
    };

    // Parse HTML content on a blocking thread to avoid starving Tokio worker threads.
    // body is `Arc<str>`; `Arc::clone` is cheap (pointer + atomic refcount bump).
    let body = Arc::clone(&resp_data.body);
    let final_domain = resp_data.final_domain.clone();
    let error_stats = ctx.runtime.error_stats.clone();
    let html_data =
        tokio::task::spawn_blocking(move || parse_html_content(&body, &final_domain, &error_stats))
            .await
            .map_err(|e| anyhow::anyhow!("HTML parsing task failed: {e}"))?;

    Ok(Some(ExtractedResponse {
        resp_data,
        html_data,
        html_parsing_us: duration_to_us(html_parse_start.elapsed()),
    }))
}

struct EnrichmentResult {
    technologies_vec: Vec<DetectedTechnology>,
    tech_detection_us: u64,
    tls_dns_data: crate::fetch::dns::TlsDnsData,
    additional_dns: crate::fetch::dns::AdditionalDnsData,
    partial_failures: Vec<(crate::error_handling::ErrorType, String)>,
    dns_forward_us: u64,
    dns_reverse_us: u64,
    dns_additional_us: u64,
    tls_handshake_us: u64,
    favicon_result: Option<crate::fetch::favicon::FaviconData>,
    external_script_scan: crate::fetch::external_scripts::ExternalScriptScanResult,
    well_known: crate::fetch::well_known::WellKnownData,
}

/// Run independent enrichments in parallel: tech detection, DNS/TLS, favicon, scripts.
async fn parallel_enrich(
    html_data: &HtmlData,
    resp_data: &ResponseData,
    ctx: &ProcessingContext,
    final_url_str: &str,
) -> Result<EnrichmentResult, Error> {
    let skip_tech = crate::fingerprint::is_challenge_interstitial(
        resp_data.status,
        &html_data.title,
        resp_data.body.as_ref(),
    );
    let (tech_result, dns_result, favicon_result, external_script_scan, well_known) = tokio::join!(
        async {
            use crate::fetch::record::detect_technologies_safely;

            let tech_start = Instant::now();
            let technologies = if skip_tech {
                log::debug!(
                    "Skipping technology detection for challenge interstitial {}",
                    resp_data.final_domain
                );
                Vec::new()
            } else {
                detect_technologies_safely(
                    html_data,
                    resp_data,
                    &ctx.runtime.error_stats,
                    &ctx.ruleset,
                )
                .await
            };
            let tech_detection_us = duration_to_us(tech_start.elapsed());
            (technologies, tech_detection_us)
        },
        async {
            fetch_all_dns_data(resp_data, &ctx.network.resolver, &ctx.runtime.error_stats).await
        },
        crate::fetch::favicon::fetch_and_hash_favicon(
            &ctx.network.client,
            html_data.favicon_url.as_deref(),
            final_url_str,
        ),
        async {
            if ctx.runtime.scan_external_scripts {
                crate::fetch::external_scripts::scan_external_scripts(
                    &ctx.network.client,
                    &resp_data.final_url,
                    &html_data.script_sources,
                    ctx.runtime.allow_localhost_for_tests,
                )
                .await
            } else {
                crate::fetch::external_scripts::ExternalScriptScanResult::default()
            }
        },
        crate::fetch::well_known::fetch_well_known(&ctx.network.client, final_url_str)
    );

    let (technologies_vec, tech_detection_us) = tech_result;
    let (
        tls_dns_data,
        additional_dns,
        partial_failures,
        (dns_forward_us, dns_reverse_us, dns_additional_us, tls_handshake_us),
    ) = dns_result?;

    let technologies_vec = if skip_tech {
        Vec::new()
    } else {
        supplement_technologies_after_enrichment(
            ctx,
            technologies_vec,
            &tls_dns_data,
            &additional_dns,
            &external_script_scan,
            &resp_data.final_domain,
        )
        .await
    };

    Ok(EnrichmentResult {
        technologies_vec,
        tech_detection_us,
        tls_dns_data,
        additional_dns,
        partial_failures,
        dns_forward_us,
        dns_reverse_us,
        dns_additional_us,
        tls_handshake_us,
        favicon_result,
        external_script_scan,
        well_known,
    })
}

/// Merge DNS/cert and fetched-script-body signals into the first tech pass.
///
/// Official **late-signal** pattern for technology detection:
/// `parallel_enrich` runs the first tech pass in parallel with DNS/TLS and
/// (optionally) external script fetch. Signals those stages produce are not
/// available during pass 1, so fold them here afterward (DNS/cert first, then
/// script bodies). New late signals should extend this function rather than
/// inventing a second pipeline. ADR 0005 still forbids JS *execution*; matching
/// static text from already-fetched bodies is in scope.
async fn supplement_technologies_after_enrichment(
    ctx: &ProcessingContext,
    technologies_vec: Vec<DetectedTechnology>,
    tls_dns_data: &crate::fetch::dns::TlsDnsData,
    additional_dns: &crate::fetch::dns::AdditionalDnsData,
    external_script_scan: &crate::fetch::external_scripts::ExternalScriptScanResult,
    final_domain: &str,
) -> Vec<DetectedTechnology> {
    // Build the DNS haystack on the async task (cheap string assembly), then run
    // DNS/cert + optional script-text matching on the blocking pool with the
    // main fingerprint pass.
    let dns_haystack = crate::fingerprint::dns_records_haystack(
        additional_dns.nameservers.as_deref(),
        additional_dns.cname_chain.as_deref(),
    );
    let ruleset = Arc::clone(&ctx.ruleset);
    let cert_issuer = tls_dns_data.issuer.clone();
    let script_text = external_script_scan.script_bodies_text.clone();
    let fallback = technologies_vec.clone();

    match tokio::task::spawn_blocking(move || {
        let techs = crate::fingerprint::supplement_technologies_with_dns_cert(
            ruleset.as_ref(),
            technologies_vec,
            &dns_haystack,
            cert_issuer.as_deref(),
        );
        if script_text.is_empty() {
            return techs;
        }
        crate::fingerprint::supplement_technologies_with_script_text(
            ruleset.as_ref(),
            techs,
            &script_text,
        )
    })
    .await
    {
        Ok(techs) => techs,
        Err(e) => {
            log::warn!("Technology late-signal supplement join failed for {final_domain}: {e}");
            fallback
        }
    }
}

/// Merge external-script and response-header secret findings into `html_data`.
///
/// Header secret regex work runs on `spawn_blocking` so it does not occupy a
/// Tokio async worker (same posture as HTML / external-script secret scans).
async fn merge_secrets(
    html_data: &mut HtmlData,
    resp_data: &ResponseData,
    external_script_scan: crate::fetch::external_scripts::ExternalScriptScanResult,
) {
    html_data.external_scripts_eligible = external_script_scan.eligible;
    html_data.external_scripts_scanned = external_script_scan.scanned;
    if !external_script_scan.secrets.is_empty() {
        log::info!(
            "Detected {} additional secret(s) in external scripts for {}",
            external_script_scan.secrets.len(),
            resp_data.final_domain
        );
        html_data
            .exposed_secrets
            .extend(external_script_scan.secrets);
    }

    let headers_blob = serialize_headers(&resp_data.headers);
    let final_domain = resp_data.final_domain.clone();
    let header_secrets = tokio::task::spawn_blocking(move || {
        crate::parse::detect_exposed_secrets_in_headers(&headers_blob)
    })
    .await
    .unwrap_or_else(|e| {
        log::warn!("Header secret scan join failed for {final_domain}: {e}");
        Vec::new()
    });
    if !header_secrets.is_empty() {
        log::info!(
            "Detected {} secret(s) in response headers for {}",
            header_secrets.len(),
            resp_data.final_domain
        );
        html_data.exposed_secrets.extend(header_secrets);
    }
}

/// Prepare and insert the scan record; returns enrichment timing micros.
async fn persist(
    resp_data: ResponseData,
    html_data: HtmlData,
    enrichment: EnrichmentResult,
    redirect_chain_vec: Vec<(String, u16)>,
    elapsed: f64,
    timestamp: i64,
    ctx: &ProcessingContext,
) -> Result<(u64, u64, bool), Error> {
    debug!(
        "Preparing to insert record for URL: {}",
        resp_data.final_url
    );
    log::debug!(
        "Attempting to insert record into database for domain: {}",
        resp_data.initial_domain
    );

    let final_url_for_logging = resp_data.final_url.clone();
    let EnrichmentResult {
        technologies_vec,
        tls_dns_data,
        additional_dns,
        partial_failures,
        favicon_result,
        well_known,
        ..
    } = enrichment;

    let (persisted_record, (geoip_lookup_us, whois_lookup_us)) =
        prepare_record_for_insertion(crate::fetch::record::RecordPreparationParams {
            resp_data,
            html_data,
            tls_dns_data,
            additional_dns,
            technologies_vec,
            partial_failures,
            redirect_chain: redirect_chain_vec,
            elapsed,
            timestamp,
            ctx,
            favicon: favicon_result,
            well_known,
        })
        .await;

    let upsert = insert_persisted_url_record(&ctx.pool, persisted_record)
        .await
        .map_err(|e| {
            log::error!("Failed to insert record for URL {final_url_for_logging}: {e}");
            anyhow::anyhow!("Database write failed: {e}")
        })?;
    ctx.runtime.runtime_metrics.record_partial_failures(
        upsert.partial_failures_inserted,
        upsert.satellite_insert_errors_inserted,
    );

    Ok((geoip_lookup_us, whois_lookup_us, upsert.inserted))
}

/// Handles an HTTP response, extracting all relevant data and storing it in the database.
///
/// Stages: `extract_and_parse` → `parallel_enrich` → `merge_secrets` → `persist`.
///
/// # Errors
///
/// Returns an error if domain extraction, DNS resolution, or database insertion fails.
pub async fn handle_response(
    response: reqwest::Response,
    original_url: &str,
    final_url_str: &str,
    ctx: &ProcessingContext,
    elapsed: f64,
    redirect_chain: Option<Vec<(String, u16)>>,
    start_time: std::time::Instant,
) -> Result<UrlProcessOutcome, Error> {
    debug!("Started processing response for {final_url_str}");

    let mut metrics = UrlTimingMetrics {
        #[allow(
            clippy::cast_possible_truncation,
            clippy::cast_precision_loss,
            clippy::cast_sign_loss
        )]
        http_request_us: (elapsed * 1_000_000.0).min(u64::MAX as f64).max(0.0) as u64,
        ..Default::default()
    };

    let Some(extracted) = extract_and_parse(response, original_url, final_url_str, ctx).await?
    else {
        debug!("Skipping URL {final_url_str} (non-scannable content-type)");
        return Ok(UrlProcessOutcome::Skipped);
    };
    metrics.html_parsing_us = extracted.html_parsing_us;

    let ExtractedResponse {
        resp_data,
        mut html_data,
        ..
    } = extracted;

    let timestamp = chrono::Utc::now().timestamp_millis();
    let redirect_chain_vec = redirect_chain.unwrap_or_default();

    let mut enrichment = parallel_enrich(&html_data, &resp_data, ctx, final_url_str).await?;
    metrics.dns_forward_us = enrichment.dns_forward_us;
    metrics.dns_reverse_us = enrichment.dns_reverse_us;
    metrics.dns_additional_us = enrichment.dns_additional_us;
    metrics.tls_handshake_us = enrichment.tls_handshake_us;
    metrics.tech_detection_us = enrichment.tech_detection_us;

    let external_script_scan = std::mem::take(&mut enrichment.external_script_scan);
    merge_secrets(&mut html_data, &resp_data, external_script_scan).await;

    let (geoip_lookup_us, whois_lookup_us, inserted) = persist(
        resp_data,
        html_data,
        enrichment,
        redirect_chain_vec,
        elapsed,
        timestamp,
        ctx,
    )
    .await?;

    metrics.geoip_lookup_us = geoip_lookup_us;
    metrics.whois_lookup_us = whois_lookup_us;
    metrics.total_us = duration_to_us(start_time.elapsed());
    ctx.runtime.timing_stats.record(&metrics);

    Ok(if inserted {
        UrlProcessOutcome::Inserted
    } else {
        UrlProcessOutcome::Updated
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error_handling::ProcessingStats;
    use crate::fetch::{NetworkContext, ProcessingContext, RuntimeContext};
    use crate::initialization::test_resolver;
    use crate::utils::TimingStats;
    use httptest::{matchers::*, responders::*, Expectation, Server};
    use std::sync::Arc;

    async fn create_test_context(server: &Server) -> ProcessingContext {
        // IP hosts are valid domain keys; successful HTML paths may fetch /favicon.ico
        // and same-origin well-known probes (security.txt / robots.txt).
        server.expect(
            Expectation::matching(request::method_path("GET", "/favicon.ico"))
                .times(0..)
                .respond_with(status_code(404)),
        );
        server.expect(
            Expectation::matching(request::method_path("GET", "/.well-known/security.txt"))
                .times(0..)
                .respond_with(status_code(404)),
        );
        server.expect(
            Expectation::matching(request::method_path("GET", "/security.txt"))
                .times(0..)
                .respond_with(status_code(404)),
        );
        server.expect(
            Expectation::matching(request::method_path("GET", "/robots.txt"))
                .times(0..)
                .respond_with(status_code(404)),
        );

        let client = Arc::new(
            reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(5))
                .build()
                .expect("Failed to create HTTP client"),
        );
        let redirect_client = Arc::new(
            reqwest::Client::builder()
                .redirect(reqwest::redirect::Policy::none())
                .timeout(std::time::Duration::from_secs(5))
                .build()
                .expect("Failed to create redirect client"),
        );
        let resolver = test_resolver();
        let error_stats = Arc::new(ProcessingStats::new());
        let timing_stats = Arc::new(TimingStats::new());
        let pool = Arc::new(
            sqlx::SqlitePool::connect("sqlite::memory:")
                .await
                .expect("Failed to create test pool"),
        );

        ProcessingContext::new(
            NetworkContext::new(client, redirect_client, resolver),
            pool,
            RuntimeContext::new(
                error_stats,
                timing_stats,
                None,
                false,
                std::path::PathBuf::from("/tmp/domain_status_whois_test"),
                false,
                Arc::new(crate::runtime_metrics::RuntimeMetrics::default()),
                true,
            ),
            Arc::new(crate::fingerprint::FingerprintRuleset::empty_for_tests()),
        )
    }

    #[tokio::test]
    async fn test_handle_response_non_html_skips_silently() {
        let server = Server::run();
        let server_url = server.url("/json").to_string();
        let original_url = "https://example.com/json";

        // Return JSON instead of HTML
        server.expect(
            Expectation::matching(request::method_path("GET", "/json")).respond_with(
                status_code(200)
                    .insert_header("Content-Type", "application/json")
                    .body(r#"{"key": "value"}"#),
            ),
        );

        let client = reqwest::Client::new();
        let response = client.get(&server_url).send().await.unwrap();
        let ctx = create_test_context(&server).await;
        let start_time = std::time::Instant::now();

        // application/json is scannable (secret detection); without migrations insert fails.
        let result = handle_response(
            response,
            original_url,
            &server_url,
            &ctx,
            0.1,
            None,
            start_time,
        )
        .await;

        assert!(
            result.is_err(),
            "expected DB insert failure without migrations"
        );
        let error_msg = result.unwrap_err().to_string();
        assert!(
            error_msg.contains("Database")
                || error_msg.contains("migration")
                || error_msg.contains("no such table")
                || error_msg.contains("url_status"),
            "Expected database/schema error, got: {error_msg}"
        );
    }

    #[tokio::test]
    async fn test_handle_response_empty_body_skips_silently() {
        let server = Server::run();
        let server_url = server.url("/empty").to_string();
        let original_url = "https://example.com/empty";

        // Return empty body
        server.expect(
            Expectation::matching(request::method_path("GET", "/empty")).respond_with(
                status_code(200)
                    .insert_header("Content-Type", "text/html; charset=utf-8")
                    .body(""),
            ),
        );

        let client = reqwest::Client::new();
        let response = client.get(&server_url).send().await.unwrap();
        let ctx = create_test_context(&server).await;
        let start_time = std::time::Instant::now();

        // Empty bodies still record metadata; without migrations insert fails.
        let result = handle_response(
            response,
            original_url,
            &server_url,
            &ctx,
            0.1,
            None,
            start_time,
        )
        .await;

        assert!(
            result.is_err(),
            "expected DB insert failure without migrations"
        );
        let error_msg = result.unwrap_err().to_string();
        assert!(
            error_msg.contains("Database")
                || error_msg.contains("migration")
                || error_msg.contains("no such table")
                || error_msg.contains("url_status"),
            "Expected database/schema error, got: {error_msg}"
        );
    }

    #[tokio::test]
    async fn test_handle_response_dns_failure_propagates_error() {
        let server = Server::run();
        let server_url = server.url("/test").to_string();
        let original_url = "https://this-domain-definitely-does-not-exist-12345.invalid/test";

        // Return valid HTML
        server.expect(
            Expectation::matching(request::method_path("GET", "/test")).respond_with(
                status_code(200)
                    .insert_header("Content-Type", "text/html; charset=utf-8")
                    .body("<html><head><title>Test</title></head><body>Hello</body></html>"),
            ),
        );

        let client = reqwest::Client::new();
        let response = client.get(&server_url).send().await.unwrap();
        let ctx = create_test_context(&server).await;
        let start_time = std::time::Instant::now();

        // Final URL is an IP mock host (valid domain key). Insert fails without migrations;
        // DNS on the mock IP may also fail depending on resolver.
        let result = handle_response(
            response,
            original_url,
            &server_url,
            &ctx,
            0.1,
            None,
            start_time,
        )
        .await;

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(
            error_msg.contains("DNS")
                || error_msg.contains("Database")
                || error_msg.contains("no such table")
                || error_msg.contains("url_status"),
            "Expected DNS/database error, got: {error_msg}"
        );
    }

    #[tokio::test]
    async fn test_handle_response_timing_metrics_overflow_protection() {
        // Test that very large elapsed times don't cause overflow
        // This is critical - elapsed * 1_000_000.0 could overflow u64
        // The code at line 53 uses .min(u64::MAX as f64) to prevent overflow
        let very_large_elapsed = 1_000_000.0; // 1 million seconds

        // Verify the calculation doesn't panic
        let http_request_ms: f64 = very_large_elapsed * 1_000_000.0;
        // Safe cast: value is clamped to [0.0, u64::MAX] range before conversion
        #[allow(
            clippy::cast_possible_truncation,
            clippy::cast_precision_loss,
            clippy::cast_sign_loss
        )]
        let http_request_ms = http_request_ms.min(u64::MAX as f64).max(0.0) as u64;
        // With protection, should be clamped to u64::MAX (not overflow)
        // Note: 1e6 * 1e6 = 1e12, which is less than u64::MAX (~1.8e19)
        // So it should be the actual value, not clamped
        // The important thing is the calculation doesn't overflow
        assert!(http_request_ms > 0);
        // Verify it's approximately the expected value (1e12 microseconds = 1e6 seconds)
        // Allow for floating point precision differences (cast to u64 truncates)
        // 1e12 as u64 = 1_000_000_000_000 (1 trillion)
        assert!(http_request_ms >= 999_999_000_000);
        assert!(http_request_ms <= 1_000_001_000_000);
    }

    #[tokio::test]
    async fn test_handle_response_negative_elapsed_clamped() {
        // Test that negative elapsed times are clamped to 0
        // This is critical - clock adjustments could cause negative elapsed
        // The code at line 53 uses .max(0.0) to prevent negative values
        let negative_elapsed = -1.0;

        // Verify the calculation clamps negative values
        let http_request_ms: f64 = negative_elapsed * 1_000_000.0;
        // Safe cast: negative values are clamped to 0, ensuring non-negative result
        #[allow(
            clippy::cast_possible_truncation,
            clippy::cast_precision_loss,
            clippy::cast_sign_loss
        )]
        let http_request_ms = http_request_ms.min(u64::MAX as f64).max(0.0) as u64;
        // Should be clamped to 0, not negative
        assert_eq!(http_request_ms, 0);
    }

    #[tokio::test]
    async fn test_handle_response_database_insertion_failure_propagates() {
        // Test that database insertion failures are correctly propagated
        // This is critical - database errors should not be silently ignored
        let server = Server::run();
        let server_url = server.url("/test").to_string();
        let original_url = "https://example.com/test";

        // Return valid HTML
        server.expect(
            Expectation::matching(request::method_path("GET", "/test")).respond_with(
                status_code(200)
                    .insert_header("Content-Type", "text/html; charset=utf-8")
                    .body("<html><head><title>Test</title></head><body>Hello</body></html>"),
            ),
        );

        let client = reqwest::Client::new();
        let response = client.get(&server_url).send().await.unwrap();
        let ctx = create_test_context(&server).await;
        let start_time = std::time::Instant::now();

        // Database insertion will fail (no migrations), error should propagate
        let result = handle_response(
            response,
            original_url,
            &server_url,
            &ctx,
            0.1,
            None,
            start_time,
        )
        .await;

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(
            error_msg.contains("Database write failed")
                || error_msg.contains("Database")
                || error_msg.contains("migration")
                || error_msg.contains("table")
                || error_msg.contains("url_status"),
            "Expected database error, got: {error_msg}"
        );
    }

    #[tokio::test]
    async fn test_handle_response_database_insertion_failure_propagates_error() {
        // Test that database insertion failures are correctly propagated
        // This is critical - database errors should not be silently ignored
        let server = Server::run();
        let server_url = server.url("/test").to_string();
        let original_url = "https://example.com/test";

        // Return valid HTML
        server.expect(
            Expectation::matching(request::method_path("GET", "/test")).respond_with(
                status_code(200)
                    .insert_header("Content-Type", "text/html; charset=utf-8")
                    .body("<html><head><title>Test</title></head><body>Hello</body></html>"),
            ),
        );

        // IP hosts are valid domain keys; successful HTML paths may fetch /favicon.ico
        // and same-origin well-known probes (security.txt / robots.txt).
        server.expect(
            Expectation::matching(request::method_path("GET", "/favicon.ico"))
                .times(0..)
                .respond_with(status_code(404)),
        );
        server.expect(
            Expectation::matching(request::method_path("GET", "/.well-known/security.txt"))
                .times(0..)
                .respond_with(status_code(404)),
        );
        server.expect(
            Expectation::matching(request::method_path("GET", "/security.txt"))
                .times(0..)
                .respond_with(status_code(404)),
        );
        server.expect(
            Expectation::matching(request::method_path("GET", "/robots.txt"))
                .times(0..)
                .respond_with(status_code(404)),
        );

        let client = reqwest::Client::new();
        let response = client.get(&server_url).send().await.unwrap();

        // Create context with a closed/invalid pool to trigger database insertion failure
        let pool = Arc::new(
            sqlx::SqlitePool::connect("sqlite::memory:")
                .await
                .expect("Failed to create test pool"),
        );
        // Close the pool to make insertion fail
        pool.close().await;

        let client_arc = Arc::new(
            reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(5))
                .build()
                .expect("Failed to create HTTP client"),
        );
        let redirect_client = Arc::new(
            reqwest::Client::builder()
                .redirect(reqwest::redirect::Policy::none())
                .timeout(std::time::Duration::from_secs(5))
                .build()
                .expect("Failed to create redirect client"),
        );
        let resolver = test_resolver();
        let error_stats = Arc::new(ProcessingStats::new());
        let timing_stats = Arc::new(TimingStats::new());

        let ctx = ProcessingContext::new(
            NetworkContext::new(client_arc, redirect_client, resolver),
            pool,
            RuntimeContext::new(
                error_stats,
                timing_stats,
                Some("test-run".to_string()),
                false,
                std::path::PathBuf::from("/tmp/domain_status_whois_test"),
                false,
                Arc::new(crate::runtime_metrics::RuntimeMetrics::default()),
                true,
            ),
            Arc::new(crate::fingerprint::FingerprintRuleset::empty_for_tests()),
        );

        let start_time = std::time::Instant::now();

        // Should fail at database insertion
        let result = handle_response(
            response,
            original_url,
            &server_url,
            &ctx,
            0.1,
            None,
            start_time,
        )
        .await;

        assert!(result.is_err(), "Should fail when database insertion fails");
        let error_msg = result.unwrap_err().to_string();
        assert!(
            error_msg.contains("Database write failed")
                || error_msg.contains("pool")
                || error_msg.contains("closed")
                || error_msg.contains("Connection")
                || error_msg.contains("Database"),
            "Expected database insertion error, got: {error_msg}"
        );
    }
}