aprender-shell 0.33.0

AI-powered shell completion trained on your history
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
//! CLI Integration Tests for aprender-shell
//!
//! Uses assert_cmd (MANDATORY) for end-to-end CLI testing.
//! Tests actual binary execution with real inputs/outputs.

#![allow(clippy::unwrap_used)] // Tests can use unwrap for simplicity
#![allow(deprecated)] // cargo_bin still works, just deprecated for custom build-dir

use assert_cmd::Command;
use predicates::prelude::*;
use std::io::Write;
use tempfile::NamedTempFile;

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

/// Create an aprender-shell command (MANDATORY pattern)
fn aprender_shell() -> Command {
    Command::cargo_bin("aprender-shell").expect("Failed to find aprender-shell binary")
}

/// Create a temporary history file with given commands
fn create_temp_history(commands: &[&str]) -> NamedTempFile {
    let mut file = NamedTempFile::new().expect("Failed to create temp file");
    for cmd in commands {
        writeln!(file, "{}", cmd).expect("Failed to write command");
    }
    file
}

/// Create a ZSH-style history file with timestamps
fn create_zsh_history(commands: &[&str]) -> NamedTempFile {
    let mut file = NamedTempFile::new().expect("Failed to create temp file");
    for (i, cmd) in commands.iter().enumerate() {
        writeln!(file, ": {}:0;{}", 1700000000 + i, cmd).expect("Failed to write command");
    }
    file
}

// ============================================================================
// Test: CLI_001 - Help and Version
// ============================================================================

#[test]
fn test_cli_001_help_flag() {
    aprender_shell()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("aprender-shell"))
        .stdout(predicate::str::contains("AI-powered shell completion"));
}

#[test]
fn test_cli_001_version_flag() {
    aprender_shell()
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("aprender-shell"));
}

#[test]
fn test_cli_001_subcommand_help() {
    aprender_shell()
        .args(["train", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Train a model"));
}

// ============================================================================
// Test: CLI_002 - Train Command
// ============================================================================

#[test]
fn test_cli_002_train_basic() {
    let history = create_temp_history(&[
        "git status",
        "git commit -m test",
        "git push",
        "cargo build",
        "cargo test",
    ]);

    let output = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            output.path().to_str().unwrap(),
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("Training"))
        .stdout(predicate::str::contains("Model saved"));
}

#[test]
fn test_cli_002_train_filters_corrupted() {
    // Train with corrupted commands - they should be filtered
    let history = create_temp_history(&[
        "git status",
        "git commit-m test", // corrupted - should be filtered
        "git push",
        "cargo build-r", // corrupted - should be filtered
        "cargo test",
    ]);

    let output = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            output.path().to_str().unwrap(),
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("Commands loaded: 3")); // Only 3 valid
}

#[test]
fn test_cli_002_train_zsh_format() {
    let history = create_zsh_history(&["git status", "git commit -m test", "ls -la"]);

    let output = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            output.path().to_str().unwrap(),
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("Commands loaded: 3"));
}

// ============================================================================
// Test: CLI_003 - Suggest Command
// ============================================================================

#[test]
fn test_cli_003_suggest_basic() {
    // First train a model
    let history = create_temp_history(&[
        "git status",
        "git status",
        "git commit -m test",
        "git push origin main",
    ]);

    let model = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            model.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    // Now test suggestions
    aprender_shell()
        .args(["suggest", "git ", "-m", model.path().to_str().unwrap()])
        .assert()
        .success()
        .stdout(predicate::str::contains("git status")); // Most frequent
}

#[test]
fn test_cli_003_suggest_partial_token() {
    let history = create_temp_history(&[
        "git commit -m test",
        "git checkout main",
        "git clone url",
        "git status",
    ]);

    let model = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            model.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    // Partial token "git c" should suggest commit/checkout/clone
    aprender_shell()
        .args(["suggest", "git c", "-m", model.path().to_str().unwrap()])
        .assert()
        .success()
        .stdout(predicate::str::contains("git c")); // All should start with "git c"
}

#[test]
fn test_cli_003_suggest_no_corrupted() {
    let history = create_temp_history(&[
        "git commit -m test",
        "git commit-m broken", // corrupted
        "git checkout main",
    ]);

    let model = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            model.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    // Should NOT suggest corrupted "commit-m"
    aprender_shell()
        .args(["suggest", "git co", "-m", model.path().to_str().unwrap()])
        .assert()
        .success()
        .stdout(predicate::str::contains("commit-m").not());
}

// ============================================================================
// Test: CLI_004 - Stats Command
// ============================================================================

#[test]
fn test_cli_004_stats() {
    let history = create_temp_history(&["git status", "git commit -m test", "cargo build"]);

    let model = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            model.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    aprender_shell()
        .args(["stats", "-m", model.path().to_str().unwrap()])
        .assert()
        .success()
        .stdout(predicate::str::contains("N-gram size"))
        .stdout(predicate::str::contains("Vocabulary size"));
}

// ============================================================================
// Test: CLI_005 - Validate Command
// ============================================================================

#[test]
fn test_cli_005_validate() {
    // Validate trains its own model internally using train/test split
    let history = create_temp_history(&[
        "git status",
        "git status",
        "git commit -m test",
        "git push",
        "cargo build",
        "cargo test",
        "cargo run",
        "ls -la",
        "cd src",
        "cat file.txt",
    ]);

    aprender_shell()
        .args(["validate", "-f", history.path().to_str().unwrap()])
        .assert()
        .success()
        .stdout(predicate::str::contains("VALIDATION RESULTS"))
        .stdout(predicate::str::contains("Hit@"));
}

// ============================================================================
// Test: CLI_006 - Augment Command (Synthetic Data)
// ============================================================================

#[test]
fn test_cli_006_augment_basic() {
    let history = create_temp_history(&[
        "git status",
        "git commit -m test",
        "git push origin main",
        "cargo build --release",
        "cargo test --all",
    ]);

    let model = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "augment",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            model.path().to_str().unwrap(),
            "-a",
            "0.5", // 50% augmentation
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("Data Augmentation"))
        .stdout(predicate::str::contains("Coverage"));
}

#[test]
fn test_cli_006_augment_with_diversity() {
    let history = create_temp_history(&[
        "git status",
        "git commit -m test",
        "git push",
        "cargo build",
        "cargo test",
    ]);

    let model = NamedTempFile::new().unwrap();

    aprender_shell()
        .args([
            "augment",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            model.path().to_str().unwrap(),
            "--monitor-diversity",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("Diversity"));
}

// ============================================================================
// Test: CLI_007 - Error Handling
// ============================================================================

#[test]
fn test_cli_007_missing_history_file() {
    aprender_shell()
        .args(["train", "-f", "/nonexistent/path/history"])
        .assert()
        .failure();
}

#[test]
fn test_cli_007_invalid_ngram_size() {
    let history = create_temp_history(&["git status"]);

    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-n",
            "99", // Invalid - should be 2-5
        ])
        .assert()
        .failure() // Rejects invalid n-gram sizes
        .stderr(predicate::str::contains("N-gram size must be between 2 and 5"));
}

// ============================================================================
// Test: CLI_008 - ZSH Widget Generation
// ============================================================================

#[test]
fn test_cli_008_zsh_widget() {
    aprender_shell()
        .arg("zsh-widget")
        .assert()
        .success()
        .stdout(predicate::str::contains("aprender-shell ZSH widget"))
        .stdout(predicate::str::contains("_aprender_suggest"))
        .stdout(predicate::str::contains("bindkey"));
}

// ============================================================================
// Test: CLI_009 - Export/Import
// ============================================================================

#[test]
fn test_cli_009_export_import_roundtrip() {
    let history = create_temp_history(&["git status", "git commit -m test", "cargo build"]);

    let model = NamedTempFile::new().unwrap();
    let export_file = NamedTempFile::new().unwrap();
    let imported_model = NamedTempFile::new().unwrap();

    // Train
    aprender_shell()
        .args([
            "train",
            "-f",
            history.path().to_str().unwrap(),
            "-o",
            model.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    // Export
    aprender_shell()
        .args([
            "export",
            export_file.path().to_str().unwrap(),
            "-m",
            model.path().to_str().unwrap(),
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("exported"));

    // Import
    aprender_shell()
        .args([
            "import",
            export_file.path().to_str().unwrap(),
            "-o",
            imported_model.path().to_str().unwrap(),
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("imported"));
}

include!("parts/cli_integration_010.rs");
include!("parts/cli_integration_017.rs");
include!("parts/cli_integration_021.rs");