use std::os::unix::net::UnixListener;
use std::path::Path;
use super::policy::{Rule, escape, text};
use super::tests::{Fixture, SESSION, unset};
use super::*;
#[test]
fn a_cursor_childs_private_directory_is_short_and_its_own() {
let private = Private::make_for(Agent::Cursor).unwrap();
let made = private.path().canonicalize().unwrap();
assert!(made.join(".cursor").as_os_str().len() <= 83, "{made:?}");
assert!(made.parent() == Path::new(SHORT_PARENT).canonicalize().ok().as_deref());
let data = Path::new(
"/Users/someone/a-data-directory-name-that-is-longer-than-eighty-three-characters-xxxxxx",
);
let worker = super::cursor_run::worker_dir(data, &made, Path::new("/Users/someone/w"));
assert!(worker.starts_with(&made), "{worker:?}");
assert_eq!(
super::cursor_run::worker(data, &made, Path::new("/w")),
Ok(None)
);
let gone = made.clone();
drop(private);
assert!(!gone.exists());
}
#[test]
fn the_shared_tmp_fallback_is_refused() {
let data = Path::new(
"/Users/someone/a-data-directory-name-that-is-longer-than-eighty-three-characters-xxxxxx",
);
let long = Path::new(
"/var/folders/ab/cdefghijklmnopqrstuvwxyz0123/T/neosian-harness-41-0123456789abcdef0123456789abcdef",
);
let refused = super::cursor_run::worker(data, long, Path::new("/Users/someone/w"));
assert!(refused.is_err_and(|e| e.contains("local worker")));
}
fn below_data(name: &str) -> (Fixture, std::path::PathBuf, std::path::PathBuf) {
let fixture = Fixture::new(name);
let data = fixture.path("d");
std::fs::create_dir_all(&data).unwrap();
let worker =
super::cursor_run::worker_dir(&data, &fixture.private(), &fixture.path("home/work"));
assert!(worker.starts_with(&data), "{worker:?}");
(fixture, data, worker)
}
#[test]
fn a_worker_directory_that_is_a_link_a_file_or_holds_a_foreign_socket_refuses() {
let check = |fixture: &Fixture, data: &Path| {
super::cursor_run::worker(data, &fixture.private(), &fixture.path("home/work"))
};
let (fixture, data, worker) = below_data("cw-link");
std::fs::create_dir_all(fixture.path("elsewhere")).unwrap();
std::os::unix::fs::symlink(fixture.path("elsewhere"), &worker).unwrap();
assert!(check(&fixture, &data).is_err_and(|e| e.contains("is a link")));
let (fixture, data, worker) = below_data("cw-file");
std::fs::write(&worker, "").unwrap();
assert!(check(&fixture, &data).is_err());
let (fixture, data, worker) = below_data("cw-sock");
std::fs::create_dir_all(&worker).unwrap();
std::fs::write(worker.join("worker.sock"), "").unwrap();
assert!(
check(&fixture, &data).is_err(),
"a file at the socket's name"
);
let (fixture, data, worker) = below_data("cw-ok");
std::fs::create_dir_all(&worker).unwrap();
assert_eq!(check(&fixture, &data), Ok(Some(worker.clone())));
}
#[test]
fn a_running_worker_of_the_persons_own_refuses_and_a_stale_socket_does_not() {
let (fixture, data, worker) = below_data("cw-busy");
std::fs::create_dir_all(&worker).unwrap();
let socket = worker.join("worker.sock");
let listener = UnixListener::bind(&socket).unwrap();
let found = super::cursor_run::worker(&data, &fixture.private(), &fixture.path("home/work"));
assert!(found.is_err_and(|e| e.contains("answers")));
drop(listener);
let stale = super::cursor_run::worker(&data, &fixture.private(), &fixture.path("home/work"));
assert_eq!(stale, Ok(Some(worker)));
}
#[test]
fn a_resumed_chat_has_its_persistent_session_root_made_first() {
let fixture = Fixture::new("cw-persist");
let persist = fixture.path("persist");
let home = fixture.path("home");
let chat = home.join(".cursor/chats");
std::fs::create_dir_all(&chat).unwrap();
let directory = fixture.path("home/work");
let state = (fixture.private(), None, None);
let made = admission::ready_in(
Agent::Cursor,
(SESSION, true),
&directory,
(&state.0, state.1, state.2),
(Ok(home), &unset, &persist),
);
assert!(made.is_err());
for dir in [
&persist,
&persist.join("claim-locks"),
&persist.join("bindings"),
] {
assert!(dir.is_dir(), "{dir:?}");
}
}
#[test]
fn a_cursor_child_writes_its_worker_socket_and_a_resumed_chat_its_claim_lock() {
let fixture = Fixture::new("cursor-live");
let data = fixture.path(&format!("home/{}", "d".repeat(84)));
std::fs::create_dir_all(&data).unwrap();
let directory = fixture.path("home/work");
let worker = super::cursor_run::worker_dir(&data, &fixture.private(), &directory);
assert!(worker.starts_with(fixture.private()), "{worker:?}");
let long = data.clone().into_os_string();
let var = move |name: &str| (name == cursor::DATA_VAR).then(|| long.clone());
let first = fixture.build(Agent::Cursor, Mode::ReadOnly, &var).unwrap();
let named = |rule: &Rule| match rule {
Rule::Literal(p) | Rule::Subpath(p) | Rule::Pattern(p) => p.clone(),
};
assert!(
first
.state
.iter()
.all(|r| !named(r).contains("worker.sock"))
);
assert!(first.state.iter().all(|r| !named(r).contains("/tmp/")));
let lock = fixture.path(&format!("persist/claim-locks/{}.lock", tests::SESSION));
assert!(first.state.contains(&Rule::Subpath(text(&lock).unwrap())));
let abandoned = format!(
"^{}\\.abandoned-[0-9a-f]+(/.*)?$",
escape(&text(&lock).unwrap())
);
assert!(first.state.contains(&Rule::Pattern(abandoned)));
assert!(
!first
.state
.iter()
.any(|r| matches!(r, Rule::Subpath(p) if p.ends_with("claim-locks")))
);
assert_eq!(
super::cursor_run::rules(&fixture.path("persist"), "not-a-uuid"),
Ok(Vec::new())
);
}
#[test]
fn a_cursor_chat_may_write_its_stores_rollback_journal() {
let fixture = Fixture::new("cw-journal");
let chats = cursor::chats(&fixture.path("home/.cursor"), &fixture.path("home/work"));
for session in [None, Some(SESSION)] {
let policy = policy::build(&Inputs {
agent: Agent::Cursor,
mode: Mode::ReadOnly,
directory: &fixture.path("home/work"),
private: &fixture.private(),
home: &fixture.path("home"),
stores: &fixture.stores(),
var: &unset,
session,
child_home: None,
session_dir: None,
persist: &fixture.path("persist"),
})
.unwrap();
let allowed = |name: &str| {
let path = chats.join(SESSION).join(name);
policy.state.iter().any(|rule| match rule {
policy::Rule::Pattern(p) => fancy_regex::Regex::new(p)
.unwrap()
.is_match(path.to_str().unwrap())
.unwrap(),
_ => false,
})
};
assert!(allowed("store.db-journal"), "{session:?}");
assert!(
allowed("store.db") && !allowed("store.db-other"),
"{session:?}"
);
}
}