leankg 0.19.21

Lightweight Knowledge Graph for AI-Assisted Development
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
// Phase 1 structural parity integration tests
// Tests using LeanKG codebase patterns as fixtures.
// Run with: cargo test --release -- phase1_integration_test

use leankg::db::models::{CodeElement, Relationship};
use leankg::db::schema::init_db;
use leankg::graph::GraphEngine;
use tempfile::TempDir;

fn make_engine() -> (GraphEngine, TempDir) {
    let tmp = TempDir::new().unwrap();
    let db_path = tmp.path().join("test.db");
    let db = init_db(&db_path).unwrap();
    (GraphEngine::new(db), tmp)
}

fn populate_with_leankg_patterns(engine: &GraphEngine) {
    let elements = vec![
        ("src/main.rs", "main", "function", "rust", 1, 30),
        ("src/main.rs", "setup_logging", "function", "rust", 32, 40),
        ("src/lib.rs", "run_benchmark", "function", "rust", 1, 25),
        (
            "src/db/models.rs",
            "RelationshipType",
            "enum",
            "rust",
            10,
            72,
        ),
        (
            "src/db/models.rs",
            "CodeElement",
            "struct",
            "rust",
            215,
            250,
        ),
        ("src/db/schema.rs", "init_db", "function", "rust", 89, 121),
        (
            "src/db/schema.rs",
            "init_schema",
            "function",
            "rust",
            180,
            280,
        ),
        ("src/mcp/tools.rs", "ToolRegistry", "struct", "rust", 4, 6),
        ("src/mcp/tools.rs", "list_tools", "function", "rust", 7, 930),
        (
            "src/mcp/handler.rs",
            "execute_tool",
            "function",
            "rust",
            206,
            281,
        ),
        (
            "src/graph/query.rs",
            "get_architecture",
            "function",
            "rust",
            3160,
            3278,
        ),
        (
            "src/graph/query.rs",
            "get_graph_schema",
            "function",
            "rust",
            3292,
            3331,
        ),
        (
            "src/graph/query.rs",
            "find_dead_code",
            "function",
            "rust",
            3334,
            3370,
        ),
        (
            "src/graph/clustering.rs",
            "cluster_elements",
            "function",
            "rust",
            1,
            50,
        ),
        ("src/graph/cache.rs", "QueryCache", "struct", "rust", 1, 30),
        (
            "src/indexer/call_graph.rs",
            "CallGraphBuilder",
            "struct",
            "rust",
            6,
            14,
        ),
        (
            "src/indexer/call_graph.rs",
            "extract_calls_with_resolution",
            "function",
            "rust",
            416,
            425,
        ),
        (
            "src/indexer/extractor.rs",
            "extract_elements",
            "function",
            "rust",
            1,
            100,
        ),
        (
            "src/indexer/parser.rs",
            "parse_file",
            "function",
            "rust",
            1,
            50,
        ),
        (
            "src/api/handlers.rs",
            "handle_request",
            "function",
            "rust",
            1,
            30,
        ),
    ];

    for (file, name, etype, lang, ls, le) in &elements {
        let elem = CodeElement {
            qualified_name: format!("{}::{}", file, name),
            element_type: etype.to_string(),
            name: name.to_string(),
            file_path: file.to_string(),
            line_start: *ls,
            line_end: *le,
            language: lang.to_string(),
            cluster_id: Some(format!("cluster-{}", etype)),
            cluster_label: Some(etype.to_string()),
            ..Default::default()
        };
        engine.insert_element(&elem).unwrap();
    }

    let calls = vec![
        ("src/main.rs::main", "src/main.rs::setup_logging", 0.95),
        ("src/main.rs::main", "src/lib.rs::run_benchmark", 0.90),
        (
            "src/mcp/handler.rs::execute_tool",
            "src/graph/query.rs::get_architecture",
            0.90,
        ),
        (
            "src/mcp/handler.rs::execute_tool",
            "src/graph/query.rs::get_graph_schema",
            0.90,
        ),
        (
            "src/mcp/handler.rs::execute_tool",
            "src/graph/query.rs::find_dead_code",
            0.90,
        ),
        (
            "src/indexer/call_graph.rs::extract_calls_with_resolution",
            "src/indexer/call_graph.rs::CallGraphBuilder",
            0.95,
        ),
        (
            "src/indexer/extractor.rs::extract_elements",
            "src/indexer/parser.rs::parse_file",
            0.85,
        ),
        (
            "src/db/schema.rs::init_db",
            "src/db/schema.rs::init_schema",
            0.90,
        ),
    ];

    for (source, target, conf) in &calls {
        let rel = Relationship {
            source_qualified: source.to_string(),
            target_qualified: target.to_string(),
            rel_type: "calls".to_string(),
            confidence: *conf,
            metadata: serde_json::json!({"resolution_method": "name", "is_resolved": true, "line": 1}),
            ..Default::default()
        };
        engine.insert_relationship(&rel).unwrap();
    }

    let tests = vec![
        (
            "src/graph/query.rs::t1",
            "src/graph/query.rs::get_architecture",
        ),
        (
            "src/graph/query.rs::t2",
            "src/graph/query.rs::get_graph_schema",
        ),
        (
            "src/graph/query.rs::t3",
            "src/graph/query.rs::find_dead_code",
        ),
    ];

    for (test_name, tested_fn) in &tests {
        let rel = Relationship {
            source_qualified: test_name.to_string(),
            target_qualified: tested_fn.to_string(),
            rel_type: "tested_by".to_string(),
            confidence: 0.90,
            metadata: serde_json::json!({}),
            ..Default::default()
        };
        engine.insert_relationship(&rel).unwrap();
    }
}

#[test]
fn test_get_architecture_on_leankg_patterns() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);
    let arch = engine
        .get_architecture(None)
        .expect("get_architecture should succeed");
    let obj = arch.as_object().unwrap();

    let langs = obj["languages"].as_array().unwrap();
    assert!(!langs.is_empty(), "Should detect at least one language");
    let rust_lang = langs
        .iter()
        .find(|l| l["language"].as_str().unwrap() == "rust");
    assert!(rust_lang.is_some(), "Should detect Rust");
    assert!(rust_lang.unwrap()["element_count"].as_u64().unwrap() > 0);

    let eps = obj["entry_points"].as_array().unwrap();
    let has_main = eps
        .iter()
        .any(|e| e["qualified_name"].as_str().unwrap().contains("main"));
    assert!(has_main, "Should find main as entry point");

    let hs = obj["hotspots"].as_array().unwrap();
    assert!(!hs.is_empty(), "Should find hotspots");

    let clusters = obj["clusters"].as_array().unwrap();
    assert!(!clusters.is_empty(), "Should find clusters");

    let rels = obj["relationship_summary"].as_array().unwrap();
    let calls_rel = rels
        .iter()
        .find(|r| r["rel_type"].as_str().unwrap() == "calls")
        .unwrap();
    assert_eq!(calls_rel["count"].as_u64().unwrap(), 8);

    assert_eq!(obj["total_elements"].as_u64().unwrap(), 20);
    assert!(obj["total_files"].as_u64().unwrap() > 0);
}

#[test]
fn test_get_graph_schema_on_leankg_patterns() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);
    let schema = engine
        .get_graph_schema(None)
        .expect("get_graph_schema should succeed");
    let obj = schema.as_object().unwrap();

    let types = obj["element_types"].as_array().unwrap();
    assert!(!types.is_empty());
    let func = types
        .iter()
        .find(|t| t["element_type"].as_str().unwrap() == "function");
    assert!(func.is_some());
    assert!(func.unwrap()["count"].as_u64().unwrap() >= 10);

    let struct_type = types
        .iter()
        .find(|t| t["element_type"].as_str().unwrap() == "struct");
    assert!(struct_type.is_some());
    let enum_type = types
        .iter()
        .find(|t| t["element_type"].as_str().unwrap() == "enum");
    assert!(enum_type.is_some());

    let rel_types = obj["relationship_types"].as_array().unwrap();
    let calls = rel_types
        .iter()
        .find(|t| t["rel_type"].as_str().unwrap() == "calls");
    assert!(calls.is_some());
    let tb = rel_types
        .iter()
        .find(|t| t["rel_type"].as_str().unwrap() == "tested_by");
    assert!(tb.is_some());

    assert_eq!(obj["total_elements"].as_u64().unwrap(), 20);
    assert_eq!(obj["total_relationships"].as_u64().unwrap(), 11);
}

#[test]
fn test_find_dead_code_on_leankg_patterns() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);
    let dead = engine.find_dead_code(5).unwrap();
    let dead_names: Vec<&str> = dead.iter().map(|d| d["name"].as_str().unwrap()).collect();

    assert!(
        !dead_names.contains(&"main"),
        "main should be excluded (entry point)"
    );
    assert!(
        !dead_names.contains(&"get_architecture"),
        "get_architecture should be excluded (called + tested)"
    );
    assert!(
        !dead_names.contains(&"find_dead_code"),
        "find_dead_code should be excluded (called + tested)"
    );
    assert!(
        !dead_names.contains(&"setup_logging"),
        "setup_logging should be excluded (called by main)"
    );
    assert!(
        !dead_names.contains(&"init_schema"),
        "init_schema should be excluded (called by init_db)"
    );
    assert!(
        !dead_names.contains(&"execute_tool"),
        "execute_tool should be excluded (calls many)"
    );

    // Some functions should be dead (no callers, no tests, > 5 lines)
    let has_dead = dead_names.contains(&"cluster_elements")
        || dead_names.contains(&"list_tools")
        || dead_names.contains(&"parse_file")
        || dead_names.contains(&"handle_request");
    assert!(
        has_dead,
        "Should find at least some dead functions, found: {:?}",
        dead_names
    );
}

#[test]
fn test_architecture_structure_contract() {
    let (engine, _tmp) = make_engine();
    let arch = engine.get_architecture(None).unwrap();
    let obj = arch.as_object().unwrap();
    for key in &[
        "languages",
        "entry_points",
        "routes",
        "clusters",
        "hotspots",
        "relationship_summary",
        "knowledge_count",
        "total_elements",
        "total_files",
        "max_items",
        "truncated_sections",
    ] {
        assert!(obj.contains_key(*key), "Architecture missing key: {}", key);
    }
}

#[test]
fn test_graph_schema_structure_contract() {
    let (engine, _tmp) = make_engine();
    let schema = engine.get_graph_schema(None).unwrap();
    let obj = schema.as_object().unwrap();
    for key in &[
        "element_types",
        "relationship_types",
        "total_elements",
        "total_relationships",
        "max_items",
        "truncated_sections",
    ] {
        assert!(obj.contains_key(*key), "Schema missing key: {}", key);
    }
}

#[test]
fn test_resolution_method_in_metadata() {
    let (engine, _tmp) = make_engine();
    let elem = CodeElement {
        qualified_name: "src/handler.rs::process".to_string(),
        element_type: "function".to_string(),
        name: "process".to_string(),
        file_path: "src/handler.rs".to_string(),
        line_start: 10,
        line_end: 30,
        language: "rust".to_string(),
        ..Default::default()
    };
    engine.insert_element(&elem).unwrap();

    let elem2 = CodeElement {
        qualified_name: "src/utils.rs::validate".to_string(),
        element_type: "function".to_string(),
        name: "validate".to_string(),
        file_path: "src/utils.rs".to_string(),
        line_start: 1,
        line_end: 15,
        language: "rust".to_string(),
        ..Default::default()
    };
    engine.insert_element(&elem2).unwrap();

    let rel = Relationship {
        source_qualified: "src/handler.rs::process".to_string(),
        target_qualified: "src/utils.rs::validate".to_string(),
        rel_type: "calls".to_string(),
        confidence: 0.95,
        metadata: serde_json::json!({"resolution_method": "typed", "is_resolved": true}),
        ..Default::default()
    };
    engine.insert_relationship(&rel).unwrap();

    let callers = engine
        .get_callers("validate", Some("src/utils.rs"))
        .unwrap();
    assert!(!callers.is_empty(), "Should find callers");
}

// ── FR-B22: Token budget truncation integration tests ──

#[test]
fn test_arch_truncation_with_leankg_patterns() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);
    let arch = engine.get_architecture(Some(1)).unwrap();
    let obj = arch.as_object().unwrap();

    // Each array section should have at most 1 item
    for key in &[
        "languages",
        "entry_points",
        "hotspots",
        "clusters",
        "relationship_summary",
    ] {
        let arr = obj[*key].as_array().unwrap();
        assert!(
            arr.len() <= 1,
            "Section {} should have <=1 item with max_items=1, got {}",
            key,
            arr.len()
        );
    }
    // truncated_sections should not be empty (multiple sections have >1 item)
    let trunc = obj["truncated_sections"].as_array().unwrap();
    assert!(!trunc.is_empty(), "Should report truncated sections");
}

#[test]
fn test_schema_truncation_with_leankg_patterns() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);
    let schema = engine.get_graph_schema(Some(1)).unwrap();
    let obj = schema.as_object().unwrap();

    // element_types has multiple types (function, struct, enum), should be truncated to 1
    assert_eq!(obj["element_types"].as_array().unwrap().len(), 1);
    // relationship_types has calls + tested_by, should be truncated to 1
    assert_eq!(obj["relationship_types"].as_array().unwrap().len(), 1);

    let trunc = obj["truncated_sections"].as_array().unwrap();
    assert!(!trunc.is_empty(), "Should report truncated sections");
}

#[test]
fn test_arch_truncation_preserves_structure() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);
    let arch = engine.get_architecture(Some(1)).unwrap();
    let obj = arch.as_object().unwrap();
    for key in &[
        "languages",
        "entry_points",
        "routes",
        "clusters",
        "hotspots",
        "relationship_summary",
        "knowledge_count",
        "total_elements",
        "total_files",
        "max_items",
        "truncated_sections",
    ] {
        assert!(
            obj.contains_key(*key),
            "Missing key after truncation: {}",
            key
        );
    }
}

#[test]
fn test_arch_truncation_reports_original_counts() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);

    // First get full result to know original counts
    let full = engine.get_architecture(None).unwrap();
    let full_obj = full.as_object().unwrap();
    let full_clusters = full_obj["clusters"].as_array().unwrap().len();

    // Now truncate to 1
    let arch = engine.get_architecture(Some(1)).unwrap();
    let obj = arch.as_object().unwrap();
    let trunc = obj["truncated_sections"].as_array().unwrap();

    let cluster_trunc = trunc
        .iter()
        .find(|t| t["section"].as_str() == Some("clusters"));
    if full_clusters > 1 {
        assert!(
            cluster_trunc.is_some(),
            "clusters should be in truncated_sections"
        );
        assert_eq!(
            cluster_trunc.unwrap()["original_count"].as_u64(),
            Some(full_clusters as u64)
        );
    }
}

#[test]
fn test_arch_none_no_truncation_with_patterns() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);
    let arch = engine.get_architecture(None).unwrap();
    let obj = arch.as_object().unwrap();
    assert!(obj["truncated_sections"].as_array().unwrap().is_empty());
    assert!(obj["max_items"].is_null());
}

#[test]
fn test_schema_truncation_preserves_scalars() {
    let (engine, _tmp) = make_engine();
    populate_with_leankg_patterns(&engine);
    let schema = engine.get_graph_schema(Some(1)).unwrap();
    let obj = schema.as_object().unwrap();
    assert!(obj.contains_key("total_elements"));
    assert!(obj.contains_key("total_relationships"));
    // total_elements should still be 20 (not affected by truncation)
    assert_eq!(obj["total_elements"].as_u64().unwrap(), 20);
}