hyalo-cli 0.14.0

CLI for exploring and managing Markdown knowledge bases with YAML frontmatter
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
use std::fs;

use super::common::{hyalo, hyalo_no_hints, write_md};
use serde_json::Value;
use tempfile::TempDir;

/// Set a "drafts" view and return the temp dir.
fn setup_with_view() -> TempDir {
    let tmp = setup();
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "set", "drafts", "--property", "status=draft"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "views set failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    tmp
}

fn setup() -> TempDir {
    let tmp = tempfile::tempdir().unwrap();
    write_md(
        tmp.path(),
        "note1.md",
        "---\nstatus: draft\ntags:\n  - project\n---\nHello world",
    );
    write_md(
        tmp.path(),
        "note2.md",
        "---\nstatus: completed\ntags:\n  - research\n---\nGoodbye world",
    );
    tmp
}

#[test]
fn views_set_and_list() {
    let tmp = setup();
    // Set a view
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "set", "drafts", "--property", "status=draft"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "set failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // List views
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "list"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "list failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["total"], 1);
    assert_eq!(json["results"][0]["name"], "drafts");
}

#[test]
fn views_remove() {
    let tmp = setup();
    // Set then remove
    hyalo()
        .current_dir(tmp.path())
        .args(["views", "set", "drafts", "--property", "status=draft"])
        .output()
        .unwrap();

    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "remove", "drafts"])
        .output()
        .unwrap();
    assert!(output.status.success());

    // List should be empty
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "list"])
        .output()
        .unwrap();
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["total"], 0);
}

#[test]
fn find_with_view() {
    let tmp = setup();
    // Set a view
    hyalo()
        .current_dir(tmp.path())
        .args(["views", "set", "drafts", "--property", "status=draft"])
        .output()
        .unwrap();

    // Use the view with find
    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["find", "--view", "drafts"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "find --view failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["total"], 1);
    assert!(
        json["results"][0]["file"]
            .as_str()
            .unwrap()
            .contains("note1")
    );
}

#[test]
fn find_with_view_and_overrides() {
    let tmp = setup();
    // Create more files
    write_md(
        tmp.path(),
        "note3.md",
        "---\nstatus: draft\ntags:\n  - project\n---\nAnother draft",
    );

    // Set a view with property filter
    hyalo()
        .current_dir(tmp.path())
        .args(["views", "set", "drafts", "--property", "status=draft"])
        .output()
        .unwrap();

    // Use view with limit override
    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["find", "--view", "drafts", "--limit", "1"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["results"].as_array().unwrap().len(), 1);
}

#[test]
fn find_with_unknown_view_errors() {
    let tmp = setup();
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["find", "--view", "nonexistent"])
        .output()
        .unwrap();
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("unknown view"), "stderr: {stderr}");
}

#[test]
fn views_remove_nonexistent_errors() {
    let tmp = setup();
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "remove", "nonexistent"])
        .output()
        .unwrap();
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("not found"), "stderr: {stderr}");
}

#[test]
fn views_set_empty_filters_errors() {
    let tmp = setup();
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "set", "empty"])
        .output()
        .unwrap();
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("no filters"), "stderr: {stderr}");
}

#[test]
fn views_set_preserves_existing_config() {
    let tmp = setup();
    // Write a .hyalo.toml with dir set
    fs::write(tmp.path().join(".hyalo.toml"), "dir = \".\"\n").unwrap();

    // Set a view
    hyalo()
        .current_dir(tmp.path())
        .args(["views", "set", "drafts", "--property", "status=draft"])
        .output()
        .unwrap();

    // Verify dir is preserved
    let content = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(content.contains("dir = \".\""), "dir was lost: {content}");
    assert!(
        content.contains("[views.drafts]"),
        "view not written: {content}"
    );
}

#[test]
fn hint_suggests_saving_non_trivial_query_as_view() {
    let tmp = setup();
    // Two filters = non-trivial → should suggest saving as a view
    let output = hyalo()
        .current_dir(tmp.path())
        .args([
            "find",
            "--property",
            "status=draft",
            "--tag",
            "project",
            "--hints",
        ])
        .output()
        .unwrap();
    assert!(output.status.success());
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    let hints = json["hints"].as_array().unwrap();
    let has_view_hint = hints
        .iter()
        .any(|h| h["cmd"].as_str().unwrap_or("").contains("views set"));
    assert!(
        has_view_hint,
        "expected 'views set' hint for non-trivial query, got: {hints:?}"
    );
}

#[test]
fn hint_does_not_suggest_view_for_single_filter() {
    let tmp = setup();
    // Single filter = trivial → should NOT suggest saving as a view
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["find", "--property", "status=draft", "--hints"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    let hints = json["hints"].as_array().unwrap();
    let has_view_hint = hints
        .iter()
        .any(|h| h["cmd"].as_str().unwrap_or("").contains("views set"));
    assert!(
        !has_view_hint,
        "should not suggest view for single-filter query, got: {hints:?}"
    );
}

#[test]
fn views_no_subcommand_defaults_to_list() {
    let tmp = setup_with_view();

    let list_output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["views", "list"])
        .output()
        .unwrap();
    let default_output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["views"])
        .output()
        .unwrap();

    assert!(list_output.status.success());
    assert!(default_output.status.success());
    assert_eq!(
        list_output.stdout, default_output.stdout,
        "`hyalo views` should produce same output as `hyalo views list`"
    );
}

#[test]
fn views_list_format_text() {
    let tmp = setup_with_view();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["views", "list", "--format", "text"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let text = String::from_utf8_lossy(&output.stdout);
    // Text output should not start with '{' (JSON object)
    assert!(
        !text.trim_start().starts_with('{'),
        "expected text output, got JSON-like: {text}"
    );
    // Should contain the view name
    assert!(
        text.contains("drafts"),
        "expected 'drafts' in text output: {text}"
    );
}

#[test]
fn views_set_format_text() {
    let tmp = setup();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args([
            "views",
            "set",
            "my-view",
            "--property",
            "status=draft",
            "--format",
            "text",
        ])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let text = String::from_utf8_lossy(&output.stdout);
    // Text output should not start with '{'
    assert!(
        !text.trim_start().starts_with('{'),
        "expected text output, got JSON-like: {text}"
    );
    // Should mention the action and view name
    assert!(
        text.contains("set") && text.contains("my-view"),
        "expected action and name in text output: {text}"
    );
}

#[test]
fn views_remove_format_text() {
    let tmp = setup_with_view();

    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["views", "remove", "drafts", "--format", "text"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let text = String::from_utf8_lossy(&output.stdout);
    assert!(
        !text.trim_start().starts_with('{'),
        "expected text output, got JSON-like: {text}"
    );
    assert!(
        text.contains("removed") && text.contains("drafts"),
        "expected action and name in text output: {text}"
    );
}

#[test]
fn views_list_jq_total() {
    let tmp = setup_with_view();

    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "list", "--jq", ".total"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let text = String::from_utf8_lossy(&output.stdout).trim().to_owned();
    assert_eq!(text, "1", "expected total=1, got: {text}");
}

#[test]
fn views_list_jq_results() {
    let tmp = setup_with_view();

    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "list", "--jq", ".results[0].name"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let text = String::from_utf8_lossy(&output.stdout).trim().to_owned();
    assert_eq!(text, "drafts", "expected view name, got: {text}");
}

#[test]
fn views_list_count() {
    let tmp = setup_with_view();

    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "list", "--count"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let text = String::from_utf8_lossy(&output.stdout).trim().to_owned();
    assert_eq!(text, "1", "expected count=1, got: {text}");
}

#[test]
fn hint_does_not_suggest_view_when_using_view() {
    let tmp = setup();
    // Create a view with two filters
    let set_output = hyalo()
        .current_dir(tmp.path())
        .args([
            "views",
            "set",
            "drafts",
            "--property",
            "status=draft",
            "--tag",
            "project",
        ])
        .output()
        .unwrap();
    assert!(
        set_output.status.success(),
        "views set failed: {}",
        String::from_utf8_lossy(&set_output.stderr)
    );

    // Use it — should NOT suggest saving again
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["find", "--view", "drafts", "--hints"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    let hints = json["hints"].as_array().unwrap();
    let has_view_hint = hints
        .iter()
        .any(|h| h["cmd"].as_str().unwrap_or("").contains("views set"));
    assert!(
        !has_view_hint,
        "should not suggest view when already using --view, got: {hints:?}"
    );
}

#[test]
fn views_set_with_bm25_pattern_and_find() {
    let tmp = tempfile::tempdir().unwrap();

    // File that contains the search term and matching tag
    write_md(
        tmp.path(),
        "relevant.md",
        "---\nstatus: active\ntags:\n  - sometag\n---\nThis document discusses fermentation in detail.",
    );
    // File with matching tag but no search term in body
    write_md(
        tmp.path(),
        "other.md",
        "---\nstatus: active\ntags:\n  - sometag\n---\nThis document is about something else entirely.",
    );
    // File with the search term but wrong tag
    write_md(
        tmp.path(),
        "untagged.md",
        "---\nstatus: active\ntags:\n  - othertag\n---\nThis document discusses fermentation as well.",
    );

    // Set a view with a BM25 pattern and tag filter
    let output = hyalo()
        .current_dir(tmp.path())
        .args([
            "views",
            "set",
            "test-pattern",
            "fermentation",
            "--tag",
            "sometag",
        ])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "views set with pattern failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify the view appears in views list with the pattern stored in filters
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "list"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "views list failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["total"], 1);
    let view = &json["results"][0];
    assert_eq!(view["name"], "test-pattern");
    assert_eq!(
        view["filters"]["pattern"], "fermentation",
        "pattern should be stored in view filters"
    );
    assert!(
        view["filters"]["tag"]
            .as_array()
            .unwrap()
            .contains(&Value::String("sometag".to_owned())),
        "tag filter should be stored in view"
    );

    // Use the view with find — should return only the file with the term AND the tag
    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["find", "--view", "test-pattern"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "find --view test-pattern failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    let results = json["results"].as_array().unwrap();
    assert_eq!(results.len(), 1, "expected exactly one result: {results:?}");
    assert!(
        results[0]["file"].as_str().unwrap().contains("relevant"),
        "expected relevant.md in results: {results:?}"
    );
}

/// Regression test: when `.hyalo.toml` in the project root has `dir = "subdir"`,
/// views must be read from the root config — not from `subdir/.hyalo.toml`.
#[test]
fn views_work_when_dir_is_a_subdir() {
    let tmp = tempfile::tempdir().unwrap();
    let subdir = tmp.path().join("notes");
    fs::create_dir_all(&subdir).unwrap();
    write_md(&subdir, "a.md", "---\nstatus: draft\n---\nhello");

    // Write root .hyalo.toml with dir pointing to subdir
    fs::write(tmp.path().join(".hyalo.toml"), "dir = \"notes\"\n").unwrap();

    // Set a view (should write to root .hyalo.toml)
    let output = hyalo()
        .current_dir(tmp.path())
        .args(["views", "set", "drafts", "--property", "status=draft"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "views set failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Root config should have the view; subdir should NOT have a .hyalo.toml
    let root_toml = fs::read_to_string(tmp.path().join(".hyalo.toml")).unwrap();
    assert!(
        root_toml.contains("[views.drafts]"),
        "view should be in root .hyalo.toml: {root_toml}"
    );
    assert!(
        !subdir.join(".hyalo.toml").exists(),
        "subdir should not have its own .hyalo.toml"
    );

    // List views — should find the view
    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["views", "list"])
        .output()
        .unwrap();
    assert!(output.status.success());
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["total"], 1, "expected 1 view: {json}");

    // find --view should work
    let output = hyalo_no_hints()
        .current_dir(tmp.path())
        .args(["find", "--view", "drafts"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "find --view failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    let results = json["results"].as_array().unwrap();
    assert_eq!(results.len(), 1, "expected 1 result: {results:?}");
}