millipede-core 0.1.1

Core primitives for the Millipede web crawler: request model, storage traits, events, errors, configuration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
//! Integration tests for streaming sitemap request lists.

use std::{
    collections::HashMap,
    io::Write,
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, AtomicUsize, Ordering},
    },
};

use bytes::Bytes;
use flate2::{Compression, write::GzEncoder};
use futures_util::{StreamExt, stream};
use http::{HeaderMap, StatusCode};
use millipede_core::{
    http_client::{HttpClient, HttpClientError, HttpRequest, HttpResponse, StreamingResponse},
    request::UserData,
    sitemap::{SitemapRequestList, SitemapRequestListBuilder},
    storage::{KeyList, KeyValueStore, KvEntry, ListKeysOptions, StorageError, StorageResult},
};
use millipede_storage_memory::MemoryKeyValueStore;
use tokio::sync::Notify;
use url::Url;

#[derive(Clone)]
struct ResponseFixture {
    status: StatusCode,
    chunks: Vec<Bytes>,
    fetched: Arc<AtomicUsize>,
    served: Arc<AtomicUsize>,
    body_error: bool,
}

#[derive(Default)]
struct FakeHttpClient {
    fixtures: Mutex<HashMap<String, ResponseFixture>>,
}

struct FaultyKvs {
    inner: Arc<MemoryKeyValueStore>,
    fail_first_get: bool,
    fail_first_set: bool,
    get_calls: AtomicUsize,
    set_calls: AtomicUsize,
}

struct BlockingHttpClient {
    inner: Arc<FakeHttpClient>,
    block_first: AtomicBool,
    started: Notify,
}

struct BlockingBodyHttpClient {
    inner: Arc<FakeHttpClient>,
    block_first: AtomicBool,
    started: Arc<Notify>,
}

struct BlockingSetKvs {
    inner: Arc<MemoryKeyValueStore>,
    block_first: AtomicBool,
    started: Notify,
}

impl FaultyKvs {
    fn new(inner: Arc<MemoryKeyValueStore>, fail_first_get: bool, fail_first_set: bool) -> Self {
        Self {
            inner,
            fail_first_get,
            fail_first_set,
            get_calls: AtomicUsize::new(0),
            set_calls: AtomicUsize::new(0),
        }
    }
}

#[async_trait::async_trait]
impl KeyValueStore for FaultyKvs {
    async fn get_bytes(&self, key: &str) -> StorageResult<Option<KvEntry>> {
        if self.fail_first_get && self.get_calls.fetch_add(1, Ordering::SeqCst) == 0 {
            return Err(StorageError::Backend(anyhow::anyhow!(
                "transient get failure"
            )));
        }
        self.inner.get_bytes(key).await
    }

    async fn set_bytes(&self, key: &str, bytes: Bytes, content_type: &str) -> StorageResult<()> {
        if self.fail_first_set && self.set_calls.fetch_add(1, Ordering::SeqCst) == 0 {
            return Err(StorageError::Backend(anyhow::anyhow!(
                "transient set failure"
            )));
        }
        self.inner.set_bytes(key, bytes, content_type).await
    }

    async fn delete(&self, key: &str) -> StorageResult<()> {
        self.inner.delete(key).await
    }

    async fn list_keys(&self, opts: ListKeysOptions) -> StorageResult<KeyList> {
        self.inner.list_keys(opts).await
    }
}

impl FakeHttpClient {
    fn add(&self, url: &str, body: impl AsRef<[u8]>, chunk_size: usize) -> ResponseFixture {
        self.add_status(url, StatusCode::OK, body, chunk_size)
    }

    fn add_status(
        &self,
        url: &str,
        status: StatusCode,
        body: impl AsRef<[u8]>,
        chunk_size: usize,
    ) -> ResponseFixture {
        let fixture = ResponseFixture {
            status,
            chunks: body
                .as_ref()
                .chunks(chunk_size)
                .map(Bytes::copy_from_slice)
                .collect(),
            fetched: Arc::new(AtomicUsize::new(0)),
            served: Arc::new(AtomicUsize::new(0)),
            body_error: false,
        };
        self.fixtures
            .lock()
            .expect("fixture lock")
            .insert(url.to_owned(), fixture.clone());
        fixture
    }

    fn add_body_error(
        &self,
        url: &str,
        body: impl AsRef<[u8]>,
        chunk_size: usize,
    ) -> ResponseFixture {
        let mut fixture = self.add(url, body, chunk_size);
        fixture.body_error = true;
        self.fixtures
            .lock()
            .expect("fixture lock")
            .insert(url.to_owned(), fixture.clone());
        fixture
    }
}

#[async_trait::async_trait]
impl HttpClient for BlockingHttpClient {
    async fn send(&self, _request: HttpRequest) -> Result<HttpResponse, HttpClientError> {
        panic!("sitemap request lists must use streaming HTTP")
    }

    async fn stream(&self, request: HttpRequest) -> Result<StreamingResponse, HttpClientError> {
        if self.block_first.swap(false, Ordering::SeqCst) {
            self.started.notify_one();
            std::future::pending::<()>().await;
        }
        self.inner.stream(request).await
    }
}

#[async_trait::async_trait]
impl HttpClient for BlockingBodyHttpClient {
    async fn send(&self, _request: HttpRequest) -> Result<HttpResponse, HttpClientError> {
        panic!("sitemap request lists must use streaming HTTP")
    }

    async fn stream(&self, request: HttpRequest) -> Result<StreamingResponse, HttpClientError> {
        if self.block_first.swap(false, Ordering::SeqCst) {
            let started = self.started.clone();
            let body = stream::once(async move {
                started.notify_one();
                std::future::pending::<Result<Bytes, HttpClientError>>().await
            })
            .boxed();
            return Ok(StreamingResponse::new(
                request.url,
                StatusCode::OK,
                HeaderMap::new(),
                body,
            ));
        }
        self.inner.stream(request).await
    }
}

#[async_trait::async_trait]
impl KeyValueStore for BlockingSetKvs {
    async fn get_bytes(&self, key: &str) -> StorageResult<Option<KvEntry>> {
        self.inner.get_bytes(key).await
    }

    async fn set_bytes(&self, key: &str, bytes: Bytes, content_type: &str) -> StorageResult<()> {
        if self.block_first.swap(false, Ordering::SeqCst) {
            self.started.notify_one();
            std::future::pending::<()>().await;
        }
        self.inner.set_bytes(key, bytes, content_type).await
    }

    async fn delete(&self, key: &str) -> StorageResult<()> {
        self.inner.delete(key).await
    }

    async fn list_keys(&self, opts: ListKeysOptions) -> StorageResult<KeyList> {
        self.inner.list_keys(opts).await
    }
}

#[async_trait::async_trait]
impl HttpClient for FakeHttpClient {
    async fn send(&self, _request: HttpRequest) -> Result<HttpResponse, HttpClientError> {
        panic!("sitemap request lists must use streaming HTTP")
    }

    async fn stream(&self, request: HttpRequest) -> Result<StreamingResponse, HttpClientError> {
        let fixture = self
            .fixtures
            .lock()
            .expect("fixture lock")
            .get(request.url.as_str())
            .cloned()
            .ok_or_else(|| HttpClientError::connect(anyhow::anyhow!("missing fixture")))?;
        fixture.fetched.fetch_add(1, Ordering::SeqCst);
        let served = fixture.served.clone();
        let body_error = fixture.body_error;
        let body = stream::iter(fixture.chunks.into_iter().map(move |chunk| {
            served.fetch_add(1, Ordering::SeqCst);
            Ok(chunk)
        }))
        .chain(stream::iter(body_error.then(|| {
            Err(HttpClientError::io(anyhow::anyhow!("fixture body failure")))
        })))
        .boxed();
        Ok(StreamingResponse::new(
            request.url,
            fixture.status,
            HeaderMap::new(),
            body,
        ))
    }
}

fn urlset(start: usize, count: usize) -> String {
    let entries = (start..start + count)
        .map(|index| {
            format!(
                "<url><loc>https://example.com/{index}?a=1&amp;b=2</loc><lastmod>2026-01-01</lastmod><priority>0.5</priority><changefreq>daily</changefreq></url>"
            )
        })
        .collect::<String>();
    format!("<?xml version=\"1.0\"?><urlset xmlns=\"x\">{entries}</urlset>")
}

fn list(client: Arc<FakeHttpClient>, roots: &[&str]) -> SitemapRequestList {
    SitemapRequestListBuilder::default()
        .sitemap_urls(roots.iter().map(|url| Url::parse(url).expect("valid URL")))
        .http_client(client)
        .build()
        .expect("valid sitemap list")
}

async fn drain(list: &SitemapRequestList) -> Vec<String> {
    let mut urls = Vec::new();
    while let Some(request) = list.fetch_next().await.expect("fetch next") {
        urls.push(request.url.to_string());
    }
    urls
}

#[tokio::test]
async fn plain_urlset_applies_request_metadata() {
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml", urlset(0, 5), 11);
    let mut user_data = UserData::default();
    user_data.set_typed("source", &"sitemap").expect("JSON");
    let list = SitemapRequestListBuilder::default()
        .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
        .http_client(client)
        .label("product")
        .user_data(user_data.clone())
        .build()
        .expect("list");

    for index in 0..5 {
        let request = list.fetch_next().await.expect("fetch").expect("request");
        assert_eq!(request.label.as_deref(), Some("product"));
        assert_eq!(request.user_data, user_data);
        assert_eq!(request.crawl_depth, 0);
        assert!(request.url.as_str().contains(&format!("/{index}?")));
    }
    assert!(list.fetch_next().await.expect("fetch").is_none());
    assert!(list.is_finished().await);
    assert_eq!(list.processed_count().await, 5);
}

#[tokio::test]
async fn text_and_cdata_fragments_are_concatenated() {
    let client = Arc::new(FakeHttpClient::default());
    let body = "<urlset><url><loc>https://example.com/<![CDATA[path]]></loc><priority>0.<![CDATA[5]]></priority></url></urlset>";
    client.add("https://example.com/sitemap.xml", body, 4);

    assert_eq!(
        drain(&list(client, &["https://example.com/sitemap.xml"])).await,
        vec!["https://example.com/path"]
    );
}

#[tokio::test]
async fn gzip_is_detected_by_suffix_and_magic() {
    let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
    encoder
        .write_all(urlset(0, 2).as_bytes())
        .expect("compress");
    let compressed = encoder.finish().expect("finish gzip");
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml.gz", &compressed, 7);
    client.add("https://example.com/magic.xml", &compressed, 1);

    assert_eq!(
        drain(&list(
            client.clone(),
            &["https://example.com/sitemap.xml.gz"]
        ))
        .await
        .len(),
        2
    );
    assert_eq!(
        drain(&list(client, &["https://example.com/magic.xml"]))
            .await
            .len(),
        2
    );
}

#[tokio::test]
async fn sitemap_index_fetches_children_lazily() {
    let client = Arc::new(FakeHttpClient::default());
    let index = "<sitemapindex><sitemap><loc>https://example.com/one.xml</loc></sitemap><sitemap><loc>https://example.com/two.xml</loc></sitemap></sitemapindex>";
    client.add("https://example.com/index.xml", index, 9);
    client.add("https://example.com/one.xml", urlset(0, 2), 8);
    let second = client.add("https://example.com/two.xml", urlset(2, 2), 8);
    let list = list(client, &["https://example.com/index.xml"]);

    assert!(list.fetch_next().await.expect("fetch").is_some());
    assert!(list.fetch_next().await.expect("fetch").is_some());
    assert_eq!(second.fetched.load(Ordering::SeqCst), 0);
    assert!(list.fetch_next().await.expect("fetch").is_some());
    assert_eq!(second.fetched.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn failed_index_releases_staged_children_for_later_roots() {
    let client = Arc::new(FakeHttpClient::default());
    let child = "https://example.com/child.xml";
    let index = format!(
        "<sitemapindex><sitemap><loc>{child}</loc></sitemap></sitemapindex>{}",
        " ".repeat(1_024)
    );
    client.add_body_error("https://example.com/bad.xml", index, 1);
    client.add(
        "https://example.com/good.xml",
        format!("<sitemapindex><sitemap><loc>{child}</loc></sitemap></sitemapindex>"),
        7,
    );
    let child_fixture = client.add(child, urlset(0, 1), 7);

    assert_eq!(
        drain(&list(
            client,
            &[
                "https://example.com/bad.xml",
                "https://example.com/good.xml",
            ],
        ))
        .await,
        vec!["https://example.com/0?a=1&b=2"]
    );
    assert_eq!(child_fixture.fetched.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn configured_root_referenced_by_an_index_keeps_depth_zero() {
    let client = Arc::new(FakeHttpClient::default());
    client.add(
        "https://example.com/a.xml",
        "<sitemapindex><sitemap><loc>https://example.com/b.xml</loc></sitemap></sitemapindex>",
        7,
    );
    for depth in 0..5 {
        let current = if depth == 0 {
            "https://example.com/b.xml".to_owned()
        } else {
            format!("https://example.com/l{depth}.xml")
        };
        let next = if depth == 4 {
            "https://example.com/content.xml".to_owned()
        } else {
            format!("https://example.com/l{}.xml", depth + 1)
        };
        client.add(
            &current,
            format!("<sitemapindex><sitemap><loc>{next}</loc></sitemap></sitemapindex>"),
            7,
        );
    }
    client.add("https://example.com/content.xml", urlset(0, 1), 7);

    assert_eq!(
        drain(&list(
            client,
            &["https://example.com/a.xml", "https://example.com/b.xml"],
        ))
        .await,
        vec!["https://example.com/0?a=1&b=2"]
    );
}

#[tokio::test]
async fn malformed_entries_are_skipped_without_losing_following_entries() {
    let body = "<urlset><url><lastmod>today</lastmod></url><url><loc>https://example.com/broken</oops></url><url><loc>https://example.com/good</loc></url></urlset>";
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml", body, 5);
    assert_eq!(
        drain(&list(client, &["https://example.com/sitemap.xml"])).await,
        vec!["https://example.com/good"]
    );
}

#[tokio::test]
async fn malformed_sitemap_index_entries_are_skipped() {
    let body = "<sitemapindex><sitemap><loc>https://example.com/broken.xml</oops></sitemap><sitemap><loc>https://example.com/good.xml</loc></sitemap></sitemapindex>";
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/index.xml", body, 5);
    client.add("https://example.com/good.xml", urlset(0, 1), 5);

    assert_eq!(
        drain(&list(client, &["https://example.com/index.xml"])).await,
        vec!["https://example.com/0?a=1&b=2"]
    );
}

#[tokio::test]
async fn limit_stops_at_exactly_three_requests() {
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml", urlset(0, 10), 13);
    let list = SitemapRequestListBuilder::default()
        .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
        .http_client(client)
        .limit(3)
        .build()
        .expect("list");
    assert_eq!(drain(&list).await.len(), 3);
    assert!(list.is_finished().await);
}

#[tokio::test]
async fn persisted_progress_resumes_without_refetching_completed_sitemap() {
    let client = Arc::new(FakeHttpClient::default());
    let first = client.add("https://example.com/one.xml", urlset(0, 5), 7);
    client.add("https://example.com/two.xml", urlset(5, 5), 7);
    let kvs: Arc<dyn KeyValueStore> = Arc::new(MemoryKeyValueStore::new("sitemap"));
    let build = || {
        SitemapRequestListBuilder::default()
            .sitemap_urls([
                Url::parse("https://example.com/one.xml").expect("URL"),
                Url::parse("https://example.com/two.xml").expect("URL"),
            ])
            .http_client(client.clone())
            .persist(kvs.clone(), "progress")
            .build()
            .expect("list")
    };
    let original = build();
    let mut initial = Vec::new();
    for _ in 0..7 {
        initial.push(
            original
                .fetch_next()
                .await
                .expect("fetch")
                .expect("request")
                .url
                .to_string(),
        );
    }
    original.persist().await.expect("persist");
    let resumed = build();
    let remaining = drain(&resumed).await;
    assert_eq!(remaining.len(), 3);
    assert!(initial.iter().all(|url| !remaining.contains(url)));
    assert_eq!(first.fetched.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn all_root_failures_return_an_error() {
    let client = Arc::new(FakeHttpClient::default());
    client.add_status(
        "https://example.com/one.xml",
        StatusCode::INTERNAL_SERVER_ERROR,
        "",
        1,
    );
    client.add_status("https://example.com/two.xml", StatusCode::NOT_FOUND, "", 1);
    let list = list(
        client,
        &["https://example.com/one.xml", "https://example.com/two.xml"],
    );
    let error = list.fetch_next().await.expect_err("all roots should fail");
    assert!(error.is_retryable());
    assert!(error.counts_against_retries());
}

#[tokio::test]
async fn duplicated_failing_root_still_returns_an_error() {
    let client = Arc::new(FakeHttpClient::default());
    client.add_status(
        "https://example.com/failing.xml",
        StatusCode::INTERNAL_SERVER_ERROR,
        "",
        1,
    );
    let list = list(
        client,
        &[
            "https://example.com/failing.xml",
            "https://example.com/failing.xml",
        ],
    );

    assert!(list.fetch_next().await.is_err());
}

#[tokio::test]
async fn cancellation_while_opening_does_not_lose_the_pending_sitemap() {
    let inner = Arc::new(FakeHttpClient::default());
    inner.add("https://example.com/sitemap.xml", urlset(0, 1), 8);
    let client = Arc::new(BlockingHttpClient {
        inner,
        block_first: AtomicBool::new(true),
        started: Notify::new(),
    });
    let list = Arc::new(
        SitemapRequestListBuilder::default()
            .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
            .http_client(client.clone())
            .build()
            .expect("list"),
    );
    let task_list = list.clone();
    let task = tokio::spawn(async move { task_list.fetch_next().await });
    client.started.notified().await;
    task.abort();
    assert!(
        task.await
            .expect_err("fetch should be cancelled")
            .is_cancelled()
    );

    assert!(list.fetch_next().await.expect("retry fetch").is_some());
}

#[tokio::test]
async fn cancellation_during_resumed_body_peek_preserves_the_skip_count() {
    let inner = Arc::new(FakeHttpClient::default());
    inner.add("https://example.com/sitemap.xml", urlset(0, 3), 8);
    let kvs: Arc<dyn KeyValueStore> = Arc::new(MemoryKeyValueStore::new("cancel-body-peek"));
    let original = SitemapRequestListBuilder::default()
        .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
        .http_client(inner.clone())
        .persist(kvs.clone(), "progress")
        .build()
        .expect("list");
    assert_eq!(
        original
            .fetch_next()
            .await
            .expect("fetch")
            .expect("request")
            .url
            .as_str(),
        "https://example.com/0?a=1&b=2"
    );
    original.persist().await.expect("persist");

    let started = Arc::new(Notify::new());
    let client = Arc::new(BlockingBodyHttpClient {
        inner,
        block_first: AtomicBool::new(true),
        started: started.clone(),
    });
    let resumed = Arc::new(
        SitemapRequestListBuilder::default()
            .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
            .http_client(client)
            .persist(kvs, "progress")
            .build()
            .expect("list"),
    );
    let task_list = resumed.clone();
    let task = tokio::spawn(async move { task_list.fetch_next().await });
    started.notified().await;
    task.abort();
    assert!(
        task.await
            .expect_err("fetch should be cancelled")
            .is_cancelled()
    );

    assert_eq!(
        resumed
            .fetch_next()
            .await
            .expect("retry fetch")
            .expect("request")
            .url
            .as_str(),
        "https://example.com/1?a=1&b=2"
    );
    assert_eq!(drain(&resumed).await, vec!["https://example.com/2?a=1&b=2"]);
}

#[tokio::test]
async fn first_request_arrives_before_the_document_is_fully_served() {
    let client = Arc::new(FakeHttpClient::default());
    let fixture = client.add("https://example.com/large.xml", urlset(0, 1_000), 16);
    let total_chunks = fixture.chunks.len();
    let list = list(client, &["https://example.com/large.xml"]);

    assert!(list.fetch_next().await.expect("fetch").is_some());
    assert!(fixture.served.load(Ordering::SeqCst) < total_chunks);
}

#[tokio::test]
async fn extension_locations_do_not_replace_direct_child_locations() {
    let client = Arc::new(FakeHttpClient::default());
    let extension_urlset = "<urlset xmlns:image=\"image\"><url><loc>https://example.com/page</loc><image:image><image:loc>https://example.com/image.jpg</image:loc></image:image></url></urlset>";
    client.add("https://example.com/urls.xml", extension_urlset, 9);
    assert_eq!(
        drain(&list(client.clone(), &["https://example.com/urls.xml"])).await,
        vec!["https://example.com/page"]
    );

    let index = "<sitemapindex xmlns:ext=\"extension\"><sitemap><loc>https://example.com/child.xml</loc><ext:data><ext:loc>https://example.com/wrong.xml</ext:loc></ext:data></sitemap></sitemapindex>";
    client.add("https://example.com/index.xml", index, 7);
    client.add("https://example.com/child.xml", urlset(0, 1), 7);
    assert_eq!(
        drain(&list(client, &["https://example.com/index.xml"])).await,
        vec!["https://example.com/0?a=1&b=2"]
    );
}

#[tokio::test]
async fn extension_entry_end_tags_do_not_close_sitemap_entries() {
    let client = Arc::new(FakeHttpClient::default());
    let urlset_body = "<urlset xmlns:ext=\"extension\"><url><ext:url></ext:url><loc>https://example.com/good</loc></url></urlset>";
    client.add("https://example.com/urls.xml", urlset_body, 7);
    assert_eq!(
        drain(&list(client.clone(), &["https://example.com/urls.xml"])).await,
        vec!["https://example.com/good"]
    );

    let index = "<sitemapindex xmlns:ext=\"extension\"><sitemap><ext:sitemap></ext:sitemap><loc>https://example.com/child.xml</loc></sitemap></sitemapindex>";
    client.add("https://example.com/index.xml", index, 7);
    client.add("https://example.com/child.xml", urlset(0, 1), 7);
    assert_eq!(
        drain(&list(client, &["https://example.com/index.xml"])).await,
        vec!["https://example.com/0?a=1&b=2"]
    );
}

#[tokio::test]
async fn failed_resumed_fetch_does_not_skip_entries_in_the_next_sitemap() {
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/resume.xml", urlset(0, 2), 8);
    client.add("https://example.com/next.xml", urlset(10, 3), 8);
    let kvs: Arc<dyn KeyValueStore> = Arc::new(MemoryKeyValueStore::new("resume-failure"));
    let build = || {
        SitemapRequestListBuilder::default()
            .sitemap_urls([
                Url::parse("https://example.com/resume.xml").expect("URL"),
                Url::parse("https://example.com/next.xml").expect("URL"),
            ])
            .http_client(client.clone())
            .persist(kvs.clone(), "progress")
            .build()
            .expect("list")
    };
    let original = build();
    assert!(original.fetch_next().await.expect("fetch").is_some());
    original.persist().await.expect("persist");
    client.add_status(
        "https://example.com/resume.xml",
        StatusCode::INTERNAL_SERVER_ERROR,
        "",
        1,
    );

    assert_eq!(drain(&build()).await.len(), 3);
}

#[tokio::test]
async fn transient_load_failure_is_retried() {
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml", urlset(0, 3), 8);
    let inner = Arc::new(MemoryKeyValueStore::new("load-retry"));
    let original = SitemapRequestListBuilder::default()
        .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
        .http_client(client.clone())
        .persist(inner.clone(), "progress")
        .build()
        .expect("list");
    assert!(original.fetch_next().await.expect("fetch").is_some());
    original.persist().await.expect("persist");

    let faulty: Arc<dyn KeyValueStore> = Arc::new(FaultyKvs::new(inner, true, false));
    let resumed = SitemapRequestListBuilder::default()
        .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
        .http_client(client)
        .persist(faulty, "progress")
        .build()
        .expect("list");
    assert!(resumed.fetch_next().await.is_err());
    assert_eq!(
        resumed
            .fetch_next()
            .await
            .expect("retried load")
            .expect("request")
            .url
            .as_str(),
        "https://example.com/1?a=1&b=2"
    );
}

#[tokio::test]
async fn automatic_checkpoint_failure_does_not_drop_the_request() {
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml", urlset(0, 101), 32);
    let faulty: Arc<dyn KeyValueStore> = Arc::new(FaultyKvs::new(
        Arc::new(MemoryKeyValueStore::new("set-failure")),
        false,
        true,
    ));
    let list = SitemapRequestListBuilder::default()
        .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
        .http_client(client)
        .persist(faulty, "progress")
        .build()
        .expect("list");
    for index in 0..101 {
        let request = list
            .fetch_next()
            .await
            .expect("checkpoint failure is best effort")
            .expect("request");
        assert!(request.url.as_str().contains(&format!("/{index}?")));
    }
    assert_eq!(list.processed_count().await, 101);
}

#[tokio::test]
async fn cancellation_during_checkpoint_preserves_the_pending_request() {
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml", urlset(0, 101), 32);
    let kvs = Arc::new(BlockingSetKvs {
        inner: Arc::new(MemoryKeyValueStore::new("cancel-checkpoint")),
        block_first: AtomicBool::new(true),
        started: Notify::new(),
    });
    let list = Arc::new(
        SitemapRequestListBuilder::default()
            .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
            .http_client(client)
            .persist(kvs.clone(), "progress")
            .build()
            .expect("list"),
    );
    for _ in 0..99 {
        assert!(list.fetch_next().await.expect("fetch").is_some());
    }
    let task_list = list.clone();
    let task = tokio::spawn(async move { task_list.fetch_next().await });
    kvs.started.notified().await;
    task.abort();
    assert!(
        task.await
            .expect_err("fetch should be cancelled")
            .is_cancelled()
    );
    assert_eq!(list.processed_count().await, 99);

    let request = list
        .fetch_next()
        .await
        .expect("retry checkpoint")
        .expect("preserved request");
    assert!(request.url.as_str().contains("/99?"));
    assert_eq!(list.processed_count().await, 100);
}

#[tokio::test]
async fn duplicate_locations_stay_deduplicated_across_resume() {
    let client = Arc::new(FakeHttpClient::default());
    let body = "<urlset><url><loc>https://example.com/a</loc></url><url><loc>https://example.com/a</loc></url><url><loc>https://example.com/b</loc></url></urlset>";
    client.add("https://example.com/sitemap.xml", body, 7);
    let kvs: Arc<dyn KeyValueStore> = Arc::new(MemoryKeyValueStore::new("dedup-resume"));
    let build = || {
        SitemapRequestListBuilder::default()
            .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
            .http_client(client.clone())
            .persist(kvs.clone(), "progress")
            .build()
            .expect("list")
    };
    let original = build();
    assert_eq!(
        original
            .fetch_next()
            .await
            .expect("fetch")
            .expect("request")
            .url
            .as_str(),
        "https://example.com/a"
    );
    original.persist().await.expect("persist");
    assert_eq!(drain(&build()).await, vec!["https://example.com/b"]);
}

#[tokio::test]
async fn root_failure_status_survives_restart() {
    let client = Arc::new(FakeHttpClient::default());
    client.add_status(
        "https://example.com/one.xml",
        StatusCode::INTERNAL_SERVER_ERROR,
        "",
        1,
    );
    client.add("https://example.com/two.xml", urlset(0, 1), 8);
    let kvs: Arc<dyn KeyValueStore> = Arc::new(MemoryKeyValueStore::new("root-status"));
    let build = || {
        SitemapRequestListBuilder::default()
            .sitemap_urls([
                Url::parse("https://example.com/one.xml").expect("URL"),
                Url::parse("https://example.com/two.xml").expect("URL"),
            ])
            .http_client(client.clone())
            .persist(kvs.clone(), "progress")
            .build()
            .expect("list")
    };
    let original = build();
    assert!(original.fetch_next().await.expect("fetch").is_some());
    original.persist().await.expect("persist");
    client.add_status(
        "https://example.com/two.xml",
        StatusCode::INTERNAL_SERVER_ERROR,
        "",
        1,
    );
    assert!(build().fetch_next().await.is_err());
}

#[tokio::test]
async fn state_queries_are_in_memory_only_and_persist_loads_before_writing() {
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml", urlset(0, 4), 8);
    let kvs: Arc<dyn KeyValueStore> = Arc::new(MemoryKeyValueStore::new("lazy-load"));
    let build = || {
        SitemapRequestListBuilder::default()
            .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
            .http_client(client.clone())
            .persist(kvs.clone(), "progress")
            .build()
            .expect("list")
    };
    let original = build();
    assert!(original.fetch_next().await.expect("fetch").is_some());
    assert!(original.fetch_next().await.expect("fetch").is_some());
    original.persist().await.expect("persist");

    let observer = build();
    assert_eq!(observer.processed_count().await, 0);
    assert!(!observer.is_finished().await);
    observer
        .persist()
        .await
        .expect("preserve loaded checkpoint");
    assert_eq!(drain(&build()).await.len(), 2);
}

#[tokio::test]
async fn persisted_state_has_the_strict_version_two_schema() {
    let client = Arc::new(FakeHttpClient::default());
    client.add("https://example.com/sitemap.xml", urlset(0, 2), 8);
    let kvs = Arc::new(MemoryKeyValueStore::new("state-shape"));
    let list = SitemapRequestListBuilder::default()
        .sitemap_url(Url::parse("https://example.com/sitemap.xml").expect("URL"))
        .http_client(client)
        .persist(kvs.clone(), "progress")
        .build()
        .expect("list");
    assert!(list.fetch_next().await.expect("fetch").is_some());
    list.persist().await.expect("persist");

    let entry = kvs
        .get_bytes("progress")
        .await
        .expect("read state")
        .expect("stored state");
    let value: serde_json::Value = serde_json::from_slice(&entry.value).expect("JSON state");
    let object = value.as_object().expect("state object");
    assert_eq!(object.get("version"), Some(&serde_json::json!(2)));
    let mut fields = object.keys().map(String::as_str).collect::<Vec<_>>();
    fields.sort_unstable();
    assert_eq!(
        fields,
        [
            "completed",
            "completed_failures",
            "emitted_in_progress",
            "emitted_total",
            "in_progress",
            "in_progress_depth",
            "pending",
            "pending_depths",
            "version",
        ]
    );
}

#[tokio::test]
async fn pending_depth_survives_restart_and_enforces_the_cap() {
    let client = Arc::new(FakeHttpClient::default());
    for depth in 0..4 {
        let current = format!("https://example.com/l{depth}.xml");
        let next = format!("https://example.com/l{}.xml", depth + 1);
        client.add(
            &current,
            format!("<sitemapindex><sitemap><loc>{next}</loc></sitemap></sitemapindex>"),
            9,
        );
    }
    client.add(
        "https://example.com/l4.xml",
        "<sitemapindex><sitemap><loc>https://example.com/content.xml</loc></sitemap><sitemap><loc>https://example.com/l5.xml</loc></sitemap></sitemapindex>",
        9,
    );
    client.add("https://example.com/content.xml", urlset(0, 1), 9);
    client.add(
        "https://example.com/l5.xml",
        "<sitemapindex><sitemap><loc>https://example.com/too-deep.xml</loc></sitemap></sitemapindex>",
        9,
    );
    client.add("https://example.com/too-deep.xml", urlset(10, 1), 9);
    let kvs: Arc<dyn KeyValueStore> = Arc::new(MemoryKeyValueStore::new("depth"));
    let build = || {
        SitemapRequestListBuilder::default()
            .sitemap_url(Url::parse("https://example.com/l0.xml").expect("URL"))
            .http_client(client.clone())
            .persist(kvs.clone(), "progress")
            .build()
            .expect("list")
    };
    let original = build();
    assert!(original.fetch_next().await.expect("fetch").is_some());
    original.persist().await.expect("persist");
    assert!(drain(&build()).await.is_empty());
}

#[tokio::test]
async fn pending_roots_keep_root_failure_semantics_after_restart() {
    let client = Arc::new(FakeHttpClient::default());
    client.add_status(
        "https://example.com/one.xml",
        StatusCode::INTERNAL_SERVER_ERROR,
        "",
        1,
    );
    client.add_status(
        "https://example.com/two.xml",
        StatusCode::INTERNAL_SERVER_ERROR,
        "",
        1,
    );
    let kvs: Arc<dyn KeyValueStore> = Arc::new(MemoryKeyValueStore::new("pending-roots"));
    let build = || {
        SitemapRequestListBuilder::default()
            .sitemap_urls([
                Url::parse("https://example.com/one.xml").expect("URL"),
                Url::parse("https://example.com/two.xml").expect("URL"),
            ])
            .http_client(client.clone())
            .persist(kvs.clone(), "progress")
            .build()
            .expect("list")
    };
    build().persist().await.expect("persist initial roots");
    assert!(build().fetch_next().await.is_err());
}