context-creator 1.5.0

High-performance CLI tool to convert codebases to Markdown for LLM context
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
#![cfg(test)]

//! Tests for dependency graph edge typing
//!
//! These tests verify that different types of dependencies (imports, function calls, inheritance)
//! are properly distinguished in the dependency graph.

use context_creator::cli::Config;
use context_creator::core::cache::FileCache;
use context_creator::core::semantic_graph::perform_semantic_analysis_graph;
use context_creator::core::walker::{walk_directory, WalkOptions};
use std::fs;
use tempfile::TempDir;

#[test]
fn test_import_edge_type() {
    let temp_dir = TempDir::new().unwrap();
    let base_path = temp_dir.path();

    // Create git directory
    fs::create_dir_all(base_path.join(".git")).unwrap();
    fs::create_dir_all(base_path.join("src")).unwrap();

    // Create files with import relationships
    fs::write(
        base_path.join("src/utils.rs"),
        r#"
pub fn helper() -> String {
    "helper".to_string()
}
"#,
    )
    .unwrap();

    fs::write(
        base_path.join("src/main.rs"),
        r#"
mod utils;
use utils::helper;

fn main() {
    let result = helper();
    println!("{}", result);
}
"#,
    )
    .unwrap();

    let walk_options = WalkOptions::default();
    let mut files = walk_directory(base_path, walk_options).unwrap();
    files.retain(|f| f.path.extension().is_some_and(|ext| ext == "rs"));

    let config = Config {
        trace_imports: true,
        ..Default::default()
    };

    let cache = FileCache::new();
    perform_semantic_analysis_graph(&mut files, &config, &cache).unwrap();

    // Verify import relationships were detected
    let main_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("main.rs"))
        .unwrap();
    let utils_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("utils.rs"))
        .unwrap();

    // Check that main.rs imports utils.rs
    assert!(
        main_file.imports.contains(&utils_file.path),
        "main.rs should import utils.rs"
    );

    // Check that utils.rs is imported by main.rs
    assert!(
        utils_file.imported_by.contains(&main_file.path),
        "utils.rs should be imported by main.rs"
    );
}

#[test]
fn test_function_call_edge_type() {
    let temp_dir = TempDir::new().unwrap();
    let base_path = temp_dir.path();

    // Create git directory
    fs::create_dir_all(base_path.join(".git")).unwrap();
    fs::create_dir_all(base_path.join("src")).unwrap();

    // Create files with function call relationships
    fs::write(
        base_path.join("src/math.rs"),
        r#"
pub fn calculate(a: i32, b: i32) -> i32 {
    a + b
}

pub fn multiply(a: i32, b: i32) -> i32 {
    a * b
}
"#,
    )
    .unwrap();

    fs::write(
        base_path.join("src/processor.rs"),
        r#"
mod math;
use math::{calculate, multiply};

pub fn process_data(values: Vec<i32>) -> i32 {
    let sum = calculate(values[0], values[1]);
    let product = multiply(sum, values[2]);
    product
}
"#,
    )
    .unwrap();

    let walk_options = WalkOptions::default();
    let mut files = walk_directory(base_path, walk_options).unwrap();
    files.retain(|f| f.path.extension().is_some_and(|ext| ext == "rs"));

    let config = Config {
        trace_imports: true,
        include_callers: true,
        ..Default::default()
    };

    let cache = FileCache::new();
    perform_semantic_analysis_graph(&mut files, &config, &cache).unwrap();

    // Verify function call relationships were detected
    let processor_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("processor.rs"))
        .unwrap();
    let math_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("math.rs"))
        .unwrap();

    // Check that processor.rs has function calls
    assert!(
        !processor_file.function_calls.is_empty(),
        "processor.rs should have function calls"
    );

    // Check that processor.rs calls calculate and multiply
    let called_functions: Vec<&str> = processor_file
        .function_calls
        .iter()
        .map(|fc| fc.name.as_str())
        .collect();

    assert!(
        called_functions.contains(&"calculate"),
        "processor.rs should call calculate function"
    );
    assert!(
        called_functions.contains(&"multiply"),
        "processor.rs should call multiply function"
    );

    // Check that processor.rs has the mod statement (which creates an import relationship)
    assert!(
        processor_file.imports.contains(&math_file.path)
            || math_file.imported_by.contains(&processor_file.path),
        "There should be an import relationship between processor.rs and math.rs"
    );
}

#[test]
fn test_inheritance_edge_type() {
    let temp_dir = TempDir::new().unwrap();
    let base_path = temp_dir.path();

    // Create git directory
    fs::create_dir_all(base_path.join(".git")).unwrap();
    fs::create_dir_all(base_path.join("src")).unwrap();

    // Create Python files with inheritance relationships
    fs::write(
        base_path.join("src/base.py"),
        r#"
class Animal:
    def __init__(self, name):
        self.name = name
    
    def speak(self):
        pass
"#,
    )
    .unwrap();

    fs::write(
        base_path.join("src/dog.py"),
        r#"
from base import Animal

class Dog(Animal):
    def speak(self):
        return f"{self.name} barks!"
"#,
    )
    .unwrap();

    let walk_options = WalkOptions::default();
    let mut files = walk_directory(base_path, walk_options).unwrap();
    files.retain(|f| f.path.extension().is_some_and(|ext| ext == "py"));

    let config = Config {
        trace_imports: true,
        include_types: true,
        ..Default::default()
    };

    let cache = FileCache::new();
    perform_semantic_analysis_graph(&mut files, &config, &cache).unwrap();

    // Verify inheritance relationships were detected
    let dog_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("dog.py"))
        .unwrap();
    let base_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("base.py"))
        .unwrap();

    // Check that dog.py imports base.py
    assert!(
        dog_file.imports.contains(&base_file.path),
        "dog.py should import base.py"
    );

    // Check that base.py is imported by dog.py
    assert!(
        base_file.imported_by.contains(&dog_file.path),
        "base.py should be imported by dog.py"
    );

    // With include_types, we should have type references
    assert!(
        !dog_file.type_references.is_empty() || !dog_file.imports.is_empty(),
        "dog.py should have type references or imports for inheritance"
    );
}

#[test]
fn test_mixed_dependency_types() {
    let temp_dir = TempDir::new().unwrap();
    let base_path = temp_dir.path();

    // Create git directory
    fs::create_dir_all(base_path.join(".git")).unwrap();
    fs::create_dir_all(base_path.join("src")).unwrap();

    // Create TypeScript files with mixed dependency types
    fs::write(
        base_path.join("src/interfaces.ts"),
        r#"
export interface Logger {
    log(message: string): void;
}

export interface Config {
    debug: boolean;
    level: string;
}
"#,
    )
    .unwrap();

    fs::write(
        base_path.join("src/base-logger.ts"),
        r#"
import { Logger, Config } from './interfaces';

export abstract class BaseLogger implements Logger {
    protected config: Config;
    
    constructor(config: Config) {
        this.config = config;
    }
    
    abstract log(message: string): void;
}
"#,
    )
    .unwrap();

    fs::write(
        base_path.join("src/console-logger.ts"),
        r#"
import { Config } from './interfaces';
import { BaseLogger } from './base-logger';

export class ConsoleLogger extends BaseLogger {
    constructor(config: Config) {
        super(config);
    }
    
    log(message: string): void {
        if (this.config.debug) {
            console.log(`[${this.config.level}] ${message}`);
        }
    }
}
"#,
    )
    .unwrap();

    let walk_options = WalkOptions::default();
    let mut files = walk_directory(base_path, walk_options).unwrap();
    files.retain(|f| f.path.extension().is_some_and(|ext| ext == "ts"));

    let config = Config {
        trace_imports: true,
        include_types: true,
        ..Default::default()
    };

    let cache = FileCache::new();
    perform_semantic_analysis_graph(&mut files, &config, &cache).unwrap();

    // Verify multiple dependency types in TypeScript files
    let base_logger = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("base-logger.ts"))
        .unwrap();
    let console_logger = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("console-logger.ts"))
        .unwrap();
    let interfaces = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("interfaces.ts"))
        .unwrap();

    // base-logger.ts imports from interfaces.ts
    assert!(
        base_logger.imports.contains(&interfaces.path),
        "base-logger.ts should import from interfaces.ts"
    );

    // Check type references in base-logger.ts
    let base_type_refs: Vec<&str> = base_logger
        .type_references
        .iter()
        .map(|tr| tr.name.as_str())
        .collect();
    assert!(
        base_type_refs.contains(&"Logger") || base_type_refs.contains(&"Config"),
        "base-logger.ts should reference Logger or Config types"
    );

    // console-logger.ts imports from both base-logger.ts and interfaces.ts
    assert!(
        console_logger.imports.contains(&base_logger.path),
        "console-logger.ts should import from base-logger.ts"
    );
    assert!(
        console_logger.imports.contains(&interfaces.path),
        "console-logger.ts should import from interfaces.ts"
    );

    // TypeScript super calls might not be tracked as regular function calls
    // This is a limitation of the current implementation
    // Just verify the imports are correct
    assert!(
        !console_logger.imports.is_empty(),
        "console-logger.ts should have imports"
    );
}

#[test]
fn test_edge_type_querying() {
    let temp_dir = TempDir::new().unwrap();
    let base_path = temp_dir.path();

    // Create git directory
    fs::create_dir_all(base_path.join(".git")).unwrap();
    fs::create_dir_all(base_path.join("src")).unwrap();

    // Create a complex project structure
    fs::write(
        base_path.join("src/models.rs"),
        r#"
pub struct User {
    pub name: String,
}

pub trait Validator {
    fn validate(&self) -> bool;
}
"#,
    )
    .unwrap();

    fs::write(
        base_path.join("src/validators.rs"),
        r#"
use crate::models::{User, Validator};

impl Validator for User {
    fn validate(&self) -> bool {
        !self.name.is_empty()
    }
}
"#,
    )
    .unwrap();

    fs::write(
        base_path.join("src/main.rs"),
        r#"
mod models;
mod validators;

use models::{User, Validator};

fn main() {
    let user = User { name: "Alice".to_string() };
    if user.validate() {
        println!("Valid user");
    }
}
"#,
    )
    .unwrap();

    let walk_options = WalkOptions::default();
    let mut files = walk_directory(base_path, walk_options).unwrap();
    files.retain(|f| f.path.extension().is_some_and(|ext| ext == "rs"));

    let config = Config {
        trace_imports: true,
        include_types: true,
        include_callers: true,
        ..Default::default()
    };

    let cache = FileCache::new();
    perform_semantic_analysis_graph(&mut files, &config, &cache).unwrap();

    // Verify complex relationships in Rust code
    let main_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("main.rs"))
        .unwrap();
    let models_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("models.rs"))
        .unwrap();
    let validators_file = files
        .iter()
        .find(|f| f.path.to_string_lossy().contains("validators.rs"))
        .unwrap();

    // main.rs should import both models and validators modules
    assert!(!main_file.imports.is_empty(), "main.rs should have imports");

    // Check that imports are working in general
    assert!(
        !validators_file.imports.is_empty() || !models_file.imported_by.is_empty(),
        "There should be import relationships between files"
    );

    // Check type references
    let main_type_refs: Vec<&str> = main_file
        .type_references
        .iter()
        .map(|tr| tr.name.as_str())
        .collect();
    assert!(
        main_type_refs.contains(&"User") || !main_type_refs.is_empty(),
        "main.rs should have type references"
    );

    // Check function calls
    let main_func_calls: Vec<&str> = main_file
        .function_calls
        .iter()
        .map(|fc| fc.name.as_str())
        .collect();
    assert!(
        main_func_calls.contains(&"validate") || !main_func_calls.is_empty(),
        "main.rs should call validate method"
    );
}

#[test]
fn test_edge_type_serialization() {
    // For now, just verify that the types exist and basic functionality works
    // Full serialization testing would require exposing the graph structure

    let temp_dir = TempDir::new().unwrap();
    let base_path = temp_dir.path();

    // Create git directory and a simple file
    fs::create_dir_all(base_path.join(".git")).unwrap();
    fs::write(
        base_path.join("test.rs"),
        r#"
fn main() {
    println!("Hello");
}
"#,
    )
    .unwrap();

    let walk_options = WalkOptions::default();
    let mut files = walk_directory(base_path, walk_options).unwrap();
    files.retain(|f| f.path.extension().is_some_and(|ext| ext == "rs"));

    let config = Config {
        trace_imports: true,
        ..Default::default()
    };

    let cache = FileCache::new();

    // Just verify the analysis completes without errors
    assert!(
        perform_semantic_analysis_graph(&mut files, &config, &cache).is_ok(),
        "Semantic analysis should complete successfully"
    );
}