sokf-core 0.6.0

sokf-core holds the logic of the sokf command-line tool: it loads and validates Superdev Open Knowledge Format (SOKF) knowledge stores, and runs each sokf command.
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
//! This module reads the words that a user gives the graph commands:
//! `resolve_start` reads the `START` of a graph command (CLI-7), and
//! `resolve_id` reads the id that `locate` takes (CLI-8).

use std::path::Path;

use super::{Graph, NodeKey, NodeKind};
use crate::{
    Error, Model, PathArg, RepoPath, Result,
    model::{KeyEntry, KeyIndex, split_store},
};

/// `Start` is what a `START` resolves to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Start {
    /// A knowledge root, or a directory in one, whose layout the command
    /// prints.
    Container(RepoPath),
    /// A document, an additional file, or a repository file, for which the
    /// command prints one row.
    Node(NodeKey),
    /// A directory outside every knowledge store, for which the command
    /// prints the roots and the files under it.
    Outside(RepoPath),
}

impl Start {
    /// `key` returns the key of the start's graph node.
    pub fn key(&self) -> NodeKey {
        match self {
            Start::Container(p) | Start::Outside(p) => NodeKey::Path(p.clone()),
            Start::Node(k) => k.clone(),
        }
    }

    /// `path` returns the start as a path, for the `start` field of the
    /// JSON output, or `None` for a node that has no path.
    pub fn path(&self) -> Option<&RepoPath> {
        match self {
            Start::Container(p) | Start::Outside(p) => Some(p),
            Start::Node(k) => k.path(),
        }
    }
}

/// `is_key_reference` reports whether `arg` is a reference to a key alone,
/// `#key` or `<store>:#key`, which the caller can resolve only with the
/// index of captured keys.
pub fn is_key_reference(arg: &str) -> bool {
    let arg = arg.strip_prefix("sokf:").unwrap_or(arg);
    arg.split_once('#')
        .is_some_and(|(head, _)| split_store(head).1.is_empty())
}

/// `key_entry` returns the key that a reference identifies, with the key's
/// slug. A reference to a key alone identifies the key when exactly one
/// document captures it (CLI-7_AC-74), at repository or knowledge scope or,
/// failing those, at document scope (CLI-7_AC-98). A reference
/// `<id>#<key>` identifies the key when the document with that id captures
/// it (CLI-7_AC-97).
pub fn key_entry(model: &Model, keys: &KeyIndex, arg: &str) -> Option<(KeyEntry, String)> {
    let written = arg.split_once('#')?.1;
    if written.is_empty() {
        return None;
    }
    let path = match paths_of_reference(model, Some(keys), arg).as_slice() {
        [one] => one.clone(),
        [] => match document_scoped(model, keys, arg).as_slice() {
            [(one, _)] => one.clone(),
            _ => return None,
        },
        _ => return None,
    };
    [written.to_string(), crate::markdown::slug(written)]
        .into_iter()
        .find_map(|slug| {
            let entry = keys.document.get(&(path.clone(), slug.clone()))?.first()?;
            Some((entry.clone(), slug))
        })
}

/// `document_scoped` returns each document that captures the key of `arg`,
/// with the key's slug, in the order of the documents' paths, when `arg` is
/// a reference to a key alone that matches no key of repository or knowledge
/// scope (CLI-7_AC-98 to CLI-7_AC-100). The documents lie in the stores
/// that the reference gives, or in every store when it gives none.
fn document_scoped(model: &Model, keys: &KeyIndex, arg: &str) -> Vec<(RepoPath, String)> {
    if !is_key_reference(arg) || !paths_of_reference(model, Some(keys), arg).is_empty() {
        return Vec::new();
    }
    let reference = arg.strip_prefix("sokf:").unwrap_or(arg);
    let Some((head, written)) = reference.split_once('#') else {
        return Vec::new();
    };
    let stores = stores_of(model, split_store(head).0);
    let slugs = [written.to_string(), crate::markdown::slug(written)];
    let mut found: Vec<(RepoPath, String)> = keys
        .document
        .keys()
        .filter(|(p, s)| {
            slugs.contains(s) && model.knowledge_of(p).is_some_and(|k| stores.contains(&k))
        })
        .cloned()
        .collect();
    // The key as written and its slug can both match one document, so each
    // document keeps one slug.
    found.sort();
    found.dedup_by(|a, b| a.0 == b.0);
    found
}

/// `stores_of` returns the stores that a reference searches: each store of
/// the name it gives, or every store when it gives none.
fn stores_of(model: &Model, store: Option<&str>) -> Vec<usize> {
    match store {
        None => (0..model.knowledges.len()).collect(),
        Some(s) => model.stores_named(s),
    }
}

/// `note` returns the note that `graph` prints for `arg`, when `arg` is a
/// reference to a key alone that one document captures at document scope
/// only. The note gives the reference that names the key in a link
/// (CLI-7_AC-98).
pub(super) fn note(model: &Model, keys: Option<&KeyIndex>, arg: &str) -> Option<String> {
    let [(path, slug)] = document_scoped(model, keys?, arg).try_into().ok()?;
    let reference = key_reference(model, &path, &slug)?;
    let id = reference.split_once('#')?.0;
    Some(format!(
        "`{arg}` is unique only within `{id}`, so link it as `{reference}`"
    ))
}

/// `key_reference` returns the reference `<id>#<slug>` to the key `slug` of
/// the document at `path`. The id has its store before it when another
/// store has a document with the same id. `key_reference` returns `None`
/// when another document of the same store has the id, because no reference
/// then names the document.
fn key_reference(model: &Model, path: &RepoPath, slug: &str) -> Option<String> {
    let (k, id) = id_of(model, path)?;
    let mut elsewhere = false;
    for o in 0..model.knowledges.len() {
        if model.ids(o, &id).into_iter().any(|p| &p != path) {
            if o == k {
                return None;
            }
            elsewhere = true;
        }
    }
    Some(if elsewhere {
        format!("{}:{id}#{slug}", model.store_name(k)?)
    } else {
        format!("{id}#{slug}")
    })
}

/// `id_of` returns the store and the id of the document at `path`.
fn id_of(model: &Model, path: &RepoPath) -> Option<(usize, String)> {
    if let Some(found) = model.id_at(path) {
        Some(found)
    } else {
        // The run does not hold this document, so the id comes from the
        // store's row.
        let k = model.knowledge_of(path)?;
        let id = model.fallback.as_ref()?.fields(path)?.id?;
        Some((k, id))
    }
}

/// `paths_of_reference` returns the paths of every document and schema that
/// a reference identifies, when it is written outside every store, such as
/// on the command line. The reference
/// `[sokf:][<store>:]<id>[#<fragment>]` identifies the document with that
/// id, whatever its fragment. The reference `[sokf:][<store>:]#<key>`
/// identifies the document that captures the key, when only one document
/// captures it (CLI-7_AC-29 to CLI-7_AC-32). A reference with no store
/// searches every root.
fn paths_of_reference(model: &Model, keys: Option<&KeyIndex>, arg: &str) -> Vec<RepoPath> {
    // sokf-implements: CLI-7_AC-60
    let reference = arg.strip_prefix("sokf:").unwrap_or(arg);
    let (head, fragment) = match reference.split_once('#') {
        Some((h, f)) => (h, Some(f)),
        None => (reference, None),
    };
    let (store, id) = split_store(head);
    let stores = stores_of(model, store);
    let mut paths: Vec<RepoPath> =
        if id.is_empty() {
            let (Some(keys), Some(key)) = (keys, fragment.filter(|f| !f.is_empty())) else {
                return Vec::new();
            };
            // The index holds each key under its slug, so the lookup tries
            // the key as written and then its slug.
            [key.to_string(), crate::markdown::slug(key)]
                .into_iter()
                .flat_map(|key| {
                    keys.repository
                        .get(&key)
                        .into_iter()
                        .flatten()
                        .chain(stores.iter().flat_map(|&k| {
                            keys.knowledge.get(&(k, key.clone())).into_iter().flatten()
                        }))
                        .filter(|e| stores.contains(&e.knowledge))
                        .map(|e| e.path.clone())
                        .collect::<Vec<_>>()
                })
                .collect()
        } else {
            // The lookup searches the run's documents and the store's rows
            // (CACHE-1_AC-21).
            stores.iter().flat_map(|&k| model.ids(k, id)).collect()
        };
    paths.sort();
    paths.dedup();
    paths
}

/// `qualified` returns the qualified form of `arg` for each document in
/// `paths`, as advice for an ambiguous reference. Each form starts with the
/// store's name, or with its whole identity when another root shares that
/// name. The document's id follows, or `#key` for a reference to a key
/// alone. `qualified` returns an empty string when it cannot qualify every
/// document.
fn qualified(model: &Model, arg: &str, paths: &[RepoPath]) -> String {
    let key = is_key_reference(arg)
        .then(|| arg.split_once('#').map(|(_, k)| k))
        .flatten();
    let forms: Vec<String> = paths
        .iter()
        .filter_map(|p| {
            let (k, id) = id_of(model, p)?;
            let store = model.store_name(k)?;
            Some(match key {
                Some(key) => format!("`{store}:#{key}`"),
                None => format!("`{store}:{id}`"),
            })
        })
        .collect();
    if forms.len() == paths.len() {
        format!(". Write {}", forms.join(" or "))
    } else {
        String::new()
    }
}

fn join(paths: &[RepoPath]) -> String {
    paths
        .iter()
        .map(RepoPath::as_str)
        .collect::<Vec<_>>()
        .join(", ")
}

/// `resolve_id` returns the one document that a reference identifies across
/// every root, or refuses a reference that identifies no document or more
/// than one (CLI-8_AC-9).
// sokf-implements: CLI-8_AC-9
// sokf-implements: CLI-8_AC-10
// sokf-implements: CLI-8_AC-11
// sokf-implements: CLI-8_AC-6
// sokf-implements: FORMAT-2_AC-1
pub fn resolve_id(model: &Model, keys: Option<&KeyIndex>, id: &str) -> Result<RepoPath> {
    match paths_of_reference(model, keys, id).as_slice() {
        [] => Err(Error::Locate(format!("`{id}` identifies no document"))),
        [one] => Ok(one.clone()),
        many => Err(Error::Locate(format!(
            "`{id}` identifies more than one document: {}{}",
            join(many),
            qualified(model, id, many)
        ))),
    }
}

/// `resolve_start` reads `arg` both as an `id` and as a path, and refuses it
/// unless exactly one reading resolves (CLI-7_AC-19, CLI-7_AC-20). When the
/// start is a directory or a repository file, the function adds it to the
/// graph as a node.
// sokf-implements: CLI-7_AC-29
// sokf-implements: CLI-7_AC-30
// sokf-implements: CLI-7_AC-31
// sokf-implements: CLI-7_AC-32
// sokf-implements: CLI-7_AC-33
// sokf-implements: CLI-1_AC-7
// sokf-implements: CLI-1_AC-8
// sokf-implements: CLI-7_AC-4
// sokf-implements: CLI-7_AC-19
// sokf-implements: CLI-7_AC-20
#[expect(
    clippy::case_sensitive_file_extension_comparisons,
    reason = "a START that ends in a lowercase `.md` is path-shaped, as CLI-7_AC-28 says, so the comparison is case-sensitive on purpose"
)]
pub fn resolve_start(
    graph: &mut Graph,
    model: &Model,
    keys: Option<&KeyIndex>,
    cwd: &Path,
    arg: &str,
) -> Result<Start> {
    // The function reads a word shaped as a path only as a path, even when
    // a document has that word as its id.
    // sokf-implements: CLI-7_AC-28
    let path_shaped = arg.starts_with('.') || arg.ends_with('/') || arg.ends_with(".md");
    let ids = if path_shaped {
        Vec::new()
    } else {
        paths_of_reference(model, keys, arg)
    };
    let path = PathArg(arg).resolve(&model.tree.root, cwd);
    match (ids.as_slice(), path) {
        ([_, ..], Some(p)) => Err(Error::Start(format!(
            "`{arg}` is both an id ({}) and a path ({p})",
            join(&ids)
        ))),
        ([_, _, ..], None) => Err(Error::Start(format!(
            "`{arg}` identifies more than one document: {}{}",
            join(&ids),
            qualified(model, arg, &ids)
        ))),
        // For a reference to a key alone, and for `<id>#<key>` when the
        // document captures the key, the start is the key's node when the
        // graph has one.
        // sokf-implements: CLI-7_AC-32
        // sokf-implements: CLI-7_AC-97
        ([one], None) => Ok(key_start(graph, model, keys, arg, one)),
        ([], Some(p)) => Ok(path_start(graph, model, p)),
        ([], None) => {
            // A key that no document captures at repository or knowledge
            // scope can still be a key of document scope.
            let scoped = keys.map_or_else(Vec::new, |k| document_scoped(model, k, arg));
            match scoped.as_slice() {
                // sokf-implements: CLI-7_AC-98
                [(one, _)] => Ok(key_start(graph, model, keys, arg, one)),
                // sokf-implements: CLI-7_AC-100
                [] => Err(Error::Start(format!(
                    "`{arg}` is neither an id nor a path in the repository"
                ))),
                // sokf-implements: CLI-7_AC-99
                many => {
                    let forms: Vec<String> = many
                        .iter()
                        .map(|(p, slug)| match key_reference(model, p, slug) {
                            Some(r) => format!("`{r}`"),
                            None => p.to_string(),
                        })
                        .collect();
                    Err(Error::Start(format!(
                        "`{arg}` is a key of document scope that more than one document captures. Write {}",
                        capped(&forms)
                    )))
                }
            }
        }
    }
}

/// `LISTED` is the number of references that the error for a key of
/// document scope lists before it counts the rest (CLI-7_AC-99).
const LISTED: usize = 10;

/// `capped` joins the first `LISTED` of `forms` with "or", in their order,
/// and follows them with "and N more" when `forms` holds more.
// sokf-implements: CLI-7_AC-99
fn capped(forms: &[String]) -> String {
    let listed = forms[..forms.len().min(LISTED)].join(" or ");
    match forms.len().saturating_sub(LISTED) {
        0 => listed,
        more => format!("{listed}, and {more} more"),
    }
}

/// `key_start` returns the start of `arg` in the document at `path`: the
/// node of the key that `arg` names, when the graph has one, and the
/// document otherwise.
fn key_start(
    graph: &Graph,
    model: &Model,
    keys: Option<&KeyIndex>,
    arg: &str,
    path: &RepoPath,
) -> Start {
    Start::Node(
        keys.and_then(|k| key_entry(model, k, arg))
            .map(|(_, slug)| NodeKey::Key(format!("{path}#{slug}")))
            .filter(|k| graph.node(k).is_some())
            .unwrap_or_else(|| NodeKey::Path(path.clone())),
    )
}

/// `path_start` reads a path as a start. It tries each reading in order: a
/// root, a directory, a node that the graph has, and then a repository
/// file.
fn path_start(graph: &mut Graph, model: &Model, path: RepoPath) -> Start {
    if model.tree.roots.contains(&path) {
        return Start::Container(path);
    }
    if path.to_fs(&model.tree.root).is_dir() {
        graph.ensure(path.clone(), NodeKind::Directory);
        return if model.knowledge_of(&path).is_some() {
            Start::Container(path)
        } else {
            Start::Outside(path)
        };
    }
    let key = NodeKey::Path(path.clone());
    if graph.node(&key).is_none() {
        graph.ensure(path, NodeKind::File);
    }
    Start::Node(key)
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::{
        graph::build,
        test_support::{fresh_dir, model_at, write},
    };

    fn temp(name: &str) -> PathBuf {
        let dir = fresh_dir(&format!("start-{name}"));
        for (rel, text) in [
            (
                "knowledge/manifest.sokf.yaml",
                "sokf: \"0.6\"\nname: k\nscope: test\n",
            ),
            (
                "knowledge/core/a.md",
                "---\ntype: Note\nid: a\nresource: /src/a.rs\n---\n\n# A\n",
            ),
            (
                "knowledge/core/b.md",
                "---\ntype: Note\nid: dup\n---\n\n# B\n",
            ),
            ("knowledge/c.md", "---\ntype: Note\nid: dup\n---\n\n# C\n"),
            (
                "knowledge/core.md",
                "---\ntype: Note\nid: core\n---\n\n# Core\n",
            ),
            (
                "other/manifest.sokf.yaml",
                "sokf: \"0.6\"\nname: o\nscope: test\n",
            ),
            ("other/n.md", "---\ntype: Note\nid: a\n---\n\n# N\n"),
            ("src/a.rs", ""),
            ("src/plain.rs", ""),
            ("docs/x.txt", ""),
        ] {
            write(&dir, rel, text);
        }
        std::fs::create_dir_all(dir.join("knowledge/empty")).unwrap();
        dir
    }

    fn p(s: &str) -> RepoPath {
        RepoPath::parse(s).unwrap()
    }

    // sokf-tests: CLI-7_AC-4
    // sokf-tests: CLI-7_AC-19
    // sokf-tests: CLI-7_AC-20
    // sokf-tests: CLI-7_AC-29
    // sokf-tests: CLI-1_AC-7
    // sokf-tests: CLI-1_AC-8
    #[test]
    fn each_reading_and_each_refusal() {
        let dir = temp("reading");
        let m = model_at(&dir, &["/knowledge"]);
        let mut g = build(&m);
        let mut start = |arg: &str, cwd: &Path| resolve_start(&mut g, &m, None, cwd, arg);
        // An id that identifies exactly one document starts at that document.
        assert_eq!(
            start("a", &dir).unwrap(),
            Start::Node(NodeKey::Path(p("/knowledge/core/a.md")))
        );
        // A root and a directory in a root each start as a container, in
        // both spellings.
        assert_eq!(
            start("knowledge", &dir).unwrap(),
            Start::Container(p("/knowledge"))
        );
        assert_eq!(
            start("/knowledge", &dir).unwrap(),
            Start::Container(p("/knowledge"))
        );
        assert_eq!(
            start("core/", &dir.join("knowledge")).unwrap(),
            Start::Container(p("/knowledge/core"))
        );
        assert_eq!(
            start("./core", &dir.join("knowledge")).unwrap(),
            Start::Container(p("/knowledge/core"))
        );
        assert_eq!(
            start("empty", &dir.join("knowledge")).unwrap(),
            Start::Container(p("/knowledge/empty"))
        );
        // A document given by its path, a file that an edge points to, and
        // a plain file each start at their own node.
        assert_eq!(
            start("core.md", &dir.join("knowledge")).unwrap(),
            Start::Node(NodeKey::Path(p("/knowledge/core.md")))
        );
        assert_eq!(
            start("src/a.rs", &dir).unwrap(),
            Start::Node(NodeKey::Path(p("/src/a.rs")))
        );
        assert_eq!(
            start("src/plain.rs", &dir).unwrap(),
            Start::Node(NodeKey::Path(p("/src/plain.rs")))
        );
        // A directory outside every knowledge store, the repository root
        // among them, starts outside the stores.
        assert_eq!(start("src", &dir).unwrap(), Start::Outside(p("/src")));
        assert_eq!(start(".", &dir).unwrap(), Start::Outside(p("/")));
        // `resolve_start` refuses a duplicate id, an id that is also a path,
        // and a word that resolves to nothing.
        for (arg, needle) in [
            (
                "dup",
                "identifies more than one document: /knowledge/c.md, /knowledge/core/b.md",
            ),
            (
                "core",
                "is both an id (/knowledge/core.md) and a path (/knowledge/core)",
            ),
            ("nothing", "neither an id nor a path"),
            ("/knowledge/gone.md", "neither an id nor a path"),
        ] {
            let err = start(arg, &dir.join("knowledge")).unwrap_err();
            assert!(matches!(err, Error::Start(_)), "{arg}: {err}");
            assert!(err.to_string().contains(needle), "{arg}: {err}");
        }
        // The graph holds the nodes that the starts resolved to, each with
        // its kind.
        assert_eq!(
            g.kind(&NodeKey::Path(p("/knowledge/empty"))),
            Some(NodeKind::Directory)
        );
        assert_eq!(g.kind(&NodeKey::Path(p("/src"))), Some(NodeKind::Directory));
        assert_eq!(
            g.kind(&NodeKey::Path(p("/src/plain.rs"))),
            Some(NodeKind::File)
        );
        assert_eq!(g.kind(&NodeKey::Path(p("/src/a.rs"))), Some(NodeKind::File));
        assert_eq!(Start::Outside(p("/src")).key(), NodeKey::Path(p("/src")));
        assert_eq!(Start::Node(NodeKey::Missing("x".into())).path(), None);
        std::fs::remove_dir_all(&dir).unwrap();
    }

    // sokf-tests: CLI-8_AC-6
    // sokf-tests: CLI-8_AC-9
    // sokf-tests: CLI-8_AC-11
    // sokf-tests: CLI-7_AC-30
    #[test]
    fn references_resolve_across_roots_or_refuse() {
        let dir = temp("ids");
        let m = model_at(&dir, &["/knowledge", "/other"]);
        assert_eq!(
            resolve_id(&m, None, "core").unwrap(),
            p("/knowledge/core.md")
        );
        let err = resolve_id(&m, None, "a").unwrap_err();
        assert!(matches!(err, Error::Locate(_)));
        assert_eq!(
            err.to_string(),
            "locate: `a` identifies more than one document: /knowledge/core/a.md, /other/n.md. Write `k:a` or `o:a`"
        );
        assert_eq!(
            resolve_id(&m, None, "zz").unwrap_err().to_string(),
            "locate: `zz` identifies no document"
        );
        // Each form of a reference resolves: a store given by its whole
        // identity or by its name, the `sokf:` scheme, and a fragment.
        for form in ["o:a", "test/o:a", "sokf:o:a", "o:a#n"] {
            assert_eq!(
                resolve_id(&m, None, form).unwrap(),
                p("/other/n.md"),
                "{form}"
            );
        }
        assert!(resolve_id(&m, None, "nope:a").is_err());
        assert!(is_key_reference("#k") && is_key_reference("sokf:o:#k"));
        assert!(!is_key_reference("o:a#k") && !is_key_reference("a"));
        // `resolve_start` also resolves an id across every root.
        let mut g = build(&m);
        let err = resolve_start(&mut g, &m, None, &dir, "a").unwrap_err();
        assert!(err.to_string().contains("/other/n.md"), "{err}");
        std::fs::remove_dir_all(&dir).unwrap();
    }

    // sokf-tests: CLI-7_AC-31
    // sokf-tests: CLI-7_AC-97
    // sokf-tests: CLI-7_AC-98
    // sokf-tests: CLI-7_AC-99
    // sokf-tests: CLI-7_AC-100
    #[test]
    fn a_key_of_document_scope_starts_when_one_document_captures_it() {
        use crate::grammar::KeyScope;
        let dir = temp("document-scope");
        let m = model_at(&dir, &["/knowledge", "/other"]);
        let mut keys = KeyIndex::default();
        for (key, path, scope) in [
            ("X-1", "/knowledge/core/a.md", KeyScope::Document),
            ("Y-1", "/knowledge/core.md", KeyScope::Document),
            ("Y-1", "/knowledge/core/a.md", KeyScope::Document),
            (
                "R-1",
                "/knowledge/core/a.md",
                KeyScope::Host("knowledge".into()),
            ),
            ("R-1", "/knowledge/core.md", KeyScope::Document),
        ] {
            keys.add(KeyEntry {
                key: key.into(),
                path: p(path),
                line: 3,
                heading_slug: None,
                scope,
                knowledge: m.knowledge_of(&p(path)).unwrap(),
                rule_id: None,
            });
        }
        let mut g = build(&m);
        for (path, slug) in [
            ("/knowledge/core/a.md", "x-1"),
            ("/knowledge/core/a.md", "y-1"),
            ("/knowledge/core.md", "y-1"),
        ] {
            g.add_key(&p(path), slug, &slug.to_uppercase(), 3, &[]);
        }
        let key = |s: &str| Start::Node(NodeKey::Key(s.into()));
        let mut start = |arg: &str| resolve_start(&mut g, &m, Some(&keys), &dir, arg);
        // One document captures `X-1`, so `#x-1` starts at the key, in each
        // spelling, and the note names the document. The id `a` is also an
        // id of the store `o`, so the reference gives the store.
        for arg in ["#x-1", "#X-1", "k:#x-1", "sokf:#x-1"] {
            assert_eq!(
                start(arg).unwrap(),
                key("/knowledge/core/a.md#x-1"),
                "{arg}"
            );
        }
        assert_eq!(
            note(&m, Some(&keys), "#x-1").as_deref(),
            Some("`#x-1` is unique only within `k:a`, so link it as `k:a#x-1`")
        );
        // `<id>#<key>` starts at the key that the document captures, and at
        // the document when the fragment names no key of it.
        assert_eq!(start("core#y-1").unwrap(), key("/knowledge/core.md#y-1"));
        assert_eq!(
            start("core#nothing").unwrap(),
            Start::Node(NodeKey::Path(p("/knowledge/core.md")))
        );
        // A key of knowledge scope keeps its start, with no note.
        assert_eq!(
            start("#r-1").unwrap(),
            Start::Node(NodeKey::Path(p("/knowledge/core/a.md")))
        );
        assert_eq!(note(&m, Some(&keys), "#r-1"), None);
        assert_eq!(note(&m, Some(&keys), "core#y-1"), None);
        // Two documents capture `Y-1`, so the error lists the reference of
        // each, in the order of their paths. No document of the store `o`
        // captures it, and no document captures `#none`.
        for (arg, message) in [
            (
                "#y-1",
                "graph: `#y-1` is a key of document scope that more than one document captures. Write `core#y-1` or `k:a#y-1`",
            ),
            (
                "o:#y-1",
                "graph: `o:#y-1` is neither an id nor a path in the repository",
            ),
            (
                "#none",
                "graph: `#none` is neither an id nor a path in the repository",
            ),
        ] {
            let err = start(arg).unwrap_err();
            assert!(matches!(err, Error::Start(_)), "{arg}: {err}");
            assert_eq!(err.to_string(), message);
        }
        std::fs::remove_dir_all(&dir).unwrap();
    }

    // sokf-tests: CLI-7_AC-99
    #[test]
    fn the_error_lists_ten_references_and_counts_the_rest() {
        let forms = |n: usize| (1..=n).map(|i| format!("`n{i}#y-1`")).collect::<Vec<_>>();
        let ten = (1..=10)
            .map(|i| format!("`n{i}#y-1`"))
            .collect::<Vec<_>>()
            .join(" or ");
        assert_eq!(capped(&forms(2)), "`n1#y-1` or `n2#y-1`");
        assert_eq!(capped(&forms(10)), ten);
        assert_eq!(capped(&forms(11)), format!("{ten}, and 1 more"));
        assert_eq!(capped(&forms(103)), format!("{ten}, and 93 more"));
    }

    // sokf-tests: CLI-7_AC-28
    #[test]
    fn a_word_shaped_as_a_path_is_read_only_as_a_path() {
        let dir = temp("path-shaped");
        // Each id breaks the slug rules and has the shape of a path.
        for (rel, id) in [
            ("knowledge/core.md.md", "core.md"),
            ("knowledge/e.md", "core/"),
            ("knowledge/f.md", ".hidden"),
        ] {
            std::fs::write(
                dir.join(rel),
                format!("---\ntype: Note\nid: \"{id}\"\n---\n\n# X\n"),
            )
            .unwrap();
        }
        std::fs::create_dir_all(dir.join(".hidden")).unwrap();
        let m = model_at(&dir, &["/knowledge"]);
        let mut g = build(&m);
        let mut start = |arg: &str, cwd: &Path| resolve_start(&mut g, &m, None, cwd, arg);
        let knowledge = dir.join("knowledge");
        assert_eq!(
            start("core.md", &knowledge).unwrap(),
            Start::Node(NodeKey::Path(p("/knowledge/core.md")))
        );
        assert_eq!(
            start("core/", &knowledge).unwrap(),
            Start::Container(p("/knowledge/core"))
        );
        assert_eq!(
            start(".hidden", &dir).unwrap(),
            Start::Outside(p("/.hidden"))
        );
        let _ = std::fs::remove_dir_all(&dir);
    }
}