tread 0.4.2

A terminal reader for markdown, CSV, JSON and source code. Zero 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
//! Navigation tests. Path resolution runs against an in-memory filesystem so
//! it is deterministic; the index parser and slug matching are additionally
//! checked against the real codex corpus when it is present on this machine.
#![deny(unsafe_code)]

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use super::index;
use super::link::{self, Fs, Target};
use super::{absolutize, discover, Navigator};
use crate::md;


// -- in-memory filesystem ----------------------------------------------------

#[derive(Default)]
struct MemFs {
    files: HashMap<PathBuf, String>,
}

impl MemFs {
    fn with(paths: &[(&str, &str)]) -> MemFs {
        MemFs {
            files: paths
                .iter()
                .map(|(p, c)| (PathBuf::from(p), c.to_string()))
                .collect(),
        }
    }
}

impl Fs for MemFs {
    fn is_file(&self, path: &Path) -> bool {
        self.files.contains_key(path)
    }
    fn is_dir(&self, path: &Path) -> bool {
        self.files.keys().any(|f| f.parent() == Some(path))
            || self.files.keys().any(|f| f.starts_with(path) && f != path)
    }
    fn read(&self, path: &Path) -> Result<String, String> {
        self.files
            .get(path)
            .cloned()
            .ok_or_else(|| format!("{}: not found", path.display()))
    }
}

const ROOT: &str = "/corpus";

fn fs() -> MemFs {
    MemFs::with(&[
        ("/corpus/README.md", "# Index\n\n[a](models/A.md)\n"),
        ("/corpus/CONVENTIONS.md", "# Conventions\n"),
        ("/corpus/models/A.md", "# A\n\n## Deep Heading\n"),
        ("/corpus/models/B.md", "# B\n"),
        ("/corpus/models/README.md", "# Models\n"),
        ("/corpus/assets/diagram.png", "binary"),
        ("/outside/SECRET.md", "# Secret\n"),
    ])
}

fn resolve(raw: &str, from: &str) -> Target {
    link::resolve(raw, Path::new(from), Path::new(ROOT), &fs())
}

// -- path resolution ---------------------------------------------------------

#[test]
fn relative_links_resolve_against_the_document_not_the_cwd() {
    assert_eq!(
        resolve("B.md", "/corpus/models"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/B.md"),
            anchor: None
        }
    );
    // The same link text from the root resolves somewhere else entirely.
    assert!(matches!(
        resolve("B.md", "/corpus"),
        Target::Broken { .. }
    ));
    assert_eq!(
        resolve("models/A.md", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/A.md"),
            anchor: None
        }
    );
}

#[test]
fn dot_and_dotdot_are_folded() {
    assert_eq!(
        resolve("../CONVENTIONS.md", "/corpus/models"),
        Target::Doc {
            path: PathBuf::from("/corpus/CONVENTIONS.md"),
            anchor: None
        }
    );
    assert_eq!(
        resolve("./B.md", "/corpus/models"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/B.md"),
            anchor: None
        }
    );
    assert_eq!(
        resolve("../models/./../models/B.md", "/corpus/models"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/B.md"),
            anchor: None
        }
    );
}

#[test]
fn escapes_above_the_index_root_are_rejected() {
    for raw in ["../../outside/SECRET.md", "../outside/SECRET.md", "../../../../etc/passwd"] {
        match resolve(raw, "/corpus/models") {
            Target::Broken { why, .. } => assert!(
                why.contains("escapes") || why.contains("no such file"),
                "{raw}: {why}"
            ),
            other => panic!("{raw} should not resolve: {other:?}"),
        }
    }
    // …and the message names the escape, so the status bar can say so.
    let t = resolve("../../outside/SECRET.md", "/corpus/models");
    assert!(t.describe(Path::new(ROOT)).contains("escapes the index root"));
}

#[test]
fn a_directory_resolves_to_its_readme() {
    assert_eq!(
        resolve("models", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/README.md"),
            anchor: None
        }
    );
    assert_eq!(
        resolve("models/", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/README.md"),
            anchor: None
        }
    );
}

#[test]
fn an_extensionless_target_resolves_to_the_md_file() {
    assert_eq!(
        resolve("CONVENTIONS", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/CONVENTIONS.md"),
            anchor: None
        }
    );
    assert_eq!(
        resolve("../CONVENTIONS#when-in-doubt", "/corpus/models"),
        Target::Doc {
            path: PathBuf::from("/corpus/CONVENTIONS.md"),
            anchor: Some("when-in-doubt".to_string())
        }
    );
}

#[test]
fn root_absolute_links_resolve_against_the_corpus_root() {
    assert_eq!(
        resolve("/models/B.md", "/corpus/models"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/B.md"),
            anchor: None
        }
    );
}

#[test]
fn link_kinds_are_classified() {
    assert!(matches!(
        resolve("https://example.com/x", "/corpus"),
        Target::External(_)
    ));
    assert!(matches!(
        resolve("mailto:a@b.c", "/corpus"),
        Target::External(_)
    ));
    assert!(matches!(resolve("#some-heading", "/corpus"), Target::Anchor(a) if a == "some-heading"));
    // A file that is not markdown is followable now that the reader can show
    // any file (SPEC.md §Plain text). This used to assert `Target::Other`,
    // which was the refusal that section removes.
    assert!(matches!(
        resolve("assets/diagram.png", "/corpus"),
        Target::Doc { ref path, anchor: None } if path.ends_with("diagram.png")
    ));
    assert!(matches!(resolve("nope.md", "/corpus"), Target::Broken { .. }));
    assert!(matches!(resolve("", "/corpus"), Target::Broken { .. }));
}

#[test]
fn cross_document_anchors_carry_a_slug() {
    assert_eq!(
        resolve("models/A.md#Deep Heading", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/A.md"),
            anchor: Some("deep-heading".to_string())
        }
    );
    assert_eq!(
        resolve("models/A.md#deep-heading", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/A.md"),
            anchor: Some("deep-heading".to_string())
        }
    );
}

#[test]
fn percent_escapes_and_schemes() {
    assert_eq!(link::percent_decode("a%20b"), "a b");
    assert_eq!(link::percent_decode("a%zzb"), "a%zzb");
    assert_eq!(link::scheme_of("https://x"), Some("https"));
    assert_eq!(link::scheme_of("models/DNS:MODEL.md"), None);
    assert_eq!(link::scheme_of("./x"), None);
}

#[test]
fn describe_and_yank_use_the_right_text() {
    let root = Path::new(ROOT);
    let doc = resolve("models/A.md#deep-heading", "/corpus");
    assert_eq!(doc.describe(root), "models/A.md#deep-heading");
    let ext = resolve("https://example.com/x", "/corpus");
    assert_eq!(ext.describe(root), "https://example.com/x");
    assert_eq!(ext.yank_text(root), "https://example.com/x");
    assert_eq!(doc.yank_text(root), "models/A.md");
}

// -- root discovery ----------------------------------------------------------

#[test]
fn explicit_index_wins_and_accepts_a_directory() {
    let fs = fs();
    let (root, idx) = discover(&fs, Path::new("/corpus/models/A.md"), Some(Path::new("/corpus")));
    assert_eq!(root, PathBuf::from("/corpus"));
    assert_eq!(idx, Some(PathBuf::from("/corpus/README.md")));
    let (root, idx) = discover(
        &fs,
        Path::new("/corpus/models/A.md"),
        Some(Path::new("/corpus/README.md")),
    );
    assert_eq!((root, idx), (PathBuf::from("/corpus"), Some(PathBuf::from("/corpus/README.md"))));
}

#[test]
fn discovery_walks_up_to_the_readme_that_links_to_the_file() {
    let fs = fs();
    // /corpus/models/README.md exists but does not link to A.md, so the walk
    // continues to /corpus/README.md, which does.
    let (root, idx) = discover(&fs, Path::new("/corpus/models/A.md"), None);
    assert_eq!(root, PathBuf::from("/corpus"));
    assert_eq!(idx, Some(PathBuf::from("/corpus/README.md")));
}

#[test]
fn discovery_falls_back_to_the_files_own_directory() {
    let fs = MemFs::with(&[("/lone/NOTES.md", "# Notes\n")]);
    let (root, idx) = discover(&fs, Path::new("/lone/NOTES.md"), None);
    assert_eq!(root, PathBuf::from("/lone"));
    assert_eq!(idx, None);
}

#[test]
fn absolutize_normalizes_relative_paths() {
    assert_eq!(
        absolutize(Path::new("../a/./b.md"), Path::new("/x/y")),
        PathBuf::from("/x/a/b.md")
    );
    assert_eq!(
        absolutize(Path::new("/x/../y.md"), Path::new("/ignored")),
        PathBuf::from("/y.md")
    );
}

/// The nav layer is platform-agnostic because it delegates to `plat::path`,
/// whose Windows rules are proved in `plat::path_tests` from any host. What is
/// checked here is the *wiring*: that these entry points speak this platform's
/// dialect rather than a hardcoded `/` one.
#[test]
fn path_handling_speaks_this_platforms_dialect() {
    use crate::plat::{path as ppath, Platform};
    let sep = ppath::sep(Platform::HOST);
    let root = PathBuf::from(ppath::join(Platform::HOST, "/corpus", "").unwrap());
    let deep = PathBuf::from(ppath::join(Platform::HOST, "/corpus", "models/a.md").unwrap());
    // Joining speaks the native dialect...
    assert!(deep.to_string_lossy().contains(&format!("models{}a.md", ppath::sep(Platform::HOST))));
    // ...but the corpus-relative name a user sees and yanks is always `/`.
    assert_eq!(link::rel_to(&deep, &root), "models/a.md");
    assert!(link::within_root(&deep, &root));
    assert!(!link::within_root(Path::new("/elsewhere/a.md"), &root));
    // Identity survives a `.` and a redundant separator.
    let noisy = PathBuf::from(format!("{}{sep}.{sep}models{sep}a.md", root.display()));
    assert!(link::same_path(&deep, &noisy));
    assert!(!link::same_path(&deep, &root));
    // A link that walks above the root is still an escape, not a fold to `/`.
    assert_eq!(link::normalize(&root, "../../etc/passwd"), None);
}

// -- index parsing -----------------------------------------------------------

const INDEX_SRC: &str = "\
# Corpus

## Models

| Doc | Status |
|---|---|
| [models/A.md](models/A.md) — the A model | Active |
| [models/B.md](models/B.md) | Draft |

## Links we ignore

- [external](https://example.com)
- [picture](assets/diagram.png)
- [again](models/A.md)
";

fn parsed_index() -> Vec<index::Entry> {
    let doc = md::parse(INDEX_SRC);
    index::parse(&doc, Path::new(ROOT), Path::new(ROOT), &fs())
}

#[test]
fn index_groups_by_section_and_keeps_trailing_text() {
    let e = parsed_index();
    assert_eq!(e.len(), 2, "external, non-markdown and repeat links drop out");
    assert_eq!(e[0].section, "Models");
    assert_eq!(e[0].title, "models/A.md");
    assert_eq!(e[0].path, PathBuf::from("/corpus/models/A.md"));
    assert_eq!(e[0].desc, "the A model");
    // With no trailing text the row's other columns become the description.
    assert_eq!(e[1].desc, "Draft");
    assert!(e[0].row().contains("the A model"));
    assert!(e[0].haystack().contains("models/a.md"));
}

#[test]
fn navigator_walks_the_corpus_in_index_order() {
    let files = [
        ("/corpus/README.md", INDEX_SRC),
        ("/corpus/models/A.md", "# A\n"),
        ("/corpus/models/B.md", "# B\n"),
        ("/corpus/assets/diagram.png", "x"),
    ];
    let mk = |cur: &str| {
        Navigator::with_fs(
            Box::new(MemFs::with(&files)),
            Path::new(cur),
            None,
            Path::new("/corpus"),
        )
    };
    let nav = mk("/corpus/README.md");
    assert_eq!(nav.entries().len(), 2);
    assert_eq!(nav.sibling(1), Some(PathBuf::from("/corpus/models/A.md")));
    assert_eq!(nav.sibling(-1), None);
    let nav = mk("/corpus/models/A.md");
    assert_eq!(nav.sibling(1), Some(PathBuf::from("/corpus/models/B.md")));
    assert_eq!(nav.sibling(-1), Some(PathBuf::from("/corpus/README.md")));
    let nav = mk("/corpus/models/B.md");
    assert_eq!(nav.sibling(1), None);
    assert_eq!(nav.label(Path::new("/corpus/models/B.md")), "models/B.md");
}

#[test]
fn navigator_history_restores_position() {
    let files = [
        ("/corpus/README.md", INDEX_SRC),
        ("/corpus/models/A.md", "# A\n"),
        ("/corpus/models/B.md", "# B\n"),
    ];
    let mut nav = Navigator::with_fs(
        Box::new(MemFs::with(&files)),
        Path::new("/corpus/README.md"),
        None,
        Path::new("/corpus"),
    );
    let mut here = super::history::Snapshot::of(PathBuf::from("/corpus/README.md"));
    here.top = 30;
    here.cursor = 42;
    here.collapsed = vec!["models".to_string()];
    nav.push(here.clone());
    nav.set_current(PathBuf::from("/corpus/models/A.md"));
    assert_eq!(nav.depth(), 1);
    let back = nav
        .back(super::history::Snapshot::of(PathBuf::from("/corpus/models/A.md")))
        .unwrap();
    assert_eq!(back, here);
    assert!(nav.load(Path::new("/corpus/models/A.md")).is_ok());
    assert!(nav.load(Path::new("/corpus/nope.md")).is_err());
}

// -- a corpus that names itself ----------------------------------------------

/// `corpus/models/A.md`, written *inside* `/corpus`. The leading segment is
/// the corpus's own name, so the path only resolves from the root's parent —
/// which is how such a reference gets written in the first place.
#[test]
fn a_path_prefixed_with_the_root_name_resolves_from_the_root() {
    assert_eq!(
        resolve("corpus/models/A.md", "/corpus/decisions"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/A.md"),
            anchor: None
        }
    );
    // And with an anchor, from the root itself.
    assert_eq!(
        resolve("corpus/models/A.md#deep-heading", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/A.md"),
            anchor: Some("deep-heading".into())
        }
    );
}

/// The fallback must not invent a file. A path that is wrong in the ordinary
/// way stays wrong rather than being retried into something that exists.
#[test]
fn the_root_name_fallback_never_conjures_a_target() {
    assert!(matches!(
        resolve("corpus/models/NOPE.md", "/corpus/decisions"),
        Target::Broken { .. }
    ));
    assert!(matches!(
        resolve("models/NOPE.md", "/corpus"),
        Target::Broken { .. }
    ));
}

/// A real relative path wins: the fallback only runs when nothing was found,
/// so it cannot shadow a directory that genuinely sits inside the corpus.
#[test]
fn a_real_relative_path_is_preferred_to_the_fallback() {
    assert_eq!(
        resolve("models/A.md", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/A.md"),
            anchor: None
        }
    );
}

/// A listing's entries resolve against the directory itself, and a directory
/// without a README opens as another listing rather than being refused.
#[test]
fn a_listing_entry_resolves_against_its_own_directory() {
    // `models` holds A.md and B.md but no README.md.
    assert_eq!(
        resolve("A.md", "/corpus/models"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/A.md"),
            anchor: None
        }
    );
    // A directory that documents itself still wins with its README.
    assert_eq!(
        resolve("models/", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/models/README.md"),
            anchor: None
        }
    );
    // One that does not opens as a listing, trailing slash and all — where it
    // used to be refused as "directory has no README.md".
    assert_eq!(
        resolve("assets/", "/corpus"),
        Target::Doc {
            path: PathBuf::from("/corpus/assets"),
            anchor: None
        }
    );
}