use std::path::Path;
use newt_acp_worker::TaskReply;
use crate::cases::{MockResponse, TestCase};
use crate::evaluators::{Evaluator, PythonImportsEvaluator};
use crate::scorecard::{EvalContext, EvalResult};
pub fn score_python_workspace(workspace: &Path, surface_dir: &Path) -> anyhow::Result<EvalResult> {
let case = TestCase {
name: "score".to_string(),
description: "ad-hoc workspace score".to_string(),
language: "python".to_string(),
prompt: String::new(),
evaluators: vec!["python_imports".to_string()],
expected_patterns: Vec::new(),
expected_output: None,
output_match: None,
mock_response: MockResponse {
content: String::new(),
},
difficulty: "L1".to_string(),
case_dir: surface_dir.to_path_buf(),
};
let reply = TaskReply::new("none", "", "", true)?;
let ctx = EvalContext {
case,
workspace: workspace.to_path_buf(),
baseline: workspace.to_path_buf(),
reply,
};
Ok(PythonImportsEvaluator.evaluate(&ctx))
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
fn write_file(path: &Path, contents: &str) {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).unwrap();
}
let mut f = std::fs::File::create(path).unwrap();
f.write_all(contents.as_bytes()).unwrap();
}
#[test]
fn scores_a_workspace_and_catches_the_incident() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
write_file(
&root.join("python_surface.json"),
r#"{"modules": ["newt_agent.core", "newt_agent.data"]}"#,
);
write_file(
&root.join("examples/ex.py"),
"from newt_core import classify\nfrom newt_agent.core import Router\nimport os\n",
);
let result = score_python_workspace(root, root).unwrap();
assert!(!result.passed, "a fabricated import must fail the score");
assert!(
result.details.contains("newt_core"),
"details: {}",
result.details
);
}
#[test]
fn clean_workspace_passes() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
write_file(
&root.join("python_surface.json"),
r#"{"modules": ["newt_agent.core"]}"#,
);
write_file(
&root.join("ok.py"),
"from newt_agent.core import Router\nimport json\n",
);
let result = score_python_workspace(root, root).unwrap();
assert!(result.passed, "details: {}", result.details);
}
}