use std::path::PathBuf;
use crate::brain::agent::service::AgentService;
use crate::brain::agent::service::session_cwd::restorable_cwd;
use crate::brain::provider::Provider;
use crate::db::Database;
use crate::services::{ServiceContext, SessionService};
use crate::tests::agent_service_mocks::MockProvider;
use std::sync::Arc;
use uuid::Uuid;
#[test]
fn unset_or_blank_persisted_value_restores_nothing() {
assert_eq!(restorable_cwd(None), None);
assert_eq!(restorable_cwd(Some("")), None);
assert_eq!(restorable_cwd(Some(" ")), None);
}
#[test]
fn stale_path_restores_nothing() {
assert_eq!(
restorable_cwd(Some("/nonexistent/repo/moved/away")),
None,
"a directory that no longer exists must not be restored"
);
}
#[test]
fn existing_directory_is_restored() {
let dir = std::env::temp_dir();
let restored = restorable_cwd(Some(dir.to_string_lossy().as_ref()));
assert_eq!(
restored,
Some(dir),
"an existing directory is restored as written, not canonicalized"
);
}
#[test]
fn collapsed_home_path_is_expanded() {
let home = dirs::home_dir().expect("home dir");
let restored = restorable_cwd(Some("~")).expect("home must restore");
assert_eq!(restored, home);
assert_ne!(
restored,
PathBuf::from("~"),
"the tilde must be expanded, not passed through as a literal directory name"
);
}
async fn make_service_with_context() -> (AgentService, ServiceContext) {
let db = Database::connect_in_memory().await.unwrap();
db.run_migrations().await.unwrap();
let context = ServiceContext::new(db.pool().clone());
let provider: Arc<dyn Provider> = Arc::new(MockProvider);
let svc = AgentService::new_for_test(provider, context.clone()).await;
(svc, context)
}
#[tokio::test]
async fn readers_do_not_seed_and_persisted_restore_still_has_a_say() {
let (svc, context) = make_service_with_context().await;
let launch_dir = svc.get_working_directory();
let session_dir =
std::env::temp_dir().join(format!("opencrabs-1810-{}", Uuid::new_v4().simple()));
std::fs::create_dir_all(&session_dir).expect("create session dir");
let sessions = SessionService::new(context);
let created = sessions.create_session(None).await.expect("create session");
sessions
.update_session_working_directory(
created.id,
Some(session_dir.to_string_lossy().into_owned()),
)
.await
.expect("persist session working directory");
assert!(
svc.session_working_dir_unset(created.id),
"a fresh session must not have a cwd handle before any turn"
);
let read = svc.get_working_directory_for_session(created.id);
assert_eq!(
read, launch_dir,
"an untouched session still falls back to the global launch directory"
);
assert!(
svc.session_working_dir_unset(created.id),
"a reader must not seed the handle: the persisted-cwd restore would lose its say (#1810)"
);
svc.restore_persisted_working_directory(created.id).await;
assert_eq!(
svc.get_working_directory_for_session(created.id),
session_dir,
"the persisted directory wins over the launch directory"
);
assert_eq!(
svc.get_working_directory(),
launch_dir,
"a channel session restore must never drag the global cwd"
);
}