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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#![cfg(feature = "slow-tests")]
//! Contract: retrieval and graph traversal — recall, hybrid-search, link, unlink, related, graph.
//!
//! Part of the JSON-contract suite split by GAP-SG-208: the single file held
//! 1393 lines and 41 tests, past the 800-line ceiling this project sets for
//! itself. The shared harness lives in `tests/contract_support/`.
//!
//! Ground truth: `docs/schemas/*.schema.json`. Each test checks the expected
//! exit code, valid JSON, and the presence of the required keys.
#[path = "contract_support/mod.rs"]
mod support;
use serial_test::serial;
use support::{assert_array_items_have_keys, assert_has_keys, Env};
// ---------------------------------------------------------------------------
// 13 — recall
// ---------------------------------------------------------------------------
#[test]
#[serial]
fn contract_13_recall() {
let env = Env::new();
env.init();
env.remember(
"mem-recall-contrato",
"texto de busca semântica de contrato",
);
let out = env.cmd().args(["recall", "contrato"]).output().unwrap();
// exit 0 (found) or 4 (not found) are both valid
let code = out.status.code().unwrap_or(1);
assert!(
code == 0 || code == 4,
"recall exit code inesperado: {code}"
);
if code == 0 {
let json = Env::parse_stdout(&out);
assert_has_keys(
"recall",
&json,
&[
"query",
"k",
"direct_matches",
"graph_matches",
"results",
"elapsed_ms",
],
);
assert!(json["results"].is_array());
}
}
// ---------------------------------------------------------------------------
// 14 — hybrid-search
// ---------------------------------------------------------------------------
#[test]
#[serial]
fn contract_14_hybrid_search() {
let env = Env::new();
env.init();
env.remember("mem-hybrid-contrato", "texto para hybrid search contrato");
let out = env
.cmd()
.args(["hybrid-search", "contrato"])
.output()
.unwrap();
let code = out.status.code().unwrap_or(1);
assert!(
code == 0 || code == 4,
"hybrid-search exit code inesperado: {code}"
);
if code == 0 {
let json = Env::parse_stdout(&out);
assert_has_keys(
"hybrid-search",
&json,
&[
"query",
"k",
"rrf_k",
"weights",
"results",
"graph_matches",
"elapsed_ms",
],
);
assert!(json["results"].is_array());
}
}
// ---------------------------------------------------------------------------
// 15 — link
// ---------------------------------------------------------------------------
#[test]
#[serial]
fn contract_15_link() {
let env = Env::new();
env.init();
let (ent_a, ent_b) = env.remember_with_entities("mem-link-contrato", "corpo link entidades");
let out = env
.cmd()
.args([
"link",
"--from",
&ent_a,
"--to",
&ent_b,
"--relation",
"related",
])
.output()
.unwrap();
assert!(
out.status.success(),
"link failed: {:?}\nstderr: {}",
out.status,
String::from_utf8_lossy(&out.stderr)
);
let json = Env::parse_stdout(&out);
assert_has_keys(
"link",
&json,
&["action", "from", "to", "relation", "weight", "namespace"],
);
assert_eq!(json["action"], "created");
}
// ---------------------------------------------------------------------------
// 16 — unlink
// ---------------------------------------------------------------------------
#[test]
#[serial]
fn contract_16_unlink() {
let env = Env::new();
env.init();
let (ent_a, ent_b) =
env.remember_with_entities("mem-unlink-contrato", "corpo unlink entidades");
// Create relation first
env.cmd()
.args([
"link",
"--from",
&ent_a,
"--to",
&ent_b,
"--relation",
"related",
])
.assert()
.success();
let out = env
.cmd()
.args([
"unlink",
"--from",
&ent_a,
"--to",
&ent_b,
"--relation",
"related",
])
.output()
.unwrap();
assert!(
out.status.success(),
"unlink failed: {:?}\nstderr: {}",
out.status,
String::from_utf8_lossy(&out.stderr)
);
let json = Env::parse_stdout(&out);
assert_has_keys(
"unlink",
&json,
&[
"action",
"relationships_removed",
"from_name",
"to_name",
"relation",
"namespace",
"elapsed_ms",
],
);
assert_eq!(json["action"], "deleted");
}
// ---------------------------------------------------------------------------
// 17 — related
// O contrato publico atual exige objeto com {elapsed_ms, results:[...]}.
// Aceitar array root aqui enfraquece a deteccao de regressao documental.
// ---------------------------------------------------------------------------
#[test]
#[serial]
fn contract_17_related() {
let env = Env::new();
env.init();
let (ent_a, _ent_b) =
env.remember_with_entities("mem-related-a", "corpo entidade A para grafo");
let (ent_c, _ent_d) =
env.remember_with_entities("mem-related-b", "corpo entidade B para grafo");
// Liga as entidades para garantir que related retorna algo
env.cmd()
.args([
"link",
"--from",
&ent_a,
"--to",
&ent_c,
"--relation",
"related",
])
.assert()
.success();
let out = env
.cmd()
.args(["related", "--name", "mem-related-a"])
.output()
.unwrap();
let code = out.status.code().unwrap_or(1);
assert!(
code == 0 || code == 4,
"related exit code inesperado: {code}"
);
if code == 0 {
let json = Env::parse_stdout(&out);
let results = json.get("results").unwrap_or_else(|| {
panic!("related: expected object with {{results:[...]}}, got: {json}")
});
assert!(
results.is_array(),
"related: 'results' nao e array: {results}"
);
let arr = results.as_array().unwrap();
if !arr.is_empty() {
assert_array_items_have_keys(
"related",
results,
&[
"memory_id",
"name",
"namespace",
"type",
"description",
"hop_distance",
"source_entity",
"target_entity",
"relation",
"weight",
],
);
}
}
}
// ---------------------------------------------------------------------------
// 18 — graph
// ---------------------------------------------------------------------------
#[test]
#[serial]
fn contract_18_graph() {
let env = Env::new();
env.init();
let out = env
.cmd()
.args(["graph", "--format", "json"])
.output()
.unwrap();
assert!(out.status.success());
let json = Env::parse_stdout(&out);
assert_has_keys("graph", &json, &["nodes", "edges"]);
assert!(json["nodes"].is_array());
assert!(json["edges"].is_array());
}