1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Regression tests for graph traverse edge cases.
//!
//! P0-7: `graph traverse --from <nonexistent-entity>` must return exit 4
//! (NotFound) and never return exit 0 with a null/empty payload.
use assert_cmd::Command;
use tempfile::TempDir;
/// Builds a fresh `Command` with the mock LLM PATH prepended.
///
/// v1.0.76 spawns `claude` or `codex` on every `remember` / `ingest` /
/// `edit`. The bundled mocks under `tests/mock-llm/` return a fixed
/// 64-dim zero vector so the binary finishes without a real OAuth
/// login. The mock directory is leaked (no TempDir cleanup) so the
/// spawned subprocess always finds the mocks.
fn sgr_cmd() -> Command {
let mock_dir = common::mock_llm_path();
let mut c = Command::cargo_bin("sqlite-graphrag").expect("sqlite-graphrag binary not found");
c.env("PATH", common::prepend_path(&mock_dir));
c
}
#[path = "common/mod.rs"]
mod common;
/// Base command bound to the sandbox.
///
/// GAP-SG-101: this file used `SQLITE_GRAPHRAG_DB_PATH`, which no production
/// code reads (`src/paths.rs` documents this normatively). Every `remember`
/// below therefore landed in the developer's REAL database, and the second run
/// failed on a duplicate created by the first — the flakiness that made this
/// file the acceptance case for the gap. `--config-dir` / `--cache-dir` are
/// honoured on every OS, unlike `XDG_*`, which `directories` reads on Linux
/// only.
fn cmd_base(tmp: &TempDir) -> Command {
let mut c = sgr_cmd();
c.arg("--config-dir").arg(tmp.path().join("config"));
c.arg("--cache-dir").arg(tmp.path().join("cache"));
c.arg("--skip-memory-guard");
c
}
fn init_db(tmp: &TempDir) {
// Select the database through the XDG key so callers do not have to thread
// `--db` after every subcommand.
cmd_base(tmp)
.args(["config", "set", "db.path"])
.arg(tmp.path().join("test.sqlite"))
.assert()
.success();
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();
}
// ---------------------------------------------------------------------------
// P0-7 regression: traverse from nonexistent entity → exit 4, never exit 0
// ---------------------------------------------------------------------------
/// Regression for P0-7: `graph traverse --from <entity-that-does-not-exist>`
/// must fail with exit code 4 (NotFound).
///
/// Previously observed (audit v1.0.23): command returned exit 0 with payload
/// `{root: null, depth: null, visited_count: 0}` instead of the correct
/// exit 4 error response.
#[test]
fn test_p0_7_traverse_nonexistent_entity_exits_4() {
let tmp = TempDir::new().unwrap();
init_db(&tmp);
// Seed one real memory so the DB has at least one entity; this ensures the
// code path reaches the entity-lookup step and is not short-circuited by an
// empty-graph fast path.
remember_with_body(
&tmp,
"seed-memory-for-traverse-test",
"Anthropic builds AI systems",
);
// Traversal from a name that was never ingested into the namespace must
// produce exit 4, not exit 0 with a null payload.
cmd_base(&tmp)
.args([
"graph",
"traverse",
"--from",
"EntityThatAbsolutelyDoesNotExist",
"--depth",
"2",
"--namespace",
"audit",
])
.assert()
.failure()
.code(4);
}
/// Traverse from a valid entity must succeed (exit 0) and return JSON with
/// non-null `root` field. Guards against regressions in the happy path.
#[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",
);
// After ingestion the entity extractor may or may not have written an
// entity named "Anthropic". We therefore test the happy path only when the
// graph has at least one node by checking exit 0 OR exit 4.
//
// The invariant we are protecting: exit MUST be in {0, 4}; it must NEVER
// be 0 accompanied by a null root (the original P0-7 bug).
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 {
// When success, root must NOT be null (the P0-7 failure signature).
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)"
);
}
}
/// Traverse with --namespace that does not exist must also exit 4, not 0.
#[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);
}