musefs-core 1.2.0

Orchestration for musefs: virtual tree, tag resolution, and scanning.
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
mod common;

use std::collections::BTreeMap;
use std::time::Duration;

use musefs_core::{Mode, MountConfig, Musefs, scan_directory};
use musefs_db::{Db, Tag};

use common::corpus::{CorpusParams, Format, Target, prepare};
use proptest::prelude::*;

/// A small single-album FLAC corpus with `n` tracks. The returned `Target` owns
/// the tempdir — keep it alive for the whole test.
fn small_corpus(n: usize) -> Target {
    prepare(&CorpusParams::single(Format::Flac, 1, n))
}

fn config() -> MountConfig {
    MountConfig {
        template: "$artist/$album/$title".into(),
        fallbacks: BTreeMap::new(),
        default_fallback: "Unknown".into(),
        mode: Mode::Synthesis,
        poll_interval: Duration::ZERO,
        case_insensitive: false,
        read_ahead_budget: 64 * 1024 * 1024,
        read_ahead_prefetch: false,
        skip_on_missing: false,
    }
}

fn config_ci() -> MountConfig {
    MountConfig {
        case_insensitive: true,
        read_ahead_budget: 64 * 1024 * 1024,
        read_ahead_prefetch: false,
        skip_on_missing: false,
        ..config()
    }
}

fn config_skip() -> MountConfig {
    MountConfig {
        skip_on_missing: true,
        ..config()
    }
}

/// (rendered tree path -> inode) for every FILE, walking from root. Tests compare
/// only the PATH KEYS across two independent Musefs instances: their inode-allocator
/// histories differ, so inode numbers legitimately differ between instances. (Inode
/// stability within one instance across refreshes is gated by the Stage B B5 debug_assert.)
fn tree_fingerprint(fs: &Musefs) -> BTreeMap<String, u64> {
    let mut out = BTreeMap::new();
    let mut stack = vec![(1u64, String::new())];
    while let Some((ino, prefix)) = stack.pop() {
        for (name, child, is_dir) in fs.readdir(ino).unwrap() {
            let path = if prefix.is_empty() {
                name.clone()
            } else {
                format!("{prefix}/{name}")
            };
            if is_dir {
                stack.push((child, path));
            } else {
                out.insert(path, child);
            }
        }
    }
    out
}

#[test]
fn incremental_refresh_matches_full_rebuild_over_edits() {
    let target = small_corpus(8);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();

    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();

    let writer = Db::open(&db_path).unwrap();
    let ids: Vec<i64> = writer.list_tracks().unwrap().iter().map(|t| t.id).collect();

    writer
        .replace_tags(
            ids[0],
            &[Tag::new("ARTIST", "Zed", 0), Tag::new("TITLE", "moved", 0)],
        )
        .unwrap();
    fs.poll_refresh().unwrap();
    writer
        .replace_tags(ids[1], &[Tag::new("ALBUM", "NewAlbum", 0)])
        .unwrap();
    fs.poll_refresh().unwrap();
    writer.delete_track(ids[2]).unwrap();
    fs.poll_refresh().unwrap();

    let reference = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();

    assert_eq!(
        tree_fingerprint(&fs).keys().collect::<Vec<_>>(),
        tree_fingerprint(&reference).keys().collect::<Vec<_>>(),
        "incremental and full-rebuild paths must match"
    );
}

#[test]
fn incremental_skip_on_missing_matches_full_rebuild_over_key_loss_and_gain() {
    let target = small_corpus(4);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();

    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config_skip()).unwrap();

    let writer = Db::open(&db_path).unwrap();
    let ids: Vec<i64> = writer.list_tracks().unwrap().iter().map(|t| t.id).collect();

    let full = |label: &str, fs: &Musefs| {
        let reference = Musefs::open(Db::open(&db_path).unwrap(), config_skip()).unwrap();
        let inc = tree_fingerprint(fs);
        assert_eq!(
            inc.keys().collect::<Vec<_>>(),
            tree_fingerprint(&reference).keys().collect::<Vec<_>>(),
            "{label}: incremental must match full rebuild"
        );
        inc.len()
    };

    assert_eq!(full("baseline", &fs), 4);

    // Key loss: drop TITLE (a top-level template field) from ids[0]. Full rebuild
    // skips it, so the incremental path must drop it too.
    writer
        .replace_tags(
            ids[0],
            &[Tag::new("ARTIST", "Zed", 0), Tag::new("ALBUM", "Al", 0)],
        )
        .unwrap();
    fs.poll_refresh().unwrap();
    assert_eq!(full("key loss", &fs), 3);

    // Key gain: restore TITLE; the track must reappear.
    writer
        .replace_tags(
            ids[0],
            &[
                Tag::new("ARTIST", "Zed", 0),
                Tag::new("ALBUM", "Al", 0),
                Tag::new("TITLE", "Back", 0),
            ],
        )
        .unwrap();
    fs.poll_refresh().unwrap();
    assert_eq!(full("key gain", &fs), 4);
}

#[test]
fn case_insensitive_refresh_merges_and_matches_full_rebuild() {
    let target = small_corpus(2);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();

    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();

    // Make the two tracks' artists differ only by case (same album so they share
    // a parent): under folding the artist dir must MERGE.
    let writer = Db::open(&db_path).unwrap();
    let ids: Vec<i64> = writer.list_tracks().unwrap().iter().map(|t| t.id).collect();
    writer
        .replace_tags(
            ids[0],
            &[
                Tag::new("ARTIST", "Foo", 0),
                Tag::new("ALBUM", "Al", 0),
                Tag::new("TITLE", "One", 0),
            ],
        )
        .unwrap();
    writer
        .replace_tags(
            ids[1],
            &[
                Tag::new("ARTIST", "foo", 0),
                Tag::new("ALBUM", "Al", 0),
                Tag::new("TITLE", "Two", 0),
            ],
        )
        .unwrap();

    let fs = Musefs::open(Db::open(&db_path).unwrap(), config_ci()).unwrap();

    // Exactly one top-level artist directory (the merged "Foo"/"foo").
    let fp = tree_fingerprint(&fs);
    let top_dirs: std::collections::BTreeSet<String> = fp
        .keys()
        .map(|p| p.split('/').next().unwrap().to_string())
        .collect();
    assert_eq!(
        top_dirs.len(),
        1,
        "case-variant artists must merge into one dir"
    );

    // An external edit is still picked up - incremental is bypassed, so this goes
    // through a full folded rebuild - and the result matches a fresh folded build.
    writer
        .replace_tags(
            ids[1],
            &[
                Tag::new("ARTIST", "foo", 0),
                Tag::new("ALBUM", "Al", 0),
                Tag::new("TITLE", "Renamed", 0),
            ],
        )
        .unwrap();
    fs.poll_refresh().unwrap();

    let reference = Musefs::open(Db::open(&db_path).unwrap(), config_ci()).unwrap();
    assert_eq!(
        tree_fingerprint(&fs).keys().collect::<Vec<_>>(),
        tree_fingerprint(&reference).keys().collect::<Vec<_>>(),
        "case-insensitive refresh (full rebuild) must match a fresh folded build"
    );
}

#[test]
fn non_render_column_edit_is_noop_refresh() {
    // Re-running scan_directory over an unchanged corpus bumps data_version but
    // changes no rendered path, so the tree must be identical before and after.
    let target = small_corpus(4);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();
    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();

    // Re-scan: touches updated_at (data_version bump) but no rendered path changes.
    let db2 = Db::open(&db_path).unwrap();
    scan_directory(&db2, &corpus).unwrap();

    let before = tree_fingerprint(&fs);
    let rebuilt = fs.poll_refresh().unwrap();
    let after = tree_fingerprint(&fs);
    assert_eq!(before, after, "non-render edit must not change the tree");
    let _ = rebuilt; // tree-equality is the gate, not the bool
}

#[test]
fn format_only_change_notifies_old_inode() {
    let target = small_corpus(2);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();
    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();

    let writer = Db::open(&db_path).unwrap();
    let id = writer.list_tracks().unwrap()[0].id;
    let old_ino = fs.lookup_track_inode_for_test(id).unwrap();

    // Force a format change directly (no tags trigger), bumping data_version.
    writer
        .set_format_for_test(id, musefs_db::Format::Mp3)
        .unwrap();

    let mut notified = Vec::new();
    fs.poll_refresh_notify(|ino| notified.push(ino)).unwrap();

    assert!(
        notified.contains(&old_ino),
        "format-only move must invalidate the old inode (extension changed)"
    );
}

#[derive(Clone, Debug)]
enum Op {
    Retag(usize, String, String), // retag the i-th LIVE track (forces collisions -> moves)
    Delete(usize),                // delete the i-th LIVE track (remove-cascade + prune)
    Add(String, String),          // add a brand-new DB track row (added-side propagation)
}

#[test]
fn apply_failure_falls_back_to_full_rebuild() {
    let target = small_corpus(4);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();
    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
    let writer = Db::open(&db_path).unwrap();
    let id = writer.list_tracks().unwrap()[0].id;
    writer
        .replace_tags(id, &[Tag::new("TITLE", "moved", 0)])
        .unwrap();

    fs.force_apply_failure_for_test(true);
    fs.poll_refresh().unwrap();

    let reference = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
    // `tree_fingerprint` is a BTreeMap, so `into_keys` already yields sorted keys.
    let fs_keys: Vec<String> = tree_fingerprint(&fs).into_keys().collect();
    let ref_keys: Vec<String> = tree_fingerprint(&reference).into_keys().collect();
    assert_eq!(
        fs_keys, ref_keys,
        "fallback full rebuild must produce a tree identical to a fresh open"
    );
}

#[test]
fn changelog_gap_falls_back_to_full_rebuild() {
    let target = small_corpus(4);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();
    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();

    let writer = Db::open(&db_path).unwrap();
    let ids: Vec<i64> = writer.list_tracks().unwrap().iter().map(|t| t.id).collect();
    writer
        .replace_tags(ids[0], &[Tag::new("TITLE", "moved-by-gap", 0)])
        .unwrap();
    // Simulate the ring having pruned past the mount's watermark: drop every
    // retained row. The next poll must detect the gap and full-rebuild.
    let max_seq = writer.changelog_since(0).unwrap().max_seq;
    writer.delete_changelog_through_for_test(max_seq).unwrap();

    assert!(fs.poll_refresh().unwrap());
    assert_eq!(
        fs.gap_fallbacks_for_test(),
        1,
        "a fully truncated ring must be detected as a gap"
    );
    let reference = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
    assert_eq!(
        tree_fingerprint(&fs).into_keys().collect::<Vec<_>>(),
        tree_fingerprint(&reference).into_keys().collect::<Vec<_>>(),
        "gap fallback must produce a tree identical to a fresh open"
    );
}

#[test]
fn removed_track_is_pruned_and_refresh_recovers_after_gap() {
    // After a gap-driven full rebuild, subsequent incremental refreshes still
    // work: the watermark re-anchors to the ring and deletes propagate.
    let target = small_corpus(4);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();
    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
    let writer = Db::open(&db_path).unwrap();
    let ids: Vec<i64> = writer.list_tracks().unwrap().iter().map(|t| t.id).collect();

    let max_seq = writer.changelog_since(0).unwrap().max_seq;
    writer.delete_changelog_through_for_test(max_seq).unwrap();
    writer.delete_track(ids[0]).unwrap();
    assert!(fs.poll_refresh().unwrap());
    // Truncation-then-edit leaves min_seq == last_seq + 1: contiguous, NOT a
    // gap. Both polls must stay on the incremental path (and the second one
    // starts from a retained, mutated-in-place snapshot).
    assert_eq!(
        fs.gap_fallbacks_for_test(),
        0,
        "an adjacent (min_seq == last_seq + 1) ring read is not a gap"
    );

    writer.delete_track(ids[1]).unwrap();
    assert!(fs.poll_refresh().unwrap());
    assert_eq!(fs.gap_fallbacks_for_test(), 0);

    let reference = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
    assert_eq!(
        tree_fingerprint(&fs).into_keys().collect::<Vec<_>>(),
        tree_fingerprint(&reference).into_keys().collect::<Vec<_>>()
    );
}

#[test]
fn pruned_ring_prefix_is_a_gap_and_full_rebuild_recovers_lost_change() {
    // A TRUE prune gap: the ring still holds rows, but its window starts past
    // the watermark + 1. The pruned prefix held a real change; only the gap
    // path (full rebuild) can recover it.
    let target = small_corpus(4);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();
    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
    let writer = Db::open(&db_path).unwrap();
    let ids: Vec<i64> = writer.list_tracks().unwrap().iter().map(|t| t.id).collect();

    let last_seq = writer.changelog_since(0).unwrap().max_seq; // == fs watermark
    writer
        .replace_tags(ids[0], &[Tag::new("TITLE", "lost-in-pruned-prefix", 0)])
        .unwrap();
    let x_rows = writer.changelog_since(last_seq).unwrap().max_seq;
    writer
        .replace_tags(ids[1], &[Tag::new("TITLE", "still-in-ring", 0)])
        .unwrap();
    // Prune exactly X's rows: the ring now starts at last_seq + (x_rows-last_seq) + 1
    // > last_seq + 1, and X's change is no longer derivable from it.
    writer.delete_changelog_through_for_test(x_rows).unwrap();

    assert!(fs.poll_refresh().unwrap());
    assert_eq!(
        fs.gap_fallbacks_for_test(),
        1,
        "a pruned-prefix ring (min_seq > last_seq + 1) must be detected as a gap"
    );
    let reference = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
    assert_eq!(
        tree_fingerprint(&fs).into_keys().collect::<Vec<_>>(),
        tree_fingerprint(&reference).into_keys().collect::<Vec<_>>(),
        "the gap rebuild must recover the change lost from the pruned prefix"
    );
}

#[test]
fn empty_ring_with_zero_watermark_polls_incremental() {
    // A data_version bump with no changelog rows and no watermark (the ring was
    // empty at open) is NOT a gap: nothing can have been missed.
    let target = small_corpus(2);
    let db_path = target.db_path.clone();
    let corpus = target.corpus_dir.clone();
    let db = Db::open(&db_path).unwrap();
    scan_directory(&db, &corpus).unwrap();
    let pre = Db::open(&db_path).unwrap();
    let max_seq = pre.changelog_since(0).unwrap().max_seq;
    pre.delete_changelog_through_for_test(max_seq).unwrap();

    // Opened on an empty ring: watermark 0.
    let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
    // An orphan art insert bumps data_version without touching `tracks`, so the
    // ring stays empty.
    let writer = Db::open(&db_path).unwrap();
    writer
        .upsert_art(&musefs_db::NewArt {
            mime: "image/png".into(),
            width: None,
            height: None,
            data: vec![0u8; 8],
        })
        .unwrap();

    assert!(fs.poll_refresh().unwrap());
    assert_eq!(
        fs.gap_fallbacks_for_test(),
        0,
        "empty ring + zero watermark must stay on the incremental path"
    );
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(64))]
    #[test]
    fn incremental_equivalent_to_full_under_random_edits(
        ops in proptest::collection::vec(
            prop_oneof![
                (0usize..8, "[A-B]", "[x-y]").prop_map(|(i, a, t)| Op::Retag(i, a, t)),
                (0usize..8).prop_map(Op::Delete),
                ("[A-B]", "[x-y]").prop_map(|(a, t)| Op::Add(a, t)),
            ], 0..24)
    ) {
        let target = small_corpus(6);
        let db_path = target.db_path.clone();
        let corpus = target.corpus_dir.clone();
        let db = Db::open(&db_path).unwrap();
        scan_directory(&db, &corpus).unwrap();
        let fs = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
        let writer = Db::open(&db_path).unwrap();
        let mut add_seq = 0u32;

        for op in ops {
            // Re-query the live id set each step (deletes/adds change it).
            let live: Vec<i64> = writer.list_tracks().unwrap().iter().map(|t| t.id).collect();
            // ARTIST is fixed to "X" throughout; album/title carry the random
            // variation. Artist-level collisions are out of scope here — the
            // album+title space already drives disambiguation and path moves.
            match op {
                Op::Retag(i, album, title) if !live.is_empty() => {
                    writer.replace_tags(live[i % live.len()], &[
                        Tag::new("ARTIST", "X", 0),
                        Tag::new("ALBUM", &album, 0),
                        Tag::new("TITLE", &title, 0),
                    ]).unwrap();
                }
                Op::Delete(i) if !live.is_empty() => {
                    writer.delete_track(live[i % live.len()]).unwrap();
                }
                Op::Add(album, title) => {
                    add_seq += 1;
                    // DB-only track: tree-building never reads the backing file, and
                    // both fs and reference read the same DB, so equivalence holds.
                    let new = musefs_db::NewTrack {
                        backing_path: format!("/virt/added-{add_seq}.flac"),
                        format: musefs_db::Format::Flac,
                        audio_offset: 0, audio_length: 1, backing_size: 1, backing_mtime_ns: 0, backing_ctime_ns: 0,
                    };
                    // Surface DB errors instead of vacuously skipping the op.
                    let id = writer.upsert_track(&new).unwrap();
                    writer
                        .replace_tags(
                            id,
                            &[
                                Tag::new("ARTIST", "X", 0),
                                Tag::new("ALBUM", &album, 0),
                                Tag::new("TITLE", &title, 0),
                            ],
                        )
                        .unwrap();
                }
                _ => {}
            }
            fs.poll_refresh().unwrap();
            let reference = Musefs::open(Db::open(&db_path).unwrap(), config()).unwrap();
            let incr_keys: Vec<String> = tree_fingerprint(&fs).into_keys().collect();
            let full_keys: Vec<String> = tree_fingerprint(&reference).into_keys().collect();
            prop_assert_eq!(incr_keys, full_keys);
        }
    }
}

// A ctime-only change (mtime forged back after an in-place same-size rewrite)
// must NOT be skipped as "unchanged": revalidate re-probes it.
#[test]
fn revalidate_reprobes_on_ctime_only_change() {
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("a.flac");
    common::write_flac(&src, &["TITLE=Old"], &[0xAB; 4096]);
    let db_path = dir.path().join("m.db");
    {
        let db = Db::open(&db_path).unwrap();
        scan_directory(&db, dir.path()).unwrap();
    }
    let original_modified = std::fs::metadata(&src).unwrap().modified().unwrap();

    // Rewrite in place (same size, new tag), then forge mtime back. ctime moved.
    common::write_flac(&src, &["TITLE=New"], &[0xCD; 4096]);
    let f = std::fs::OpenOptions::new().write(true).open(&src).unwrap();
    f.set_times(std::fs::FileTimes::new().set_modified(original_modified))
        .unwrap();
    drop(f);

    let db = Db::open(&db_path).unwrap();
    let stats = musefs_core::revalidate(&db, dir.path()).unwrap();
    assert_eq!(stats.updated, 1, "ctime-only change must be re-probed");
}

#[test]
fn revalidate_changed_file_refreshes_layer_a_preserves_layer_b() {
    let dir = tempfile::tempdir().unwrap();
    let db = Db::open_in_memory().unwrap();
    let path = dir.path().join("a.flac");
    common::write_flac(&path, &["TITLE=A"], &[0xAA; 30]);
    scan_directory(&db, dir.path()).unwrap();
    let id = db.list_tracks().unwrap()[0].id;
    db.replace_tags(id, &[Tag::new("title", "Curated", 0)])
        .unwrap();

    common::write_flac(&path, &["TITLE=B-on-disk"], &[0xBB; 40]);
    let stats = musefs_core::revalidate(&db, dir.path()).unwrap();

    assert_eq!(stats.updated, 1);
    assert_eq!(stats.pruned, 0);
    let tags = db.get_tags(id).unwrap();
    assert_eq!(tags[0].value, "Curated");
}

#[test]
fn revalidate_ignores_new_files() {
    let dir = tempfile::tempdir().unwrap();
    let db = Db::open_in_memory().unwrap();
    common::write_flac(&dir.path().join("a.flac"), &["TITLE=A"], &[0xAA; 30]);
    scan_directory(&db, dir.path()).unwrap();

    common::write_flac(&dir.path().join("b.flac"), &["TITLE=B"], &[0xBB; 40]);
    let stats = musefs_core::revalidate(&db, dir.path()).unwrap();

    assert_eq!(stats.updated, 0);
    assert_eq!(db.list_tracks().unwrap().len(), 1);
}

#[test]
fn revalidate_prunes_only_with_flag() {
    let dir = tempfile::tempdir().unwrap();
    let db = Db::open_in_memory().unwrap();
    let path = dir.path().join("a.flac");
    common::write_flac(&path, &["TITLE=A"], &[0xAA; 30]);
    scan_directory(&db, dir.path()).unwrap();
    std::fs::remove_file(&path).unwrap();

    let stats = musefs_core::revalidate(&db, dir.path()).unwrap();
    assert_eq!(stats.pruned, 0);
    assert_eq!(db.list_tracks().unwrap().len(), 1);

    let opts = musefs_core::ScanOptions {
        prune: true,
        ..Default::default()
    };
    let stats = musefs_core::revalidate_with(&db, dir.path(), &opts).unwrap();
    assert_eq!(stats.pruned, 1);
    assert_eq!(db.list_tracks().unwrap().len(), 0);
}

#[test]
fn revalidate_backfill_does_not_clobber_tags() {
    let dir = tempfile::tempdir().unwrap();
    let db = Db::open_in_memory().unwrap();
    let path = dir.path().join("a.flac");
    common::write_flac(&path, &["TITLE=OnDisk"], &[0xAA; 30]);
    scan_directory(&db, dir.path()).unwrap();
    let id = db.list_tracks().unwrap()[0].id;
    db.replace_tags(id, &[Tag::new("title", "Curated", 0)])
        .unwrap();
    db.set_structural_blocks(id, &[]).unwrap();

    let stats = musefs_core::revalidate(&db, dir.path()).unwrap();
    assert_eq!(stats.updated, 1);
    let tags = db.get_tags(id).unwrap();
    assert_eq!(tags[0].value, "Curated");
    assert!(!db.get_structural_blocks(id).unwrap().is_empty());
}