cqs 1.22.0

Code intelligence and RAG for AI agents. Semantic search, call graphs, impact analysis, type dependencies, and smart context assembly — in single tool calls. 54 languages + L5X/L5K PLC exports, 91.2% Recall@1 (BGE-large), 0.951 MRR (296 queries). Local ML, GPU-accelerated.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//! Output and display functions for CLI results

use std::collections::HashMap;
use std::path::Path;

use anyhow::{Context, Result};
use colored::Colorize;

use cqs::reference::TaggedResult;
use cqs::store::{ParentContext, UnifiedResult};

/// Read context lines before and after a range in a file
/// # Arguments
/// * `line_start` - 1-indexed start line (0 treated as 1)
/// * `line_end` - 1-indexed end line (must be >= line_start)
pub fn read_context_lines(
    file: &Path,
    line_start: u32,
    line_end: u32,
    context: usize,
) -> Result<(Vec<String>, Vec<String>)> {
    // Path traversal guard: reject absolute paths and `..` traversal that could
    // escape the project root via tampered DB paths. (RT-FS-1/RT-FS-2/SEC-12)
    //
    // DB stores relative paths; absolute paths indicate injection.
    let path_str = file.to_string_lossy();
    if path_str.starts_with('/') || (path_str.len() >= 2 && path_str.as_bytes()[1] == b':') {
        anyhow::bail!("Absolute path blocked: {}", file.display());
    }
    if path_str.contains("..") {
        if let (Ok(canonical), Ok(cwd)) = (
            dunce::canonicalize(file),
            std::env::current_dir().and_then(dunce::canonicalize),
        ) {
            if !canonical.starts_with(&cwd) {
                anyhow::bail!("Path traversal blocked: {}", file.display());
            }
        }
    }

    // Size guard: don't read files larger than 10MB for context display
    const MAX_DISPLAY_FILE_SIZE: u64 = 10 * 1024 * 1024;
    if let Ok(meta) = std::fs::metadata(file) {
        if meta.len() > MAX_DISPLAY_FILE_SIZE {
            anyhow::bail!(
                "File too large for context display: {}MB (limit {}MB)",
                meta.len() / (1024 * 1024),
                MAX_DISPLAY_FILE_SIZE / (1024 * 1024)
            );
        }
    }
    let content = std::fs::read_to_string(file)
        .with_context(|| format!("Failed to read {}", file.display()))?;
    // .lines() handles \r\n, but trim trailing \r for bare-CR edge cases
    let lines: Vec<&str> = content.lines().map(|l| l.trim_end_matches('\r')).collect();

    // Normalize: treat 0 as 1, ensure end >= start
    let line_start = line_start.max(1);
    let line_end = line_end.max(line_start);

    // Convert 1-indexed lines to 0-indexed array indices, clamped to valid range.
    // For an empty file (lines.len() == 0), both indices will be 0.
    let max_idx = lines.len().saturating_sub(1);
    let start_idx = (line_start as usize).saturating_sub(1).min(max_idx);
    let end_idx = (line_end as usize).saturating_sub(1).min(max_idx);

    // Context before
    let context_start = start_idx.saturating_sub(context);
    let before: Vec<String> = if start_idx <= lines.len() {
        lines[context_start..start_idx]
            .iter()
            .map(|s| s.to_string())
            .collect()
    } else {
        vec![]
    };

    // Context after (saturating_add prevents overflow near usize::MAX)
    let context_end = end_idx
        .saturating_add(context)
        .saturating_add(1)
        .min(lines.len());
    let after: Vec<String> = if end_idx + 1 < lines.len() {
        lines[(end_idx + 1)..context_end]
            .iter()
            .map(|s| s.to_string())
            .collect()
    } else {
        vec![]
    };

    Ok((before, after))
}

/// Display unified search results (code + notes)
pub fn display_unified_results(
    results: &[UnifiedResult],
    root: &Path,
    no_content: bool,
    context: Option<usize>,
    parents: Option<&HashMap<String, ParentContext>>,
) -> Result<()> {
    for result in results {
        match result {
            UnifiedResult::Code(r) => {
                // Paths are stored relative; strip_prefix handles legacy absolute paths
                let rel_path = cqs::rel_display(&r.chunk.file, root);

                let parent_tag = if r.chunk.parent_id.is_some() {
                    " [has parent]"
                } else {
                    ""
                };
                let header = format!(
                    "{}:{} ({} {}) [{}] [{:.2}]{}",
                    rel_path,
                    r.chunk.line_start,
                    r.chunk.chunk_type,
                    r.chunk.name,
                    r.chunk.language,
                    r.score,
                    parent_tag
                );

                println!("{}", header.cyan());

                if !no_content {
                    println!("{}", "".repeat(50));

                    // Read context if requested
                    if let Some(n) = context {
                        if n > 0 {
                            let abs_path = root.join(&r.chunk.file);
                            match read_context_lines(
                                &abs_path,
                                r.chunk.line_start,
                                r.chunk.line_end,
                                n,
                            ) {
                                Ok((before, _)) => {
                                    for line in &before {
                                        println!("{}", format!("  {}", line).dimmed());
                                    }
                                }
                                Err(e) => {
                                    tracing::trace!(
                                        error = %e,
                                        file = %abs_path.display(),
                                        "Failed to read context lines (before)"
                                    );
                                }
                            }
                        }
                    }

                    // Show signature or truncated content
                    if r.chunk.content.lines().count() <= 10 {
                        println!("{}", r.chunk.content);
                    } else {
                        for line in r.chunk.content.lines().take(8) {
                            println!("{}", line);
                        }
                        println!("    ...");
                    }

                    // Print after context if requested
                    if let Some(n) = context {
                        if n > 0 {
                            let abs_path = root.join(&r.chunk.file);
                            match read_context_lines(
                                &abs_path,
                                r.chunk.line_start,
                                r.chunk.line_end,
                                n,
                            ) {
                                Ok((_, after)) => {
                                    for line in &after {
                                        println!("{}", format!("  {}", line).dimmed());
                                    }
                                }
                                Err(e) => {
                                    tracing::trace!(
                                        error = %e,
                                        file = %abs_path.display(),
                                        "Failed to read context lines (after)"
                                    );
                                }
                            }
                        }
                    }

                    // Show parent context if --expand
                    if let Some(parent) = parents.and_then(|p| p.get(&r.chunk.id)) {
                        let parent_header = format!(
                            "  Parent context: {} ({}:{}-{})",
                            parent.name, rel_path, parent.line_start, parent.line_end,
                        );
                        println!("{}", parent_header.dimmed());
                        println!("{}", "  ────────────────────────────────".dimmed());
                        for line in parent.content.lines().take(20) {
                            println!("{}", format!("  {}", line).dimmed());
                        }
                        if parent.content.lines().count() > 20 {
                            println!("{}", "  ...".dimmed());
                        }
                    }

                    println!();
                }
            }
        }
    }

    println!("{} results", results.len());
    Ok(())
}

/// Display unified results as JSON
pub fn display_unified_results_json(
    results: &[UnifiedResult],
    query: &str,
    parents: Option<&HashMap<String, ParentContext>>,
    token_info: Option<(usize, usize)>,
) -> Result<()> {
    let json_results: Vec<_> = results
        .iter()
        .map(|r| {
            // Delegate to UnifiedResult::to_json() for the canonical base keys,
            // then layer on parent context fields (CQ-NEW-3).
            let mut obj = r.to_json();
            let UnifiedResult::Code(sr) = r;
            if let Some(parent) = parents.and_then(|p| p.get(&sr.chunk.id)) {
                obj["parent_name"] = serde_json::json!(parent.name);
                obj["parent_content"] = serde_json::json!(parent.content);
                obj["parent_line_start"] = serde_json::json!(parent.line_start);
                obj["parent_line_end"] = serde_json::json!(parent.line_end);
            }
            obj
        })
        .collect();

    let mut output = serde_json::json!({
        "results": json_results,
        "query": query,
        "total": results.len(),
    });
    if let Some((used, budget)) = token_info {
        output["token_count"] = serde_json::json!(used);
        output["token_budget"] = serde_json::json!(budget);
    }

    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

/// Display tagged search results (multi-index with source labels)
pub fn display_tagged_results(
    results: &[TaggedResult],
    root: &Path,
    no_content: bool,
    context: Option<usize>,
    parents: Option<&HashMap<String, ParentContext>>,
) -> Result<()> {
    for tagged in results {
        match &tagged.result {
            UnifiedResult::Code(r) => {
                let rel_path = cqs::rel_display(&r.chunk.file, root);

                // Prepend source name for reference results
                let source_prefix = tagged
                    .source
                    .as_ref()
                    .map(|s| format!("[{}] ", s))
                    .unwrap_or_default();

                let parent_tag = if r.chunk.parent_id.is_some() {
                    " [has parent]"
                } else {
                    ""
                };
                let header = format!(
                    "{}{}:{} ({} {}) [{}] [{:.2}]{}",
                    source_prefix,
                    rel_path,
                    r.chunk.line_start,
                    r.chunk.chunk_type,
                    r.chunk.name,
                    r.chunk.language,
                    r.score,
                    parent_tag
                );

                println!("{}", header.cyan());

                if !no_content {
                    println!("{}", "".repeat(50));

                    // Context lines only for project results (reference source files may not exist)
                    if tagged.source.is_none() {
                        if let Some(n) = context {
                            if n > 0 {
                                let abs_path = root.join(&r.chunk.file);
                                match read_context_lines(
                                    &abs_path,
                                    r.chunk.line_start,
                                    r.chunk.line_end,
                                    n,
                                ) {
                                    Ok((before, _)) => {
                                        for line in &before {
                                            println!("{}", format!("  {}", line).dimmed());
                                        }
                                    }
                                    Err(e) => {
                                        tracing::trace!(
                                            error = %e,
                                            file = %abs_path.display(),
                                            "Failed to read context lines (before)"
                                        );
                                    }
                                }
                            }
                        }
                    }

                    if r.chunk.content.lines().count() <= 10 {
                        println!("{}", r.chunk.content);
                    } else {
                        for line in r.chunk.content.lines().take(8) {
                            println!("{}", line);
                        }
                        println!("    ...");
                    }

                    // After context only for project results
                    if tagged.source.is_none() {
                        if let Some(n) = context {
                            if n > 0 {
                                let abs_path = root.join(&r.chunk.file);
                                match read_context_lines(
                                    &abs_path,
                                    r.chunk.line_start,
                                    r.chunk.line_end,
                                    n,
                                ) {
                                    Ok((_, after)) => {
                                        for line in &after {
                                            println!("{}", format!("  {}", line).dimmed());
                                        }
                                    }
                                    Err(e) => {
                                        tracing::trace!(
                                            error = %e,
                                            file = %abs_path.display(),
                                            "Failed to read context lines (after)"
                                        );
                                    }
                                }
                            }
                        }
                    }

                    // Show parent context if --expand
                    if let Some(parent) = parents.and_then(|p| p.get(&r.chunk.id)) {
                        let parent_header = format!(
                            "  Parent context: {} ({}:{}-{})",
                            parent.name, rel_path, parent.line_start, parent.line_end,
                        );
                        println!("{}", parent_header.dimmed());
                        println!("{}", "  ────────────────────────────────".dimmed());
                        for line in parent.content.lines().take(20) {
                            println!("{}", format!("  {}", line).dimmed());
                        }
                        if parent.content.lines().count() > 20 {
                            println!("{}", "  ...".dimmed());
                        }
                    }

                    println!();
                }
            }
        }
    }

    println!("{} results", results.len());
    Ok(())
}

/// Display similar results as JSON
pub fn display_similar_results_json(
    results: &[cqs::store::SearchResult],
    target: &str,
) -> Result<()> {
    // Delegate to SearchResult::to_json() for canonical base keys.
    // Previously missing `type` and `has_parent` (CQ-NEW-5).
    let json_results: Vec<_> = results.iter().map(|r| r.to_json()).collect();

    let output = serde_json::json!({
        "target": target,
        "results": json_results,
        "total": results.len(),
    });

    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

/// Display tagged results as JSON (multi-index with source field)
pub fn display_tagged_results_json(
    results: &[TaggedResult],
    query: &str,
    parents: Option<&HashMap<String, ParentContext>>,
    token_info: Option<(usize, usize)>,
) -> Result<()> {
    let json_results: Vec<_> = results
        .iter()
        .map(|t| {
            // Delegate to UnifiedResult::to_json() for canonical base keys,
            // then layer on parent context and source fields (CQ-NEW-7).
            let mut json = t.result.to_json();
            let UnifiedResult::Code(sr) = &t.result;
            if let Some(parent) = parents.and_then(|p| p.get(&sr.chunk.id)) {
                json["parent_name"] = serde_json::json!(parent.name);
                json["parent_content"] = serde_json::json!(parent.content);
                json["parent_line_start"] = serde_json::json!(parent.line_start);
                json["parent_line_end"] = serde_json::json!(parent.line_end);
            }
            if let Some(source) = &t.source {
                json["source"] = serde_json::json!(source);
            }
            json
        })
        .collect();

    let mut output = serde_json::json!({
        "results": json_results,
        "query": query,
        "total": results.len(),
    });
    if let Some((used, budget)) = token_info {
        output["token_count"] = serde_json::json!(used);
        output["token_budget"] = serde_json::json!(budget);
    }

    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    // ===== read_context_lines tests (P3-14, P3-18) =====

    /// Creates a temp test file and returns (TempDir, relative_path).
    /// Returns a relative path (just the filename) suitable for the SEC-12
    /// absolute-path guard. The returned TempDir must stay alive for the
    /// duration of the test (drop deletes the dir). The CWD is changed to
    /// the temp dir so the relative path resolves.
    fn write_test_file(lines: &[&str]) -> (tempfile::TempDir, std::path::PathBuf) {
        let dir = tempfile::TempDir::new().unwrap();
        let file_path = dir.path().join("test.rs");
        let content = lines.join("\n");
        std::fs::write(&file_path, &content).unwrap();
        // SEC-12: return absolute path but guard won't fire during tests
        // because we set CWD. Use file_path directly for tests that need
        // to read outside the guard.
        (dir, file_path)
    }

    /// Read context lines bypassing the path guard (for unit tests with temp files).
    fn read_context_lines_test(
        file: &Path,
        line_start: u32,
        line_end: u32,
        context: usize,
    ) -> anyhow::Result<(Vec<String>, Vec<String>)> {
        let content = std::fs::read_to_string(file)
            .with_context(|| format!("Failed to read {}", file.display()))?;
        let lines: Vec<&str> = content.lines().map(|l| l.trim_end_matches('\r')).collect();
        let line_start = line_start.max(1);
        let line_end = line_end.max(line_start);
        let max_idx = lines.len().saturating_sub(1);
        let start_idx = (line_start as usize).saturating_sub(1).min(max_idx);
        let end_idx = (line_end as usize).saturating_sub(1).min(max_idx);
        let context_start = start_idx.saturating_sub(context);
        let before: Vec<String> = if start_idx <= lines.len() {
            lines[context_start..start_idx]
                .iter()
                .map(|s| s.to_string())
                .collect()
        } else {
            vec![]
        };
        let context_end = end_idx
            .saturating_add(context)
            .saturating_add(1)
            .min(lines.len());
        let after: Vec<String> = if end_idx + 1 < lines.len() {
            lines[(end_idx + 1)..context_end]
                .iter()
                .map(|s| s.to_string())
                .collect()
        } else {
            vec![]
        };
        Ok((before, after))
    }

    #[test]
    fn test_read_context_lines_basic() {
        let lines = vec![
            "line 1", "line 2", "line 3", "line 4", "line 5", "line 6", "line 7",
        ];
        let (_dir, path) = write_test_file(&lines);

        // Function at lines 3-5, context=1
        let (before, after) = read_context_lines_test(&path, 3, 5, 1).unwrap();
        assert_eq!(before.len(), 1, "Should have 1 line before");
        assert_eq!(before[0], "line 2");
        assert_eq!(after.len(), 1, "Should have 1 line after");
        assert_eq!(after[0], "line 6");
    }

    #[test]
    fn test_read_context_lines_at_start() {
        let lines = vec!["first", "second", "third", "fourth"];
        let (_dir, path) = write_test_file(&lines);

        // Function at line 1, context=2 -- no before lines available
        let (before, after) = read_context_lines_test(&path, 1, 1, 2).unwrap();
        assert!(before.is_empty(), "No lines before line 1");
        assert_eq!(after.len(), 2, "Should have 2 lines after");
        assert_eq!(after[0], "second");
        assert_eq!(after[1], "third");
    }

    #[test]
    fn test_read_context_lines_at_end() {
        let lines = vec!["first", "second", "third", "last"];
        let (_dir, path) = write_test_file(&lines);

        // Function at last line, context=2
        let (before, after) = read_context_lines_test(&path, 4, 4, 2).unwrap();
        assert_eq!(before.len(), 2, "Should have 2 lines before");
        assert_eq!(before[0], "second");
        assert_eq!(before[1], "third");
        assert!(after.is_empty(), "No lines after last line");
    }

    #[test]
    fn test_read_context_lines_zero_context() {
        let lines = vec!["line 1", "line 2", "line 3"];
        let (_dir, path) = write_test_file(&lines);

        let (before, after) = read_context_lines_test(&path, 2, 2, 0).unwrap();
        assert!(before.is_empty());
        assert!(after.is_empty());
    }

    #[test]
    fn test_read_context_lines_single_line_file() {
        let (_dir, path) = write_test_file(&["only line"]);

        let (before, after) = read_context_lines_test(&path, 1, 1, 5).unwrap();
        assert!(before.is_empty());
        assert!(after.is_empty());
    }

    #[test]
    fn test_read_context_lines_line_zero_normalized() {
        let lines = vec!["first", "second"];
        let (_dir, path) = write_test_file(&lines);

        // line_start=0 should be normalized to 1
        let (before, after) = read_context_lines_test(&path, 0, 1, 1).unwrap();
        assert!(before.is_empty(), "Line 0 normalizes to 1, nothing before");
        assert_eq!(after.len(), 1);
        assert_eq!(after[0], "second");
    }

    #[test]
    fn test_read_context_lines_nonexistent_file() {
        let result = read_context_lines(Path::new("nonexistent/file.rs"), 1, 5, 2);
        assert!(result.is_err(), "Should fail for nonexistent file");
    }

    #[test]
    fn test_read_context_lines_absolute_path_blocked() {
        let result = read_context_lines(Path::new("/etc/passwd"), 1, 5, 2);
        assert!(result.is_err(), "Should block absolute paths");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("Absolute path blocked"),
            "Expected absolute path error, got: {err}"
        );
    }

    #[test]
    fn test_read_context_lines_multi_line_range() {
        let lines = vec!["a", "b", "c", "d", "e", "f", "g", "h"];
        let (_dir, path) = write_test_file(&lines);

        // Function spans lines 3-6, context=1
        let (before, after) = read_context_lines_test(&path, 3, 6, 1).unwrap();
        assert_eq!(before.len(), 1);
        assert_eq!(before[0], "b");
        assert_eq!(after.len(), 1);
        assert_eq!(after[0], "g");
    }

    // ===== HP-7: display_similar_results_json tests =====

    fn make_search_result(
        name: &str,
        score: f32,
        parent_id: Option<&str>,
    ) -> cqs::store::SearchResult {
        cqs::store::SearchResult {
            chunk: cqs::store::ChunkSummary {
                id: format!("id-{name}"),
                file: std::path::PathBuf::from(format!("src/{name}.rs")),
                language: cqs::parser::Language::Rust,
                chunk_type: cqs::parser::ChunkType::Function,
                name: name.to_string(),
                signature: format!("fn {name}()"),
                content: format!("fn {name}() {{}}"),
                doc: None,
                line_start: 10,
                line_end: 20,
                parent_id: parent_id.map(|s| s.to_string()),
                parent_type_name: None,
                content_hash: String::new(),
                window_idx: None,
            },
            score,
        }
    }

    /// Verify display_similar_results_json succeeds with non-empty results.
    /// Output goes to stdout (hard to capture in-process), so we assert Ok(()).
    #[test]
    fn test_display_similar_results_json_returns_ok() {
        let results = vec![
            make_search_result("alpha", 0.95, None),
            make_search_result("beta", 0.80, Some("parent-1")),
        ];
        let result = super::display_similar_results_json(&results, "my_target");
        assert!(
            result.is_ok(),
            "display_similar_results_json should succeed"
        );
    }

    /// Verify display_similar_results_json succeeds with empty results.
    #[test]
    fn test_display_similar_results_json_empty() {
        let results: Vec<cqs::store::SearchResult> = vec![];
        let result = super::display_similar_results_json(&results, "no_matches");
        assert!(result.is_ok(), "should succeed with empty results");
    }

    /// Verify the JSON structure that display_similar_results_json produces.
    /// Since we cannot easily capture println! output, we replicate the same
    /// construction logic and verify field completeness.
    #[test]
    fn test_display_similar_results_json_structure() {
        let results = vec![
            make_search_result("alpha", 0.95, None),
            make_search_result("beta", 0.80, Some("parent-1")),
        ];

        // Use the same canonical to_json() that display_similar_results_json delegates to
        let json_results: Vec<_> = results.iter().map(|r| r.to_json()).collect();

        let output = serde_json::json!({
            "target": "my_target",
            "results": json_results,
            "total": results.len(),
        });

        // Top-level fields
        assert!(output.get("target").is_some(), "missing 'target'");
        assert!(output.get("results").is_some(), "missing 'results'");
        assert!(output.get("total").is_some(), "missing 'total'");
        assert_eq!(output["target"], "my_target");
        assert_eq!(output["total"], 2);

        // Per-result fields
        let arr = output["results"].as_array().unwrap();
        assert_eq!(arr.len(), 2);

        for (i, item) in arr.iter().enumerate() {
            let obj = item.as_object().unwrap_or_else(|| {
                panic!("result[{i}] should be an object");
            });
            for field in [
                "file",
                "line_start",
                "line_end",
                "name",
                "signature",
                "language",
                "chunk_type",
                "score",
                "content",
            ] {
                assert!(
                    obj.contains_key(field),
                    "result[{i}] missing field '{field}'"
                );
            }
        }

        // Verify specific values
        assert_eq!(arr[0]["name"], "alpha");
        assert_eq!(arr[1]["name"], "beta");
        assert_eq!(arr[0]["line_start"], 10);
        assert_eq!(arr[0]["line_end"], 20);
        assert_eq!(arr[0]["language"], "rust");
        assert_eq!(arr[0]["chunk_type"], "function");

        // Score values
        let s0 = arr[0]["score"].as_f64().unwrap();
        assert!((s0 - 0.95).abs() < 1e-4, "alpha score should be ~0.95");
        let s1 = arr[1]["score"].as_f64().unwrap();
        assert!((s1 - 0.80).abs() < 1e-4, "beta score should be ~0.80");
    }
}