rusty-commit 1.0.27

Rust-powered AI commit message generator - Write impressive commits in seconds
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
#![allow(deprecated)]
#![allow(
    clippy::field_reassign_with_default,
    clippy::assertions_on_constants,
    clippy::overly_complex_bool_expr,
    clippy::useless_vec
)]

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::process::Command as StdCommand;
use tempfile::tempdir;

fn init_test_git_repo(dir: &std::path::Path) {
    // Initialize git repo
    StdCommand::new("git")
        .arg("init")
        .current_dir(dir)
        .output()
        .expect("Failed to init git repo");

    // Configure git
    StdCommand::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(dir)
        .output()
        .expect("Failed to set git email");

    StdCommand::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(dir)
        .output()
        .expect("Failed to set git name");

    // Create initial commit to establish main branch
    fs::write(dir.join(".gitignore"), "").expect("Failed to create .gitignore");
    StdCommand::new("git")
        .args(["add", ".gitignore"])
        .current_dir(dir)
        .output()
        .expect("Failed to add .gitignore");

    StdCommand::new("git")
        .args(["commit", "-m", "Initial commit"])
        .current_dir(dir)
        .output()
        .expect("Failed to create initial commit");
}

#[test]
fn test_auth_command_help() {
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.arg("auth")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Authenticate with Claude using OAuth",
        ));
}

#[test]
fn test_auth_status_not_authenticated() {
    let temp_dir = tempdir().unwrap();
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", temp_dir.path())
        .arg("auth")
        .arg("status")
        .assert()
        .success()
        .stdout(predicate::str::contains("Not authenticated"));
}

#[test]
fn test_auth_logout_when_not_logged_in() {
    let temp_dir = tempdir().unwrap();
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", temp_dir.path())
        .arg("auth")
        .arg("logout")
        .assert()
        .success()
        .stdout(predicate::str::contains("Successfully logged out"));
}

#[test]
fn test_config_commands_comprehensive() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    // Test setting various provider configurations
    let test_cases = vec![
        ("RCO_AI_PROVIDER", "anthropic"),
        ("RCO_AI_PROVIDER", "openai"),
        ("RCO_AI_PROVIDER", "openrouter"),
        ("RCO_AI_PROVIDER", "groq"),
        ("RCO_AI_PROVIDER", "deepseek"),
        ("RCO_AI_PROVIDER", "mistral"),
        ("RCO_AI_PROVIDER", "amazon-bedrock"),
        ("RCO_AI_PROVIDER", "azure"),
        ("RCO_AI_PROVIDER", "together"),
        ("RCO_AI_PROVIDER", "deepinfra"),
        ("RCO_AI_PROVIDER", "huggingface"),
        ("RCO_AI_PROVIDER", "github-models"),
        ("RCO_AI_PROVIDER", "github-copilot"),
        ("RCO_AI_PROVIDER", "gemini"),
        ("RCO_AI_PROVIDER", "ollama"),
        ("RCO_AI_PROVIDER", "fireworks"),
        ("RCO_AI_PROVIDER", "moonshot"),
        ("RCO_AI_PROVIDER", "dashscope"),
    ];

    for (key, value) in test_cases {
        // Set config
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("{}={}", key, value))
            .assert()
            .success()
            .stdout(predicate::str::contains(format!(
                "{} set to: {}",
                key, value
            )));

        // Get config
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("get")
            .arg(key)
            .assert()
            .success()
            .stdout(predicate::str::contains(format!("{}: {}", key, value)));
    }
}

#[test]
fn test_config_model_settings() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    let model_tests = vec![
        ("gpt-4o", "openai"),
        ("claude-3-5-haiku-20241022", "anthropic"),
        ("llama-3.1-70b-versatile", "groq"),
        ("deepseek-chat", "deepseek"),
        ("mistral-large-latest", "mistral"),
        ("gemini-1.5-pro", "gemini"),
        ("meta-llama/Llama-3.2-3B-Instruct", "together"),
        ("kimi-k2", "moonshot"),
        ("qwen3-coder-32b-instruct", "dashscope"),
    ];

    for (model, provider) in model_tests {
        // Set provider first
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("RCO_AI_PROVIDER={}", provider))
            .assert()
            .success();

        // Set model
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("RCO_MODEL={}", model))
            .assert()
            .success()
            .stdout(predicate::str::contains(format!(
                "RCO_MODEL set to: {}",
                model
            )));

        // Verify model was set
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("get")
            .arg("RCO_MODEL")
            .assert()
            .success()
            .stdout(predicate::str::contains(format!("RCO_MODEL: {}", model)));
    }
}

#[test]
fn test_config_boolean_values() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    let boolean_tests = vec![
        ("RCO_EMOJI", "true"),
        ("RCO_EMOJI", "false"),
        ("RCO_GITPUSH", "true"),
        ("RCO_GITPUSH", "false"),
        ("RCO_DESCRIPTION_CAPITALIZE", "true"),
        ("RCO_DESCRIPTION_CAPITALIZE", "false"),
    ];

    for (key, value) in boolean_tests {
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("{}={}", key, value))
            .assert()
            .success()
            .stdout(predicate::str::contains(format!(
                "{} set to: {}",
                key, value
            )));
    }
}

#[test]
fn test_config_numeric_values() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    let numeric_tests = vec![
        ("RCO_TOKENS_MAX_INPUT", "8192"),
        ("RCO_TOKENS_MAX_OUTPUT", "1000"),
        ("RCO_DESCRIPTION_MAX_LENGTH", "72"),
    ];

    for (key, value) in numeric_tests {
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("{}={}", key, value))
            .assert()
            .success()
            .stdout(predicate::str::contains(format!(
                "{} set to: {}",
                key, value
            )));
    }
}

#[test]
fn test_config_api_urls() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    let url_tests = vec![
        ("https://api.openai.com/v1", "openai"),
        ("https://api.anthropic.com", "anthropic"),
        ("https://openrouter.ai/api/v1", "openrouter"),
        ("https://api.groq.com/openai/v1", "groq"),
        ("https://api.deepseek.com", "deepseek"),
        ("https://api.mistral.ai/v1", "mistral"),
        ("https://api.together.xyz/v1", "together"),
        ("https://api.deepinfra.com/v1/openai", "deepinfra"),
        ("https://api-inference.huggingface.co/v1", "huggingface"),
        ("https://models.inference.ai.azure.com", "github-models"),
        ("https://generativelanguage.googleapis.com/v1beta", "gemini"),
        ("http://localhost:11434", "ollama"),
        ("https://api.fireworks.ai/inference/v1", "fireworks"),
        ("https://api.moonshot.cn/v1", "moonshot"),
        (
            "https://dashscope.aliyuncs.com/compatible-mode/v1",
            "dashscope",
        ),
    ];

    for (url, provider) in url_tests {
        // Set provider first
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("RCO_AI_PROVIDER={}", provider))
            .assert()
            .success();

        // Set API URL
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("RCO_API_URL={}", url))
            .assert()
            .success()
            .stdout(predicate::str::contains(format!(
                "RCO_API_URL set to: {}",
                url
            )));
    }
}

#[test]
fn test_config_invalid_values() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    let invalid_tests = vec![
        ("RCO_EMOJI", "not_a_boolean"),
        ("RCO_TOKENS_MAX_INPUT", "not_a_number"),
        ("RCO_TOKENS_MAX_OUTPUT", "negative_number"),
        ("INVALID_KEY", "any_value"),
    ];

    for (key, value) in invalid_tests {
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("{}={}", key, value))
            .assert()
            .success()
            .stderr(predicate::str::contains(format!("Failed to set {}", key)));
    }
}

#[test]
fn test_config_status() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    // Test status with no configuration
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", home)
        .arg("config")
        .arg("status")
        .assert()
        .success()
        .stdout(predicate::str::contains("Secure Storage Status"))
        .stdout(predicate::str::contains("No API key configured"));

    // Set some configuration and test status
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", home)
        .arg("config")
        .arg("set")
        .arg("RCO_AI_PROVIDER=openai")
        .assert()
        .success();

    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", home)
        .arg("config")
        .arg("status")
        .assert()
        .success()
        .stdout(predicate::str::contains("AI Provider: openai"));
}

#[test]
fn test_config_reset() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    // Set some configuration
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", home)
        .arg("config")
        .arg("set")
        .arg("RCO_EMOJI=true")
        .arg("RCO_GITPUSH=true")
        .assert()
        .success();

    // Reset specific key
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", home)
        .arg("config")
        .arg("reset")
        .arg("RCO_EMOJI")
        .assert()
        .success()
        .stdout(predicate::str::contains("Reset keys: RCO_EMOJI"));

    // Verify reset
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", home)
        .arg("config")
        .arg("get")
        .arg("RCO_EMOJI")
        .assert()
        .success()
        .stdout(predicate::str::contains("RCO_EMOJI: false"));

    // Reset all
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.env("HOME", home)
        .arg("config")
        .arg("reset")
        .arg("--all")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "All configuration reset to defaults",
        ));
}

#[test]
fn test_main_command_options() {
    let temp_dir = tempdir().unwrap();
    init_test_git_repo(temp_dir.path());

    // Test --help
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.current_dir(temp_dir.path())
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Rusty Commit"))
        .stdout(predicate::str::contains("--fgm"))
        .stdout(predicate::str::contains("--context"))
        .stdout(predicate::str::contains("--yes"));

    // Test --version (don't pin exact number to avoid churn)
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.current_dir(temp_dir.path())
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("rco "));
}

#[test]
fn test_hook_commands() {
    let temp_dir = tempdir().unwrap();
    init_test_git_repo(temp_dir.path());

    // Test hook help
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.current_dir(temp_dir.path())
        .arg("hook")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Setup git hooks"));

    // Note: We can't easily test actual hook installation without
    // risking modifying the test system's git hooks
}

#[test]
fn test_commitlint_commands() {
    let temp_dir = tempdir().unwrap();
    init_test_git_repo(temp_dir.path());

    // Test commitlint help
    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.current_dir(temp_dir.path())
        .arg("commitlint")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Generate commitlint configuration",
        ));
}

#[test]
fn test_error_handling_no_git_repo() {
    let temp_dir = tempdir().unwrap();
    // Don't initialize git repo

    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.current_dir(temp_dir.path())
        .env("HOME", temp_dir.path())
        .assert()
        .failure(); // Should fail when not in git repo
}

#[test]
fn test_error_handling_no_api_key() {
    let temp_dir = tempdir().unwrap();
    init_test_git_repo(temp_dir.path());

    // Create a staged file for commit
    fs::write(temp_dir.path().join("test.txt"), "test content").unwrap();
    StdCommand::new("git")
        .args(["add", "test.txt"])
        .current_dir(temp_dir.path())
        .output()
        .unwrap();

    let mut cmd = Command::cargo_bin("rco").unwrap();
    cmd.current_dir(temp_dir.path())
        .env("HOME", temp_dir.path())
        .assert()
        .failure() // Should fail without API key
        .stderr(predicate::str::contains("API key").or(predicate::str::contains("authentication")));
}

#[test]
fn test_multiple_provider_configurations() {
    let temp_dir = tempdir().unwrap();
    let home = temp_dir.path();

    // Test switching between providers
    let providers = vec![
        ("openai", "gpt-4o-mini"),
        ("anthropic", "claude-3-5-haiku-20241022"),
        ("groq", "llama-3.1-70b-versatile"),
        ("openrouter", "openai/gpt-4o-mini"),
    ];

    for (provider, model) in providers {
        // Set provider
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("RCO_AI_PROVIDER={}", provider))
            .assert()
            .success();

        // Set model
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("set")
            .arg(format!("RCO_MODEL={}", model))
            .assert()
            .success();

        // Verify both are set correctly
        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("get")
            .arg("RCO_AI_PROVIDER")
            .assert()
            .success()
            .stdout(predicate::str::contains(format!(
                "RCO_AI_PROVIDER: {}",
                provider
            )));

        let mut cmd = Command::cargo_bin("rco").unwrap();
        cmd.env("HOME", home)
            .arg("config")
            .arg("get")
            .arg("RCO_MODEL")
            .assert()
            .success()
            .stdout(predicate::str::contains(format!("RCO_MODEL: {}", model)));
    }
}