pathfinder-mcp 0.20.1

Pathfinder — The Headless IDE MCP Server for AI Coding Agents
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
use super::*;
use pathfinder_common::config::PathfinderConfig;
use pathfinder_common::sandbox::Sandbox;
use pathfinder_common::types::WorkspaceRoot;
use pathfinder_search::MockScout;
use pathfinder_treesitter::mock::MockSurgeon;
use std::fs;
use std::sync::Arc;
use tempfile::tempdir;

#[tokio::test]
async fn test_read_files_happy_path() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let mock_surgeon = Arc::new(MockSurgeon::new());
    let content1 = "fn main() {}\nfn test() {}";
    let content2 = "const x = 1;\nconst y = 2;";
    let content3 = "def foo():\n    pass";

    fs::write(ws_dir.path().join("file1.rs"), content1).expect("write");
    fs::write(ws_dir.path().join("file2.ts"), content2).expect("write");
    fs::write(ws_dir.path().join("file3.py"), content3).expect("write");

    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((content1.to_string(), "rust".to_string(), vec![])));
    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((content2.to_string(), "typescript".to_string(), vec![])));
    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((content3.to_string(), "python".to_string(), vec![])));

    let server = PathfinderServer::with_all_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        mock_surgeon,
        Arc::new(pathfinder_lsp::NoOpLawyer),
    );

    let params = ReadParams {
        paths: Some(vec![
            "file1.rs".to_string(),
            "file2.ts".to_string(),
            "file3.py".to_string(),
        ]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.files.len(), 3);
    assert_eq!(response.succeeded, 3);
    assert_eq!(response.failed, 0);

    for file in &response.files {
        assert!(file.content.is_some());
        assert!(file.language.is_some());
        assert!(file.total_lines.is_some());
        assert!(file.version_hash.is_some());
        assert!(file.error.is_none());
    }
}

#[tokio::test]
async fn test_read_files_partial_failure() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    fs::write(ws_dir.path().join("valid1.txt"), "content1").expect("write");
    fs::write(ws_dir.path().join("valid2.txt"), "content2").expect("write");

    let params = ReadParams {
        paths: Some(vec![
            "valid1.txt".to_string(),
            "valid2.txt".to_string(),
            "missing.txt".to_string(),
        ]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.files.len(), 3);
    assert_eq!(response.succeeded, 2);
    assert_eq!(response.failed, 1);

    assert!(response.files[0].content.is_some());
    assert!(response.files[0].error.is_none());
    assert!(response.files[1].content.is_some());
    assert!(response.files[1].error.is_none());

    assert!(response.files[2].content.is_none());
    assert!(response.files[2].error.is_some());
    assert_eq!(response.files[2].error.as_ref().unwrap(), "file not found");
}

#[tokio::test]
async fn test_read_files_sandbox_denial() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    let params = ReadParams {
        paths: Some(vec![".git/HEAD".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.files.len(), 1);
    assert_eq!(response.succeeded, 0);
    assert_eq!(response.failed, 1);
    assert!(response.files[0].content.is_none());
    assert!(response.files[0].error.is_some());
    assert!(response.files[0]
        .error
        .as_ref()
        .unwrap()
        .contains("ACCESS_DENIED"));
}

#[tokio::test]
async fn test_read_files_max_limit() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    let params = ReadParams {
        paths: Some((0..11).map(|i| format!("file{i}.txt")).collect()),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server.read_files_impl(params).await;
    assert!(result.is_err(), "should fail with >10 paths");
    let err = result.unwrap_err();
    assert_eq!(err.code, rmcp::model::ErrorCode::INVALID_PARAMS);
}

#[tokio::test]
async fn test_read_files_empty_paths() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    let params = ReadParams {
        paths: Some(vec![]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server.read_files_impl(params).await;

    // Empty paths should error (1-10 paths required by spec)
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert_eq!(err.code, rmcp::model::ErrorCode::INVALID_PARAMS);
}

#[tokio::test]
async fn test_read_files_mixed_source_and_config() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let mock_surgeon = Arc::new(MockSurgeon::new());
    let rust_content = "fn main() {}";
    fs::write(ws_dir.path().join("main.rs"), rust_content).expect("write");
    fs::write(
        ws_dir.path().join("config.toml"),
        "[settings]\nkey = \"value\"",
    )
    .expect("write");

    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((rust_content.to_string(), "rust".to_string(), vec![])));

    let server = PathfinderServer::with_all_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        mock_surgeon,
        Arc::new(pathfinder_lsp::NoOpLawyer),
    );

    let params = ReadParams {
        paths: Some(vec!["main.rs".to_string(), "config.toml".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.files.len(), 2);
    assert_eq!(response.succeeded, 2);

    let source_file = response.files.iter().find(|f| f.path == "main.rs").unwrap();
    assert_eq!(source_file.language.as_deref(), Some("rust"));

    let config_file = response
        .files
        .iter()
        .find(|f| f.path == "config.toml")
        .unwrap();
    assert_eq!(config_file.language.as_deref(), Some("toml"));
}

#[tokio::test]
async fn test_read_files_version_hash_format() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let mock_surgeon = Arc::new(MockSurgeon::new());
    let content = "fn main() {}\n";
    fs::write(ws_dir.path().join("test.rs"), content).expect("write");
    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((content.to_string(), "rust".to_string(), vec![])));

    let server = PathfinderServer::with_all_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        mock_surgeon,
        Arc::new(pathfinder_lsp::NoOpLawyer),
    );

    let params1 = ReadParams {
        paths: Some(vec!["test.rs".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };
    let result1 = server
        .read_files_impl(params1)
        .await
        .expect("should succeed");
    let response1: ReadFilesResponse =
        serde_json::from_value(result1.structured_content.unwrap()).unwrap();
    let hash1 = response1.files[0].version_hash.as_ref().unwrap().clone();

    // read tool now emits short() — 7-char hex, consistent with explore's version_hashes.
    assert_eq!(hash1.len(), 7, "hash should be 7 hex chars (short format)");
    assert!(
        hash1.chars().all(|c| c.is_ascii_hexdigit()),
        "hash should be lowercase hex only, got: {hash1}"
    );
    assert!(
        !hash1.starts_with("sha256:"),
        "read tool must not emit sha256: prefix; that would diverge from explore"
    );

    let binding = pathfinder_common::types::VersionHash::compute(content.as_bytes());
    let expected_hash = binding.short();
    assert_eq!(
        hash1, expected_hash,
        "hash should match VersionHash::short()"
    );
}

#[tokio::test]
async fn test_read_files_max_lines_per_file_truncation() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    let lines: Vec<String> = (1..=10).map(|i| format!("line{i}")).collect();
    let content = lines.join("\n");
    fs::write(ws_dir.path().join("test.txt"), &content).expect("write");

    let params = ReadParams {
        paths: Some(vec!["test.txt".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 3,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.succeeded, 1);
    let file_result = &response.files[0];

    let content_lines: Vec<&str> = file_result.content.as_ref().unwrap().lines().collect();
    assert_eq!(content_lines.len(), 3);
    assert_eq!(file_result.total_lines.unwrap(), 3);
    assert_eq!(content_lines[0], "line1");
    assert_eq!(content_lines[2], "line3");
}

#[test]
fn test_is_source_file() {
    assert!(is_source_file(Path::new("test.rs")));
    assert!(is_source_file(Path::new("test.ts")));
    assert!(is_source_file(Path::new("test.tsx")));
    assert!(is_source_file(Path::new("test.go")));
    assert!(is_source_file(Path::new("test.py")));
    assert!(is_source_file(Path::new("test.pyi")));
    assert!(is_source_file(Path::new("test.vue")));
    assert!(is_source_file(Path::new("test.jsx")));
    assert!(is_source_file(Path::new("test.js")));
    assert!(is_source_file(Path::new("test.mjs")));
    assert!(is_source_file(Path::new("test.cjs")));
    assert!(is_source_file(Path::new("test.java")));

    assert!(!is_source_file(Path::new("test.json")));
    assert!(!is_source_file(Path::new("test.toml")));
    assert!(!is_source_file(Path::new("test.yaml")));
    assert!(!is_source_file(Path::new("test.yml")));
    assert!(!is_source_file(Path::new("test.txt")));
    assert!(!is_source_file(Path::new("test.md")));
    assert!(!is_source_file(Path::new("Dockerfile")));
    assert!(!is_source_file(Path::new("Makefile")));
    assert!(!is_source_file(Path::new(".gitignore")));
}

#[test]
fn test_truncate_content() {
    let content = "line1\nline2\nline3\nline4\nline5";
    assert_eq!(truncate_content(content, 3), "line1\nline2\nline3");
    assert_eq!(truncate_content(content, 10), content);
    assert_eq!(truncate_content("", 5), "");
}

#[tokio::test]
async fn test_read_files_empty_file() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let mock_surgeon = Arc::new(MockSurgeon::new());
    let content = "";
    fs::write(ws_dir.path().join("empty.rs"), content).expect("write");
    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((content.to_string(), "rust".to_string(), vec![])));

    let server = PathfinderServer::with_all_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        mock_surgeon,
        Arc::new(pathfinder_lsp::NoOpLawyer),
    );

    let params = ReadParams {
        paths: Some(vec!["empty.rs".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.succeeded, 1);
    let file_result = &response.files[0];
    assert_eq!(file_result.content, Some(String::new()));
    assert_eq!(file_result.total_lines, Some(0));
    assert!(file_result.version_hash.is_some());
}

#[tokio::test]
async fn test_read_files_binary_file() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    fs::write(
        ws_dir.path().join("binary.bin"),
        b"\x00\x01\x02\x03\xFF\xFE",
    )
    .expect("write");

    let params = ReadParams {
        paths: Some(vec!["binary.bin".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.succeeded, 0);
    assert_eq!(response.failed, 1);
    assert!(response.files[0].content.is_none());
    assert!(response.files[0].error.is_some());
    assert!(response.files[0].error.as_ref().unwrap().contains("binary"));
}

#[tokio::test]
async fn test_read_files_duplicate_paths() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let mock_surgeon = Arc::new(MockSurgeon::new());
    let content = "fn main() {}";
    fs::write(ws_dir.path().join("test.rs"), content).expect("write");
    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((content.to_string(), "rust".to_string(), vec![])));
    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((content.to_string(), "rust".to_string(), vec![])));

    let server = PathfinderServer::with_all_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        mock_surgeon,
        Arc::new(pathfinder_lsp::NoOpLawyer),
    );

    let params = ReadParams {
        paths: Some(vec!["test.rs".to_string(), "test.rs".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.files.len(), 2);
    assert_eq!(response.succeeded, 2);

    assert_eq!(response.files[0].content, response.files[1].content);
    assert_eq!(
        response.files[0].version_hash,
        response.files[1].version_hash
    );
}

#[tokio::test]
async fn test_read_files_mjs_cjs_extensions() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let mock_surgeon = Arc::new(MockSurgeon::new());
    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((
            "export default {}".to_string(),
            "javascript".to_string(),
            vec![],
        )));
    mock_surgeon
        .read_source_file_results
        .lock()
        .unwrap()
        .push(Ok((
            "module.exports = {}".to_string(),
            "javascript".to_string(),
            vec![],
        )));

    let server = PathfinderServer::with_all_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        mock_surgeon,
        Arc::new(pathfinder_lsp::NoOpLawyer),
    );

    fs::write(ws_dir.path().join("test.mjs"), "export default {}").expect("write");
    fs::write(ws_dir.path().join("test.cjs"), "module.exports = {}").expect("write");

    let params = ReadParams {
        paths: Some(vec!["test.mjs".to_string(), "test.cjs".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.succeeded, 2);
    assert!(response.files[0].language.is_some());
    assert!(response.files[1].language.is_some());
}

#[tokio::test]
async fn test_read_files_binary_file_png_header() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    // PNG header bytes
    let png_header = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01";
    fs::write(ws_dir.path().join("image.png"), png_header).expect("write");

    let params = ReadParams {
        paths: Some(vec!["image.png".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.succeeded, 0);
    assert_eq!(response.failed, 1);
    assert!(response.files[0].content.is_none());
    assert!(response.files[0].error.is_some());
    assert!(response.files[0].error.as_ref().unwrap().contains("binary"));
}

#[tokio::test]
async fn test_read_files_version_hash_non_source_file() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    let content = "[settings]\nkey = \"value\"";
    fs::write(ws_dir.path().join("config.toml"), content).expect("write");

    let params = ReadParams {
        paths: Some(vec!["config.toml".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.succeeded, 1);
    let file_result = &response.files[0];
    assert!(file_result.version_hash.is_some());
    let hash = file_result.version_hash.as_ref().unwrap();
    // short format: 7-char hex, no sha256: prefix
    assert_eq!(hash.len(), 7, "hash must be 7-char short format");
    assert!(!hash.starts_with("sha256:"), "must not have sha256: prefix");
    let expected = pathfinder_common::types::VersionHash::compute(content.as_bytes());
    assert_eq!(hash, expected.short());
}

#[tokio::test]
async fn test_read_files_truncation_exact_boundary() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    // Create content with exactly 5 lines
    let content = "line1\nline2\nline3\nline4\nline5";
    fs::write(ws_dir.path().join("exact.txt"), content).expect("write");

    let params = ReadParams {
        paths: Some(vec!["exact.txt".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 5,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");
    let response: ReadFilesResponse =
        serde_json::from_value(result.structured_content.unwrap()).unwrap();

    assert_eq!(response.succeeded, 1);
    let file_result = &response.files[0];
    // Should NOT be truncated since content has exactly max_lines lines
    assert_eq!(file_result.content.as_deref(), Some(content));
    assert_eq!(file_result.total_lines, Some(5));
}

#[tokio::test]
async fn test_read_files_text_content_includes_file_data() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    fs::write(ws_dir.path().join("hello.txt"), "hello world").expect("write");
    fs::write(
        ws_dir.path().join("second.toml"),
        "[settings]\nkey = \"val\"",
    )
    .expect("write");

    let params = ReadParams {
        paths: Some(vec!["hello.txt".to_string(), "second.toml".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");

    let text = match result.content.first() {
        Some(rmcp::model::Content {
            raw: rmcp::model::RawContent::Text(t),
            ..
        }) => t.text.clone(),
        _ => panic!("expected text content"),
    };

    assert!(
        text.contains("hello.txt"),
        "text output must include file path, got: {text}"
    );
    assert!(
        text.contains("hello world"),
        "text output must include file content, got: {text}"
    );
    assert!(
        text.contains("second.toml"),
        "text output must include second file path, got: {text}"
    );
    assert!(
        text.contains("key = \"val\""),
        "text output must include second file content, got: {text}"
    );

    assert!(
        !text.starts_with("[completed in"),
        "text output must NOT be just the timing footer, got: {text}"
    );
}

#[tokio::test]
async fn test_read_files_text_content_shows_errors_for_missing() {
    let ws_dir = tempdir().expect("temp dir");
    let ws = WorkspaceRoot::new(ws_dir.path()).expect("valid root");
    let config = PathfinderConfig::default();
    let sandbox = Sandbox::new(ws.path(), &config.sandbox);

    let server = PathfinderServer::with_engines(
        ws,
        config,
        sandbox,
        Arc::new(MockScout::default()),
        Arc::new(MockSurgeon::new()),
    );

    fs::write(ws_dir.path().join("exists.txt"), "content").expect("write");

    let params = ReadParams {
        paths: Some(vec!["exists.txt".to_string(), "missing.txt".to_string()]),
        detail_level: "source_only".to_string(),
        max_lines_per_file: 500,
        ..Default::default()
    };

    let result = server
        .read_files_impl(params)
        .await
        .expect("should succeed");

    let text = match result.content.first() {
        Some(rmcp::model::Content {
            raw: rmcp::model::RawContent::Text(t),
            ..
        }) => t.text.clone(),
        _ => panic!("expected text content"),
    };

    assert!(
        text.contains("exists.txt"),
        "text must include successful file path"
    );
    assert!(
        text.contains("missing.txt"),
        "text must include failed file path"
    );
    assert!(
        text.contains("error") || text.contains("failed") || text.contains("not found"),
        "text must indicate the error for missing file, got: {text}"
    );
}