chaser 0.1.0

An automated file path synchronization tool that updates changed paths in configuration files in real time.
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
// Integration tests for complete application workflows

use chaser::{cli, config::Config, i18n};
use serial_test::serial;
use std::env;
use std::fs;
use tempfile::TempDir;

fn setup_test_env() -> TempDir {
    let temp_dir = TempDir::new().unwrap();

    let locales_dir = temp_dir.path().join("locales");
    fs::create_dir_all(&locales_dir).unwrap();

    let en_content = r#"
app_description: "An automated file path synchronization tool"
cmd_add: "Add a path to watch"
cmd_remove: "Remove a path from watch list"
cmd_list: "List all watched paths and settings"
cmd_config: "Show config file location"
cmd_recursive: "Set recursive watching (true/false)"
cmd_ignore: "Add ignore pattern"
cmd_reset: "Reset config to default"
cmd_lang: "Set interface language"
cmd_add_target: "Add a target file for path synchronization"
cmd_remove_target: "Remove a target file"
cmd_list_targets: "List all target files"
cmd_status: "Show path synchronization status"
cmd_sync: "Start path synchronization monitoring"
cmd_update_path: "Manually update a path in target files"
arg_path: "Path to add to watch list"
arg_path_remove: "Path to remove from watch list"
arg_recursive_enabled: "Enable or disable recursive watching"
arg_ignore_pattern: "Pattern to ignore (e.g., \"*.tmp\", \".git/**\")"
arg_language: "Language code (en, zh-cn)"
arg_target_file: "Target file path (json, yaml, toml, csv)"
arg_target_file_remove: "Target file path to remove"
arg_sync_once: "Perform one-time sync without monitoring"
arg_old_path: "Old path to replace"
arg_new_path: "New path to replace with"
msg_path_added: "✓ Added watch path: {0}"
msg_path_exists: "⚠ Path already exists: {0}"
msg_path_removed: "✓ Removed watch path: {0}"
msg_path_not_found: "❌ Path not found: {0}"
msg_config_loaded: "✓ Config loaded from: {0}"
msg_config_saved: "✓ Config saved to: {0}"
msg_config_created: "✓ Created default config at: {0}"
ui_watch_paths: "Watch Paths:"
ui_settings: "Settings:"
ui_recursive: "Recursive: {0}"
"#;
    fs::write(locales_dir.join("en.yaml"), en_content).unwrap();

    temp_dir
}

fn setup_test_cli() -> clap::Command {
    // Fallback to a simple CLI without i18n to avoid test environment issues
    clap::Command::new("chaser")
        .about("An automated file path synchronization tool")
        .version(env!("CARGO_PKG_VERSION"))
        .subcommand_required(false)
        .arg_required_else_help(false)
        .subcommand(
            clap::Command::new("add")
                .about("Add a path to watch")
                .arg(clap::Arg::new("path").index(1).required(true)),
        )
        .subcommand(
            clap::Command::new("remove")
                .about("Remove a path from watch list")
                .arg(clap::Arg::new("path").index(1).required(true)),
        )
        .subcommand(clap::Command::new("list").about("List all watched paths and settings"))
        .subcommand(clap::Command::new("config").about("Show config file location"))
        .subcommand(
            clap::Command::new("recursive")
                .about("Set recursive watching (true/false)")
                .arg(clap::Arg::new("enabled").index(1).required(true)),
        )
        .subcommand(
            clap::Command::new("ignore")
                .about("Add ignore pattern")
                .arg(clap::Arg::new("pattern").index(1).required(true)),
        )
        .subcommand(clap::Command::new("reset").about("Reset config to default"))
        .subcommand(
            clap::Command::new("lang")
                .about("Set interface language")
                .arg(clap::Arg::new("language").index(1).required(true)),
        )
        .subcommand(
            clap::Command::new("add-target")
                .about("Add a target file for path synchronization")
                .arg(clap::Arg::new("file").index(1).required(true)),
        )
        .subcommand(
            clap::Command::new("remove-target")
                .about("Remove a target file")
                .arg(clap::Arg::new("file").index(1).required(true)),
        )
        .subcommand(clap::Command::new("list-targets").about("List all target files"))
        .subcommand(clap::Command::new("status").about("Show path synchronization status"))
        .subcommand(
            clap::Command::new("sync")
                .about("Start path synchronization monitoring")
                .arg(
                    clap::Arg::new("once")
                        .long("once")
                        .action(clap::ArgAction::SetTrue),
                ),
        )
        .subcommand(
            clap::Command::new("update-path")
                .about("Manually update a path in target files")
                .arg(clap::Arg::new("old_path").index(1).required(true))
                .arg(clap::Arg::new("new_path").index(2).required(true)),
        )
}

#[test]
#[serial]
fn test_config_operations_integration() {
    let temp_dir = setup_test_env();
    let original_dir = env::current_dir().unwrap();
    env::set_current_dir(temp_dir.path()).unwrap();

    // Test with the temporary directory containing locales
    let result = i18n::init_i18n_with_locale("en");
    if result.is_err() {
        eprintln!("Warning: Could not initialize i18n: {:?}", result);
    }

    let mut config = Config::default();

    // Test adding paths
    let result = config.add_path("/test/path1".to_string());
    assert!(result.is_ok());
    assert!(config.watch_paths.contains(&"/test/path1".to_string()));

    let result = config.add_path("/test/path2".to_string());
    assert!(result.is_ok());
    assert!(config.watch_paths.contains(&"/test/path2".to_string()));

    // Test adding duplicate path
    let result = config.add_path("/test/path1".to_string());
    assert!(result.is_ok()); // Should not error, just not add duplicate

    // Test removing path
    let result = config.remove_path("/test/path1");
    assert!(result.is_ok());
    assert!(!config.watch_paths.contains(&"/test/path1".to_string()));

    // Test removing non-existent path
    let result = config.remove_path("/non/existent");
    assert!(result.is_ok()); // Should not error

    // Restore original directory
    env::set_current_dir(original_dir).unwrap();
}

#[test]
fn test_cli_parsing_integration() {
    let command = setup_test_cli();
    let matches = command.try_get_matches_from(&["chaser"]).unwrap();
    assert!(cli::parse_command(&matches).is_none());

    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "add", "/new/path"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::Add { path }) => assert_eq!(path, "/new/path"),
        _ => panic!("Expected Add command"),
    }

    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "remove", "/old/path"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::Remove { path }) => assert_eq!(path, "/old/path"),
        _ => panic!("Expected Remove command"),
    }

    let command = setup_test_cli();
    let matches = command.try_get_matches_from(&["chaser", "list"]).unwrap();
    assert!(matches!(
        cli::parse_command(&matches),
        Some(cli::Commands::List)
    ));

    let command = setup_test_cli();
    let matches = command.try_get_matches_from(&["chaser", "config"]).unwrap();
    assert!(matches!(
        cli::parse_command(&matches),
        Some(cli::Commands::Config)
    ));

    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "recursive", "false"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::Recursive { enabled }) => assert_eq!(enabled, "false"),
        _ => panic!("Expected Recursive command"),
    }

    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "ignore", "*.backup"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::Ignore { pattern }) => assert_eq!(pattern, "*.backup"),
        _ => panic!("Expected Ignore command"),
    }

    let command = setup_test_cli();
    let matches = command.try_get_matches_from(&["chaser", "reset"]).unwrap();
    assert!(matches!(
        cli::parse_command(&matches),
        Some(cli::Commands::Reset)
    ));

    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "lang", "zh-cn"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::Lang { language }) => assert_eq!(language, "zh-cn"),
        _ => panic!("Expected Lang command"),
    }
}

#[test]
#[serial]
fn test_config_persistence_integration() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.yaml");

    let mut original_config = Config::default();
    original_config.watch_paths = vec!["/test1".to_string(), "/test2".to_string()];
    original_config.recursive = false;
    original_config.ignore_patterns = vec!["*.test".to_string()];
    original_config.language = Some("zh-cn".to_string());

    let yaml_content = serde_yaml_ng::to_string(&original_config).unwrap();
    fs::write(&config_path, yaml_content).unwrap();

    let content = fs::read_to_string(&config_path).unwrap();
    let loaded_config: Config = serde_yaml_ng::from_str(&content).unwrap();

    assert_eq!(original_config, loaded_config);
    assert_eq!(loaded_config.watch_paths, vec!["/test1", "/test2"]);
    assert_eq!(loaded_config.recursive, false);
    assert_eq!(loaded_config.ignore_patterns, vec!["*.test"]);
    assert_eq!(loaded_config.language, Some("zh-cn".to_string()));
}

#[test]
fn test_recursive_option_parsing() {
    let test_cases = vec![
        ("true", true),
        ("1", true),
        ("yes", true),
        ("on", true),
        ("false", false),
        ("0", false),
        ("no", false),
        ("off", false),
    ];

    for (input, expected) in test_cases {
        let command = setup_test_cli();
        let matches = command
            .try_get_matches_from(&["chaser", "recursive", input])
            .unwrap();
        match cli::parse_command(&matches) {
            Some(cli::Commands::Recursive { enabled }) => {
                let parsed = match enabled.to_lowercase().as_str() {
                    "true" | "1" | "yes" | "on" => true,
                    "false" | "0" | "no" | "off" => false,
                    _ => false,
                };
                assert_eq!(parsed, expected, "Failed for input: {}", input);
            }
            _ => panic!("Expected Recursive command for input: {}", input),
        }
    }
}

#[test]
fn test_error_handling() {
    let command = setup_test_cli();
    let result = command.try_get_matches_from(&["chaser", "invalid_command"]);
    assert!(result.is_err());

    let command = setup_test_cli();
    let result = command.try_get_matches_from(&["chaser", "add"]);
    assert!(result.is_err());

    let command = setup_test_cli();
    let result = command.try_get_matches_from(&["chaser", "remove"]);
    assert!(result.is_err());

    let command = setup_test_cli();
    let result = command.try_get_matches_from(&["chaser", "recursive"]);
    assert!(result.is_err());

    let command = setup_test_cli();
    let result = command.try_get_matches_from(&["chaser", "ignore"]);
    assert!(result.is_err());

    let command = setup_test_cli();
    let result = command.try_get_matches_from(&["chaser", "lang"]);
    assert!(result.is_err());
}

#[test]
fn test_config_edge_cases() {
    let mut config = Config::default();

    let result = config.add_path(String::new());
    assert!(result.is_ok());

    let path = "./test_path".to_string();
    config.add_path(path.clone()).unwrap();
    assert!(config.watch_paths.contains(&path));

    config.remove_path(&path).unwrap();
    assert!(!config.watch_paths.contains(&path));

    // Test with paths containing special characters
    let special_path = "/path with spaces/and-dashes_and.dots".to_string();
    config.add_path(special_path.clone()).unwrap();
    assert!(config.watch_paths.contains(&special_path));

    // Test case sensitivity
    let case_path = "/CaseSensitive/Path".to_string();
    config.add_path(case_path.clone()).unwrap();
    assert!(config.watch_paths.contains(&case_path));

    let lower_case_path = "/casesensitive/path".to_string();
    config.add_path(lower_case_path.clone()).unwrap();
    assert!(config.watch_paths.contains(&lower_case_path));
    assert_ne!(case_path, lower_case_path);
}

#[test]
fn test_new_commands() {
    // Test add-target command
    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "add-target", "config.json"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::AddTarget { file }) => assert_eq!(file, "config.json"),
        _ => panic!("Expected AddTarget command"),
    }

    // Test remove-target command
    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "remove-target", "config.json"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::RemoveTarget { file }) => assert_eq!(file, "config.json"),
        _ => panic!("Expected RemoveTarget command"),
    }

    // Test list-targets command
    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "list-targets"])
        .unwrap();
    assert!(matches!(
        cli::parse_command(&matches),
        Some(cli::Commands::ListTargets)
    ));

    // Test status command
    let command = setup_test_cli();
    let matches = command.try_get_matches_from(&["chaser", "status"]).unwrap();
    assert!(matches!(
        cli::parse_command(&matches),
        Some(cli::Commands::Status)
    ));

    // Test sync command
    let command = setup_test_cli();
    let matches = command.try_get_matches_from(&["chaser", "sync"]).unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::Sync { once }) => assert!(!once),
        _ => panic!("Expected Sync command"),
    }

    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "sync", "--once"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::Sync { once }) => assert!(once),
        _ => panic!("Expected Sync command with once flag"),
    }

    // Test update-path command
    let command = setup_test_cli();
    let matches = command
        .try_get_matches_from(&["chaser", "update-path", "/old/path", "/new/path"])
        .unwrap();
    match cli::parse_command(&matches) {
        Some(cli::Commands::UpdatePath { old_path, new_path }) => {
            assert_eq!(old_path, "/old/path");
            assert_eq!(new_path, "/new/path");
        }
        _ => panic!("Expected UpdatePath command"),
    }
}