llman 0.0.78

A tool for managing LLM application rules(prompts) ...
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
use crate::common::*;
use llman::tool::command::CleanUselessCommentsArgs;
use llman::tool::config::ToolConfig;
use llman::tool::processor::CommentProcessor;

/// Comprehensive tests for configuration system validation
/// Tests all aspects of YAML configuration loading, validation, and error handling

#[test]
fn test_config_default_values() {
    let _env = TestEnvironment::new();

    // Test default configuration loading
    let config = ToolConfig::default();

    assert_eq!(config.version, "0.1");
    assert!(config.tools.clean_useless_comments.is_some());

    let clean_config = config.tools.clean_useless_comments.unwrap();
    assert!(!clean_config.scope.include.is_empty());
}

#[test]
fn test_config_yaml_parsing() {
    let env = TestEnvironment::new();

    let config_content = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
        - "**/*.js"
      exclude:
        - "**/node_modules/**"
        - "**/target/**"
    lang-rules:
      python:
        single-line-comments: true
        multi-line-comments: false
        min-comment-length: 10
        preserve-patterns:
          - "^\\s*#\\s*(TODO|FIXME):"
      javascript:
        single-line-comments: true
        multi-line-comments: true
        doc-comments: false
        min-comment-length: 15
        preserve-patterns:
          - "^\\s*//\\s*(TODO|FIXME):"
          - "^\\s*/\\*\\*.*?\\*/"
"#;

    env.create_config(config_content);

    let config = ToolConfig::load(env.path().join(".llman").join("config.yaml")).unwrap();

    assert_eq!(config.version, "0.1");
    assert!(config.tools.clean_useless_comments.is_some());

    let clean_config = config.tools.clean_useless_comments.unwrap();
    assert_eq!(clean_config.scope.include.len(), 2);
    assert_eq!(clean_config.scope.exclude.len(), 2);
    assert!(clean_config.scope.include.contains(&"**/*.py".to_string()));
    assert!(clean_config.scope.include.contains(&"**/*.js".to_string()));

    assert!(clean_config.lang_rules.python.is_some());
    assert!(clean_config.lang_rules.javascript.is_some());

    let python_rules = clean_config.lang_rules.python.unwrap();
    assert_eq!(python_rules.single_line_comments, Some(true));
    assert_eq!(python_rules.multi_line_comments, Some(false));
    assert_eq!(python_rules.min_comment_length, Some(10));
    assert_eq!(python_rules.preserve_patterns.unwrap().len(), 1);

    let js_rules = clean_config.lang_rules.javascript.unwrap();
    assert_eq!(js_rules.single_line_comments, Some(true));
    assert_eq!(js_rules.multi_line_comments, Some(true));
    assert_eq!(js_rules.doc_comments, Some(false));
    assert_eq!(js_rules.min_comment_length, Some(15));
    assert_eq!(js_rules.preserve_patterns.unwrap().len(), 2);
}

#[test]
fn test_config_invalid_yaml() {
    let env = TestEnvironment::new();

    let invalid_yaml = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
      invalid_yaml: [unclosed array
    lang-rules:
      python:
        single-line-comments: not_a_boolean
"#;

    env.create_config(invalid_yaml);

    let config_result = ToolConfig::load(env.path().join(".llman").join("config.yaml"));
    assert!(config_result.is_err(), "Should fail to load invalid YAML");
}

#[test]
fn test_config_missing_required_fields() {
    let env = TestEnvironment::new();

    let incomplete_config = r#"
# Missing version field
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
    # Missing lang-rules
"#;

    env.create_config(incomplete_config);

    let config_result = ToolConfig::load(env.path().join(".llman").join("config.yaml"));
    // This might succeed with defaults or fail - depends on implementation
    // We just want to ensure it doesn't crash
    match config_result {
        Ok(config) => {
            // If it succeeds, check that reasonable defaults are set
            assert!(config.version.is_empty() || config.version == "0.1");
        }
        Err(_) => {
            // Failure is also acceptable for missing required fields
        }
    }
}

#[test]
fn test_config_invalid_types() {
    let env = TestEnvironment::new();

    let config_with_invalid_types = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include: "should_be_array_not_string"
      exclude: 12345  # Should be array
    lang-rules:
      python:
        single-line-comments: "should_be_boolean"
        min-comment-length: "should_be_number"
        preserve-patterns: "should_be_array"
"#;

    env.create_config(config_with_invalid_types);

    let config_result = ToolConfig::load(env.path().join(".llman").join("config.yaml"));
    // Should fail due to type mismatches
    assert!(config_result.is_err(), "Should fail due to type mismatches");
}

#[test]
fn test_config_invalid_regex_patterns() {
    let env = TestEnvironment::new();

    let config_with_invalid_regex = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
    lang-rules:
      python:
        single-line-comments: true
        preserve-patterns:
          - "[invalid_regex*(unclosed"
          - "^\\s*#\\s*(TODO|FIXME):"  # This one is valid
        min-comment-length: 10
"#;

    env.create_config(config_with_invalid_regex);

    let config_result = ToolConfig::load(env.path().join(".llman").join("config.yaml"));

    // Config loading might succeed (regex validation might happen at runtime)
    match config_result {
        Ok(config) => {
            // If config loads successfully, test that invalid regex is handled gracefully
            let test_file = env.create_file(
                "test.py",
                "# TODO: preserve this\n# remove this\ndef test(): pass",
            );

            let args = CleanUselessCommentsArgs {
                config: Some(env.path().join(".llman").join("config.yaml")),
                dry_run: true,
                yes: false,
                interactive: false,
                force: false,
                verbose: true,
                git_only: false,
                files: vec![test_file],
            };

            let mut processor = CommentProcessor::new(config, args);
            let result = processor.process();

            // Should handle invalid regex gracefully
            match result {
                Ok(_) => println!("Handled invalid regex gracefully"),
                Err(e) => println!("Failed as expected with invalid regex: {:?}", e),
            }
        }
        Err(_) => {
            // Failure during config loading is also acceptable
        }
    }
}

#[test]
fn test_config_edge_case_values() {
    let env = TestEnvironment::new();

    let edge_case_config = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include: []
      exclude: []
    lang-rules:
      python:
        single-line-comments: true
        multi-line-comments: false
        min-comment-length: 0  # Edge case: zero minimum
        preserve-patterns: []  # Edge case: empty patterns
      javascript:
        single-line-comments: false  # Edge case: disabled processing
        min-comment-length: 999999  # Edge case: very large minimum
        preserve-patterns:  # Edge case: complex patterns
          - "^\\s*//\\s*(TODO|FIXME|NOTE|HACK|XXX):\\s*.*$"
          - "^\\s*/\\*\\*[\\s\\S]*?\\*/"
          - "^\\s*//\\s*@[a-zA-Z].*$"
"#;

    env.create_config(edge_case_config);

    let config_result = ToolConfig::load(env.path().join(".llman").join("config.yaml"));
    assert!(config_result.is_ok(), "Should handle edge case values");

    let config = config_result.unwrap();
    let clean_config = config.tools.clean_useless_comments.unwrap();

    assert_eq!(clean_config.scope.include.len(), 0);
    assert_eq!(clean_config.scope.exclude.len(), 0);

    let python_rules = clean_config.lang_rules.python.unwrap();
    assert_eq!(python_rules.min_comment_length, Some(0));
    assert_eq!(python_rules.preserve_patterns.unwrap().len(), 0);

    let js_rules = clean_config.lang_rules.javascript.unwrap();
    assert_eq!(js_rules.min_comment_length, Some(999999));
    assert_eq!(js_rules.preserve_patterns.unwrap().len(), 3);
}

#[test]
fn test_config_unicode_support() {
    let env = TestEnvironment::new();

    let unicode_config = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
        - "**/*.测试.py"  # Unicode filename pattern
    lang-rules:
      python:
        single-line-comments: true
        preserve-patterns:
          - "^\\s*#\\s*(TODO|FIXME|注意|修复):"  # Unicode patterns
          - "^\\s*#.*[🚀⚠️]"  # Emoji in patterns
        min-comment-length: 5
    # Unicode comment for configuration
    description: "这是一个配置文件"
    author: "开发者👨‍💻"
"#;

    env.create_config(unicode_config);

    let config_result = ToolConfig::load(env.path().join(".llman").join("config.yaml"));
    assert!(
        config_result.is_ok(),
        "Should handle Unicode in configuration"
    );

    let config = config_result.unwrap();
    let clean_config = config.tools.clean_useless_comments.unwrap();

    assert!(
        clean_config
            .scope
            .include
            .contains(&"**/*.测试.py".to_string())
    );

    let python_rules = clean_config.lang_rules.python.unwrap();
    let patterns = python_rules.preserve_patterns.unwrap();
    assert!(
        patterns
            .iter()
            .any(|p| p.contains("注意") || p.contains("修复"))
    );
}

#[test]
fn test_config_file_not_found() {
    let env = TestEnvironment::new();

    let config_result = ToolConfig::load(env.path().join("nonexistent_config.yaml"));
    assert!(
        config_result.is_err(),
        "Should fail to load non-existent config file"
    );
}

#[test]
fn test_config_partial_configuration() {
    let env = TestEnvironment::new();

    let partial_config = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
    # lang-rules section is missing - should use defaults
"#;

    env.create_config(partial_config);

    let config_result = ToolConfig::load(env.path().join(".llman").join("config.yaml"));
    // Check if it succeeds or fails gracefully - both are acceptable
    match config_result {
        Ok(config) => {
            let clean_config = config.tools.clean_useless_comments.unwrap();
            assert_eq!(clean_config.scope.include.len(), 1);
            // lang_rules might be None or have default values
        }
        Err(_) => {
            // Failure is acceptable for partial configuration missing required fields
        }
    }
}

#[test]
fn test_config_schema_validation() {
    let env = TestEnvironment::new();

    let config_content = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
    lang-rules:
      python:
        single-line-comments: true
        min-comment-length: 10
"#;

    env.create_config(config_content);

    let _config = ToolConfig::load(env.path().join(".llman").join("config.yaml")).unwrap();

    // Test schema generation
    let schema_result = ToolConfig::generate_schema();
    assert!(schema_result.is_ok(), "Should generate valid JSON schema");

    let schema = schema_result.unwrap();
    // Schema is a string, not an object with properties
    assert!(!schema.is_empty(), "Schema should have content");
}

#[test]
fn test_config_environment_substitution() {
    let env = TestEnvironment::new();

    let config_with_env = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
    lang-rules:
      python:
        single-line-comments: true
        min-comment-length: ${TEST_MIN_LENGTH}
"#;

    env.create_config(config_with_env);

    let config_result = ToolConfig::load(env.path().join(".llman").join("config.yaml"));

    // This test depends on whether environment substitution is implemented
    // If it is, the value should be 15; if not, it might fail or remain as string
    match config_result {
        Ok(config) => {
            let clean_config = config.tools.clean_useless_comments.unwrap();
            let python_rules = clean_config.lang_rules.python.unwrap();
            // Either it's 15 (substitution worked) or some other default
            println!(
                "Environment substitution result: {:?}",
                python_rules.min_comment_length
            );
        }
        Err(_) => {
            println!("Environment substitution not implemented or failed");
        }
    }
}

#[test]
fn test_config_inheritance_and_overrides() {
    let env = TestEnvironment::new();

    // Test global config
    let global_config = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
        - "**/*.js"
      exclude:
        - "**/test/**"
    lang-rules:
      python:
        single-line-comments: true
        min-comment-length: 10
      javascript:
        single-line-comments: true
        min-comment-length: 15
"#;

    env.create_config(global_config);

    let config = ToolConfig::load(env.path().join(".llman").join("config.yaml")).unwrap();
    let clean_config = config.tools.clean_useless_comments.unwrap();

    // Verify inheritance worked correctly
    assert_eq!(clean_config.scope.include.len(), 2);
    assert_eq!(clean_config.scope.exclude.len(), 1);

    let python_rules = clean_config.lang_rules.python.unwrap();
    let js_rules = clean_config.lang_rules.javascript.unwrap();

    assert_eq!(python_rules.min_comment_length, Some(10));
    assert_eq!(js_rules.min_comment_length, Some(15));
}

#[test]
fn test_config_validation_integration() {
    let env = TestEnvironment::new();

    let test_file = env.create_file(
        "test.py",
        "# Short comment\n# TODO: Important comment\ndef test(): pass",
    );

    let valid_config = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.py"
    lang-rules:
      python:
        single-line-comments: true
        preserve-patterns:
          - "^\\s*#\\s*(TODO|FIXME):"
        min-comment-length: 15
"#;

    env.create_config(valid_config);

    let config = ToolConfig::load(env.path().join(".llman").join("config.yaml")).unwrap();

    // Test that config works with processor
    let args = CleanUselessCommentsArgs {
        config: Some(env.path().join(".llman").join("config.yaml")),
        dry_run: true,
        yes: false,
        interactive: false,
        force: false,
        verbose: true,
        git_only: false,
        files: vec![test_file],
    };

    let mut processor = CommentProcessor::new(config, args);
    let result = processor.process();

    assert!(
        result.is_ok(),
        "Config should work correctly with processor"
    );

    let processing_result = result.unwrap();
    assert_eq!(
        processing_result.errors, 0,
        "Should have no processing errors"
    );
    // In dry-run mode with high min-comment-length (15), changes might not be detected
    // Both detecting changes and not detecting changes are valid outcomes
    println!(
        "Files changed: {} (this is normal for dry-run with conservative settings)",
        processing_result.files_changed.len()
    );
}

#[test]
fn test_config_typescript_rules() {
    let env = TestEnvironment::new();

    let typescript_config = r#"
version: "0.1"
tools:
  clean-useless-comments:
    scope:
      include:
        - "**/*.ts"
        - "**/*.tsx"
    lang-rules:
      javascript:
        single-line-comments: true
        multi-line-comments: true
        doc-comments: false
        preserve-patterns:
          - "^\\s*//\\s*(TODO|FIXME):"
          - "^\\s*/\\*\\*[\\s\\S]*?\\*/"
          - "^\\s*//\\s*@[a-zA-Z].*$"
        min-comment-length: 12
"#;

    env.create_config(typescript_config);

    let config = ToolConfig::load(env.path().join(".llman").join("config.yaml")).unwrap();
    let clean_config = config.tools.clean_useless_comments.unwrap();

    // TypeScript should use javascript rules
    let js_rules = clean_config.lang_rules.javascript.unwrap();
    assert_eq!(js_rules.single_line_comments, Some(true));
    assert_eq!(js_rules.multi_line_comments, Some(true));
    assert_eq!(js_rules.doc_comments, Some(false));
    assert_eq!(js_rules.min_comment_length, Some(12));
    assert_eq!(js_rules.preserve_patterns.unwrap().len(), 3);

    // Should include both .ts and .tsx files
    assert!(clean_config.scope.include.contains(&"**/*.ts".to_string()));
    assert!(clean_config.scope.include.contains(&"**/*.tsx".to_string()));
}