splice 2.6.2

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

use splice::graph::CodeGraph;
use splice::ingest::cpp::extract_cpp_symbols;
use splice::ingest::java::extract_java_symbols;
use splice::ingest::javascript::extract_javascript_symbols;
use splice::ingest::python::extract_python_symbols;
use splice::ingest::rust::extract_rust_symbols;
use splice::ingest::typescript::extract_typescript_symbols;
use splice::symbol::Language;
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::TempDir;

///////////////////////////////////////////////////////////////////////////////
// Test fixtures
///////////////////////////////////////////////////////////////////////////////

/// Enum representing all supported languages for testing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TestLanguage {
    Rust,
    Python,
    C,
    Cpp,
    Java,
    JavaScript,
    TypeScript,
}

impl TestLanguage {
    /// Get the file extension for this language.
    pub fn extension(&self) -> &'static str {
        match self {
            TestLanguage::Rust => "rs",
            TestLanguage::Python => "py",
            TestLanguage::C => "c",
            TestLanguage::Cpp => "cpp",
            TestLanguage::Java => "java",
            TestLanguage::JavaScript => "js",
            TestLanguage::TypeScript => "ts",
        }
    }

    /// Get the validation command for this language (if applicable).
    ///
    /// Returns None for JavaScript which has no compiler gate.
    pub fn validate_command(&self) -> Option<&'static str> {
        match self {
            TestLanguage::Rust => Some("cargo check"),
            TestLanguage::Python => Some("python -m py_compile"),
            TestLanguage::C => Some("gcc -c"),
            TestLanguage::Cpp => Some("g++ -c"),
            TestLanguage::Java => Some("javac"),
            TestLanguage::JavaScript => None, // No compiler gate
            TestLanguage::TypeScript => Some("tsc --noEmit"),
        }
    }

    /// Get the Splice Language enum value.
    pub fn splice_language(&self) -> Language {
        match self {
            TestLanguage::Rust => Language::Rust,
            TestLanguage::Python => Language::Python,
            TestLanguage::C => Language::C,
            TestLanguage::Cpp => Language::Cpp,
            TestLanguage::Java => Language::Java,
            TestLanguage::JavaScript => Language::JavaScript,
            TestLanguage::TypeScript => Language::TypeScript,
        }
    }

    /// Create a sample source file for this language.
    pub fn create_sample_file(&self, dir: &Path) -> PathBuf {
        let filename = format!("sample.{}", self.extension());
        let file_path = dir.join(&filename);

        let content = match self {
            TestLanguage::Rust => {
                r#"
/// Sample Rust function
pub fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

/// Sample Rust struct
pub struct Point {
    pub x: i32,
    pub y: i32,
}

impl Point {
    pub fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }
}
"#
            }
            TestLanguage::Python => {
                r#"
# Sample Python function
def greet(name: str) -> str:
    return f"Hello, {name}!"

# Sample Python class
class Point:
    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y
"#
            }
            TestLanguage::C => {
                r#"
/* Sample C function */
#include <stdio.h>

int greet(const char* name) {
    printf("Hello, %s!\n", name);
    return 0;
}

/* Sample C struct */
struct Point {
    int x;
    int y;
};
"#
            }
            TestLanguage::Cpp => {
                r#"
// Sample C++ function
#include <iostream>
#include <string>

std::string greet(const std::string& name) {
    return "Hello, " + name + "!";
}

// Sample C++ class
class Point {
public:
    int x;
    int y;

    Point(int x, int y) : x(x), y(y) {}
};
"#
            }
            TestLanguage::Java => {
                r#"
// Sample Java class
public class Greeter {
    public String greet(String name) {
        return "Hello, " + name + "!";
    }
}

// Sample Java class with fields
class Point {
    public int x;
    public int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
"#
            }
            TestLanguage::JavaScript => {
                r#"
// Sample JavaScript function
function greet(name) {
    return `Hello, ${name}!`;
}

// Sample JavaScript class
class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

// Sample arrow function
const add = (a, b) => a + b;
"#
            }
            TestLanguage::TypeScript => {
                r#"
// Sample TypeScript function
function greet(name: string): string {
    return `Hello, ${name}!`;
}

// Sample TypeScript interface
interface Point {
    x: number;
    y: number;
}

// Sample TypeScript class with types
class PointImpl implements Point {
    constructor(public x: number, public y: number) {}
}

// Sample generic function
function identity<T>(value: T): T {
    return value;
}
"#
            }
        };

        fs::write(&file_path, content.trim()).expect("Failed to write sample file");
        file_path
    }
}

/// Create a multi-language workspace with all 7 languages.
pub fn create_multi_lang_workspace() -> TempDir {
    let workspace_dir = TempDir::new().expect("Failed to create temp workspace");
    let workspace_path = workspace_dir.path();

    // Create sample files for all languages
    for lang in &[
        TestLanguage::Rust,
        TestLanguage::Python,
        TestLanguage::C,
        TestLanguage::Cpp,
        TestLanguage::Java,
        TestLanguage::JavaScript,
        TestLanguage::TypeScript,
    ] {
        lang.create_sample_file(workspace_path);
    }

    workspace_dir
}

/// Verify that language-specific validation gate works.
///
/// This function checks:
/// 1. Language detection from file extension
/// 2. Validation command mapping
/// 3. Language enum conversion
pub fn verify_validation_gate(lang: TestLanguage, file_path: &Path) {
    // Check file extension matches language
    let extension = file_path
        .extension()
        .and_then(|ext| ext.to_str())
        .expect("File should have extension");

    assert_eq!(
        extension,
        lang.extension(),
        "File extension should match language: expected {}, got {}",
        lang.extension(),
        extension
    );

    // Check validation command mapping (except JavaScript which has none)
    if let Some(cmd) = lang.validate_command() {
        assert!(!cmd.is_empty(), "Validation command should not be empty");
    }

    // Check Splice language enum conversion
    let splice_lang = lang.splice_language();
    // Just verify it converts without panicking
    match splice_lang {
        Language::Rust
        | Language::Python
        | Language::C
        | Language::Cpp
        | Language::Java
        | Language::JavaScript
        | Language::TypeScript => {
            // Valid language
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
// Unit tests for fixtures (Task 1)
///////////////////////////////////////////////////////////////////////////////

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

    /// Test 1: Verify each language has correct extension
    #[test]
    fn test_language_extensions() {
        assert_eq!(TestLanguage::Rust.extension(), "rs");
        assert_eq!(TestLanguage::Python.extension(), "py");
        assert_eq!(TestLanguage::C.extension(), "c");
        assert_eq!(TestLanguage::Cpp.extension(), "cpp");
        assert_eq!(TestLanguage::Java.extension(), "java");
        assert_eq!(TestLanguage::JavaScript.extension(), "js");
        assert_eq!(TestLanguage::TypeScript.extension(), "ts");
    }

    /// Test 2: Verify sample file creation for each language
    #[test]
    fn test_sample_file_creation() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        for lang in &[
            TestLanguage::Rust,
            TestLanguage::Python,
            TestLanguage::C,
            TestLanguage::Cpp,
            TestLanguage::Java,
            TestLanguage::JavaScript,
            TestLanguage::TypeScript,
        ] {
            let file_path = lang.create_sample_file(temp_dir.path());
            assert!(
                file_path.exists(),
                "Sample file should exist for {:?}",
                lang
            );
            assert!(
                file_path.extension().unwrap() == lang.extension(),
                "File extension should match for {:?}",
                lang
            );

            // Verify file is not empty
            let content = fs::read_to_string(&file_path).expect("Failed to read sample file");
            assert!(!content.is_empty(), "Sample file should not be empty");
        }
    }

    /// Test 3: Verify multi-language workspace creation
    #[test]
    fn test_multi_lang_workspace() {
        let workspace = create_multi_lang_workspace();
        let workspace_path = workspace.path();

        // Count files by extension
        let mut rust_count = 0;
        let mut python_count = 0;
        let mut c_count = 0;
        let mut cpp_count = 0;
        let mut java_count = 0;
        let mut js_count = 0;
        let mut ts_count = 0;

        for entry in fs::read_dir(workspace_path).expect("Failed to read workspace") {
            let entry = entry.expect("Failed to read entry");
            let path = entry.path();

            if let Some(ext) = path.extension() {
                match ext.to_str().unwrap() {
                    "rs" => rust_count += 1,
                    "py" => python_count += 1,
                    "c" => c_count += 1,
                    "cpp" => cpp_count += 1,
                    "java" => java_count += 1,
                    "js" => js_count += 1,
                    "ts" => ts_count += 1,
                    _ => {}
                }
            }
        }

        assert_eq!(rust_count, 1, "Should have 1 Rust file");
        assert_eq!(python_count, 1, "Should have 1 Python file");
        assert_eq!(c_count, 1, "Should have 1 C file");
        assert_eq!(cpp_count, 1, "Should have 1 C++ file");
        assert_eq!(java_count, 1, "Should have 1 Java file");
        assert_eq!(js_count, 1, "Should have 1 JavaScript file");
        assert_eq!(ts_count, 1, "Should have 1 TypeScript file");
    }

    /// Test 4: Verify validation command mapping
    #[test]
    fn test_validate_command_mapping() {
        // All languages except JavaScript should have validation commands
        assert_eq!(TestLanguage::Rust.validate_command(), Some("cargo check"));
        assert_eq!(
            TestLanguage::Python.validate_command(),
            Some("python -m py_compile")
        );
        assert_eq!(TestLanguage::C.validate_command(), Some("gcc -c"));
        assert_eq!(TestLanguage::Cpp.validate_command(), Some("g++ -c"));
        assert_eq!(TestLanguage::Java.validate_command(), Some("javac"));
        assert_eq!(TestLanguage::JavaScript.validate_command(), None);
        assert_eq!(
            TestLanguage::TypeScript.validate_command(),
            Some("tsc --noEmit")
        );
    }
}

///////////////////////////////////////////////////////////////////////////////
// Language-specific symbol extraction tests (Task 2 - simplified)
///////////////////////////////////////////////////////////////////////////////

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

    /// Test Rust symbol extraction
    #[test]
    fn test_rust_symbol_extraction() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::Rust.create_sample_file(temp_dir.path());

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_rust_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract Rust symbols");

        assert!(
            !symbols.is_empty(),
            "Should extract at least one Rust symbol"
        );
    }

    /// Test Python symbol extraction
    #[test]
    fn test_python_symbol_extraction() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::Python.create_sample_file(temp_dir.path());

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_python_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract Python symbols");

        assert!(
            !symbols.is_empty(),
            "Should extract at least one Python symbol"
        );
    }

    /// Test C symbol extraction
    #[test]
    fn test_c_symbol_extraction() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::C.create_sample_file(temp_dir.path());

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_cpp_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract C symbols");

        assert!(!symbols.is_empty(), "Should extract at least one C symbol");
    }

    /// Test C++ symbol extraction
    #[test]
    fn test_cpp_symbol_extraction() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::Cpp.create_sample_file(temp_dir.path());

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_cpp_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract C++ symbols");

        assert!(
            !symbols.is_empty(),
            "Should extract at least one C++ symbol"
        );
    }

    /// Test Java symbol extraction
    #[test]
    fn test_java_symbol_extraction() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::Java.create_sample_file(temp_dir.path());

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_java_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract Java symbols");

        assert!(
            !symbols.is_empty(),
            "Should extract at least one Java symbol"
        );
    }

    /// Test JavaScript symbol extraction
    #[test]
    fn test_javascript_symbol_extraction() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::JavaScript.create_sample_file(temp_dir.path());

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_javascript_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract JavaScript symbols");

        assert!(
            !symbols.is_empty(),
            "Should extract at least one JavaScript symbol"
        );
    }

    /// Test TypeScript symbol extraction
    #[test]
    fn test_typescript_symbol_extraction() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::TypeScript.create_sample_file(temp_dir.path());

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_typescript_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract TypeScript symbols");

        assert!(
            !symbols.is_empty(),
            "Should extract at least one TypeScript symbol"
        );
    }
}

///////////////////////////////////////////////////////////////////////////////
// Language detection and file extension tests (Tasks 5-6)
///////////////////////////////////////////////////////////////////////////////

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

    /// Test detecting Rust from .rs extension
    #[test]
    fn test_detect_rust_from_rs_extension() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::Rust.create_sample_file(temp_dir.path());

        let extension = file_path.extension().unwrap().to_str().unwrap();
        assert_eq!(extension, "rs");

        // Verify we can extract symbols from this file
        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols =
            extract_rust_symbols(&file_path, source.as_bytes()).expect("Failed to extract symbols");

        assert!(!symbols.is_empty());
    }

    /// Test detecting Python from .py extension
    #[test]
    fn test_detect_python_from_py_extension() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::Python.create_sample_file(temp_dir.path());

        let extension = file_path.extension().unwrap().to_str().unwrap();
        assert_eq!(extension, "py");

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_python_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract symbols");

        assert!(!symbols.is_empty());
    }

    /// Test detecting C from .c extension
    #[test]
    fn test_detect_c_from_c_extension() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::C.create_sample_file(temp_dir.path());

        let extension = file_path.extension().unwrap().to_str().unwrap();
        assert_eq!(extension, "c");

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols =
            extract_cpp_symbols(&file_path, source.as_bytes()).expect("Failed to extract symbols");

        assert!(!symbols.is_empty());
    }

    /// Test detecting C++ from .cpp extension
    #[test]
    fn test_detect_cpp_from_cpp_extension() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::Cpp.create_sample_file(temp_dir.path());

        let extension = file_path.extension().unwrap().to_str().unwrap();
        assert_eq!(extension, "cpp");

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols =
            extract_cpp_symbols(&file_path, source.as_bytes()).expect("Failed to extract symbols");

        assert!(!symbols.is_empty());
    }

    /// Test detecting Java from .java extension
    #[test]
    fn test_detect_java_from_java_extension() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::Java.create_sample_file(temp_dir.path());

        let extension = file_path.extension().unwrap().to_str().unwrap();
        assert_eq!(extension, "java");

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols =
            extract_java_symbols(&file_path, source.as_bytes()).expect("Failed to extract symbols");

        assert!(!symbols.is_empty());
    }

    /// Test detecting JavaScript from .js extension
    #[test]
    fn test_detect_javascript_from_js_extension() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::JavaScript.create_sample_file(temp_dir.path());

        let extension = file_path.extension().unwrap().to_str().unwrap();
        assert_eq!(extension, "js");

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_javascript_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract symbols");

        assert!(!symbols.is_empty());
    }

    /// Test detecting TypeScript from .ts extension
    #[test]
    fn test_detect_typescript_from_ts_extension() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let file_path = TestLanguage::TypeScript.create_sample_file(temp_dir.path());

        let extension = file_path.extension().unwrap().to_str().unwrap();
        assert_eq!(extension, "ts");

        let source = fs::read_to_string(&file_path).expect("Failed to read file");
        let symbols = extract_typescript_symbols(&file_path, source.as_bytes())
            .expect("Failed to extract symbols");

        assert!(!symbols.is_empty());
    }
}