turboprop 0.1.2

Fast semantic code search and indexing tool
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
//! End-to-end CLI tests for model commands.
//!
//! These tests validate the complete CLI interface for model management
//! including list, info, download, and clear commands with proper
//! argument handling and output validation.
//!
//! Run these tests with: `cargo test cli_model`

use anyhow::Result;
use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;

/// Test model list command shows all available models
#[tokio::test]
async fn test_model_list_command() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("list");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Available Embedding Models"))
        .stdout(predicate::str::contains(
            "sentence-transformers/all-MiniLM-L6-v2",
        ))
        .stdout(predicate::str::contains("nomic-embed-code.Q5_K_S.gguf"))
        .stdout(predicate::str::contains("Qwen/Qwen3-Embedding-0.6B"))
        .stdout(predicate::str::contains("Dimensions:"))
        .stdout(predicate::str::contains("Size:"))
        .stdout(predicate::str::contains("Backend:"));

    Ok(())
}

/// Test model list command shows cache status
#[tokio::test]
async fn test_model_list_cache_status() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("list");

    let output = cmd.assert().success();

    // Should show cache status for each model
    output.stdout(
        predicate::str::contains("cached").or(predicate::str::contains("download required")),
    );

    Ok(())
}

/// Test model info command for sentence transformer model
#[tokio::test]
async fn test_model_info_sentence_transformer() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model")
        .arg("info")
        .arg("sentence-transformers/all-MiniLM-L6-v2");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Model Information"))
        .stdout(predicate::str::contains(
            "sentence-transformers/all-MiniLM-L6-v2",
        ))
        .stdout(predicate::str::contains("Dimensions: 384"))
        .stdout(predicate::str::contains("FastEmbed"))
        .stdout(predicate::str::contains("SentenceTransformer"))
        .stdout(predicate::str::contains("Cache Status:"));

    Ok(())
}

/// Test model info command for GGUF model
#[tokio::test]
async fn test_model_info_gguf() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model")
        .arg("info")
        .arg("nomic-embed-code.Q5_K_S.gguf");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Model Information"))
        .stdout(predicate::str::contains("nomic-embed-code.Q5_K_S.gguf"))
        .stdout(predicate::str::contains("Dimensions: 768"))
        .stdout(predicate::str::contains("Candle"))
        .stdout(predicate::str::contains("GGUF"))
        .stdout(predicate::str::contains("Download URL:"))
        .stdout(predicate::str::contains("Nomic Embed Code Features:"))
        .stdout(predicate::str::contains("code search"));

    Ok(())
}

/// Test model info command for Qwen3 model
#[tokio::test]
async fn test_model_info_qwen3() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model")
        .arg("info")
        .arg("Qwen/Qwen3-Embedding-0.6B");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Model Information"))
        .stdout(predicate::str::contains("Qwen/Qwen3-Embedding-0.6B"))
        .stdout(predicate::str::contains("Dimensions: 1024"))
        .stdout(predicate::str::contains("Custom"))
        .stdout(predicate::str::contains("HuggingFace"))
        .stdout(predicate::str::contains("Qwen3 Model Features:"))
        .stdout(predicate::str::contains("instruction-based embeddings"))
        .stdout(predicate::str::contains("Multilingual support"));

    Ok(())
}

/// Test model info command with nonexistent model
#[tokio::test]
async fn test_model_info_nonexistent() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("info").arg("nonexistent/model");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("not found"));

    Ok(())
}

/// Test model download command for FastEmbed model
#[tokio::test]
async fn test_model_download_fastembed() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model")
        .arg("download")
        .arg("sentence-transformers/all-MiniLM-L6-v2");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("FastEmbed model"))
        .stdout(predicate::str::contains("downloaded automatically"));

    Ok(())
}

/// Test model download command for nonexistent model
#[tokio::test]
async fn test_model_download_nonexistent() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("download").arg("nonexistent/model");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("not found"));

    Ok(())
}

/// Test model clear all command
#[tokio::test]
async fn test_model_clear_all() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("clear");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Clearing all model caches"))
        .stdout(predicate::str::contains("Successfully cleared"));

    Ok(())
}

/// Test model clear specific model command
#[tokio::test]
async fn test_model_clear_specific() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model")
        .arg("clear")
        .arg("sentence-transformers/all-MiniLM-L6-v2");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Clearing cache for model"))
        .stdout(predicate::str::contains(
            "sentence-transformers/all-MiniLM-L6-v2",
        ))
        .stdout(predicate::str::contains("Successfully cleared"));

    Ok(())
}

/// Check if tests should run in offline mode (default: offline)
fn is_offline_mode() -> bool {
    // Default to offline mode unless explicitly enabled online
    std::env::var("TURBOPROP_TEST_ONLINE").unwrap_or_default() != "1"
}

/// Test index command with custom model selection
#[tokio::test]
async fn test_index_with_custom_model() -> Result<()> {
    let temp_dir = TempDir::new()?;

    // Create a small test repository
    std::fs::write(
        temp_dir.path().join("test.js"),
        "function hello() { console.log('world'); }",
    )?;

    // Initialize git repository
    let mut git_init = Command::new("git");
    git_init
        .args(["init", "--quiet"])
        .current_dir(temp_dir.path());
    git_init.assert().success();

    // Add test file to git
    let mut git_add = Command::new("git");
    git_add
        .args(["add", "test.js"])
        .current_dir(temp_dir.path());
    git_add.assert().success();

    let offline_mode = is_offline_mode();
    let model = if offline_mode {
        "mock://test-model"
    } else {
        "sentence-transformers/all-MiniLM-L6-v2"
    };

    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("index")
        .arg("--repo")
        .arg(temp_dir.path())
        .arg("--model")
        .arg(model);

    if offline_mode {
        // In offline mode, we expect model-related failures and that's OK
        let result = cmd.assert();
        if result.try_success().is_err() {
            // Command failed due to mock model, which is expected in offline mode
            return Ok(());
        }
    } else {
        cmd.assert().success();
    }

    Ok(())
}

/// Test search command with custom model selection
#[tokio::test]
async fn test_search_with_custom_model() -> Result<()> {
    let temp_dir = TempDir::new()?;

    // Create and index a test repository first
    std::fs::write(
        temp_dir.path().join("test.py"),
        "def authenticate_user(username, password):\n    return username == 'admin' and password == 'secret'"
    )?;

    // Initialize git repository
    let mut git_init = Command::new("git");
    git_init
        .args(["init", "--quiet"])
        .current_dir(temp_dir.path());
    git_init.assert().success();

    // Add test file to git
    let mut git_add = Command::new("git");
    git_add
        .args(["add", "test.py"])
        .current_dir(temp_dir.path());
    git_add.assert().success();

    let offline_mode = is_offline_mode();
    let model = if offline_mode {
        "mock://test-model"
    } else {
        "sentence-transformers/all-MiniLM-L6-v2"
    };

    // Index first
    let mut index_cmd = Command::cargo_bin("tp")?;
    index_cmd
        .arg("index")
        .arg("--repo")
        .arg(temp_dir.path())
        .arg("--model")
        .arg(model);

    if offline_mode {
        // In offline mode, indexing may fail due to mock model, which is expected
        let result = index_cmd.assert();
        if result.try_success().is_err() {
            // Indexing failed, so search will also fail - return early
            return Ok(());
        }
    } else {
        index_cmd.assert().success();
    }

    // Then search
    let mut search_cmd = Command::cargo_bin("tp")?;
    search_cmd
        .arg("search")
        .arg("authentication")
        .arg("--repo")
        .arg(temp_dir.path())
        .arg("--model")
        .arg(model);

    if offline_mode {
        // In offline mode, search may fail due to mock model or missing index
        let result = search_cmd.assert();
        if result.try_success().is_err() {
            // Search failed due to mock model, which is expected in offline mode
            return Ok(());
        }
    } else {
        search_cmd
            .assert()
            .success()
            .stdout(predicate::str::contains("authenticate_user"));
    }

    Ok(())
}

/// Test CLI help for model commands
#[tokio::test]
async fn test_model_help() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("--help");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("model management commands"))
        .stdout(predicate::str::contains("list"))
        .stdout(predicate::str::contains("info"))
        .stdout(predicate::str::contains("download"))
        .stdout(predicate::str::contains("clear"));

    Ok(())
}

/// Test CLI help for specific model subcommands
#[tokio::test]
async fn test_model_list_help() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("list").arg("--help");

    cmd.assert().success().stdout(predicate::str::contains(
        "List all available embedding models",
    ));

    Ok(())
}

#[tokio::test]
async fn test_model_info_help() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("info").arg("--help");

    cmd.assert().success().stdout(predicate::str::contains(
        "Show detailed information about a specific model",
    ));

    Ok(())
}

#[tokio::test]
async fn test_model_download_help() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("download").arg("--help");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Download a specific model"));

    Ok(())
}

#[tokio::test]
async fn test_model_clear_help() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("clear").arg("--help");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Clear model cache"));

    Ok(())
}

/// Test error handling for missing arguments
#[tokio::test]
async fn test_model_info_missing_argument() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("info");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("required"));

    Ok(())
}

#[tokio::test]
async fn test_model_download_missing_argument() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("download");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("required"));

    Ok(())
}

/// Test JSON output format (if supported)
#[tokio::test]
async fn test_model_list_output_format() -> Result<()> {
    let mut cmd_default = Command::cargo_bin("tp")?;
    cmd_default.arg("model").arg("list");

    let output = cmd_default.assert().success();

    // Verify human-readable output contains expected formatting
    output
        .stdout(predicate::str::contains("Available Embedding Models"))
        .stdout(predicate::str::contains("Description:"))
        .stdout(predicate::str::contains("Type:"))
        .stdout(predicate::str::contains("Backend:"))
        .stdout(predicate::str::contains("Dimensions:"))
        .stdout(predicate::str::contains("Size:"));

    Ok(())
}

/// Test model command with verbose output
#[tokio::test]
async fn test_model_list_verbose() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model").arg("list").arg("--verbose");

    // May not be implemented yet, but should not crash
    let result = cmd.assert();

    // Either succeeds with verbose output or fails gracefully
    match result.try_success() {
        Ok(output) => {
            output.stdout(predicate::str::contains("Available Embedding Models"));
        }
        Err(_) => {
            // Verbose flag might not be implemented yet
            // Test that it fails gracefully
        }
    }

    Ok(())
}

/// Test model commands with different log levels
#[tokio::test]
async fn test_model_command_with_log_level() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.env("RUST_LOG", "debug").arg("model").arg("list");

    cmd.assert().success();

    Ok(())
}

/// Test model caching behavior through CLI
#[tokio::test]
async fn test_model_caching_through_cli() -> Result<()> {
    let temp_dir = TempDir::new()?;

    // Set custom cache directory
    let cache_dir = temp_dir.path().join("custom_cache");
    std::fs::create_dir_all(&cache_dir)?;

    let mut cmd = Command::cargo_bin("tp")?;
    cmd.env("TURBOPROP_CACHE_DIR", cache_dir.to_str().unwrap())
        .arg("model")
        .arg("list");

    cmd.assert().success();

    Ok(())
}

#[tokio::test]
#[ignore] // Requires Qwen3 instruction support to be fully implemented
async fn test_qwen3_instruction_cli() -> Result<()> {
    let temp_dir = TempDir::new()?;

    std::fs::write(
        temp_dir.path().join("code.rs"),
        "fn main() { println!(\"Hello, world!\"); }",
    )?;

    // Initialize git repository
    let mut git_init = Command::new("git");
    git_init
        .args(["init", "--quiet"])
        .current_dir(temp_dir.path());
    git_init.assert().success();

    // Add test file to git
    let mut git_add = Command::new("git");
    git_add
        .args(["add", "code.rs"])
        .current_dir(temp_dir.path());
    git_add.assert().success();

    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("index")
        .arg("--repo")
        .arg(temp_dir.path())
        .arg("--model")
        .arg("Qwen/Qwen3-Embedding-0.6B")
        .arg("--instruction")
        .arg("Represent this code for search");

    // This test may be ignored if Qwen3 model is not fully available
    // When implemented, it should succeed
    cmd.assert().success();

    Ok(())
}

/// Test model download with progress indication (if supported)
#[tokio::test]
#[ignore] // Requires actual model download which is slow
async fn test_model_download_with_progress() -> Result<()> {
    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model")
        .arg("download")
        .arg("nomic-embed-code.Q5_K_S.gguf");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Downloading"))
        .stdout(predicate::str::contains("Successfully downloaded"));

    Ok(())
}

/// Test model download for HuggingFace model (placeholder behavior)
#[tokio::test]
async fn test_model_download_huggingface_placeholder() -> Result<()> {
    let offline_mode = is_offline_mode();

    if offline_mode {
        // Skip this test in offline mode as it requires actual directory creation
        return Ok(());
    }

    let mut cmd = Command::cargo_bin("tp")?;
    cmd.arg("model")
        .arg("download")
        .arg("Qwen/Qwen3-Embedding-0.6B");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Downloading Hugging Face model"))
        .stdout(predicate::str::contains("placeholder"));

    Ok(())
}