dbmd-cli 0.2.4

The `dbmd` command-line tool for db.md — the open database in plain files. A thin wrapper over dbmd-core: validate, search, query, graph, write, index, and log over a db.md store. Zero AI dependencies.
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
//! Integration tests for `dbmd fm` — frontmatter get / set / query / init.
//!
//! Intent-derived from plan Block 5: `get` reads one value; `set` mutates one
//! value atomically and re-sorts the type-folder index write-through, refusing a
//! `DB.md` frozen page; `query` is the sidecar-backed dedup primitive; `init`
//! detects type, seeds timestamps, composes a default `summary`, and folds the
//! file into its index. Read paths run against the committed corpus-a; write
//! paths run against a temp copy so the committed corpora are never mutated. The
//! frozen-page refusal is checked against both corpora's `## Policies`.

mod common;

use std::collections::BTreeSet;

use common::{copy_store_to_temp, corpus_a, corpus_b, dbmd};

// ── fm get ─────────────────────────────────────────────────────────────────────

#[test]
fn get_reads_a_universal_field_from_db_md() {
    // `dbmd fm get DB.md scope` — the SPEC's store-identity example.
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    let out = dbmd()
        .current_dir(&store)
        .args(["fm", "get", "DB.md", "scope"])
        .assert()
        .success();
    let v = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    assert_eq!(v.trim(), "company");
}

#[test]
fn get_reads_a_type_specific_field() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    let out = dbmd()
        .current_dir(&store)
        .args(["fm", "get", "records/contacts/sarah-chen.md", "role"])
        .assert()
        .success();
    let v = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    assert_eq!(v.trim(), "Director of Operations");
}

#[test]
fn get_json_carries_typed_value() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    let out = dbmd()
        .current_dir(&store)
        .args([
            "--json",
            "fm",
            "get",
            "records/contacts/sarah-chen.md",
            "tags",
        ])
        .assert()
        .success();
    let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(v["key"], serde_json::json!("tags"));
    // `tags` is a YAML list → a JSON array, not a stringified scalar.
    assert_eq!(v["value"], serde_json::json!(["customer", "renewal"]));
}

#[test]
fn get_missing_key_is_a_runtime_error() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    dbmd()
        .current_dir(&store)
        .args(["fm", "get", "records/contacts/sarah-chen.md", "no-such-key"])
        .assert()
        .failure()
        .code(1);
}

// ── fm set ─────────────────────────────────────────────────────────────────────

#[test]
fn set_updates_value_and_keeps_index_in_sync() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());

    dbmd()
        .current_dir(&store)
        .args([
            "fm",
            "set",
            "records/contacts/marcus-okafor.md",
            "status=archived",
        ])
        .assert()
        .success();

    // The file's frontmatter now carries the new value.
    let file = std::fs::read_to_string(store.join("records/contacts/marcus-okafor.md")).unwrap();
    assert!(file.contains("status: archived"), "value written:\n{file}");

    // Write-through: the type-folder sidecar reflects the change (no rebuild).
    let jsonl = std::fs::read_to_string(store.join("records/contacts/index.jsonl")).unwrap();
    let marcus_line = jsonl
        .lines()
        .find(|l| l.contains("marcus-okafor"))
        .expect("marcus still indexed");
    assert!(
        marcus_line.contains("\"status\":\"archived\""),
        "index.jsonl updated write-through; got: {marcus_line}"
    );
}

#[test]
fn set_resorts_index_when_recency_changes() {
    // Bumping `updated` to the newest instant must move the entry to the FRONT
    // of the type-folder index.md (recency-ordered), proving the write-through
    // re-sort, not just an in-place field edit.
    let (_tmp, store) = copy_store_to_temp(&corpus_a());

    dbmd()
        .current_dir(&store)
        .args([
            "fm",
            "set",
            "records/contacts/david-kim.md",
            "updated=2099-01-01T00:00:00Z",
        ])
        .assert()
        .success();

    let md = std::fs::read_to_string(store.join("records/contacts/index.md")).unwrap();
    let first_entry_line = md
        .lines()
        .find(|l| l.starts_with("- [["))
        .expect("index.md has entries");
    assert!(
        first_entry_line.contains("records/contacts/david-kim"),
        "the freshly-bumped record sorts to the top; first entry was: {first_entry_line}"
    );
}

#[test]
fn set_refuses_a_frozen_page_corpus_a() {
    // corpus-a freezes wiki/synthesis/2026-renewal-plan.md.
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    let frozen = "wiki/synthesis/2026-renewal-plan.md";
    let before = std::fs::read_to_string(store.join(frozen)).unwrap();

    let out = dbmd()
        .current_dir(&store)
        .args(["--json", "fm", "set", frozen, "status=draft"])
        .assert()
        .failure()
        .code(4);

    // Structured POLICY_FROZEN_PAGE error on stderr (JSON mode).
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    let v: serde_json::Value = serde_json::from_str(stderr.trim()).expect("json error on stderr");
    assert_eq!(v["error"]["code"], serde_json::json!("POLICY_FROZEN_PAGE"));

    // No write occurred — the frozen file is byte-for-byte unchanged.
    let after = std::fs::read_to_string(store.join(frozen)).unwrap();
    assert_eq!(
        before, after,
        "a frozen-page refusal must not mutate the file"
    );
}

#[test]
fn set_refuses_a_frozen_page_corpus_b() {
    // corpus-b freezes records/decisions/2026-q1-strategy.md (the policy-refusal
    // EXPECTED fixture). Refusal is exit 4 with the same code.
    let (_tmp, store) = copy_store_to_temp(&corpus_b());
    let frozen = "records/decisions/2026-q1-strategy.md";
    let before = std::fs::read_to_string(store.join(frozen)).unwrap();

    dbmd()
        .current_dir(&store)
        .args(["fm", "set", frozen, "status=draft"])
        .assert()
        .failure()
        .code(4);

    let after = std::fs::read_to_string(store.join(frozen)).unwrap();
    assert_eq!(before, after);
}

#[test]
fn set_refuses_a_frozen_page_passed_as_absolute_path() {
    // Regression: `fm set` always opens the store at the CWD (`store.root` is
    // the literal `.`), so an ABSOLUTE `<file>` only matches the relative frozen
    // entry if the path resolution canonicalizes both sides. Before the fix this
    // exited 0 and mutated the frozen file (it added `status: draft`).
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    let frozen = "wiki/synthesis/2026-renewal-plan.md";
    let abs = store.join(frozen);
    let before = std::fs::read_to_string(&abs).unwrap();

    dbmd()
        .current_dir(&store)
        .args(["fm", "set", abs.to_str().unwrap(), "status=draft"])
        .assert()
        .failure()
        .code(4);

    let after = std::fs::read_to_string(&abs).unwrap();
    assert_eq!(
        before, after,
        "an absolute-path frozen refusal must not mutate the file"
    );
}

#[test]
fn set_refuses_an_extensionless_frozen_entry() {
    // Regression for the divergent-frozen-policy finding: `fm set` historically
    // used a byte-exact `PathBuf` equality against `config.frozen_pages`, which
    // is `.md`-SENSITIVE. An operator who froze a page with the natural
    // extensionless spelling (`records/decisions/q1`) was silently NOT
    // protected on this surface — `fm set records/decisions/q1.md ...` compared
    // `records/decisions/q1.md != records/decisions/q1` and mutated the frozen
    // file. `fm set` now funnels through the single canonical
    // `Config::frozen_match` (`.md`-insensitive), so the refusal must hold.
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let store = tmp.path();
    common::write_file(
        store,
        "DB.md",
        "---\ntype: db-md\nscope: company\nowner: T\n---\n\n# S\n\n\
         ## Policies\n\n### Frozen pages\n- records/decisions/q1\n",
    );
    let frozen = "records/decisions/q1.md";
    common::write_file(
        store,
        frozen,
        "---\ntype: decision\nsummary: a finalized decision\n---\n# Q1\n",
    );
    let before = std::fs::read_to_string(store.join(frozen)).unwrap();

    let out = dbmd()
        .current_dir(store)
        .args(["--json", "fm", "set", frozen, "status=draft"])
        .assert()
        .failure()
        .code(4);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    let v: serde_json::Value = serde_json::from_str(stderr.trim()).expect("json error on stderr");
    assert_eq!(v["error"]["code"], serde_json::json!("POLICY_FROZEN_PAGE"));

    let after = std::fs::read_to_string(store.join(frozen)).unwrap();
    assert_eq!(
        before, after,
        "an extensionless frozen entry must still block `fm set` (no mutation)"
    );
}

#[test]
fn set_rejects_a_non_assignment_argument() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    dbmd()
        .current_dir(&store)
        .args([
            "fm",
            "set",
            "records/contacts/sarah-chen.md",
            "no-equals-sign",
        ])
        .assert()
        .failure()
        .code(1);
}

// ── fm query ───────────────────────────────────────────────────────────────────

/// Run `dbmd fm query <args> --dir corpus_a` and return stdout path lines.
fn query_paths(args: &[&str]) -> BTreeSet<String> {
    let out = dbmd()
        .arg("fm")
        .arg("query")
        .args(args)
        .arg("--dir")
        .arg(corpus_a())
        .assert()
        .success();
    let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    stdout.lines().map(str::to_string).collect()
}

#[test]
fn query_by_typed_field_with_type_scope() {
    let got = query_paths(&["name=Sarah Chen", "--type", "contact"]);
    assert!(got.contains("records/contacts/sarah-chen.md"));
    assert_eq!(got.len(), 1, "exactly the one matching contact: {got:?}");
}

#[test]
fn query_returns_full_records_in_json() {
    let out = dbmd()
        .args([
            "fm",
            "query",
            "email=sarah.chen@northstar.io",
            "--type",
            "contact",
        ])
        .arg("--json")
        .arg("--dir")
        .arg(corpus_a())
        .assert()
        .success();
    let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let arr = v.as_array().expect("array of records");
    assert_eq!(arr.len(), 1);
    // Full record fields come straight from the sidecar.
    assert_eq!(arr[0]["type"], serde_json::json!("contact"));
    assert_eq!(arr[0]["name"], serde_json::json!("Sarah Chen"));
    assert_eq!(
        arr[0]["path"],
        serde_json::json!("records/contacts/sarah-chen.md")
    );
}

#[test]
fn query_limit_caps_results() {
    // tags=vendor spans many expenses; --limit 2 caps the path list.
    let capped = query_paths(&["tags=vendor", "--type", "expense", "--limit", "2"]);
    assert_eq!(capped.len(), 2, "limit caps the result set: {capped:?}");
}

#[test]
fn query_rejects_a_non_assignment() {
    dbmd()
        .args(["fm", "query", "no-equals"])
        .arg("--dir")
        .arg(corpus_a())
        .assert()
        .failure()
        .code(1);
}

// ── fm init ────────────────────────────────────────────────────────────────────

#[test]
fn init_infers_type_seeds_timestamps_and_composes_summary() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    // An externally-dropped contact: no type, no summary, no timestamps.
    common::write_file(
        &store,
        "records/contacts/tom-vega.md",
        "---\nname: Tom Vega\nrole: VP Finance\ncompany: \"[[records/companies/northstar]]\"\n---\n\n# Tom Vega\n\nNew finance contact.\n",
    );

    dbmd()
        .current_dir(&store)
        .args(["fm", "init", "records/contacts/tom-vega.md"])
        .assert()
        .success();

    let file = std::fs::read_to_string(store.join("records/contacts/tom-vega.md")).unwrap();
    // Type inferred from the records/contacts/ folder.
    assert!(file.contains("type: contact"), "type inferred:\n{file}");
    // Timestamps seeded.
    assert!(file.contains("created:"), "created seeded:\n{file}");
    assert!(file.contains("updated:"), "updated seeded:\n{file}");
    // Deterministic default summary composed (resolves the company link to its
    // name); the contact composer is `<role> at <company-name>`.
    assert!(
        file.contains("summary: VP Finance at Northstar Logistics"),
        "default summary composed + company resolved:\n{file}"
    );

    // Folded into the type-folder index write-through (both artifacts).
    let jsonl = std::fs::read_to_string(store.join("records/contacts/index.jsonl")).unwrap();
    assert!(jsonl.contains("tom-vega"), "new file indexed in jsonl");
    let md = std::fs::read_to_string(store.join("records/contacts/index.md")).unwrap();
    assert!(
        md.contains("records/contacts/tom-vega"),
        "new file in index.md"
    );
}

#[test]
fn init_honors_explicit_summary_override() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    common::write_file(
        &store,
        "records/contacts/nina-ray.md",
        "---\nname: Nina Ray\nrole: Analyst\n---\n\n# Nina Ray\n",
    );

    dbmd()
        .current_dir(&store)
        .args([
            "fm",
            "init",
            "records/contacts/nina-ray.md",
            "--summary",
            "Hand-written override summary",
        ])
        .assert()
        .success();

    let file = std::fs::read_to_string(store.join("records/contacts/nina-ray.md")).unwrap();
    assert!(
        file.contains("summary: Hand-written override summary"),
        "explicit --summary wins over the composed default:\n{file}"
    );
}

#[test]
fn init_errors_when_type_cannot_be_inferred() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    // A 2-component path under records/ with no usable type-folder shape and no
    // `type` field — init can't classify it.
    common::write_file(&store, "records/loose.md", "---\nfoo: bar\n---\n\nbody\n");

    dbmd()
        .current_dir(&store)
        .args(["fm", "init", "records/loose.md"])
        .assert()
        .failure()
        .code(1);
}

#[test]
fn init_refuses_a_frozen_page() {
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    let frozen = "wiki/synthesis/2026-renewal-plan.md";
    let before = std::fs::read_to_string(store.join(frozen)).unwrap();

    dbmd()
        .current_dir(&store)
        .args(["fm", "init", frozen])
        .assert()
        .failure()
        .code(4);

    let after = std::fs::read_to_string(store.join(frozen)).unwrap();
    assert_eq!(before, after, "init must not touch a frozen page");
}

#[test]
fn init_refuses_a_frozen_page_passed_as_absolute_path() {
    // Regression: same absolute-path bypass as `fm set`. `fm init` opens the
    // store at the CWD, so an ABSOLUTE `<file>` must still canonicalize to the
    // store-relative key the frozen entry uses. Before the fix this exited 0 and
    // rewrote the frozen file's frontmatter.
    let (_tmp, store) = copy_store_to_temp(&corpus_a());
    let frozen = "wiki/synthesis/2026-renewal-plan.md";
    let abs = store.join(frozen);
    let before = std::fs::read_to_string(&abs).unwrap();

    dbmd()
        .current_dir(&store)
        .args(["fm", "init", abs.to_str().unwrap()])
        .assert()
        .failure()
        .code(4);

    let after = std::fs::read_to_string(&abs).unwrap();
    assert_eq!(
        before, after,
        "init must not touch a frozen page addressed by absolute path"
    );
}

#[test]
fn init_refuses_an_extensionless_frozen_entry() {
    // Regression for the divergent-frozen-policy finding (the `fm init` arm):
    // like `fm set`, `fm init` used a byte-exact `.md`-SENSITIVE `PathBuf`
    // equality, so a page frozen with the natural extensionless spelling
    // (`records/decisions/q1`) was silently NOT protected — `fm init
    // records/decisions/q1.md` exited 0 and rewrote the frozen file's
    // frontmatter. Both `fm` write arms now funnel through the single canonical
    // `Config::frozen_match` (`.md`-insensitive), so the refusal must hold.
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let store = tmp.path();
    common::write_file(
        store,
        "DB.md",
        "---\ntype: db-md\nscope: company\nowner: T\n---\n\n# S\n\n\
         ## Policies\n\n### Frozen pages\n- records/decisions/q1\n",
    );
    let frozen = "records/decisions/q1.md";
    common::write_file(
        store,
        frozen,
        "---\ntype: decision\nsummary: a finalized decision\n---\n# Q1\n",
    );
    let before = std::fs::read_to_string(store.join(frozen)).unwrap();

    let out = dbmd()
        .current_dir(store)
        .args(["--json", "fm", "init", frozen])
        .assert()
        .failure()
        .code(4);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    let v: serde_json::Value = serde_json::from_str(stderr.trim()).expect("json error on stderr");
    assert_eq!(v["error"]["code"], serde_json::json!("POLICY_FROZEN_PAGE"));

    let after = std::fs::read_to_string(store.join(frozen)).unwrap();
    assert_eq!(
        before, after,
        "an extensionless frozen entry must still block `fm init` (no mutation)"
    );
}