journey-cli 0.9.11

A CLI-based journal application with automatic timestamping, vault management, and Obsidian integration
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
use journey::config::{Config, VaultConfig};
use std::collections::HashMap;
use std::path::PathBuf;
use serial_test::serial;

#[test]
fn test_config_new() {
    let config = Config::new();
    assert!(config.vaults.is_empty());
}

#[test]
fn test_config_add_vault() {
    let mut config = Config::new();
    let vault = VaultConfig::test_config("test", "/tmp/test");
    
    config.add_vault(vault);
    assert_eq!(config.vaults.len(), 1);
    assert!(config.vaults.contains_key("test"));
}

#[test]
fn test_config_get_vault() {
    let mut config = Config::new();
    let vault = VaultConfig::test_config("test", "/tmp/test");
    
    config.add_vault(vault);
    
    let retrieved = config.get_vault("test");
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().name, "test");
    
    let not_found = config.get_vault("nonexistent");
    assert!(not_found.is_none());
}

#[test]
fn test_config_get_default_vault() {
    let mut config = Config::new();
    
    // No vaults
    assert!(config.get_default_vault().is_none());
    
    // Add a vault
    let vault = VaultConfig::test_config("test", "/tmp/test");
    
    config.add_vault(vault);
    let default = config.get_default_vault();
    assert!(default.is_some());
    assert_eq!(default.unwrap().name, "test");
}

#[test]
fn test_vault_config_creation() {
    let mut vault = VaultConfig::test_config("test", "/tmp/test");
    vault.section_name = Some("Daily Notes".to_string());
    
    assert_eq!(vault.name, "test");
    assert_eq!(vault.path, PathBuf::from("/tmp/test"));
    assert_eq!(vault.locale, "en-US");
    assert_eq!(vault.section_name, Some("Daily Notes".to_string()));
}

#[test]
#[serial]
fn test_tilde_expansion() {
    use serde_yaml_ng;
    use std::env;
    
    // Set a test HOME environment variable
    let original_home = env::var("HOME").unwrap_or_default();
    env::set_var("HOME", "/home/testuser");
    
    // Test YAML with tilde expansion
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: ~/journal
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: null
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // The path should be expanded to the full home directory path
    assert_eq!(vault.path, PathBuf::from("/home/testuser/journal"));
    
    // Restore original HOME
    if !original_home.is_empty() {
        env::set_var("HOME", original_home);
    } else {
        env::remove_var("HOME");
    }
}

#[test]
#[serial]
fn test_tilde_expansion_with_subdirectory() {
    use serde_yaml_ng;
    use std::env;
    
    // Get the current HOME for expected path
    let current_home = env::var("HOME").unwrap_or_else(|_| "/home/user".to_string());
    
    // Test YAML with tilde expansion in subdirectory
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: ~/Documents/journals
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: null
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // The path should be expanded to the current home directory path
    let expected_path = PathBuf::from(current_home).join("Documents/journals");
    assert_eq!(vault.path, expected_path);
}

#[test]
#[serial]
fn test_tilde_expansion_windows_style() {
    use serde_yaml_ng;
    use std::env;
    
    // Test Windows-style environment variables
    let original_userprofile = env::var("USERPROFILE").unwrap_or_default();
    let original_home = env::var("HOME").unwrap_or_default();
    
    // Set Windows-style environment variables
    env::set_var("USERPROFILE", "C:\\Users\\testuser");
    env::set_var("HOME", "/home/testuser"); // Keep this as fallback
    
    // Test YAML with tilde expansion
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: ~/Documents/journal
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: null
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // On Windows, should use USERPROFILE, on Unix should use HOME
    let expected_path = if cfg!(windows) {
        PathBuf::from("C:\\Users\\testuser\\Documents\\journal")
    } else {
        PathBuf::from("/home/testuser/Documents/journal")
    };
    assert_eq!(vault.path, expected_path);
    
    // Restore original environment variables
    if !original_userprofile.is_empty() {
        env::set_var("USERPROFILE", original_userprofile);
    } else {
        env::remove_var("USERPROFILE");
    }
    if !original_home.is_empty() {
        env::set_var("HOME", original_home);
    } else {
        env::remove_var("HOME");
    }
}

#[test]
#[serial]
fn test_tilde_expansion_fallback() {
    use serde_yaml_ng;
    use std::env;
    
    // Test fallback behavior when no home directory is found
    let original_userprofile = env::var("USERPROFILE").ok();
    let original_home = env::var("HOME").ok();
    
    // Remove environment variables to test fallback
    env::remove_var("USERPROFILE");
    env::remove_var("HOME");
    
    // Test YAML with tilde expansion
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: ~/fallback-test
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: null
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // Should fall back to the original path when no home directory is found
    // Note: This test might not work in all environments due to system HOME variables
    // The important thing is that the code handles the case gracefully
    let path_str = vault.path.to_string_lossy();
    assert!(path_str.contains("fallback-test"));
    
    // Restore original environment variables
    if let Some(val) = original_userprofile {
        env::set_var("USERPROFILE", val);
    }
    // Don't remove if it wasn't set originally
    
    if let Some(val) = original_home {
        env::set_var("HOME", val);
    }
    // Don't remove if it wasn't set originally
}

#[test]
#[serial]
fn test_windows_env_var_expansion() {
    use serde_yaml_ng;
    use std::env;
    
    // Test Windows-style environment variable expansion
    let original_userprofile = env::var("USERPROFILE").unwrap_or_default();
    let original_appdata = env::var("APPDATA").unwrap_or_default();
    
    // Set Windows-style environment variables
    env::set_var("USERPROFILE", "C:\\Users\\testuser");
    env::set_var("APPDATA", "C:\\Users\\testuser\\AppData\\Roaming");
    
    // Test YAML with Windows environment variable expansion
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: "%USERPROFILE%/Documents/journal"
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: null
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // On Windows, should expand %USERPROFILE%, on Unix should leave as-is
    let expected_path = if cfg!(windows) {
        PathBuf::from("C:\\Users\\testuser\\Documents\\journal")
    } else {
        PathBuf::from("%USERPROFILE%/Documents/journal")
    };
    assert_eq!(vault.path, expected_path);
    
    // Restore original environment variables
    if !original_userprofile.is_empty() {
        env::set_var("USERPROFILE", original_userprofile);
    } else {
        env::remove_var("USERPROFILE");
    }
    if !original_appdata.is_empty() {
        env::set_var("APPDATA", original_appdata);
    } else {
        env::remove_var("APPDATA");
    }
}

#[test]
#[serial]
fn test_windows_env_var_expansion_appdata() {
    use serde_yaml_ng;
    use std::env;
    
    // Test APPDATA environment variable expansion
    let original_appdata = env::var("APPDATA").unwrap_or_default();
    
    // Set APPDATA environment variable
    env::set_var("APPDATA", "C:\\Users\\testuser\\AppData\\Roaming");
    
    // Test YAML with APPDATA expansion
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: "%APPDATA%/journey"
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: null
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // On Windows, should expand %APPDATA%, on Unix should leave as-is
    let expected_path = if cfg!(windows) {
        PathBuf::from("C:\\Users\\testuser\\AppData\\Roaming\\journey")
    } else {
        PathBuf::from("%APPDATA%/journey")
    };
    assert_eq!(vault.path, expected_path);
    
    // Restore original environment variable
    if !original_appdata.is_empty() {
        env::set_var("APPDATA", original_appdata);
    } else {
        env::remove_var("APPDATA");
    }
}

#[test]
#[serial]
fn test_windows_env_var_expansion_multiple() {
    use serde_yaml_ng;
    use std::env;
    
    // Test multiple environment variables in one path
    let original_userprofile = env::var("USERPROFILE").unwrap_or_default();
    let original_username = env::var("USERNAME").unwrap_or_default();
    
    // Set environment variables
    env::set_var("USERPROFILE", "C:\\Users\\testuser");
    env::set_var("USERNAME", "testuser");
    
    // Test YAML with multiple environment variables
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: "%USERPROFILE%/Documents/%USERNAME%_journal"
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: null
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // On Windows, should expand both variables, on Unix should leave as-is
    let expected_path = if cfg!(windows) {
        PathBuf::from("C:\\Users\\testuser\\Documents\\testuser_journal")
    } else {
        PathBuf::from("%USERPROFILE%/Documents/%USERNAME%_journal")
    };
    assert_eq!(vault.path, expected_path);
    
    // Restore original environment variables
    if !original_userprofile.is_empty() {
        env::set_var("USERPROFILE", original_userprofile);
    } else {
        env::remove_var("USERPROFILE");
    }
    if !original_username.is_empty() {
        env::set_var("USERNAME", original_username);
    } else {
        env::remove_var("USERNAME");
    }
}

#[test]
#[serial]
fn test_template_file_tilde_expansion() {
    use serde_yaml_ng;
    use std::env;
    
    // Test tilde expansion in template_file
    let original_home = env::var("HOME").unwrap_or_default();
    env::set_var("HOME", "/home/testuser");
    
    // Test YAML with tilde expansion in template_file
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: /tmp/test
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: "~/Documents/templates/journal.md"
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // The template_file should be expanded to the full home directory path
    assert_eq!(vault.template_file, Some("/home/testuser/Documents/templates/journal.md".to_string()));
    
    // Restore original HOME
    if !original_home.is_empty() {
        env::set_var("HOME", original_home);
    } else {
        env::remove_var("HOME");
    }
}

#[test]
#[serial]
fn test_template_file_windows_env_var_expansion() {
    use serde_yaml_ng;
    use std::env;
    
    // Test Windows environment variable expansion in template_file
    let original_userprofile = env::var("USERPROFILE").unwrap_or_default();
    env::set_var("USERPROFILE", "C:\\Users\\testuser");
    
    // Test YAML with Windows environment variable expansion in template_file
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: /tmp/test
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: "%USERPROFILE%/Documents/templates/journal.md"
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // On Windows, should expand %USERPROFILE%, on Unix should leave as-is
    let expected_template = if cfg!(windows) {
        "C:\\Users\\testuser\\Documents\\templates\\journal.md"
    } else {
        "%USERPROFILE%/Documents/templates/journal.md"
    };
    assert_eq!(vault.template_file, Some(expected_template.to_string()));
    
    // Restore original environment variable
    if !original_userprofile.is_empty() {
        env::set_var("USERPROFILE", original_userprofile);
    } else {
        env::remove_var("USERPROFILE");
    }
}

#[test]
fn test_template_file_no_expansion() {
    use serde_yaml_ng;
    
    // Test that regular paths without tildes or env vars are not modified
    let yaml_content = r#"
vaults:
  test:
    name: test
    path: /tmp/test
    locale: en-US
    phrases: {}
    section_name: null
    date_format: null
    template_file: "/absolute/path/to/template.md"
    file_path_format: null
"#;
    
    let config: Config = serde_yaml_ng::from_str(yaml_content).unwrap();
    let vault = config.get_vault("test").unwrap();
    
    // The template_file should remain unchanged
    assert_eq!(vault.template_file, Some("/absolute/path/to/template.md".to_string()));
}