use super::*;
use crate::tools::{WriteFileTool, WriteFilesTool};
fn multi_write_file_response(files: &[(&str, &str, &str)]) -> Value {
let tool_calls: Vec<Value> = files
.iter()
.map(|(id, path, content)| {
let arguments = json!({ "path": path, "content": content }).to_string();
json!({
"id": id,
"type": "function",
"function": { "name": "write_file", "arguments": arguments }
})
})
.collect();
json!({
"id": "gen-multi-write",
"choices": [{
"message": { "role": "assistant", "content": null, "tool_calls": tool_calls },
"finish_reason": "tool_calls"
}],
"usage": { "prompt_tokens": 20, "completion_tokens": 15, "total_tokens": 35 }
})
}
fn write_files_call_response(call_id: &str, files: &[(&str, &str)]) -> Value {
let arr: Vec<Value> = files
.iter()
.map(|(path, content)| json!({ "path": path, "content": content }))
.collect();
let arguments = json!({ "files": arr }).to_string();
json!({
"id": "gen-write-files",
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": call_id,
"type": "function",
"function": { "name": "write_files", "arguments": arguments }
}]
},
"finish_reason": "tool_calls"
}],
"usage": { "prompt_tokens": 20, "completion_tokens": 15, "total_tokens": 35 }
})
}
#[tokio::test]
async fn batched_write_file_calls_write_all_files_in_one_turn() {
let tmp = tempfile::tempdir().expect("tempdir");
let mut reg = ToolRegistry::new();
reg.register(Arc::new(WriteFileTool::new(tmp.path())));
let registry = Arc::new(reg);
let llm = Arc::new(ScriptedLlm::from_json(&[
multi_write_file_response(&[
("w1", "a.py", "# a"),
("w2", "pkg/b.py", "# b"),
("w3", "src/main.rs", "fn main() {}"),
]),
stop_response("scaffolded three files in one turn"),
]));
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default());
let out = agent
.run("system", "scaffold three files")
.await
.expect("batched write turn should complete");
assert_eq!(out.content, "scaffolded three files in one turn");
assert_eq!(
llm.calls(),
2,
"all three writes must happen in ONE turn, then one stop — not three turns"
);
assert_eq!(
std::fs::read_to_string(tmp.path().join("a.py")).unwrap(),
"# a"
);
assert_eq!(
std::fs::read_to_string(tmp.path().join("pkg/b.py")).unwrap(),
"# b"
);
assert_eq!(
std::fs::read_to_string(tmp.path().join("src/main.rs")).unwrap(),
"fn main() {}"
);
}
#[tokio::test]
async fn write_files_tool_writes_all_files_in_one_turn() {
let tmp = tempfile::tempdir().expect("tempdir");
let mut reg = ToolRegistry::new();
reg.register(Arc::new(WriteFilesTool::new(tmp.path())));
let registry = Arc::new(reg);
let llm = Arc::new(ScriptedLlm::from_json(&[
write_files_call_response(
"wf1",
&[
("app/__init__.py", ""),
("app/main.py", "print('hi')"),
("app/models.py", "# models"),
("tests/test_main.py", "def test(): pass"),
],
),
stop_response("wrote the whole scaffold in one call"),
]));
let agent = make_loop(llm.clone(), registry, AgentLoopConfig::default());
let out = agent
.run("system", "scaffold the app")
.await
.expect("write_files turn should complete");
assert_eq!(out.content, "wrote the whole scaffold in one call");
assert_eq!(
llm.calls(),
2,
"one write_files call writes every file in ONE turn, then one stop"
);
for (path, content) in [
("app/__init__.py", ""),
("app/main.py", "print('hi')"),
("app/models.py", "# models"),
("tests/test_main.py", "def test(): pass"),
] {
assert_eq!(
std::fs::read_to_string(tmp.path().join(path)).unwrap(),
content,
"file {path} must be written by the single write_files call"
);
}
}