pathfinder-mcp 0.6.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
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::needless_return)]
use crate::server::PathfinderServer;

use std::path::Path;

use super::super::text_edit::*;
use super::super::*;
#[test]
fn test_apply_sorted_edits_overlap() {
    // Edit 1: bytes 0..5, Edit 2: bytes 2..7 (overlap)
    let edits = vec![
        (
            0,
            "edit0".to_string(),
            ResolvedEdit {
                start_byte: 0,
                end_byte: 5,
                replacement: vec![],
            },
        ),
        (
            1,
            "edit1".to_string(),
            ResolvedEdit {
                start_byte: 2,
                end_byte: 7,
                replacement: vec![],
            },
        ),
    ];

    let result = PathfinderServer::apply_sorted_edits(b"0123456789", edits);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert_eq!(err.code.0, -32602); // INVALID_PARAMS mapped from InvalidTarget
    let data = err.data.expect("should have data");
    assert_eq!(data["details"]["edit_index"], 0);
    assert_eq!(data["details"]["valid_edit_types"], serde_json::Value::Null);
    // not applicable here
}

fn src(lines: &[&str]) -> Vec<u8> {
    lines.join("\n").into_bytes()
}

// ── success cases ───────────────────────────────────────────────────

#[test]
fn test_exact_match_replaces_correctly() {
    let source = src(&[
        "line 1",
        "line 2",
        "line 3",
        "line 4",
        "<button>Click me</button>",
        "line 6",
        "line 7",
    ]);
    let r = resolve_text_edit(
        &source,
        "<button>Click me</button>",
        5,
        "<button>Submit</button>",
        false,
        Path::new("app.vue"),
    )
    .expect("exact match should succeed");

    let mut out = source;
    out.splice(r.start_byte..r.end_byte, r.replacement);
    let out_str = String::from_utf8(out).unwrap();
    assert!(
        out_str.contains("<button>Submit</button>"),
        "replacement present: {out_str}"
    );
    assert!(
        !out_str.contains("<button>Click me</button>"),
        "old text gone: {out_str}"
    );
}

#[test]
fn test_window_boundary_at_plus_25_is_included() {
    // Target on line 35, context_line 10 → window = [1, 35] (±25 from line 10).
    let mut lines = vec!["filler"; 40];
    lines[34] = "special text"; // line 35 (1-indexed)
    let source = src(&lines);
    resolve_text_edit(
        &source,
        "special text",
        10,
        "replaced",
        false,
        Path::new("a.rs"),
    )
    .expect("±25 edge should be included");
}

#[test]
fn test_context_line_zero_clamped_safely() {
    let source = src(&["only line"]);
    let r = resolve_text_edit(
        &source,
        "only line",
        0,
        "replaced",
        false,
        Path::new("a.rs"),
    )
    .expect("context_line=0 clamped to center=0 → window ok");
    assert_eq!(&r.replacement, b"replaced");
}

#[test]
fn test_multiline_old_text() {
    let source = src(&[
        "fn foo() {",
        "    let x = 1;",
        "    let y = 2;",
        "}",
        "fn bar() {}",
    ]);
    let old = "    let x = 1;\n    let y = 2;";
    let r = resolve_text_edit(
        &source,
        old,
        2,
        "    let z = 42;",
        false,
        Path::new("lib.rs"),
    )
    .expect("multi-line match should succeed");
    let mut out = source;
    out.splice(r.start_byte..r.end_byte, r.replacement);
    let s = String::from_utf8(out).unwrap();
    assert!(s.contains("let z = 42;"), "replacement present: {s}");
    assert!(!s.contains("let x = 1;"), "old text removed: {s}");
}

// ── normalize_whitespace ────────────────────────────────────────────

#[test]
fn test_normalize_whitespace_matches_with_collapsed_spaces() {
    let source = src(&[
        "<div>",
        "  <button   class=\"btn\"   >Click</button>",
        "</div>",
    ]);
    let r = resolve_text_edit(
        &source,
        "<button class=\"btn\" >Click</button>",
        2,
        "<button class=\"btn\">Submit</button>",
        true,
        Path::new("comp.vue"),
    )
    .expect("normalized whitespace should match");
    let mut out = source;
    out.splice(r.start_byte..r.end_byte, r.replacement);
    let s = String::from_utf8(out).unwrap();
    assert!(s.contains("Submit"), "replacement present: {s}");
}

#[test]
fn test_no_normalize_fails_on_spacing_mismatch() {
    // With fuzzy fallback, spacing mismatches are now handled automatically
    let source = src(&["<button   class=\"btn\">Click</button>"]);
    let r = resolve_text_edit(
        &source,
        "<button class=\"btn\">Click</button>",
        1,
        "<button>Submit</button>",
        false,
        Path::new("a.vue"),
    )
    .expect("fuzzy fallback should handle spacing mismatch");

    let mut out = source;
    out.splice(r.start_byte..r.end_byte, r.replacement);
    let s = String::from_utf8(out).unwrap();
    assert!(s.contains("Submit"), "replacement present");
    assert!(!s.contains("Click"), "old text removed");
}

// ── failure cases ───────────────────────────────────────────────────

#[test]
fn test_text_not_in_window_returns_text_not_found() {
    let mut lines = vec!["line"; 60];
    lines[50] = "target text"; // line 51
    let source = src(&lines);
    // Window for context_line=5 covers lines 1–30 (±25); line 51 is outside.
    let err = resolve_text_edit(&source, "target text", 5, "r", false, Path::new("a.rs"))
        .expect_err("out-of-window match should fail");
    let pathfinder_common::error::PathfinderError::TextNotFound { context_line, .. } = err else {
        panic!("expected TextNotFound, got: {err:?}");
    };
    assert_eq!(context_line, 5);
}

#[test]
fn test_text_not_found_at_all_returns_error() {
    let source = src(&["hello world"]);
    let err = resolve_text_edit(&source, "not present", 1, "", false, Path::new("f.rs"))
        .expect_err("missing text returns TextNotFound");
    assert!(matches!(
        err,
        pathfinder_common::error::PathfinderError::TextNotFound { .. }
    ));
}

// ── Fix 3: Text Edit Improvements ─────────────────────────────────────

#[test]
fn test_window_25_lines() {
    // Test that the search window is ±25 lines (not ±10)
    let source = src(&[
        "line 1", "line 2", "line 3", "line 4", "line 5", "line 6", "line 7", "line 8", "line 9",
        "line 10", "line 11", "line 12", "line 13", "line 14", "line 15", "line 16", "line 17",
        "line 18", "line 19", "line 20", "line 21", "line 22", "line 23", "line 24", "line 25",
        "line 26", "line 27", "line 28", "line 29", "line 30",
    ]);

    // context_line=15, target text at line 30 (15+15=30, within ±25 window)
    let r = resolve_text_edit(
        &source,
        "line 30",
        15,
        "replaced",
        false,
        Path::new("test.rs"),
    )
    .expect("match at line 30 (±25 from line 15) should succeed");

    let mut out = source;
    out.splice(r.start_byte..r.end_byte, r.replacement);
    let s = String::from_utf8(out).unwrap();
    assert!(s.contains("replaced"), "replacement present");
    assert!(!s.contains("line 30"), "old text removed");
}

#[test]
fn test_fuzzy_whitespace_fallback() {
    // Test that whitespace-normalized fuzzy matching is attempted when exact match fails
    let source = src(&[
        "<div>",
        "  <button   class=\"btn\">Click</button>",
        "</div>",
    ]);

    // Exact match should fail (wrong spacing), but fuzzy fallback should succeed
    let r = resolve_text_edit(
        &source,
        "<button class=\"btn\">Click</button>",
        2,
        "<button>Submit</button>",
        false,
        Path::new("comp.vue"),
    )
    .expect("fuzzy whitespace fallback should succeed");

    let mut out = source;
    out.splice(r.start_byte..r.end_byte, r.replacement);
    let s = String::from_utf8(out).unwrap();
    assert!(s.contains("Submit"), "replacement present");
    assert!(!s.contains("Click"), "old text removed");
}

#[test]
fn test_text_not_found_includes_actual_content() {
    let source = src(&["line 1", "line 2", "line 3"]);

    let err = resolve_text_edit(&source, "not present", 2, "", false, Path::new("f.rs"))
        .expect_err("missing text returns TextNotFound");

    let pathfinder_common::error::PathfinderError::TextNotFound {
        filepath,
        old_text,
        context_line,
        actual_content,
        ..
    } = err
    else {
        panic!("expected TextNotFound, got: {err:?}");
    };

    assert_eq!(filepath, Path::new("f.rs"));
    assert_eq!(old_text, "not present");
    assert_eq!(context_line, 2);
    assert!(
        actual_content.is_some(),
        "actual_content should be populated with window text"
    );

    let content = actual_content.unwrap();
    assert!(
        content.contains("line 1") || content.contains("line 2") || content.contains("line 3"),
        "actual_content should contain context from the source file"
    );
}

// ── is_whitespace_significant_file (L353-357) ────────────────────────────

#[test]
fn test_is_whitespace_significant_file_known_extensions() {
    assert!(is_whitespace_significant_file(Path::new("main.py")));
    assert!(is_whitespace_significant_file(Path::new("config.yaml")));
    assert!(is_whitespace_significant_file(Path::new("config.yml")));
    assert!(is_whitespace_significant_file(Path::new("Cargo.toml")));
}

#[test]
fn test_is_whitespace_significant_file_non_significant_extensions() {
    assert!(!is_whitespace_significant_file(Path::new("src/main.rs")));
    assert!(!is_whitespace_significant_file(Path::new("comp.vue")));
    assert!(!is_whitespace_significant_file(Path::new("app.ts")));
    assert!(!is_whitespace_significant_file(Path::new("index.go")));
}

#[test]
fn test_is_whitespace_significant_file_no_extension() {
    assert!(!is_whitespace_significant_file(Path::new("Makefile")));
    assert!(!is_whitespace_significant_file(Path::new("Dockerfile")));
}

// ── normalize_blank_lines (L332-351) ─────────────────────────────────────

#[test]
fn test_normalize_blank_lines_collapses_triple_blank() {
    // Three consecutive newlines → must collapse to two (one blank line).
    let input = b"line1\n\n\n\nline2\n";
    let result = normalize_blank_lines(input);
    let s = String::from_utf8(result).unwrap();
    assert_eq!(
        s, "line1\n\nline2\n",
        "three consecutive \\n must collapse to two"
    );
}

#[test]
fn test_normalize_blank_lines_preserves_single_blank() {
    let input = b"line1\n\nline2\n";
    let result = normalize_blank_lines(input);
    assert_eq!(
        result, input,
        "a single blank line must be preserved unchanged"
    );
}

#[test]
fn test_normalize_blank_lines_no_blanks_unchanged() {
    let input = b"line1\nline2\nline3";
    let result = normalize_blank_lines(input);
    assert_eq!(result, input, "no blank lines must be preserved unchanged");
}

#[test]
fn test_normalize_blank_lines_empty_input() {
    let result = normalize_blank_lines(b"");
    assert!(result.is_empty(), "empty input must produce empty output");
}

// ── strip_orphaned_doc_comment (L359-395) ────────────────────────────────

#[test]
fn test_strip_orphaned_doc_comment_zero_before_end() {
    // before_end == 0: early return must be exercised.
    let result = strip_orphaned_doc_comment(b"anything", 0);
    assert_eq!(result, 0, "before_end=0 must return 0 unchanged");
}

#[test]
fn test_strip_orphaned_doc_comment_no_doc_comment() {
    // The last line before the cut is NOT a doc comment → before_end unchanged.
    let src = b"fn hello() {}\nfn world() {}\n";
    let before_end = src.len();
    let result = strip_orphaned_doc_comment(src, before_end);
    assert_eq!(
        result, before_end,
        "no doc comment → before_end must be unchanged"
    );
}

#[test]
fn test_strip_orphaned_doc_comment_strips_trailing_doc_comment() {
    // The line immediately before the cut point is `/// orphaned`
    // → the function should step back to exclude it.
    let src = b"fn foo() {}\n/// orphaned doc\n";
    let before_end = src.len();
    let result = strip_orphaned_doc_comment(src, before_end);
    // Result must be strictly less than before_end (comment stripped)
    assert!(
        result < before_end,
        "orphaned /// comment must be stripped: result={result} before_end={before_end}"
    );
    // Stripped position must still include fn foo() {}
    let kept = std::str::from_utf8(&src[..result]).unwrap();
    assert!(kept.contains("fn foo()"), "fn foo must still be present");
    assert!(!kept.contains("orphaned"), "orphaned doc must be removed");
}

#[test]
fn test_strip_orphaned_doc_comment_strips_inner_doc_comment() {
    // `//!` inner doc is also an orphaned comment and must be stripped.
    let src = b"mod foo {}\n//! inner doc\n";
    let before_end = src.len();
    let result = strip_orphaned_doc_comment(src, before_end);
    assert!(result < before_end, "orphaned //! comment must be stripped");
}

// ── whitespace-significant TextNotFound path (L183-190) ─────────────────

#[test]
fn test_text_not_found_in_python_file_no_fuzzy_fallback() {
    // Python files are whitespace-significant: the fuzzy fallback must NOT
    // be attempted when the exact match fails.  The error must be
    // TextNotFound with a `closest_match` field (not a recursive retry).
    let source = src(&["def hello():", "    pass"]);
    let err = resolve_text_edit(
        &source,
        "def hello( ):", // wrong spacing — different from source
        1,
        "def goodbye():",
        false,
        Path::new("app.py"),
    )
    .expect_err("spacing-mismatch in .py must fail without fuzzy fallback");
    assert!(
        matches!(
            err,
            pathfinder_common::error::PathfinderError::TextNotFound { .. }
        ),
        "expected TextNotFound, got: {err:?}"
    );
}