use std::path::{Path, PathBuf};
use arabica_adapters::{NewSession, SqliteSessionStore};
use arabica_protocol::SessionId;
fn temp_dir(label: &str) -> PathBuf {
let unique = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock is valid")
.as_nanos();
let path = std::env::temp_dir().join(format!("arabica-sessions-e2e-{label}-{unique}"));
std::fs::create_dir_all(&path).expect("temp dir created");
path.canonicalize().expect("temp dir canonicalizes")
}
fn seed_session(arabica_home: &Path, workspace_root: &Path, session_id: &str) {
let workspace_id = arabica_cli::host::workspace_id_for(workspace_root);
SqliteSessionStore::create(
arabica_home,
NewSession {
session_id: &SessionId::new(session_id),
workspace_id: &workspace_id,
cwd: workspace_root,
profile: None,
instructions_sha256: None,
},
)
.expect("session file is created");
}
fn run_sessions(arabica_home: &Path, workspace_root: &Path, args: &[&str]) -> std::process::Output {
std::process::Command::new(env!("CARGO_BIN_EXE_arabica"))
.arg("sessions")
.args(args)
.current_dir(workspace_root)
.env_remove("OPENAI__API_KEY")
.env_remove("OPENAI__BASE_URL")
.env_remove("OPENAI__MODEL")
.env("ARABICA_HOME", arabica_home)
.output()
.expect("spawn the real structure binary")
}
#[test]
fn sessions_list_needs_no_model_provider_and_reports_an_empty_home() {
let arabica_home = temp_dir("empty-home");
let workspace_root = temp_dir("empty-workspace");
let output = run_sessions(&arabica_home, &workspace_root, &["list"]);
assert!(
output.status.success(),
"sessions list must succeed with no provider env vars set; stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(
String::from_utf8_lossy(&output.stdout).contains("no sessions found"),
"stdout: {}",
String::from_utf8_lossy(&output.stdout)
);
std::fs::remove_dir_all(&workspace_root).ok();
}
#[test]
fn sessions_list_scopes_to_the_current_workspace_unless_all_is_passed() {
let arabica_home = temp_dir("scoped-home");
let workspace_a = temp_dir("workspace-a");
let workspace_b = temp_dir("workspace-b");
seed_session(&arabica_home, &workspace_a, "session-a");
seed_session(&arabica_home, &workspace_b, "session-b");
let scoped = run_sessions(&arabica_home, &workspace_a, &["list"]);
assert!(scoped.status.success());
let scoped_stdout = String::from_utf8_lossy(&scoped.stdout).into_owned();
assert!(
scoped_stdout.contains("session-a"),
"stdout: {scoped_stdout}"
);
assert!(
!scoped_stdout.contains("session-b"),
"unscoped listing must not include the other workspace's session; stdout: {scoped_stdout}"
);
let all = run_sessions(&arabica_home, &workspace_a, &["list", "--all"]);
assert!(all.status.success());
let all_stdout = String::from_utf8_lossy(&all.stdout).into_owned();
assert!(all_stdout.contains("session-a"), "stdout: {all_stdout}");
assert!(all_stdout.contains("session-b"), "stdout: {all_stdout}");
std::fs::remove_dir_all(&workspace_a).ok();
std::fs::remove_dir_all(&workspace_b).ok();
}
#[test]
fn rename_persists_a_title_across_binary_invocations() {
let home = temp_dir("rename-home");
let root = temp_dir("rename-root");
seed_session(&home, &root, "session-rename");
let output = run_sessions(
&home,
&root,
&["rename", "session-rename", "My saved title"],
);
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let listing = run_sessions(&home, &root, &["list"]);
assert!(listing.status.success());
assert!(String::from_utf8_lossy(&listing.stdout).contains("My saved title"));
let workspace = arabica_cli::host::workspace_id_for(&root);
let stored =
SqliteSessionStore::read_session(&home, &workspace, &SessionId::new("session-rename"))
.unwrap();
assert_eq!(stored.header.title.as_deref(), Some("My saved title"));
std::fs::remove_dir_all(root).unwrap();
std::fs::remove_dir_all(home).unwrap();
}