directory-indexer 0.0.10

AI-powered directory indexing with semantic search for MCP servers
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
// CLI integration tests

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::sync::Mutex;
use tempfile::TempDir;

// Mutex to ensure only one test creates the shared collection at a time
static COLLECTION_INIT_LOCK: Mutex<()> = Mutex::new(());

fn ensure_shared_collection_exists() {
    let _lock = COLLECTION_INIT_LOCK.lock().unwrap();

    // Use a semantic test name to get the shared collection
    let shared_test_name = "semantic-init";

    eprintln!("=== ensure_shared_collection_exists: Starting ===");

    // Check if collection already has data to avoid re-indexing
    eprintln!("=== Checking if collection has data ===");
    let test_search = test_command(shared_test_name)
        .arg("search")
        .arg("test")
        .arg("--limit")
        .arg("1")
        .timeout(std::time::Duration::from_secs(10))
        .output();

    match test_search {
        Ok(output) => {
            let stdout = String::from_utf8_lossy(&output.stdout);
            let stderr = String::from_utf8_lossy(&output.stderr);
            eprintln!("=== Search test output ===");
            eprintln!("STDOUT: {}", stdout);
            eprintln!("STDERR: {}", stderr);
            eprintln!("Exit code: {}", output.status);

            if !stdout.contains("No results found") {
                eprintln!("=== Collection has data, skipping indexing ===");
                return;
            } else {
                eprintln!("=== No results found, need to index ===");
            }
        }
        Err(e) => {
            eprintln!("=== Search failed: {}, need to index ===", e);
        }
    }

    // Collection doesn't exist or is empty, index test data
    let test_data_path = get_test_data_path();
    eprintln!("=== Indexing test data from: {} ===", test_data_path);

    let index_result = test_command(shared_test_name)
        .arg("index")
        .arg(&test_data_path)
        .timeout(std::time::Duration::from_secs(120))
        .output()
        .expect("Failed to run index command");

    eprintln!("=== Index result ===");
    eprintln!("STDOUT: {}", String::from_utf8_lossy(&index_result.stdout));
    eprintln!("STDERR: {}", String::from_utf8_lossy(&index_result.stderr));
    eprintln!("Exit code: {}", index_result.status);

    if !index_result.status.success() {
        panic!("Index command failed: {:?}", index_result.status);
    }

    eprintln!("=== ensure_shared_collection_exists: Complete ===");
}

fn test_command(test_name: &str) -> Command {
    let mut cmd = Command::cargo_bin("directory-indexer").unwrap();

    // Determine collection name based on whether we're using temp data or test_data
    let collection_name = if test_name.starts_with("semantic-")
        || test_name.contains("test-data")
        || test_name == "search-path-filter"
        || test_name == "search-limit"
        || test_name == "similar-workflow"
    {
        // Use shared collection for tests that work with test_data
        std::env::var("DIRECTORY_INDEXER_QDRANT_COLLECTION")
            .unwrap_or_else(|_| "directory-indexer-integration-test".to_string())
    } else {
        // Use unique collection for tests with temp data
        format!("di-test-cli-{}", test_name)
    };

    cmd.env("DIRECTORY_INDEXER_QDRANT_COLLECTION", &collection_name);
    eprintln!(
        "Test '{}' using collection env var: {}",
        test_name, collection_name
    );
    eprintln!(
        "CI should have pre-indexed into: {}",
        std::env::var("DIRECTORY_INDEXER_QDRANT_COLLECTION").unwrap_or("NOT_SET".to_string())
    );
    cmd
}

fn are_services_available() -> bool {
    let qdrant_endpoint =
        std::env::var("QDRANT_ENDPOINT").unwrap_or_else(|_| "http://localhost:6333".to_string());
    let ollama_endpoint =
        std::env::var("OLLAMA_ENDPOINT").unwrap_or_else(|_| "http://localhost:11434".to_string());

    let qdrant_available = std::process::Command::new("curl")
        .args(["-s", &format!("{}/", qdrant_endpoint), "-o", "/dev/null"])
        .status()
        .map(|s| s.success())
        .unwrap_or(false);

    let ollama_available = std::process::Command::new("curl")
        .args([
            "-s",
            &format!("{}/api/tags", ollama_endpoint),
            "-o",
            "/dev/null",
        ])
        .status()
        .map(|s| s.success())
        .unwrap_or(false);

    qdrant_available && ollama_available
}

fn create_simple_test_files(dir: &TempDir) -> std::io::Result<()> {
    fs::write(
        dir.path().join("readme.md"),
        "# Project README\nThis is documentation about the project.",
    )?;
    fs::write(
        dir.path().join("main.rs"),
        "fn main() {\n    println!(\"Hello, world!\");\n}",
    )?;
    fs::write(
        dir.path().join("config.json"),
        r#"{"name": "test", "version": "1.0"}"#,
    )?;
    Ok(())
}

#[test]
fn test_basic_workflow() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    create_simple_test_files(&temp_dir).unwrap();
    let test_path = temp_dir.path().to_str().unwrap();

    // Test index command
    test_command("basic-workflow")
        .arg("index")
        .arg(test_path)
        .timeout(std::time::Duration::from_secs(60))
        .assert()
        .success()
        .stdout(predicate::str::contains("Indexing"));

    // Test search command
    test_command("basic-workflow")
        .arg("search")
        .arg("documentation")
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success();

    // Test status command
    test_command("basic-workflow")
        .arg("status")
        .timeout(std::time::Duration::from_secs(10))
        .assert()
        .success()
        .stdout(predicate::str::contains("Status"));
}

#[test]
fn test_search_with_options() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    create_simple_test_files(&temp_dir).unwrap();
    let test_path = temp_dir.path().to_str().unwrap();

    // Index first
    test_command("search-options")
        .arg("index")
        .arg(test_path)
        .timeout(std::time::Duration::from_secs(60))
        .assert()
        .success();

    // Test search with path filter
    test_command("search-options")
        .arg("search")
        .arg("project")
        .arg("--path")
        .arg(test_path)
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success();

    // Test search with limit
    test_command("search-options")
        .arg("search")
        .arg("project")
        .arg("--limit")
        .arg("5")
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success();
}

#[test]
fn test_similar_command() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    create_simple_test_files(&temp_dir).unwrap();
    let test_path = temp_dir.path().to_str().unwrap();
    let readme_path = temp_dir.path().join("readme.md");

    // Index first
    test_command("similar-command")
        .arg("index")
        .arg(test_path)
        .timeout(std::time::Duration::from_secs(60))
        .assert()
        .success();

    // Test similar command
    test_command("similar-command")
        .arg("similar")
        .arg(readme_path.to_str().unwrap())
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success();
}

#[test]
fn test_get_command() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    let temp_dir = TempDir::new().unwrap();
    create_simple_test_files(&temp_dir).unwrap();
    let test_path = temp_dir.path().to_str().unwrap();
    let readme_path = temp_dir.path().join("readme.md");

    // Index first
    test_command("get-command")
        .arg("index")
        .arg(test_path)
        .timeout(std::time::Duration::from_secs(60))
        .assert()
        .success();

    // Test get command
    test_command("get-command")
        .arg("get")
        .arg(readme_path.to_str().unwrap())
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success()
        .stdout(predicate::str::contains("README"));
}

#[test]
fn test_error_handling() {
    // Test invalid commands
    test_command("error-handling")
        .arg("nonexistent-command")
        .assert()
        .failure();

    // Test missing arguments
    test_command("error-handling")
        .arg("search")
        .assert()
        .failure();

    test_command("error-handling")
        .arg("similar")
        .assert()
        .failure();

    test_command("error-handling").arg("get").assert().failure();
}

#[tokio::test]
async fn test_health_check_functions() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    let config = directory_indexer::config::Config::load().expect("Config should load");

    // Test system health check
    let health = directory_indexer::health::check_system_health(&config).await;
    assert!(health.is_ready_for_indexing() || health.is_ready_for_retrieval());

    // Test embedding generation
    let result = directory_indexer::health::test_embedding_generation(&config).await;
    // Should either succeed or fail gracefully
    assert!(result.is_ok() || result.is_err());
}

#[tokio::test]
async fn test_qdrant_delete_operations() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    let config = directory_indexer::config::Config::load().expect("Config should load");
    let store = directory_indexer::storage::QdrantStore::new(
        &config.storage.qdrant.endpoint,
        "test-delete-ops".to_string(),
    )
    .await
    .expect("Store should be created");

    // Test delete points by file
    let result = store.delete_points_by_file("/test/file.txt").await;
    assert!(result.is_ok(), "Delete points by file should succeed");

    // Test delete collection
    let result = store.delete_collection().await;
    assert!(result.is_ok(), "Delete collection should succeed");
}

// ============================================================================
// Semantic Search Quality Tests using test_data
// ============================================================================

fn get_test_data_path() -> String {
    std::env::current_dir()
        .unwrap()
        .join("test_data")
        .to_string_lossy()
        .to_string()
}

#[test]
fn test_semantic_search_authentication() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    // Ensure shared collection exists and has test data
    ensure_shared_collection_exists();

    // Search for authentication - should find API guide
    let output = test_command("semantic-auth")
        .arg("search")
        .arg("authentication")
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success()
        .get_output()
        .clone();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stdout.contains("api_guide.md") || stdout.contains("Search Results"),
        "Should find API guide when searching for authentication. \nSTDOUT: {}\nSTDERR: {}",
        stdout,
        stderr
    );
}

#[test]
fn test_semantic_search_error_handling() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    // Ensure shared collection exists and has test data
    ensure_shared_collection_exists();

    // Search for error handling - should find troubleshooting docs
    let output = test_command("semantic-error")
        .arg("search")
        .arg("error handling")
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success()
        .get_output()
        .clone();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stdout.contains("troubleshooting.md") || stdout.contains("Search Results"),
        "Should find troubleshooting guide when searching for error handling. \nSTDOUT: {}\nSTDERR: {}",
        stdout, stderr
    );
}

#[test]
fn test_semantic_search_programming() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    // Ensure shared collection exists and has test data
    ensure_shared_collection_exists();

    // Search for rust programming - should find rust files
    let output = test_command("semantic-prog")
        .arg("search")
        .arg("rust programming")
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success()
        .get_output()
        .clone();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stdout.contains("rust.txt")
            || stdout.contains("hello.rs")
            || stdout.contains("Search Results"),
        "Should find Rust files when searching for rust programming. \nSTDOUT: {}\nSTDERR: {}",
        stdout,
        stderr
    );
}

#[test]
fn test_search_with_path_filter() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    let test_data_path = get_test_data_path();
    let programming_path = std::path::Path::new(&test_data_path).join("programming");

    // Ensure shared collection exists and has test data
    ensure_shared_collection_exists();

    // Search within programming directory only
    test_command("search-path-filter")
        .arg("search")
        .arg("function")
        .arg("--path")
        .arg(programming_path.to_str().unwrap())
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success();
}

#[test]
fn test_search_with_limit() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    // Ensure shared collection exists and has test data
    ensure_shared_collection_exists();

    // Search with limit
    test_command("search-limit")
        .arg("search")
        .arg("configuration")
        .arg("--limit")
        .arg("2")
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success();
}

#[test]
fn test_similar_files_workflow() {
    if !are_services_available() {
        println!("Skipping test - required services not available");
        return;
    }

    let test_data_path = get_test_data_path();
    let hello_rs_path = std::path::Path::new(&test_data_path)
        .join("programming")
        .join("hello.rs");

    // Ensure shared collection exists and has test data
    ensure_shared_collection_exists();

    // Find files similar to hello.rs
    test_command("similar-workflow")
        .arg("similar")
        .arg(hello_rs_path.to_str().unwrap())
        .timeout(std::time::Duration::from_secs(30))
        .assert()
        .success();
}