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
488
489
490
491
492
use journey::date_time::DateTimeHandler;
use chrono::{NaiveDate, NaiveTime, Local};

#[test]
fn test_date_handler_creation() {
    let handler = DateTimeHandler::new("en-US".to_string());
    assert_eq!(handler.locale, "en-US");
}

// ============================================================================
// US LOCALE TESTS
// ============================================================================

#[test]
fn test_us_locale_date_parsing() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test various US date formats
    let test_cases = [
        ("2025-10-24", NaiveDate::from_ymd_opt(2025, 10, 24).unwrap()),
        ("10/24/2025", NaiveDate::from_ymd_opt(2025, 10, 24).unwrap()),
        ("10-24-2025", NaiveDate::from_ymd_opt(2025, 10, 24).unwrap()),
    ];
    
    for (date_str, expected) in test_cases {
        let result = handler.parse_date(date_str).unwrap();
        assert_eq!(result, expected, "Failed to parse US date: {}", date_str);
    }
}

#[test]
fn test_us_locale_time_parsing_24h() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test 24-hour format
    let test_cases = [
        ("14:30", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("14:30:45", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
        ("09:15", NaiveTime::from_hms_opt(9, 15, 0).unwrap()),
        ("23:59", NaiveTime::from_hms_opt(23, 59, 0).unwrap()),
    ];
    
    for (time_str, expected) in test_cases {
        let result = handler.parse_time(time_str).unwrap();
        assert_eq!(result, expected, "Failed to parse US 24h time: {}", time_str);
    }
}

#[test]
fn test_us_locale_time_parsing_12h() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test 12-hour format
    let test_cases = [
        ("2:30 PM", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("2:30:45 PM", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
        ("9:15 AM", NaiveTime::from_hms_opt(9, 15, 0).unwrap()),
        ("11:59 PM", NaiveTime::from_hms_opt(23, 59, 0).unwrap()),
        ("12:00 PM", NaiveTime::from_hms_opt(12, 0, 0).unwrap()),
        ("12:00 AM", NaiveTime::from_hms_opt(0, 0, 0).unwrap()),
        ("2:30PM", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("2:30:45PM", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
    ];
    
    for (time_str, expected) in test_cases {
        let result = handler.parse_time(time_str).unwrap();
        assert_eq!(result, expected, "Failed to parse US 12h time: {}", time_str);
    }
}

#[test]
fn test_us_locale_invalid_time() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    let invalid_times = ["25:00", "13:00 PM", "invalid", "25:30", "14:70"];
    
    for time_str in invalid_times {
        let result = handler.parse_time(time_str);
        assert!(result.is_err(), "Should fail to parse invalid US time: {}", time_str);
    }
}

// ============================================================================
// NORWEGIAN LOCALE TESTS
// ============================================================================

#[test]
fn test_norwegian_locale_date_parsing() {
    let handler = DateTimeHandler::new("no-NO".to_string());
    
    // Test various Norwegian date formats
    let test_cases = [
        ("2025-10-24", NaiveDate::from_ymd_opt(2025, 10, 24).unwrap()),
        ("24.10.2025", NaiveDate::from_ymd_opt(2025, 10, 24).unwrap()),
        ("24/10/2025", NaiveDate::from_ymd_opt(2025, 10, 24).unwrap()),
        ("24-10-2025", NaiveDate::from_ymd_opt(2025, 10, 24).unwrap()),
    ];
    
    for (date_str, expected) in test_cases {
        let result = handler.parse_date(date_str).unwrap();
        assert_eq!(result, expected, "Failed to parse Norwegian date: {}", date_str);
    }
}

#[test]
fn test_norwegian_locale_time_parsing_24h() {
    let handler = DateTimeHandler::new("no-NO".to_string());
    
    // Test 24-hour format (same as US)
    let test_cases = [
        ("14:30", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("14:30:45", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
        ("09:15", NaiveTime::from_hms_opt(9, 15, 0).unwrap()),
        ("23:59", NaiveTime::from_hms_opt(23, 59, 0).unwrap()),
    ];
    
    for (time_str, expected) in test_cases {
        let result = handler.parse_time(time_str).unwrap();
        assert_eq!(result, expected, "Failed to parse Norwegian 24h time: {}", time_str);
    }
}

#[test]
fn test_norwegian_locale_time_parsing_12h() {
    let handler = DateTimeHandler::new("no-NO".to_string());
    
    // Test 12-hour format (using English AM/PM)
    let test_cases = [
        ("2:30 PM", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("2:30:45 PM", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
        ("9:15 AM", NaiveTime::from_hms_opt(9, 15, 0).unwrap()),
        ("11:59 PM", NaiveTime::from_hms_opt(23, 59, 0).unwrap()),
        ("2:30PM", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("2:30:45PM", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
    ];
    
    for (time_str, expected) in test_cases {
        let result = handler.parse_time(time_str).unwrap();
        assert_eq!(result, expected, "Failed to parse Norwegian 12h time: {}", time_str);
    }
}

#[test]
fn test_norwegian_locale_invalid_time() {
    let handler = DateTimeHandler::new("no-NO".to_string());
    
    let invalid_times = ["25:00", "13:00 PM", "invalid", "25:30", "14:70"];
    
    for time_str in invalid_times {
        let result = handler.parse_time(time_str);
        assert!(result.is_err(), "Should fail to parse invalid Norwegian time: {}", time_str);
    }
}

// ============================================================================
// CROSS-LOCALE COMPATIBILITY TESTS
// ============================================================================

#[test]
fn test_iso_date_cross_locale() {
    let us_handler = DateTimeHandler::new("en-US".to_string());
    let no_handler = DateTimeHandler::new("no-NO".to_string());
    
    let iso_date = "2025-10-24";
    let expected = NaiveDate::from_ymd_opt(2025, 10, 24).unwrap();
    
    let us_result = us_handler.parse_date(iso_date).unwrap();
    let no_result = no_handler.parse_date(iso_date).unwrap();
    
    assert_eq!(us_result, expected);
    assert_eq!(no_result, expected);
    assert_eq!(us_result, no_result);
}

#[test]
fn test_24h_time_cross_locale() {
    let us_handler = DateTimeHandler::new("en-US".to_string());
    let no_handler = DateTimeHandler::new("no-NO".to_string());
    
    let time_24h = "14:30:45";
    let expected = NaiveTime::from_hms_opt(14, 30, 45).unwrap();
    
    let us_result = us_handler.parse_time(time_24h).unwrap();
    let no_result = no_handler.parse_time(time_24h).unwrap();
    
    assert_eq!(us_result, expected);
    assert_eq!(no_result, expected);
    assert_eq!(us_result, no_result);
}

#[test]
fn test_12h_time_cross_locale() {
    let us_handler = DateTimeHandler::new("en-US".to_string());
    let no_handler = DateTimeHandler::new("no-NO".to_string());
    
    let time_12h = "2:30:45 PM";
    let expected = NaiveTime::from_hms_opt(14, 30, 45).unwrap();
    
    let us_result = us_handler.parse_time(time_12h).unwrap();
    let no_result = no_handler.parse_time(time_12h).unwrap();
    
    assert_eq!(us_result, expected);
    assert_eq!(no_result, expected);
    assert_eq!(us_result, no_result);
}

// ============================================================================
// LOCALE-SPECIFIC DATE FORMAT TESTS
// ============================================================================

#[test]
fn test_us_specific_date_formats() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test US-specific formats that should work
    let us_formats = [
        "10/24/2025",  // MM/DD/YYYY
        "10-24-2025",  // MM-DD-YYYY
    ];
    
    for date_str in us_formats {
        let result = handler.parse_date(date_str);
        assert!(result.is_ok(), "US handler should parse US format: {}", date_str);
        assert_eq!(result.unwrap(), NaiveDate::from_ymd_opt(2025, 10, 24).unwrap());
    }
}

#[test]
fn test_norwegian_specific_date_formats() {
    let handler = DateTimeHandler::new("no-NO".to_string());
    
    // Test Norwegian-specific formats that should work
    let no_formats = [
        "24.10.2025",  // DD.MM.YYYY
        "24/10/2025",  // DD/MM/YYYY
        "24-10-2025",  // DD-MM-YYYY
    ];
    
    for date_str in no_formats {
        let result = handler.parse_date(date_str);
        assert!(result.is_ok(), "Norwegian handler should parse Norwegian format: {}", date_str);
        assert_eq!(result.unwrap(), NaiveDate::from_ymd_opt(2025, 10, 24).unwrap());
    }
}

#[test]
fn test_cross_locale_date_ambiguity() {
    let us_handler = DateTimeHandler::new("en-US".to_string());
    let no_handler = DateTimeHandler::new("no-NO".to_string());
    
    // Test ambiguous date that means different things in different locales
    let ambiguous_date = "01/02/2025"; // Could be Jan 2 or Feb 1
    
    let us_result = us_handler.parse_date(ambiguous_date).unwrap();
    let no_result = no_handler.parse_date(ambiguous_date).unwrap();
    
    // US should interpret as MM/DD/YYYY (Jan 2)
    assert_eq!(us_result, NaiveDate::from_ymd_opt(2025, 1, 2).unwrap());
    // Norwegian should interpret as DD/MM/YYYY (Feb 1)
    assert_eq!(no_result, NaiveDate::from_ymd_opt(2025, 2, 1).unwrap());
    
    // They should be different
    assert_ne!(us_result, no_result);
}

// ============================================================================
// LEGACY TESTS (keeping for backward compatibility)
// ============================================================================

#[test]
fn test_parse_date_invalid_format() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    let result = handler.parse_date("invalid-date");
    assert!(result.is_err());
}

#[test]
fn test_parse_time_invalid_format() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    let result = handler.parse_time("invalid-time");
    assert!(result.is_err());
}

#[test]
fn test_parse_relative_date() {
    let handler = DateTimeHandler::new("en-US".to_string());
    let today = Local::now().date_naive();
    
    // Test 0 days (today)
    let date = handler.parse_relative_date(0);
    assert_eq!(date, today);
    
    // Test positive values (past dates) - intuitive numbering
    let date = handler.parse_relative_date(1);
    let yesterday = today - chrono::Duration::days(1);
    assert_eq!(date, yesterday);
    
    let date = handler.parse_relative_date(7);
    let week_ago = today - chrono::Duration::days(7);
    assert_eq!(date, week_ago);
    
    // Test negative values (future dates) - intuitive numbering
    let date = handler.parse_relative_date(-1);
    let tomorrow = today + chrono::Duration::days(1);
    assert_eq!(date, tomorrow);
    
    let date = handler.parse_relative_date(-7);
    let week_from_now = today + chrono::Duration::days(7);
    assert_eq!(date, week_from_now);
}

#[test]
fn test_format_date() {
    let handler = DateTimeHandler::new("en-US".to_string());
    let date = NaiveDate::from_ymd_opt(2025, 10, 24).unwrap();
    let formatted = handler.format_date(date);
    assert_eq!(formatted, "2025-10-24");
}

#[test]
fn test_format_datetime() {
    let handler = DateTimeHandler::new("en-US".to_string());
    let datetime = Local::now();
    let formatted = handler.format_datetime(datetime);
    // Should only contain time (HH:MM:SS format)
    assert!(formatted.contains(":"));
    // Should not contain year (no date)
    assert!(!formatted.contains("2025"));
    // Should be in HH:MM:SS format
    assert!(formatted.matches(':').count() == 2);
}

#[test]
fn test_combine_date_time() {
    let handler = DateTimeHandler::new("en-US".to_string());
    let date = NaiveDate::from_ymd_opt(2025, 10, 24).unwrap();
    let time = NaiveTime::from_hms_opt(14, 30, 0).unwrap();
    
    let combined = handler.combine_date_time(date, time);
    assert_eq!(combined.date_naive(), date);
    assert_eq!(combined.time(), time);
}

// ============================================================================
// TIME FORMAT OVERRIDE TESTS
// ============================================================================

#[test]
fn test_time_format_override_12h() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test 12h format override with valid 12h times
    let test_cases = [
        ("2:30 PM", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("2:30:45 PM", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
        ("2:30PM", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("2:30:45PM", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
    ];
    
    for (time_str, expected) in test_cases {
        let result = handler.parse_time_with_format_override(time_str, Some("12h"));
        assert!(result.is_ok(), "Failed to parse {} with 12h override", time_str);
        assert_eq!(result.unwrap(), expected);
    }
}

#[test]
fn test_time_format_override_24h() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test 24h format override with valid 24h times
    let test_cases = [
        ("14:30", NaiveTime::from_hms_opt(14, 30, 0).unwrap()),
        ("14:30:45", NaiveTime::from_hms_opt(14, 30, 45).unwrap()),
    ];
    
    for (time_str, expected) in test_cases {
        let result = handler.parse_time_with_format_override(time_str, Some("24h"));
        assert!(result.is_ok(), "Failed to parse {} with 24h override", time_str);
        assert_eq!(result.unwrap(), expected);
    }
}

#[test]
fn test_time_format_override_invalid_format() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test invalid format override
    let result = handler.parse_time_with_format_override("14:30", Some("invalid"));
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Invalid time format override"));
}

#[test]
fn test_time_format_override_mismatch() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test 12h time with 24h format override (should fail)
    let result = handler.parse_time_with_format_override("2:30 PM", Some("24h"));
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Could not parse time"));
    
    // Test 24h time with 12h format override (should fail)
    let result = handler.parse_time_with_format_override("14:30", Some("12h"));
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Could not parse time"));
}

#[test]
fn test_time_format_override_none() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test that None format override falls back to locale-based parsing
    let result = handler.parse_time_with_format_override("14:30", None);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), NaiveTime::from_hms_opt(14, 30, 0).unwrap());
    
    let result = handler.parse_time_with_format_override("2:30 PM", None);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), NaiveTime::from_hms_opt(14, 30, 0).unwrap());
}

// ============================================================================
// DATE FORMAT OVERRIDE TESTS
// ============================================================================

#[test]
fn test_date_format_override_iso() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test ISO format override
    let result = handler.parse_date_with_format_override("2025-10-24", Some("YYYY-MM-DD"));
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), NaiveDate::from_ymd_opt(2025, 10, 24).unwrap());
}

#[test]
fn test_date_format_override_us() {
    let handler = DateTimeHandler::new("no-NO".to_string());
    
    // Test US format override (should work even with Norwegian locale)
    let result = handler.parse_date_with_format_override("10/24/2025", Some("MM/DD/YYYY"));
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), NaiveDate::from_ymd_opt(2025, 10, 24).unwrap());
}

#[test]
fn test_date_format_override_european() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test European format override (should work even with US locale)
    let result = handler.parse_date_with_format_override("24.10.2025", Some("DD.MM.YYYY"));
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), NaiveDate::from_ymd_opt(2025, 10, 24).unwrap());
}

#[test]
fn test_date_format_override_custom_chrono() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test custom chrono format string
    let result = handler.parse_date_with_format_override("2025/10/24", Some("%Y/%m/%d"));
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), NaiveDate::from_ymd_opt(2025, 10, 24).unwrap());
}

#[test]
fn test_date_format_override_mismatch() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test format mismatch (should fail)
    let result = handler.parse_date_with_format_override("10/24/2025", Some("DD.MM.YYYY"));
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Could not parse date"));
}

#[test]
fn test_date_format_override_none() {
    let handler = DateTimeHandler::new("en-US".to_string());
    
    // Test that None format override falls back to locale-based parsing
    let result = handler.parse_date_with_format_override("10/24/2025", None);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), NaiveDate::from_ymd_opt(2025, 10, 24).unwrap());
    
    // Test with European date and US locale (should still work with fallback)
    let result = handler.parse_date_with_format_override("2025-10-24", None);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), NaiveDate::from_ymd_opt(2025, 10, 24).unwrap());
}