codenexus 0.3.4

A queryable code knowledge graph tool built on LadybugDB and tree-sitter
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
// Copyright (c) 2026 Kirky.X. All rights reserved.
// SPDX-License-Identifier: MIT

//! Repository pattern over [`StorageConnection`] (ADD §3.5).
//!
//! Provides CRUD operations on the code knowledge graph, abstracting away the
//! underlying Cypher queries and CSV bulk-loading mechanics. Callers interact
//! with domain types ([`Node`], [`Edge`]) and simple record structs
//! ([`ProjectRecord`], [`FunctionRecord`]) rather than raw query strings.
//!
//! # Multi-project isolation
//!
//! Every node table carries a `project` column (DDD §2.3). All repository
//! read/delete methods accept a `project` parameter and filter on it, ensuring
//! that data from one project never leaks into another (BR-INDEX-004).

use super::capability::Storage;
use super::connection::{SchemaInitReport, StorageConnection};
use super::error::{Result, StorageError};
use super::loader::{load_from_csv, write_csv_temp, write_edges_csv, write_nodes_csv};
use super::schema::{escape_cypher_string, escape_identifier, node_table_columns};
use crate::model::{Edge, Node, NodeLabel};

/// A simplified project record returned by [`Repository::get_project`] and
/// [`Repository::list_projects`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectRecord {
    /// Project node id.
    pub id: String,
    /// Project display name.
    pub name: String,
    /// Repository root path.
    pub root_path: String,
    /// Primary source language.
    pub language: String,
    /// Number of indexed files.
    pub file_count: i64,
    /// Indexing timestamp (unix seconds).
    pub indexed_at: i64,
    /// Git commit hash at index time (empty if not a git repo).
    pub last_commit: String,
}

/// A simplified function record returned by [`Repository::query_functions`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionRecord {
    /// Function node id.
    pub id: String,
    /// Function name.
    pub name: String,
    /// Fully qualified name.
    pub qualified_name: String,
    /// Source file path.
    pub file_path: String,
    /// Start line (1-based).
    pub start_line: i64,
    /// End line (1-based, inclusive).
    pub end_line: i64,
    /// Function signature.
    pub signature: String,
}

/// Repository abstraction providing CRUD operations on the code knowledge
/// graph (ADD §3.5).
///
/// Wraps a [`StorageConnection`] and exposes domain-friendly methods. The
/// connection is owned by the repository; obtain one via
/// [`Repository::new`] or [`Repository::open`].
pub struct Repository {
    conn: StorageConnection,
}

impl Repository {
    /// Creates a new [`Repository`] wrapping the given connection.
    #[must_use]
    pub fn new(conn: StorageConnection) -> Self {
        Self { conn }
    }

    /// Opens (or creates) a LadybugDB database at `path`, initializes the
    /// schema, and returns a [`Repository`] over it.
    pub fn open(path: impl AsRef<std::path::Path>) -> Result<Self> {
        let conn = StorageConnection::open(path)?;
        conn.init_schema()?;
        Ok(Self::new(conn))
    }

    /// Creates an in-memory repository (useful for tests).
    pub fn in_memory() -> Result<Self> {
        let conn = StorageConnection::in_memory()?;
        conn.init_schema()?;
        Ok(Self::new(conn))
    }

    /// Returns a reference to the underlying connection (for advanced callers).
    pub fn connection(&self) -> &StorageConnection {
        &self.conn
    }

    /// Initializes the schema on the underlying connection. Idempotent.
    ///
    /// Returns a [`SchemaInitReport`] describing any DDL statements that were
    /// skipped (unsupported by the linked LadybugDB build, or already present
    /// on re-init).
    pub fn init_schema(&self) -> Result<SchemaInitReport> {
        self.conn.init_schema()
    }

    /// Saves a single `Project` node.
    ///
    /// The node must have `label == NodeLabel::Project`; its `rootPath`,
    /// `fileCount`, and `indexedAt` are read from `node.properties`.
    pub fn save_project(&self, node: &Node) -> Result<()> {
        if node.label != NodeLabel::Project {
            return Err(StorageError::InvalidData(format!(
                "save_project requires NodeLabel::Project, got {}",
                node.label
            )));
        }
        let root_path = str_prop(node, "rootPath");
        let language = node.language.map(|l| l.to_string()).unwrap_or_default();
        let file_count = i64_prop(node, "fileCount");
        let indexed_at = i64_prop(node, "indexedAt");
        let last_commit = str_prop(node, "lastCommit");
        let cypher = format!(
            "CREATE (:Project {{id: '{}', name: '{}', rootPath: '{}', language: '{}', fileCount: {}, indexedAt: {}, lastCommit: '{}'}});",
            escape_cypher_string(&node.id),
            escape_cypher_string(&node.name),
            escape_cypher_string(&root_path),
            escape_cypher_string(&language),
            file_count,
            indexed_at,
            escape_cypher_string(&last_commit),
        );
        self.conn.execute(&cypher)
    }

    /// Bulk-saves nodes of a single label via CSV `COPY FROM` (ADR-014).
    ///
    /// Nodes are grouped by `label` because each label maps to a distinct
    /// table with a different column layout. The `label` field on each node
    /// is not checked — callers are responsible for passing a homogeneous
    /// slice.
    pub fn save_nodes(&self, nodes: &[Node], label: NodeLabel) -> Result<()> {
        if nodes.is_empty() {
            return Ok(());
        }
        let csv = write_nodes_csv(nodes, label);
        let table = label.table_name();
        let safe_id = nodes[0].id.replace(['/', '\\'], "_");
        let file_name = format!("{table}_{safe_id}.csv");
        let csv_path = write_csv_temp(&csv, &file_name)?;
        load_from_csv(&self.conn, table, &csv_path)
    }

    /// Bulk-saves edges via CSV `COPY FROM` into the `CodeRelation` table.
    pub fn save_edges(&self, edges: &[Edge]) -> Result<()> {
        if edges.is_empty() {
            return Ok(());
        }
        let csv = write_edges_csv(edges);
        let csv_path = write_csv_temp(&csv, "coderelation.csv")?;
        load_from_csv(&self.conn, "CodeRelation", &csv_path)
    }

    /// Returns the project with the given id, or `None` if not found.
    pub fn get_project(&self, id: &str) -> Result<Option<ProjectRecord>> {
        let cypher = format!(
            "MATCH (p:Project {{id: '{}'}}) RETURN p.id AS id, p.name AS name, p.rootPath AS rootPath, p.language AS language, p.fileCount AS fileCount, p.indexedAt AS indexedAt, p.lastCommit AS lastCommit;",
            escape_cypher_string(id),
        );
        let rows = self.conn.query(&cypher)?;
        Ok(rows.into_iter().next().map(row_to_project))
    }

    /// Lists all indexed projects.
    pub fn list_projects(&self) -> Result<Vec<ProjectRecord>> {
        let cypher = "MATCH (p:Project) RETURN p.id AS id, p.name AS name, p.rootPath AS rootPath, p.language AS language, p.fileCount AS fileCount, p.indexedAt AS indexedAt, p.lastCommit AS lastCommit ORDER BY p.name;";
        let rows = self.conn.query(cypher)?;
        Ok(rows.into_iter().map(row_to_project).collect())
    }

    /// Deletes a project and every node whose `project` column matches its id.
    ///
    /// Also deletes `CodeRelation` rows belonging to the project. This
    /// implements the multi-project isolation cleanup (BR-INDEX-004).
    pub fn delete_project(&self, project_id: &str) -> Result<()> {
        let escaped = escape_cypher_string(project_id);
        // Delete CodeRelation rows for the project.
        let cypher = format!("MATCH (r:CodeRelation) WHERE r.project = '{escaped}' DELETE r;");
        self.conn.execute(&cypher)?;
        // Delete nodes from every node table that has a `project` column.
        // Project itself is matched by id; all other tables by `project`.
        for label in NodeLabel::all() {
            let table = escape_identifier(label.table_name());
            let cypher = if label == NodeLabel::Project {
                format!("MATCH (n:{table} {{id: '{escaped}'}}) DELETE n;")
            } else {
                format!("MATCH (n:{table}) WHERE n.project = '{escaped}' DELETE n;")
            };
            // Some tables may not exist or the column may be missing; treat
            // those as non-fatal.
            if let Err(err) = self.conn.execute(&cypher) {
                let msg = err.to_string();
                if !msg.contains("does not exist") && !msg.contains("no such") {
                    return Err(err);
                }
            }
        }
        Ok(())
    }

    /// Returns the stored hash for a file in the given project, or `None`.
    ///
    /// Used by the incremental indexer to detect changes (BR-INDEX-001).
    pub fn get_file_hash(&self, file_path: &str, project: &str) -> Result<Option<String>> {
        let cypher = format!(
            "MATCH (f:File) WHERE f.filePath = '{}' AND f.project = '{}' RETURN f.hash AS hash;",
            escape_cypher_string(file_path),
            escape_cypher_string(project),
        );
        let rows = self.conn.query(&cypher)?;
        Ok(rows
            .into_iter()
            .next()
            .and_then(|row| row.into_iter().next())
            .and_then(|v| v.as_str().map(String::from)))
    }

    /// Returns `(file_path, hash)` pairs for every file in the given project.
    ///
    /// Used by the incremental indexer to compute the diff set
    /// (added/changed/deleted) in a single query.
    pub fn get_all_file_hashes(&self, project: &str) -> Result<Vec<(String, String)>> {
        let cypher = format!(
            "MATCH (f:File) WHERE f.project = '{}' RETURN f.filePath AS filePath, f.hash AS hash;",
            escape_cypher_string(project),
        );
        let rows = self.conn.query(&cypher)?;
        let mut out = Vec::with_capacity(rows.len());
        for row in rows {
            let path = row
                .first()
                .and_then(|v| v.as_str())
                .map(String::from)
                .unwrap_or_default();
            let hash = row
                .get(1)
                .and_then(|v| v.as_str())
                .map(String::from)
                .unwrap_or_default();
            out.push((path, hash));
        }
        Ok(out)
    }

    /// Deletes every node whose `filePath` matches `file_path` in the given
    /// project, across all node tables that carry a `filePath` column.
    ///
    /// Also deletes `CodeRelation` rows whose `source` or `target` references
    /// a deleted node. Used by the incremental indexer when a file is removed
    /// or re-parsed (BR-INDEX-002).
    pub fn delete_file_nodes(&self, file_path: &str, project: &str) -> Result<()> {
        let path_escaped = escape_cypher_string(file_path);
        let proj_escaped = escape_cypher_string(project);
        // Collect the ids of nodes belonging to this file so we can clean up
        // CodeRelation rows referencing them.
        let mut orphan_ids: Vec<String> = Vec::new();
        for label in NodeLabel::all() {
            if label == NodeLabel::Project {
                continue;
            }
            // Skip tables without a `filePath` column (e.g. Process, Community,
            // Embedding) — querying `n.filePath` against them raises a binder
            // error. Deterministic column check per Rule 5 instead of relying
            // on error-message matching.
            if !node_table_columns(label).contains(&"filePath") {
                continue;
            }
            let table = escape_identifier(label.table_name());
            // Only tables with a filePath column are affected.
            let select = format!(
                "MATCH (n:{table}) WHERE n.filePath = '{path_escaped}' AND n.project = '{proj_escaped}' RETURN n.id AS id;"
            );
            if let Ok(rows) = self.conn.query(&select) {
                for row in rows {
                    if let Some(id) = row.first().and_then(|v| v.as_str()).map(String::from) {
                        orphan_ids.push(id);
                    }
                }
            }
            let delete = format!(
                "MATCH (n:{table}) WHERE n.filePath = '{path_escaped}' AND n.project = '{proj_escaped}' DELETE n;"
            );
            if let Err(err) = self.conn.execute(&delete) {
                let msg = err.to_string();
                if !msg.contains("does not exist") && !msg.contains("no such") {
                    return Err(err);
                }
            }
        }
        // Delete CodeRelation rows referencing the orphaned node ids.
        if !orphan_ids.is_empty() {
            let id_list = orphan_ids
                .iter()
                .map(|id| format!("'{}'", escape_cypher_string(id)))
                .collect::<Vec<_>>()
                .join(", ");
            let cypher = format!(
                "MATCH (r:CodeRelation) WHERE r.source IN [{id_list}] OR r.target IN [{id_list}] DELETE r;"
            );
            if let Err(err) = self.conn.execute(&cypher) {
                let msg = err.to_string();
                if !msg.contains("does not exist") && !msg.contains("no such") {
                    return Err(err);
                }
            }
        }
        Ok(())
    }

    /// Batch version of [`delete_file_nodes`](Self::delete_file_nodes) that
    /// removes nodes for multiple file paths in a single pass over the node
    /// labels, instead of one pass per file.
    ///
    /// # Performance motivation
    ///
    /// `delete_file_nodes` runs ~21 Cypher queries per file (one SELECT +
    /// one DELETE per label with a `filePath` column, plus one CodeRelation
    /// DELETE). For an incremental re-index touching 500 of 1000 files that
    /// is 10 500 queries — the dominant cost behind the
    /// `incremental_500_of_1000` bench SLO violation (33 files/s vs the
    /// PRD ≥100 files/s target).
    ///
    /// This batch variant collapses the per-file loop into a single
    /// `WHERE n.filePath IN [...]` pass, keeping the query count fixed at
    /// ~21 regardless of how many files are deleted.
    ///
    /// # Arguments
    ///
    /// * `paths` - Relative file paths whose nodes should be removed.
    /// * `project` - Project id isolating the delete (BR-INDEX-004).
    ///
    /// # Errors
    ///
    /// Returns [`StorageError`] if a non-"table missing" delete fails. Missing
    /// tables are tolerated (same tolerance as `delete_file_nodes`) so the
    /// batch path works on schemas that have not yet created every label
    /// table.
    pub fn delete_file_nodes_batch(&self, paths: &[String], project: &str) -> Result<()> {
        if paths.is_empty() {
            return Ok(());
        }
        let proj_escaped = escape_cypher_string(project);
        let path_list = paths
            .iter()
            .map(|p| format!("'{}'", escape_cypher_string(p)))
            .collect::<Vec<_>>()
            .join(", ");

        let mut orphan_ids: Vec<String> = Vec::new();
        for label in NodeLabel::all() {
            if label == NodeLabel::Project {
                continue;
            }
            if !node_table_columns(label).contains(&"filePath") {
                continue;
            }
            let table = escape_identifier(label.table_name());
            let select = format!(
                "MATCH (n:{table}) WHERE n.filePath IN [{path_list}] AND n.project = '{proj_escaped}' RETURN n.id AS id;"
            );
            if let Ok(rows) = self.conn.query(&select) {
                for row in rows {
                    if let Some(id) = row.first().and_then(|v| v.as_str()).map(String::from) {
                        orphan_ids.push(id);
                    }
                }
            }
            let delete = format!(
                "MATCH (n:{table}) WHERE n.filePath IN [{path_list}] AND n.project = '{proj_escaped}' DELETE n;"
            );
            if let Err(err) = self.conn.execute(&delete) {
                let msg = err.to_string();
                if !msg.contains("does not exist") && !msg.contains("no such") {
                    return Err(err);
                }
            }
        }
        if !orphan_ids.is_empty() {
            let id_list = orphan_ids
                .iter()
                .map(|id| format!("'{}'", escape_cypher_string(id)))
                .collect::<Vec<_>>()
                .join(", ");
            let cypher = format!(
                "MATCH (r:CodeRelation) WHERE r.source IN [{id_list}] OR r.target IN [{id_list}] DELETE r;"
            );
            if let Err(err) = self.conn.execute(&cypher) {
                let msg = err.to_string();
                if !msg.contains("does not exist") && !msg.contains("no such") {
                    return Err(err);
                }
            }
        }
        Ok(())
    }

    /// Returns all functions in the given project.
    ///
    /// Functions are ordered by `qualifiedName` for deterministic output.
    pub fn query_functions(&self, project: &str) -> Result<Vec<FunctionRecord>> {
        let cypher = format!(
            "MATCH (f:Function) WHERE f.project = '{}' RETURN f.id AS id, f.name AS name, f.qualifiedName AS qualifiedName, f.filePath AS filePath, f.startLine AS startLine, f.endLine AS endLine, f.signature AS signature ORDER BY f.qualifiedName;",
            escape_cypher_string(project),
        );
        let rows = self.conn.query(&cypher)?;
        Ok(rows.into_iter().map(row_to_function).collect())
    }
}

/// Delegates the [`Storage`] capability trait to a bare [`Repository`].
///
/// This exists so callers which need a FRESH `Repository` (opened after
/// writes occurred) can pass it to APIs expecting `&dyn Storage` — most
/// notably [`QualityChecker`](super::QualityChecker) in `index_cmd::run`.
/// The Kit's storage capability wraps a `Repository` opened at boot; using
/// it after indexing would read a stale MVCC snapshot and risk "checkpoint
/// interference on drop" (the stale Repository's destructor may flush its
/// empty view over the indexer's writes). Opening a new `Repository` and
/// passing it as `&dyn Storage` avoids both problems.
///
/// # Thread-safety
///
/// This impl does NOT lock a mutex (unlike the Kit's storage capability,
/// which wraps `Repository` in a `Mutex`). It is safe for single-threaded
/// consumers such as `QualityChecker`. Multi-threaded consumers must use
/// the Kit's mutex-guarded capability instead.
impl Storage for Repository {
    fn init_schema(&self) -> std::result::Result<SchemaInitReport, StorageError> {
        Repository::init_schema(self)
    }

    fn execute(&self, cypher: &str) -> std::result::Result<(), StorageError> {
        self.connection().execute(cypher)
    }

    fn query(
        &self,
        cypher: &str,
    ) -> std::result::Result<Vec<Vec<serde_json::Value>>, StorageError> {
        self.connection().query(cypher)
    }

    fn save_project(&self, node: &Node) -> std::result::Result<(), StorageError> {
        Repository::save_project(self, node)
    }

    fn save_nodes(
        &self,
        nodes: &[Node],
        label: NodeLabel,
    ) -> std::result::Result<(), StorageError> {
        Repository::save_nodes(self, nodes, label)
    }

    fn save_edges(&self, edges: &[Edge]) -> std::result::Result<(), StorageError> {
        Repository::save_edges(self, edges)
    }

    fn get_project(&self, id: &str) -> std::result::Result<Option<ProjectRecord>, StorageError> {
        Repository::get_project(self, id)
    }

    fn list_projects(&self) -> std::result::Result<Vec<ProjectRecord>, StorageError> {
        Repository::list_projects(self)
    }

    fn query_functions(
        &self,
        project: &str,
    ) -> std::result::Result<Vec<FunctionRecord>, StorageError> {
        Repository::query_functions(self, project)
    }

    fn get_file_hash(
        &self,
        file_path: &str,
        project: &str,
    ) -> std::result::Result<Option<String>, StorageError> {
        Repository::get_file_hash(self, file_path, project)
    }

    fn get_all_file_hashes(
        &self,
        project: &str,
    ) -> std::result::Result<Vec<(String, String)>, StorageError> {
        Repository::get_all_file_hashes(self, project)
    }

    fn delete_project(&self, project_id: &str) -> std::result::Result<(), StorageError> {
        Repository::delete_project(self, project_id)
    }

    fn delete_file_nodes(
        &self,
        file_path: &str,
        project: &str,
    ) -> std::result::Result<(), StorageError> {
        Repository::delete_file_nodes(self, file_path, project)
    }
}

/// Extracts a string property from a node's `properties` JSON.
fn str_prop(node: &Node, key: &str) -> String {
    node.properties
        .get(key)
        .and_then(|v| v.as_str())
        .map(String::from)
        .unwrap_or_default()
}

/// Extracts an integer property from a node's `properties` JSON.
fn i64_prop(node: &Node, key: &str) -> i64 {
    node.properties
        .get(key)
        .and_then(|v| v.as_i64())
        .unwrap_or(0)
}

/// Converts a query row into a [`ProjectRecord`].
fn row_to_project(row: Vec<serde_json::Value>) -> ProjectRecord {
    let get_str = |idx: usize| -> String {
        row.get(idx)
            .and_then(|v| v.as_str())
            .map(String::from)
            .unwrap_or_default()
    };
    let get_i64 = |idx: usize| -> i64 { row.get(idx).and_then(|v| v.as_i64()).unwrap_or(0) };
    ProjectRecord {
        id: get_str(0),
        name: get_str(1),
        root_path: get_str(2),
        language: get_str(3),
        file_count: get_i64(4),
        indexed_at: get_i64(5),
        last_commit: get_str(6),
    }
}

/// Converts a query row into a [`FunctionRecord`].
fn row_to_function(row: Vec<serde_json::Value>) -> FunctionRecord {
    let get_str = |idx: usize| -> String {
        row.get(idx)
            .and_then(|v| v.as_str())
            .map(String::from)
            .unwrap_or_default()
    };
    let get_i64 = |idx: usize| -> i64 { row.get(idx).and_then(|v| v.as_i64()).unwrap_or(0) };
    FunctionRecord {
        id: get_str(0),
        name: get_str(1),
        qualified_name: get_str(2),
        file_path: get_str(3),
        start_line: get_i64(4),
        end_line: get_i64(5),
        signature: get_str(6),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{EdgeType, Language};

    /// Creates a fresh in-memory repository with the schema initialized.
    fn fresh_repo() -> Repository {
        Repository::in_memory().expect("in_memory repository")
    }

    /// Builds a sample Project node.
    fn sample_project(id: &str, name: &str) -> Node {
        Node::builder(NodeLabel::Project, name, name)
            .id(id)
            .language(Language::Rust)
            .properties(serde_json::json!({
                "rootPath": "/repo/".to_string() + name,
                "fileCount": 10,
                "indexedAt": 1_700_000_000,
            }))
            .build()
    }

    /// Builds a sample File node.
    fn sample_file(id: &str, project: &str, path: &str, hash: &str) -> Node {
        Node::builder(NodeLabel::File, path, path)
            .id(id)
            .project(project)
            .file_path(path)
            .language(Language::Rust)
            .properties(serde_json::json!({"hash": hash, "lineCount": 100}))
            .build()
    }

    /// Builds a sample Function node.
    fn sample_function(id: &str, project: &str, name: &str, qn: &str) -> Node {
        Node::builder(NodeLabel::Function, name, qn)
            .id(id)
            .project(project)
            .file_path("/src/main.rs")
            .start_line(1)
            .end_line(10)
            .signature("fn main()")
            .build()
    }

    // --- save_project / get_project ---

    #[test]
    fn save_project_persists_node() {
        let repo = fresh_repo();
        let node = sample_project("proj_1", "demo");
        repo.save_project(&node).expect("save_project");

        let fetched = repo.get_project("proj_1").expect("get_project");
        assert!(fetched.is_some());
        let rec = fetched.unwrap();
        assert_eq!(rec.id, "proj_1");
        assert_eq!(rec.name, "demo");
        assert_eq!(rec.root_path, "/repo/demo");
        assert_eq!(rec.language, "rust");
        assert_eq!(rec.file_count, 10);
        assert_eq!(rec.indexed_at, 1_700_000_000);
    }

    #[test]
    fn get_project_returns_none_when_missing() {
        let repo = fresh_repo();
        let fetched = repo.get_project("does_not_exist").expect("get_project");
        assert!(fetched.is_none());
    }

    #[test]
    fn save_project_rejects_non_project_label() {
        let repo = fresh_repo();
        let node = Node::builder(NodeLabel::Function, "f", "qn")
            .id("f1")
            .build();
        let err = repo.save_project(&node);
        assert!(err.is_err());
        assert!(err.unwrap_err().to_string().contains("Project"));
    }

    #[test]
    fn save_project_escapes_single_quotes_in_name() {
        let repo = fresh_repo();
        let node = Node::builder(NodeLabel::Project, "it's demo", "qn")
            .id("p1")
            .properties(serde_json::json!({"rootPath": "/", "fileCount": 0, "indexedAt": 0}))
            .build();
        repo.save_project(&node).expect("save_project");

        let rec = repo.get_project("p1").unwrap().unwrap();
        assert_eq!(rec.name, "it's demo");
    }

    // --- list_projects ---

    #[test]
    fn list_projects_returns_all_projects_ordered_by_name() {
        let repo = fresh_repo();
        repo.save_project(&sample_project("p1", "zeta")).unwrap();
        repo.save_project(&sample_project("p2", "alpha")).unwrap();
        repo.save_project(&sample_project("p3", "mid")).unwrap();

        let projects = repo.list_projects().expect("list_projects");
        assert_eq!(projects.len(), 3);
        assert_eq!(projects[0].name, "alpha");
        assert_eq!(projects[1].name, "mid");
        assert_eq!(projects[2].name, "zeta");
    }

    #[test]
    fn list_projects_empty_when_none() {
        let repo = fresh_repo();
        let projects = repo.list_projects().expect("list_projects");
        assert!(projects.is_empty());
    }

    // --- save_nodes / save_edges ---

    #[test]
    fn save_nodes_loads_function_nodes() {
        let repo = fresh_repo();
        let nodes = vec![
            sample_function("f1", "demo", "main", "demo.main"),
            sample_function("f2", "demo", "helper", "demo.helper"),
        ];
        repo.save_nodes(&nodes, NodeLabel::Function)
            .expect("save_nodes");

        let funcs = repo.query_functions("demo").expect("query_functions");
        assert_eq!(funcs.len(), 2);
        assert_eq!(funcs[0].name, "helper");
        assert_eq!(funcs[1].name, "main");
    }

    #[test]
    fn save_nodes_empty_slice_is_noop() {
        let repo = fresh_repo();
        let result = repo.save_nodes(&[], NodeLabel::Function);
        assert!(result.is_ok());
    }

    #[test]
    fn save_nodes_handles_macro_label() {
        let repo = fresh_repo();
        let node = Node::builder(NodeLabel::Macro, "M", "demo.M")
            .id("m1")
            .project("demo")
            .start_line(1)
            .end_line(2)
            .signature("#define M x")
            .properties(serde_json::json!({"content": "#define M x"}))
            .build();
        repo.save_nodes(&[node], NodeLabel::Macro)
            .expect("save_nodes Macro");

        let rows = repo
            .connection()
            .query("MATCH (m:`Macro`) RETURN m.name AS name;")
            .expect("query Macro");
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], serde_json::json!("M"));
    }

    #[test]
    fn save_edges_loads_code_relations() {
        let repo = fresh_repo();
        let edges = vec![
            Edge::builder("f1", "f2", EdgeType::Calls, "demo")
                .confidence(0.9)
                .start_line(5)
                .build(),
            Edge::builder("f2", "f3", EdgeType::Calls, "demo")
                .confidence(0.8)
                .build(),
        ];
        repo.save_edges(&edges).expect("save_edges");

        let rows = repo
            .connection()
            .query("MATCH (r:CodeRelation) RETURN r.type AS type ORDER BY r.id;")
            .expect("query CodeRelation");
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0][0], serde_json::json!("CALLS"));
    }

    #[test]
    fn save_edges_empty_slice_is_noop() {
        let repo = fresh_repo();
        let result = repo.save_edges(&[]);
        assert!(result.is_ok());
    }

    // --- delete_project ---

    #[test]
    fn delete_project_removes_project_and_related_nodes() {
        let repo = fresh_repo();
        repo.save_project(&sample_project("demo", "demo")).unwrap();
        repo.save_nodes(
            &[sample_function("f1", "demo", "main", "demo.main")],
            NodeLabel::Function,
        )
        .unwrap();
        repo.save_nodes(
            &[sample_file("file_1", "demo", "/src/main.rs", "abc")],
            NodeLabel::File,
        )
        .unwrap();

        // Sanity check: data is present.
        assert!(repo.get_project("demo").unwrap().is_some());
        assert_eq!(repo.query_functions("demo").unwrap().len(), 1);

        repo.delete_project("demo").expect("delete_project");

        // Project and its nodes are gone.
        assert!(repo.get_project("demo").unwrap().is_none());
        assert!(repo.query_functions("demo").unwrap().is_empty());

        let file_rows = repo
            .connection()
            .query("MATCH (f:File) RETURN f.id AS id;")
            .unwrap();
        assert!(file_rows.is_empty());
    }

    #[test]
    fn delete_project_only_removes_specified_project() {
        let repo = fresh_repo();
        repo.save_project(&sample_project("alpha", "alpha"))
            .unwrap();
        repo.save_project(&sample_project("beta", "beta")).unwrap();
        repo.save_nodes(
            &[sample_function("f1", "alpha", "main", "alpha.main")],
            NodeLabel::Function,
        )
        .unwrap();
        repo.save_nodes(
            &[sample_function("f2", "beta", "main", "beta.main")],
            NodeLabel::Function,
        )
        .unwrap();

        repo.delete_project("alpha").expect("delete_project");

        // alpha is gone, beta remains.
        assert!(repo.get_project("alpha").unwrap().is_none());
        assert!(repo.get_project("beta").unwrap().is_some());
        assert!(repo.query_functions("alpha").unwrap().is_empty());
        assert_eq!(repo.query_functions("beta").unwrap().len(), 1);
    }

    #[test]
    fn delete_project_nonexistent_is_noop() {
        let repo = fresh_repo();
        // Should not error even though the project doesn't exist.
        let result = repo.delete_project("never_existed");
        assert!(result.is_ok());
    }

    // --- get_file_hash / get_all_file_hashes ---

    #[test]
    fn get_file_hash_returns_stored_hash() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_file("file_1", "demo", "/src/main.rs", "sha256:abc")],
            NodeLabel::File,
        )
        .unwrap();

        let hash = repo
            .get_file_hash("/src/main.rs", "demo")
            .expect("get_file_hash");
        assert_eq!(hash.as_deref(), Some("sha256:abc"));
    }

    #[test]
    fn get_file_hash_returns_none_when_missing() {
        let repo = fresh_repo();
        let hash = repo
            .get_file_hash("/nope.rs", "demo")
            .expect("get_file_hash");
        assert!(hash.is_none());
    }

    #[test]
    fn get_file_hash_isolates_by_project() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_file("f1", "alpha", "/src/main.rs", "hash_alpha")],
            NodeLabel::File,
        )
        .unwrap();
        repo.save_nodes(
            &[sample_file("f2", "beta", "/src/main.rs", "hash_beta")],
            NodeLabel::File,
        )
        .unwrap();

        // Same path, different projects → different hashes.
        assert_eq!(
            repo.get_file_hash("/src/main.rs", "alpha")
                .unwrap()
                .as_deref(),
            Some("hash_alpha")
        );
        assert_eq!(
            repo.get_file_hash("/src/main.rs", "beta")
                .unwrap()
                .as_deref(),
            Some("hash_beta")
        );
    }

    #[test]
    fn get_all_file_hashes_returns_all_files_for_project() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_file("f1", "demo", "/a.rs", "hash_a"),
                sample_file("f2", "demo", "/b.rs", "hash_b"),
                sample_file("f3", "demo", "/c.rs", "hash_c"),
            ],
            NodeLabel::File,
        )
        .unwrap();
        // A file in another project should not appear.
        repo.save_nodes(
            &[sample_file("f4", "other", "/d.rs", "hash_d")],
            NodeLabel::File,
        )
        .unwrap();

        let hashes = repo
            .get_all_file_hashes("demo")
            .expect("get_all_file_hashes");
        assert_eq!(hashes.len(), 3);
        let paths: Vec<&str> = hashes.iter().map(|(p, _)| p.as_str()).collect();
        assert!(paths.contains(&"/a.rs"));
        assert!(paths.contains(&"/b.rs"));
        assert!(paths.contains(&"/c.rs"));
        assert!(!paths.contains(&"/d.rs"));
    }

    #[test]
    fn get_all_file_hashes_empty_when_no_files() {
        let repo = fresh_repo();
        let hashes = repo
            .get_all_file_hashes("demo")
            .expect("get_all_file_hashes");
        assert!(hashes.is_empty());
    }

    // --- delete_file_nodes ---

    #[test]
    fn delete_file_nodes_removes_nodes_for_file() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "main", "demo.main"),
                sample_function("f2", "demo", "helper", "demo.helper"),
            ],
            NodeLabel::Function,
        )
        .unwrap();

        // Both functions are in /src/main.rs; deleting that file removes both.
        repo.delete_file_nodes("/src/main.rs", "demo")
            .expect("delete_file_nodes");
        assert!(repo.query_functions("demo").unwrap().is_empty());
    }

    #[test]
    fn delete_file_nodes_isolates_by_project() {
        let repo = fresh_repo();
        // Same path, different projects.
        repo.save_nodes(
            &[sample_function("f1", "alpha", "main", "alpha.main")],
            NodeLabel::Function,
        )
        .unwrap();
        repo.save_nodes(
            &[sample_function("f2", "beta", "main", "beta.main")],
            NodeLabel::Function,
        )
        .unwrap();

        repo.delete_file_nodes("/src/main.rs", "alpha")
            .expect("delete_file_nodes");
        assert!(repo.query_functions("alpha").unwrap().is_empty());
        assert_eq!(repo.query_functions("beta").unwrap().len(), 1);
    }

    #[test]
    fn delete_file_nodes_also_removes_related_edges() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "main", "demo.main"),
                sample_function("f2", "demo", "helper", "demo.helper"),
            ],
            NodeLabel::Function,
        )
        .unwrap();
        repo.save_edges(&[Edge::builder("f1", "f2", EdgeType::Calls, "demo")
            .start_line(3)
            .build()])
            .unwrap();

        // Sanity: edge exists.
        let rows = repo
            .connection()
            .query("MATCH (r:CodeRelation) RETURN count(r) AS cnt;")
            .unwrap();
        assert_eq!(rows[0][0], serde_json::json!(1));

        repo.delete_file_nodes("/src/main.rs", "demo")
            .expect("delete_file_nodes");

        // Edge referencing the deleted nodes is gone.
        let rows = repo
            .connection()
            .query("MATCH (r:CodeRelation) RETURN count(r) AS cnt;")
            .unwrap();
        assert_eq!(rows[0][0], serde_json::json!(0));
    }

    #[test]
    fn delete_file_nodes_nonexistent_is_noop() {
        let repo = fresh_repo();
        let result = repo.delete_file_nodes("/nope.rs", "demo");
        assert!(result.is_ok());
    }

    // --- query_functions ---

    #[test]
    fn query_functions_returns_functions_ordered_by_qn() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "zeta", "demo.zeta"),
                sample_function("f2", "demo", "alpha", "demo.alpha"),
                sample_function("f3", "demo", "mid", "demo.mid"),
            ],
            NodeLabel::Function,
        )
        .unwrap();

        let funcs = repo.query_functions("demo").expect("query_functions");
        assert_eq!(funcs.len(), 3);
        assert_eq!(funcs[0].qualified_name, "demo.alpha");
        assert_eq!(funcs[1].qualified_name, "demo.mid");
        assert_eq!(funcs[2].qualified_name, "demo.zeta");
    }

    #[test]
    fn query_functions_isolates_by_project() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function("f1", "alpha", "main", "alpha.main")],
            NodeLabel::Function,
        )
        .unwrap();
        repo.save_nodes(
            &[sample_function("f2", "beta", "main", "beta.main")],
            NodeLabel::Function,
        )
        .unwrap();

        assert_eq!(repo.query_functions("alpha").unwrap().len(), 1);
        assert_eq!(repo.query_functions("beta").unwrap().len(), 1);
        assert_eq!(repo.query_functions("gamma").unwrap().len(), 0);
    }

    #[test]
    fn query_functions_empty_when_none() {
        let repo = fresh_repo();
        let funcs = repo.query_functions("demo").expect("query_functions");
        assert!(funcs.is_empty());
    }

    // --- multi-project isolation (BR-INDEX-004) ---

    #[test]
    fn multi_project_isolation_br_index_004() {
        // Two projects coexist; querying/deleting one never affects the other.
        let repo = fresh_repo();
        repo.save_project(&sample_project("alpha", "alpha"))
            .unwrap();
        repo.save_project(&sample_project("beta", "beta")).unwrap();

        repo.save_nodes(
            &[
                sample_function("a1", "alpha", "main", "alpha.main"),
                sample_function("a2", "alpha", "util", "alpha.util"),
            ],
            NodeLabel::Function,
        )
        .unwrap();
        repo.save_nodes(
            &[
                sample_function("b1", "beta", "main", "beta.main"),
                sample_function("b2", "beta", "util", "beta.util"),
                sample_function("b3", "beta", "extra", "beta.extra"),
            ],
            NodeLabel::Function,
        )
        .unwrap();

        // Each project sees only its own functions.
        assert_eq!(repo.query_functions("alpha").unwrap().len(), 2);
        assert_eq!(repo.query_functions("beta").unwrap().len(), 3);

        // Deleting alpha leaves beta untouched.
        repo.delete_project("alpha").expect("delete_project alpha");
        assert!(repo.get_project("alpha").unwrap().is_none());
        assert!(repo.get_project("beta").unwrap().is_some());
        assert_eq!(repo.query_functions("alpha").unwrap().len(), 0);
        assert_eq!(repo.query_functions("beta").unwrap().len(), 3);

        // list_projects reflects the deletion.
        let projects = repo.list_projects().unwrap();
        assert_eq!(projects.len(), 1);
        assert_eq!(projects[0].id, "beta");
    }

    // --- open / connection / init_schema ---

    #[test]
    fn open_creates_repository_with_schema() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("testdb");
        let repo = Repository::open(&path).expect("Repository::open");
        std::mem::forget(dir);

        // Schema is initialized: querying an empty Project table works.
        let projects = repo.list_projects().expect("list_projects");
        assert!(projects.is_empty());
    }

    #[test]
    fn connection_returns_underlying_handle() {
        let repo = fresh_repo();
        let _ = repo.connection();
    }

    #[test]
    fn init_schema_is_idempotent() {
        let repo = fresh_repo();
        repo.init_schema().expect("first init_schema");
        repo.init_schema().expect("second init_schema");
    }

    // --- helpers ---

    #[test]
    fn project_record_equality() {
        let a = ProjectRecord {
            id: "p1".into(),
            name: "demo".into(),
            root_path: "/".into(),
            language: "rust".into(),
            file_count: 1,
            indexed_at: 2,
            last_commit: "abc123".into(),
        };
        let b = a.clone();
        assert_eq!(a, b);
    }

    #[test]
    fn function_record_debug() {
        let rec = FunctionRecord {
            id: "f1".into(),
            name: "main".into(),
            qualified_name: "demo.main".into(),
            file_path: "/src/main.rs".into(),
            start_line: 1,
            end_line: 10,
            signature: "fn main()".into(),
        };
        let s = format!("{rec:?}");
        assert!(s.contains("FunctionRecord"));
        assert!(s.contains("main"));
    }

    // --- Storage trait impl on Repository (delegation coverage) ---

    #[test]
    fn repository_impl_storage_init_schema_works() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        let report = storage
            .init_schema()
            .expect("init_schema via Storage trait");
        // Idempotent — schema already initialized by fresh_repo.
        let _ = report.skipped_count;
    }

    #[test]
    fn repository_impl_storage_execute_and_query() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        storage
            .execute("CREATE (:Project {id: 'p1', name: 'demo', rootPath: '/', language: 'rust', fileCount: 0, indexedAt: 0, lastCommit: ''});")
            .expect("execute via Storage trait");
        let rows = storage
            .query("MATCH (p:Project) RETURN p.name AS name;")
            .expect("query via Storage trait");
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0].as_str(), Some("demo"));
    }

    #[test]
    fn repository_impl_storage_save_and_get_project() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        storage
            .save_project(&sample_project("demo", "demo"))
            .expect("save_project");
        let proj = storage.get_project("demo").expect("get_project");
        assert!(proj.is_some());
        assert_eq!(proj.unwrap().name, "demo");
    }

    #[test]
    fn repository_impl_storage_list_projects() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        storage
            .save_project(&sample_project("alpha", "alpha"))
            .unwrap();
        storage
            .save_project(&sample_project("beta", "beta"))
            .unwrap();
        let projects = storage.list_projects().expect("list_projects");
        assert_eq!(projects.len(), 2);
    }

    #[test]
    fn repository_impl_storage_save_nodes_and_query_functions() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        storage
            .save_project(&sample_project("demo", "demo"))
            .unwrap();
        let nodes = vec![sample_function("f1", "demo", "main", "demo.main")];
        storage
            .save_nodes(&nodes, NodeLabel::Function)
            .expect("save_nodes");
        let funcs = storage.query_functions("demo").expect("query_functions");
        assert_eq!(funcs.len(), 1);
        assert_eq!(funcs[0].name, "main");
    }

    #[test]
    fn repository_impl_storage_save_edges() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        let edge = Edge::builder("s", "t", EdgeType::Calls, "demo")
            .start_line(1)
            .build();
        storage.save_edges(&[edge]).expect("save_edges");
        let rows = storage
            .query("MATCH (r:CodeRelation) RETURN r.source AS src;")
            .expect("query edges");
        assert_eq!(rows.len(), 1);
    }

    #[test]
    fn repository_impl_storage_get_file_hash() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        storage
            .save_project(&sample_project("demo", "demo"))
            .unwrap();
        storage
            .save_nodes(
                &[sample_file("f1", "demo", "/src/main.rs", "abc123")],
                NodeLabel::File,
            )
            .unwrap();
        let hash = storage
            .get_file_hash("/src/main.rs", "demo")
            .expect("get_file_hash");
        assert_eq!(hash.as_deref(), Some("abc123"));
    }

    #[test]
    fn repository_impl_storage_get_all_file_hashes() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        storage
            .save_project(&sample_project("demo", "demo"))
            .unwrap();
        storage
            .save_nodes(
                &[sample_file("f1", "demo", "/src/a.rs", "hash1")],
                NodeLabel::File,
            )
            .unwrap();
        storage
            .save_nodes(
                &[sample_file("f2", "demo", "/src/b.rs", "hash2")],
                NodeLabel::File,
            )
            .unwrap();
        let hashes = storage
            .get_all_file_hashes("demo")
            .expect("get_all_file_hashes");
        assert_eq!(hashes.len(), 2);
    }

    #[test]
    fn repository_impl_storage_delete_project() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        storage
            .save_project(&sample_project("demo", "demo"))
            .unwrap();
        storage.delete_project("demo").expect("delete_project");
        assert!(storage.get_project("demo").unwrap().is_none());
    }

    #[test]
    fn repository_impl_storage_delete_file_nodes() {
        let repo = fresh_repo();
        let storage: &dyn Storage = &repo;
        storage
            .save_project(&sample_project("demo", "demo"))
            .unwrap();
        storage
            .save_nodes(
                &[sample_file("f1", "demo", "/src/main.rs", "abc")],
                NodeLabel::File,
            )
            .unwrap();
        storage
            .save_nodes(
                &[sample_function("f1", "demo", "main", "demo.main")],
                NodeLabel::Function,
            )
            .unwrap();
        storage
            .delete_file_nodes("/src/main.rs", "demo")
            .expect("delete_file_nodes");
        let file_rows = storage.query("MATCH (f:File) RETURN f.id;").unwrap();
        assert!(file_rows.is_empty());
    }

    // --- delete_file_nodes_batch ---

    #[test]
    fn delete_file_nodes_batch_removes_multiple_files() {
        let repo = fresh_repo();
        repo.save_project(&sample_project("demo", "demo")).unwrap();
        repo.save_nodes(
            &[
                sample_file("f1", "demo", "/src/a.rs", "h1"),
                sample_file("f2", "demo", "/src/b.rs", "h2"),
                sample_file("f3", "demo", "/src/c.rs", "h3"),
            ],
            NodeLabel::File,
        )
        .unwrap();

        repo.delete_file_nodes_batch(&["/src/a.rs".to_string(), "/src/b.rs".to_string()], "demo")
            .expect("delete_file_nodes_batch");

        let rows = repo
            .connection()
            .query("MATCH (f:File) RETURN f.filePath AS p;")
            .unwrap();
        assert_eq!(rows.len(), 1, "only /src/c.rs should remain");
        assert_eq!(rows[0][0].as_str(), Some("/src/c.rs"));
    }

    #[test]
    fn delete_file_nodes_batch_empty_list_is_noop() {
        let repo = fresh_repo();
        repo.save_project(&sample_project("demo", "demo")).unwrap();
        repo.save_nodes(
            &[sample_file("f1", "demo", "/src/a.rs", "h1")],
            NodeLabel::File,
        )
        .unwrap();

        repo.delete_file_nodes_batch(&[], "demo")
            .expect("empty batch noop");

        let rows = repo
            .connection()
            .query("MATCH (f:File) RETURN f.filePath AS p;")
            .unwrap();
        assert_eq!(rows.len(), 1, "file should still exist");
    }

    #[test]
    fn delete_file_nodes_batch_with_changed_files_removes_them() {
        let repo = fresh_repo();
        repo.save_project(&sample_project("demo", "demo")).unwrap();
        repo.save_nodes(
            &[
                sample_file("f1", "demo", "/src/old.rs", "h1"),
                sample_file("f2", "demo", "/src/new.rs", "h2"),
            ],
            NodeLabel::File,
        )
        .unwrap();

        // Simulate incremental: deleted + changed files.
        let paths = vec!["/src/old.rs".to_string(), "/src/new.rs".to_string()];
        repo.delete_file_nodes_batch(&paths, "demo")
            .expect("batch delete deleted+changed");

        let rows = repo
            .connection()
            .query("MATCH (f:File) RETURN f.filePath AS p;")
            .unwrap();
        assert!(rows.is_empty(), "both files should be deleted");
    }

    // --- Coverage gap tests: lastCommit property, empty orphan_ids ---

    #[test]
    fn save_project_with_last_commit_persists_value() {
        let repo = fresh_repo();
        let node = Node::builder(NodeLabel::Project, "demo", "demo")
            .id("p1")
            .language(Language::Rust)
            .properties(serde_json::json!({
                "rootPath": "/repo/demo",
                "fileCount": 5,
                "indexedAt": 1_700_000_001,
                "lastCommit": "abc123def",
            }))
            .build();
        repo.save_project(&node).expect("save_project");

        let rec = repo.get_project("p1").unwrap().unwrap();
        assert_eq!(rec.last_commit, "abc123def");
    }

    #[test]
    fn delete_file_nodes_batch_nonexistent_paths_is_noop() {
        // Non-existent paths → orphan_ids stays empty → CodeRelation
        // cleanup is skipped (line 390 false branch).
        let repo = fresh_repo();
        repo.save_project(&sample_project("demo", "demo")).unwrap();
        repo.save_nodes(
            &[sample_file("f1", "demo", "/src/a.rs", "h1")],
            NodeLabel::File,
        )
        .unwrap();

        repo.delete_file_nodes_batch(&["/nonexistent.rs".to_string()], "demo")
            .expect("batch delete non-existent paths");

        // Original file should still exist.
        let rows = repo
            .connection()
            .query("MATCH (f:File) RETURN f.filePath AS p;")
            .unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0].as_str(), Some("/src/a.rs"));
    }

    #[test]
    fn project_record_debug_format() {
        let rec = ProjectRecord {
            id: "p1".into(),
            name: "demo".into(),
            root_path: "/".into(),
            language: "rust".into(),
            file_count: 1,
            indexed_at: 2,
            last_commit: "abc".into(),
        };
        let s = format!("{rec:?}");
        assert!(s.contains("ProjectRecord"));
        assert!(s.contains("demo"));
    }

    // --- Coverage gap: save_project without language ---

    #[test]
    fn save_project_without_language_defaults_to_empty() {
        // Cover the `node.language.map(...).unwrap_or_default()` branch
        // (line 121) when language is None → empty string in the DB.
        let repo = fresh_repo();
        let node = Node::builder(NodeLabel::Project, "nolang", "nolang")
            .id("p_nolang")
            .properties(serde_json::json!({
                "rootPath": "/repo/nolang",
                "fileCount": 0,
                "indexedAt": 0,
            }))
            .build();
        repo.save_project(&node).expect("save_project");

        let rec = repo.get_project("p_nolang").unwrap().unwrap();
        assert_eq!(rec.language, "", "language should default to empty string");
        assert_eq!(rec.name, "nolang");
    }

    #[test]
    fn save_project_without_last_commit_defaults_to_empty() {
        // Cover the str_prop returning empty for lastCommit when the
        // property is absent.
        let repo = fresh_repo();
        let node = Node::builder(NodeLabel::Project, "nocommit", "nocommit")
            .id("p_nocommit")
            .language(Language::Rust)
            .properties(serde_json::json!({
                "rootPath": "/repo/nocommit",
                "fileCount": 1,
                "indexedAt": 100,
            }))
            .build();
        repo.save_project(&node).expect("save_project");

        let rec = repo.get_project("p_nocommit").unwrap().unwrap();
        assert_eq!(rec.last_commit, "");
        assert_eq!(rec.file_count, 1);
    }

    #[test]
    fn delete_file_nodes_batch_removes_related_edges() {
        // Cover the orphan_ids cleanup path in delete_file_nodes_batch
        // (lines 390-405) when edges reference the deleted nodes.
        let repo = fresh_repo();
        repo.save_project(&sample_project("demo", "demo")).unwrap();
        repo.save_nodes(
            &[
                sample_file("f1", "demo", "/src/a.rs", "h1"),
                sample_file("f2", "demo", "/src/b.rs", "h2"),
            ],
            NodeLabel::File,
        )
        .unwrap();
        repo.save_nodes(
            &[
                sample_function("fn1", "demo", "func_a", "demo.func_a"),
                sample_function("fn2", "demo", "func_b", "demo.func_b"),
            ],
            NodeLabel::Function,
        )
        .unwrap();
        repo.save_edges(&[Edge::builder("fn1", "fn2", EdgeType::Calls, "demo")
            .start_line(1)
            .build()])
            .unwrap();

        // Sanity: edge exists.
        let rows = repo
            .connection()
            .query("MATCH (r:CodeRelation) RETURN count(r) AS cnt;")
            .unwrap();
        assert_eq!(rows[0][0], serde_json::json!(1));

        // Delete both files → orphan_ids should include fn1, fn2 → edge deleted.
        repo.delete_file_nodes_batch(&["/src/main.rs".to_string()], "demo")
            .expect("batch delete");

        // The edge should be gone because the nodes it references were deleted.
        let rows = repo
            .connection()
            .query("MATCH (r:CodeRelation) RETURN count(r) AS cnt;")
            .unwrap();
        assert_eq!(rows[0][0], serde_json::json!(0));
    }
}