context-creator 1.5.0

High-performance CLI tool to convert codebases to Markdown for LLM context
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#![cfg(test)]

use clap::Parser;
use context_creator::cli::{Config, LlmTool};
use std::path::PathBuf;

#[test]
fn test_llm_tool_default() {
    let config = Config::parse_from(["context-creator", "."]);
    assert_eq!(config.llm_tool, LlmTool::Gemini);
}

#[test]
fn test_llm_tool_gemini() {
    let config = Config::parse_from(["context-creator", "--tool", "gemini", "."]);
    assert_eq!(config.llm_tool, LlmTool::Gemini);
}

#[test]
fn test_llm_tool_codex() {
    let config = Config::parse_from(["context-creator", "--tool", "codex", "."]);
    assert_eq!(config.llm_tool, LlmTool::Codex);
}

#[test]
fn test_llm_tool_long_flag() {
    let config = Config::parse_from(["context-creator", "--tool", "codex", "."]);
    assert_eq!(config.llm_tool, LlmTool::Codex);
}

#[test]
fn test_llm_tool_command_names() {
    assert_eq!(LlmTool::Gemini.command(), "gemini");
    assert_eq!(LlmTool::Codex.command(), "codex");
}

#[test]
fn test_llm_tool_install_instructions() {
    assert!(LlmTool::Gemini
        .install_instructions()
        .contains("pip install"));
    assert!(LlmTool::Codex.install_instructions().contains("github.com"));
}

#[test]
fn test_llm_tool_claude() {
    let config = Config::parse_from(["context-creator", "--tool", "claude", "."]);
    assert_eq!(config.llm_tool, LlmTool::Claude);
}

#[test]
fn test_llm_tool_ollama() {
    let config = Config::parse_from(["context-creator", "--tool", "ollama", "."]);
    assert_eq!(config.llm_tool, LlmTool::Ollama);
}

#[test]
fn test_llm_tool_claude_command_name() {
    assert_eq!(LlmTool::Claude.command(), "claude");
}

#[test]
fn test_llm_tool_ollama_command_name() {
    assert_eq!(LlmTool::Ollama.command(), "ollama");
}

#[test]
fn test_llm_tool_claude_install_instructions() {
    assert!(LlmTool::Claude
        .install_instructions()
        .contains("npm install -g @anthropic-ai/claude-code"));
}

#[test]
fn test_llm_tool_ollama_install_instructions() {
    assert!(LlmTool::Ollama
        .install_instructions()
        .contains("brew install ollama"));
}

#[test]
fn test_ollama_model_argument() {
    let config = Config::parse_from([
        "context-creator",
        "--tool",
        "ollama",
        "--ollama-model",
        "llama3",
        ".",
    ]);
    assert_eq!(config.llm_tool, LlmTool::Ollama);
    assert_eq!(config.ollama_model, Some("llama3".to_string()));
}

#[test]
fn test_ollama_without_model_validation_error() {
    let config = Config::parse_from([
        "context-creator",
        "--tool",
        "ollama",
        "--prompt",
        "Test prompt",
        ".",
    ]);
    assert_eq!(config.llm_tool, LlmTool::Ollama);
    assert_eq!(config.ollama_model, None);

    // Should fail validation when using Ollama without model
    let result = config.validate();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("--ollama-model is required when using --tool ollama"));
}

#[test]
fn test_ollama_model_with_other_tools_ignored() {
    let config = Config::parse_from([
        "context-creator",
        "--tool",
        "gemini",
        "--ollama-model",
        "llama3", // Should be ignored for non-ollama tools
        ".",
    ]);
    assert_eq!(config.llm_tool, LlmTool::Gemini);
    assert_eq!(config.ollama_model, Some("llama3".to_string()));

    // Should pass validation - ollama_model is ignored for other tools
    assert!(config.validate().is_ok());
}

#[test]
fn test_repo_argument() {
    let config = Config::parse_from([
        "context-creator",
        "--remote",
        "https://github.com/owner/repo",
    ]);
    assert_eq!(
        config.remote,
        Some("https://github.com/owner/repo".to_string())
    );
}

#[test]
fn test_repo_and_directory_now_disallowed() {
    // This combination is now disallowed to prevent silent overwriting bug
    let config = Config::parse_from([
        "context-creator",
        "--remote",
        "https://github.com/owner/repo",
        ".",
    ]);

    assert_eq!(
        config.remote,
        Some("https://github.com/owner/repo".to_string())
    );
    assert_eq!(config.paths, Some(vec![PathBuf::from(".")]));

    // This should FAIL validation to prevent confusion where paths get silently ignored
    let result = config.validate();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Cannot specify both --remote and local paths"));
}

#[test]
fn test_valid_repo_url_accepted() {
    let config = Config::parse_from([
        "context-creator",
        "--remote",
        "https://github.com/matiasvillaverde/context-creator",
    ]);
    assert_eq!(
        config.remote,
        Some("https://github.com/matiasvillaverde/context-creator".to_string())
    );
}

#[test]
fn test_prompt_flag_with_spaces() {
    let config = Config::parse_from([
        "context-creator",
        "--prompt",
        "How does authentication work in this codebase?",
    ]);
    assert_eq!(
        config.get_prompt(),
        Some("How does authentication work in this codebase?".to_string())
    );
    assert_eq!(config.get_directories(), vec![PathBuf::from(".")]);
}

#[test]
fn test_prompt_short_flag() {
    let config = Config::parse_from(["context-creator", "-p", "Analyze security"]);
    assert_eq!(config.get_prompt(), Some("Analyze security".to_string()));
}

#[test]
fn test_positional_directories() {
    let config = Config::parse_from(["context-creator", "src/auth", "src/models", "tests/auth"]);
    assert_eq!(
        config.get_directories(),
        vec![
            PathBuf::from("src/auth"),
            PathBuf::from("src/models"),
            PathBuf::from("tests/auth")
        ]
    );
}

#[test]
fn test_multiple_directories() {
    let config = Config::parse_from(["context-creator", "src/core", "src/utils", "tests"]);
    assert_eq!(
        config.get_directories(),
        vec![
            PathBuf::from("src/core"),
            PathBuf::from("src/utils"),
            PathBuf::from("tests")
        ]
    );
}

#[test]
fn test_prompt_and_paths_now_allowed() {
    // This combination is now allowed per issue #34
    let config = Config::parse_from(["context-creator", "--prompt", "test", "src"]);

    assert_eq!(config.get_prompt(), Some("test".to_string()));
    assert_eq!(config.paths, Some(vec![PathBuf::from("src")]));

    // This should pass validation with the new flexible combinations
    assert!(config.validate().is_ok());
}

#[test]
fn test_stdin_flag() {
    // Test with explicit stdin flag
    let config = Config::parse_from(["context-creator", "--stdin"]);
    assert!(config.read_stdin);
    assert!(config.should_read_stdin());

    // Test without stdin flag
    let config = Config::parse_from(["context-creator", "src"]);
    assert!(!config.read_stdin);
}

#[test]
fn test_copy_flag() {
    let config = Config::parse_from(["context-creator", "src", "--copy"]);
    assert!(config.copy);
}

#[test]
fn test_copy_short_flag() {
    let config = Config::parse_from(["context-creator", "src", "-C"]);
    assert!(config.copy);
}

#[test]
fn test_copy_default_false() {
    let config = Config::parse_from(["context-creator", "src"]);
    assert!(!config.copy);
}

#[test]
fn test_copy_with_output_conflict() {
    use tempfile::TempDir;
    let temp_dir = TempDir::new().unwrap();
    let config = Config::parse_from([
        "context-creator",
        temp_dir.path().to_str().unwrap(),
        "--copy",
        "-o",
        "out.md",
    ]);
    let result = config.validate();
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Cannot specify both --copy and --output"));
}

#[test]
fn test_enhanced_context_flag() {
    let config = Config::parse_from(["context-creator", "--enhanced-context", "."]);
    assert!(config.enhanced_context);
}

#[test]
fn test_enhanced_context_default_false() {
    let config = Config::parse_from(["context-creator", "."]);
    assert!(!config.enhanced_context);
}

// Tests for --include flag functionality
#[test]
fn test_include_single_path() {
    let config = Config::parse_from(["context-creator", "--include", "src/"]);
    // When using include patterns, base directory is current dir
    assert_eq!(config.get_directories(), vec![PathBuf::from(".")]);
    // The patterns themselves are accessed via get_include_patterns
    assert_eq!(config.get_include_patterns(), vec!["src/"]);
}

#[test]
fn test_include_multiple_paths() {
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "src/",
        "--include",
        "tests/",
    ]);
    // When using include patterns, base directory is current dir
    assert_eq!(config.get_directories(), vec![PathBuf::from(".")]);
    // The patterns themselves are accessed via get_include_patterns
    assert_eq!(config.get_include_patterns(), vec!["src/", "tests/"]);
}

#[test]
fn test_include_three_paths() {
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "src/",
        "--include",
        "tests/",
        "--include",
        "docs/",
    ]);
    // When using include patterns, base directory is current dir
    assert_eq!(config.get_directories(), vec![PathBuf::from(".")]);
    // The patterns themselves are accessed via get_include_patterns
    assert_eq!(
        config.get_include_patterns(),
        vec!["src/", "tests/", "docs/"]
    );
}

#[test]
fn test_positional_and_include_now_allowed() {
    // This combination is now allowed per issue #34
    let config = Config::parse_from(["context-creator", "src/", "--include", "tests/"]);

    assert_eq!(config.paths, Some(vec![PathBuf::from("src/")]));
    assert_eq!(config.get_include_patterns(), vec!["tests/"]);

    // This should pass validation with the new flexible combinations
    assert!(config.validate().is_ok());
}

#[test]
fn test_include_with_prompt_success() {
    // This should now work - prompt and include can be used together
    let result =
        Config::try_parse_from(["context-creator", "--prompt", "test", "--include", "src/"]);
    assert!(result.is_ok());
    let config = result.unwrap();
    assert_eq!(config.get_prompt(), Some("test".to_string()));
    assert_eq!(config.get_include_patterns(), vec!["src/"]);
}

#[test]
fn test_include_with_repo_now_allowed() {
    // This combination is now allowed per issue #34
    let config = Config::parse_from([
        "context-creator",
        "--remote",
        "https://github.com/owner/repo",
        "--include",
        "src/",
    ]);

    assert_eq!(
        config.remote,
        Some("https://github.com/owner/repo".to_string())
    );
    assert_eq!(config.get_include_patterns(), vec!["src/"]);

    // This should pass validation with the new flexible combinations
    assert!(config.validate().is_ok());
}

#[test]
fn test_include_with_stdin_now_allowed() {
    // This combination is now allowed per issue #34
    let config = Config::parse_from(["context-creator", "--stdin", "--include", "src/"]);

    assert!(config.read_stdin);
    assert_eq!(config.get_include_patterns(), vec!["src/"]);

    // This should pass validation with the new flexible combinations
    assert!(config.validate().is_ok());
}

#[test]
fn test_no_arguments_defaults_to_current_directory() {
    // This test ensures that when no paths or include flags are provided,
    // we default to current directory "."
    let config = Config::parse_from(["context-creator", "--prompt", "test"]);
    // Note: This is testing that the default behavior is preserved
    assert_eq!(config.get_directories(), vec![PathBuf::from(".")]);
}

#[test]
fn test_positional_with_file_path_validation_success() {
    use std::fs;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test_file.txt");
    fs::write(&file_path, "test content").unwrap();

    let config = Config::parse_from([
        "context-creator",
        file_path.to_str().unwrap(),
        "--output-file",
        "test.md",
    ]);

    // Should pass validation because files are now accepted
    let result = config.validate();
    assert!(result.is_ok());
}

#[test]
fn test_include_with_file_path_validation_success() {
    use std::fs;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let dir_path = temp_dir.path().join("test_dir");
    fs::create_dir(&dir_path).unwrap();

    let config = Config::parse_from([
        "context-creator",
        "--include",
        dir_path.to_str().unwrap(),
        "--output-file",
        "test.md",
    ]);

    // Should succeed validation because include path points to a directory
    let result = config.validate();
    assert!(result.is_ok());
}

#[test]
fn test_include_pattern_validation_valid_patterns() {
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "*.py",
        "--include",
        "**/*.rs",
        "--include",
        "src/**/*.js",
        "--output-file",
        "test.md",
    ]);

    // Should succeed validation for valid glob patterns
    let result = config.validate();
    assert!(result.is_ok());
}

#[test]
fn test_include_empty_pattern() {
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "",
        "--include",
        "*.py",
        "--output-file",
        "test.md",
    ]);

    // Should succeed validation - empty patterns are skipped
    let result = config.validate();
    assert!(result.is_ok());
}

#[test]
fn test_include_whitespace_only_pattern() {
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "   ",
        "--include",
        "*.py",
        "--output-file",
        "test.md",
    ]);

    // Should succeed validation - whitespace-only patterns are skipped
    let result = config.validate();
    assert!(result.is_ok());
}

#[test]
fn test_include_glob_pattern_simple_wildcard() {
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "*.py",
        "--output-file",
        "test.md",
    ]);

    // Should succeed validation for simple wildcard pattern
    let result = config.validate();
    assert!(result.is_ok());
}

#[test]
fn test_include_glob_pattern_recursive_directory() {
    // Test recursive directory matching
    let config = Config::parse_from(["context-creator", "--include", "**/*.rs"]);
    assert_eq!(config.include, Some(vec!["**/*.rs".to_string()]));
}

#[test]
fn test_include_glob_pattern_brace_expansion() {
    // Test brace expansion
    let config = Config::parse_from(["context-creator", "--include", "src/**/*.{py,js}"]);
    assert_eq!(config.include, Some(vec!["src/**/*.{py,js}".to_string()]));
}

#[test]
fn test_include_glob_pattern_character_sets() {
    // Test character sets and ranges
    let config = Config::parse_from(["context-creator", "--include", "**/test[0-9].py"]);
    assert_eq!(config.include, Some(vec!["**/test[0-9].py".to_string()]));
}

#[test]
fn test_include_multiple_glob_patterns() {
    // Test multiple glob patterns
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "**/*repository*.py",
        "--include",
        "**/db/**",
    ]);
    assert_eq!(
        config.include,
        Some(vec![
            "**/*repository*.py".to_string(),
            "**/db/**".to_string()
        ])
    );
}

#[test]
fn test_include_complex_pattern_combinations() {
    // Test complex pattern combinations
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "**/*{repository,service,model}*.py",
        "--include",
        "**/db/**",
    ]);
    assert_eq!(
        config.include,
        Some(vec![
            "**/*{repository,service,model}*.py".to_string(),
            "**/db/**".to_string()
        ])
    );
}

#[test]
fn test_include_pattern_validation_invalid_pattern() {
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "src/[", // Invalid unclosed bracket
        "--output-file",
        "test.md",
    ]);

    // CLI validation now passes - pattern validation happens in walker.rs for better security
    let result = config.validate();
    assert!(
        result.is_ok(),
        "CLI validation should pass, walker handles pattern validation"
    );
}

// === SECURITY INTEGRATION TESTS ===

#[test]
fn test_cli_security_directory_traversal_rejected() {
    // Test that directory traversal patterns are rejected during file processing
    use std::fs;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let current_dir = std::env::current_dir().unwrap();
    std::env::set_current_dir(temp_dir.path()).unwrap();

    // Create a test file
    fs::write("test.py", "print('hello')").unwrap();

    let config = Config::parse_from([
        "context-creator",
        "--include",
        "../../../etc/passwd", // Directory traversal attempt
        "--output-file",
        "output.md",
    ]);

    // CLI validation should pass (we moved validation to walker)
    assert!(config.validate().is_ok());

    // But actual execution should fail during file processing
    let result = std::panic::catch_unwind(|| {
        // This would normally trigger the walker code path
        // Since we can't easily test the full CLI execution here,
        // we verify the config is parsed correctly
        let patterns = config.get_include_patterns();
        assert_eq!(patterns, vec!["../../../etc/passwd"]);
    });

    std::env::set_current_dir(current_dir).unwrap();
    assert!(
        result.is_ok(),
        "Config parsing should succeed, validation happens later"
    );
}

#[test]
fn test_cli_security_null_byte_patterns() {
    // Test that patterns with null bytes are handled gracefully
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "test\0.py", // Null byte in pattern
        "--output-file",
        "output.md",
    ]);

    // CLI validation should pass - security validation happens in walker
    let result = config.validate();
    assert!(result.is_ok(), "CLI should parse null byte patterns");

    let patterns = config.get_include_patterns();
    assert_eq!(patterns, vec!["test\0.py"]);
}

#[test]
fn test_cli_security_long_pattern_handling() {
    // Test very long patterns to check for buffer overflow vulnerabilities
    let long_pattern = "a".repeat(2000); // Longer than our 1000 char limit

    let config = Config::parse_from([
        "context-creator",
        "--include",
        &long_pattern,
        "--output-file",
        "output.md",
    ]);

    // CLI should handle long patterns gracefully
    let result = config.validate();
    assert!(result.is_ok(), "CLI should handle long patterns");

    let patterns = config.get_include_patterns();
    assert_eq!(patterns, vec![long_pattern]);
}

#[test]
fn test_cli_security_multiple_suspicious_patterns() {
    // Test multiple potentially dangerous patterns
    let config = Config::parse_from([
        "context-creator",
        "--include",
        "../../../etc/passwd",
        "--include",
        "/etc/shadow",
        "--include",
        "..\\..\\Windows\\System32\\*",
        "--include",
        "test\0file.py",
        "--output-file",
        "output.md",
    ]);

    // CLI validation should pass
    assert!(config.validate().is_ok());

    let patterns = config.get_include_patterns();
    assert_eq!(
        patterns,
        vec![
            "../../../etc/passwd",
            "/etc/shadow",
            "..\\..\\Windows\\System32\\*",
            "test\0file.py"
        ]
    );
}

#[test]
fn test_cli_security_control_character_patterns() {
    // Test patterns with various control characters
    let patterns_with_controls = vec![
        "file\x01.py",   // SOH
        "test\x08.txt",  // Backspace
        "dir\x0c/*.rs",  // Form feed
        "file\nname.py", // Newline
        "tab\tfile.rs",  // Tab
    ];

    for pattern in patterns_with_controls {
        let config = Config::parse_from([
            "context-creator",
            "--include",
            pattern,
            "--output-file",
            "output.md",
        ]);

        // CLI should parse these patterns
        assert!(
            config.validate().is_ok(),
            "CLI should parse pattern with control chars: {pattern:?}"
        );

        let parsed_patterns = config.get_include_patterns();
        assert_eq!(parsed_patterns, vec![pattern]);
    }
}

// === Tests for prepare_command() method ===

#[test]
fn test_prepare_command_gemini() {
    let config = Config::parse_from(["context-creator", "--tool", "gemini", "."]);
    let (cmd, combined_input) = LlmTool::Gemini.prepare_command(&config).unwrap();

    // Gemini should use stdin for combined prompt+context
    assert!(combined_input);

    // Verify command setup
    assert_eq!(cmd.get_program(), "gemini");

    // Check that no args are passed
    let args: Vec<_> = cmd.get_args().collect();
    assert_eq!(args.len(), 0);
}

#[test]
fn test_prepare_command_codex() {
    let config = Config::parse_from(["context-creator", "--tool", "codex", "."]);
    let (cmd, combined_input) = LlmTool::Codex.prepare_command(&config).unwrap();

    // Codex should use stdin for combined prompt+context
    assert!(combined_input);

    // Verify command setup
    assert_eq!(cmd.get_program(), "codex");

    // Check that no args are passed
    let args: Vec<_> = cmd.get_args().collect();
    assert_eq!(args.len(), 0);
}

#[test]
fn test_prepare_command_claude() {
    let mut config = Config::parse_from(["context-creator", "--tool", "claude", "."]);
    config.prompt = Some("Test prompt".to_string());

    let (cmd, combined_input) = LlmTool::Claude.prepare_command(&config).unwrap();

    // Claude should use command args for prompt, stdin for context only
    assert!(!combined_input);

    // Verify command setup
    assert_eq!(cmd.get_program(), "claude");

    // Check that -p flag and prompt are passed
    let args: Vec<_> = cmd.get_args().collect();
    assert_eq!(args.len(), 2);
    assert_eq!(args[0], "-p");
    assert_eq!(args[1], "Test prompt");
}

#[test]
fn test_prepare_command_ollama_with_model() {
    let config = Config::parse_from([
        "context-creator",
        "--tool",
        "ollama",
        "--ollama-model",
        "llama3",
        ".",
    ]);

    let (cmd, combined_input) = LlmTool::Ollama.prepare_command(&config).unwrap();

    // Ollama should use stdin for combined prompt+context
    assert!(combined_input);

    // Verify command setup
    assert_eq!(cmd.get_program(), "ollama");

    // Check that "run" and model are passed
    let args: Vec<_> = cmd.get_args().collect();
    assert_eq!(args.len(), 2);
    assert_eq!(args[0], "run");
    assert_eq!(args[1], "llama3");
}

#[test]
fn test_prepare_command_ollama_without_model() {
    let config = Config::parse_from(["context-creator", "--tool", "ollama", "."]);

    let result = LlmTool::Ollama.prepare_command(&config);

    // Should return error when model is not specified
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("--ollama-model is required when using --tool ollama"));
}