use assert_cmd::Command;
use tempfile::TempDir;
fn cmd_base(tmp: &TempDir) -> Command {
let mut c = Command::cargo_bin("sqlite-graphrag").unwrap();
c.env("SQLITE_GRAPHRAG_DB_PATH", tmp.path().join("test.sqlite"));
c.env("SQLITE_GRAPHRAG_CACHE_DIR", tmp.path().join("cache"));
c.env("SQLITE_GRAPHRAG_LOG_LEVEL", "error");
c.arg("--skip-memory-guard");
c
}
fn init_db(tmp: &TempDir) {
cmd_base(tmp).arg("init").assert().success();
}
fn remember_with_body(tmp: &TempDir, name: &str, body: &str) {
cmd_base(tmp)
.args([
"remember",
"--name",
name,
"--type",
"user",
"--description",
"desc",
"--namespace",
"audit",
"--body",
body,
])
.assert()
.success();
}
#[test]
fn test_p0_7_traverse_nonexistent_entity_exits_4() {
let tmp = TempDir::new().unwrap();
init_db(&tmp);
remember_with_body(
&tmp,
"seed-memory-for-traverse-test",
"Anthropic builds AI systems",
);
cmd_base(&tmp)
.args([
"graph",
"traverse",
"--from",
"EntityThatAbsolutelyDoesNotExist",
"--depth",
"2",
"--namespace",
"audit",
])
.assert()
.failure()
.code(4);
}
#[test]
fn test_traverse_valid_entity_exits_0() {
let tmp = TempDir::new().unwrap();
init_db(&tmp);
remember_with_body(
&tmp,
"anthropic-memory",
"Anthropic builds Claude the AI assistant",
);
let output = cmd_base(&tmp)
.args([
"graph",
"traverse",
"--from",
"Anthropic",
"--depth",
"1",
"--namespace",
"audit",
])
.output()
.unwrap();
let exit_code = output.status.code().unwrap_or(-1);
assert!(
exit_code == 0 || exit_code == 4,
"expected exit 0 or 4, got {exit_code}"
);
if exit_code == 0 {
let stdout = String::from_utf8_lossy(&output.stdout);
let json: serde_json::Value =
serde_json::from_str(&stdout).expect("exit 0 must produce valid JSON on stdout");
assert!(
!json["root"].is_null(),
"exit 0 must not return a null root (P0-7 regression)"
);
}
}
#[test]
fn test_traverse_nonexistent_namespace_exits_4() {
let tmp = TempDir::new().unwrap();
init_db(&tmp);
cmd_base(&tmp)
.args([
"graph",
"traverse",
"--from",
"AnyEntity",
"--depth",
"2",
"--namespace",
"namespace-that-does-not-exist",
])
.assert()
.failure()
.code(4);
}