rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;

use super::*;

/// Three files' patches, the first with a line that names `diff --git `
/// within it, which starts no file.
const PATCH: &str = "\
diff --git a/one b/one
--- a/one
+++ b/one
@@ -1 +1 @@
-see diff --git here
+b
diff --git a/two b/two
new file mode 100644
+x
diff --git a/three b/three
+y
";

/// The files a splitter keeps of `patch` at `limit`, fed `chunk` bytes at
/// a time as git's reads may cut it, and whether it cut the patch.
fn split(patch: &[u8], limit: u64, chunk: usize) -> (Vec<Vec<u8>>, bool) {
    let mut splitter = Splitter::new(limit);
    let mut files = Vec::new();
    for bytes in patch.chunks(chunk) {
        let split = splitter.push(bytes);
        files.extend(split.files);
        if split.cut {
            return (files, true);
        }
    }
    let last = splitter.end();
    files.extend(last.files);
    (files, last.cut)
}

// Covers: the splitter keeps the same whole files, and cuts at the same
// one, whether git's output comes whole or a byte at a time, so a file's
// start split across two reads is still found; a limit exactly a file's
// end keeps that file, and one byte less drops it.
// Owner: daemon host calls.
#[test]
fn a_patch_splits_into_the_same_whole_files_however_its_reads_are_cut() {
    let patch = PATCH.as_bytes();
    let two_at = PATCH.find("diff --git a/two").unwrap();
    let three_at = PATCH.find("diff --git a/three").unwrap();
    let (one, two, three) = (
        &patch[..two_at],
        &patch[two_at..three_at],
        &patch[three_at..],
    );
    let whole = patch.len() as u64;
    let cases: [(u64, &[&[u8]], bool); 6] = [
        (whole, &[one, two, three], false),
        (whole - 1, &[one, two], true),
        ((one.len() + two.len()) as u64, &[one, two], true),
        (one.len() as u64, &[one], true),
        (one.len() as u64 - 1, &[], true),
        (0, &[], true),
    ];

    for (limit, kept, cut) in cases {
        let expected: (Vec<Vec<u8>>, bool) = (kept.iter().map(|file| file.to_vec()).collect(), cut);
        let at_once = split(patch, limit, patch.len());
        assert_eq!(at_once, expected, "whole at {limit}");
        for chunk in [1, 5] {
            assert_eq!(
                split(patch, limit, chunk),
                at_once,
                "{chunk} bytes at a time at {limit}"
            );
        }
    }
}