cc-switch 0.1.8

A CLI tool for managing multiple Claude API configurations and automatically switching between them
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
#[cfg(test)]
mod tests {
    use cc_switch::cli::completion::*;

    // generate_aliases Tests
    #[test]
    fn test_generate_aliases_fish() {
        let result = generate_aliases("fish");
        assert!(result.is_ok(), "Should generate fish aliases successfully");
    }

    #[test]
    fn test_generate_aliases_zsh() {
        let result = generate_aliases("zsh");
        assert!(result.is_ok(), "Should generate zsh aliases successfully");
    }

    #[test]
    fn test_generate_aliases_bash() {
        let result = generate_aliases("bash");
        assert!(result.is_ok(), "Should generate bash aliases successfully");
    }

    #[test]
    fn test_generate_aliases_unsupported_shell() {
        let result = generate_aliases("unsupported");
        assert!(result.is_err(), "Should fail for unsupported shell");

        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Unsupported shell: unsupported"));
        assert!(error_msg.contains("Supported shells: fish, zsh, bash"));
    }

    #[test]
    fn test_generate_aliases_empty_string() {
        let result = generate_aliases("");
        assert!(result.is_err(), "Should fail for empty shell string");
    }

    #[test]
    fn test_generate_aliases_case_sensitivity() {
        let result_upper = generate_aliases("FISH");
        let result_mixed = generate_aliases("Fish");

        assert!(
            result_upper.is_err(),
            "Should be case sensitive - FISH should fail"
        );
        assert!(
            result_mixed.is_err(),
            "Should be case sensitive - Fish should fail"
        );
    }

    #[test]
    fn test_generate_aliases_special_characters() {
        let test_cases = vec!["fish!", "z$h", "bash#", "fish\n", "zsh\t"];

        for shell in test_cases {
            let result = generate_aliases(shell);
            assert!(
                result.is_err(),
                "Should fail for shell with special characters: {}",
                shell
            );
        }
    }

    // generate_completion Tests
    #[test]
    fn test_generate_completion_fish() {
        let result = generate_completion("fish");
        assert!(
            result.is_ok(),
            "Should generate fish completion successfully"
        );
    }

    #[test]
    fn test_generate_completion_zsh() {
        let result = generate_completion("zsh");
        assert!(
            result.is_ok(),
            "Should generate zsh completion successfully"
        );
    }

    #[test]
    fn test_generate_completion_bash() {
        let result = generate_completion("bash");
        assert!(
            result.is_ok(),
            "Should generate bash completion successfully"
        );
    }

    #[test]
    fn test_generate_completion_elvish() {
        let result = generate_completion("elvish");
        assert!(
            result.is_ok(),
            "Should generate elvish completion successfully"
        );
    }

    #[test]
    fn test_generate_completion_powershell() {
        let result = generate_completion("powershell");
        assert!(
            result.is_ok(),
            "Should generate powershell completion successfully"
        );
    }

    #[test]
    fn test_generate_completion_unsupported_shell() {
        let result = generate_completion("unsupported");
        assert!(result.is_err(), "Should fail for unsupported shell");

        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Unsupported shell: unsupported"));
        assert!(error_msg.contains("Supported shells: fish, zsh, bash, elvish, powershell"));
    }

    #[test]
    fn test_generate_completion_nushell_not_supported() {
        // nushell is mentioned in docs but not implemented
        let result = generate_completion("nushell");
        assert!(
            result.is_err(),
            "Should fail for nushell as it's not implemented"
        );
    }

    #[test]
    fn test_generate_completion_case_sensitivity() {
        let result_upper = generate_completion("FISH");
        let result_mixed = generate_completion("Fish");

        assert!(
            result_upper.is_err(),
            "Should be case sensitive - FISH should fail"
        );
        assert!(
            result_mixed.is_err(),
            "Should be case sensitive - Fish should fail"
        );
    }

    #[test]
    fn test_generate_completion_empty_string() {
        let result = generate_completion("");
        assert!(result.is_err(), "Should fail for empty shell string");
    }

    // list_aliases_for_completion Tests
    #[test]
    fn test_list_aliases_for_completion_empty_storage() {
        // This test relies on a clean configuration storage
        // Since we can't easily mock the file system, we test that it doesn't panic
        let result = list_aliases_for_completion();
        // Should succeed even if no configs exist (will create default storage)
        if let Err(e) = &result {
            eprintln!("Error: {:?}", e);
        }
        assert!(result.is_ok(), "Should handle empty storage gracefully");
    }

    #[test]
    fn test_list_aliases_for_completion_with_current_config() {
        // This test verifies the logic around 'current' configuration priority
        // We test the function doesn't panic and handles the case properly
        let result = list_aliases_for_completion();
        assert!(
            result.is_ok(),
            "Should handle configurations with 'current' alias"
        );
    }

    #[test]
    fn test_list_aliases_for_completion_multiple_configs() {
        // This test verifies multiple configurations are handled properly
        let result = list_aliases_for_completion();
        assert!(result.is_ok(), "Should handle multiple configurations");
    }

    // Integration Tests for Shell-specific Logic
    #[test]
    fn test_supported_shells_list() {
        let supported_alias_shells = vec!["fish", "zsh", "bash"];
        let supported_completion_shells = vec!["fish", "zsh", "bash", "elvish", "powershell"];

        // Test all supported alias shells
        for shell in supported_alias_shells {
            let result = generate_aliases(shell);
            assert!(
                result.is_ok(),
                "Shell {} should be supported for aliases",
                shell
            );
        }

        // Test all supported completion shells
        for shell in supported_completion_shells {
            let result = generate_completion(shell);
            assert!(
                result.is_ok(),
                "Shell {} should be supported for completion",
                shell
            );
        }
    }

    #[test]
    fn test_unsupported_shells_consistency() {
        let unsupported_shells = vec!["tcsh", "csh", "sh", "nushell", "ion", "xonsh"];

        for shell in unsupported_shells {
            let alias_result = generate_aliases(shell);
            let completion_result = generate_completion(shell);

            // Both should fail for unsupported shells
            assert!(
                alias_result.is_err(),
                "Shell {} should not be supported for aliases",
                shell
            );

            // nushell is a special case - mentioned in docs but not implemented
            if shell != "nushell" {
                assert!(
                    completion_result.is_err(),
                    "Shell {} should not be supported for completion",
                    shell
                );
            }
        }
    }

    #[test]
    fn test_alias_shell_subset_of_completion_shells() {
        // Alias shells should be a subset of completion shells
        let alias_shells = vec!["fish", "zsh", "bash"];

        for shell in alias_shells {
            let alias_result = generate_aliases(shell);
            let completion_result = generate_completion(shell);

            assert!(alias_result.is_ok(), "Alias shell {} should work", shell);
            assert!(
                completion_result.is_ok(),
                "Completion shell {} should work",
                shell
            );
        }
    }

    // Error Message Quality Tests
    #[test]
    fn test_alias_error_message_quality() {
        let result = generate_aliases("invalid_shell");
        assert!(result.is_err());

        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Unsupported shell"));
        assert!(error_msg.contains("invalid_shell"));
        assert!(error_msg.contains("fish"));
        assert!(error_msg.contains("zsh"));
        assert!(error_msg.contains("bash"));
    }

    #[test]
    fn test_completion_error_message_quality() {
        let result = generate_completion("invalid_shell");
        assert!(result.is_err());

        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Unsupported shell"));
        assert!(error_msg.contains("invalid_shell"));
        assert!(error_msg.contains("fish"));
        assert!(error_msg.contains("zsh"));
        assert!(error_msg.contains("bash"));
        assert!(error_msg.contains("elvish"));
        assert!(error_msg.contains("powershell"));
    }

    // Edge Cases and Robustness Tests
    #[test]
    fn test_whitespace_in_shell_names() {
        let whitespace_shells = vec![" fish", "fish ", " fish ", "fi sh", "\tfish", "fish\n"];

        for shell in whitespace_shells {
            let alias_result = generate_aliases(shell);
            let completion_result = generate_completion(shell);

            assert!(
                alias_result.is_err(),
                "Should reject shell name with whitespace: '{}'",
                shell
            );
            assert!(
                completion_result.is_err(),
                "Should reject shell name with whitespace: '{}'",
                shell
            );
        }
    }

    #[test]
    fn test_unicode_shell_names() {
        let unicode_shells = vec!["fish🐟", "zsh📚", "bash💥", "ﻪtset"];

        for shell in unicode_shells {
            let alias_result = generate_aliases(shell);
            let completion_result = generate_completion(shell);

            assert!(
                alias_result.is_err(),
                "Should reject unicode shell name: '{}'",
                shell
            );
            assert!(
                completion_result.is_err(),
                "Should reject unicode shell name: '{}'",
                shell
            );
        }
    }

    #[test]
    fn test_very_long_shell_names() {
        let long_shell = "a".repeat(1000);

        let alias_result = generate_aliases(&long_shell);
        let completion_result = generate_completion(&long_shell);

        assert!(alias_result.is_err(), "Should reject very long shell name");
        assert!(
            completion_result.is_err(),
            "Should reject very long shell name"
        );
    }

    // Function Behavior Consistency Tests
    #[test]
    fn test_function_consistency_supported_shells() {
        let common_shells = vec!["fish", "zsh", "bash"];

        for shell in common_shells {
            let alias_result = generate_aliases(shell);
            let completion_result = generate_completion(shell);

            // Both should succeed for common shells
            assert!(
                alias_result.is_ok() && completion_result.is_ok(),
                "Both alias and completion should work for shell: {}",
                shell
            );
        }
    }

    #[test]
    fn test_function_consistency_unsupported_shells() {
        let unsupported_shells = vec!["tcsh", "csh", "invalid"];

        for shell in unsupported_shells {
            let alias_result = generate_aliases(shell);

            // Alias should fail for all unsupported shells
            assert!(
                alias_result.is_err(),
                "Alias should fail for unsupported shell: {}",
                shell
            );
        }
    }

    // Stress Tests
    #[test]
    fn test_multiple_calls_same_shell() {
        // Test that multiple calls to the same function work
        for _ in 0..10 {
            let result = generate_aliases("fish");
            assert!(result.is_ok(), "Multiple calls should work");
        }
    }

    #[test]
    fn test_alternating_shell_calls() {
        let shells = vec!["fish", "zsh", "bash"];

        for i in 0..30 {
            let shell = shells[i % shells.len()];
            let alias_result = generate_aliases(shell);
            let completion_result = generate_completion(shell);

            assert!(
                alias_result.is_ok(),
                "Alternating call {} should work for aliases",
                i
            );
            assert!(
                completion_result.is_ok(),
                "Alternating call {} should work for completion",
                i
            );
        }
    }

    // Configuration Storage Integration Tests
    #[test]
    fn test_list_aliases_error_handling() {
        // Test that the function handles storage errors gracefully
        // This is a basic test since we can't easily simulate storage failures
        let result = list_aliases_for_completion();

        // The function should either succeed or fail gracefully
        match result {
            Ok(_) => {
                // Success case - normal operation
            }
            Err(e) => {
                // Error case - should be a meaningful error
                let error_msg = e.to_string();
                assert!(!error_msg.is_empty(), "Error message should not be empty");
            }
        }
    }

    // Output Format Tests (indirect testing)
    #[test]
    fn test_shell_output_generation_does_not_panic() {
        let shells = vec!["fish", "zsh", "bash", "elvish", "powershell"];

        for shell in shells {
            // Test that generation doesn't panic
            let alias_result = if shell == "fish" || shell == "zsh" || shell == "bash" {
                generate_aliases(shell)
            } else {
                Ok(()) // Skip alias test for shells that don't support it
            };
            let completion_result = generate_completion(shell);

            if shell == "fish" || shell == "zsh" || shell == "bash" {
                assert!(
                    alias_result.is_ok(),
                    "Alias generation should not panic for {}",
                    shell
                );
            }
            assert!(
                completion_result.is_ok(),
                "Completion generation should not panic for {}",
                shell
            );
        }
    }

    // Test command factory integration
    #[test]
    fn test_command_factory_usage() {
        use cc_switch::cli::Cli;
        use clap::CommandFactory;

        // Test that we can create the command factory (used in generate_completion)
        let app = Cli::command();

        // Verify basic command structure
        assert_eq!(app.get_name(), "cc-switch");

        // Test that the command has expected subcommands
        let subcommand_names: Vec<&str> = app.get_subcommands().map(|cmd| cmd.get_name()).collect();

        assert!(
            subcommand_names.contains(&"add"),
            "Should have 'add' subcommand"
        );
        assert!(
            subcommand_names.contains(&"list"),
            "Should have 'list' subcommand"
        );
        assert!(
            subcommand_names.contains(&"completion"),
            "Should have 'completion' subcommand"
        );
    }

    // Performance Tests (basic)
    #[test]
    fn test_multiple_operations_performance() {
        // Basic test to ensure functions complete in reasonable time
        let start = std::time::Instant::now();

        // Perform multiple operations
        for _ in 0..100 {
            let _ = generate_aliases("fish");
            let _ = generate_completion("zsh");
        }

        let duration = start.elapsed();

        // Should complete within a reasonable time (10 seconds is very generous)
        assert!(
            duration.as_secs() < 10,
            "Operations should complete within 10 seconds, took {:?}",
            duration
        );
    }
}