fetchkit 0.2.0

AI-friendly web content fetching and HTML-to-Markdown conversion library
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
//! Integration tests for FetchKit using wiremock

use fetchkit::{
    fetch_with_options, DnsPolicy, FetchError, FetchOptions, FetchRequest, FetcherRegistry,
    HttpMethod, LocalFileSaver, Tool,
};
use serde_json::json;
use tower::Service;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

/// Helper: create FetchOptions that permit loopback (for wiremock tests)
fn test_options() -> FetchOptions {
    FetchOptions {
        enable_markdown: true,
        enable_text: true,
        dns_policy: DnsPolicy::allow_all(),
        ..Default::default()
    }
}

/// Helper: create a Tool that permits loopback (for wiremock tests)
fn test_tool() -> Tool {
    Tool::builder().block_private_ips(false).build()
}

/// Helper: create a Tool with save_to_file enabled (for wiremock tests)
fn test_tool_with_save() -> Tool {
    Tool::builder()
        .block_private_ips(false)
        .enable_save_to_file(true)
        .build()
}

#[tokio::test]
async fn test_simple_get() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("Hello, World!")
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert_eq!(resp.content_type, Some("text/plain".to_string()));
    assert!(resp.content.unwrap().contains("Hello, World!"));
    assert_eq!(resp.format, Some("raw".to_string()));
}

#[tokio::test]
async fn test_head_request() {
    let mock_server = MockServer::start().await;

    Mock::given(method("HEAD"))
        .and(path("/file.pdf"))
        .respond_with(
            ResponseTemplate::new(200)
                .insert_header("content-type", "application/pdf")
                .insert_header("content-length", "12345")
                .insert_header("last-modified", "Tue, 01 Jan 2024 00:00:00 GMT"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/file.pdf", mock_server.uri())).method(HttpMethod::Head);
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert_eq!(resp.method, Some("HEAD".to_string()));
    assert_eq!(resp.content_type, Some("application/pdf".to_string()));
    assert_eq!(resp.size, Some(12345));
    assert_eq!(
        resp.last_modified,
        Some("Tue, 01 Jan 2024 00:00:00 GMT".to_string())
    );
    assert!(resp.content.is_none());
}

#[tokio::test]
async fn test_html_to_markdown() {
    let mock_server = MockServer::start().await;

    let html = r#"<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
    <h1>Hello World</h1>
    <p>This is a <strong>test</strong> paragraph.</p>
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
    </ul>
</body>
</html>"#;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(html, "text/html"))
        .mount(&mock_server)
        .await;

    let tool = test_tool();
    let req = FetchRequest::new(format!("{}/", mock_server.uri())).as_markdown();
    let resp = tool.execute(req).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert_eq!(resp.format, Some("markdown".to_string()));

    let content = resp.content.unwrap();
    assert!(content.contains("# Hello World"));
    assert!(content.contains("**test**"));
    assert!(content.contains("- Item 1"));
    assert!(content.contains("- Item 2"));
}

#[tokio::test]
async fn test_html_to_text() {
    let mock_server = MockServer::start().await;

    let html = r#"<!DOCTYPE html>
<html>
<body>
    <h1>Title</h1>
    <p>Paragraph text.</p>
    <script>alert('bad');</script>
</body>
</html>"#;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(html, "text/html"))
        .mount(&mock_server)
        .await;

    let tool = test_tool();
    let req = FetchRequest::new(format!("{}/", mock_server.uri())).as_text();
    let resp = tool.execute(req).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert_eq!(resp.format, Some("text".to_string()));

    let content = resp.content.unwrap();
    assert!(content.contains("Title"));
    assert!(content.contains("Paragraph text"));
    assert!(!content.contains("alert")); // Script should be stripped
}

#[tokio::test]
async fn test_binary_content() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/image.png"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_bytes(vec![0x89, 0x50, 0x4E, 0x47]) // PNG magic bytes
                .insert_header("content-type", "image/png")
                .insert_header("content-length", "4"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/image.png", mock_server.uri()));
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert_eq!(resp.content_type, Some("image/png".to_string()));
    assert_eq!(resp.size, Some(4));
    assert!(resp.content.is_none());
    assert!(resp.error.is_some());
    assert!(resp.error.unwrap().contains("Binary content"));
}

#[tokio::test]
async fn test_4xx_status() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/not-found"))
        .respond_with(
            ResponseTemplate::new(404)
                .set_body_string("Not Found")
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/not-found", mock_server.uri()));
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    // 4xx is still a success response (not a tool error)
    assert_eq!(resp.status_code, 404);
    assert!(resp.content.unwrap().contains("Not Found"));
}

#[tokio::test]
async fn test_5xx_status() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/error"))
        .respond_with(
            ResponseTemplate::new(500)
                .set_body_string("Internal Server Error")
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/error", mock_server.uri()));
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    // 5xx is still a success response (not a tool error)
    assert_eq!(resp.status_code, 500);
    assert!(resp.content.unwrap().contains("Internal Server Error"));
}

#[tokio::test]
async fn test_content_disposition_filename() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/download"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("file content")
                .insert_header("content-type", "text/plain")
                .insert_header("content-disposition", "attachment; filename=\"report.txt\""),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/download", mock_server.uri()));
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    assert_eq!(resp.filename, Some("report.txt".to_string()));
}

#[tokio::test]
async fn test_filename_from_url() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/path/to/document.pdf"))
        .respond_with(
            ResponseTemplate::new(200)
                .insert_header("content-type", "application/pdf")
                .insert_header("content-length", "100"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/path/to/document.pdf", mock_server.uri()))
        .method(HttpMethod::Head);
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    assert_eq!(resp.filename, Some("document.pdf".to_string()));
}

#[tokio::test]
async fn test_size_for_text_content() {
    let mock_server = MockServer::start().await;

    let body = "Hello, this is test content!";

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string(body)
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    // Size should equal bytes read from body
    assert_eq!(resp.size, Some(body.len() as u64));
}

#[tokio::test]
async fn test_text_body_truncated_at_safety_limit() {
    let mock_server = MockServer::start().await;

    let body = "A".repeat(1024);

    Mock::given(method("GET"))
        .and(path("/large"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string(body)
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/large", mock_server.uri()));
    let resp = fetch_with_options(
        req,
        FetchOptions {
            enable_markdown: true,
            enable_text: true,
            dns_policy: DnsPolicy::allow_all(),
            max_body_size: Some(128),
            ..Default::default()
        },
    )
    .await
    .unwrap();

    assert_eq!(resp.size, Some(128));
    assert_eq!(resp.truncated, Some(true));

    let content = resp.content.unwrap();
    assert!(content.starts_with(&"A".repeat(128)));
    assert!(content.contains("[..content truncated...]"));
}

#[tokio::test]
async fn test_url_prefix_allow_list() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .mount(&mock_server)
        .await;

    // Create tool with allow list that doesn't include the mock server
    let tool = Tool::builder()
        .block_private_ips(false)
        .allow_prefix("https://allowed.example.com")
        .build();

    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let result = tool.execute(req).await;

    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("not allowed by policy"));
}

#[tokio::test]
async fn test_url_prefix_block_list() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .mount(&mock_server)
        .await;

    // Create tool with block list that includes localhost
    let tool = Tool::builder()
        .block_private_ips(false)
        .block_prefix("http://127.0.0.1")
        .build();

    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let result = tool.execute(req).await;

    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("not allowed by policy"));
}

#[tokio::test]
async fn test_invalid_url_scheme() {
    let req = FetchRequest::new("ftp://example.com/file.txt");
    let result = fetch_with_options(req, test_options()).await;

    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("http:// or https://"));
}

#[tokio::test]
async fn test_missing_url() {
    let req = FetchRequest::new("");
    let result = fetch_with_options(req, test_options()).await;

    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Missing"));
}

#[tokio::test]
async fn test_entity_decoding_in_html() {
    let mock_server = MockServer::start().await;

    let html = "<p>Tom &amp; Jerry &lt;3 &gt; others &quot;quoted&quot;</p>";

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(html, "text/html"))
        .mount(&mock_server)
        .await;

    let tool = test_tool();
    let req = FetchRequest::new(format!("{}/", mock_server.uri())).as_text();
    let resp = tool.execute(req).await.unwrap();

    let content = resp.content.unwrap();
    assert!(content.contains("Tom & Jerry"));
    assert!(content.contains("<3"));
    assert!(content.contains("> others"));
    assert!(content.contains("\"quoted\""));
}

#[tokio::test]
async fn test_non_html_with_conversion_flags() {
    let mock_server = MockServer::start().await;

    let json = r#"{"key": "value"}"#;

    Mock::given(method("GET"))
        .and(path("/api/data"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string(json)
                .insert_header("content-type", "application/json"),
        )
        .mount(&mock_server)
        .await;

    let tool = test_tool();
    let req = FetchRequest::new(format!("{}/api/data", mock_server.uri())).as_markdown();
    let resp = tool.execute(req).await.unwrap();

    // Non-HTML should return raw even with as_markdown flag
    assert_eq!(resp.format, Some("raw".to_string()));
    assert!(resp.content.unwrap().contains("\"key\""));
}

#[tokio::test]
async fn test_html_detection_by_body() {
    let mock_server = MockServer::start().await;

    // Server returns HTML without proper content-type
    let html = "<!DOCTYPE html><html><body>Hello</body></html>";

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string(html)
                .insert_header("content-type", "text/plain"), // Wrong content-type
        )
        .mount(&mock_server)
        .await;

    let tool = test_tool();
    let req = FetchRequest::new(format!("{}/", mock_server.uri())).as_markdown();
    let resp = tool.execute(req).await.unwrap();

    // Should detect HTML by body content and convert
    assert_eq!(resp.format, Some("markdown".to_string()));
}

#[tokio::test]
async fn test_custom_user_agent() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .mount(&mock_server)
        .await;

    let tool = Tool::builder()
        .block_private_ips(false)
        .user_agent("CustomBot/1.0")
        .build();

    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let resp = tool.execute(req).await.unwrap();

    assert_eq!(resp.status_code, 200);
}

#[tokio::test]
async fn test_excessive_newlines_filtered() {
    let mock_server = MockServer::start().await;

    let body = "Line1\n\n\n\n\n\nLine2";

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string(body)
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    // Should have at most 2 consecutive newlines
    assert!(!resp.content.unwrap().contains("\n\n\n"));
}

// ============================================================================
// Fetcher System Integration Tests
// ============================================================================

#[tokio::test]
async fn test_fetcher_registry_with_defaults() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/page"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("<html><body><h1>Test</h1></body></html>")
                .insert_header("content-type", "text/html"),
        )
        .mount(&mock_server)
        .await;

    let registry = FetcherRegistry::with_defaults();
    let options = FetchOptions {
        enable_markdown: true,
        enable_text: true,
        dns_policy: DnsPolicy::allow_all(),
        ..Default::default()
    };

    let req = FetchRequest::new(format!("{}/page", mock_server.uri())).as_markdown();
    let resp = registry.fetch(req, options).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert_eq!(resp.format, Some("markdown".to_string()));
    assert!(resp.content.unwrap().contains("# Test"));
}

#[tokio::test]
async fn test_fetcher_registry_url_validation() {
    let registry = FetcherRegistry::with_defaults();
    let options = FetchOptions {
        dns_policy: DnsPolicy::allow_all(),
        ..Default::default()
    };

    // Invalid scheme
    let req = FetchRequest::new("ftp://example.com");
    let result = registry.fetch(req, options.clone()).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("http://"));

    // Empty URL handled by fetch_with_options before registry
    let req = FetchRequest::new("");
    let result = fetch_with_options(req, options).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fetcher_registry_allow_block_lists() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .mount(&mock_server)
        .await;

    let registry = FetcherRegistry::with_defaults();

    // Block list
    let options = FetchOptions {
        block_prefixes: vec!["http://127.0.0.1".to_string()],
        dns_policy: DnsPolicy::allow_all(),
        ..Default::default()
    };
    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let result = registry.fetch(req, options).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Blocked"));

    // Allow list (not matching)
    let options = FetchOptions {
        allow_prefixes: vec!["https://allowed.com".to_string()],
        dns_policy: DnsPolicy::allow_all(),
        ..Default::default()
    };
    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let result = registry.fetch(req, options).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fetcher_registry_allow_list_rejects_lookalike_host() {
    let registry = FetcherRegistry::with_defaults();
    let options = FetchOptions {
        allow_prefixes: vec!["https://allowed.example.com".to_string()],
        dns_policy: DnsPolicy::allow_all(),
        ..Default::default()
    };

    let req = FetchRequest::new("https://allowed.example.com.evil.test/");
    let result = registry.fetch(req, options).await;

    assert!(matches!(result, Err(FetchError::BlockedUrl)));
}

#[tokio::test]
async fn test_github_fetcher_url_matching() {
    // These URLs should NOT match GitHubRepoFetcher (will use DefaultFetcher)
    let mock_server = MockServer::start().await;

    // Mock for non-GitHub URLs
    Mock::given(method("GET"))
        .and(path("/owner/repo/issues"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("issues page")
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let req = FetchRequest::new(format!("{}/owner/repo/issues", mock_server.uri()));
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    // Should use default fetcher (format is "raw", not "github_repo")
    assert_eq!(resp.format, Some("raw".to_string()));
    assert!(resp.content.unwrap().contains("issues page"));
}

#[tokio::test]
async fn test_fetch_enables_conversions_by_default() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("<html><body><p>Hello</p></body></html>")
                .insert_header("content-type", "text/html"),
        )
        .mount(&mock_server)
        .await;

    // test_options() has enable_markdown: true (matching fetch() default)
    let req = FetchRequest::new(format!("{}/", mock_server.uri())).as_markdown();
    let resp = fetch_with_options(req, test_options()).await.unwrap();

    assert_eq!(resp.format, Some("markdown".to_string()));
}

#[tokio::test]
async fn test_fetch_with_options_respects_disabled_conversion() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("<html><body><p>Hello</p></body></html>")
                .insert_header("content-type", "text/html"),
        )
        .mount(&mock_server)
        .await;

    // Disable markdown conversion
    let options = FetchOptions {
        enable_markdown: false,
        enable_text: false,
        dns_policy: DnsPolicy::allow_all(),
        ..Default::default()
    };

    let req = FetchRequest::new(format!("{}/", mock_server.uri())).as_markdown();
    let resp = fetch_with_options(req, options).await.unwrap();

    // Should be raw because conversion is disabled
    assert_eq!(resp.format, Some("raw".to_string()));
}

// ============================================================================
// Safe-by-default: fetch() and Tool::default() block private IPs
// ============================================================================

#[tokio::test]
async fn test_fetch_blocks_loopback_by_default() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .mount(&mock_server)
        .await;

    // fetch() uses default options which now block private IPs
    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let result = fetchkit::fetch(req).await;
    assert!(matches!(result, Err(fetchkit::FetchError::BlockedUrl)));
}

#[tokio::test]
async fn test_tool_default_blocks_loopback() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .mount(&mock_server)
        .await;

    let tool = Tool::default();
    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let result = tool.execute(req).await;
    assert!(matches!(result, Err(fetchkit::FetchError::BlockedUrl)));
}

// ============================================================================
// File Save Integration Tests
// ============================================================================

#[tokio::test]
async fn test_save_to_file_text_content() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/data.json"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string(r#"{"key": "value"}"#)
                .insert_header("content-type", "application/json"),
        )
        .mount(&mock_server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let saver = LocalFileSaver::new(Some(dir.path().to_path_buf()));
    let tool = test_tool_with_save();

    let req =
        FetchRequest::new(format!("{}/data.json", mock_server.uri())).save_to_file("output.json");
    let resp = tool.execute_with_saver(req, Some(&saver)).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert!(resp.saved_path.is_some());
    assert_eq!(resp.bytes_written, Some(16));
    // No inline content when saving to file
    assert!(resp.content.is_none());

    // Verify file on disk
    let content = std::fs::read_to_string(dir.path().join("output.json")).unwrap();
    assert_eq!(content, r#"{"key": "value"}"#);
}

#[tokio::test]
async fn test_save_to_file_binary_content() {
    let mock_server = MockServer::start().await;

    let binary_data: Vec<u8> = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];

    Mock::given(method("GET"))
        .and(path("/image.png"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_bytes(binary_data.clone())
                .insert_header("content-type", "image/png"),
        )
        .mount(&mock_server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let saver = LocalFileSaver::new(Some(dir.path().to_path_buf()));
    let tool = test_tool_with_save();

    let req =
        FetchRequest::new(format!("{}/image.png", mock_server.uri())).save_to_file("image.png");
    let resp = tool.execute_with_saver(req, Some(&saver)).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert!(resp.saved_path.is_some());
    assert_eq!(resp.bytes_written, Some(8));
    // Binary saved without error (unlike normal fetch which rejects binary)
    assert!(resp.error.is_none());

    // Verify binary content on disk
    let saved = std::fs::read(dir.path().join("image.png")).unwrap();
    assert_eq!(saved, binary_data);
}

#[tokio::test]
async fn test_save_to_file_creates_subdirectories() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/file"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("content")
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let saver = LocalFileSaver::new(Some(dir.path().to_path_buf()));
    let tool = test_tool_with_save();

    let req =
        FetchRequest::new(format!("{}/file", mock_server.uri())).save_to_file("sub/dir/file.txt");
    let resp = tool.execute_with_saver(req, Some(&saver)).await.unwrap();

    assert!(resp.saved_path.is_some());
    assert!(dir.path().join("sub/dir/file.txt").exists());
}

#[tokio::test]
async fn test_save_to_file_rejects_path_traversal() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_string("OK"))
        .mount(&mock_server)
        .await;

    let dir = tempfile::tempdir().unwrap();
    let saver = LocalFileSaver::new(Some(dir.path().to_path_buf()));
    let tool = test_tool_with_save();

    let req = FetchRequest::new(format!("{}/", mock_server.uri())).save_to_file("../../etc/passwd");
    let result = tool.execute_with_saver(req, Some(&saver)).await;

    // Path traversal should be rejected before HTTP request
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("save file"));
}

#[tokio::test]
async fn test_save_to_file_without_saver_errors() {
    let tool = test_tool_with_save();

    let req = FetchRequest::new("https://example.com/file").save_to_file("file.txt");
    let result = tool.execute_with_saver(req, None).await;

    assert!(matches!(
        result,
        Err(fetchkit::FetchError::SaverNotAvailable)
    ));
}

#[tokio::test]
async fn test_save_to_file_disabled_by_default() {
    // Default tool does NOT have save_to_file enabled
    let tool = Tool::builder().block_private_ips(false).build();

    let req = FetchRequest::new("https://example.com/file").save_to_file("file.txt");
    let dir = tempfile::tempdir().unwrap();
    let saver = LocalFileSaver::new(Some(dir.path().to_path_buf()));
    let result = tool.execute_with_saver(req, Some(&saver)).await;

    assert!(matches!(
        result,
        Err(fetchkit::FetchError::SaverNotAvailable)
    ));
}

#[tokio::test]
async fn test_save_to_file_schema_gating() {
    // Default: save_to_file not in schema
    let tool = Tool::default();
    let schema = tool.input_schema();
    if let Some(props) = schema.get("properties").and_then(|p| p.as_object()) {
        assert!(!props.contains_key("save_to_file"));
    }

    // Enabled: save_to_file in schema
    let tool = Tool::builder().enable_save_to_file(true).build();
    let schema = tool.input_schema();
    if let Some(props) = schema.get("properties").and_then(|p| p.as_object()) {
        assert!(props.contains_key("save_to_file"));
    }
}

#[tokio::test]
async fn test_execute_with_saver_no_save_falls_through() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("Hello")
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let tool = test_tool_with_save();

    // No save_to_file — should behave like normal execute
    let req = FetchRequest::new(format!("{}/", mock_server.uri()));
    let resp = tool.execute_with_saver(req, None).await.unwrap();

    assert_eq!(resp.status_code, 200);
    assert!(resp.content.unwrap().contains("Hello"));
    assert!(resp.saved_path.is_none());
    assert!(resp.bytes_written.is_none());
}

#[tokio::test]
async fn test_tool_execution_returns_contract_output() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("<h1>Hello</h1>")
                .insert_header("content-type", "text/html"),
        )
        .mount(&mock_server)
        .await;

    let tool = test_tool();
    let output = tool
        .execution(json!({
            "url": format!("{}/", mock_server.uri()),
            "as_markdown": true
        }))
        .unwrap()
        .execute()
        .await
        .unwrap();

    assert_eq!(output.result["status_code"], 200);
    assert!(output.result["format"].is_string());
    assert!(output.result["content"].as_str().unwrap().contains("Hello"));
    assert_eq!(output.metadata.extra["http_status"], 200);
    assert!(output.images.is_empty());
}

#[tokio::test]
async fn test_tool_service_executes_json_calls() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_string("Hello, Service!")
                .insert_header("content-type", "text/plain"),
        )
        .mount(&mock_server)
        .await;

    let mut service = Tool::builder().block_private_ips(false).build_service();
    let result = service
        .call(json!({
            "url": format!("{}/", mock_server.uri())
        }))
        .await
        .unwrap();

    assert_eq!(result["status_code"], 200);
    assert_eq!(result["content"], "Hello, Service!");
}