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
use super::common::{hyalo_no_hints, md, write_md};
use std::fs;
use tempfile::TempDir;

// ---------------------------------------------------------------------------
// Fixture
// ---------------------------------------------------------------------------

fn setup() -> TempDir {
    let tmp = TempDir::new().unwrap();

    write_md(
        tmp.path(),
        "note.md",
        md!(r"
---
title: Test Note
status: draft
tags:
  - rust
---
# Body

Some content here.

## Tasks

- [ ] First task
- [x] Second task
"),
    );

    write_md(
        tmp.path(),
        "other.md",
        md!(r"
---
title: Other
---
See [[note]] for details.
"),
    );

    tmp
}

// ---------------------------------------------------------------------------
// read: positional file
// ---------------------------------------------------------------------------

#[test]
fn read_positional_file() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["read", "note.md", "--format", "text"])
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Some content here."));
}

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

    let positional = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["read", "note.md"])
        .output()
        .unwrap();

    let flag = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["read", "--file", "note.md"])
        .output()
        .unwrap();

    assert!(positional.status.success());
    assert!(flag.status.success());
    assert_eq!(positional.stdout, flag.stdout);
}

// ---------------------------------------------------------------------------
// read: conflict — both positional and --file
// ---------------------------------------------------------------------------

#[test]
fn read_positional_and_flag_conflicts() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["read", "note.md", "--file", "note.md"])
        .output()
        .unwrap();

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used with"),
        "expected conflict error, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// read: no file at all → error
// ---------------------------------------------------------------------------

#[test]
fn read_no_file_at_all_errors() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["read"])
        .output()
        .unwrap();

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("required argument missing"),
        "expected missing file error, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// find: positional file + --file flag conflicts
// ---------------------------------------------------------------------------

#[test]
fn find_positional_file_and_flag_conflicts() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["find", "pattern", "note.md", "--file", "other.md"])
        .output()
        .unwrap();

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used with"),
        "expected conflict error, got: {stderr}"
    );
}

// ---------------------------------------------------------------------------
// backlinks: positional file
// ---------------------------------------------------------------------------

#[test]
fn backlinks_positional_file() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["backlinks", "note.md"])
        .output()
        .unwrap();

    assert!(output.status.success());
    let envelope: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    // other.md links to note via [[note]]
    assert_eq!(envelope["total"], 1);
}

// ---------------------------------------------------------------------------
// mv: positional file
// ---------------------------------------------------------------------------

#[test]
fn mv_positional_file() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["mv", "note.md", "--to", "archive/note.md"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let envelope: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(envelope["results"]["from"], "note.md");
    assert_eq!(envelope["results"]["to"], "archive/note.md");
    assert!(tmp.path().join("archive/note.md").exists());
    assert!(!tmp.path().join("note.md").exists());
}

// ---------------------------------------------------------------------------
// set: positional file(s)
// ---------------------------------------------------------------------------

#[test]
fn set_positional_file() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["set", "note.md", "--property", "priority=5"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = fs::read_to_string(tmp.path().join("note.md")).unwrap();
    assert!(content.contains("priority: 5"));
}

// ---------------------------------------------------------------------------
// remove: positional file(s)
// ---------------------------------------------------------------------------

#[test]
fn remove_positional_file() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["remove", "note.md", "--property", "status"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = fs::read_to_string(tmp.path().join("note.md")).unwrap();
    assert!(!content.contains("status:"));
}

// ---------------------------------------------------------------------------
// append: positional file(s)
// ---------------------------------------------------------------------------

#[test]
fn append_positional_file() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["append", "note.md", "--property", "tags=cli"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = fs::read_to_string(tmp.path().join("note.md")).unwrap();
    assert!(content.contains("cli"));
    assert!(content.contains("rust"));
}

// ---------------------------------------------------------------------------
// find: positional pattern + positional file
// ---------------------------------------------------------------------------

#[test]
fn find_positional_pattern_and_file() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["find", "content", "note.md"])
        .output()
        .unwrap();

    assert!(output.status.success());
    let envelope: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(envelope["total"], 1);
    assert_eq!(envelope["results"][0]["file"], "note.md");
}

#[test]
fn find_positional_file_only_no_pattern() {
    // When only one positional is given to find, it's treated as PATTERN, not file.
    // To pass a file without a pattern, --file is required.
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["find", "--file", "note.md"])
        .output()
        .unwrap();

    assert!(output.status.success());
    let envelope: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(envelope["total"], 1);
}

// ---------------------------------------------------------------------------
// task read: positional file
// ---------------------------------------------------------------------------

#[test]
fn task_read_positional_file() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["task", "read", "note.md", "--all"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let envelope: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    let results = envelope["results"].as_array().unwrap();
    assert_eq!(results.len(), 2);
}

// ---------------------------------------------------------------------------
// task toggle: positional file
// ---------------------------------------------------------------------------

#[test]
fn task_toggle_positional_file() {
    let tmp = setup();
    // Toggle line 13 (- [ ] First task)
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["task", "toggle", "note.md", "--line", "13"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = fs::read_to_string(tmp.path().join("note.md")).unwrap();
    assert!(content.contains("- [x] First task"));
}

// ---------------------------------------------------------------------------
// task set: positional file
// ---------------------------------------------------------------------------

#[test]
fn task_set_status_positional_file() {
    let tmp = setup();
    // Set line 13 (- [ ] First task) to status '?'
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["task", "set", "note.md", "--line", "13", "--status", "?"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = fs::read_to_string(tmp.path().join("note.md")).unwrap();
    assert!(content.contains("- [?] First task"));
}

// ---------------------------------------------------------------------------
// find: two positionals — first is pattern, second is file
// ---------------------------------------------------------------------------

#[test]
fn find_two_positionals_first_is_pattern() {
    let tmp = setup();
    // "note.md" is passed as the first positional (PATTERN) and "other.md" as the
    // second (FILE). Verify that "other.md" is treated as a file filter (scopes
    // the search to other.md only), not as a second pattern.
    // With BM25, "note" (stemmed from "note.md") does appear in other.md's body
    // ("See [[note]] for details."), so the search returns a result for other.md.
    // The key invariant: only other.md is searched (the FILE positional scopes it).
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["find", "xyz_notpresent_anywhere", "other.md"])
        .output()
        .unwrap();

    assert!(output.status.success());
    let envelope: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    // "xyz_notpresent_anywhere" does not appear in other.md — so total should be 0
    assert_eq!(
        envelope["total"], 0,
        "pattern not found in scoped file: {envelope}"
    );

    // Also verify that the FILE positional actually scopes to other.md:
    // search for something that IS in other.md's body ("note") to confirm scoping.
    let output2 = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["find", "note", "other.md"])
        .output()
        .unwrap();
    assert!(output2.status.success());
    let envelope2: serde_json::Value = serde_json::from_slice(&output2.stdout).unwrap();
    // "note" appears in other.md ("See [[note]] for details."), so total=1
    assert_eq!(
        envelope2["total"], 1,
        "note found in other.md (file-scoped search): {envelope2}"
    );
    // And only other.md is in the results
    let results = envelope2["results"].as_array().unwrap();
    assert!(results[0]["file"].as_str().unwrap().ends_with("other.md"));
}

// ---------------------------------------------------------------------------
// set: multiple positional files
// ---------------------------------------------------------------------------

#[test]
fn set_multiple_positional_files() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["set", "note.md", "other.md", "--property", "reviewed=true"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let note = fs::read_to_string(tmp.path().join("note.md")).unwrap();
    let other = fs::read_to_string(tmp.path().join("other.md")).unwrap();
    assert!(
        note.contains("reviewed: true"),
        "note.md not updated: {note}"
    );
    assert!(
        other.contains("reviewed: true"),
        "other.md not updated: {other}"
    );
}

// ---------------------------------------------------------------------------
// set: positional file conflicts with --file flag
// ---------------------------------------------------------------------------

#[test]
fn set_positional_conflicts_with_file_flag() {
    let tmp = setup();
    let output = hyalo_no_hints()
        .args(["--dir", tmp.path().to_str().unwrap()])
        .args(["set", "note.md", "--file", "other.md", "--property", "k=v"])
        .output()
        .unwrap();

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used with"),
        "expected conflict error, got: {stderr}"
    );
}