kimun-notes 0.9.0

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
// tui/tests/json_integration_test.rs
//
// Integration tests for JSON output in search and notes commands.
// These tests verify that --format json produces valid, well-structured JSON.

use kimun_core::nfs::VaultPath;
use kimun_core::{NoteVault, VaultConfig};
use kimun_notes::cli::output::OutputFormat;
use kimun_notes::cli::{CliCommand, run_cli};
use tempfile::TempDir;

/// Create a temporary vault with test notes indexed.
async fn setup_json_test_vault(dir: &TempDir) -> NoteVault {
    let vault = NoteVault::new(VaultConfig::new(dir.path()))
        .await
        .expect("failed to create vault");
    vault
        .validate_and_init()
        .await
        .expect("failed to init vault");

    vault
        .create_note(
            &VaultPath::note_path_from("rust-intro"),
            "# Introduction to Rust\n\n#programming #rust\n\nRust is a systems programming language.\n\n[[memory-safety]] [[ownership]]",
        )
        .await
        .expect("failed to create rust note");

    vault
        .create_note(
            &VaultPath::note_path_from("python-basics"),
            "# Python Basics\n\n#programming #python\n\nPython is great for scripting.",
        )
        .await
        .expect("failed to create python note");

    vault
        .create_note(
            &VaultPath::note_path_from("notes/deep-dive"),
            "# Deep Dive\n\n## Overview\n\nA nested note for testing path filtering.",
        )
        .await
        .expect("failed to create deep dive note");

    vault
        .recreate_index()
        .await
        .expect("failed to recreate index");
    vault
}

/// Write a minimal config file pointing workspace at the given path.
fn write_config(config_path: &std::path::Path, workspace: &std::path::Path) {
    let toml = format!(
        "workspace_dir = {:?}\n",
        workspace.to_string_lossy().as_ref()
    );
    std::fs::write(config_path, toml).expect("failed to write config file");
}

// ---------------------------------------------------------------------------
// test_search_json_output_is_valid
// ---------------------------------------------------------------------------

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

    setup_json_test_vault(&workspace_dir).await;
    write_config(&config_path, workspace_dir.path());

    // Capture stdout by using the vault directly
    let vault = NoteVault::new(VaultConfig::new(workspace_dir.path()))
        .await
        .unwrap();
    vault.validate_and_init().await.unwrap();

    let results = vault.search_notes("rust").await.unwrap();

    let json_str = kimun_notes::cli::json_output::format_notes_as_json(
        &vault,
        &results,
        "default",
        Some("rust"),
        false, // is_listing
    )
    .await
    .expect("format_notes_as_json should succeed");

    let json: serde_json::Value =
        serde_json::from_str(&json_str).expect("output should be valid JSON");

    // Verify top-level structure
    assert!(json["metadata"].is_object(), "should have metadata object");
    assert!(json["notes"].is_array(), "should have notes array");

    // Verify metadata fields
    assert_eq!(json["metadata"]["workspace"], "default");
    assert_eq!(
        json["metadata"]["workspace_path"],
        workspace_dir.path().to_string_lossy().to_string()
    );
    assert_eq!(json["metadata"]["query"], "rust");
    assert_eq!(json["metadata"]["is_listing"], false);
    assert!(json["metadata"]["total_results"].as_u64().unwrap() >= 1);
    assert!(json["metadata"]["generated_at"].is_string());

    // Verify note structure
    let note = &json["notes"][0];
    assert!(note["path"].is_string(), "note should have path");
    assert!(note["title"].is_string(), "note should have title");
    assert!(note["size"].is_number(), "note should have size");
    assert!(note["modified"].is_number(), "note should have modified");
    assert!(note["hash"].is_string(), "note should have hash");

    // Verify nested metadata structure
    assert!(
        note["metadata"].is_object(),
        "note should have metadata object"
    );
    assert!(
        note["metadata"]["tags"].is_array(),
        "metadata should have tags"
    );
    assert!(
        note["metadata"]["links"].is_array(),
        "metadata should have links"
    );
    assert!(
        note["metadata"]["headers"].is_array(),
        "metadata should have headers"
    );
}

// ---------------------------------------------------------------------------
// test_notes_json_output_is_valid
// ---------------------------------------------------------------------------

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

    setup_json_test_vault(&workspace_dir).await;
    write_config(&config_path, workspace_dir.path());

    let vault = NoteVault::new(VaultConfig::new(workspace_dir.path()))
        .await
        .unwrap();
    vault.validate_and_init().await.unwrap();

    let results = vault.get_all_notes().await.unwrap();
    let json_str = kimun_notes::cli::json_output::format_notes_as_json(
        &vault,
        &results,
        "my-workspace",
        None,
        true, // is_listing
    )
    .await
    .expect("format_notes_as_json should succeed");

    let json: serde_json::Value =
        serde_json::from_str(&json_str).expect("output should be valid JSON");

    assert_eq!(json["metadata"]["workspace"], "my-workspace");
    assert_eq!(json["metadata"]["is_listing"], true);
    assert!(
        json["metadata"]["query"].is_null(),
        "query should be null for listing"
    );
    assert!(json["notes"].as_array().unwrap().len() >= 3);

    // Each note should have a nested metadata object
    for note in json["notes"].as_array().unwrap() {
        assert!(
            note["metadata"].is_object(),
            "each note should have metadata: {:?}",
            note["path"]
        );
    }
}

// ---------------------------------------------------------------------------
// test_search_json_metadata_contains_tags_and_links
// ---------------------------------------------------------------------------

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

    setup_json_test_vault(&workspace_dir).await;
    write_config(&config_path, workspace_dir.path());

    let vault = NoteVault::new(VaultConfig::new(workspace_dir.path()))
        .await
        .unwrap();
    vault.validate_and_init().await.unwrap();

    let results = vault.search_notes("rust").await.unwrap();

    let json_str = kimun_notes::cli::json_output::format_notes_as_json(
        &vault,
        &results,
        "default",
        Some("rust"),
        false, // is_listing
    )
    .await
    .expect("format_notes_as_json should succeed");

    let json: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    let notes = json["notes"].as_array().unwrap();

    assert!(!notes.is_empty(), "should find the rust note");

    let rust_note = notes.iter().find(|n| {
        n["path"]
            .as_str()
            .map(|p| p.contains("rust"))
            .unwrap_or(false)
    });

    assert!(rust_note.is_some(), "should find rust-intro note");
    let note = rust_note.unwrap();

    let tags = note["metadata"]["tags"].as_array().unwrap();
    assert!(
        tags.iter()
            .any(|t| t.as_str() == Some("programming") || t.as_str() == Some("rust")),
        "should extract tags from content; found: {:?}",
        tags
    );

    let links = note["metadata"]["links"].as_array().unwrap();
    assert!(
        links.iter().any(|l| l
            .as_str()
            .map(|s| s.contains("memory-safety") || s.contains("ownership"))
            .unwrap_or(false)),
        "should extract wiki links from content; found: {:?}",
        links
    );
}

// ---------------------------------------------------------------------------
// test_notes_json_journal_date_field_present
// ---------------------------------------------------------------------------

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

    setup_json_test_vault(&workspace_dir).await;
    write_config(&config_path, workspace_dir.path());

    let vault = NoteVault::new(VaultConfig::new(workspace_dir.path()))
        .await
        .unwrap();
    vault.validate_and_init().await.unwrap();

    // Create a journal note
    let journal_path = VaultPath::note_path_from("journal/2024-01-15");
    vault
        .create_note(
            &journal_path,
            "# January 15, 2024\n\nToday's journal entry.",
        )
        .await
        .expect("failed to create journal note");

    vault.recreate_index().await.unwrap();

    let results = vault.get_all_notes().await.unwrap();

    let json_str = kimun_notes::cli::json_output::format_notes_as_json(
        &vault, &results, "default", None, true, // is_listing
    )
    .await
    .expect("format_notes_as_json should succeed");

    let json: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    let notes = json["notes"].as_array().unwrap();

    // Find the journal note
    let journal_note = notes.iter().find(|n| {
        n["path"]
            .as_str()
            .map(|p| p.contains("journal"))
            .unwrap_or(false)
    });

    assert!(journal_note.is_some(), "should find journal note");
    let note = journal_note.unwrap();

    // journal_date should be set for journal notes
    assert!(
        note["journal_date"].is_string(),
        "journal notes should have journal_date field set; got: {:?}",
        note["journal_date"]
    );
    assert_eq!(note["journal_date"], "2024-01-15");
}

// ---------------------------------------------------------------------------
// test_notes_json_created_field_present
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_notes_json_created_field_present() {
    let workspace_dir = TempDir::new().unwrap();

    setup_json_test_vault(&workspace_dir).await;

    let vault = NoteVault::new(VaultConfig::new(workspace_dir.path()))
        .await
        .unwrap();
    vault.validate_and_init().await.unwrap();

    let results = vault.get_all_notes().await.unwrap();

    let json_str = kimun_notes::cli::json_output::format_notes_as_json(
        &vault, &results, "default", None, true, // is_listing
    )
    .await
    .expect("format_notes_as_json should succeed");

    let json: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    let notes = json["notes"].as_array().unwrap();

    assert!(!notes.is_empty());
    for note in notes {
        assert!(
            note["created"].is_number(),
            "note should have created field; note: {:?}",
            note["path"]
        );
    }
}

// ---------------------------------------------------------------------------
// test_search_json_via_run_cli
// ---------------------------------------------------------------------------

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

    setup_json_test_vault(&workspace_dir).await;
    write_config(&config_path, workspace_dir.path());

    let result = run_cli(
        CliCommand::Search {
            query: "rust".to_string(),
            format: OutputFormat::Json,
        },
        Some(config_path),
    )
    .await;

    assert!(
        result.is_ok(),
        "search with JSON format should succeed: {:?}",
        result
    );
}

// ---------------------------------------------------------------------------
// test_notes_json_via_run_cli
// ---------------------------------------------------------------------------

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

    setup_json_test_vault(&workspace_dir).await;
    write_config(&config_path, workspace_dir.path());

    let result = run_cli(
        CliCommand::Notes {
            path: None,
            format: OutputFormat::Json,
        },
        Some(config_path),
    )
    .await;

    assert!(
        result.is_ok(),
        "notes with JSON format should succeed: {:?}",
        result
    );
}

// ---------------------------------------------------------------------------
// test_text_format_unaffected
// ---------------------------------------------------------------------------

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

    setup_json_test_vault(&workspace_dir).await;
    write_config(&config_path, workspace_dir.path());

    // Text format should still work for both commands
    let result = run_cli(
        CliCommand::Search {
            query: "python".to_string(),
            format: OutputFormat::Text,
        },
        Some(config_path.clone()),
    )
    .await;
    assert!(
        result.is_ok(),
        "search text format should still work: {:?}",
        result
    );

    let result = run_cli(
        CliCommand::Notes {
            path: None,
            format: OutputFormat::Text,
        },
        Some(config_path),
    )
    .await;
    assert!(
        result.is_ok(),
        "notes text format should still work: {:?}",
        result
    );
}