use std::ffi::OsString;
use std::path::Path;
use super::policy::{Rule, text};
use super::tests::{Fixture, SESSION, unset};
use super::*;
#[test]
fn the_chats_directory_and_project_name_are_cursors() {
let dir = Path::new("/Users/person/work/app");
let chats = cursor::chats(Path::new("/c"), dir);
assert_eq!(
chats,
Path::new("/c/chats/ea5e120a24f433955facbd56b4d9e0d3")
);
assert_eq!(cursor::slug(dir), "Users-person-work-app");
assert_eq!(cursor::slug(Path::new("/a b/é--c/")), "a-b-c");
}
#[test]
fn the_config_root_follows_cursors_order_and_stays_below_home() {
let fixture = Fixture::new("cursor-roots");
let home = fixture.path("home");
let at = |pairs: Vec<(&'static str, OsString)>| {
move |name: &str| {
pairs
.iter()
.find(|(k, _)| *k == name)
.map(|(_, v)| v.clone())
}
};
let moved = OsString::from(fixture.path("home/cfg"));
let xdg = OsString::from(fixture.path("home/xdg"));
let both = at(vec![
("CURSOR_CONFIG_DIR", moved.clone()),
("XDG_CONFIG_HOME", xdg.clone()),
]);
assert_eq!(
cursor::config_root(&home, &both),
Ok(fixture.path("home/cfg"))
);
let only_xdg = at(vec![("XDG_CONFIG_HOME", xdg)]);
assert_eq!(
cursor::config_root(&home, &only_xdg),
Ok(fixture.path("home/xdg/cursor"))
);
assert_eq!(cursor::config_root(&home, &unset), Ok(home.join(".cursor")));
let relative = at(vec![("CURSOR_CONFIG_DIR", OsString::from("rel"))]);
assert!(cursor::config_root(&home, &relative).is_err());
let away = at(vec![("CURSOR_DATA_DIR", OsString::from(fixture.private()))]);
assert!(cursor::data_root(&home, &away).is_err());
let data = at(vec![
("CURSOR_DATA_DIR", OsString::from(fixture.path("home/data"))),
("CURSOR_CONFIG_DIR", moved),
]);
let policy = policy::build(&Inputs {
agent: Agent::Codex,
mode: Mode::Write,
directory: &fixture.path("home/work"),
private: &fixture.private(),
home: &home,
stores: &fixture.stores(),
var: &data,
session: None,
child_home: None,
session_dir: None,
persist: &fixture.path("persist"),
})
.unwrap();
for root in ["home/.cursor", "home/cfg", "home/data"] {
assert!(
policy.roots.contains(&text(&fixture.path(root)).unwrap()),
"{root}"
);
}
let project = Rule::Subpath(text(&fixture.path("home/work/.cursor")).unwrap());
assert!(policy.protect.contains(&project));
}
#[test]
fn a_cursor_child_writes_its_chats_and_reads_no_integration() {
let fixture = Fixture::new("cursor-policy");
let work = fixture.path("home/work");
let chats = cursor::chats(&fixture.path("home/.cursor"), &work);
let shown = text(&chats).unwrap();
let continued = fixture
.build(Agent::Cursor, Mode::ReadOnly, &unset)
.unwrap();
assert!(
continued
.state
.contains(&Rule::Literal(format!("{shown}/{SESSION}")))
);
assert!(
!continued
.state
.iter()
.any(|r| matches!(r, Rule::Pattern(p) if p.contains("[^/]+$")))
);
let first = policy::build(&Inputs {
agent: Agent::Cursor,
mode: Mode::Write,
directory: &work,
private: &fixture.private(),
home: &fixture.path("home"),
stores: &fixture.stores(),
var: &unset,
session: None,
child_home: None,
session_dir: None,
persist: &fixture.path("persist"),
})
.unwrap();
let project = fixture
.path("home/.cursor/projects")
.join(cursor::slug(&work));
assert!(
first
.state
.contains(&Rule::Literal(text(&project).unwrap()))
);
assert!(
first
.state
.iter()
.any(|r| matches!(r, Rule::Pattern(p) if p.ends_with("/[^/]+$")))
);
let moved = OsString::from(fixture.path("home/cfg"));
let at = move |name: &str| (name == "CURSOR_CONFIG_DIR").then(|| moved.clone());
let elsewhere = fixture.build(Agent::Cursor, Mode::ReadOnly, &at).unwrap();
for hidden in [
"home/.cursor/mcp.json",
"home/cfg/mcp.json",
"home/.cursor/plugins",
"home/.claude/settings.json",
"home/work/.cursor/mcp.json",
"home/.cursor/hooks.json",
"home/work/.claude/settings.local.json",
] {
let place = text(&fixture.path(hidden)).unwrap();
assert!(elsewhere.hidden.contains(&place), "{hidden}");
}
assert!(
elsewhere
.hidden
.iter()
.any(|p| p.ends_with("Cursor/hooks.json"))
);
let codex = fixture.build(Agent::Codex, Mode::ReadOnly, &unset).unwrap();
assert!(!codex.hidden.iter().any(|p| p.contains("mcp.json")));
}
#[test]
fn trust_is_read_as_cursor_reads_it_and_a_cursor_child_needs_macos() {
let fixture = Fixture::new("cursor-trust");
let home = fixture.path("home");
let work = fixture.path("home/work");
std::fs::create_dir_all(work.join("sub")).unwrap();
let projects = home.join(".cursor/projects");
let mark = |dir: &Path| {
let place = projects.join(cursor::slug(dir));
std::fs::create_dir_all(&place).unwrap();
std::fs::write(place.join(".workspace-trusted"), "{}").unwrap();
};
assert!(!cursor::trusted(&home, &unset, &work));
mark(&home);
assert!(
!cursor::trusted(&home, &unset, &work),
"home itself never counts"
);
mark(&work);
assert!(cursor::trusted(&home, &unset, &work));
assert!(
cursor::trusted(&home, &unset, &work.join("sub")),
"an ancestor counts"
);
let refused = presets(Agent::Cursor, &[], &work).unwrap_err();
assert!(refused.contains("only on macOS"), "{refused}");
assert_eq!(platform_for("linux"), Platform::Presets);
}
#[test]
fn a_first_turn_writes_no_chat_that_existed_at_launch() {
let fixture = Fixture::new("cursor-existing");
let work = fixture.path("home/work");
let chats = cursor::chats(&fixture.path("home/.cursor"), &work);
std::fs::create_dir_all(chats.join("older")).unwrap();
let first = |var: &dyn Fn(&str) -> Option<OsString>| {
policy::build(&Inputs {
agent: Agent::Cursor,
mode: Mode::ReadOnly,
directory: &work,
private: &fixture.private(),
home: &fixture.path("home"),
stores: &fixture.stores(),
var,
session: None,
child_home: None,
session_dir: None,
persist: &fixture.path("persist"),
})
};
let policy = first(&unset).unwrap();
let older = Rule::Subpath(text(&chats.join("older")).unwrap());
assert!(policy.protect.contains(&older));
assert!(
!policy
.protect
.iter()
.any(|r| matches!(r, Rule::Subpath(p) if p.ends_with("/newer")))
);
let continued = fixture
.build(Agent::Cursor, Mode::ReadOnly, &unset)
.unwrap();
assert!(!continued.protect.contains(&older));
for n in 1..cursor::CHATS_NAMED {
std::fs::create_dir(chats.join(format!("c{n}"))).unwrap();
}
assert!(first(&unset).is_ok(), "{} chats", cursor::CHATS_NAMED);
std::fs::create_dir(chats.join("one-more")).unwrap();
let refused = first(&unset).unwrap_err();
assert!(refused.contains("more than 512 chats"), "{refused}");
}
#[test]
fn a_continued_chat_must_still_be_cursors_own() {
let fixture = Fixture::new("cursor-chat-ready");
let (home, work) = (fixture.path("home"), fixture.path("home/work"));
let dir = cursor::chats(&home.join(".cursor"), &work).join(SESSION);
let ready = || cursor::chat_ready(&home, &unset, &work, SESSION);
assert!(
ready().unwrap_err().contains("is not a real directory"),
"absent"
);
std::fs::create_dir_all(&dir).unwrap();
assert!(ready().is_err(), "no database");
std::fs::write(dir.join("store.db"), "x").unwrap();
assert_eq!(ready(), Ok(()));
std::fs::hard_link(dir.join("store.db"), fixture.private().join("db")).unwrap();
assert!(ready().is_err(), "a database with two names");
std::fs::remove_file(fixture.private().join("db")).unwrap();
let moved = fixture.private().join("chat");
std::fs::rename(&dir, &moved).unwrap();
std::os::unix::fs::symlink(&moved, &dir).unwrap();
assert!(ready().is_err(), "a link to a chat elsewhere");
}
#[test]
fn vibe_and_antigravity_configuration_stays_unwritable_inside_a_workspace() {
let fixture = Fixture::new("vibe-agy");
let inside = OsString::from(fixture.path("home/work/vibe-home"));
let var = move |name: &str| (name == "VIBE_HOME").then(|| inside.clone());
for agent in Agent::ALL {
let policy = fixture.build(agent, Mode::Write, &var).unwrap();
let project = Rule::Subpath(text(&fixture.path("home/work/.vibe")).unwrap());
assert!(policy.protect.contains(&project), "{agent:?}");
for root in [
"home/work/vibe-home",
"home/.gemini/config",
"home/.gemini/antigravity-cli",
] {
let root = text(&fixture.path(root)).unwrap();
assert!(policy.roots.contains(&root), "{agent:?} {root}");
}
}
let default = fixture.build(Agent::Codex, Mode::Write, &unset).unwrap();
assert!(
default
.roots
.contains(&text(&fixture.path("home/.vibe")).unwrap())
);
}