cora-code 0.13.0

CLI-first AI code review — BYOK, diff/scan/branch, pre-commit hooks
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! Symbol index engine — persistent SQLite-backed symbol store.
//!
//! Build, query, and maintain a symbol index for code intelligence.
//! Uses regex-based extraction (same approach as `engine/context/extraction.rs`)
//! stored in SQLite with FTS5 for fast full-text search.

#[cfg(feature = "tree-sitter")]
mod ast;
pub mod brain;
mod extract;
pub mod graph;
pub mod schema;
mod symbols;
pub mod vector;

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

use rayon::prelude::*;
use rusqlite::Connection;
#[cfg(test)]
use sha2::{Digest, Sha256};
use tracing::{debug, info};

#[allow(unused_imports)]
pub use graph::{CallEdge, CalleeResult, CallerResult, ImpactNode};
pub use symbols::{SearchResult, SymbolKind, SymbolQuery};

/// Open or create the **global** symbol index database.
///
/// All projects share a single SQLite database at `~/.codecora/cora-code/cora.db`.
/// Project isolation is handled via the `project_id` foreign key.
pub fn open_global_index() -> anyhow::Result<Connection> {
    crate::data_dir::ensure_data_dir()?;
    let db_path = crate::data_dir::graph_db_path();

    let conn = Connection::open(&db_path)?;
    conn.execute_batch(
        "PRAGMA journal_mode=WAL;\
         PRAGMA foreign_keys=ON;\
         PRAGMA synchronous=NORMAL;\
         PRAGMA cache_size=-65536;\
         PRAGMA mmap_size=268435456;\
         PRAGMA temp_store=MEMORY;",
    )?;
    schema::run_migrations(&conn)?;

    debug!("Opened global index at {}", db_path.display());
    Ok(conn)
}
/// Resolve the `project_id` for a given root path, creating the project row if needed.
pub fn ensure_project(conn: &Connection, root: &Path) -> anyhow::Result<i64> {
    let root_str = root.to_string_lossy().to_string();
    schema::get_or_create_project(conn, &root_str)
}

/// Detect the project root by walking up from `start` looking for marker files.
///
/// Search order: `.cora.yaml` → `Cargo.toml` → `package.json` → `.git` (dir or file).
/// Returns the directory containing the first marker found, or `None` if none is found.
pub fn resolve_project_root(start: &Path) -> Option<std::path::PathBuf> {
    let dir = if start.is_file() {
        start.parent()?
    } else {
        start
    };

    const MARKERS: &[&str] = &[".cora.yaml", "Cargo.toml", "package.json", ".git"];

    let mut current = dir.to_path_buf();
    loop {
        for marker in MARKERS {
            let candidate = current.join(marker);
            if candidate.exists() {
                debug!(root = %current.display(), marker, "detected project root");
                return Some(current);
            }
        }
        match current.parent() {
            Some(parent) if parent != current => current = parent.to_path_buf(),
            _ => return None,
        }
    }
}

/// Resolve `project_id` from the current directory, using project root detection.
///
/// Walks up from CWD to find a project root (`.cora.yaml`, `Cargo.toml`, etc.).
/// Falls back to CWD if no marker is found.
pub fn resolve_project_id(conn: &Connection) -> anyhow::Result<(i64, std::path::PathBuf)> {
    let cwd = std::env::current_dir()?;
    let root = resolve_project_root(&cwd).unwrap_or_else(|| cwd.clone());
    let project_id = ensure_project(conn, &root)?;
    Ok((project_id, root))
}

#[cfg(test)]
/// Index a single file: extract symbols and store in the database.
/// Test-only — production uses `index_project_with_id` with batch fingerprinting.
pub fn index_file(
    conn: &Connection,
    project_id: i64,
    file_path: &str,
    content: &str,
    language: &str,
) -> anyhow::Result<usize> {
    let fingerprint = file_fingerprint(content);
    let extracted = extract::extract_all(content, language, file_path);

    let tx = conn.unchecked_transaction()?;
    let count = index_file_in_tx(
        &tx,
        project_id,
        file_path,
        &fingerprint,
        language,
        &extracted,
    )?;

    tx.commit()?;

    debug!(
        "Indexed {file_path}: {count} symbols, {} call edges ({language})",
        extracted.calls.len()
    );
    Ok(count)
}

/// Write extracted data for a single file within an existing transaction.
/// Does NOT commit — caller is responsible for `tx.commit()`.
fn index_file_in_tx(
    tx: &rusqlite::Transaction,
    project_id: i64,
    file_path: &str,
    fingerprint: &str,
    language: &str,
    extracted: &extract::ExtractedAll,
) -> anyhow::Result<usize> {
    // Delete existing symbols for this file within this project
    tx.execute(
        "DELETE FROM symbols WHERE file = ?1 AND project_id = ?2",
        rusqlite::params![file_path, project_id],
    )?;

    // Upsert file fingerprint
    tx.execute(
        "INSERT INTO files (path, fingerprint, last_indexed, language, symbol_count, project_id)
         VALUES (?1, ?2, datetime('now'), ?3, ?4, ?5)
         ON CONFLICT(path) DO UPDATE SET
           fingerprint = excluded.fingerprint,
           last_indexed = excluded.last_indexed,
           language = excluded.language,
           symbol_count = excluded.symbol_count,
           project_id = excluded.project_id",
        rusqlite::params![
            file_path,
            fingerprint,
            language,
            extracted.symbols.len() as i64,
            project_id
        ],
    )?;

    // Batch INSERT symbols using prepared statement
    let count = extracted.symbols.len();
    if !extracted.symbols.is_empty() {
        let mut stmt = tx.prepare(
            "INSERT INTO symbols (name, kind, file, line, signature, language, project_id)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
        )?;
        for sym in &extracted.symbols {
            stmt.execute(rusqlite::params![
                sym.name,
                sym.kind.as_str(),
                sym.file,
                sym.line as i64,
                sym.signature,
                language,
                project_id,
            ])?;
        }
    }

    // Clear and insert call graph edges
    tx.execute(
        "DELETE FROM call_graph WHERE file = ?1 AND project_id = ?2",
        rusqlite::params![file_path, project_id],
    )?;
    if !extracted.calls.is_empty() {
        let mut stmt = tx.prepare(
            "INSERT INTO call_graph (caller, callee, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5)"
        )?;
        for site in &extracted.calls {
            stmt.execute(rusqlite::params![
                site.caller,
                site.callee,
                site.file,
                site.line as i64,
                project_id,
            ])?;
        }
    }

    // Clear and insert knowledge graph edges (tree-sitter only)
    #[cfg(feature = "tree-sitter")]
    {
        tx.execute(
            "DELETE FROM edges WHERE file = ?1 AND project_id = ?2",
            rusqlite::params![file_path, project_id],
        )?;
        if !extracted.kg_edges.is_empty() {
            let mut stmt = tx.prepare(
                "INSERT INTO edges (source, kind, target, file, line, project_id) VALUES (?1, ?2, ?3, ?4, ?5, ?6)"
            )?;
            for e in &extracted.kg_edges {
                stmt.execute(rusqlite::params![
                    e.source,
                    e.kind.as_str(),
                    e.target,
                    e.file,
                    e.line as i64,
                    project_id,
                ])?;
            }
        }
    }

    Ok(count)
}

/// Load all file fingerprints for a project in a single query.
/// Returns `HashMap<file_path, fingerprint>` — O(1) lookup per file
/// instead of N individual DB roundtrips.
fn load_all_fingerprints(
    conn: &Connection,
    project_id: i64,
) -> anyhow::Result<HashMap<String, String>> {
    let mut stmt = conn.prepare("SELECT path, fingerprint FROM files WHERE project_id = ?1")?;
    let map: HashMap<String, String> = stmt
        .query_map([project_id], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
        })?
        .filter_map(|r| r.ok())
        .collect();
    Ok(map)
}

/// Index a project directory, respecting .gitignore.
///
/// Returns summary stats.
pub fn index_project(conn: &Connection, root: &Path, verbose: bool) -> anyhow::Result<IndexStats> {
    index_project_with_skip(conn, root, verbose, None)
}

/// Index a project directory, with config hash invalidation.
///
/// If `skip_patterns` is provided, the config hash is compared against
/// the stored hash in the DB. If they differ, all fingerprints are
/// cleared, forcing a full re-index.
pub fn index_project_with_skip(
    conn: &Connection,
    root: &Path,
    verbose: bool,
    skip_patterns: Option<&[String]>,
) -> anyhow::Result<IndexStats> {
    let project_id = ensure_project(conn, root)?;

    // Config hash invalidation: if skip_patterns changed, clear fingerprints
    if let Some(patterns) = skip_patterns {
        let new_hash = schema::compute_index_config_hash(patterns);
        let stored_hash: Option<String> = conn
            .query_row(
                "SELECT index_config_hash FROM projects WHERE id = ?1",
                rusqlite::params![project_id],
                |row| row.get(0),
            )
            .ok()
            .flatten();

        if stored_hash.as_deref() != Some(&new_hash) {
            if verbose {
                eprintln!("  ↻ Config changed — invalidating fingerprints for full re-index");
            }
            // Clear all fingerprints for this project to force re-index
            conn.execute(
                "DELETE FROM files WHERE project_id = ?1",
                rusqlite::params![project_id],
            )?;
            // Also clear symbols/edges so they're rebuilt from scratch
            conn.execute(
                "DELETE FROM symbols WHERE project_id = ?1",
                rusqlite::params![project_id],
            )?;
            conn.execute(
                "DELETE FROM edges WHERE project_id = ?1",
                rusqlite::params![project_id],
            )?;
            conn.execute(
                "DELETE FROM call_graph WHERE project_id = ?1",
                rusqlite::params![project_id],
            )?;
            // Store the new config hash
            conn.execute(
                "UPDATE projects SET index_config_hash = ?1 WHERE id = ?2",
                rusqlite::params![new_hash, project_id],
            )?;
        }
    }

    index_project_with_id(conn, project_id, root, verbose)
}

/// Internal: index a project with an already-resolved `project_id`.
fn index_project_with_id(
    conn: &Connection,
    project_id: i64,
    root: &Path,
    verbose: bool,
) -> anyhow::Result<IndexStats> {
    let mut stats = IndexStats::default();

    // Collect files to index
    let mut files_to_index: Vec<(String, String, String, String)> = Vec::new(); // (rel_str, content, language, cheap_fp)

    // Batch-load all fingerprints in ONE query — eliminates N per-file DB roundtrips.
    let stored_fingerprints = load_all_fingerprints(conn, project_id).unwrap_or_default();

    let walker = ignore::WalkBuilder::new(root)
        .hidden(true)
        .git_ignore(true)
        .git_exclude(true)
        .build();

    for entry in walker {
        let entry = entry?;
        if !entry.file_type().is_some_and(|ft| ft.is_file()) {
            continue;
        }

        let path = entry.path();
        let rel = path.strip_prefix(root).unwrap_or(path);
        let rel_str = rel.to_string_lossy().to_string();

        let language = crate::engine::diff_parser::detect_language(&rel_str);
        if language == "unknown" || language == "text" {
            continue;
        }

        stats.files_scanned += 1;

        // Compute mtime:size fingerprint — cheap, no file read needed.
        // metadata() is a stat() call, ~microseconds per file.
        let metadata = match std::fs::metadata(path) {
            Ok(m) => m,
            Err(_) => continue,
        };
        let mtime = metadata
            .modified()
            .ok()
            .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let size = metadata.len();
        let cheap_fp = format!("{mtime}:{size}");

        // O(1) hashmap lookup — skip file entirely if unchanged
        if let Some(stored) = stored_fingerprints.get(&rel_str) {
            if *stored == cheap_fp {
                stats.files_skipped += 1;
                continue;
            }
        }

        // Only read file content when we know it changed
        let content = match std::fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        files_to_index.push((rel_str, content, language.to_string(), cheap_fp));
    }

    if !files_to_index.is_empty() {
        // ── Phase 1: Parallel extraction (CPU-bound tree-sitter + regex) ───
        // Rayon par_iter distributes file parsing across all CPU cores.
        // extract_all is CPU-bound (~4ms/file) and fully thread-safe.
        let t_extract = std::time::Instant::now();
        let extracted_files: Vec<(String, extract::ExtractedAll, String, String)> = files_to_index
            .par_iter()
            .map(|(rel_str, content, language, cheap_fp)| {
                let mut extracted = extract::extract_all(content, language, rel_str);
                // Detect HTTP route edges and append to kg_edges
                extracted
                    .kg_edges
                    .extend(extract::detect_routes(content, language, rel_str));
                (
                    rel_str.clone(),
                    extracted,
                    language.clone(),
                    cheap_fp.clone(),
                )
            })
            .collect();
        let extract_ms = t_extract.elapsed().as_millis();

        // ── Phase 2: Serial SQLite writes (single transaction) ─────────
        let t_db = std::time::Instant::now();
        let tx = conn.unchecked_transaction()?;

        // Disable FTS5 triggers during bulk insert to avoid 3x write amplification.
        tx.execute_batch(
            "DROP TRIGGER IF EXISTS symbols_fts_insert;\
             DROP TRIGGER IF EXISTS symbols_fts_delete;\
             DROP TRIGGER IF EXISTS symbols_fts_update;",
        )?;

        for (rel_str, extracted, language, cheap_fp) in &extracted_files {
            match index_file_in_tx(&tx, project_id, rel_str, cheap_fp, language, extracted) {
                Ok(n) => {
                    stats.files_indexed += 1;
                    stats.symbols_indexed += n;
                }
                Err(e) => {
                    stats.errors += 1;
                    if verbose {
                        eprintln!("  ⚠ Failed to index {rel_str}: {e}");
                    }
                }
            }
        }

        // Rebuild FTS5 index from the content table in one shot.
        // 'rebuild' tells FTS5 to discard its data and re-read from the
        // external content table (symbols) — much cheaper than per-row triggers.
        tx.execute("INSERT INTO symbols_fts(symbols_fts) VALUES('rebuild')", [])?;

        // Recreate FTS5 triggers for incremental updates after bulk load
        tx.execute_batch(
            "CREATE TRIGGER IF NOT EXISTS symbols_fts_insert
             AFTER INSERT ON symbols
             BEGIN
                 INSERT INTO symbols_fts(rowid, name, signature, file)
                 VALUES (new.id, new.name, new.signature, new.file);
             END;

             CREATE TRIGGER IF NOT EXISTS symbols_fts_delete
             AFTER DELETE ON symbols
             BEGIN
                 INSERT INTO symbols_fts(symbols_fts, rowid, name, signature, file)
                 VALUES ('delete', old.id, old.name, old.signature, old.file);
             END;

             CREATE TRIGGER IF NOT EXISTS symbols_fts_update
             AFTER UPDATE ON symbols
             BEGIN
                 INSERT INTO symbols_fts(symbols_fts, rowid, name, signature, file)
                 VALUES ('delete', old.id, old.name, old.signature, old.file);
                 INSERT INTO symbols_fts(rowid, name, signature, file)
                 VALUES (new.id, new.name, new.signature, new.file);
             END;",
        )?;

        tx.commit()?;
        tracing::debug!(
            "extract={}ms (rayon), db={}ms, files={}",
            extract_ms,
            t_db.elapsed().as_millis(),
            files_to_index.len()
        );
    }

    // Update project's last_indexed timestamp
    conn.execute(
        "UPDATE projects SET last_indexed = datetime('now') WHERE id = ?1",
        rusqlite::params![project_id],
    )?;

    info!(
        "Index complete: {} files scanned, {} indexed, {} symbols, {} errors",
        stats.files_scanned, stats.files_indexed, stats.symbols_indexed, stats.errors
    );

    // Embed symbols into vector index for brain search — only when files changed
    if stats.files_indexed > 0 {
        match brain::embed_project(conn, project_id) {
            Ok(n) => {
                stats.embedded_symbols = Some(n);
                info!("Brain: embedded {n} symbols");
            }
            Err(e) => {
                if verbose {
                    eprintln!("  ⚠ Embedding failed (non-fatal): {e}");
                }
                tracing::warn!("Embedding failed: {e}");
            }
        }
    }

    Ok(stats)
}

/// Search the symbol index using FTS5 full-text search, scoped to a project.
pub fn search(
    conn: &Connection,
    project_id: i64,
    query: &SymbolQuery,
) -> anyhow::Result<Vec<SearchResult>> {
    symbols::search(conn, project_id, query)
}

/// Get index statistics for a specific project.
pub fn index_stats(conn: &Connection, project_id: i64) -> anyhow::Result<IndexSummary> {
    let total_symbols: i64 = conn.query_row(
        "SELECT COUNT(*) FROM symbols WHERE project_id = ?1",
        rusqlite::params![project_id],
        |row| row.get(0),
    )?;
    let total_files: i64 = conn.query_row(
        "SELECT COUNT(*) FROM files WHERE project_id = ?1",
        rusqlite::params![project_id],
        |row| row.get(0),
    )?;
    let db_size: i64 = {
        let page_size: i64 = conn
            .query_row("PRAGMA page_size", [], |row| row.get(0))
            .unwrap_or(4096);
        let page_count: i64 = conn
            .query_row("PRAGMA page_count", [], |row| row.get(0))
            .unwrap_or(0);
        page_size * page_count
    };

    let mut kind_counts: HashMap<String, usize> = HashMap::new();
    let mut stmt =
        conn.prepare("SELECT kind, COUNT(*) FROM symbols WHERE project_id = ?1 GROUP BY kind")?;
    let rows = stmt.query_map(rusqlite::params![project_id], |row| {
        Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)? as usize))
    })?;
    for row in rows {
        let (kind, count) = row?;
        kind_counts.insert(kind, count);
    }

    let mut lang_counts: HashMap<String, usize> = HashMap::new();
    let mut stmt = conn.prepare(
        "SELECT language, COUNT(*) FROM symbols WHERE project_id = ?1 GROUP BY language",
    )?;
    let rows = stmt.query_map(rusqlite::params![project_id], |row| {
        Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)? as usize))
    })?;
    for row in rows {
        let (lang, count) = row?;
        lang_counts.insert(lang, count);
    }

    Ok(IndexSummary {
        total_symbols: total_symbols as usize,
        total_files: total_files as usize,
        db_size_bytes: db_size as u64,
        symbols_by_kind: kind_counts,
        symbols_by_language: lang_counts,
    })
}

/// Remove symbols for files that no longer exist on disk, scoped to a project.
pub fn prune_deleted(conn: &Connection, project_id: i64, root: &Path) -> anyhow::Result<usize> {
    let mut deleted = 0;

    let mut stmt = conn.prepare("SELECT path FROM files WHERE project_id = ?1")?;
    let paths: Vec<String> = stmt
        .query_map(rusqlite::params![project_id], |row| row.get::<_, String>(0))?
        .filter_map(|r| r.ok())
        .collect();

    let to_prune: Vec<&String> = paths
        .iter()
        .filter(|path| !root.join(path).exists())
        .collect();

    if !to_prune.is_empty() {
        let tx = conn.unchecked_transaction()?;
        for path in &to_prune {
            tx.execute(
                "DELETE FROM symbols WHERE file = ?1 AND project_id = ?2",
                rusqlite::params![path, project_id],
            )?;
            tx.execute(
                "DELETE FROM call_graph WHERE file = ?1 AND project_id = ?2",
                rusqlite::params![path, project_id],
            )?;
            tx.execute(
                "DELETE FROM files WHERE path = ?1 AND project_id = ?2",
                rusqlite::params![path, project_id],
            )?;
        }
        tx.commit()?;
        deleted = to_prune.len();
    }

    if deleted > 0 {
        info!("Pruned {deleted} deleted files from index");
    }

    Ok(deleted)
}

#[cfg(test)]
/// Compute a SHA-256 fingerprint for file content (test-only).
fn file_fingerprint(content: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(content.as_bytes());
    format!("{:x}", hasher.finalize())
}

/// Statistics from an index build run.
#[derive(Debug, Clone, Default)]
pub struct IndexStats {
    pub files_scanned: usize,
    pub files_indexed: usize,
    pub files_skipped: usize,
    pub symbols_indexed: usize,
    pub errors: usize,
    pub embedded_symbols: Option<usize>,
}

/// Summary of the current index state.
#[derive(Debug, Clone)]
pub struct IndexSummary {
    pub total_symbols: usize,
    pub total_files: usize,
    pub db_size_bytes: u64,
    pub symbols_by_kind: HashMap<String, usize>,
    pub symbols_by_language: HashMap<String, usize>,
}

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

    fn mem_conn() -> Connection {
        let conn = Connection::open_in_memory().unwrap();
        conn.execute_batch("PRAGMA foreign_keys=ON;").unwrap();
        schema::run_migrations(&conn).unwrap();
        conn
    }

    /// Create a test project and return its project_id.
    fn test_project(conn: &Connection) -> i64 {
        schema::get_or_create_project(conn, "/tmp/test-project").unwrap()
    }

    #[test]
    fn test_open_and_migrate() {
        let conn = mem_conn();
        // Tables should exist
        let count: i64 = conn
            .query_row("SELECT COUNT(*) FROM symbols", [], |row| row.get(0))
            .unwrap();
        assert_eq!(count, 0);
    }

    #[test]
    fn test_index_rust_file() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        let code = r#"
use std::collections::HashMap;

pub struct Cache {
    inner: HashMap<String, String>,
}

impl Cache {
    pub fn new() -> Self {
        Self { inner: HashMap::new() }
    }

    pub fn get(&self, key: &str) -> Option<&String> {
        self.inner.get(key)
    }
}
"#;
        let count = index_file(&conn, project_id, "src/cache.rs", code, "rs").unwrap();
        assert!(count > 0, "Should extract symbols from Rust code");
    }

    #[test]
    fn test_search() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        let code = r#"
pub fn authenticate(token: &str) -> bool {
    false
}

pub struct AuthService {
    secret: String,
}
"#;
        index_file(&conn, project_id, "src/auth.rs", code, "rs").unwrap();

        let query = SymbolQuery::text("authenticate");
        let results = search(&conn, project_id, &query).unwrap();
        assert!(!results.is_empty());
        assert!(results[0].symbol.name.contains("authenticate"));
    }

    #[test]
    fn test_index_stats() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        index_file(&conn, project_id, "a.rs", "fn foo() {}", "rs").unwrap();
        index_file(&conn, project_id, "b.rs", "struct Bar {}", "rs").unwrap();

        let stats = index_stats(&conn, project_id).unwrap();
        assert!(stats.total_symbols >= 2);
        assert_eq!(stats.total_files, 2);
        assert!(stats.symbols_by_kind.contains_key("function"));
        assert!(stats.symbols_by_kind.contains_key("struct"));
    }

    #[test]
    fn test_prune_deleted() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        index_file(&conn, project_id, "gone.rs", "fn removed() {}", "rs").unwrap();

        // Create temp dir to use as root
        let tmp = tempfile::tempdir().unwrap();
        // gone.rs doesn't exist in temp dir → should be pruned
        let deleted = prune_deleted(&conn, project_id, tmp.path()).unwrap();
        assert_eq!(deleted, 1);

        let stats = index_stats(&conn, project_id).unwrap();
        assert_eq!(stats.total_symbols, 0);
    }

    #[test]
    fn test_reindex_replaces_symbols() {
        let conn = mem_conn();
        let project_id = test_project(&conn);
        index_file(&conn, project_id, "test.rs", "fn old_name() {}", "rs").unwrap();
        index_file(&conn, project_id, "test.rs", "fn new_name() {}", "rs").unwrap();

        let stats = index_stats(&conn, project_id).unwrap();
        // Should have 1 symbol (replaced, not 2)
        assert_eq!(stats.total_symbols, 1);
    }

    #[test]
    fn test_resolve_project_root_finds_cargo_toml() {
        // CWD of the test process is the crate root — Cargo.toml exists here.
        let cwd = std::env::current_dir().unwrap();
        let root = resolve_project_root(&cwd);
        assert!(root.is_some(), "should find project root from CWD");
        let root = root.unwrap();
        assert!(
            root.join("Cargo.toml").exists(),
            "resolved root should contain Cargo.toml"
        );
    }

    #[test]
    fn test_resolve_project_root_finds_cora_yaml() {
        let tmp = tempfile::TempDir::new().unwrap();
        let root = tmp.path().to_path_buf();
        std::fs::write(root.join(".cora.yaml"), "version: 1\n").unwrap();
        let subdir = root.join("src").join("deep").join("nested");
        std::fs::create_dir_all(&subdir).unwrap();
        let resolved = resolve_project_root(&subdir);
        assert_eq!(resolved, Some(root));
    }

    #[test]
    fn test_resolve_project_root_returns_none_in_tmp() {
        // /tmp itself should have no markers (or at least a very deep /tmp subdirectory
        // that we create and then check).
        let tmp = tempfile::TempDir::new().unwrap();
        // Create a deep directory with no markers.
        let deep = tmp.path().join("a").join("b").join("c");
        std::fs::create_dir_all(&deep).unwrap();
        // Walk up from `deep` — tempfile dirs are under /tmp which typically has no
        // Cargo.toml/.cora.yaml/.git. If it does find one we just skip this assertion.
        let resolved = resolve_project_root(&deep);
        // /tmp should not have project markers, but if the test machine has a git
        // repo at /tmp, we can't guarantee this. Only assert if /tmp is clean.
        if !std::path::Path::new("/tmp/.git").exists()
            && !std::path::Path::new("/tmp/Cargo.toml").exists()
            && !std::path::Path::new("/tmp/.cora.yaml").exists()
        {
            assert!(
                resolved.is_none(),
                "should not find project root in empty tmp dir, got {resolved:?}"
            );
        }
    }

    #[test]
    fn test_resolve_project_id_uses_project_root() {
        let conn = mem_conn();
        // resolve_project_id uses CWD — which is the cora-code crate root.
        let (pid, root) = resolve_project_id(&conn).unwrap();
        assert!(pid > 0);
        assert!(
            root.join("Cargo.toml").exists(),
            "resolved root should contain Cargo.toml"
        );
    }
}