use std::path::Path;
use serde_json::Value;
pub fn class_of(path: &str) -> &'static str {
let p = Path::new(path);
let base = p.file_name().and_then(|b| b.to_str()).unwrap_or("");
let norm = path.replace('\\', "/");
let segs: Vec<&str> = norm.split('/').collect();
let dir_segs = &segs[..segs.len().saturating_sub(1)];
const TEST_CONFIG: [&str; 7] = [
"conftest.py",
"pytest.ini",
"tox.ini",
"noxfile.py",
".rspec",
"phpunit.xml",
"phpunit.xml.dist",
];
const TEST_CONFIG_PREFIX: [&str; 6] = [
"jest.config.",
"vitest.config.",
"karma.conf.",
"playwright.config.",
"cypress.config.",
".mocharc",
];
if TEST_CONFIG.contains(&base)
|| TEST_CONFIG_PREFIX.iter().any(|x| base.starts_with(x))
|| norm.ends_with(".cargo/config")
|| norm.ends_with(".cargo/config.toml")
{
return "test-config";
}
const BUILD: [&str; 13] = [
"package.json",
"pyproject.toml",
"setup.cfg",
"setup.py",
"Cargo.toml",
"Makefile",
"makefile",
"justfile",
"go.mod",
"pom.xml",
"build.gradle",
"build.gradle.kts",
"deno.json",
];
if BUILD.contains(&base) || base.starts_with("vite.config.") {
return "build";
}
const TEST_DIRS: [&str; 5] = ["tests", "test", "__tests__", "spec", "specs"];
const JS: [&str; 8] = ["js", "jsx", "ts", "tsx", "mjs", "cjs", "mts", "cts"];
let js_test = || {
let mut parts = base.rsplitn(3, '.');
let ext = parts.next().unwrap_or("");
let mid = parts.next().unwrap_or("");
parts.next().is_some() && JS.contains(&ext) && matches!(mid, "test" | "spec")
};
if dir_segs.iter().any(|s| TEST_DIRS.contains(s))
|| (base.starts_with("test_") && base.ends_with(".py"))
|| base.ends_with("_test.py")
|| base.ends_with("_test.go")
|| base.ends_with("_spec.rb")
|| ["Test.java", "Tests.java", "Test.kt", "Tests.cs"]
.iter()
.any(|s| base.ends_with(s) && base.len() > s.len())
|| js_test()
{
return "test";
}
let ext = p.extension().and_then(|e| e.to_str()).unwrap_or("");
if matches!(ext, "md" | "rst" | "txt" | "adoc")
|| dir_segs.iter().any(|s| matches!(*s, "docs" | "doc"))
{
return "doc";
}
"code"
}
fn is_word(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
type Alt = fn(&[char], usize) -> Option<usize>;
fn lit(s: &[char], i: usize, w: &str) -> Option<usize> {
let mut n = 0;
for c in w.chars() {
if s.get(i + n) != Some(&c) {
return None;
}
n += 1;
}
Some(n)
}
fn starts_word(s: &[char], i: usize) -> bool {
i == 0 || !is_word(s[i - 1])
}
fn ends_word(s: &[char], i: usize) -> bool {
s.get(i).is_none_or(|c| !is_word(*c))
}
fn skip_while(s: &[char], mut i: usize, f: fn(char) -> bool) -> usize {
while s.get(i).is_some_and(|c| f(*c)) {
i += 1;
}
i
}
fn word_then_paren(s: &[char], i: usize, w: &str, boundary: bool) -> Option<usize> {
if boundary && !starts_word(s, i) {
return None;
}
let mut j = i + lit(s, i, w)?;
j = skip_while(s, j, is_word);
j = skip_while(s, j, char::is_whitespace);
(s.get(j) == Some(&'(')).then_some(j + 1 - i)
}
fn tight_paren(s: &[char], i: usize, w: &str) -> Option<usize> {
let mut j = i + lit(s, i, w)?;
j = skip_while(s, j, char::is_whitespace);
(s.get(j) == Some(&'(')).then_some(j + 1 - i)
}
const TESTS: [Alt; 8] = [
|s, i| lit(s, i, "#[test]"),
|s, i| lit(s, i, "#[tokio::test"),
|s, i| word_then_paren(s, i, "def test", true),
|s, i| word_then_paren(s, i, "func Test", true),
|s, i| {
if !starts_word(s, i) {
return None;
}
let n = lit(s, i, "it").or_else(|| lit(s, i, "test"))?;
let mut j = skip_while(s, i + n, char::is_whitespace);
if s.get(j) != Some(&'(') {
return None;
}
j = skip_while(s, j + 1, char::is_whitespace);
matches!(s.get(j), Some('\'' | '"' | '`')).then_some(j + 1 - i)
},
|s, i| lit(s, i, "@Test").filter(|n| ends_word(s, i + n)),
|s, i| lit(s, i, "[Fact]"),
|s, i| lit(s, i, "[TestMethod]"),
];
const ASSERTS: [Alt; 4] = [
|s, i| {
if !starts_word(s, i) {
return None;
}
let mut j = skip_while(s, i + lit(s, i, "assert")?, is_word);
if s.get(j) == Some(&'!') {
j += 1;
}
Some(j - i)
},
|s, i| {
starts_word(s, i)
.then(|| tight_paren(s, i, "expect"))
.flatten()
},
|s, i| {
if !starts_word(s, i) {
return None;
}
let j = i + lit(s, i, "t.")?;
let k = j + lit(s, j, "Error")
.or_else(|| lit(s, j, "Fatal"))
.or_else(|| lit(s, j, "Fail"))?;
let k = skip_while(s, k, is_word);
(s.get(k) == Some(&'(')).then_some(k + 1 - i)
},
|s, i| {
let n = lit(s, i, "should")?;
(starts_word(s, i) && ends_word(s, i + n)).then_some(n)
},
];
const SKIPS: [Alt; 11] = [
|s, i| lit(s, i, "#[ignore"),
|s, i| lit(s, i, "@pytest.mark.skip").or_else(|| lit(s, i, "@pytest.mark.xfail")),
|s, i| lit(s, i, "pytest.skip("),
|s, i| lit(s, i, "@unittest.skip"),
|s, i| tight_paren(s, i, ".skip"),
|s, i| {
if !starts_word(s, i) {
return None;
}
["xit", "xdescribe", "xtest"]
.iter()
.find_map(|w| tight_paren(s, i, w))
},
|s, i| tight_paren(s, i, ".todo"),
|s, i| word_then_paren(s, i, "t.Skip", false),
|s, i| lit(s, i, "@Disabled"),
|s, i| lit(s, i, "@Ignore").filter(|n| ends_word(s, i + n)),
|s, i| lit(s, i, "[Ignore]"),
];
fn count(s: &[char], alts: &[Alt]) -> i64 {
let (mut i, mut n) = (0, 0);
while i < s.len() {
match alts.iter().find_map(|a| a(s, i)) {
Some(len) if len > 0 => {
n += 1;
i += len;
}
_ => i += 1,
}
}
n
}
pub fn surface(text: &str) -> [i64; 3] {
let s: Vec<char> = text.chars().collect();
[count(&s, &TESTS), count(&s, &ASSERTS), count(&s, &SKIPS)]
}
pub fn delta(input: &Value) -> Option<[i64; 3]> {
let s = |p: &str| input.pointer(p).and_then(Value::as_str);
let pairs: Vec<(String, String)> = match s("/tool_name")? {
"Edit" => vec![(
s("/tool_input/old_string")?.into(),
s("/tool_input/new_string")?.into(),
)],
"MultiEdit" => input
.pointer("/tool_input/edits")?
.as_array()?
.iter()
.map(|e| {
Some((
e.get("old_string")?.as_str()?.to_string(),
e.get("new_string")?.as_str()?.to_string(),
))
})
.collect::<Option<_>>()?,
"Write" => {
let old = match input.pointer("/tool_response/originalFile") {
Some(Value::String(o)) => o.clone(),
_ if s("/tool_response/type") == Some("create") => String::new(),
_ => return None,
};
vec![(old, s("/tool_input/content")?.into())]
}
_ => return None,
};
Some(pairs.iter().fold([0; 3], |acc, (old, new)| {
let (a, b) = (surface(old), surface(new));
[
acc[0] + b[0] - a[0],
acc[1] + b[1] - a[1],
acc[2] + b[2] - a[2],
]
}))
}
pub fn created(input: &Value) -> Option<bool> {
match input.pointer("/tool_name").and_then(Value::as_str)? {
"Edit" | "MultiEdit" => Some(false),
"Write" => match input
.pointer("/tool_response/type")
.and_then(Value::as_str)?
{
"create" => Some(true),
"update" => Some(false),
_ => None,
},
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn counts_agree_with_the_python_replay_on_every_shared_case() {
let fx: Value =
serde_json::from_str(include_str!("../tests/fixtures/test_surface_cases.json"))
.unwrap();
for c in fx["cases"].as_array().unwrap() {
let t = c["text"].as_str().unwrap();
let want = [
c["tests"].as_i64().unwrap(),
c["asserts"].as_i64().unwrap(),
c["skips"].as_i64().unwrap(),
];
assert_eq!(surface(t), want, "case {t:?}");
}
}
#[test]
fn classes_follow_the_runners_default_file_patterns() {
for (p, want) in [
("/r/tests/cli.rs", "test"),
("/r/src/test/java/a/FooTest.java", "test"),
("/r/pkg/add_test.go", "test"),
("/r/test_add.py", "test"),
("/r/app/add_test.py", "test"),
("/r/src/a.test.tsx", "test"),
("/r/src/a.spec.mjs", "test"),
("/r/src/__tests__/a.js", "test"),
("/r/spec/models/user_spec.rb", "test"),
("/r/conftest.py", "test-config"),
("/r/tests/conftest.py", "test-config"),
("/r/jest.config.ts", "test-config"),
("/r/vitest.config.mts", "test-config"),
("/r/.cargo/config.toml", "test-config"),
("/r/package.json", "build"),
("/r/Cargo.toml", "build"),
("/r/vite.config.ts", "build"),
("/r/README.md", "doc"),
("/r/docs/api.html", "doc"),
("/r/tests/README.md", "test"),
("/r/src/main.rs", "code"),
("/r/src/testing.py", "code"),
("/r/src/latest.ts", "code"),
("/r/Test.java", "code"),
("C:\\r\\tests\\a.py", "test"),
] {
assert_eq!(class_of(p), want, "{p}");
}
}
#[test]
fn a_weakening_edit_reads_as_falling_counts_and_a_new_test_as_rising() {
let skip = json!({"tool_name": "Edit", "tool_input": {
"old_string": "def test_a():\n assert f() == 1\n",
"new_string": "@pytest.mark.skip\ndef test_a():\n pass\n"}});
assert_eq!(delta(&skip), Some([0, -1, 1]));
let multi = json!({"tool_name": "MultiEdit", "tool_input": {"edits": [
{"old_string": "#[test]\nfn a() { assert!(x); }", "new_string": ""},
{"old_string": "", "new_string": "#[test]\nfn b() { assert!(y); assert!(z); }"}]}});
assert_eq!(delta(&multi), Some([0, 1, 0]));
let create = json!({"tool_name": "Write",
"tool_input": {"content": "it('a', () => expect(1).toBe(1))"},
"tool_response": {"type": "create", "filePath": "/r/a.test.js"}});
assert_eq!(delta(&create), Some([1, 1, 0]));
assert_eq!(created(&create), Some(true));
let blind = json!({"tool_name": "Write", "tool_input": {"content": "x"},
"tool_response": {"type": "update", "filePath": "/r/a.py"}});
assert_eq!(delta(&blind), None);
assert_eq!(created(&blind), Some(false));
let nb = json!({"tool_name": "NotebookEdit", "tool_input": {"new_source": "x"}});
assert_eq!((delta(&nb), created(&nb)), (None, None));
}
}