sqry-cli 8.0.0

CLI for sqry - semantic code search
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! JSON error output tests (IT-CLI-DIAG-02)
//!
//! Tests JSON-formatted error output for automation/MCP workflows.
//! Verifies that --json flag produces structured error information with:
//! - Error code (`sqry::parse`, `sqry::validation`, etc.)
//! - Human-readable error message
//! - Query string that caused the error
//! - Precise error location (span with start/end positions)
//! - Helpful suggestions and labels
//! - Valid JSON format

mod common;
use common::sqry_bin;

use assert_cmd::Command;
use serde_json::Value;
use std::fs;
use tempfile::TempDir;

/// Helper: Create test project with files
fn create_test_project(files: &[(&str, &str)]) -> TempDir {
    let dir = TempDir::new().unwrap();
    for (path, content) in files {
        let file_path = dir.path().join(path);
        fs::create_dir_all(file_path.parent().unwrap()).unwrap();
        fs::write(&file_path, content).unwrap();
    }
    dir
}

/// Helper: Get sqry binary command
fn sqry_cmd() -> Command {
    let path = sqry_bin();
    Command::new(path)
}

/// Helper: Parse JSON from output and verify it's valid
fn parse_json_output(output: &str) -> Value {
    serde_json::from_str(output).expect("Output should be valid JSON")
}

// ============================================================================
// Parse Error JSON Output
// ============================================================================

#[test]
fn test_json_unmatched_parenthesis_error() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("(kind:function AND name:test")
        .arg(project.path())
        .assert()
        .failure()
        .code(2) // Parse error exit code
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));

    // Verify error structure
    assert!(json["error"].is_object(), "Should have 'error' object");
    assert_eq!(json["error"]["code"], "sqry::parse");
    assert!(
        json["error"]["message"]
            .as_str()
            .unwrap()
            .contains("Unmatched")
    );
    assert_eq!(json["error"]["query"], "(kind:function AND name:test");

    // Verify span is present
    assert!(
        json["error"]["span"].is_object(),
        "Should have span information"
    );
    assert!(json["error"]["span"]["start"].is_number());
    assert!(json["error"]["span"]["end"].is_number());

    // Verify label and help
    assert!(json["error"]["label"].is_string(), "Should have label");
    assert!(json["error"]["help"].is_string(), "Should have help text");
}

#[test]
fn test_json_unterminated_string_error() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg(r#"kind:function AND name:"test"#)
        .arg(project.path())
        .assert()
        .failure()
        .code(2)
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));

    // After parser unification, error code may be sqry::parse or sqry::syntax
    let code = json["error"]["code"].as_str().unwrap();
    assert!(
        code == "sqry::syntax" || code == "sqry::parse",
        "Expected sqry::syntax or sqry::parse, got {code}"
    );
    assert!(
        json["error"]["message"]
            .as_str()
            .unwrap()
            .contains("Unterminated")
    );
    assert!(json["error"]["span"].is_object());
    // Help text may be included in the wrapped message after parser unification
}

#[test]
fn test_json_unterminated_regex_error() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("kind:function AND name~=/test")
        .arg(project.path())
        .assert()
        .failure()
        .code(2)
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));

    // After parser unification, error code may be sqry::parse or sqry::syntax
    let code = json["error"]["code"].as_str().unwrap();
    assert!(
        code == "sqry::syntax" || code == "sqry::parse",
        "Expected sqry::syntax or sqry::parse, got {code}"
    );
    assert!(
        json["error"]["message"]
            .as_str()
            .unwrap()
            .contains("Unterminated")
    );
    // Help text may be included in the wrapped message after parser unification
}

// ============================================================================
// Validation Error JSON Output
// ============================================================================

#[test]
fn test_json_unknown_field_with_suggestion() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("kind:function AND knd:test")
        .arg(project.path())
        .assert()
        .failure()
        .code(2)
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));

    assert_eq!(json["error"]["code"], "sqry::validation");
    assert!(
        json["error"]["message"]
            .as_str()
            .unwrap()
            .contains("Unknown field")
    );
    assert!(json["error"]["message"].as_str().unwrap().contains("knd"));

    // Verify suggestion is present
    assert!(
        json["error"]["suggestion"].is_string(),
        "Should have suggestion"
    );
    assert_eq!(json["error"]["suggestion"], "kind");

    // Verify help includes suggestion
    assert!(
        json["error"]["help"]
            .as_str()
            .unwrap()
            .contains("Did you mean 'kind'?")
    );
}

#[test]
fn test_json_invalid_enum_value() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("kind:invalid_kind")
        .arg(project.path())
        .assert()
        .failure()
        .code(2)
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));

    assert_eq!(json["error"]["code"], "sqry::validation");
    assert!(
        json["error"]["message"]
            .as_str()
            .unwrap()
            .contains("Invalid value")
    );
    assert!(
        json["error"]["message"]
            .as_str()
            .unwrap()
            .contains("invalid_kind")
    );

    // Verify help includes valid values
    let help = json["error"]["help"].as_str().unwrap();
    assert!(help.contains("Valid values:"));
    assert!(help.contains("function"));
    assert!(help.contains("method"));
    assert!(help.contains("class"));
}

#[test]
fn test_json_unknown_field_no_suggestion() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("kind:function AND xyz:test")
        .arg(project.path())
        .assert()
        .failure()
        .code(2)
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));

    assert_eq!(json["error"]["code"], "sqry::validation");
    assert!(
        json["error"]["message"]
            .as_str()
            .unwrap()
            .contains("Unknown field")
    );
    assert!(json["error"]["message"].as_str().unwrap().contains("xyz"));

    // No suggestion field if there's no close match
    // (field may not be present or may be null)
    if json["error"]["suggestion"].is_string() {
        // If present, it should be empty or a different value
        assert_ne!(json["error"]["suggestion"].as_str().unwrap(), "xyz");
    }
}

// ============================================================================
// JSON Format Validation
// ============================================================================

#[test]
fn test_json_output_is_valid_json() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("(kind:function")
        .arg(project.path())
        .assert()
        .failure()
        .code(2)
        .get_output()
        .stdout
        .clone();

    // Should not panic
    let _json = parse_json_output(&String::from_utf8_lossy(&output));
}

#[test]
fn test_json_error_has_required_fields() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("(kind:function AND name:test")
        .arg(project.path())
        .assert()
        .failure()
        .code(2)
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));

    // Required fields that should always be present
    assert!(json["error"].is_object());
    assert!(json["error"]["code"].is_string());
    assert!(json["error"]["message"].is_string());
    assert!(json["error"]["query"].is_string());
}

#[test]
fn test_json_span_has_start_and_end() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("kind:function AND knd:test")
        .arg(project.path())
        .assert()
        .failure()
        .code(2)
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));

    // Span should have both start and end positions
    assert!(json["error"]["span"]["start"].is_number());
    assert!(json["error"]["span"]["end"].is_number());

    let start = json["error"]["span"]["start"].as_u64().unwrap();
    let end = json["error"]["span"]["end"].as_u64().unwrap();
    assert!(end > start, "End position should be after start position");
}

// ============================================================================
// Error Code Verification
// ============================================================================

#[test]
fn test_json_parse_error_code() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("(kind:function")
        .arg(project.path())
        .assert()
        .failure()
        .code(2) // Parse errors should exit with code 2
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));
    assert_eq!(json["error"]["code"], "sqry::parse");
}

#[test]
fn test_json_validation_error_code() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    let output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("kind:function AND knd:test")
        .arg(project.path())
        .assert()
        .failure()
        .code(2) // Validation errors should exit with code 2
        .get_output()
        .stdout
        .clone();

    let json = parse_json_output(&String::from_utf8_lossy(&output));
    assert_eq!(json["error"]["code"], "sqry::validation");
}

// ============================================================================
// Comparison: JSON vs Terminal Output
// ============================================================================

#[test]
fn test_json_flag_changes_output_format() {
    let project = create_test_project(&[("test.rs", "fn test() {}")]);

    // Without --json: should get terminal output (not JSON)
    let terminal_output = sqry_cmd()
        .arg("query")
        .arg("(kind:function")
        .arg(project.path())
        .assert()
        .failure()
        .get_output()
        .stderr // Terminal errors go to stderr
        .clone();

    let terminal_str = String::from_utf8_lossy(&terminal_output);

    // Terminal output should contain box drawing characters, not JSON
    assert!(
        terminal_str.contains("") || terminal_str.contains("") || terminal_str.contains("Error")
    );
    assert!(!terminal_str.contains(r#""error""#));

    // With --json: should get JSON output
    let json_output = sqry_cmd()
        .arg("query")
        .arg("--json")
        .arg("(kind:function")
        .arg(project.path())
        .assert()
        .failure()
        .get_output()
        .stdout // JSON output goes to stdout
        .clone();

    let json_str = String::from_utf8_lossy(&json_output);

    // JSON output should be parseable
    let _json = serde_json::from_str::<Value>(&json_str).expect("Should be valid JSON");
}