rumdl 0.3.3

A fast Markdown linter and formatter written in Rust
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
//! Regression tests for cross-file rule suppression (inline disable +
//! per-file-ignores), especially across the lint-cache fast path.
//!
//! When `rumdl` gets a lint-cache hit but the workspace-index cache is absent
//! or stale, it rebuilds the cross-file index via `build_file_index_only`. That
//! rebuild must honor the same suppression the normal lint path applies:
//! inline `<!-- rumdl-disable -->` blocks. Separately, `per-file-ignores` must
//! suppress cross-file rules for the ignored file on every path.
//!
//! MD051 (cross-file link-fragment validation) is the live cross-file rule used
//! to exercise these paths.

use std::fs;
use std::path::Path;
use std::process::Command;

/// Run the rumdl binary in `dir` with `args`, returning combined stdout+stderr.
///
/// Pins `RUMDL_CACHE_DIR` to `dir/.rumdl_cache` so the cache location is
/// deterministic regardless of any ambient `RUMDL_CACHE_DIR` in the test
/// environment (which would otherwise redirect or share the workspace index).
fn run(dir: &Path, args: &[&str]) -> String {
    run_with_status(dir, args).1
}

fn run_with_status(dir: &Path, args: &[&str]) -> (bool, String) {
    let exe = env!("CARGO_BIN_EXE_rumdl");
    let output = Command::new(exe)
        .current_dir(dir)
        .env("RUMDL_CACHE_DIR", dir.join(".rumdl_cache"))
        .args(args)
        .output()
        .expect("failed to execute rumdl");
    let mut combined = String::from_utf8_lossy(&output.stdout).to_string();
    combined.push_str(&String::from_utf8_lossy(&output.stderr));
    (output.status.success(), combined)
}

/// Delete only the workspace-index cache, leaving the lint cache intact, so the
/// next run takes the lint-cache-hit + index-rebuild path.
fn delete_workspace_index(dir: &Path) {
    let path = dir.join(".rumdl_cache").join("workspace_index.bin");
    assert!(
        path.exists(),
        "expected workspace index cache at {} after first run",
        path.display()
    );
    fs::remove_file(&path).expect("failed to remove workspace_index.bin");
}

/// b.md provides a heading anchor; a.md links to a *missing* fragment in b.md,
/// which is what MD051's cross-file check flags.
fn write_target(dir: &Path) {
    fs::write(dir.join("b.md"), "# Target\n\n## Real Heading\n").unwrap();
}

#[test]
fn md057_cache_misses_when_a_missing_target_appears() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing target must fail on the first run:\n{first}");
    assert!(first.contains("Relative link 'b.md' does not exist"), "got:\n{first}");

    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(
        second_success,
        "creating the target must invalidate the cached MD057 warning:\n{second}"
    );
    assert!(!second.contains("MD057"), "got:\n{second}");
}

#[test]
fn md057_cache_misses_when_an_existing_target_disappears() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();
    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(first_success, "existing target must pass on the first run:\n{first}");

    fs::remove_file(dir.join("b.md")).unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!second_success, "removing the target must invalidate the cached pass");
    assert!(second.contains("Relative link 'b.md' does not exist"), "got:\n{second}");
}

#[test]
fn md057_file_without_relative_links_keeps_hitting_the_cache() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(first_success, "first run must populate a clean cache entry:\n{first}");

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057", "--verbose"]);
    assert!(second_success, "second run must remain clean:\n{second}");
    assert!(
        second.contains("Cache hit for"),
        "expected a measured cache hit:\n{second}"
    );
    assert!(!second.contains("Cache miss for"), "unexpected cache miss:\n{second}");
}

#[test]
fn md057_cache_misses_when_workspace_link_data_is_unavailable() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();
    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(first_success, "first run must populate both caches:\n{first}");
    delete_workspace_index(dir);

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057", "--verbose"]);
    assert!(
        second_success,
        "fallback lint must preserve the clean verdict:\n{second}"
    );
    assert!(
        second.contains("cross-file dependency state is unavailable"),
        "missing workspace data must force a cache miss:\n{second}"
    );
}

#[test]
fn cache_hit_does_not_require_workspace_data_when_md057_is_disabled() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD013"]);
    assert!(first_success, "first run must populate a clean cache entry:\n{first}");
    assert!(!dir.join(".rumdl_cache/workspace_index.bin").exists());

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD013", "--verbose"]);
    assert!(second_success, "second run must remain clean:\n{second}");
    assert!(
        second.contains("Cache hit for"),
        "MD057-disabled run must hit:\n{second}"
    );
}

#[test]
fn md057_cache_misses_when_target_changes_from_file_to_directory() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();
    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(first_success, "file target must pass:\n{first}");

    fs::remove_file(dir.join("b.md")).unwrap();
    fs::create_dir(dir.join("b.md")).unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057", "--verbose"]);
    assert!(second_success, "directory target is also valid:\n{second}");
    assert!(
        second.contains("cross-file dependency state changed"),
        "target kind change must invalidate the cached identity:\n{second}"
    );
}

/// A link resolves only to the spelling the filesystem stores, so renaming the
/// target to the case the link uses turns a reported link into a working one.
/// On a case-insensitive volume nothing about the file changes except its
/// spelling, which is exactly what the cached verdict has to notice.
#[test]
fn md057_cache_misses_after_a_case_only_rename() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](B.md)\n").unwrap();
    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "b.md", "--enable", "MD057"]);
    assert!(
        !first_success,
        "a link naming a spelling the filesystem does not store must fail:\n{first}"
    );
    assert!(first.contains("Relative link 'B.md' does not exist"), "got:\n{first}");

    fs::rename(dir.join("b.md"), dir.join("B.md")).unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "B.md", "--enable", "MD057", "--verbose"]);
    assert!(
        second_success,
        "renaming the target to the spelling the link uses must invalidate the cached warning:\n{second}"
    );
    assert!(!second.contains("[MD057]"), "got:\n{second}");
    assert!(
        second.contains("cross-file dependency state changed"),
        "a case-only rename must invalidate the cached identity:\n{second}"
    );
}

#[test]
fn md057_cache_tracks_configured_search_paths() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "[MD057]\nsearch-paths = [\"assets\"]\n").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n![image](photo.png)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing search-path target must fail:\n{first}");

    fs::create_dir(dir.join("assets")).unwrap();
    fs::write(dir.join("assets/photo.png"), "image").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(
        second_success,
        "search-path target must invalidate the warning:\n{second}"
    );
}

#[test]
fn md057_cache_tracks_obsidian_attachment_folder() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::create_dir(dir.join(".obsidian")).unwrap();
    fs::write(
        dir.join(".obsidian/app.json"),
        r#"{"attachmentFolderPath":"Attachments"}"#,
    )
    .unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n![image](photo.png)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057", "--flavor", "obsidian"]);
    assert!(!first_success, "missing attachment must fail:\n{first}");

    fs::create_dir(dir.join("Attachments")).unwrap();
    fs::write(dir.join("Attachments/photo.png"), "image").unwrap();

    let (second_success, second) =
        run_with_status(dir, &["check", "a.md", "--enable", "MD057", "--flavor", "obsidian"]);
    assert!(
        second_success,
        "attachment target must invalidate the warning:\n{second}"
    );
}

#[test]
fn md057_cache_tracks_absolute_link_roots() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(
        dir.join(".rumdl.toml"),
        "[MD057]\nabsolute-links = \"relative_to_roots\"\nroots = [\"content\"]\n",
    )
    .unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[guide](/guide.md)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing absolute target must fail:\n{first}");

    fs::create_dir(dir.join("content")).unwrap();
    fs::write(dir.join("content/guide.md"), "# Guide\n").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(second_success, "root target must invalidate the warning:\n{second}");
}

#[test]
fn md057_cache_tracks_mkdocs_docs_dir() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(
        dir.join(".rumdl.toml"),
        "[MD057]\nabsolute-links = \"relative_to_docs\"\n",
    )
    .unwrap();
    fs::write(dir.join("mkdocs.yml"), "site_name: Test\ndocs_dir: docs\n").unwrap();
    fs::create_dir(dir.join("docs")).unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[guide](/guide.md)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing docs-dir target must fail:\n{first}");

    fs::write(dir.join("docs/guide.md"), "# Guide\n").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(second_success, "docs-dir target must invalidate the warning:\n{second}");
}

#[test]
fn md057_cache_tracks_reference_definition_targets() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(
        dir.join("a.md"),
        "# Source\n\n[download][asset]\n\n[asset]: artifact.zip\n",
    )
    .unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing reference target must fail:\n{first}");

    fs::write(dir.join("artifact.zip"), "archive").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(
        second_success,
        "reference target must invalidate the warning:\n{second}"
    );
}

#[test]
fn md057_cache_tracks_checked_frontmatter_targets() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "[MD057]\ncheck-frontmatter = true\n").unwrap();
    fs::write(dir.join("a.md"), "---\nmanual: docs/guide.pdf\n---\n\n# Source\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(!first_success, "missing frontmatter target must fail:\n{first}");

    fs::create_dir(dir.join("docs")).unwrap();
    fs::write(dir.join("docs/guide.pdf"), "guide").unwrap();

    let (second_success, second) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(
        second_success,
        "frontmatter target must invalidate the warning:\n{second}"
    );
}

#[test]
fn md057_fmt_does_not_replay_a_stale_warning() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md)\n").unwrap();

    let (first_success, first) = run_with_status(dir, &["check", "a.md", "--enable", "MD057"]);
    assert!(
        !first_success,
        "missing target must warm a failing cache entry:\n{first}"
    );

    fs::write(dir.join("b.md"), "# Target\n").unwrap();

    let (_, formatted) = run_with_status(dir, &["fmt", "a.md", "b.md", "--enable", "MD057"]);
    assert!(
        !formatted.contains("MD057"),
        "fmt must not replay the stale warning:\n{formatted}"
    );
}

#[test]
fn cache_hit_respects_inline_disable_for_cross_file_rule() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    write_target(dir);
    fs::write(
        dir.join("a.md"),
        "# Source\n\n\
         <!-- rumdl-disable MD051 -->\n\
         [link](b.md#nonexistent)\n\
         <!-- rumdl-enable MD051 -->\n",
    )
    .unwrap();

    // First run: populate caches. MD051 is suppressed by the inline block.
    let first = run(dir, &["check", "."]);
    assert!(
        !first.contains("MD051"),
        "baseline: inline disable should suppress MD051, got:\n{first}"
    );

    // Drop only the workspace-index cache, forcing the index-rebuild path.
    delete_workspace_index(dir);

    // Second run: lint-cache hit + index rebuild must still honor the disable.
    let second = run(dir, &["check", "."]);
    assert!(
        !second.contains("MD051"),
        "MD051 must stay suppressed on a cache hit (inline disable), got:\n{second}"
    );
}

/// The fragment of a reference definition, used or not, is checked against the
/// file it names, and the warning sits on the definition's destination.
#[test]
fn md051_checks_reference_definition_fragments_without_cache() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    write_target(dir);
    fs::write(
        dir.join("a.md"),
        "# Source\n\n## Local\n\nSee [the target][t].\n\n\
         [t]: b.md#nonexistent\n[unused]: #locl\n[fine]: b.md#real-heading\n[here]: #local\n",
    )
    .unwrap();

    let (success, out) = run_with_status(dir, &["check", ".", "--no-cache", "--isolated"]);
    assert!(!success, "broken definition fragments must fail:\n{out}");
    let mut md051: Vec<&str> = out.lines().filter(|line| line.contains("[MD051]")).collect();
    md051.sort_unstable();
    assert_eq!(
        md051,
        [
            "a.md:7:6: [MD051] Link fragment 'nonexistent' not found in 'b.md'",
            "a.md:8:11: [MD051] Link anchor '#locl' does not exist in document headings (did you mean '#local'?)",
        ],
        "{out}"
    );
}

/// A reference definition's fragment is cached in the index like an inline
/// link's: the rebuilt index after a lint-cache hit still reports it, and a
/// target gaining the heading clears it.
#[test]
fn md051_reference_definition_fragment_survives_the_cache() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    write_target(dir);
    fs::write(dir.join("a.md"), "# Source\n\nSee [later][t].\n\n[t]: b.md#later\n").unwrap();

    let first = run(dir, &["check", "."]);
    assert!(first.contains("Link fragment 'later' not found in 'b.md'"), "{first}");

    delete_workspace_index(dir);
    let second = run(dir, &["check", "."]);
    assert!(
        second.contains("Link fragment 'later' not found in 'b.md'"),
        "a lint-cache hit must rebuild the definition's index entry:\n{second}"
    );

    fs::write(dir.join("b.md"), "# Target\n\n## Later\n").unwrap();
    let (third_success, third) = run_with_status(dir, &["check", "."]);
    assert!(third_success, "the new heading must clear the warning:\n{third}");
}

#[test]
fn per_file_ignores_suppresses_cross_file_rule_without_cache() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "[per-file-ignores]\n\"a.md\" = [\"MD051\"]\n").unwrap();
    write_target(dir);
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md#nonexistent)\n").unwrap();

    // --no-cache isolates this from the lint cache: per-file-ignores alone must
    // suppress the cross-file rule for a.md.
    let out = run(dir, &["check", ".", "--no-cache"]);
    assert!(
        !out.contains("MD051"),
        "per-file-ignores must suppress cross-file MD051 for a.md, got:\n{out}"
    );
}

#[test]
fn per_file_ignores_suppresses_cross_file_rule_on_cache_hit() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "[per-file-ignores]\n\"a.md\" = [\"MD051\"]\n").unwrap();
    write_target(dir);
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md#nonexistent)\n").unwrap();

    // First run populates caches.
    let _ = run(dir, &["check", "."]);
    delete_workspace_index(dir);

    // Cache-hit + index rebuild must still honor per-file-ignores.
    let out = run(dir, &["check", "."]);
    assert!(
        !out.contains("MD051"),
        "per-file-ignores must suppress cross-file MD051 on a cache hit, got:\n{out}"
    );
}

/// The index cache is replaced by an atomic rename, so a rewrite gives the
/// file a new inode.
#[cfg(unix)]
fn workspace_index_inode(dir: &Path) -> u64 {
    use std::os::unix::fs::MetadataExt;
    fs::metadata(dir.join(".rumdl_cache").join("workspace_index.bin"))
        .expect("workspace index cache must exist")
        .ino()
}

#[cfg(unix)]
#[test]
fn workspace_index_cache_is_kept_when_no_file_changed() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    write_target(dir);
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md#nonexistent)\n").unwrap();

    let first = run(dir, &["check", "."]);
    assert!(first.contains("MD051"), "broken fragment must be reported:\n{first}");
    let saved = workspace_index_inode(dir);

    // Every file hits both caches: the cross-file verdict comes from the
    // loaded index alone, and that index is left as it was.
    let second = run(dir, &["check", "."]);
    assert_eq!(
        second.matches("MD051").count(),
        first.matches("MD051").count(),
        "got:\n{second}"
    );
    assert_eq!(
        workspace_index_inode(dir),
        saved,
        "an unchanged workspace must not rewrite the index cache"
    );
}

#[cfg(unix)]
#[test]
fn workspace_index_cache_is_saved_when_files_change() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path();

    fs::write(dir.join(".rumdl.toml"), "").unwrap();
    write_target(dir);
    fs::write(dir.join("a.md"), "# Source\n\n[link](b.md#added-heading)\n").unwrap();
    fs::write(dir.join("c.md"), "# Other\n").unwrap();

    let first = run(dir, &["check", "."]);
    assert!(first.contains("MD051"), "missing heading must be reported:\n{first}");
    let saved = workspace_index_inode(dir);

    // An edited target updates the index, and the next fully cached run
    // reads the saved update.
    fs::write(dir.join("b.md"), "# Target\n\n## Added Heading\n").unwrap();
    let edited = run(dir, &["check", "."]);
    assert!(!edited.contains("MD051"), "the added heading must resolve:\n{edited}");
    let after_edit = workspace_index_inode(dir);
    assert_ne!(after_edit, saved, "an edited file must be saved to the index cache");
    let cached = run(dir, &["check", "."]);
    assert!(
        !cached.contains("MD051"),
        "the saved index must hold the edit:\n{cached}"
    );

    // A deleted file is pruned from the index, which is then saved too.
    fs::remove_file(dir.join("c.md")).unwrap();
    let pruned = run(dir, &["check", "."]);
    assert!(!pruned.contains("MD051"), "got:\n{pruned}");
    assert_ne!(
        workspace_index_inode(dir),
        after_edit,
        "a pruned file must be saved to the index cache"
    );
}