web2md 0.1.4

A tool that fetches web pages and returns them as Markdown for MCP token efficiency
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
use std::time::Duration;
use url::Url;
use web2md::{
    extract_feed_links, extract_metadata, extract_page_metadata, feed_to_markdown,
    normalize_crawl_url, parse_feed, parse_sitemap_urls, same_origin_links, Browser,
    BrowserOptions, McpRequest, McpServer, PageToMarkdown,
};

#[tokio::test]
async fn fetch_and_convert_to_markdown() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/article")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            "<html><head><title>My Article</title></head>
             <body><h1>Heading</h1><p>First paragraph.</p><p>Second paragraph.</p></body>
             </html>",
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/article", server.url())).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();

    assert!(md.contains("Heading"));
    assert!(md.contains("First paragraph."));
    assert!(md.contains("Second paragraph."));
    mock.assert_async().await;
}

#[tokio::test]
async fn fetch_and_convert_to_plain_text() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/plain")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            "<html><body><h1>Heading</h1><p>Text with <strong>bold</strong>.</p></body></html>",
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/plain", server.url())).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();
    let text = PageToMarkdown::to_plain_text(&md);

    assert!(text.contains("Heading"));
    assert!(text.contains("bold"));
    assert!(!text.contains("**"));
    mock.assert_async().await;
}

#[tokio::test]
async fn fetch_404_propagates_error() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/missing")
        .with_status(404)
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let result = browser.fetch(&format!("{}/missing", server.url())).await;

    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("404"));
    mock.assert_async().await;
}

#[tokio::test]
async fn mcp_server_end_to_end() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/doc")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            "<html><head><title>Integration Test</title></head>
             <body><h1>Title</h1><p>Body text.</p></body>
             </html>",
        )
        .create_async()
        .await;

    let mcp = McpServer::new().unwrap();
    let resp = mcp
        .handle(McpRequest {
            url: format!("{}/doc", server.url()),
            include_images: false,
            keep_header: false,
            main_content: false,
            max_length: None,
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(resp.meta.title, Some("Integration Test".to_string()));
    assert!(resp.markdown.contains("Title"));
    assert!(resp.markdown.contains("Body text."));
    mock.assert_async().await;
}

#[tokio::test]
async fn mcp_server_max_length_truncation() {
    let mut server = mockito::Server::new_async().await;
    let body = "<html><body><p>".to_string()
        + &"a ".repeat(500)
        + "</p></body></html>";
    let mock = server
        .mock("GET", "/long")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(body)
        .create_async()
        .await;

    let mcp = McpServer::new().unwrap();
    let resp = mcp
        .handle(McpRequest {
            url: format!("{}/long", server.url()),
            include_images: false,
            keep_header: false,
            main_content: false,
            max_length: Some(100),
            ..Default::default()
        })
        .await
        .unwrap();

    assert!(resp.markdown.contains("[truncated]"));
    assert!(resp.markdown.len() <= 120); // rough bound: 100 chars + "\n\n[truncated]"
    mock.assert_async().await;
}

#[tokio::test]
async fn custom_timeout_is_applied() {
    let mut opts = BrowserOptions::default();
    opts.timeout = Duration::from_secs(5);

    let browser = Browser::new(opts).unwrap();
    assert_eq!(browser.options().timeout, Duration::from_secs(5));
}

#[tokio::test]
async fn strips_scripts_and_styles_in_integration() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/styled")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            "<html><head><style>.red{color:red}</style></head>
             <body>
                <script>alert('xss')</script>
                <p>Visible content</p>
             </body></html>",
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/styled", server.url())).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();

    assert!(!md.contains("alert"));
    assert!(!md.contains("color:red"));
    assert!(md.contains("Visible content"));
    mock.assert_async().await;
}

#[tokio::test]
async fn strips_noise_tags_in_integration() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/noisy")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            r#"<html>
             <head><!-- tracking comment --></head>
             <body>
                <nav><a href="/">Home</a></nav>
                <p>Real content here</p>
                <aside>Related links</aside>
                <noscript>Enable JS</noscript>
                <footer>Copyright 2025</footer>
             </body></html>"#,
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/noisy", server.url())).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();

    assert!(md.contains("Real content here"));
    assert!(!md.contains("Home"));
    assert!(!md.contains("Related links"));
    assert!(!md.contains("Enable JS"));
    assert!(!md.contains("Copyright"));
    assert!(!md.contains("tracking"));
    mock.assert_async().await;
}

#[tokio::test]
async fn cli_format_html_emits_raw_html() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/raw")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body("<html><body><h1>Title</h1><p>Content</p></body></html>")
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/raw", server.url())).await.unwrap();

    assert!(html.contains("<html>") || html.contains("<body>") || html.contains("<h1>"),
        "expected raw HTML tags in output, got: {}", html);
    mock.assert_async().await;
}

#[tokio::test]
async fn cli_render_adds_ansi_codes() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/render")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body("<html><body><h1>Title</h1><p>Content with <a href=\"/link\">link</a>.</p></body></html>")
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/render", server.url())).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();

    // Simulate what --render does: the render_markdown_ansi function is in main.rs
    // and not exposed via the library, so we verify the markdown contains content
    // that would produce ANSI output. The actual ANSI rendering is tested in main.rs unit tests.
    assert!(md.contains("Title"), "expected title in markdown, got: {}", md);
    assert!(md.contains("Content"), "expected content in markdown, got: {}", md);
    mock.assert_async().await;
}

#[tokio::test]
async fn readability_main_content_extracts_from_div_layout() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/layout")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            r#"<html><body>
            <div><a href="/">Home</a><a href="/about">About</a><a href="/contact">Contact</a></div>
            <div><h2>Real Article</h2><p>This is the main article content with enough text to be extracted by the readability scoring algorithm. It contains substantial paragraphs that should score higher than the navigation div above.</p></div>
            </body></html>"#,
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/layout", server.url())).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, true, &[]).unwrap();

    assert!(md.contains("main article content"));
    assert!(!md.contains("Contact"));
    mock.assert_async().await;
}

#[tokio::test]
async fn json_output_format_emits_structured_json() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/json")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(r#"<html lang="en"><head>
            <title>JSON Test Page</title>
            <meta name="description" content="A test page for JSON output">
            <meta name="author" content="Test Author">
            <meta property="article:published_time" content="2025-07-04T12:00:00Z">
            <meta property="og:url" content="https://example.com/json-canonical">
            <meta property="article:section" content="Testing">
            <link rel="canonical" href="https://example.com/json">
        </head><body><h1>Heading</h1><p>Body content for JSON with enough words to populate the excerpt metadata field.</p></body></html>"#)
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/json", server.url())).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();
    let meta = extract_metadata(&html);

    let json = serde_json::json!({
        "markdown": md,
        "title": meta.title,
        "description": meta.description,
        "author": meta.author,
        "published_date": meta.published_date,
        "categories": meta.categories,
        "excerpt": meta.excerpt,
        "canonical_url": meta.canonical_url,
        "language": meta.language,
    });
    let json_str = serde_json::to_string(&json).unwrap();

    assert!(json_str.contains("JSON Test Page"));
    assert!(json_str.contains("A test page for JSON output"));
    assert!(json_str.contains("Test Author"));
    assert!(json_str.contains("2025-07-04T12:00:00Z"));
    assert!(json_str.contains("json-canonical"));
    assert!(json_str.contains("\"language\":\"en\""));
    assert!(json_str.contains("Testing"));
    assert!(json_str.contains("Body content for JSON"));
    mock.assert_async().await;
}

#[tokio::test]
async fn csv_output_format_emits_header_and_row() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/csv")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            r#"<html><head><title>CSV Page</title><meta name="author" content="CSV Author">
            </head><body><article><h1>CSV Page</h1>
            <p>This English paragraph is long enough for language detection and CSV export of the extracted plain text content for corpus pipelines.</p>
            </article></body></html>"#,
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let url = format!("{}/csv", server.url());
    let html = browser.fetch(&url).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, true, &[]).unwrap();
    let text = PageToMarkdown::to_plain_text(&md);
    let meta = extract_page_metadata(&html, &md);
    let csv = meta.to_csv(&url, &text);

    assert!(csv.starts_with(
        "url,title,author,published_date,language,page_type,extraction_quality,fingerprint,text\n"
    ));
    assert!(csv.contains("CSV Page"));
    assert!(csv.contains("CSV Author"));
    assert!(csv.contains("article"));
    assert!(csv.contains("corpus pipelines"));
    assert_eq!(meta.language.as_deref(), Some("eng"));
    assert!(meta.fingerprint.as_ref().map(|f| f.len() == 16).unwrap_or(false));
    mock.assert_async().await;
}

#[tokio::test]
async fn tei_output_format_emits_tei_document() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/tei")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            r#"<html lang="en"><head><title>TEI Page</title><meta name="author" content="TEI Author">
            <meta property="og:site_name" content="TEI Site">
            </head><body><article><h1>TEI Page</h1>
            <p>This English paragraph is long enough for TEI export of the extracted plain text content for corpus pipelines.</p>
            </article></body></html>"#,
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let url = format!("{}/tei", server.url());
    let html = browser.fetch(&url).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, true, &[]).unwrap();
    let text = PageToMarkdown::to_plain_text(&md);
    let meta = extract_page_metadata(&html, &md);
    let tei = meta.to_tei(&url, &text);

    assert!(tei.contains("<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">"));
    assert!(tei.contains("<title>TEI Page</title>"));
    assert!(tei.contains("<author>TEI Author</author>"));
    assert!(tei.contains("<publisher>TEI Site</publisher>"));
    assert!(tei.contains("<language ident=\"en\"/>"));
    assert!(tei.contains("<div type=\"entry\">"));
    assert!(tei.contains("corpus pipelines"));
    mock.assert_async().await;
}

#[tokio::test]
async fn xml_output_format_emits_doc() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/xml")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            r#"<html lang="en"><head><title>XML Page</title><meta name="author" content="XML Author">
            </head><body><article><h1>XML Page</h1>
            <p>This English paragraph is long enough for plain XML export of the extracted plain text content for corpus pipelines.</p>
            </article></body></html>"#,
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let url = format!("{}/xml", server.url());
    let html = browser.fetch(&url).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, true, &[]).unwrap();
    let text = PageToMarkdown::to_plain_text(&md);
    let meta = extract_page_metadata(&html, &md);
    let xml = meta.to_xml(&url, &text);

    assert!(xml.contains("<doc>"));
    assert!(xml.contains("<title>XML Page</title>"));
    assert!(xml.contains("<author>XML Author</author>"));
    assert!(xml.contains("<main>"));
    assert!(xml.contains("corpus pipelines"));
    assert!(meta.fingerprint.as_ref().map(|f| f.len() == 16).unwrap_or(false));
    mock.assert_async().await;
}

#[tokio::test]
async fn sitemap_discovery_fetches_and_parses() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/sitemap.xml")
        .with_status(200)
        .with_header("content-type", "application/xml")
        .with_body(r#"<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url><loc>https://example.com/page1</loc></url>
  <url><loc>https://example.com/page2</loc></url>
</urlset>"#)
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let sitemap_url = format!("{}/sitemap.xml", server.url());
    let xml = browser.fetch(&sitemap_url).await.unwrap();
    let urls = parse_sitemap_urls(&xml);

    assert_eq!(urls.len(), 2);
    assert!(urls.contains(&"https://example.com/page1".to_string()));
    assert!(urls.contains(&"https://example.com/page2".to_string()));
    mock.assert_async().await;
}

#[tokio::test]
async fn feed_discovery_from_html_page() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/blog")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(r#"<html><head>
            <link rel="alternate" type="application/rss+xml" href="/blog/rss.xml">
            <link rel="alternate" type="application/atom+xml" href="/blog/atom.xml">
            <link rel="alternate" type="application/feed+json" href="/blog/feed.json">
        </head><body><h1>Blog</h1></body></html>"#)
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let html = browser.fetch(&format!("{}/blog", server.url())).await.unwrap();
    let feeds = extract_feed_links(&html);

    assert_eq!(feeds.len(), 3);
    assert!(feeds.contains(&"/blog/rss.xml".to_string()));
    assert!(feeds.contains(&"/blog/atom.xml".to_string()));
    assert!(feeds.contains(&"/blog/feed.json".to_string()));
    mock.assert_async().await;
}

#[tokio::test]
async fn feed_command_parses_rss_to_markdown() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/rss.xml")
        .with_status(200)
        .with_header("content-type", "application/rss+xml")
        .with_body(r#"<?xml version="1.0"?>
<rss version="2.0"><channel>
  <title>Test Feed</title>
  <link>https://example.com</link>
  <item>
    <title>Hello RSS</title>
    <link>https://example.com/hello</link>
    <pubDate>Sat, 11 Jul 2026 12:00:00 GMT</pubDate>
    <description>A short summary</description>
  </item>
</channel></rss>"#)
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let xml = browser.fetch(&format!("{}/rss.xml", server.url())).await.unwrap();
    let feed = parse_feed(&xml).expect("should parse RSS");
    let md = feed_to_markdown(&feed);

    assert_eq!(feed.title.as_deref(), Some("Test Feed"));
    assert_eq!(feed.entries.len(), 1);
    assert!(md.contains("# Test Feed"));
    assert!(md.contains("## [Hello RSS](https://example.com/hello)"));
    assert!(md.contains("A short summary"));
    mock.assert_async().await;
}

#[tokio::test]
async fn feed_command_parses_atom() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/atom.xml")
        .with_status(200)
        .with_header("content-type", "application/atom+xml")
        .with_body(r#"<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Atom Test</title>
  <link href="https://example.com/" rel="alternate"/>
  <entry>
    <title>Atom Entry</title>
    <link href="https://example.com/atom-entry"/>
    <updated>2026-07-11T10:00:00Z</updated>
    <summary>Atom summary text</summary>
  </entry>
</feed>"#)
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let xml = browser.fetch(&format!("{}/atom.xml", server.url())).await.unwrap();
    let feed = parse_feed(&xml).expect("should parse Atom");

    assert_eq!(feed.title.as_deref(), Some("Atom Test"));
    assert_eq!(feed.link.as_deref(), Some("https://example.com/"));
    assert_eq!(feed.entries[0].title.as_deref(), Some("Atom Entry"));
    assert_eq!(
        feed.entries[0].link.as_deref(),
        Some("https://example.com/atom-entry")
    );
    mock.assert_async().await;
}

#[tokio::test]
async fn feed_command_parses_json_feed() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/feed.json")
        .with_status(200)
        .with_header("content-type", "application/feed+json")
        .with_body(r#"{
            "version": "https://jsonfeed.org/version/1.1",
            "title": "JSON Feed Test",
            "home_page_url": "https://example.com/",
            "items": [
                {
                    "id": "1",
                    "url": "https://example.com/post",
                    "title": "Hello JSON Feed",
                    "content_text": "Body from JSON Feed",
                    "date_published": "2026-07-11T12:00:00Z"
                }
            ]
        }"#)
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let body = browser.fetch(&format!("{}/feed.json", server.url())).await.unwrap();
    let feed = parse_feed(&body).expect("should parse JSON Feed");
    let md = feed_to_markdown(&feed);

    assert_eq!(feed.title.as_deref(), Some("JSON Feed Test"));
    assert_eq!(feed.entries.len(), 1);
    assert!(md.contains("# JSON Feed Test"));
    assert!(md.contains("## [Hello JSON Feed](https://example.com/post)"));
    assert!(md.contains("Body from JSON Feed"));
    mock.assert_async().await;
}

#[tokio::test]
async fn js_disabled_ignores_document_write() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/page")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            "<html><body><p>Static</p>\
             <script>document.write(\"<p>Dynamic</p>\");</script>\
             </body></html>",
        )
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let url = format!("{}/page", server.url());
    let html = browser.fetch(&url).await.unwrap();
    let html = browser.run_inline_scripts(&html);
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();

    assert!(md.contains("Static"));
    assert!(!md.contains("Dynamic"));
    mock.assert_async().await;
}

#[tokio::test]
async fn js_enabled_captures_document_write() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/page")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            "<html><body><p>Static</p>\
             <script>var items=[\"a\",\"b\"]; for (var i of items){document.write(\"<p>\"+i+\"</p>\");}</script>\
             <script type=\"application/ld+json\">{\"x\":1}</script>\
             <script src=\"external.js\"></script>\
             </body></html>",
        )
        .create_async()
        .await;

    let mut opts = BrowserOptions::default();
    opts.enable_javascript = true;
    let browser = Browser::new(opts).unwrap();
    let url = format!("{}/page", server.url());
    let html = browser.fetch(&url).await.unwrap();
    let html = browser.run_inline_scripts(&html);

    // Captured HTML is injected before </body>.
    assert!(html.contains("<p>a</p>"));
    assert!(html.contains("<p>b</p>"));
    assert!(html.contains("Static"));

    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();
    assert!(md.contains("Static"));
    assert!(md.contains("a"));
    assert!(md.contains("b"));
    mock.assert_async().await;
}

#[tokio::test]
async fn settimeout_captures_delayed_content_with_wait() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/page")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            "<html><body><p>Static</p>\
             <script>setTimeout(function(){document.write(\"<p>Delayed</p>\");}, 50);</script>\
             </body></html>",
        )
        .create_async()
        .await;

    let mut opts = BrowserOptions::default();
    opts.enable_javascript = true;
    opts.post_load_wait = Duration::from_millis(100);
    let browser = Browser::new(opts).unwrap();
    let url = format!("{}/page", server.url());
    let html = browser.fetch(&url).await.unwrap();
    let html = browser.prepare_html(&html, &url).await.unwrap();

    assert!(html.contains("Delayed"));
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();
    assert!(md.contains("Delayed"));
    mock.assert_async().await;
}

#[tokio::test]
async fn setinterval_captures_repeated_content_with_wait() {
    let mut server = mockito::Server::new_async().await;
    let mock = server
        .mock("GET", "/page")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(
            "<html><body>\
             <script>setInterval(function(){document.write(\"x\");}, 40);</script>\
             </body></html>",
        )
        .create_async()
        .await;

    let mut opts = BrowserOptions::default();
    opts.enable_javascript = true;
    opts.post_load_wait = Duration::from_millis(120);
    let browser = Browser::new(opts).unwrap();
    let url = format!("{}/page", server.url());
    let html = browser.fetch(&url).await.unwrap();
    let html = browser.prepare_html(&html, &url).await.unwrap();

    assert!(html.contains("xxx"));
    mock.assert_async().await;
}

#[tokio::test]
async fn blacklisted_iframe_not_inlined_in_pipeline() {
    let mut server = mockito::Server::new_async().await;
    let iframe_mock = server
        .mock("GET", "/beacon")
        .with_status(200)
        .with_body("TRACKED")
        .expect(0)
        .create_async()
        .await;

    let main_mock = server
        .mock("GET", "/page")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(r#"<html><body><h1>Article</h1><iframe src="/beacon"></iframe></body></html>"#)
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let url = format!("{}/page", server.url());
    let html = browser.fetch(&url).await.unwrap();
    let html = browser.inline_iframes(&html, &url).await.unwrap();
    let md = PageToMarkdown::convert(&html, false, false, false, &[]).unwrap();

    assert!(md.contains("Article"));
    assert!(!md.contains("TRACKED"));
    iframe_mock.assert_async().await;
    main_mock.assert_async().await;
}

#[tokio::test]
async fn recursive_crawl_depth_one_discovers_same_origin_links() {
    let mut server = mockito::Server::new_async().await;
    let base = server.url();

    let root = server
        .mock("GET", "/")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(format!(
            r#"<html><body>
                <p>Root page</p>
                <a href="{}/a">A</a>
                <a href="{}/b">B</a>
                <a href="https://other.example.com/x">External</a>
            </body></html>"#,
            base, base
        ))
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let root_url = format!("{}/", base);
    let origin = Url::parse(&root_url).unwrap();

    let html = browser.fetch(&root_url).await.unwrap();
    let links = same_origin_links(&html, &root_url, &origin);
    assert_eq!(links.len(), 2);
    assert!(links.iter().any(|u| u.ends_with("/a")));
    assert!(links.iter().any(|u| u.ends_with("/b")));

    root.assert_async().await;
}

#[tokio::test]
async fn recursive_crawl_depth_two_reaches_nested_page() {
    use std::collections::{HashSet, VecDeque};

    let mut server = mockito::Server::new_async().await;
    let base = server.url();

    let page_c = server
        .mock("GET", "/c")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body("<html><body><p>Page C nested</p></body></html>")
        .create_async()
        .await;

    let page_a = server
        .mock("GET", "/a")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(format!(
            r#"<html><body><p>Page A</p><a href="{}/c">C</a></body></html>"#,
            base
        ))
        .create_async()
        .await;

    let root = server
        .mock("GET", "/")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(format!(
            r#"<html><body><p>Root</p><a href="{}/a">A</a></body></html>"#,
            base
        ))
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let root_url = format!("{}/", base);
    let origin = Url::parse(&root_url).unwrap();

    let mut visited = HashSet::new();
    let mut queue = VecDeque::from([(root_url.clone(), 0u32)]);
    let depth = 2u32;
    let mut fetched = Vec::new();

    while let Some((url, level)) = queue.pop_front() {
        let key = normalize_crawl_url(&url, &url).unwrap_or(url.clone());
        if !visited.insert(key) {
            continue;
        }
        let html = browser.fetch(&url).await.unwrap();
        fetched.push(url.clone());
        if level < depth {
            for link in same_origin_links(&html, &url, &origin) {
                let link_key = normalize_crawl_url(&link, &link).unwrap_or(link.clone());
                if !visited.contains(&link_key) {
                    queue.push_back((link, level + 1));
                }
            }
        }
    }

    assert_eq!(fetched.len(), 3);
    assert!(fetched.iter().any(|u| u.ends_with("/c")));

    root.assert_async().await;
    page_a.assert_async().await;
    page_c.assert_async().await;
}

#[tokio::test]
async fn robots_txt_blocks_disallowed_paths() {
    let mut server = mockito::Server::new_async().await;
    let _robots = server
        .mock("GET", "/robots.txt")
        .with_status(200)
        .with_header("content-type", "text/plain")
        .with_body("User-agent: *\nDisallow: /hidden/\n")
        .create_async()
        .await;

    let allowed = server
        .mock("GET", "/visible")
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body("<html><body><p>Visible</p></body></html>")
        .create_async()
        .await;

    let browser = Browser::new(BrowserOptions::default()).unwrap();
    let visible = browser
        .fetch(&format!("{}/visible", server.url()))
        .await
        .unwrap();
    let md = PageToMarkdown::convert(&visible, false, false, false, &[]).unwrap();
    assert!(md.contains("Visible"));

    assert!(!browser
        .robots_allows(&format!("{}/hidden/page", server.url()))
        .await
        .unwrap());

    allowed.assert_async().await;
}