kimun-notes 0.3.7

A terminal-based notes application
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
// tui/tests/note_commands_test.rs
//
// Integration tests for note create/append/journal CLI commands.

use kimun_notes::cli::{run_cli, CliCommand};
use kimun_notes::cli::commands::{NoteSubcommand, JournalArgs};
use kimun_notes::cli::commands::journal::JournalSubcommand;
use tempfile::TempDir;

/// Helper: write a minimal Phase 2 config and initialise the vault index.
/// The `note` CLI command only calls `validate()` (not `validate_and_init()`) for speed,
/// so tests must pre-initialise the vault themselves.
async fn write_config(config_path: &std::path::Path, workspace_dir: &std::path::Path) {
    let content = format!(
        r#"config_version = 2
[global]
current_workspace = "default"
theme = "Nord"

[workspaces.default]
path = "{}"
last_paths = []
created = "2026-01-01T00:00:00Z"
"#,
        workspace_dir.display()
    );
    std::fs::write(config_path, content).unwrap();
    let vault = kimun_core::NoteVault::new(workspace_dir).await.unwrap();
    vault.validate_and_init().await.unwrap();
}

// --- note create ---

#[tokio::test]
async fn test_note_create_creates_new_note() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Create {
                path: "my-note".to_string(),
                content: Some("# My Note\n\nHello".to_string()),
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "note create should succeed: {:?}", result);

    let note_file = workspace_dir.path().join("my-note.md");
    assert!(note_file.exists(), "note file should exist at {:?}", note_file);
    let content = std::fs::read_to_string(&note_file).unwrap();
    assert!(content.contains("Hello"), "note should contain the provided content");
}

#[tokio::test]
async fn test_note_create_fails_if_note_exists() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    // Pre-create the note
    std::fs::write(workspace_dir.path().join("existing.md"), "# Existing").unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Create {
                path: "existing".to_string(),
                content: Some("new content".to_string()),
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_err(), "note create should fail when note already exists");
    let err = format!("{:?}", result.unwrap_err());
    assert!(err.contains("already exists"), "error should mention 'already exists': {}", err);
}

#[tokio::test]
async fn test_note_create_uses_quick_note_path() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();

    let content = format!(
        r#"config_version = 2
[global]
current_workspace = "default"
theme = "Nord"

[workspaces.default]
path = "{}"
last_paths = []
created = "2026-01-01T00:00:00Z"
quick_note_path = "/inbox"
"#,
        workspace_dir.path().display()
    );
    std::fs::write(&config_path, content).unwrap();
    kimun_core::NoteVault::new(workspace_dir.path()).await.unwrap()
        .validate_and_init().await.unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Create {
                path: "idea".to_string(),
                content: Some("an idea".to_string()),
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "note create should succeed: {:?}", result);
    let note_file = workspace_dir.path().join("inbox").join("idea.md");
    assert!(note_file.exists(), "note should be at {:?}", note_file);
}

#[tokio::test]
async fn test_note_create_absolute_path_ignores_quick_note_path() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();

    let content = format!(
        r#"config_version = 2
[global]
current_workspace = "default"
theme = "Nord"

[workspaces.default]
path = "{}"
last_paths = []
created = "2026-01-01T00:00:00Z"
quick_note_path = "/inbox"
"#,
        workspace_dir.path().display()
    );
    std::fs::write(&config_path, content).unwrap();
    kimun_core::NoteVault::new(workspace_dir.path()).await.unwrap()
        .validate_and_init().await.unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Create {
                path: "/projects/plan".to_string(),
                content: Some("a plan".to_string()),
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "note create should succeed: {:?}", result);
    let note_file = workspace_dir.path().join("projects").join("plan.md");
    assert!(note_file.exists(), "note should be at {:?}", note_file);
}

// --- note append ---

#[tokio::test]
async fn test_note_append_creates_if_not_exists() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Append {
                path: "new-note".to_string(),
                content: Some("first line".to_string()),
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "note append should succeed: {:?}", result);
    let note_file = workspace_dir.path().join("new-note.md");
    assert!(note_file.exists(), "note should be created");
    let content = std::fs::read_to_string(&note_file).unwrap();
    assert!(content.contains("first line"));
}

#[tokio::test]
async fn test_note_append_appends_to_existing() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    std::fs::write(workspace_dir.path().join("log.md"), "# Log\n\nFirst entry").unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Append {
                path: "log".to_string(),
                content: Some("Second entry".to_string()),
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "note append should succeed: {:?}", result);
    let content = std::fs::read_to_string(workspace_dir.path().join("log.md")).unwrap();
    assert!(content.contains("First entry"), "original content preserved");
    assert!(content.contains("Second entry"), "new content appended");
}

#[tokio::test]
async fn test_note_append_empty_content_is_noop() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    std::fs::write(workspace_dir.path().join("original.md"), "# Original").unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Append {
                path: "original".to_string(),
                content: Some("".to_string()),
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok());
    let content = std::fs::read_to_string(workspace_dir.path().join("original.md")).unwrap();
    assert_eq!(content, "# Original", "content should be unchanged on empty append");
}

// --- journal ---

#[tokio::test]
async fn test_journal_creates_todays_entry() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    let result = run_cli(
        CliCommand::Journal(JournalArgs {
            date: None,
            content: Some("Today's thought".to_string()),
            subcommand: None,
        }),
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "journal should succeed: {:?}", result);

    let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
    let journal_file = workspace_dir.path()
        .join("journal")
        .join(format!("{}.md", today));
    assert!(journal_file.exists(), "journal entry should exist at {:?}", journal_file);
    let content = std::fs::read_to_string(&journal_file).unwrap();
    assert!(content.contains("Today's thought"));
}

#[tokio::test]
async fn test_journal_appends_to_existing_entry() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
    let journal_dir = workspace_dir.path().join("journal");
    std::fs::create_dir_all(&journal_dir).unwrap();
    std::fs::write(
        journal_dir.join(format!("{}.md", today)),
        format!("# {}\n\nFirst entry", today),
    ).unwrap();

    run_cli(
        CliCommand::Journal(JournalArgs {
            date: None,
            content: Some("Second entry".to_string()),
            subcommand: None,
        }),
        Some(config_path),
    )
    .await
    .unwrap();

    let content = std::fs::read_to_string(journal_dir.join(format!("{}.md", today))).unwrap();
    assert!(content.contains("First entry"), "original content preserved");
    assert!(content.contains("Second entry"), "new content appended");
}

#[tokio::test]
async fn test_journal_empty_content_is_noop() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
    let journal_dir = workspace_dir.path().join("journal");
    std::fs::create_dir_all(&journal_dir).unwrap();
    let journal_file = journal_dir.join(format!("{}.md", today));
    std::fs::write(&journal_file, format!("# {}", today)).unwrap();

    run_cli(
        CliCommand::Journal(JournalArgs {
            date: None,
            content: Some("".to_string()),
            subcommand: None,
        }),
        Some(config_path),
    )
    .await
    .unwrap();

    let content = std::fs::read_to_string(&journal_file).unwrap();
    assert_eq!(content, format!("# {}", today), "content should be unchanged on empty journal");
}

#[tokio::test]
async fn test_journal_date_creates_specific_entry() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    run_cli(
        CliCommand::Journal(JournalArgs {
            date: Some("2024-01-15".to_string()),
            content: Some("Backdated entry".to_string()),
            subcommand: None,
        }),
        Some(config_path),
    )
    .await
    .unwrap();

    let journal_file = workspace_dir.path().join("journal").join("2024-01-15.md");
    assert!(journal_file.exists(), "specific-date journal entry should exist");
    let content = std::fs::read_to_string(&journal_file).unwrap();
    assert!(content.contains("Backdated entry"));
}

#[tokio::test]
async fn test_journal_invalid_date_returns_error() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    let result = run_cli(
        CliCommand::Journal(JournalArgs {
            date: Some("not-a-date".to_string()),
            content: Some("content".to_string()),
            subcommand: None,
        }),
        Some(config_path),
    )
    .await;

    assert!(result.is_err(), "invalid date should return an error");
    let msg = format!("{:?}", result.unwrap_err());
    assert!(msg.contains("Invalid date"), "error should mention invalid date, got: {}", msg);
}

#[tokio::test]
async fn test_journal_show_missing_entry_returns_error() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    let result = run_cli(
        CliCommand::Journal(JournalArgs {
            date: None,
            content: None,
            subcommand: Some(JournalSubcommand::Show {
                date: Some("1999-01-01".to_string()),
                format: kimun_notes::cli::output::OutputFormat::Text,
            }),
        }),
        Some(config_path),
    )
    .await;

    assert!(result.is_err(), "show on missing entry should return an error");
    let msg = format!("{:?}", result.unwrap_err());
    assert!(msg.contains("No journal entry found"), "got: {}", msg);
}

// --- note show ---

#[tokio::test]
async fn test_note_show_text_returns_ok() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    std::fs::write(
        workspace_dir.path().join("my-note.md"),
        "# My Note\n\nHello world",
    ).unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Show {
                paths: vec!["my-note".to_string()],
                format: kimun_notes::cli::output::OutputFormat::Text,
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "note show should succeed: {:?}", result);
}

#[tokio::test]
async fn test_note_show_missing_note_fails() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    // Pre-create an unrelated note so the vault initializes cleanly;
    // the test target ("does-not-exist") is intentionally absent.
    std::fs::write(workspace_dir.path().join("unrelated.md"), "# Unrelated").unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Show {
                paths: vec!["does-not-exist".to_string()],
                format: kimun_notes::cli::output::OutputFormat::Text,
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_err(), "note show on missing note should fail");
}

#[tokio::test]
async fn test_note_show_json_returns_ok() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    std::fs::write(
        workspace_dir.path().join("json-note.md"),
        "# JSON Note\n\nsome content",
    ).unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Show {
                paths: vec!["json-note".to_string()],
                format: kimun_notes::cli::output::OutputFormat::Json,
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "note show --format json should succeed: {:?}", result);
}

#[tokio::test]
async fn test_note_show_multiple_notes_ok() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    std::fs::write(workspace_dir.path().join("note-a.md"), "# Note A").unwrap();
    std::fs::write(workspace_dir.path().join("note-b.md"), "# Note B").unwrap();

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Show {
                paths: vec!["note-a".to_string(), "note-b".to_string()],
                format: kimun_notes::cli::output::OutputFormat::Text,
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_ok(), "note show with multiple notes should succeed: {:?}", result);
}

#[tokio::test]
async fn test_note_show_format_paths_returns_error() {
    use kimun_notes::cli::output::OutputFormat;
    use kimun_core::nfs::VaultPath;
    let dir = TempDir::new().unwrap();
    let vault = kimun_core::NoteVault::new(dir.path()).await.unwrap();
    vault.validate_and_init().await.unwrap();
    vault
        .create_note(
            &VaultPath::note_path_from("test/note"),
            "# Test\n\nContent.",
        )
        .await
        .unwrap();

    let config_path = dir.path().join("config.toml");
    write_config(&config_path, dir.path()).await;

    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Show {
                paths: vec!["test/note".to_string()],
                format: OutputFormat::Paths,
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_err());
    let msg = result.unwrap_err().to_string();
    assert!(
        msg.contains("--format paths is not valid for note show"),
        "got: {}",
        msg
    );
}

#[tokio::test]
async fn test_note_show_partial_failure_returns_err() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.toml");
    let workspace_dir = TempDir::new().unwrap();
    write_config(&config_path, workspace_dir.path()).await;

    std::fs::write(workspace_dir.path().join("exists.md"), "# Exists").unwrap();

    // One valid, one missing — should return Err (partial failure)
    let result = run_cli(
        CliCommand::Note {
            subcommand: NoteSubcommand::Show {
                paths: vec!["exists".to_string(), "missing".to_string()],
                format: kimun_notes::cli::output::OutputFormat::Text,
            },
        },
        Some(config_path),
    )
    .await;

    assert!(result.is_err(), "partial failure should return Err");
}