valheim-mod-manager 0.4.0

Mod manager for Valheim game
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
use crate::error::{AppError, AppResult};
use crate::target::{GAME, Target};
use std::io::{self, IsTerminal, Write};
use std::path::PathBuf;
use thunderstore_engine::ecosystem::Ecosystem;
use thunderstore_engine::profile::{self, NotRemovable, UninstallBatch, modlist};

/// Uninstalls each mod from the target, removing its files, its `_state`
/// payload, and its `mods.yml` entry.
///
/// The batch is planned through the engine's [`profile::plan_uninstall_batch`]
/// before anything is removed, so one bad identifier cannot leave a partial
/// uninstall behind. That refusal collapses to one sentence, so [`plan_batch`]
/// asks [`profile::check_removable_batch`] for the typed reason instead and
/// words each kind the way vmm always has.
///
/// The engine's `uninstall_mod_in` does not remove one mod's folders in
/// isolation: it derives a keep-set from the *remaining* `mods.yml` and hands
/// it to `remove_delisted`, which deletes every `<Owner-Name>`-shaped folder
/// under every namespaced route that the keep-set does not name. In game-dir
/// mode that route tree is the user's live game directory, so a mod folder
/// placed there by hand, or by r2modman, or by an older vmm whose `mod_list`
/// was never migrated, is swept along with the mod actually being
/// uninstalled. Rather than silently deleting somebody else's install, the
/// planned batch's `untracked` list names exactly those folders and
/// [`untracked_error`] refuses the whole command with them, unless `force`
/// says to go ahead.
pub fn run(eco: &Ecosystem, target: &Target, mods: &[String], force: bool) -> AppResult<()> {
  let installed = super::read_modlist(target)?;

  for full_name in mods {
    if modlist::find(&installed, full_name).is_none() {
      return Err(AppError::advice(
        format!(
          "{full_name} is not installed in {}.",
          crate::target::describe(target)
        ),
        "Nothing was removed. Identifiers are `Owner-ModName` and are \
         case-sensitive, so check the spelling against what is installed.",
        &["vmm list"],
      ));
    }
  }

  let batch = plan_batch(eco, target, Some(mods))?;

  if !force && !batch.untracked.is_empty() {
    return Err(untracked_error(target, &batch));
  }

  let outcome = profile::uninstall_batch(&target.dir, eco, GAME, &batch)?;

  for name in &outcome.succeeded {
    println!("removed {name}");
  }

  super::report_batch_failures(&outcome)
}

/// Plans an uninstall batch, wording the engine's typed refusal as vmm's two
/// distinct advice messages when a subject cannot be removed exactly.
///
/// `plan_uninstall_batch` returns a single `Err` for both an absent install
/// record and an unreadable one, so [`profile::check_removable_batch`] is
/// called here for the [`NotRemovable`] behind it: two refusals that want
/// different advice, and the engine has already decided which is which. When
/// nothing is blocked, the engine's own refusal is returned as is, which is
/// only reachable through `run_all`'s `names: None`, since [`run`] above
/// already checked every name's presence before calling this.
fn plan_batch(
  eco: &Ecosystem,
  target: &Target,
  names: Option<&[String]>,
) -> AppResult<UninstallBatch> {
  let plan_error = match profile::plan_uninstall_batch(&target.dir, eco, GAME, names) {
    Ok(batch) => return Ok(batch),
    Err(error) => error,
  };

  let subjects = match names {
    Some(names) => names.to_vec(),
    None => super::read_modlist(target)?
      .into_iter()
      .map(|entry| entry.name)
      .collect(),
  };

  let blocked = profile::check_removable_batch(&target.dir, eco, &subjects);

  if let Some((name, refusal)) = blocked.into_iter().next() {
    let install = format!("vmm install {name}");

    return Err(match refusal {
      NotRemovable::RecordUnreadable { path, source, .. } => AppError::advice(
        format!("{name}'s install record can't be read, so it cannot be removed exactly."),
        format!(
          "vmm found a record at {} but couldn't parse it: {source}\n\nThe record \
           is unreadable, not absent, so vmm will not guess what it once tracked. \
           Nothing was removed.\n\nReinstall {name} to rebuild its record, then \
           try again, or remove its files by hand.",
          path.display()
        ),
        &[install.as_str()],
      ),
      NotRemovable::RecordAbsent { path, .. } => AppError::advice(
        format!("{name} has no install record, so it cannot be removed exactly."),
        format!(
          "vmm expected one at {}. This target was adopted from disk rather than \
           installed, so vmm does not know which files the loader placed and will \
           not guess. Nothing was removed.\n\nReinstall the loader to give it a \
           record, then try again, or remove its files by hand.",
          path.display()
        ),
        &[install.as_str()],
      ),
    });
  }

  Err(plan_error.into())
}

/// The refusal [`run`] returns when the reconcile sweep would take mod folders
/// `mods.yml` does not track, unless `force` accepts that.
fn untracked_error(target: &Target, batch: &UninstallBatch) -> AppError {
  AppError::advice(
    format!(
      "{} holds mod folders that vmm does not track.",
      crate::target::describe(target)
    ),
    format!(
      "An uninstall reconciles every install route against {}, so these \
       would be deleted along with the mods you named:\n\n  {}\n\nNothing \
       was removed. Bring them under management with `vmm install \
       <Owner-ModName>` if you installed them by hand, or `vmm migrate` if \
       they're left over from a deprecated `mod_list`. Otherwise, re-run \
       with `--force` to delete them too.",
      target.mods_yml().display(),
      render_untracked(&batch.untracked)
    ),
    &["vmm install <Owner-ModName>", "vmm migrate"],
  )
}

/// Renders a batch's untracked folders as the indented list every uninstall
/// message shows them in.
fn render_untracked(untracked: &[PathBuf]) -> String {
  untracked
    .iter()
    .map(|path| path.display().to_string())
    .collect::<Vec<_>>()
    .join("\n  ")
}

/// How many mods `batch` accounts for, loaders included.
///
/// A local helper rather than a method on `UninstallBatch`: it is a foreign
/// type, so vmm cannot add an inherent method to it.
fn batch_len(batch: &UninstallBatch) -> usize {
  batch.mods.len() + batch.loaders.len()
}

/// Uninstalls every mod in the target, confirming first.
///
/// `assume_yes` skips `confirm`. Because the prompt is what lists the untracked
/// folders the sweep would take, skipping it re-imposes the `--force` gate the
/// single-mod path uses: the prompt can be skipped, the awareness cannot.
///
/// `confirm` is taken as a parameter rather than called directly so tests can
/// exercise the interactive branch (decline, proceed without `--force`, and the
/// batch handed to the prompt) without a TTY. [`confirm_uninstall_all`] is the
/// real implementation `main` passes.
pub fn run_all(
  eco: &Ecosystem,
  target: &Target,
  force: bool,
  assume_yes: bool,
  confirm: impl FnOnce(&Target, &UninstallBatch) -> AppResult<bool>,
) -> AppResult<()> {
  let batch = plan_batch(eco, target, None)?;

  if batch_len(&batch) == 0 {
    println!(
      "vmm: {}",
      crate::error::advice_message(
        &format!(
          "there is nothing to uninstall in {}.",
          crate::target::describe(target)
        ),
        "No mods are installed here, so nothing was changed.",
        &["vmm list"],
      )
    );

    return Ok(());
  }

  if assume_yes {
    if !batch.untracked.is_empty() && !force {
      return Err(AppError::advice(
        format!(
          "{} holds mod folders that vmm does not track.",
          crate::target::describe(target)
        ),
        format!(
          "Removing every mod reconciles each install route against {}, so \
           these would be deleted too:\n\n  {}\n\nNothing was removed. \
           `--yes` skips the prompt that would have listed them, so confirm \
           them with `--force` instead.",
          target.mods_yml().display(),
          render_untracked(&batch.untracked)
        ),
        &["vmm uninstall --all --yes --force"],
      ));
    }
  } else if !confirm(target, &batch)? {
    println!("Nothing was changed.");

    return Ok(());
  }

  let outcome = profile::uninstall_batch(&target.dir, eco, GAME, &batch)?;

  for name in &outcome.succeeded {
    println!("{}", removed_line(name, &batch));
  }

  if let Some(summary) = removed_summary(&outcome.succeeded, &batch, target) {
    println!("{summary}");
  }

  super::report_batch_failures(&outcome)
}

/// The line `run_all` prints for one removed identifier: the mod loader gets a
/// parenthetical naming it, an ordinary mod does not.
///
/// Pulled out as a pure function so the two wordings are directly testable
/// without capturing stdout, the same way [`untracked_error`] and
/// [`no_terminal_error`] are.
fn removed_line(name: &str, batch: &UninstallBatch) -> String {
  match batch.loaders.iter().any(|loader| loader == name) {
    true => format!("removed {name} (the mod loader)"),
    false => format!("removed {name}"),
  }
}

/// The summary line `run_all` prints once the batch has run: mentions the target
/// going vanilla when a loader was among what was removed.
///
/// Counted from `succeeded` rather than from `batch`, and the loader checked for
/// membership in it rather than merely being planned, because
/// [`profile::uninstall_batch`] records per-item failures instead of aborting.
/// The batch is what was attempted; only `succeeded` is what came off, and
/// reporting the plan would claim removals that failed and call a target with
/// its loader still in place vanilla.
///
/// A batch where nothing at all came off has no summary to print: `None` rather
/// than a "removed 0 mods" line, since [`super::report_batch_failures`] is about
/// to name every failure anyway.
///
/// Pulled out for the same reason as [`removed_line`].
fn removed_summary(
  succeeded: &[String],
  batch: &UninstallBatch,
  target: &Target,
) -> Option<String> {
  if succeeded.is_empty() {
    return None;
  }

  let loader_removed = succeeded.iter().any(|name| batch.loaders.contains(name));

  Some(match loader_removed {
    false => format!(
      "\nremoved {} from {}.",
      super::describe_count(succeeded.len()),
      crate::target::describe(target)
    ),
    true => format!(
      "\nremoved {}; {} is now vanilla.",
      super::describe_count(succeeded.len()),
      crate::target::describe(target)
    ),
  })
}

/// The refusal [`confirm_uninstall_all`] returns when there is no terminal to
/// confirm with.
///
/// Pulled out as a pure function (no I/O) so its message is directly testable.
/// `confirm_uninstall_all` itself cannot safely be exercised end to end in a
/// test either way it could go: a real TTY would block on `read_line` waiting
/// for input that never comes, and there is no portable way to fake "this is
/// not a terminal" for `io::stdin()` from within the test process. This is the
/// one piece of that function a test can call directly, with zero TTY risk.
fn no_terminal_error(target: &Target, batch: &UninstallBatch) -> AppError {
  AppError::advice(
    "removing every mod needs confirming, and there is no terminal to ask.",
    format!(
      "This would remove {} from {} ({}). Nothing was changed.\n\nRe-run with \
       `--yes` to confirm without a prompt.",
      super::describe_count(batch_len(batch)),
      crate::target::describe(target),
      target.dir.display()
    ),
    &["vmm uninstall --all --yes"],
  )
}

/// Describes what will be removed and asks for confirmation.
///
/// A non-interactive stdin cannot answer, so rather than defaulting either way
/// this refuses and names the flag that makes the intent explicit.
pub fn confirm_uninstall_all(target: &Target, batch: &UninstallBatch) -> AppResult<bool> {
  if !io::stdin().is_terminal() {
    return Err(no_terminal_error(target, batch));
  }

  println!(
    "This will remove {} from {}:\n  {}",
    super::describe_count(batch_len(batch)),
    crate::target::describe(target),
    target.dir.display()
  );

  match batch.loaders.len() {
    0 => {}
    1 => println!("\nThe mod loader goes too, so you'll be left with a vanilla install."),
    _ => println!("\nThe mod loaders go too, so you'll be left with a vanilla install."),
  }

  println!("Your config files under BepInEx/config are left alone.");

  if !batch.untracked.is_empty() {
    println!(
      "\nThese mod folders aren't tracked by vmm and will be removed as \
       well:\n  {}",
      render_untracked(&batch.untracked)
    );
  }

  print!(
    "\nRemove {}? [y/N] ",
    super::describe_count(batch_len(batch))
  );
  io::stdout().flush()?;

  let mut answer = String::new();

  io::stdin().read_line(&mut answer)?;

  let answer = answer.trim().to_ascii_lowercase();

  Ok(answer == "y" || answer == "yes")
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::test_support::Fixture;
  use thunderstore_engine::ecosystem::Ecosystem;
  use thunderstore_engine::profile::modlist;
  use tokio::runtime::Runtime;

  #[test]
  fn an_unreadable_mods_yml_reports_its_path() {
    let fixture = Fixture::new();
    let target = fixture.target();

    std::fs::write(target.mods_yml(), "not: [valid").unwrap();

    let message = crate::commands::read_modlist(&target)
      .unwrap_err()
      .to_string();

    assert!(
      message.contains(&target.mods_yml().display().to_string()),
      "the error must name the file that could not be read; got: {message}"
    );
    assert!(
      message.contains("the game directory"),
      "the error must name the target; got: {message}"
    );
    // `list::run` reads this same file through this same function before doing
    // anything else, so offering `vmm list` as the way forward promises a
    // command that is guaranteed to fail with this exact message again.
    assert!(
      !message.contains("vmm list"),
      "the advice must not be a command that reproduces the error; got: {message}"
    );
    assert!(message.contains("Nothing was changed"), "got: {message}");
  }

  #[test]
  fn uninstall_removes_files_and_the_mods_yml_entry() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();
    let runtime = Runtime::new().unwrap();

    runtime
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string(), "Owner-ModB".to_string()],
        false,
      ))
      .unwrap();

    run(&eco, &target, &["Owner-ModB".to_string()], false).unwrap();

    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
    assert!(!target.dir.join("BepInEx/plugins/Owner-ModB").exists());

    let mods = modlist::read(&target.dir).unwrap();

    assert_eq!(mods.len(), 1);
    assert_eq!(mods[0].name, "Owner-ModA");
  }

  #[test]
  fn uninstall_refuses_when_an_untracked_mod_folder_would_be_swept() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string(), "Owner-ModB".to_string()],
        false,
      ))
      .unwrap();

    // A mod installed by hand (or by r2modman): `mods.yml` never recorded it, so
    // the sweep inside `uninstall_mod_in` would take it along.
    let by_hand = target.dir.join("BepInEx/plugins/Azumatt-AzuAntiCheat");
    // A loader-shipped folder that is not `<Owner-Name>`-shaped, which the sweep
    // ignores, so it must not be reported either.
    let loader_folder = target.dir.join("BepInEx/plugins/MMHOOK");

    std::fs::create_dir_all(&by_hand).unwrap();
    std::fs::write(by_hand.join("AzuAntiCheat.dll"), b"dll").unwrap();
    std::fs::create_dir_all(&loader_folder).unwrap();

    let result = run(&eco, &target, &["Owner-ModB".to_string()], false);
    let message = result.unwrap_err().to_string();

    assert!(
      message.contains("Azumatt-AzuAntiCheat"),
      "the error must name the folder at risk; got: {message}"
    );
    assert!(
      !message.contains("MMHOOK"),
      "a folder the sweep would never remove must not be reported; got: {message}"
    );
    assert!(message.contains("--force"), "got: {message}");
    // Nothing was mutated: the check runs before any removal.
    assert!(by_hand.join("AzuAntiCheat.dll").exists());
    assert!(target.dir.join("BepInEx/plugins/Owner-ModB").exists());
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 2);
  }

  #[test]
  fn force_uninstalls_and_accepts_the_sweep() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string(), "Owner-ModB".to_string()],
        false,
      ))
      .unwrap();

    let by_hand = target.dir.join("BepInEx/plugins/Azumatt-AzuAntiCheat");

    std::fs::create_dir_all(&by_hand).unwrap();
    std::fs::write(by_hand.join("AzuAntiCheat.dll"), b"dll").unwrap();

    run(&eco, &target, &["Owner-ModB".to_string()], true).unwrap();

    assert!(!target.dir.join("BepInEx/plugins/Owner-ModB").exists());
    // `--force` accepts the documented consequence: mods.yml is authoritative
    // over the whole namespaced tree, so an unrecorded folder goes with it.
    assert!(!by_hand.exists());
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
  }

  #[test]
  fn uninstall_from_a_profile_never_touches_the_game_dir() {
    let fixture = Fixture::new();
    let target = fixture.profile_target("experiment");
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string(), "Owner-ModB".to_string()],
        false,
      ))
      .unwrap();

    // A game directory holding a hand-installed mod: a profile-mode uninstall
    // reconciles the profile's routes, so it must neither refuse because of this
    // folder nor delete it.
    let in_game_dir = fixture
      .game_dir
      .path()
      .join("BepInEx/plugins/Azumatt-AzuAntiCheat");

    std::fs::create_dir_all(&in_game_dir).unwrap();
    std::fs::write(in_game_dir.join("AzuAntiCheat.dll"), b"dll").unwrap();

    run(&eco, &target, &["Owner-ModB".to_string()], false).unwrap();

    assert!(!target.dir.join("BepInEx/plugins/Owner-ModB").exists());
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 1);
    assert!(in_game_dir.join("AzuAntiCheat.dll").exists());
    assert!(
      !fixture
        .game_dir
        .path()
        .join("BepInEx/plugins/Owner-ModA")
        .exists()
    );
  }

  #[test]
  fn uninstall_rejects_the_whole_batch_when_one_mod_is_absent() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let result = run(
      &eco,
      &target,
      &["Owner-ModA".to_string(), "Owner-Absent".to_string()],
      false,
    );

    assert!(result.is_err());
    // Nothing was removed: the batch is checked before any mutation.
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
  }

  #[test]
  fn uninstalling_a_mod_that_is_not_installed_points_at_list() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    let message = run(&eco, &target, &["Owner-Absent".to_string()], false)
      .unwrap_err()
      .to_string();

    assert!(
      message.contains("Owner-Absent"),
      "the error must name the mod; got: {message}"
    );
    assert!(
      message.contains("vmm list"),
      "the error must say how to see what is installed; got: {message}"
    );
  }

  #[test]
  fn uninstalling_a_mixed_batch_refuses_before_touching_any_mod_when_the_loader_has_no_record() {
    let fixture = Fixture::new();
    let target = fixture.profile_target("adopted");
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &[
          "Owner-ModA".to_string(),
          "denikson-BepInExPack_Valheim".to_string(),
        ],
        false,
      ))
      .unwrap();

    // Simulate a profile adopted from disk: mods.yml records the loader, but
    // the tracker that says which files it placed does not exist.
    std::fs::remove_file(thunderstore_engine::install::state_file_path(
      &target.dir,
      "denikson-BepInExPack_Valheim",
    ))
    .unwrap();

    // Owner-ModA is named first: without the pre-flight this would remove it,
    // then fail on the loader, leaving a half-done batch.
    let message = run(
      &eco,
      &target,
      &[
        "Owner-ModA".to_string(),
        "denikson-BepInExPack_Valheim".to_string(),
      ],
      false,
    )
    .unwrap_err()
    .to_string();

    assert!(
      message.contains("denikson-BepInExPack_Valheim"),
      "the error must name the loader; got: {message}"
    );
    assert!(message.contains("Nothing was removed"), "got: {message}");
    // Nothing was touched: Owner-ModA survives even though it was named first
    // and would otherwise have been removed before the loader was reached.
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 2);
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
  }

  #[test]
  fn skipping_the_prompt_over_untracked_folders_still_needs_force() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let by_hand = target.dir.join("BepInEx/plugins/Azumatt-AzuAntiCheat");

    std::fs::create_dir_all(&by_hand).unwrap();
    std::fs::write(by_hand.join("AzuAntiCheat.dll"), b"dll").unwrap();

    // --yes skips the prompt that would have listed these, so the awareness has
    // to come from --force instead. `assume_yes = true` means `confirm` is never
    // reached, so a confirmer that panics if called proves that.
    let message = run_all(&eco, &target, false, true, unreachable_confirm)
      .unwrap_err()
      .to_string();

    assert!(message.contains("Azumatt-AzuAntiCheat"), "got: {message}");
    assert!(message.contains("--force"), "got: {message}");
    assert!(by_hand.join("AzuAntiCheat.dll").exists());
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 1);

    // With --force it proceeds and accepts the documented consequence.
    run_all(&eco, &target, true, true, unreachable_confirm).unwrap();

    assert!(modlist::read(&target.dir).unwrap().is_empty());
    assert!(!by_hand.exists());
  }

  #[test]
  fn uninstalling_everything_with_nothing_installed_is_a_friendly_no_op() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    // `assume_yes = false` so this actually proves the property that matters:
    // an empty target returns before the interactive prompt is ever reached.
    // `unreachable_confirm` panics if it is called, so this would fail loudly
    // if the empty-plan short circuit were ever removed.
    assert!(run_all(&eco, &target, false, false, unreachable_confirm).is_ok());
  }

  #[test]
  fn uninstalling_everything_from_a_profile_never_touches_the_game_dir() {
    let fixture = Fixture::new();
    let target = fixture.profile_target("experiment");
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let in_game_dir = fixture.game_dir.path().join("BepInEx/plugins/Owner-Kept");

    std::fs::create_dir_all(&in_game_dir).unwrap();
    std::fs::write(in_game_dir.join("Kept.dll"), b"dll").unwrap();

    // assume_yes so the interactive branch is not reached; nothing here is
    // untracked, so --force is not needed either.
    run_all(&eco, &target, false, true, unreachable_confirm).unwrap();

    assert!(modlist::read(&target.dir).unwrap().is_empty());
    assert!(in_game_dir.join("Kept.dll").exists());
  }

  #[test]
  fn run_all_removes_everything_including_the_loader() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &[
          "Owner-ModA".to_string(),
          "denikson-BepInExPack_Valheim".to_string(),
        ],
        false,
      ))
      .unwrap();

    // assume_yes so the interactive branch is not reached; nothing here is
    // untracked, so --force is not needed either. This drives `run_all`
    // through both of its loader-dependent print branches (`removed_line`'s
    // "(the mod loader)" suffix and `removed_summary`'s "is now vanilla"
    // wording, asserted for content below), proving the whole pipeline
    // reaches them and leaves the target vanilla. The removal order itself is
    // the engine's `uninstall_batch`'s guarantee, proved at the engine layer.
    run_all(&eco, &target, false, true, unreachable_confirm).unwrap();

    assert!(modlist::read(&target.dir).unwrap().is_empty());
    assert!(!target.dir.join("winhttp.dll").exists());
    assert!(!target.dir.join("BepInEx/core/BepInEx.dll").exists());
  }

  #[test]
  fn removed_line_and_summary_distinguish_the_mod_loader() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &[
          "Owner-ModA".to_string(),
          "denikson-BepInExPack_Valheim".to_string(),
        ],
        false,
      ))
      .unwrap();

    let batch = plan_batch(&eco, &target, None).unwrap();

    assert_eq!(removed_line("Owner-ModA", &batch), "removed Owner-ModA");
    assert_eq!(
      removed_line("denikson-BepInExPack_Valheim", &batch),
      "removed denikson-BepInExPack_Valheim (the mod loader)"
    );

    let vanilla_summary = removed_summary(
      &[
        "Owner-ModA".to_string(),
        "denikson-BepInExPack_Valheim".to_string(),
      ],
      &batch,
      &target,
    )
    .unwrap();

    assert!(
      vanilla_summary.contains("is now vanilla"),
      "got: {vanilla_summary}"
    );

    let loaderless_fixture = Fixture::new();
    let loaderless_target = loaderless_fixture.target();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &loaderless_fixture.client,
        &eco,
        &loaderless_target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let loaderless_batch = plan_batch(&eco, &loaderless_target, None).unwrap();
    let plain_summary = removed_summary(
      &["Owner-ModA".to_string()],
      &loaderless_batch,
      &loaderless_target,
    )
    .unwrap();

    assert!(!plain_summary.contains("vanilla"), "got: {plain_summary}");
    assert!(plain_summary.contains("removed"), "got: {plain_summary}");
  }

  #[test]
  fn the_summary_reports_what_came_off_not_what_was_planned() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &[
          "Owner-ModA".to_string(),
          "denikson-BepInExPack_Valheim".to_string(),
        ],
        false,
      ))
      .unwrap();

    let batch = plan_batch(&eco, &target, None).unwrap();

    // `uninstall_batch` records per-item failures instead of aborting, so the
    // batch is what was *attempted*. A run where the loader failed to come off
    // must neither count it nor call the target vanilla, because it isn't.
    let partial = removed_summary(&["Owner-ModA".to_string()], &batch, &target).unwrap();

    assert!(
      partial.contains("removed 1 mod"),
      "the summary must count what succeeded; got: {partial}"
    );
    assert!(
      !partial.contains("vanilla"),
      "the loader is still installed, so the target is not vanilla; got: {partial}"
    );
    // Nothing came off at all: `report_batch_failures` is about to say why, so
    // there is no summary to print rather than a "removed 0 mods" line.
    assert_eq!(removed_summary(&[], &batch, &target), None);
  }

  #[test]
  fn planning_refuses_a_loader_with_no_install_record() {
    let fixture = Fixture::new();
    let target = fixture.profile_target("adopted");
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["denikson-BepInExPack_Valheim".to_string()],
        false,
      ))
      .unwrap();

    // Simulate a profile adopted from disk: the record exists but the tracker
    // that says which files the loader placed does not.
    std::fs::remove_file(thunderstore_engine::install::state_file_path(
      &target.dir,
      "denikson-BepInExPack_Valheim",
    ))
    .unwrap();

    let message = plan_batch(&eco, &target, None).unwrap_err().to_string();

    assert!(
      message.contains("denikson-BepInExPack_Valheim"),
      "the error must name the loader; got: {message}"
    );
    // Refusing before mutating leaves the record intact.
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 1);
  }

  #[test]
  fn planning_refuses_a_loader_whose_state_file_is_unparseable() {
    let fixture = Fixture::new();
    let target = fixture.profile_target("adopted-with-junk");
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["denikson-BepInExPack_Valheim".to_string()],
        false,
      ))
      .unwrap();

    // The `_state` file is present (so a bare `.exists()` check would pass) but
    // truncated or hand-edited, so parsing it fails. `read_state_file` reports
    // that as `Err`, not `None`, and the engine's own `uninstall_mod_in` refuses
    // on exactly that predicate. Planning must use the same one, or this case
    // would pass the pre-flight, remove every other mod, and only then fail on
    // the loader itself.
    std::fs::write(
      thunderstore_engine::install::state_file_path(&target.dir, "denikson-BepInExPack_Valheim"),
      "not: [valid",
    )
    .unwrap();

    let message = plan_batch(&eco, &target, None).unwrap_err().to_string();

    assert!(
      message.contains("denikson-BepInExPack_Valheim"),
      "the error must name the loader; got: {message}"
    );
    // Distinguished from the missing-record case: the record exists, it just
    // cannot be read, so the advice should say so rather than reuse the
    // "no install record" wording.
    assert!(
      message.contains("can't be read"),
      "the message must say the record is unreadable, not absent; got: {message}"
    );
    assert!(message.contains("Nothing was removed"), "got: {message}");
    // Refusing before mutating leaves the record, and everything else, intact.
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 1);
  }

  #[test]
  fn planning_refuses_a_regular_mod_whose_state_file_is_unparseable() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string(), "Owner-ModB".to_string()],
        false,
      ))
      .unwrap();

    // Ordinary mods installed into a namespaced `<Owner-Name>/` folder get no
    // `_state` file at all: `remove_delisted` reconciles their folder by
    // presence, not via a tracker, so `write_state_file` is never called for
    // them. Nothing stops one from existing anyway though (a stray leftover, or
    // written by hand), so the `_state` directory and file are fabricated here
    // to exercise exactly that: a record that is present but unparseable for a
    // mod the loader pre-flight never used to check at all.
    let state_path = thunderstore_engine::install::state_file_path(&target.dir, "Owner-ModA");

    std::fs::create_dir_all(state_path.parent().unwrap()).unwrap();
    std::fs::write(&state_path, "not: [valid").unwrap();

    let message = plan_batch(&eco, &target, None).unwrap_err().to_string();

    assert!(
      message.contains("Owner-ModA"),
      "the error must name the mod; got: {message}"
    );
    assert!(message.contains("can't be read"), "got: {message}");
    // Refusing before mutating leaves both mods fully present: recorded in
    // mods.yml and with their files still on disk.
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 2);
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModB/ModB.dll")
        .exists()
    );
  }

  /// A confirmer that panics if called, for `run_all` calls where `assume_yes`
  /// is `true` and the interactive path must not be reached at all.
  fn unreachable_confirm(_target: &Target, _batch: &UninstallBatch) -> AppResult<bool> {
    panic!("confirm must not be called when --yes skips the prompt")
  }

  #[test]
  fn declining_the_interactive_prompt_changes_nothing() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let result = run_all(&eco, &target, false, false, |_, _| Ok(false));

    assert!(result.is_ok());
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 1);
    assert!(
      target
        .dir
        .join("BepInEx/plugins/Owner-ModA/ModA.dll")
        .exists()
    );
  }

  #[test]
  fn accepting_the_interactive_prompt_does_not_require_force() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let by_hand = target.dir.join("BepInEx/plugins/Azumatt-AzuAntiCheat");

    std::fs::create_dir_all(&by_hand).unwrap();
    std::fs::write(by_hand.join("AzuAntiCheat.dll"), b"dll").unwrap();

    // Unlike the `--yes` path, accepting the interactive prompt is enough on its
    // own: the prompt already listed the untracked folder, so `--force` is not
    // demanded here too.
    run_all(&eco, &target, false, false, |_, _| Ok(true)).unwrap();

    assert!(modlist::read(&target.dir).unwrap().is_empty());
    assert!(!by_hand.exists());
  }

  #[test]
  fn the_interactive_prompt_is_handed_the_plan_it_confirms() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let by_hand = target.dir.join("BepInEx/plugins/Azumatt-AzuAntiCheat");

    std::fs::create_dir_all(&by_hand).unwrap();
    std::fs::write(by_hand.join("AzuAntiCheat.dll"), b"dll").unwrap();

    let seen_untracked = std::cell::RefCell::new(Vec::new());

    run_all(&eco, &target, false, false, |_, batch: &UninstallBatch| {
      *seen_untracked.borrow_mut() = batch.untracked.clone();

      Ok(false)
    })
    .unwrap();

    assert!(
      seen_untracked
        .borrow()
        .iter()
        .any(|entry| entry.display().to_string().contains("Azumatt-AzuAntiCheat")),
      "the plan handed to the confirmer must name the untracked folder; got: {:?}",
      seen_untracked.borrow()
    );
    // Declined, so nothing was mutated.
    assert_eq!(modlist::read(&target.dir).unwrap().len(), 1);
    assert!(by_hand.join("AzuAntiCheat.dll").exists());
  }

  #[test]
  fn the_non_tty_refusal_names_yes() {
    let fixture = Fixture::new();
    let target = fixture.target();
    let eco = Ecosystem::bundled();

    Runtime::new()
      .unwrap()
      .block_on(crate::commands::install::run(
        &fixture.client,
        &eco,
        &target,
        &["Owner-ModA".to_string()],
        false,
      ))
      .unwrap();

    let batch = plan_batch(&eco, &target, None).unwrap();

    let message = no_terminal_error(&target, &batch).to_string();

    assert!(message.contains("--yes"), "got: {message}");
  }
}