use everruns_core::capabilities::BashTool;
use everruns_core::tools::{Tool, ToolExecutionResult};
use everruns_core::traits::ToolContext;
use everruns_core::{MountFs, SessionFileSystem, SessionId};
use everruns_runtime::RealDiskFileStore;
use serde_json::json;
use std::sync::Arc;
use tempfile::TempDir;
async fn bash_pwd(ctx: &ToolContext) -> String {
match BashTool
.execute_with_context(json!({ "commands": "pwd" }), ctx)
.await
{
ToolExecutionResult::Success(output) => output["stdout"]
.as_str()
.expect("pwd stdout is a string")
.trim()
.to_string(),
other => panic!("bash pwd did not succeed: {other:?}"),
}
}
#[tokio::test]
async fn all_surfaces_agree_on_root_across_worktree_switch() {
let session = SessionId::from_seed(660);
let worktree_a = TempDir::new().unwrap();
let backend = Arc::new(RealDiskFileStore::new(worktree_a.path()).unwrap());
let store: Arc<dyn SessionFileSystem> = MountFs::wrap(backend.clone());
let ctx = ToolContext::with_file_store(session, store.clone());
let root_a = backend.root();
store
.write_file(session, "/workspace/marker.txt", "ALPHA", "text")
.await
.unwrap();
let read = store
.read_file(session, "/workspace/marker.txt")
.await
.unwrap()
.unwrap();
assert_eq!(read.content.as_deref(), Some("ALPHA"));
assert_eq!(read.path, "/marker.txt");
let relative = store
.read_file(session, "marker.txt")
.await
.unwrap()
.unwrap();
assert_eq!(relative.content.as_deref(), Some("ALPHA"));
let hits = store.grep_files(session, "ALPHA", None).await.unwrap();
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].path, "/marker.txt");
assert_eq!(
std::fs::read_to_string(backend.root().join("marker.txt")).unwrap(),
"ALPHA"
);
assert_eq!(store.display_root(), "/workspace");
assert_eq!(store.display_path("/marker.txt"), "/workspace/marker.txt");
assert_eq!(bash_pwd(&ctx).await, "/workspace");
let worktree_b = TempDir::new().unwrap();
backend.set_host_root(worktree_b.path()).unwrap();
let root_b = backend.root();
assert_ne!(root_a, root_b);
store
.write_file(session, "/marker.txt", "BETA", "text")
.await
.unwrap();
let read_b = store
.read_file(session, "/workspace/marker.txt")
.await
.unwrap()
.unwrap();
assert_eq!(read_b.content.as_deref(), Some("BETA"));
let hits_b = store.grep_files(session, "BETA", None).await.unwrap();
assert_eq!(hits_b.len(), 1);
assert_eq!(hits_b[0].path, "/marker.txt");
assert_eq!(
std::fs::read_to_string(root_b.join("marker.txt")).unwrap(),
"BETA"
);
assert_eq!(
std::fs::read_to_string(root_a.join("marker.txt")).unwrap(),
"ALPHA",
"worktree A is untouched by writes after the switch"
);
assert_eq!(store.display_root(), "/workspace");
assert_eq!(bash_pwd(&ctx).await, "/workspace");
}