forgekit-core 0.5.0

Deterministic code intelligence SDK - Core library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Integration tests for tool crate integrations
//!
//! These tests verify that Forge SDK properly integrates with:
//! - magellan (graph operations)
//! - llmgrep (search operations)
//! - splice (edit operations)
//! - mirage (CFG analysis)
//!
//! Both SQLite and Native V3 backends are tested.

use forgekit_core::{BackendKind, Forge};
use std::sync::Arc;

// =============================================================================
// Helper Functions
// =============================================================================

async fn create_test_repo() -> tempfile::TempDir {
    let temp = tempfile::tempdir().unwrap();

    // Create a simple Rust project structure
    let src_dir = temp.path().join("src");
    tokio::fs::create_dir_all(&src_dir).await.unwrap();

    // Create lib.rs with some symbols
    tokio::fs::write(
        src_dir.join("lib.rs"),
        r#"
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

pub fn subtract(a: i32, b: i32) -> i32 {
    a - b
}

pub struct Calculator {
    value: i32,
}

impl Calculator {
    pub fn new() -> Self {
        Self { value: 0 }
    }
    
    pub fn add(&mut self, n: i32) {
        self.value = add(self.value, n);
    }
    
    pub fn result(&self) -> i32 {
        self.value
    }
}
"#,
    )
    .await
    .unwrap();

    temp
}

// =============================================================================
// Backend Tests
// =============================================================================

#[tokio::test]
async fn test_sqlite_backend_basic() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open with SQLite backend");

    assert_eq!(forge.backend_kind(), BackendKind::SQLite);
}

#[tokio::test]
#[ignore = "native-v3 backend removed"]
async fn test_native_v3_backend_basic() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::NativeV3)
        .await
        .expect("Failed to open with Native V3 backend");

    assert_eq!(forge.backend_kind(), BackendKind::NativeV3);
}

// =============================================================================
// Graph Module Tests (via magellan)
// =============================================================================

#[tokio::test]
async fn test_graph_find_symbol_sqlite() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    // Index the codebase first (via magellan)
    forge.graph().index().await.expect("Failed to index");

    // Now search for symbols
    let symbols = forge
        .graph()
        .find_symbol("add")
        .await
        .expect("Failed to find symbol");

    assert!(!symbols.is_empty(), "Should find 'add' symbol");
    assert!(symbols.iter().any(|s| s.name == Arc::from("add")));
}

#[tokio::test]
async fn test_graph_find_symbol_native_v3() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::NativeV3)
        .await
        .expect("Failed to open");

    // Index the codebase first
    forge.graph().index().await.expect("Failed to index");

    // Now search for symbols
    let symbols = forge
        .graph()
        .find_symbol("Calculator")
        .await
        .expect("Failed to find symbol");

    assert!(!symbols.is_empty(), "Should find 'Calculator' symbol");
}

#[tokio::test]
#[ignore = "requires magellan feature"]
async fn test_graph_callers_sqlite() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    forge.graph().index().await.expect("Failed to index");

    // Find callers of 'add' function
    let callers = forge
        .graph()
        .callers_of("add")
        .await
        .expect("Failed to find callers");

    // The 'add' method in Calculator calls the standalone 'add' function
    assert!(!callers.is_empty(), "Should find callers of 'add'");
}

// Note: For SQLite backend, cross-file references are limited (magellan limitation).
// For Native V3 backend, cross-file references are fully supported.
#[tokio::test]
#[ignore = "requires magellan feature"]
async fn test_graph_references_sqlite() {
    let temp = create_test_repo().await;

    // Test with Native V3 backend which supports cross-file references
    let forge = Forge::open_with_backend(temp.path(), BackendKind::NativeV3)
        .await
        .expect("Failed to open");

    forge.graph().index().await.expect("Failed to index");

    let refs = forge
        .graph()
        .references("add")
        .await
        .expect("Failed to find references");

    assert!(
        !refs.is_empty(),
        "Native V3 should find cross-file references to 'add' function"
    );
}

// =============================================================================
// Search Module Tests (via llmgrep)
// =============================================================================

#[tokio::test]
async fn test_search_semantic_sqlite() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    // Index for semantic search
    forge
        .search()
        .index()
        .await
        .expect("Failed to index for search");

    // Semantic search — query by symbol name since llmgrep uses FTS5 matching
    let results = forge
        .search()
        .semantic("add")
        .await
        .expect("Failed to search");

    assert!(!results.is_empty(), "Should find results for 'add'");
    assert!(results.iter().any(|r| r.name.contains("add")));
}

#[tokio::test]
async fn test_search_pattern_sqlite() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    // Pattern search (regex)
    let results = forge
        .search()
        .pattern(r"fn \w+\(")
        .await
        .expect("Failed to search");

    assert!(
        !results.is_empty(),
        "Should find functions matching pattern"
    );
}

#[tokio::test]
#[ignore = "native-v3 backend removed"]
async fn test_search_semantic_native_v3() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::NativeV3)
        .await
        .expect("Failed to open");

    forge
        .search()
        .index()
        .await
        .expect("Failed to index for search");

    let results = forge
        .search()
        .semantic("calculator implementation")
        .await
        .expect("Failed to search");

    assert!(!results.is_empty(), "Should find Calculator struct");
}

// =============================================================================
// Edit Module Tests (via splice)
// =============================================================================

#[tokio::test]
async fn test_edit_patch_symbol_sqlite() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    // Index first
    forge.graph().index().await.expect("Failed to index");

    // Patch a symbol
    let result = forge
        .edit()
        .patch_symbol(
            "add",
            "pub fn add(a: i32, b: i32) -> i32 { a.wrapping_add(b) }",
        )
        .await
        .expect("Failed to patch");

    assert!(result.success, "Patch should succeed");
    assert!(
        !result.changed_files.is_empty(),
        "Should have changed files"
    );

    // Verify the change
    let content = tokio::fs::read_to_string(temp.path().join("src/lib.rs"))
        .await
        .expect("Failed to read file");

    assert!(
        content.contains("wrapping_add"),
        "Should contain patched content"
    );
}

#[tokio::test]
async fn test_edit_rename_symbol_sqlite() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    forge.graph().index().await.expect("Failed to index");

    // Rename a symbol
    let result = forge
        .edit()
        .rename_symbol("add", "sum")
        .await
        .expect("Failed to rename");

    assert!(result.success, "Rename should succeed");

    // Verify the change
    let content = tokio::fs::read_to_string(temp.path().join("src/lib.rs"))
        .await
        .expect("Failed to read file");

    assert!(
        content.contains("pub fn sum("),
        "Should contain renamed function"
    );
}

#[tokio::test]
#[ignore = "native-v3 backend removed"]
async fn test_edit_patch_symbol_native_v3() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::NativeV3)
        .await
        .expect("Failed to open");

    forge.graph().index().await.expect("Failed to index");

    let result = forge
        .edit()
        .patch_symbol(
            "subtract",
            "pub fn subtract(a: i32, b: i32) -> i32 { a - b } // patched",
        )
        .await
        .expect("Failed to patch");

    assert!(result.success, "Patch should succeed");
}

// =============================================================================
// CFG Module Tests (via mirage)
// =============================================================================

#[tokio::test]
async fn test_cfg_paths_sqlite() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    // Index the graph first
    forge.graph().index().await.expect("Failed to index graph");

    // Index for CFG analysis
    forge.cfg().index().await.expect("Failed to index CFG");

    // Get function symbol ID first
    let symbols = forge
        .graph()
        .find_symbol("add")
        .await
        .expect("Failed to find symbol");

    let symbol_id = symbols.first().expect("Should find add function").id;

    // Enumerate paths
    let paths = forge
        .cfg()
        .paths(symbol_id)
        .execute()
        .await
        .expect("Failed to enumerate paths");

    assert!(!paths.is_empty(), "Should have at least one path");
}

#[tokio::test]
async fn test_cfg_dominators_sqlite() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    // Index the graph first
    forge.graph().index().await.expect("Failed to index graph");

    forge.cfg().index().await.expect("Failed to index CFG");

    let symbols = forge
        .graph()
        .find_symbol("add")
        .await
        .expect("Failed to find symbol");

    let symbol_id = symbols.first().unwrap().id;

    let dominators = forge
        .cfg()
        .dominators(symbol_id)
        .await
        .expect("Failed to compute dominators");

    // At minimum should have entry block
    assert!(!dominators.is_empty(), "Should have at least entry block");
}

#[tokio::test]
#[ignore = "native-v3 backend removed"]
async fn test_cfg_native_v3() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::NativeV3)
        .await
        .expect("Failed to open");

    forge.cfg().index().await.expect("Failed to index CFG");

    let symbols = forge
        .graph()
        .find_symbol("Calculator::new")
        .await
        .expect("Failed to find symbol");

    if let Some(symbol) = symbols.first() {
        let paths = forge
            .cfg()
            .paths(symbol.id)
            .execute()
            .await
            .expect("Failed to enumerate paths");

        // Calculator::new is simple, should have 1 path
        assert!(!paths.is_empty(), "Should have paths");
    }
}

// =============================================================================
// Cross-Backend Consistency Tests
// =============================================================================

#[tokio::test]
#[ignore = "native-v3 backend removed"]
async fn test_graph_consistency_across_backends() {
    let temp = create_test_repo().await;

    // Open with SQLite
    let forge_sqlite = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open SQLite");

    forge_sqlite
        .graph()
        .index()
        .await
        .expect("Failed to index SQLite");

    let symbols_sqlite = forge_sqlite
        .graph()
        .find_symbol("add")
        .await
        .expect("Failed to search SQLite");

    // Open with Native V3 (different database file)
    let forge_native = Forge::open_with_backend(temp.path(), BackendKind::NativeV3)
        .await
        .expect("Failed to open Native V3");

    forge_native
        .graph()
        .index()
        .await
        .expect("Failed to index Native V3");

    let symbols_native = forge_native
        .graph()
        .find_symbol("add")
        .await
        .expect("Failed to search Native V3");

    // Both should find the same symbol
    assert_eq!(
        symbols_sqlite.len(),
        symbols_native.len(),
        "Both backends should find same number of symbols"
    );
}

#[tokio::test]
#[ignore = "native-v3 backend removed"]
async fn test_search_consistency_across_backends() {
    let temp = create_test_repo().await;

    let forge_sqlite = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open SQLite");
    forge_sqlite
        .search()
        .index()
        .await
        .expect("Failed to index SQLite");

    let results_sqlite = forge_sqlite
        .search()
        .semantic("add calculator")
        .await
        .expect("Failed to search SQLite");

    let forge_native = Forge::open_with_backend(temp.path(), BackendKind::NativeV3)
        .await
        .expect("Failed to open Native V3");
    forge_native
        .search()
        .index()
        .await
        .expect("Failed to index Native V3");

    let results_native = forge_native
        .search()
        .semantic("add calculator")
        .await
        .expect("Failed to search Native V3");

    // Results should be equivalent (may have different internal IDs but same symbols)
    assert!(
        !results_sqlite.is_empty() && !results_native.is_empty(),
        "Both backends should return results"
    );
}

// =============================================================================
// Error Handling Tests
// =============================================================================

#[tokio::test]
async fn test_graph_symbol_not_found() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    forge.graph().index().await.expect("Failed to index");

    let symbols = forge
        .graph()
        .find_symbol("nonexistent_symbol_12345")
        .await
        .expect("Should not fail, just return empty");

    assert!(
        symbols.is_empty(),
        "Should return empty for nonexistent symbol"
    );
}

#[tokio::test]
async fn test_edit_symbol_not_found() {
    let temp = create_test_repo().await;

    let forge = Forge::open_with_backend(temp.path(), BackendKind::SQLite)
        .await
        .expect("Failed to open");

    forge.graph().index().await.expect("Failed to index");

    let result = forge.edit().patch_symbol("nonexistent", "content").await;

    assert!(result.is_err(), "Should fail for nonexistent symbol");
}