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
use journey::app::{App, CliArgs};
use journey::config::{Config, VaultConfig};
use journey::config_manager::ConfigManager;
use std::collections::HashMap;
use std::path::PathBuf;
use tempfile::TempDir;
use std::env;
use chrono::{NaiveDate, Local};
use serial_test::serial;

// Safety mechanism: Ensure tests never touch production config
fn ensure_test_isolation() {
    // Clear any existing config environment variable to prevent accidental production access
    env::remove_var("JOURNEY_CONFIG");
    env::remove_var("JOURNEY_CONFIG_PATH"); // Also clear the old variable name
}

// Helper function to create an app with a specific config
fn create_app_with_config(config: Config) -> (App, impl FnOnce()) {
    // Ensure test isolation before creating app
    ensure_test_isolation();
    
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("journey.yaml");
    
    // Create a temporary directory for vault paths if they exist
    let vault_temp_dir = TempDir::new().unwrap();
    
    // Update any vault paths in the config to use the temporary directory
    let mut updated_config = config;
    for (i, vault) in updated_config.vaults.values_mut().enumerate() {
        vault.path = vault_temp_dir.path().join(format!("vault_{}", i));
    }
    
    let config_manager = ConfigManager { config_path: config_path.clone() };
    config_manager.save_config(&updated_config).unwrap();
    
    // Set the config path environment variable for test isolation
    env::set_var("JOURNEY_CONFIG", config_path.to_str().unwrap());
    
    let app = App::new().unwrap();
    let cleanup = move || {
        env::remove_var("JOURNEY_CONFIG");
        // Keep temp directories alive to prevent cleanup
        let _temp_dir = temp_dir;
        let _vault_temp_dir = vault_temp_dir;
    };
    
    (app, cleanup)
}

#[test]
#[serial]
fn test_app_creation() {
    // Create a config with one vault so the app can be created
    let mut config = Config::new();
    let vault_config = VaultConfig::test_config("test-vault", "/tmp/test-vault");
    config.add_vault(vault_config);
    
    let (app, _cleanup) = create_app_with_config(config);
    
    // This should work
    assert!(app.get_vault(None).is_ok());
}

#[test]
#[serial]
fn test_single_vault_auto_selection() {
    // Create a config with one vault
    let mut config = Config::new();
    let vault_config = VaultConfig::test_config("test-vault", "/tmp/test-vault");
    config.add_vault(vault_config);
    
    let (app, _cleanup) = create_app_with_config(config);
    
    // Should automatically select the single vault when no vault specified
    let vault = app.get_vault(None);
    assert!(vault.is_ok());
    
    // Should also work when explicitly specifying the vault
    let vault = app.get_vault(Some("test-vault"));
    assert!(vault.is_ok());
}

#[test]
#[serial]
fn test_multiple_vaults_require_specification() {
    // Create a config with multiple vaults
    let mut config = Config::new();
    
    let vault1 = VaultConfig::test_config("vault1", "/tmp/vault1");
    
    let vault2 = VaultConfig::test_config("vault2", "/tmp/vault2");
    
    config.add_vault(vault1);
    config.add_vault(vault2);
    
    let (app, _cleanup) = create_app_with_config(config);
    
    // Should fail when no vault specified and multiple exist
    let vault = app.get_vault(None);
    assert!(vault.is_err());
    
    // Should work when explicitly specifying a vault
    let vault = app.get_vault(Some("vault1"));
    assert!(vault.is_ok());
    
    let vault = app.get_vault(Some("vault2"));
    assert!(vault.is_ok());
}

#[test]
#[serial]
fn test_no_vaults_configured() {
    // Create an empty config
    let config = Config::new();
    let (app, _cleanup) = create_app_with_config(config);
    
    // Should fail when no vaults are configured
    let vault = app.get_vault(None);
    assert!(vault.is_err());
    
    // Should also fail when trying to specify a non-existent vault
    let vault = app.get_vault(Some("non-existent"));
    assert!(vault.is_err());
}

#[test]
#[serial]
fn test_no_config_file_exists() {
    // Ensure test isolation
    ensure_test_isolation();
    
    // Create a temporary directory without any config file
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("journey.yaml");
    
    // Set the config path environment variable to point to non-existent file
    env::set_var("JOURNEY_CONFIG", config_path.to_str().unwrap());
    
    // App creation should fail when no config file exists
    let app = App::new();
    assert!(app.is_err(), "Expected App::new() to fail when config file doesn't exist. Got: {:?}", app.as_ref().err());
    
    // Check that the error message is helpful
    if let Err(journey::errors::JourneyError::Config(msg)) = app {
        assert!(msg.contains("No configuration file found"));
        // The error message was updated to use --init instead of init
        assert!(msg.contains("journey --init"));
    } else {
        panic!("Expected Config error");
    }
    
    // Keep temp directory alive to prevent cleanup during test
    let _temp_dir = temp_dir;
    
    // Clean up environment variable to prevent interference with other tests
    env::remove_var("JOURNEY_CONFIG");
}

#[test]
#[serial]
fn test_init_vault_with_name() {
    // Ensure test isolation
    ensure_test_isolation();
    
    // Create a temporary directory for config
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("journey.yaml");
    
    // Set the config path environment variable for test isolation
    env::set_var("JOURNEY_CONFIG", config_path.to_str().unwrap());
    
    // Create app using new_for_init (allows empty config)
    let mut app = App::new_for_init().unwrap();
    
    // Test init with explicit name using temporary directory
    let vault_temp_dir = TempDir::new().unwrap();
    let result = app.init_vault(vault_temp_dir.path().to_path_buf(), Some("test-vault".to_string()), false);
    assert!(result.is_ok());
    
    // Keep temp directories alive to prevent cleanup during test
    let _temp_dir = temp_dir;
    let _vault_temp_dir = vault_temp_dir;
    
    // Clean up environment variable to prevent interference
    env::remove_var("JOURNEY_CONFIG");
}

#[test]
#[serial]
fn test_init_vault_without_name() {
    // Ensure test isolation
    ensure_test_isolation();
    
    // Create a temporary directory for config
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("journey.yaml");
    
    // Set the config path environment variable for test isolation
    env::set_var("JOURNEY_CONFIG", config_path.to_str().unwrap());
    
    // Create app using new_for_init (allows empty config)
    let mut app = App::new_for_init().unwrap();
    
    // Test init without name - should use path basename using temporary directory
    let vault_temp_dir = TempDir::new().unwrap();
    let result = app.init_vault(vault_temp_dir.path().to_path_buf(), None, false);
    assert!(result.is_ok());
    
    // Keep temp directories alive to prevent cleanup during test
    let _temp_dir = temp_dir;
    let _vault_temp_dir = vault_temp_dir;
}

#[test]
#[serial]
fn test_init_vault_invalid_path() {
    // Ensure test isolation
    ensure_test_isolation();
    
    // Create a temporary directory for config
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("journey.yaml");
    
    // Set the config path environment variable for test isolation
    env::set_var("JOURNEY_CONFIG", config_path.to_str().unwrap());
    
    // Create app using new_for_init (allows empty config)
    let mut app = App::new_for_init().unwrap();
    
    // Test init with invalid path (empty)
    let result = app.init_vault(PathBuf::from(""), None, false);
    assert!(result.is_err());
    
    // Check that the error message is appropriate
    if let Err(journey::errors::JourneyError::Config(msg)) = result {
        assert!(msg.contains("Invalid path: cannot extract basename"));
    } else {
        panic!("Expected Config error");
    }
}

#[test]
#[serial]
fn test_production_config_isolation() {
    // This test verifies that tests never touch production config
    // even if production config exists
    
    // Ensure test isolation
    ensure_test_isolation();
    
    // Create a test config
    let mut test_config = Config::new();
    let vault_config = VaultConfig::test_config("test-vault", "/tmp/test-vault");
    test_config.add_vault(vault_config);
    
    // Create app - should use test config, not production
    // Use the helper function to ensure proper isolation
    let (app, _cleanup) = create_app_with_config(test_config);
    
    // Verify that the app is using the test config
    let vault = app.get_vault(Some("test-vault"));
    assert!(vault.is_ok());
    
    // Verify that production config (if it exists) was not touched
    // by checking that our test config path is being used
    // The app should be using the test config, not any production config
    
    // The test config should be isolated from any production config
    // This is verified by the fact that we're using a temporary directory
    // and the JOURNEY_CONFIG environment variable
    
    // The helper function handles cleanup automatically
}

#[test]
#[serial]
fn test_add_note_future_date_absolute() {
    // Test adding a note to a future date using --date
    let mut config = Config::new();
    let vault_config = VaultConfig::test_config("test", "/tmp/test-vault");
    config.add_vault(vault_config);
    
    let (app, _cleanup) = create_app_with_config(config);
    
    // This test verifies that the app can handle future dates
    // We can't easily test the full CLI parsing here, but we can test the date parsing logic
    let cli_args = CliArgs {
        vault: None,
        date: Some("2025-12-25".to_string()),
        relative_date: None,
        time: None,
        time_format: None,
    };
    
    let result = app.parse_date(&cli_args);
    assert!(result.is_ok());
    
    let date = result.unwrap();
    assert_eq!(date, NaiveDate::from_ymd_opt(2025, 12, 25).unwrap());
}

#[test]
#[serial]
fn test_add_note_future_date_relative() {
    // Test adding a note to a future date using --relative-date
    let mut config = Config::new();
    let vault_config = VaultConfig::test_config("test", "/tmp/test-vault");
    config.add_vault(vault_config);
    
    let (app, _cleanup) = create_app_with_config(config);
    
    let cli_args = CliArgs {
        vault: None,
        date: None,
        relative_date: Some(-7), // 7 days in the future (negative = future)
        time: None,
        time_format: None,
    };
    
    let result = app.parse_date(&cli_args);
    assert!(result.is_ok());
    
    let date = result.unwrap();
    let expected_date = Local::now().date_naive() + chrono::Duration::days(7);
    assert_eq!(date, expected_date);
}

#[test]
#[serial]
fn test_add_note_intuitive_relative_dates() {
    // Test the intuitive relative date behavior
    let mut config = Config::new();
    let vault_config = VaultConfig::test_config("test", "/tmp/test-vault");
    config.add_vault(vault_config);
    
    let (app, _cleanup) = create_app_with_config(config);
    
    // Test positive values (past dates)
    let cli_args = CliArgs {
        vault: None,
        date: None,
        relative_date: Some(1), // 1 day ago (yesterday)
        time: None,
        time_format: None,
    };
    
    let result = app.parse_date(&cli_args);
    assert!(result.is_ok());
    
    let date = result.unwrap();
    let expected_date = Local::now().date_naive() - chrono::Duration::days(1);
    assert_eq!(date, expected_date);
    
    // Test negative values (future dates)
    let cli_args = CliArgs {
        vault: None,
        date: None,
        relative_date: Some(-1), // 1 day in the future (tomorrow)
        time: None,
        time_format: None,
    };
    
    let result = app.parse_date(&cli_args);
    assert!(result.is_ok());
    
    let date = result.unwrap();
    let expected_date = Local::now().date_naive() + chrono::Duration::days(1);
    assert_eq!(date, expected_date);
}

#[test]
#[serial]
fn test_unlist_vault_success() {
    // Create a config with multiple vaults
    let mut config = Config::new();
    
    let vault1 = VaultConfig::test_config("vault1", "/tmp/vault1");
    
    let vault2 = VaultConfig::test_config("vault2", "/tmp/vault2");
    
    config.add_vault(vault1);
    config.add_vault(vault2);
    
    let (mut app, _cleanup) = create_app_with_config(config);
    
    // Verify both vaults exist
    assert!(app.get_config().vaults.contains_key("vault1"));
    assert!(app.get_config().vaults.contains_key("vault2"));
    
    // Unlist vault1
    let result = app.unlist_vault("vault1");
    assert!(result.is_ok());
    
    // Verify vault1 is unlisted but vault2 remains
    assert!(!app.get_config().vaults.contains_key("vault1"));
    assert!(app.get_config().vaults.contains_key("vault2"));
}

#[test]
#[serial]
fn test_unlist_vault_not_found() {
    // Create a config with one vault
    let mut config = Config::new();
    
    let vault = VaultConfig::test_config("vault1", "/tmp/vault1");
    
    config.add_vault(vault);
    
    let (mut app, _cleanup) = create_app_with_config(config);
    
    // Try to unlist non-existent vault
    let result = app.unlist_vault("non-existent");
    assert!(result.is_err());
    
    // Verify original vault still exists
    assert!(app.get_config().vaults.contains_key("vault1"));
}

#[test]
#[serial]
fn test_unlist_default_vault() {
    // Create a config with a default vault
    let mut config = Config::new();
    
    let vault1 = VaultConfig::test_config("vault1", "/tmp/vault1");
    
    let vault2 = VaultConfig::test_config("vault2", "/tmp/vault2");
    
    config.add_vault(vault1);
    config.add_vault(vault2);
    config.set_default_vault("vault1").unwrap();
    
    let (mut app, _cleanup) = create_app_with_config(config);
    
    // Verify default vault is set
    assert_eq!(app.get_config().default_vault, Some("vault1".to_string()));
    
    // Unlist the default vault
    let result = app.unlist_vault("vault1");
    assert!(result.is_ok());
    
    // Verify default vault is cleared
    assert!(app.get_config().default_vault.is_none());
    
    // Verify vault1 is unlisted but vault2 remains
    assert!(!app.get_config().vaults.contains_key("vault1"));
    assert!(app.get_config().vaults.contains_key("vault2"));
}