use super::{DEFAULT_CONFIG_REF, agent_ref, agent_worktree, repo_git};
use crate::harness_root::Roots;
use crate::template::authoring::{self, Origin};
use crate::template::{GitRunner, RealGit, scaffold};
use std::path::{Path, PathBuf};
use tempfile::TempDir;
pub(crate) fn workspace() -> (TempDir, PathBuf) {
let holder = TempDir::new().unwrap();
let data_root = holder.path().join("data");
std::fs::create_dir_all(&data_root).unwrap();
let roots = Roots {
config: holder.path().join("config"),
data: data_root,
};
let ws = holder.path().join("ws");
scaffold(&ws, &roots, &RealGit::new()).unwrap();
(holder, ws)
}
pub(crate) fn spawn_agent(ws: &Path, id: &str, start: &str) -> PathBuf {
let g = RealGit::new();
let wt = agent_worktree(ws, id);
let wt_str = wt.to_string_lossy().to_string();
let branch = agent_ref(id);
g.run(
&repo_git(ws),
&[
"worktree",
"add",
"-b",
branch.as_str(),
wt_str.as_str(),
start,
],
)
.unwrap();
std::fs::write(wt.join("goal.md"), id).unwrap();
g.run(&wt, &["add", "goal.md"]).unwrap();
g.run(&wt, &["commit", "-m", "dispatch"]).unwrap();
wt
}
pub(crate) fn spawn_root(ws: &Path, id: &str) -> PathBuf {
spawn_agent(ws, id, DEFAULT_CONFIG_REF)
}
pub(crate) fn amend_config(ws: &Path, files: &[(&str, &str)]) {
let owned: Vec<(String, String)> = files
.iter()
.map(|(r, c)| (r.to_string(), c.to_string()))
.collect();
authoring::author(
ws,
&ws.join(".no-pools"),
"default",
Origin::Advance,
move |dir| {
for (rel, content) in &owned {
let path = dir.join(rel);
std::fs::create_dir_all(path.parent().unwrap())?;
std::fs::write(path, content)?;
}
Ok(())
},
&RealGit::new(),
)
.unwrap();
}