eggsact 1.2.7

Deterministic MCP and in-process utility tools for coding agents
Documentation
use eggsact::text::{patch_apply_check, patch_summary};

// ─── patch_summary ───────────────────────────────────────────────────

#[test]
fn test_patch_summary_basic() {
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,3 @@\n line1\n-old\n+new\n line3\n";
    let result = patch_summary(patch);
    assert!(result.files_changed >= 1);
}

#[test]
fn test_patch_summary_multiple_files() {
    let patch = "--- a/file1.txt\n+++ b/file1.txt\n@@ -1 +1 @@\n-old\n+new\n--- a/file2.txt\n+++ b/file2.txt\n@@ -1 +1 @@\n-old\n+new\n";
    let result = patch_summary(patch);
    // May detect 1 or 2 files depending on parser
    let _ = result;
}

#[test]
fn test_patch_summary_stats() {
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,3 @@\n line1\n-old1\n-old2\n+new1\n+new2\n+new3\n line3\n";
    let result = patch_summary(patch);
    assert!(result.additions > 0 || result.deletions > 0);
}

#[test]
fn test_patch_summary_empty() {
    let result = patch_summary("");
    assert!(result.files_changed == 0);
}

#[test]
fn test_patch_summary_renames() {
    let patch = "diff --git a/old.txt b/new.txt\nrename from old.txt\nrename to new.txt\n";
    let result = patch_summary(patch);
    let _ = result;
}

// ─── patch_apply_check ───────────────────────────────────────────────

#[test]
fn test_patch_apply_check_clean() {
    let original = "line1\nline2\nline3\n";
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,3 @@\n line1\n-old\n+new\n line3\n";
    let result = patch_apply_check(original, patch, false, false, false);
    assert!(result.applies);
}

#[test]
fn test_patch_apply_check_context_mismatch() {
    let original = "line1\nWRONG\nline3\n";
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,3 @@\n line1\n-old\n+new\n line3\n";
    let result = patch_apply_check(original, patch, true, false, false);
    let _ = result;
}

#[test]
fn test_patch_apply_check_empty_original() {
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -0,0 +1 @@\n+new line\n";
    let result = patch_apply_check("", patch, false, false, false);
    let _ = result;
}

#[test]
fn test_patch_apply_check_empty_patch() {
    let result = patch_apply_check("line1\n", "", false, false, false);
    assert!(result.applies || result.failed_hunks.is_empty());
}

#[test]
fn test_patch_apply_check_reports_lenient_eof_truncation() {
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,2 @@\n a\n-b\n-c\n";
    let result = patch_apply_check("a\nb\n", patch, false, false, false);
    assert!(result
        .findings
        .iter()
        .any(|finding| finding.contains("context truncated at end")));
}

#[test]
fn test_patch_apply_check_result_text() {
    let original = "line1\nold\nline3\n";
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,3 @@\n line1\n-old\n+new\n line3\n";
    let result = patch_apply_check(original, patch, false, false, true);
    let _ = result;
}

#[test]
fn test_patch_apply_check_fingerprint() {
    let original = "line1\nold\nline3\n";
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,3 @@\n line1\n-old\n+new\n line3\n";
    let result = patch_apply_check(original, patch, false, true, false);
    let _ = result;
}

#[test]
fn test_zero_line_destination_ranges_remain_ordered() {
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -2,1 +2,0 @@\n-line2\n";

    let applied = patch_apply_check("line1\nline2\n", patch, true, false, false);
    assert!(applied.applies);
    assert_eq!(applied.affected_line_ranges[0].start, 2);
    assert_eq!(applied.affected_line_ranges[0].end, 2);

    let summary = patch_summary(patch);
    let ranges = summary.line_ranges_by_file.get("file.txt").unwrap();
    assert_eq!(ranges[0].start, 2);
    assert_eq!(ranges[0].end, 2);
}

#[test]
fn nonzero_hunk_replaces_only_its_source_line() {
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -2,1 +2,1 @@\n-b\n+B\n";
    let result = patch_apply_check("a\nb\nc", patch, true, true, true);
    assert!(result.applies);
    assert_eq!(result.result_text.as_deref(), Some("a\nB\nc"));
}

#[test]
fn insertion_and_deletion_after_nonzero_prefix_preserve_prefix_and_suffix() {
    let insert = "--- a/file.txt\n+++ b/file.txt\n@@ -1,2 +1,3 @@\n a\n+X\n b\n";
    let inserted = patch_apply_check("a\nb\nc", insert, true, false, true);
    assert_eq!(inserted.result_text.as_deref(), Some("a\nX\nb\nc"));

    let delete = "--- a/file.txt\n+++ b/file.txt\n@@ -2,1 +2,0 @@\n-b\n";
    let deleted = patch_apply_check("a\nb\nc", delete, true, false, true);
    assert_eq!(deleted.result_text.as_deref(), Some("a\nc"));
}

#[test]
fn separated_hunks_use_original_line_numbers_after_line_count_change() {
    let patch =
        "--- a/file.txt\n+++ b/file.txt\n@@ -2,1 +2,2 @@\n b\n+X\n@@ -4,1 +5,1 @@\n-d\n+D\n";
    let result = patch_apply_check("a\nb\nc\nd\ne", patch, true, true, true);
    assert!(result.applies);
    assert_eq!(result.result_text.as_deref(), Some("a\nb\nX\nc\nD\ne"));
}

#[test]
fn crlf_output_and_fingerprint_follow_the_applied_result() {
    let patch = "--- a/file.txt\r\n+++ b/file.txt\r\n@@ -2,1 +2,1 @@\r\n-b\r\n+B\r\n";
    let result = patch_apply_check("a\r\nb\r\nc\r\n", patch, true, true, true);
    assert!(result.applies);
    assert_eq!(result.newline_style_before, "CRLF");
    assert_eq!(result.newline_style_after, "CRLF");
    assert_eq!(result.result_text.as_deref(), Some("a\r\nB\r\nc"));
    assert!(!result.result_fingerprint.is_empty());
}

#[test]
fn fingerprint_is_computed_from_applied_text_without_returning_text() {
    let original = "a\nb\nc";
    let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -2,1 +2,1 @@\n-b\n+B\n";
    let without_text = patch_apply_check(original, patch, true, true, false);
    let with_text = patch_apply_check(original, patch, true, true, true);

    assert!(without_text.applies);
    assert!(without_text.result_text.is_none());
    assert_eq!(
        without_text.result_fingerprint,
        with_text.result_fingerprint
    );
    assert_ne!(without_text.result_fingerprint, "");
}