use everruns_core::{MountFs, SessionFileSystem, SessionId, WorkspaceRootSet};
use everruns_runtime::{InMemorySessionFileStore, RealDiskFileStore, multi_root_file_system};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let session = SessionId::new();
let backend: Arc<dyn SessionFileSystem> = Arc::new(InMemorySessionFileStore::new());
let fs = MountFs::wrap(backend);
fs.write_file(session, "/workspace/src/lib.rs", "fn main() {}", "text")
.await?;
for input in ["/workspace/src/lib.rs", "/src/lib.rs", "src/lib.rs"] {
let file = fs.read_file(session, input).await?.expect("file exists");
assert_eq!(file.content.as_deref(), Some("fn main() {}"));
assert_eq!(file.path, "/src/lib.rs");
println!("read {input:<24} -> backend key {} ✓", file.path);
}
assert_eq!(fs.display_root(), "/workspace");
assert_eq!(fs.display_path("/src/lib.rs"), "/workspace/src/lib.rs");
println!(
"display_root = {}, display_path(/src/lib.rs) = {}",
fs.display_root(),
fs.display_path("/src/lib.rs")
);
let worktree_a = tempfile::tempdir()?;
let worktree_b = tempfile::tempdir()?;
let disk = Arc::new(RealDiskFileStore::new(worktree_a.path())?);
let host_fs = MountFs::wrap(disk.clone());
host_fs
.write_file(session, "/workspace/notes.md", "from worktree A", "text")
.await?;
let on_disk_a = std::fs::read_to_string(disk.root().join("notes.md"))?;
println!("\nworktree A root: {}", disk.root().display());
println!(" /workspace/notes.md -> on disk: {on_disk_a:?}");
assert_eq!(on_disk_a, "from worktree A");
disk.set_host_root(worktree_b.path())?;
host_fs
.write_file(session, "/workspace/notes.md", "from worktree B", "text")
.await?;
let on_disk_b = std::fs::read_to_string(disk.root().join("notes.md"))?;
println!("worktree B root: {}", disk.root().display());
println!(" /workspace/notes.md -> on disk: {on_disk_b:?}");
assert_eq!(on_disk_b, "from worktree B");
assert_eq!(host_fs.display_root(), "/workspace");
println!(
"\nmodel-facing root stayed `{}` across the switch ✓",
host_fs.display_root()
);
let primary = tempfile::tempdir()?;
let backend = tempfile::tempdir()?;
let roots = WorkspaceRootSet::new(
primary.path(),
[("backend".to_string(), backend.path().to_path_buf())],
)?;
let multi_root_fs = multi_root_file_system(&roots)?;
multi_root_fs
.write_file(session, "/workspace/README.md", "primary", "text")
.await?;
multi_root_fs
.write_file(
session,
"/workspace/roots/backend/Cargo.toml",
"backend",
"text",
)
.await?;
assert_eq!(
std::fs::read_to_string(primary.path().join("README.md"))?,
"primary"
);
assert_eq!(
std::fs::read_to_string(backend.path().join("Cargo.toml"))?,
"backend"
);
println!(
"multi-root write: /workspace -> {}, /workspace/roots/backend -> {}",
primary.path().display(),
backend.path().display()
);
println!("\nmount_fs_workspace example OK");
Ok(())
}