command-vault 0.3.0

An advanced command history manager with tagging and search capabilities
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
use anyhow::Result;
use command_vault::cli::args::{Cli, Commands, TagCommands};
use clap::Parser;

#[test]
fn test_add_command_parsing() -> Result<()> {
    // Test basic command without tags
    let args = Cli::try_parse_from([
        "command-vault",
        "add",
        "--",
        "git",
        "commit",
        "-m",
        "test",
    ])?;

    match args.command {
        Commands::Add { command, tags } => {
            assert_eq!(command.join(" "), "git commit -m test");
            assert_eq!(tags, Vec::<String>::new());
        }
        _ => panic!("Expected Add command"),
    }

    // Test with tags
    let args = Cli::try_parse_from([
        "command-vault",
        "add",
        "--tags",
        "git",
        "--tags",
        "vcs",
        "--",
        "git",
        "commit",
        "-m",
        "test",
    ])?;

    match args.command {
        Commands::Add { command, tags } => {
            assert_eq!(command.join(" "), "git commit -m test");
            assert_eq!(tags, vec!["git", "vcs"]);
        }
        _ => panic!("Expected Add command"),
    }

    // Test with multiple words in command (no dashes)
    let args = Cli::try_parse_from([
        "command-vault",
        "add",
        "--",
        "echo",
        "hello world",
    ])?;

    match args.command {
        Commands::Add { command, tags } => {
            assert_eq!(command.join(" "), "echo hello world");
            assert_eq!(tags, Vec::<String>::new());
        }
        _ => panic!("Expected Add command"),
    }

    // Test add command without any command (missing --)
    let result = Cli::try_parse_from([
        "command-vault",
        "add",
    ]);
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("the following required arguments were not provided"));

    Ok(())
}

#[test]
fn test_search_command_parsing() -> Result<()> {
    let args = Cli::try_parse_from([
        "command-vault",
        "search",
        "git commit",
        "--limit",
        "5",
    ])?;

    match args.command {
        Commands::Search { query, limit } => {
            assert_eq!(query, "git commit");
            assert_eq!(limit, 5);
        }
        _ => panic!("Expected Search command"),
    }
    Ok(())
}

#[test]
fn test_ls_command_parsing() -> Result<()> {
    let args = Cli::try_parse_from([
        "command-vault",
        "ls",
        "--limit",
        "20",
        "--asc",
    ])?;

    match args.command {
        Commands::Ls { limit, asc } => {
            assert_eq!(limit, 20);
            assert!(asc);
        }
        _ => panic!("Expected Ls command"),
    }
    Ok(())
}

#[test]
fn test_ls_command_default_behavior() -> Result<()> {
    // Test ls with default values
    let args = Cli::try_parse_from([
        "command-vault",
        "ls",
    ])?;

    match args.command {
        Commands::Ls { limit, asc } => {
            assert_eq!(limit, 50); // Default limit is 50
            assert!(!asc); // Default is descending order
        }
        _ => panic!("Expected Ls command"),
    }
    Ok(())
}

#[test]
fn test_tag_commands_parsing() -> Result<()> {
    // Test tag add
    let args = Cli::try_parse_from([
        "command-vault",
        "tag",
        "add",
        "1",
        "important",
        "urgent",
    ])?;

    match args.command {
        Commands::Tag { action: TagCommands::Add { command_id, tags } } => {
            assert_eq!(command_id, 1);
            assert_eq!(tags, vec!["important", "urgent"]);
        }
        _ => panic!("Expected Tag Add command"),
    }

    // Test tag remove
    let args = Cli::try_parse_from([
        "command-vault",
        "tag",
        "remove",
        "1",
        "urgent",
    ])?;

    match args.command {
        Commands::Tag { action: TagCommands::Remove { command_id, tag } } => {
            assert_eq!(command_id, 1);
            assert_eq!(tag, "urgent");
        }
        _ => panic!("Expected Tag Remove command"),
    }

    // Test tag list
    let args = Cli::try_parse_from([
        "command-vault",
        "tag",
        "list",
    ])?;

    match args.command {
        Commands::Tag { action: TagCommands::List } => (),
        _ => panic!("Expected Tag List command"),
    }

    // Test tag search
    let args = Cli::try_parse_from([
        "command-vault",
        "tag",
        "search",
        "git",
        "--limit",
        "5",
    ])?;

    match args.command {
        Commands::Tag { action: TagCommands::Search { tag, limit } } => {
            assert_eq!(tag, "git");
            assert_eq!(limit, 5);
        }
        _ => panic!("Expected Tag Search command"),
    }
    Ok(())
}

#[test]
fn test_exec_command_parsing() -> Result<()> {
    let args = Cli::try_parse_from([
        "command-vault",
        "exec",
        "42",
    ])?;

    match args.command {
        Commands::Exec { command_id, debug } => {
            assert_eq!(command_id, 42);
            assert_eq!(debug, false);
        }
        _ => panic!("Expected Exec command"),
    }
    Ok(())
}

#[test]
fn test_parse_exec_command() {
    let args = vec!["command-vault", "exec", "123"];
    let cli = Cli::try_parse_from(args).unwrap();
    match cli.command {
        Commands::Exec { command_id, debug } => {
            assert_eq!(command_id, 123);
            assert_eq!(debug, false);
        }
        _ => panic!("Expected Exec command"),
    }

    // Test with debug flag
    let args = vec!["command-vault", "exec", "123", "--debug"];
    let cli = Cli::try_parse_from(args).unwrap();
    match cli.command {
        Commands::Exec { command_id, debug } => {
            assert_eq!(command_id, 123);
            assert_eq!(debug, true);
        }
        _ => panic!("Expected Exec command"),
    }
}

#[test]
fn test_invalid_command_id() {
    let result = Cli::try_parse_from([
        "command-vault",
        "exec",
        "not_a_number",
    ]);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("invalid value 'not_a_number'"));
}

#[test]
fn test_missing_required_args() {
    let result = Cli::try_parse_from([
        "command-vault",
        "search",
    ]);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("<QUERY>"));
}

#[test]
fn test_search_command_default_limit() -> Result<()> {
    // Test search with default limit
    let args = Cli::try_parse_from([
        "command-vault",
        "search",
        "git commit",
    ])?;

    match args.command {
        Commands::Search { query, limit } => {
            assert_eq!(query, "git commit");
            assert_eq!(limit, 10); // Default limit is 10
        }
        _ => panic!("Expected Search command"),
    }
    Ok(())
}

#[test]
fn test_delete_command_parsing() -> Result<()> {
    // Test basic delete
    let args = Cli::try_parse_from([
        "command-vault",
        "delete",
        "42",
    ])?;

    match args.command {
        Commands::Delete { command_id } => {
            assert_eq!(command_id, 42);
        }
        _ => panic!("Expected Delete command"),
    }

    // Test missing command ID
    let result = Cli::try_parse_from([
        "command-vault",
        "delete",
    ]);
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("required"));

    Ok(())
}

#[test]
fn test_shell_init_command_parsing() -> Result<()> {
    // Test default shell initialization
    let args = Cli::try_parse_from([
        "command-vault",
        "shell-init",
    ])?;

    match args.command {
        Commands::ShellInit { shell } => {
            assert!(shell.is_none());
        }
        _ => panic!("Expected ShellInit command"),
    }

    // Test explicit shell override
    let args = Cli::try_parse_from([
        "command-vault",
        "shell-init",
        "--shell",
        "fish",
    ])?;

    match args.command {
        Commands::ShellInit { shell } => {
            assert_eq!(shell, Some("fish".to_string()));
        }
        _ => panic!("Expected ShellInit command"),
    }

    Ok(())
}

#[test]
fn test_tag_commands_all() -> Result<()> {
    // Test tag remove
    let args = Cli::try_parse_from([
        "command-vault",
        "tag",
        "remove",
        "1",
        "git",
    ])?;

    match args.command {
        Commands::Tag { action: TagCommands::Remove { command_id, tag } } => {
            assert_eq!(command_id, 1);
            assert_eq!(tag, "git");
        }
        _ => panic!("Expected Tag Remove command"),
    }

    // Test tag list
    let args = Cli::try_parse_from([
        "command-vault",
        "tag",
        "list",
    ])?;

    match args.command {
        Commands::Tag { action: TagCommands::List } => (),
        _ => panic!("Expected Tag List command"),
    }

    // Test tag search
    let args = Cli::try_parse_from([
        "command-vault",
        "tag",
        "search",
        "git",
        "--limit",
        "5",
    ])?;

    match args.command {
        Commands::Tag { action: TagCommands::Search { tag, limit } } => {
            assert_eq!(tag, "git");
            assert_eq!(limit, 5);
        }
        _ => panic!("Expected Tag Search command"),
    }

    // Test tag search with default limit
    let args = Cli::try_parse_from([
        "command-vault",
        "tag",
        "search",
        "git",
    ])?;

    match args.command {
        Commands::Tag { action: TagCommands::Search { tag, limit } } => {
            assert_eq!(tag, "git");
            assert_eq!(limit, 10); // Default limit is 10
        }
        _ => panic!("Expected Tag Search command"),
    }

    Ok(())
}

#[test]
fn test_add_command_with_parameters() -> Result<()> {
    // Test add command with basic parameter
    let args = Cli::try_parse_from([
        "command-vault",
        "add",
        "--",
        "touch",
        "@filename",
    ])?;

    match args.command {
        Commands::Add { command, tags } => {
            assert_eq!(command.join(" "), "touch @filename");
            assert_eq!(tags, Vec::<String>::new());
        }
        _ => panic!("Expected Add command"),
    }

    // Test add command with parameter description
    let args = Cli::try_parse_from([
        "command-vault",
        "add",
        "--",
        "touch",
        "@filename:Name of file to create",
    ])?;

    match args.command {
        Commands::Add { command, tags } => {
            assert_eq!(command.join(" "), "touch @filename:Name of file to create");
            assert_eq!(tags, Vec::<String>::new());
        }
        _ => panic!("Expected Add command"),
    }

    // Test add command with parameter default value
    let args = Cli::try_parse_from([
        "command-vault",
        "add",
        "--",
        "touch",
        "@filename:Name of file to create=test.txt",
    ])?;

    match args.command {
        Commands::Add { command, tags } => {
            assert_eq!(command.join(" "), "touch @filename:Name of file to create=test.txt");
            assert_eq!(tags, Vec::<String>::new());
        }
        _ => panic!("Expected Add command"),
    }

    Ok(())
}