omgbase-store 13.4.4

omgbase store: the embedded SQLite database that owns block identity, history and current state โ€” Rust implementation
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! Macros (`spec/mutate/README.md` ยง5): each expands deterministically to
//! kernel ops the caller then applies (and sees). The expansions read the
//! store โ€” live hashes for the CAS tokens, the section runs, the `nodes`
//! rows for `node_set`, the candidate blocks for `links_repair`.

use std::collections::{BTreeMap, HashMap};
use std::sync::LazyLock;

use omgbase_format::hash::hex;
use omgbase_mutate::{At, ErrorCode, Expect, MutationError, Op, Parent, To};
use regex::Regex;
use rusqlite::{Connection, OptionalExtension, params};
use serde_json::{Map, Value, json};

use crate::Store;
use crate::error::{Error, Result};
use crate::links::{glob_clause, rewrite_link_destinations, split_destination};
use crate::read::blob_text;

fn raw_hash_of_block(conn: &Connection, block_id: &str) -> Result<Option<String>> {
    let h: Option<Vec<u8>> = conn
        .query_row(
            "SELECT raw_hash FROM blocks WHERE block_id = ?1 AND deleted_commit IS NULL",
            params![block_id],
            |r| r.get(0),
        )
        .optional()?;
    Ok(h.map(|h| hex(&h)))
}

fn raw_of_block(conn: &Connection, block_id: &str) -> Result<Option<String>> {
    let h: Option<Vec<u8>> = conn
        .query_row(
            "SELECT raw_hash FROM blocks WHERE block_id = ?1 AND deleted_commit IS NULL",
            params![block_id],
            |r| r.get(0),
        )
        .optional()?;
    h.map(|h| blob_text(conn, &h)).transpose()
}

fn update_markdown(block: &str, markdown: String, hash: Option<String>) -> Op {
    Op::Update {
        block: block.to_owned(),
        markdown: Some(markdown),
        attrs: None,
        expect: hash.map(Expect::content),
        trivia: None,
        child_ids: None,
    }
}

/// One rewritten block of a link repair (a dry-run preview).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RetargetHit {
    pub block: String,
    /// The block's document path.
    pub path: String,
    pub old_raw: String,
    pub new_raw: String,
}

/// A `from` โ†’ `to` destination rewrite.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LinkRepair {
    pub from: String,
    pub to: String,
}

/// A pair with how many destinations it matched.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LinkRepairCount {
    pub from: String,
    pub to: String,
    pub hits: usize,
}

/// What `links_repair` returns (ยง5).
#[derive(Clone, Debug, PartialEq)]
pub struct LinkRepairPlan {
    /// One `update` per rewritten top-most block.
    pub ops: Vec<Op>,
    pub hits: Vec<RetargetHit>,
    pub pairs: Vec<LinkRepairCount>,
}

impl LinkRepairPlan {
    /// `{ hits: [{ block, path, old_raw, new_raw }], pairs: [{ from, to, hits }] }`
    /// โ€” the extra the fixture records beside `ops`.
    #[must_use]
    pub fn extras_json(&self) -> Map<String, Value> {
        let mut m = Map::new();
        m.insert(
            "hits".to_owned(),
            Value::Array(
                self.hits
                    .iter()
                    .map(|h| json!({ "block": h.block, "path": h.path, "old_raw": h.old_raw, "new_raw": h.new_raw }))
                    .collect(),
            ),
        );
        m.insert(
            "pairs".to_owned(),
            Value::Array(
                self.pairs
                    .iter()
                    .map(|p| json!({ "from": p.from, "to": p.to, "hits": p.hits }))
                    .collect(),
            ),
        );
        m
    }
}

fn strip_slash(s: &str) -> &str {
    s.strip_prefix('/').unwrap_or(s)
}

/// Whether authored destination `dest` names the same target as `from`
/// (whole destination first, then the path part with its fragment riding
/// along); the fragment to re-append.
fn destination_match<'a>(dest: &'a str, from: &str) -> Option<&'a str> {
    if strip_slash(dest) == strip_slash(from) {
        return Some("");
    }
    let (path, fragment) = split_destination(dest);
    (!fragment.is_empty() && strip_slash(path) == strip_slash(from)).then_some(fragment)
}

static HEADING_LEVEL: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^(#{1,6})\s").expect("regex"));

/// The editable property names for a `(format, kind)`.
#[must_use]
pub fn editable_props_for(format: &str, kind: &str) -> Vec<&'static str> {
    if format != "markdown" {
        return Vec::new();
    }
    match kind {
        "md:link" => vec!["name", "value"],
        "md:task" => vec!["checked"],
        _ => Vec::new(),
    }
}

static LINK_TEXT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^\[[^\]]*\]").expect("regex"));
static LINK_TARGET: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"\]\(([^)\s]+)(\s+"[^"]*")?\)$"#).expect("regex"));

impl Store {
    /// ยง5 `tasks_complete`: one `update { attrs: { checked: true } }` per
    /// block, with the live CAS token when the block is known.
    pub fn tasks_complete(&self, blocks: &[String]) -> Result<Vec<Op>> {
        let mut attrs = Map::new();
        attrs.insert("checked".to_owned(), json!(true));
        blocks
            .iter()
            .map(|b| {
                Ok(Op::Update {
                    block: b.clone(),
                    markdown: None,
                    attrs: Some(attrs.clone()),
                    expect: raw_hash_of_block(&self.conn, b)?.map(Expect::content),
                    trivia: None,
                    child_ids: None,
                })
            })
            .collect()
    }

    /// ยง5 `sections_append`.
    #[must_use]
    pub fn sections_append(heading: &str, markdown: &str) -> Vec<Op> {
        vec![Op::Insert {
            doc: None,
            to: To {
                parent: Parent::Section {
                    heading: heading.to_owned(),
                },
                at: At::End,
            },
            markdown: markdown.to_owned(),
            expect: None,
        }]
    }

    /// ยง5 `docs_append`.
    #[must_use]
    pub fn docs_append(doc: &str, markdown: &str) -> Vec<Op> {
        vec![Op::Insert {
            doc: Some(doc.to_owned()),
            to: To {
                parent: Parent::Doc,
                at: At::End,
            },
            markdown: markdown.to_owned(),
            expect: None,
        }]
    }

    /// ยง5 `sections_rename`: the level from the live raw's leading `#` run.
    pub fn sections_rename(&self, heading: &str, title: &str) -> Result<Vec<Op>> {
        let raw = raw_of_block(&self.conn, heading)?.unwrap_or_default();
        let level = HEADING_LEVEL.captures(&raw).map_or(1, |c| c[1].len());
        let hash = raw_hash_of_block(&self.conn, heading)?;
        Ok(vec![update_markdown(
            heading,
            format!("{} {title}", "#".repeat(level)),
            hash,
        )])
    }

    /// ยง5 `sections_move`: the heading's section run (top-level ids from the
    /// heading to before the next heading of level โ‰ค its own); `[]` when the
    /// heading is unknown.
    pub fn sections_move(&self, heading: &str, to: &To) -> Result<Vec<Op>> {
        let row: Option<(String, Option<i64>)> = self
            .conn
            .query_row(
                "SELECT doc_id, json_extract(attrs, '$.level') FROM blocks WHERE block_id = ?1",
                params![heading],
                |r| Ok((r.get(0)?, r.get(1)?)),
            )
            .optional()?;
        let Some((doc_id, level)) = row else {
            return Ok(Vec::new());
        };
        let level = level.unwrap_or(1);
        let tops: Vec<(String, String, Option<i64>)> = {
            let mut stmt = self.conn.prepare(
                "SELECT block_id, type, json_extract(attrs, '$.level') FROM blocks
                 WHERE doc_id = ?1 AND parent_block IS NULL AND deleted_commit IS NULL ORDER BY ordinal",
            )?;
            let it = stmt.query_map(params![doc_id], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))?;
            it.collect::<std::result::Result<Vec<_>, _>>()?
        };
        // JavaScript `findIndex`/`slice` semantics, including a -1 start.
        let start_idx: i64 = tops
            .iter()
            .position(|t| t.0 == heading)
            .map_or(-1, |i| i as i64);
        let mut end = tops.len();
        for (i, t) in tops.iter().enumerate() {
            if (i as i64) <= start_idx {
                continue;
            }
            if t.1 == "heading" && t.2.unwrap_or(1) <= level {
                end = i;
                break;
            }
        }
        let start = if start_idx < 0 {
            (tops.len() as i64 + start_idx).max(0) as usize
        } else {
            start_idx as usize
        };
        let run: Vec<String> = if start < end {
            tops[start..end].iter().map(|t| t.0.clone()).collect()
        } else {
            Vec::new()
        };
        Ok(vec![Op::Move {
            blocks: run,
            to: to.clone(),
            expect: None,
        }])
    }

    /// ยง5 `lists_insert_item`.
    #[must_use]
    pub fn lists_insert_item(anchor: &str, at: At, markdown: &str) -> Vec<Op> {
        let item = if markdown.trim_start().starts_with("- ") {
            markdown.to_owned()
        } else {
            format!("- {markdown}")
        };
        vec![Op::Insert {
            doc: None,
            to: To {
                parent: Parent::Block(anchor.to_owned()),
                at,
            },
            markdown: item,
            expect: None,
        }]
    }

    /// ยง5 `node_set`: the adapter's editor for `(kind, prop)` over the node's
    /// block raw and byte span โ†’ one `update` with the CAS token.
    pub fn node_set(&self, node_id: &str, prop: &str, value: &str) -> Result<Vec<Op>> {
        type NodeRow = (
            String,
            Option<String>,
            Option<String>,
            String,
            Option<i64>,
            Option<i64>,
            Option<String>,
            String,
        );
        let row: Option<NodeRow> = self
            .conn
            .query_row(
                "SELECT n.kind, n.name, n.value, n.attrs, n.span_start, n.span_end, n.block_id, d.format
                 FROM nodes n JOIN docs d ON d.doc_id = n.doc_id WHERE n.node_id = ?1",
                params![node_id],
                |r| {
                    Ok((
                        r.get(0)?,
                        r.get(1)?,
                        r.get(2)?,
                        r.get(3)?,
                        r.get(4)?,
                        r.get(5)?,
                        r.get(6)?,
                        r.get(7)?,
                    ))
                },
            )
            .optional()?;
        let Some((kind, _name, _value, _attrs, span_start, span_end, block_id, format)) = row
        else {
            return Err(MutationError::new(
                ErrorCode::BlockMissing,
                format!("node {node_id} not found"),
            )
            .into());
        };
        let Some(block_id) = block_id else {
            return Err(MutationError::new(
                ErrorCode::NodeNotEditable,
                format!("node {node_id} is not anchored to a block"),
            )
            .into());
        };
        let editable = editable_props_for(&format, &kind);
        if !editable.contains(&prop) {
            return Err(MutationError::with_data(
                ErrorCode::NodeNotEditable,
                format!("no editor for {kind}.{prop}"),
                json!({ "kind": kind, "prop": prop, "editable": editable }),
            )
            .into());
        }
        let Some(block_raw) = raw_of_block(&self.conn, &block_id)? else {
            return Err(MutationError::new(
                ErrorCode::BlockMissing,
                format!("block {block_id} not found"),
            )
            .into());
        };
        let hash = raw_hash_of_block(&self.conn, &block_id)?;
        let span = match (span_start, span_end) {
            (Some(s), Some(e)) => {
                let s = usize::try_from(s).unwrap_or(0).min(block_raw.len());
                let e = usize::try_from(e).unwrap_or(0).clamp(s, block_raw.len());
                Some((s, e))
            }
            _ => None,
        };
        let seg = |(s, e): (usize, usize)| -> Result<&str> {
            block_raw.get(s..e).ok_or_else(|| {
                Error::Other(format!("node span {s}..{e} is not on a char boundary"))
            })
        };
        match (kind.as_str(), prop) {
            ("md:link", "name") => {
                let span = span
                    .ok_or_else(|| Error::Other("md:link.name requires a recorded span".into()))?;
                let seg = seg(span)?;
                let rebuilt = LINK_TEXT
                    .replace(seg, format!("[{value}]").as_str())
                    .into_owned();
                if rebuilt == seg {
                    return Err(Error::Other(format!(
                        "could not locate link text in {}",
                        serde_json::to_string(seg).unwrap_or_default()
                    )));
                }
                let md = format!("{}{rebuilt}{}", &block_raw[..span.0], &block_raw[span.1..]);
                Ok(vec![update_markdown(&block_id, md, hash)])
            }
            ("md:link", "value") => {
                let span = span
                    .ok_or_else(|| Error::Other("md:link.value requires a recorded span".into()))?;
                let seg = seg(span)?;
                let rebuilt = LINK_TARGET
                    .replace(seg, |caps: &regex::Captures<'_>| {
                        format!("]({value}{})", caps.get(2).map_or("", |m| m.as_str()))
                    })
                    .into_owned();
                if rebuilt == seg {
                    return Err(Error::Other(format!(
                        "could not locate link target in {}",
                        serde_json::to_string(seg).unwrap_or_default()
                    )));
                }
                let md = format!("{}{rebuilt}{}", &block_raw[..span.0], &block_raw[span.1..]);
                Ok(vec![update_markdown(&block_id, md, hash)])
            }
            ("md:task", "checked") => {
                let mut attrs = Map::new();
                attrs.insert("checked".to_owned(), json!(value == "true" || value == "1"));
                Ok(vec![Op::Update {
                    block: block_id,
                    markdown: None,
                    attrs: Some(attrs),
                    expect: hash.map(Expect::content),
                    trivia: None,
                    child_ids: None,
                }])
            }
            _ => unreachable!("editable props are enumerated above"),
        }
    }

    /// ยง5 `links_retarget`: the one-pair form of [`Store::links_repair`].
    pub fn links_retarget(
        &self,
        repo_id: &str,
        from: &str,
        to: &str,
        path_glob: Option<&str>,
    ) -> Result<LinkRepairPlan> {
        self.links_repair(
            repo_id,
            &[LinkRepair {
                from: from.to_owned(),
                to: to.to_owned(),
            }],
            path_glob,
        )
    }

    /// ยง5 `links_repair`: for every live non-`code_fence` block whose raw
    /// contains a slash-less `from`, rewrite each whole link destination that
    /// names it (first matching pair wins, fragment re-appended, never inside
    /// code spans); one `update` per changed top-most block.
    pub fn links_repair(
        &self,
        repo_id: &str,
        repairs: &[LinkRepair],
        path_glob: Option<&str>,
    ) -> Result<LinkRepairPlan> {
        let mut pairs: Vec<LinkRepairCount> = repairs
            .iter()
            .map(|r| LinkRepairCount {
                from: r.from.clone(),
                to: r.to.clone(),
                hits: 0,
            })
            .collect();
        let effective: Vec<usize> = pairs
            .iter()
            .enumerate()
            .filter(|(_, r)| !strip_slash(&r.from).is_empty() && r.from != r.to)
            .map(|(i, _)| i)
            .collect();
        if effective.is_empty() {
            return Ok(LinkRepairPlan {
                ops: Vec::new(),
                hits: Vec::new(),
                pairs,
            });
        }
        let (glob_sql, glob_param) = match path_glob {
            Some(g) => {
                let (clause, param) = glob_clause("d.path", g);
                (format!("AND {clause}"), Some(param))
            }
            None => (String::new(), None),
        };
        let sql = format!(
            "SELECT bl.block_id, bl.parent_block, d.path, b.bytes
             FROM blocks bl
             JOIN blobs b ON b.hash = bl.raw_hash
             JOIN docs d ON d.doc_id = bl.doc_id
             WHERE bl.repo_id = ?1 AND bl.deleted_commit IS NULL AND d.deleted_commit IS NULL
               AND bl.type != 'code_fence' AND instr(b.bytes, ?2) > 0 {glob_sql}
             ORDER BY d.path, bl.depth, bl.ordinal, bl.block_id"
        );
        struct Candidate {
            parent: Option<String>,
            path: String,
            raw: String,
        }
        let mut order: Vec<String> = Vec::new();
        let mut candidates: HashMap<String, Candidate> = HashMap::new();
        let mut keys_seen: Vec<String> = Vec::new();
        for &i in &effective {
            let key = strip_slash(&pairs[i].from).to_owned();
            if keys_seen.contains(&key) {
                continue;
            }
            keys_seen.push(key.clone());
            let mut stmt = self.conn.prepare(&sql)?;
            let rows: Vec<(String, Option<String>, String, Vec<u8>)> = match &glob_param {
                Some(p) => stmt
                    .query_map(params![repo_id, key, p], |r| {
                        Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?))
                    })?
                    .collect::<std::result::Result<Vec<_>, _>>()?,
                None => stmt
                    .query_map(params![repo_id, key], |r| {
                        Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?))
                    })?
                    .collect::<std::result::Result<Vec<_>, _>>()?,
            };
            for (block_id, parent, path, bytes) in rows {
                if let std::collections::hash_map::Entry::Vacant(slot) =
                    candidates.entry(block_id.clone())
                {
                    order.push(block_id);
                    slot.insert(Candidate {
                        parent,
                        path,
                        raw: String::from_utf8_lossy(&bytes).into_owned(),
                    });
                }
            }
        }
        struct Changed {
            parent: Option<String>,
            path: String,
            old_raw: String,
            new_raw: String,
            tally: Vec<usize>,
        }
        let mut changed_order: Vec<String> = Vec::new();
        let mut changed: HashMap<String, Changed> = HashMap::new();
        for block_id in &order {
            let c = &candidates[block_id];
            let mut tally = vec![0usize; effective.len()];
            let new_raw = rewrite_link_destinations(&c.raw, |dest| {
                for (k, &i) in effective.iter().enumerate() {
                    if let Some(fragment) = destination_match(dest, &pairs[i].from) {
                        tally[k] += 1;
                        return Some(format!("{}{fragment}", pairs[i].to));
                    }
                }
                None
            });
            if new_raw != c.raw {
                changed_order.push(block_id.clone());
                changed.insert(
                    block_id.clone(),
                    Changed {
                        parent: c.parent.clone(),
                        path: c.path.clone(),
                        old_raw: c.raw.clone(),
                        new_raw,
                        tally,
                    },
                );
            }
        }
        let parent_of = |id: &str| -> Result<Option<String>> {
            Ok(self
                .conn
                .query_row(
                    "SELECT parent_block FROM blocks WHERE block_id = ?1 AND deleted_commit IS NULL",
                    params![id],
                    |r| r.get::<_, Option<String>>(0),
                )
                .optional()?
                .flatten())
        };
        let mut ops = Vec::new();
        let mut hits = Vec::new();
        for block_id in &changed_order {
            let c = &changed[block_id];
            let mut p = c.parent.clone();
            let mut has_changed_ancestor = false;
            while let Some(pid) = p {
                if changed.contains_key(&pid) {
                    has_changed_ancestor = true;
                    break;
                }
                p = parent_of(&pid)?;
            }
            if has_changed_ancestor {
                continue;
            }
            for (k, &i) in effective.iter().enumerate() {
                pairs[i].hits += c.tally[k];
            }
            let hash = raw_hash_of_block(&self.conn, block_id)?;
            ops.push(update_markdown(block_id, c.new_raw.clone(), hash));
            hits.push(RetargetHit {
                block: block_id.clone(),
                path: c.path.clone(),
                old_raw: c.old_raw.clone(),
                new_raw: c.new_raw.clone(),
            });
        }
        Ok(LinkRepairPlan { ops, hits, pairs })
    }
}

/// Parse a macro's `expect`-free op list back from JSON (for callers that
/// carry expansions as JSON).
pub fn ops_from_json(v: &Value) -> Result<Vec<Op>> {
    v.as_array()
        .ok_or_else(|| Error::Other("ops must be an array".into()))?
        .iter()
        .map(|o| Op::from_json(o).map_err(Error::Other))
        .collect()
}

/// A `BTreeMap` of per-block expectations from JSON.
pub fn expect_map_from_json(v: &Value) -> Result<BTreeMap<String, Expect>> {
    let obj = v
        .as_object()
        .ok_or_else(|| Error::Other("expect must be an object".into()))?;
    obj.iter()
        .map(|(k, e)| {
            let h = e
                .get("content_hash")
                .and_then(Value::as_str)
                .map(str::to_owned);
            Ok((
                k.clone(),
                Expect {
                    content_hash: h,
                    parent_children_hash: e
                        .get("parent_children_hash")
                        .and_then(Value::as_str)
                        .map(str::to_owned),
                },
            ))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn destination_matching_ignores_one_leading_slash_and_keeps_fragments() {
        assert_eq!(destination_match("/x.md", "x.md"), Some(""));
        assert_eq!(destination_match("x.md", "/x.md"), Some(""));
        assert_eq!(destination_match("/x.md#S", "/x.md"), Some("#S"));
        assert_eq!(destination_match("/x.md^r", "x.md"), Some("^r"));
        assert_eq!(destination_match("/xx.md", "x.md"), None);
        assert_eq!(destination_match("/a/x.md", "x.md"), None);
    }

    #[test]
    fn pure_expansions() {
        let ops = Store::sections_append("b_1", "text");
        assert!(
            matches!(&ops[0], Op::Insert { doc: None, to, markdown, .. } if to.parent == Parent::Section { heading: "b_1".into() } && markdown == "text")
        );
        let ops = Store::docs_append("a.md", "text");
        assert!(matches!(&ops[0], Op::Insert { doc: Some(d), .. } if d == "a.md"));
        let ops = Store::lists_insert_item("b_0", At::End, "four");
        assert!(matches!(&ops[0], Op::Insert { markdown, .. } if markdown == "- four"));
        let ops = Store::lists_insert_item("b_0", At::Start, "  - zero");
        assert!(matches!(&ops[0], Op::Insert { markdown, .. } if markdown == "  - zero"));
        assert_eq!(editable_props_for("markdown", "md:link"), ["name", "value"]);
        assert_eq!(editable_props_for("markdown", "md:task"), ["checked"]);
        assert!(editable_props_for("markdown", "md:section").is_empty());
        assert!(editable_props_for("yaml", "md:link").is_empty());
    }
}