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
//! Cross-system parity tests: CLI produces consistent results across query scenarios.
//!
//! These tests verify that the `sqry` CLI produces coherent, consistent results when
//! querying the same multi-language fixture project across four key scenarios:
//!
//! 1. **Function query parity** — `kind:function` surfaces expected Rust function names.
//! 2. **Index stats consistency** — `graph stats` reports non-zero node and edge counts.
//! 3. **Search result parity** — `name:process` locates the known `process` function.
//! 4. **Multi-language surfacing** — indexing Rust + Python + TypeScript yields symbols from
//! all three languages.
//!
//! The fixture under `test-fixtures/e2e-scenarios/multi-lang/` contains:
//! - `src/main.rs` — Rust binary (`main`)
//! - `src/lib.rs` — Rust library (`process`, `helper`)
//! - `src/server.py` — Python module (`handle_request`, `transform`, `RequestHandler`)
//! - `src/utils.ts` — TypeScript module (`formatOutput`, `Formatter`)
//! - `Cargo.toml` — Minimal Rust package config
mod common;
use common::sqry_bin;
use assert_cmd::Command;
use serde_json::Value;
use std::collections::BTreeSet;
use std::fs;
use std::io::{self, Write as IoWrite};
use std::path::{Path, PathBuf};
use tempfile::TempDir;
// ── helpers ────────────────────────────────────────────────────────────────
fn sqry_cmd() -> Command {
Command::new(sqry_bin())
}
/// Copy a fixture directory into a fresh `TempDir` so tests do not mutate shared state.
fn copy_fixture(relative: &str) -> TempDir {
let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("workspace root exists")
.to_path_buf();
let source = workspace_root.join(relative);
assert!(
source.exists(),
"fixture directory {} not found — expected at {relative}",
source.display()
);
let dest = TempDir::new().expect("create temp dir");
copy_dir_all(&source, dest.path()).expect("copy fixture into temp dir");
dest
}
/// Recursive directory copy, normalising CRLF → LF in text files so tree-sitter
/// produces identical byte offsets on all platforms.
fn copy_dir_all(src: &Path, dst: &Path) -> io::Result<()> {
fs::create_dir_all(dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let file_type = entry.file_type()?;
let dest_path = dst.join(entry.file_name());
if file_type.is_dir() {
copy_dir_all(&entry.path(), &dest_path)?;
} else {
let content = fs::read(entry.path())?;
let normalised: Vec<u8> = if content.contains(&b'\r') {
content.into_iter().filter(|&b| b != b'\r').collect()
} else {
content
};
let mut f = fs::File::create(&dest_path)?;
f.write_all(&normalised)?;
}
}
Ok(())
}
/// Index a project directory with `sqry index --force`.
fn index_project(path: &Path) {
sqry_cmd()
.arg("index")
.arg("--force")
.arg(path)
.assert()
.success();
}
/// Parse stdout bytes as JSON. Panics with the raw stderr on failure.
fn parse_json(stdout: &[u8], stderr: &[u8]) -> Value {
serde_json::from_slice(stdout).unwrap_or_else(|e| {
panic!(
"Failed to parse JSON output: {e}\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(stdout),
String::from_utf8_lossy(stderr)
)
})
}
/// Extract the set of `name` strings from a JSON query result's `results` array.
fn extract_names(json: &Value) -> BTreeSet<String> {
json["results"]
.as_array()
.map(|arr| {
arr.iter()
.filter_map(|entry| entry["name"].as_str().map(String::from))
.collect()
})
.unwrap_or_default()
}
/// Fixture path relative to workspace root.
const FIXTURE: &str = "test-fixtures/e2e-scenarios/multi-lang";
// ── scenario 1: function query parity ──────────────────────────────────────
/// Verify that `sqry query "kind:function"` surfaces the expected Rust function names.
///
/// The Rust source files in the fixture define `main`, `process`, and `helper`.
/// All three must appear in the query result set after indexing.
#[test]
fn parity_function_query() {
let project = copy_fixture(FIXTURE);
index_project(project.path());
let output = sqry_cmd()
.args(["--json", "query", "kind:function"])
.arg(project.path())
.output()
.expect("sqry query kind:function");
assert!(
output.status.success(),
"sqry query kind:function failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let json = parse_json(&output.stdout, &output.stderr);
let names = extract_names(&json);
assert!(
names.contains("main"),
"expected 'main' in function query results; got: {names:?}"
);
assert!(
names.contains("process"),
"expected 'process' in function query results; got: {names:?}"
);
assert!(
names.contains("helper"),
"expected 'helper' in function query results; got: {names:?}"
);
}
// ── scenario 2: index stats consistency ────────────────────────────────────
/// Verify that `sqry graph stats` reports non-zero node and edge counts after indexing
/// the multi-language fixture.
///
/// A well-formed index of even a minimal project must contain at least one node and
/// at least one structural edge (e.g. `Defines` / `Contains`).
#[test]
fn parity_index_stats() {
let project = copy_fixture(FIXTURE);
index_project(project.path());
let output = sqry_cmd()
.arg("graph")
.arg("--path")
.arg(project.path())
.arg("--format")
.arg("json")
.arg("stats")
.output()
.expect("sqry graph stats");
assert!(
output.status.success(),
"sqry graph stats failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let json = parse_json(&output.stdout, &output.stderr);
let node_count = json["node_count"]
.as_u64()
.expect("stats JSON must contain 'node_count'");
assert!(
node_count > 0,
"expected non-zero node_count in graph stats; got {node_count}"
);
let edge_count = json["edge_count"]
.as_u64()
.expect("stats JSON must contain 'edge_count'");
assert!(
edge_count > 0,
"expected non-zero edge_count in graph stats; got {edge_count}"
);
}
// ── scenario 3: search result name parity ──────────────────────────────────
/// Verify that `sqry query "name:process"` finds the `process` function.
///
/// `process` is defined in `src/lib.rs` (Rust) and in `src/server.py` (Python, as a method).
/// At minimum the Rust-level `process` symbol must appear in the output.
#[test]
fn parity_search_results() {
let project = copy_fixture(FIXTURE);
index_project(project.path());
let output = sqry_cmd()
.args(["--json", "query", "name:process"])
.arg(project.path())
.output()
.expect("sqry query name:process");
assert!(
output.status.success(),
"sqry query name:process failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let stdout_str = String::from_utf8_lossy(&output.stdout);
assert!(
stdout_str.contains("process"),
"expected 'process' in search results; stdout:\n{stdout_str}"
);
// Verify the result set is non-empty via the structured JSON stats field.
let json = parse_json(&output.stdout, &output.stderr);
let total_matches = json["stats"]["total_matches"].as_u64().unwrap_or(0);
assert!(
total_matches >= 1,
"expected at least 1 match for 'name:process'; got {total_matches}"
);
}
// ── scenario 4: multi-language symbol surfacing ─────────────────────────────
/// Verify that `sqry query "kind:function"` surfaces symbols from all three source
/// languages present in the fixture: Rust, Python, and TypeScript.
///
/// Expected symbols per language:
/// - Rust → `main`, `process`, `helper`
/// - Python → `handle_request`, `transform`
/// - TypeScript → `formatOutput`
///
/// The assertion strategy uses sorted-set membership so ordering and count differences
/// between the systems do not cause false failures.
#[test]
fn parity_multi_language() {
let project = copy_fixture(FIXTURE);
index_project(project.path());
let output = sqry_cmd()
.args(["--json", "query", "kind:function"])
.arg(project.path())
.output()
.expect("sqry query kind:function (multi-language)");
assert!(
output.status.success(),
"sqry query kind:function (multi-language) failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let json = parse_json(&output.stdout, &output.stderr);
let names = extract_names(&json);
// Rust functions must be present.
assert!(
names.contains("main") || names.contains("process"),
"expected at least one Rust function ('main' or 'process') in results; got: {names:?}"
);
// Python functions must be present (handle_request or transform).
assert!(
names.contains("handle_request") || names.contains("transform"),
"expected at least one Python function ('handle_request' or 'transform') in results; got: {names:?}"
);
// TypeScript function must be present.
assert!(
names.contains("formatOutput"),
"expected TypeScript function 'formatOutput' in results; got: {names:?}"
);
}