pathfinder-mcp 0.1.0

Pathfinder — The Headless IDE MCP Server for AI Coding Agents
Documentation
#![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.clone();
    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.clone();
    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.clone();
    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.clone();
    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.clone();
    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.clone();
    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,
        closest_match: _,
    } = 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"
    );
}