grr-cli 1.0.1

Google tools from the terminal, at maximum performance: 401 methods across 14 Google APIs in one generated command tree, zero-config, over HTTP/3
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
//! Skill install provenance and self-migration.
//!
//! `grr skills install` records what it wrote in a manifest at
//! `<target>/.grr-skills.json`: the package version plus, per packaged
//! file, the sha256 of the packaged content it installed (the 3-way merge
//! base) and the sha256 of the exact bytes it wrote (what "untouched by
//! the user" means). A copy of each packaged file is kept under
//! `<target>/.grr-backup/<relpath>` so the base can be reconstructed.
//!
//! Base-content strategy: we store the base bytes, not "re-derive them
//! from the old packaged set". A binary only carries the CURRENT packaged
//! files (`include_str!`), so re-deriving would be impossible after
//! jumping more than one version — the backup is the only correct base
//! when the recorded version is several releases behind. The backup is
//! refreshed on every write, so it always holds the packaged content of
//! the recorded version.
//!
//! Two entry points:
//! * [`sync_into`] — the install/upgrade pass (explicit `grr skills install`
//!   or the lazy auto-migration).
//! * [`migrate_dir`] — the opportunistic pass. It is LAZY: one stat for
//!   the manifest, a tiny JSON compare, and only if the recorded version
//!   differs does anything hash a file.
//!
//! The manifest and the backup are per target directory. The `--claude`
//! mirror (`~/.claude/skills`) therefore keeps its own, so the two trees
//! never share provenance and either one can be migrated independently.

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

/// Provenance manifest, written beside the installed files.
pub(crate) const MANIFEST_NAME: &str = ".grr-skills.json";

/// Packaged-content snapshots (the 3-way merge base) live here.
pub(crate) const BACKUP_DIR: &str = ".grr-backup";

/// Suffix of the packaged-new copy left beside a conflicted file.
pub(crate) const INCOMING_SUFFIX: &str = ".grr-incoming";

#[cfg(test)]
thread_local! {
    /// Counts `sha256_hex` calls so a test can prove the version fast path
    /// does no hashing at all.
    static HASH_CALLS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

#[cfg(test)]
pub(crate) fn reset_hash_calls() {
    HASH_CALLS.with(|calls| calls.set(0));
}

#[cfg(test)]
pub(crate) fn hash_calls() -> u64 {
    HASH_CALLS.with(std::cell::Cell::get)
}

/// Lowercase hex sha256. Pure Rust; no extra digest-to-hex dependency.
pub(crate) fn sha256_hex(bytes: &[u8]) -> String {
    #[cfg(test)]
    HASH_CALLS.with(|calls| calls.set(calls.get() + 1));
    let digest = Sha256::digest(bytes);
    let mut out = String::with_capacity(digest.len() * 2);
    for byte in digest.as_slice() {
        use std::fmt::Write as _;
        let _ = write!(out, "{byte:02x}");
    }
    out
}

/// One file's recorded provenance.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct FileEntry {
    /// sha256 of the packaged content at the recorded `version` (the merge base).
    pub base_sha256: String,
    /// sha256 of the exact bytes grr last wrote for this path.
    pub written_sha256: String,
}

/// The set of files grr owns under one target directory.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct Manifest {
    pub version: String,
    #[serde(default)]
    pub files: BTreeMap<String, FileEntry>,
}

impl Manifest {
    pub(crate) fn path(target: &Path) -> PathBuf {
        target.join(MANIFEST_NAME)
    }

    /// Read the manifest, or `None` when absent or unparseable. A corrupt
    /// manifest is treated as "not installed" — never as a reason to fail.
    pub(crate) fn load(target: &Path) -> Option<Manifest> {
        let text = fs::read_to_string(Self::path(target)).ok()?;
        serde_json::from_str(&text).ok()
    }

    pub(crate) fn save(&self, target: &Path) -> Result<()> {
        let path = Self::path(target);
        let body = serde_json::to_string_pretty(self).context("serializing the skills manifest")?;
        write_file(&path, &body)
    }
}

/// Whether [`sync_into`] is the explicit install command or the lazy
/// version-change pass. The only behavioral difference: the lazy pass
/// never resurrects a file the user deleted.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SyncMode {
    Explicit,
    Migrate,
}

#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct InstallReport {
    pub target: PathBuf,
    /// Newly written (the path did not exist).
    pub installed: Vec<String>,
    /// Replaced with the new packaged content (untouched local, safe path).
    pub updated: Vec<String>,
    /// A 3-way merge combined local edits with the new packaged content.
    pub merged: Vec<String>,
    /// Byte-identical to the packaged copy, left untouched.
    pub unchanged: Vec<String>,
    /// Left alone, with the reason.
    pub skipped: Vec<Skipped>,
    /// Left untouched; the packaged-new content was written beside them.
    pub conflicts: Vec<String>,
}

#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Skipped {
    pub path: String,
    pub reason: String,
}

impl InstallReport {
    fn wrote_anything(&self) -> bool {
        !self.installed.is_empty()
            || !self.updated.is_empty()
            || !self.merged.is_empty()
            || !self.conflicts.is_empty()
    }
}

/// The install/upgrade pass over one target directory.
///
/// Per file, given the packaged-new content `P`, the recorded base `B` (a
/// backup snapshot) and the local file `L`:
/// * `L == P` → current, no write;
/// * `--force` → overwrite with `P`, no merge (the flag's original meaning);
/// * `L` is exactly what grr last wrote and that write was pure `P_old`
///   (`written == base`) → safe auto-update to `P`;
/// * otherwise → 3-way merge `diffy::merge(B, L, P)`: clean merges write
///   the result, conflicts leave `L` untouched and drop `P` beside it as
///   `<name>.grr-incoming`.
///
/// A file with no manifest entry is not ours: it is skipped, never merged
/// or overwritten (unless `--force` says override everything).
pub(crate) fn sync_into(
    target: &Path,
    packaged: &[(&str, &str)],
    version: &str,
    force: bool,
    mode: SyncMode,
) -> Result<InstallReport> {
    let mut report = InstallReport {
        target: target.to_path_buf(),
        ..InstallReport::default()
    };
    let loaded = Manifest::load(target);
    let mut manifest = loaded.clone().unwrap_or_default();
    let mut dirty = false;

    for &(rel, contents) in packaged {
        // Same spec guard as the writer: a malformed embed is skipped, not
        // installed into the wrong directory.
        if let Err(reason) = super::check_skill_entry(rel, contents) {
            report.skipped.push(Skipped {
                path: rel.to_string(),
                reason,
            });
            continue;
        }

        let path = target.join(rel);
        let packaged_sha = sha256_hex(contents.as_bytes());
        let entry = manifest.files.get(rel).cloned();

        let local = match fs::read_to_string(&path) {
            Ok(local) => Some(local),
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
            Err(err) => {
                report.skipped.push(Skipped {
                    path: rel.to_string(),
                    reason: format!("cannot read existing file: {err}"),
                });
                continue;
            }
        };

        let Some(local) = local else {
            // Absent. grr owns it (entry present) but the user deleted it:
            // the lazy pass respects that; an explicit install restores it.
            if mode == SyncMode::Migrate && entry.is_some() {
                report.skipped.push(Skipped {
                    path: rel.to_string(),
                    reason: "removed locally; rerun `grr skills install` to restore".to_string(),
                });
                continue;
            }
            write_file(&path, contents)?;
            report.installed.push(rel.to_string());
            manifest.files.insert(
                rel.to_string(),
                FileEntry {
                    base_sha256: packaged_sha.clone(),
                    written_sha256: packaged_sha.clone(),
                },
            );
            write_backup(target, rel, contents)?;
            dirty = true;
            continue;
        };

        if local == contents {
            report.unchanged.push(rel.to_string());
            if entry
                .as_ref()
                .is_none_or(|e| e.base_sha256 != packaged_sha || e.written_sha256 != packaged_sha)
            {
                manifest.files.insert(
                    rel.to_string(),
                    FileEntry {
                        base_sha256: packaged_sha.clone(),
                        written_sha256: packaged_sha.clone(),
                    },
                );
                write_backup(target, rel, contents)?;
                dirty = true;
            }
            continue;
        }

        if force {
            write_file(&path, contents)?;
            report.updated.push(rel.to_string());
            manifest.files.insert(
                rel.to_string(),
                FileEntry {
                    base_sha256: packaged_sha.clone(),
                    written_sha256: packaged_sha.clone(),
                },
            );
            write_backup(target, rel, contents)?;
            dirty = true;
            continue;
        }

        let Some(entry) = entry else {
            // Different content, no provenance: not ours. Never touch it.
            report.skipped.push(Skipped {
                path: rel.to_string(),
                reason:
                    "exists with different contents; not installed by grr — pass --force to replace"
                        .to_string(),
            });
            continue;
        };

        let local_sha = sha256_hex(local.as_bytes());

        // Safe auto-update: the local bytes are exactly what grr last wrote,
        // and that write was pure packaged content (neither a merge result
        // nor a user edit).
        if local_sha == entry.written_sha256 && entry.written_sha256 == entry.base_sha256 {
            write_file(&path, contents)?;
            report.updated.push(rel.to_string());
            manifest.files.insert(
                rel.to_string(),
                FileEntry {
                    base_sha256: packaged_sha.clone(),
                    written_sha256: packaged_sha.clone(),
                },
            );
            write_backup(target, rel, contents)?;
            dirty = true;
            continue;
        }

        let Some(base) = read_backup(target, rel) else {
            report.skipped.push(Skipped {
                path: rel.to_string(),
                reason: "no recorded base to merge against; pass --force to replace".to_string(),
            });
            continue;
        };

        match diffy::merge(&base, &local, contents) {
            Ok(merged) if merged == local => {
                // The packaged side did not change this file's merged
                // region; nothing to write, but advance the base.
                report.unchanged.push(rel.to_string());
                manifest.files.insert(
                    rel.to_string(),
                    FileEntry {
                        base_sha256: packaged_sha.clone(),
                        written_sha256: entry.written_sha256.clone(),
                    },
                );
                write_backup(target, rel, contents)?;
                dirty = true;
            }
            Ok(merged) => {
                write_file(&path, &merged)?;
                report.merged.push(rel.to_string());
                manifest.files.insert(
                    rel.to_string(),
                    FileEntry {
                        base_sha256: packaged_sha.clone(),
                        written_sha256: sha256_hex(merged.as_bytes()),
                    },
                );
                write_backup(target, rel, contents)?;
                dirty = true;
            }
            Err(_) => {
                // Conflict: the user's file is inviolable. Drop the new
                // packaged copy beside it and advance the base so the next
                // version merges from the right point.
                let incoming = incoming_path(&path);
                write_file(&incoming, contents)?;
                report.conflicts.push(rel.to_string());
                manifest.files.insert(
                    rel.to_string(),
                    FileEntry {
                        base_sha256: packaged_sha.clone(),
                        written_sha256: entry.written_sha256.clone(),
                    },
                );
                write_backup(target, rel, contents)?;
                dirty = true;
            }
        }
    }

    if dirty || manifest.version != version {
        manifest.version = version.to_string();
        manifest.save(target)?;
    }

    Ok(report)
}

/// The lazy version-change pass over one target directory.
///
/// Fast path, in order: read the manifest (a stat plus a tiny JSON read);
/// bail if absent/unparseable; bail if the recorded version already equals
/// the binary's. Only then does a full [`sync_into`] run — and only that
/// pass hashes anything.
pub(crate) fn migrate_dir(dir: &Path, packaged: &[(&str, &str)], version: &str) -> Option<String> {
    let text = fs::read_to_string(Manifest::path(dir)).ok()?;
    let manifest: Manifest = serde_json::from_str(&text).ok()?;
    if manifest.version == version {
        return None;
    }
    let from = manifest.version.clone();
    let report = sync_into(dir, packaged, version, false, SyncMode::Migrate).ok()?;
    summarize(&report, &from, version)
}

fn summarize(report: &InstallReport, from: &str, to: &str) -> Option<String> {
    if !report.wrote_anything() {
        return None;
    }
    let mut parts = Vec::new();
    if !report.installed.is_empty() {
        parts.push(format!("{} installed", report.installed.len()));
    }
    if !report.updated.is_empty() {
        parts.push(format!("{} updated", report.updated.len()));
    }
    if !report.merged.is_empty() {
        parts.push(format!("{} merged", report.merged.len()));
    }
    if !report.conflicts.is_empty() {
        parts.push(format!(
            "{} conflicts (new copy kept as {INCOMING_SUFFIX})",
            report.conflicts.len()
        ));
    }
    let from = if from.is_empty() { "unknown" } else { from };
    Some(format!(
        "grr: skills migrated {from} -> {to} ({}); local edits were preserved",
        parts.join(", ")
    ))
}

/// The per-file state `grr skills list` reports for one target.
pub(crate) fn classify(
    target: &Path,
    rel: &str,
    packaged: &str,
    manifest: Option<&Manifest>,
) -> &'static str {
    match fs::read_to_string(target.join(rel)) {
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => "missing",
        Err(_) => "unreadable",
        Ok(local) => {
            if local == packaged {
                return "current";
            }
            match manifest.and_then(|m| m.files.get(rel)) {
                Some(entry) if sha256_hex(local.as_bytes()) == entry.written_sha256 => "outdated",
                Some(_) => "locally-modified",
                None => "unmanaged",
            }
        }
    }
}

fn backup_path(target: &Path, rel: &str) -> PathBuf {
    target.join(BACKUP_DIR).join(rel)
}

fn read_backup(target: &Path, rel: &str) -> Option<String> {
    fs::read_to_string(backup_path(target, rel)).ok()
}

fn write_backup(target: &Path, rel: &str, contents: &str) -> Result<()> {
    write_file(&backup_path(target, rel), contents)
}

fn incoming_path(path: &Path) -> PathBuf {
    let mut raw = path.as_os_str().to_owned();
    raw.push(INCOMING_SUFFIX);
    PathBuf::from(raw)
}

fn write_file(path: &Path, contents: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
    }
    fs::write(path, contents).with_context(|| format!("writing {}", path.display()))
}

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

    /// A packaged-file table with no frontmatter constraints (only
    /// `SKILL.md` entries are validated), so tests can use plain bodies.
    fn packaged(body: &'static str) -> [(&'static str, &'static str); 1] {
        [("grr/body.md", body)]
    }

    fn read(target: &Path, rel: &str) -> String {
        fs::read_to_string(target.join(rel)).unwrap()
    }

    #[test]
    fn unchanged_install_is_idempotent_and_records_the_manifest() {
        let dir = tempdir().unwrap();
        let first = sync_into(
            dir.path(),
            &packaged("hello\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();
        assert_eq!(first.installed, vec!["grr/body.md".to_string()]);
        assert!(first.updated.is_empty() && first.merged.is_empty() && first.conflicts.is_empty());

        let manifest = Manifest::load(dir.path()).unwrap();
        assert_eq!(manifest.version, "1.0.0");
        assert_eq!(
            manifest.files["grr/body.md"].base_sha256,
            sha256_hex(b"hello\n")
        );
        assert_eq!(
            manifest.files["grr/body.md"].written_sha256,
            sha256_hex(b"hello\n")
        );
        // The merge base snapshot is on disk.
        assert_eq!(read(dir.path(), ".grr-backup/grr/body.md"), "hello\n");

        let second = sync_into(
            dir.path(),
            &packaged("hello\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();
        assert_eq!(second.unchanged, vec!["grr/body.md".to_string()]);
        assert!(second.installed.is_empty() && second.updated.is_empty());
    }

    #[test]
    fn new_version_safe_auto_updates_an_untouched_file() {
        let dir = tempdir().unwrap();
        sync_into(
            dir.path(),
            &packaged("v1\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();

        // No local edit; the packaged file changed.
        let report = sync_into(
            dir.path(),
            &packaged("v2\n"),
            "2.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();
        assert_eq!(report.updated, vec!["grr/body.md".to_string()]);
        assert_eq!(read(dir.path(), "grr/body.md"), "v2\n");
        assert_eq!(Manifest::load(dir.path()).unwrap().version, "2.0.0");
    }

    #[test]
    fn clean_merge_combines_local_edits_with_the_new_packaged_content() {
        let dir = tempdir().unwrap();
        sync_into(
            dir.path(),
            &packaged("alpha\nbravo\ncharlie\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();

        // Local edits the first line; the new package edits the last.
        fs::write(dir.path().join("grr/body.md"), "ALPHA\nbravo\ncharlie\n").unwrap();
        let report = sync_into(
            dir.path(),
            &packaged("alpha\nbravo\nCHARLIE\n"),
            "2.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();

        assert_eq!(report.merged, vec!["grr/body.md".to_string()]);
        let merged = read(dir.path(), "grr/body.md");
        assert!(merged.contains("ALPHA"), "local edit lost: {merged}");
        assert!(merged.contains("CHARLIE"), "packaged edit lost: {merged}");
        assert!(report.conflicts.is_empty());
        // written hash reflects the merged bytes, not the packaged ones.
        let manifest = Manifest::load(dir.path()).unwrap();
        assert_eq!(
            manifest.files["grr/body.md"].written_sha256,
            sha256_hex(merged.as_bytes())
        );
        assert_eq!(
            manifest.files["grr/body.md"].base_sha256,
            sha256_hex(b"alpha\nbravo\nCHARLIE\n")
        );
    }

    #[test]
    fn conflicting_edit_leaves_the_local_file_and_drops_an_incoming_copy() {
        let dir = tempdir().unwrap();
        sync_into(
            dir.path(),
            &packaged("alpha\nbravo\ncharlie\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();

        // Both sides change the SAME line differently.
        fs::write(dir.path().join("grr/body.md"), "LOCAL\nbravo\ncharlie\n").unwrap();
        let report = sync_into(
            dir.path(),
            &packaged("PACKAGED\nbravo\ncharlie\n"),
            "2.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();

        assert_eq!(report.conflicts, vec!["grr/body.md".to_string()]);
        assert!(report.merged.is_empty() && report.updated.is_empty());
        // The user's file is untouched...
        assert_eq!(read(dir.path(), "grr/body.md"), "LOCAL\nbravo\ncharlie\n");
        // ...and the packaged-new copy sits beside it.
        assert_eq!(
            read(dir.path(), "grr/body.md.grr-incoming"),
            "PACKAGED\nbravo\ncharlie\n"
        );
    }

    #[test]
    fn force_still_overwrites_a_local_edit() {
        let dir = tempdir().unwrap();
        sync_into(
            dir.path(),
            &packaged("v1\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();
        fs::write(dir.path().join("grr/body.md"), "local\n").unwrap();

        let report = sync_into(
            dir.path(),
            &packaged("v2\n"),
            "2.0.0",
            true,
            SyncMode::Explicit,
        )
        .unwrap();
        assert_eq!(report.updated, vec!["grr/body.md".to_string()]);
        assert_eq!(read(dir.path(), "grr/body.md"), "v2\n");
    }

    #[test]
    fn files_without_provenance_are_never_touched() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("grr")).unwrap();
        fs::write(dir.path().join("grr/body.md"), "not grr's file\n").unwrap();

        let report = sync_into(
            dir.path(),
            &packaged("v1\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();
        assert_eq!(report.skipped.len(), 1);
        assert_eq!(read(dir.path(), "grr/body.md"), "not grr's file\n");
    }

    #[test]
    fn auto_migration_runs_only_when_the_recorded_version_differs() {
        let dir = tempdir().unwrap();
        sync_into(
            dir.path(),
            &packaged("v1\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();

        // Same version: the fast path short-circuits with NO hashing.
        reset_hash_calls();
        assert_eq!(migrate_dir(dir.path(), &packaged("v2\n"), "1.0.0"), None);
        assert_eq!(hash_calls(), 0, "same-version fast path must not hash");
        assert_eq!(read(dir.path(), "grr/body.md"), "v1\n");

        // Different version: the pass runs, updates the untouched file and
        // reports one summary line.
        let summary = migrate_dir(dir.path(), &packaged("v2\n"), "2.0.0").unwrap();
        assert!(summary.contains("1.0.0 -> 2.0.0"), "{summary}");
        assert!(summary.contains("1 updated"), "{summary}");
        assert_eq!(read(dir.path(), "grr/body.md"), "v2\n");
    }

    #[test]
    fn auto_migration_never_resurrects_a_locally_deleted_file() {
        let dir = tempdir().unwrap();
        sync_into(
            dir.path(),
            &packaged("v1\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();
        fs::remove_file(dir.path().join("grr/body.md")).unwrap();

        assert_eq!(migrate_dir(dir.path(), &packaged("v2\n"), "2.0.0"), None);
        assert!(!dir.path().join("grr/body.md").exists());
    }

    #[test]
    fn missing_manifest_means_nothing_to_migrate() {
        let dir = tempdir().unwrap();
        assert_eq!(migrate_dir(dir.path(), &packaged("v1\n"), "2.0.0"), None);
    }

    #[test]
    fn classify_separates_outdated_from_locally_modified() {
        let dir = tempdir().unwrap();
        sync_into(
            dir.path(),
            &packaged("v1\n"),
            "1.0.0",
            false,
            SyncMode::Explicit,
        )
        .unwrap();
        let manifest = Manifest::load(dir.path());

        // Untouched -> outdated when the packaged content moved.
        assert_eq!(
            classify(dir.path(), "grr/body.md", "v2\n", manifest.as_ref()),
            "outdated"
        );
        // Local edit -> locally-modified.
        fs::write(dir.path().join("grr/body.md"), "mine\n").unwrap();
        assert_eq!(
            classify(dir.path(), "grr/body.md", "v2\n", manifest.as_ref()),
            "locally-modified"
        );
        // Same as packaged -> current; absent -> missing.
        assert_eq!(
            classify(dir.path(), "grr/body.md", "mine\n", manifest.as_ref()),
            "current"
        );
        assert_eq!(
            classify(dir.path(), "grr/nope.md", "v2\n", manifest.as_ref()),
            "missing"
        );
    }
}