shine-cli 1.1.1

Cross-platform CLI for managed shell commands, app configs, and machine setup
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
//! Shared install-status row builders consumed by `list` and `info`.
//!
//! Not a routed command itself (`shine check` was removed) — this is a
//! status-row library: it computes per-file/per-category install status
//! (`FileStatus`) and renders it into `AppRow`/`ShellRow` for display.

use crate::apps::{
    AppCategory, AppListMode, installed_content_hash, resolve_install_destination,
    source_hash_for_file,
};
use crate::colors;
use crate::config::Config;
use crate::env::EnvConfig;
use crate::install_core::{AppEntry, AppManifest, apply_transforms};
use crate::path_display;
use anyhow::Result;
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::path::Path;

// ---------------------------------------------------------------------------
// Shared row types
// ---------------------------------------------------------------------------

/// Per-file status used for aggregation within a category.
/// Higher discriminant = higher priority (wins in fold).
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum FileStatus {
    NotInstalled,
    UpToDate,
    UpdateAvail,
    Partial,
    UserModified,
    Missing,
}

pub struct ShellRow {
    pub symbol: String,
    pub label: String,
    pub status_sym: &'static str,
    pub status_text: &'static str,
    /// `true` when at least one of preset-file or bin-symlink exists.
    pub is_installed: bool,
}

pub struct AppRow {
    /// App preset category owning this row. Unlike `label`, this is stable
    /// even when a multi-file category supplies custom display names.
    pub category: String,
    pub sym: &'static str,
    pub label: String,
    pub simple_label: String,
    pub dest: Option<String>,
    pub status_text: &'static str,
    pub file_status: FileStatus,
}

// ---------------------------------------------------------------------------
// Shared row builders (data-only, no printing)
// ---------------------------------------------------------------------------

/// Build shell preset rows.  Does not include the PATH sentinel line.
pub async fn build_shell_rows(config: &Config) -> Result<Vec<ShellRow>> {
    let categories = crate::shells::metadata::load_active_categories(config, None).await?;
    if categories.is_empty() {
        return Ok(Vec::new());
    }

    let bin_dir = config.bin_dir();
    let shell_manifest = crate::shells::deployment::ShellManifest::load(config).await?;
    let mut rows: Vec<ShellRow> = Vec::new();

    for cat in &categories {
        let snapshot_current =
            crate::shells::deployment::snapshot_category_current(config, &cat.name)
                .await
                .unwrap_or(false);
        for script in &cat.files {
            let desired_path = crate::shells::deployment::desired_source_path(
                config,
                &cat.name,
                &script.source_rel,
            );
            let script_path = crate::shells::deployment::deployment_source_path(
                config,
                &cat.name,
                &script.source_rel,
            );
            let source_key = format!("shell/{}/{}", cat.name, script.source_rel.display());
            let display_name = format!("{}/{}", cat.name, script.command_name);
            let rendered_path =
                crate::shells::deployment::rendered_path(config, &cat.name, &script.source_rel);
            let link_name = OsString::from(&script.command_name);
            let link_path = crate::bin_links::command_path_for_name(bin_dir, &link_name);

            let file_exists = script_path.exists();
            let link_exists = link_path.exists() || {
                tokio::fs::symlink_metadata(&link_path)
                    .await
                    .map(|m| m.file_type().is_symlink())
                    .unwrap_or(false)
            };
            let effective_transforms =
                crate::shells::deployment::effective_transforms(script, &desired_path)
                    .await
                    .unwrap_or_else(|_| script.transforms.clone());
            let effective_source = if !effective_transforms.is_empty() {
                &rendered_path
            } else {
                &script_path
            };
            let runtime_env = script
                .env
                .iter()
                .map(crate::env::EnvVarSpec::to_with_arg)
                .collect::<Vec<_>>();
            let link_current = if link_exists {
                let render_target = (config.is_external_presets
                    && config.external_shell_mode == crate::config::ExternalShellMode::Live
                    && !effective_transforms.is_empty())
                .then(|| format!("shell/{}/{}", cat.name, script.command_name));
                crate::bin_links::link_is_current(
                    &link_path,
                    effective_source,
                    script.runtime,
                    &runtime_env,
                    render_target.as_deref(),
                )
                .await?
            } else {
                false
            };

            let (sym, status_text) = match (file_exists, link_exists) {
                (true, true) => ("", "up-to-date"),
                (true, false) => ("~", "preset present, bin symlink missing"),
                (false, true) => ("~", "bin symlink present, preset missing"),
                (false, false) => ("", "not installed"),
            };

            let canonical_target = format!("shell/{}/{}", cat.name, script.command_name);
            let expected_runtime = match script.runtime {
                crate::bin_links::LinkRuntime::Native => "native",
                crate::bin_links::LinkRuntime::Bun => "bun",
            };
            let manifest_current = !config.is_external_presets
                || shell_manifest.find(&canonical_target).is_some_and(|entry| {
                    entry.mode == config.external_shell_mode
                        && entry.source_path == script_path
                        && entry.runtime == expected_runtime
                        && entry.transforms == effective_transforms
                        && entry.env == runtime_env
                        && entry.needs_source == script.needs_source
                });

            let (sym, status_text) = if link_exists
                && (!link_current || !manifest_current || !snapshot_current)
            {
                ("", "update available")
            } else {
                match shell_source_status(
                    config,
                    &source_key,
                    &desired_path,
                    &script_path,
                    &rendered_path,
                    &effective_transforms,
                )
                .await
                {
                    Some(FileStatus::UpdateAvail) if file_exists || link_exists => {
                        ("", "update available")
                    }
                    Some(FileStatus::Missing) if link_exists => ("!", "rendered script missing"),
                    _ if config.is_external_presets
                        && config.external_shell_mode == crate::config::ExternalShellMode::Live
                        && file_exists
                        && link_exists =>
                    {
                        if effective_transforms.is_empty() {
                            ("", "live source")
                        } else {
                            ("", "rendered on next run")
                        }
                    }
                    _ => (sym, status_text),
                }
            };

            rows.push(ShellRow {
                symbol: colors::symbol(sym),
                label: display_name,
                status_sym: sym,
                status_text,
                is_installed: file_exists || link_exists,
            });
        }
    }

    Ok(rows)
}

async fn shell_source_status(
    config: &Config,
    source_key: &str,
    desired_path: &Path,
    script_path: &Path,
    rendered_path: &Path,
    declared_transforms: &[String],
) -> Option<FileStatus> {
    let source_bytes = if config.is_external_presets {
        tokio::fs::read(desired_path).await.ok()?
    } else {
        crate::presets::read_asset_bytes(source_key)?
    };
    if !script_path.exists() {
        return Some(FileStatus::UpdateAvail);
    }
    if config.is_external_presets
        && config.external_shell_mode == crate::config::ExternalShellMode::Live
    {
        return Some(FileStatus::UpToDate);
    }
    let current_source = tokio::fs::read(script_path).await.ok()?;
    if source_bytes != current_source {
        return Some(FileStatus::UpdateAvail);
    }
    let transforms = declared_transforms.to_vec();
    if transforms.is_empty() {
        return Some(FileStatus::UpToDate);
    }

    if !rendered_path.exists() {
        return Some(FileStatus::Missing);
    }

    let env = EnvConfig::load_or_init(config).await.ok()?;
    let rendered = apply_transforms(&transforms, &source_bytes, env.as_map()).ok()?;
    let current = tokio::fs::read(rendered_path).await.ok()?;

    if rendered == current {
        Some(FileStatus::UpToDate)
    } else {
        Some(FileStatus::UpdateAvail)
    }
}

/// Build app config rows for the given pre-loaded categories.
pub async fn build_app_rows(config: &Config, categories: &[AppCategory]) -> Result<Vec<AppRow>> {
    let manifest = AppManifest::load(config.shine_dir()).await?;
    let env = EnvConfig::load_or_init(config).await.ok();
    let empty_map = BTreeMap::new();
    let env_map = env.as_ref().map(|e| e.as_map()).unwrap_or(&empty_map);
    let mut rows: Vec<AppRow> = Vec::new();

    for cat in categories {
        if cat.has_explicit_files && cat.list_mode == AppListMode::Files {
            for file in &cat.files {
                let (dest_opt, status) =
                    app_file_row_status(config, cat, file, &manifest, env_map).await;

                let label = file
                    .display_name
                    .clone()
                    .unwrap_or_else(|| format!("{}/{}", cat.name, file.source_rel.display()));
                let simple_label = if cat.files.len() == 1 {
                    cat.name.clone()
                } else {
                    label.clone()
                };

                let dest_str = dest_opt.map(|d| path_display::format_home(&d, &config.home_dir));

                let (sym, status_text) = match status {
                    FileStatus::Missing => ("!", "destination missing"),
                    FileStatus::UserModified => ("~", "user modified"),
                    FileStatus::UpdateAvail => ("", "update available"),
                    FileStatus::UpToDate => ("", "up-to-date"),
                    FileStatus::NotInstalled | FileStatus::Partial => ("", "not installed"),
                };

                rows.push(AppRow {
                    category: cat.name.clone(),
                    sym,
                    label,
                    simple_label,
                    dest: dest_str,
                    status_text,
                    file_status: status,
                });
            }
        } else {
            let mut file_statuses: Vec<FileStatus> = Vec::new();

            for file in &cat.files {
                let (_, status) = app_file_row_status(config, cat, file, &manifest, env_map).await;
                file_statuses.push(status);
            }

            let has_installed = file_statuses.iter().any(|s| {
                matches!(
                    s,
                    FileStatus::UpToDate | FileStatus::UpdateAvail | FileStatus::UserModified
                )
            });
            let has_not_installed = file_statuses.contains(&FileStatus::NotInstalled);
            let cat_status = if has_installed && has_not_installed {
                // Use the max status of installed files. Only collapse to Partial
                // when all installed files are up-to-date; higher-severity statuses
                // (UpdateAvail, UserModified) take priority because the user action
                // ("shine upgrade") handles updates for installed files.
                let installed_max = file_statuses
                    .iter()
                    .copied()
                    .filter(|s| *s != FileStatus::NotInstalled)
                    .max()
                    .unwrap_or(FileStatus::Partial);
                if installed_max == FileStatus::UpToDate {
                    FileStatus::Partial
                } else {
                    installed_max
                }
            } else {
                file_statuses
                    .iter()
                    .copied()
                    .max()
                    .unwrap_or(FileStatus::NotInstalled)
            };

            let dest_display: Option<String> = if let Some(root) = &cat.destination_root {
                Some(path_display::format_tilde_path(root, &config.home_dir))
            } else if cat.files.len() == 1 {
                resolve_install_destination(cat, &cat.files[0], config)
                    .ok()
                    .map(|p| path_display::format_home(&p, &config.home_dir))
            } else {
                None
            };

            let (sym, status_text) = match cat_status {
                FileStatus::Missing => ("!", "destination missing"),
                FileStatus::UserModified => ("~", "user modified"),
                FileStatus::Partial => ("~", "partial install"),
                FileStatus::UpdateAvail => ("", "update available"),
                FileStatus::UpToDate => ("", "up-to-date"),
                FileStatus::NotInstalled => ("", "not installed"),
            };

            rows.push(AppRow {
                category: cat.name.clone(),
                sym,
                label: cat.name.clone(),
                simple_label: cat.name.clone(),
                dest: dest_display,
                status_text,
                file_status: cat_status,
            });
        }
    }

    Ok(rows)
}

async fn app_file_row_status(
    config: &Config,
    cat: &AppCategory,
    file: &crate::apps::AppFile,
    manifest: &AppManifest,
    env: &BTreeMap<String, String>,
) -> (Option<std::path::PathBuf>, FileStatus) {
    match resolve_install_destination(cat, file, config) {
        Err(_) => (None, FileStatus::NotInstalled),
        Ok(dest) => {
            let status = match manifest.find_by_dest(&dest) {
                None if file.generator.as_ref().is_some_and(|generator| {
                    generator.auto && env.contains_key(&generator.when_env)
                }) && manifest.entries.iter().any(|entry| {
                    entry
                        .source
                        .strip_prefix("app/")
                        .and_then(|source| source.split_once('/'))
                        .is_some_and(|(category, _)| category == cat.name)
                }) =>
                {
                    if source_hash_for_file(config, cat, file, env).await.is_some() {
                        FileStatus::UpdateAvail
                    } else {
                        FileStatus::NotInstalled
                    }
                }
                None => FileStatus::NotInstalled,
                Some(entry) => app_entry_status(config, cat, file, entry, env).await,
            };
            (Some(dest), status)
        }
    }
}

/// Computes the status of an already-resolved manifest entry: compares its
/// recorded content hash against what's currently on disk at
/// `entry.destination`, and (if unchanged) against the current preset
/// source to detect an available update.
///
/// Shared by `app_file_row_status` (used by `list`/`app info`) and `info`'s
/// `collect_app_files` — both need this exact computation once an `AppEntry`
/// has been resolved.
pub(crate) async fn app_entry_status(
    config: &Config,
    cat: &AppCategory,
    file: &crate::apps::AppFile,
    entry: &AppEntry,
    env: &BTreeMap<String, String>,
) -> FileStatus {
    // Generators are intentionally polled on every status/update pass, even
    // when the installed destination was edited. Static sources keep the
    // cheaper existing behavior and are read only after ownership is proven.
    let generator_enabled = file
        .generator
        .as_ref()
        .is_some_and(|generator| generator.auto && env.contains_key(&generator.when_env));
    let manual_generator = file
        .generator
        .as_ref()
        .is_some_and(|generator| !generator.auto);
    let generated_source_hash = if generator_enabled {
        source_hash_for_file(config, cat, file, env).await
    } else {
        None
    };
    if !entry.destination.exists() {
        return FileStatus::Missing;
    }
    match tokio::fs::read(&entry.destination).await {
        Err(_) => FileStatus::Missing,
        Ok(dest_bytes) => {
            let manifest_hash = entry.content_hash;
            match installed_content_hash(file, &dest_bytes) {
                Ok(Some(dest_hash)) if dest_hash == manifest_hash => {
                    if manual_generator {
                        return FileStatus::UpToDate;
                    }
                    let source_hash = if generator_enabled {
                        generated_source_hash
                    } else {
                        source_hash_for_file(config, cat, file, env).await
                    };
                    match source_hash {
                        Some(src) if src != manifest_hash => FileStatus::UpdateAvail,
                        _ => FileStatus::UpToDate,
                    }
                }
                Ok(None) => FileStatus::Missing,
                Ok(Some(_)) | Err(_) => FileStatus::UserModified,
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::apps::AppFile;
    use crate::config::Config;
    use crate::install_core::AppInstallStrategy;
    #[cfg(windows)]
    use crate::test_support::env_lock;
    use std::path::PathBuf;
    use tokio::fs;

    async fn make_temp_dir() -> std::path::PathBuf {
        crate::test_support::make_temp_dir("shine-check").await
    }

    fn sample_app_file() -> AppFile {
        AppFile {
            source_rel: PathBuf::from("dest.txt"),
            target_rel: PathBuf::from("dest.txt"),
            description: None,
            display_name: None,
            legacy_dest_annotation: None,
            transforms: vec![],
            install_strategy: AppInstallStrategy::Copy,
            requires_admin: false,
            restart_hint: None,
            generator: None,
        }
    }

    fn sample_app_category() -> AppCategory {
        AppCategory {
            name: "sample".to_string(),
            description: None,
            destination_root: None,
            files: vec![sample_app_file()],
            list_mode: AppListMode::Files,
            post_upgrade: Vec::new(),
            post_install: Vec::new(),
            uses_metadata: true,
            has_explicit_files: true,
            artifact: None,
        }
    }

    fn sample_app_entry(destination: PathBuf, content_hash: u64) -> AppEntry {
        AppEntry {
            source: "app/sample/dest.txt".to_string(),
            destination,
            backup: None,
            content_hash,
            install_strategy: AppInstallStrategy::Copy,
            uses_env: false,
            requires_admin: false,
        }
    }

    #[tokio::test]
    async fn app_entry_status_reports_missing_when_destination_absent() {
        let dir = make_temp_dir().await;
        let config = Config::new_for_test(&dir);
        let dest = dir.join("dest.txt");
        let entry = sample_app_entry(dest, crate::install_core::hash_content(b"hello"));

        let status = app_entry_status(
            &config,
            &sample_app_category(),
            &sample_app_file(),
            &entry,
            &BTreeMap::new(),
        )
        .await;

        assert_eq!(status, FileStatus::Missing);
        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn app_entry_status_reports_user_modified_when_dest_hash_differs() {
        let dir = make_temp_dir().await;
        let config = Config::new_for_test(&dir);
        let dest = dir.join("dest.txt");
        fs::write(&dest, b"locally edited").await.unwrap();
        let entry = sample_app_entry(dest, crate::install_core::hash_content(b"original"));

        let status = app_entry_status(
            &config,
            &sample_app_category(),
            &sample_app_file(),
            &entry,
            &BTreeMap::new(),
        )
        .await;

        assert_eq!(status, FileStatus::UserModified);
        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn app_entry_status_reports_up_to_date_when_source_unreadable() {
        // No embedded/external source exists for the synthetic "sample"
        // category, so source_hash_for_file returns None and the status
        // falls back to UpToDate once the dest hash matches the manifest.
        let dir = make_temp_dir().await;
        let config = Config::new_for_test(&dir);
        let dest = dir.join("dest.txt");
        fs::write(&dest, b"hello").await.unwrap();
        let entry = sample_app_entry(dest, crate::install_core::hash_content(b"hello"));

        let status = app_entry_status(
            &config,
            &sample_app_category(),
            &sample_app_file(),
            &entry,
            &BTreeMap::new(),
        )
        .await;

        assert_eq!(status, FileStatus::UpToDate);
        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn app_entry_status_reports_update_available_when_source_changed() {
        let dir = make_temp_dir().await;
        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;

        let source_path = config.preset_path(Path::new("app").join("sample").join("dest.txt"));
        fs::create_dir_all(source_path.parent().unwrap())
            .await
            .unwrap();
        fs::write(&source_path, b"new upstream content")
            .await
            .unwrap();

        let dest = dir.join("dest.txt");
        fs::write(&dest, b"hello").await.unwrap();
        let entry = sample_app_entry(dest, crate::install_core::hash_content(b"hello"));

        let status = app_entry_status(
            &config,
            &sample_app_category(),
            &sample_app_file(),
            &entry,
            &BTreeMap::new(),
        )
        .await;

        assert_eq!(status, FileStatus::UpdateAvail);
        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn app_file_row_status_reports_not_installed_without_manifest_entry() {
        let dir = make_temp_dir().await;
        let config = Config::new_for_test(&dir);
        let manifest = AppManifest::default();
        let category = AppCategory {
            destination_root: Some(dir.display().to_string()),
            ..sample_app_category()
        };

        let (dest, status) = app_file_row_status(
            &config,
            &category,
            &sample_app_file(),
            &manifest,
            &BTreeMap::new(),
        )
        .await;

        assert!(dest.is_some());
        assert_eq!(status, FileStatus::NotInstalled);
        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[cfg(not(unix))]
    #[tokio::test]
    async fn installed_shell_rows_use_windows_shim_path() {
        let dir = make_temp_dir().await;
        let cat_dir = dir.join("presets/shell/proxy");
        fs::create_dir_all(&cat_dir).await.unwrap();
        fs::write(
            cat_dir.join("shine.toml"),
            b"[[files]]\nsource = \"set_proxy.ps1\"\ntarget = \"setproxy\"\nneeds_source = true\n",
        )
        .await
        .unwrap();
        fs::write(cat_dir.join("set_proxy.ps1"), b"Write-Output proxy\n")
            .await
            .unwrap();

        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        fs::create_dir_all(config.bin_dir()).await.unwrap();
        fs::write(config.bin_dir().join("setproxy.ps1"), b"# shine-managed\n")
            .await
            .unwrap();

        let rows = build_shell_rows(&config).await.unwrap();
        let row = rows
            .iter()
            .find(|row| row.label == "proxy/setproxy")
            .expect("proxy/setproxy row should exist");

        assert_eq!(row.status_sym, "");
        assert_eq!(row.status_text, "up-to-date");
        assert!(row.is_installed);

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn installed_shell_rows_report_up_to_date() {
        let dir = make_temp_dir().await;
        let cat_dir = dir.join("presets/shell/proxy");
        fs::create_dir_all(&cat_dir).await.unwrap();
        fs::write(
            cat_dir.join("shine.toml"),
            b"[[files]]\nsource = \"set_proxy.sh\"\ntarget = \"setproxy\"\nneeds_source = true\n",
        )
        .await
        .unwrap();
        let script = cat_dir.join("set_proxy.sh");
        fs::write(&script, b"#!/bin/bash\necho proxy\n")
            .await
            .unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&script).await.unwrap().permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&script, perms).await.unwrap();
        }

        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        fs::create_dir_all(config.bin_dir()).await.unwrap();

        crate::shells::handle_install(&config, Some("proxy"), false)
            .await
            .unwrap();

        let rows = build_shell_rows(&config).await.unwrap();
        let row = rows
            .iter()
            .find(|row| row.label == "proxy/setproxy")
            .expect("proxy/setproxy row should exist");

        assert_eq!(row.status_sym, "");
        assert_eq!(row.status_text, "up-to-date");

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn external_template_shell_change_reports_update_available() {
        let dir = make_temp_dir().await;
        let cat_dir = dir.join("presets/shell/proxy");
        fs::create_dir_all(&cat_dir).await.unwrap();
        fs::write(
            cat_dir.join("shine.toml"),
            b"[[files]]\nsource = \"set_proxy.sh\"\ntarget = \"setproxy\"\nneeds_source = true\n",
        )
        .await
        .unwrap();
        let script = cat_dir.join("set_proxy.sh");
        fs::write(
            &script,
            b"#!/bin/bash\n# shine-template: true\necho @@PROXY_HOST@@\n",
        )
        .await
        .unwrap();

        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        fs::create_dir_all(config.bin_dir()).await.unwrap();

        crate::shells::handle_install(&config, Some("proxy"), false)
            .await
            .unwrap();

        fs::write(
            &script,
            b"#!/bin/bash\n# shine-template: true\necho changed @@PROXY_HOST@@\n",
        )
        .await
        .unwrap();

        let rows = build_shell_rows(&config).await.unwrap();
        let row = rows
            .iter()
            .find(|row| row.label == "proxy/setproxy")
            .expect("proxy/setproxy row should exist");

        assert_eq!(row.status_sym, "");
        assert_eq!(row.status_text, "update available");

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn live_raw_shell_change_stays_live_and_current() {
        let dir = make_temp_dir().await;
        let cat_dir = dir.join("presets/shell/custom");
        fs::create_dir_all(&cat_dir).await.unwrap();
        fs::write(
            cat_dir.join("shine.toml"),
            b"[[files]]\nsource = \"tool.sh\"\ntarget = \"mytool\"\n",
        )
        .await
        .unwrap();
        let source = cat_dir.join("tool.sh");
        fs::write(&source, b"#!/bin/sh\necho first\n")
            .await
            .unwrap();

        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        config.external_shell_mode = crate::config::ExternalShellMode::Live;
        fs::create_dir_all(config.bin_dir()).await.unwrap();
        crate::shells::handle_install(&config, Some("custom"), false)
            .await
            .unwrap();
        fs::write(&source, b"#!/bin/sh\necho second\n")
            .await
            .unwrap();

        let rows = build_shell_rows(&config).await.unwrap();
        let row = rows
            .iter()
            .find(|row| row.label == "custom/mytool")
            .unwrap();
        assert_eq!(row.status_sym, "");
        assert_eq!(row.status_text, "live source");
        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn embedded_bun_source_change_reports_update_available() {
        let dir = make_temp_dir().await;
        let config = Config::new_for_test(&dir);
        fs::create_dir_all(config.presets_dir()).await.unwrap();
        fs::create_dir_all(config.bin_dir()).await.unwrap();

        crate::shells::handle_install(&config, Some("agent"), false)
            .await
            .unwrap();

        let extracted = config.presets_dir().join("shell/agent/cc.ts");
        fs::write(&extracted, b"// stale extracted ccenv\n")
            .await
            .unwrap();

        let rows = build_shell_rows(&config).await.unwrap();
        let row = rows
            .iter()
            .find(|row| row.label == "agent/ccenv")
            .expect("agent/ccenv row should exist");

        assert_eq!(row.status_sym, "");
        assert_eq!(row.status_text, "update available");

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn embedded_shell_source_rename_reports_update_available() {
        let dir = make_temp_dir().await;
        let cat_dir = dir.join("presets/shell/agent");
        fs::create_dir_all(&cat_dir).await.unwrap();
        let old_source = if cfg!(windows) { "cc.ps1" } else { "cc.sh" };
        fs::write(
            cat_dir.join("shine.toml"),
            format!(
                "[[files]]\nsource = \"{old_source}\"\ntarget = \"ccenv\"\nneeds_source = true\n"
            ),
        )
        .await
        .unwrap();
        fs::write(cat_dir.join(old_source), b"# old sourced ccenv\n")
            .await
            .unwrap();

        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        fs::create_dir_all(config.bin_dir()).await.unwrap();
        crate::shells::handle_install(&config, Some("agent"), false)
            .await
            .unwrap();

        config.is_external_presets = false;
        let rows = build_shell_rows(&config).await.unwrap();
        let row = rows
            .iter()
            .find(|row| row.label == "agent/ccenv")
            .expect("embedded agent/ccenv row should exist");

        assert_eq!(row.status_sym, "");
        assert_eq!(row.status_text, "update available");

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn external_shell_runtime_and_source_change_reports_update_available() {
        let dir = make_temp_dir().await;
        let cat_dir = dir.join("presets/shell/agent");
        fs::create_dir_all(&cat_dir).await.unwrap();
        let old_source = if cfg!(windows) { "cc.ps1" } else { "cc.sh" };
        fs::write(
            cat_dir.join("shine.toml"),
            format!(
                "[[files]]\nsource = \"{old_source}\"\ntarget = \"ccenv\"\nneeds_source = true\n"
            ),
        )
        .await
        .unwrap();
        fs::write(cat_dir.join(old_source), b"# old sourced ccenv\n")
            .await
            .unwrap();

        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        fs::create_dir_all(config.bin_dir()).await.unwrap();
        crate::shells::handle_install(&config, Some("agent"), false)
            .await
            .unwrap();

        fs::write(
            cat_dir.join("shine.toml"),
            b"[[files]]\nsource = \"cc.ts\"\ntarget = \"ccenv\"\nruntime = \"bun\"\nplatforms = [\"unix\", \"windows\"]\n",
        )
        .await
        .unwrap();
        fs::write(cat_dir.join("cc.ts"), b"console.log('new ccenv');\n")
            .await
            .unwrap();

        let rows = build_shell_rows(&config).await.unwrap();
        let row = rows
            .iter()
            .find(|row| row.label == "agent/ccenv")
            .expect("external agent/ccenv row should exist");

        assert_eq!(row.status_sym, "");
        assert_eq!(row.status_text, "update available");

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn shell_env_change_reports_update_available() {
        let dir = make_temp_dir().await;
        let cat_dir = dir.join("presets/shell/proxy");
        fs::create_dir_all(&cat_dir).await.unwrap();
        fs::write(
            cat_dir.join("shine.toml"),
            b"[[files]]\nsource = \"set_proxy.sh\"\ntarget = \"setproxy\"\nneeds_source = true\n",
        )
        .await
        .unwrap();
        fs::write(
            cat_dir.join("set_proxy.sh"),
            b"#!/bin/bash\n# shine-template: true\nPROXY_NO_PROXY=\"@@PROXY_NO_PROXY@@\"\n",
        )
        .await
        .unwrap();

        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        fs::create_dir_all(config.bin_dir()).await.unwrap();

        crate::shells::handle_install(&config, Some("proxy"), false)
            .await
            .unwrap();

        config.env.insert(
            "PROXY_NO_PROXY".to_string(),
            "localhost,127.0.0.1,::1,.local".to_string(),
        );

        let rows = build_shell_rows(&config).await.unwrap();
        let row = rows
            .iter()
            .find(|row| row.label == "proxy/setproxy")
            .expect("proxy/setproxy row should exist");

        assert_eq!(row.status_sym, "");
        assert_eq!(row.status_text, "update available");

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn category_list_mode_aggregates_explicit_app_files() {
        let dir = make_temp_dir().await;
        let config = Config::new_for_test(&dir);
        fs::create_dir_all(config.shine_dir()).await.unwrap();

        let category = AppCategory {
            name: "ghostty".to_string(),
            description: Some("Ghostty terminal configuration.".to_string()),
            destination_root: Some(dir.join(".config/ghostty").display().to_string()),
            files: vec![
                AppFile {
                    source_rel: PathBuf::from("config.ghostty"),
                    target_rel: PathBuf::from("config.ghostty"),
                    description: None,
                    display_name: None,
                    legacy_dest_annotation: None,
                    transforms: vec![],
                    install_strategy: AppInstallStrategy::Copy,
                    requires_admin: false,
                    restart_hint: None,
                    generator: None,
                },
                AppFile {
                    source_rel: PathBuf::from("themes/shine-light"),
                    target_rel: PathBuf::from("themes/shine-light"),
                    description: None,
                    display_name: None,
                    legacy_dest_annotation: None,
                    transforms: vec!["template".to_string()],
                    install_strategy: AppInstallStrategy::Copy,
                    requires_admin: false,
                    restart_hint: None,
                    generator: None,
                },
            ],
            list_mode: AppListMode::Category,
            post_upgrade: Vec::new(),
            post_install: Vec::new(),
            uses_metadata: true,
            has_explicit_files: true,
            artifact: None,
        };

        let rows = build_app_rows(&config, &[category]).await.unwrap();

        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].label, "ghostty");
        assert_eq!(rows[0].simple_label, "ghostty");
        assert_eq!(rows[0].dest.as_deref(), Some("~/.config/ghostty"));
        assert_eq!(rows[0].file_status, FileStatus::NotInstalled);

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn file_list_mode_keeps_file_labels_for_multi_file_app_simple_list() {
        let dir = make_temp_dir().await;
        let config = Config::new_for_test(&dir);
        fs::create_dir_all(config.shine_dir()).await.unwrap();

        let category = AppCategory {
            name: "sample".to_string(),
            description: None,
            destination_root: Some(dir.join(".config/sample").display().to_string()),
            files: vec![
                AppFile {
                    source_rel: PathBuf::from("config.toml"),
                    target_rel: PathBuf::from("config.toml"),
                    description: None,
                    display_name: None,
                    legacy_dest_annotation: None,
                    transforms: vec![],
                    install_strategy: AppInstallStrategy::Copy,
                    requires_admin: false,
                    restart_hint: None,
                    generator: None,
                },
                AppFile {
                    source_rel: PathBuf::from("theme.toml"),
                    target_rel: PathBuf::from("theme.toml"),
                    description: None,
                    display_name: None,
                    legacy_dest_annotation: None,
                    transforms: vec![],
                    install_strategy: AppInstallStrategy::Copy,
                    requires_admin: false,
                    restart_hint: None,
                    generator: None,
                },
            ],
            list_mode: AppListMode::Files,
            post_upgrade: Vec::new(),
            post_install: Vec::new(),
            uses_metadata: true,
            has_explicit_files: true,
            artifact: None,
        };

        let rows = build_app_rows(&config, &[category]).await.unwrap();

        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].label, "sample/config.toml");
        assert_eq!(rows[0].simple_label, "sample/config.toml");
        assert_eq!(rows[1].label, "sample/theme.toml");
        assert_eq!(rows[1].simple_label, "sample/theme.toml");

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[cfg(windows)]
    #[tokio::test]
    async fn windows_docker_engine_row_uses_engine_destination() {
        let _guard = env_lock();
        let dir = make_temp_dir().await;
        // SAFETY: env_lock() serialises all env-mutation tests in this module,
        // preventing concurrent writes to the process environment from other test threads.
        unsafe { std::env::set_var("HOME", dir.to_str().unwrap()) };
        let config = Config::new_for_test(&dir);
        fs::create_dir_all(config.shine_dir()).await.unwrap();

        let categories = crate::apps::load_embedded_categories(Some("docker-engine")).unwrap();
        let rows = build_app_rows(&config, &categories).await.unwrap();

        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].label, "docker-engine/daemon.jsonc");
        assert_eq!(rows[0].simple_label, "docker-engine");
        assert_eq!(rows[0].file_status, FileStatus::NotInstalled);
        assert_eq!(rows[0].dest.as_deref(), Some("~/.docker/daemon.json"));

        // SAFETY: same env_lock() guard as above.
        unsafe { std::env::remove_var("HOME") };
        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[cfg(windows)]
    #[tokio::test]
    async fn windows_docker_desktop_row_uses_forward_slash_destination() {
        let _guard = env_lock();
        let dir = make_temp_dir().await;
        // SAFETY: env_lock() serialises all env-mutation tests in this module,
        // preventing concurrent writes to the process environment from other test threads.
        unsafe { std::env::set_var("HOME", dir.to_str().unwrap()) };
        let config = Config::new_for_test(&dir);
        fs::create_dir_all(config.shine_dir()).await.unwrap();

        let categories = crate::apps::load_embedded_categories(Some("docker-desktop")).unwrap();
        let rows = build_app_rows(&config, &categories).await.unwrap();

        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].label, "docker-desktop/settings-store.jsonc");
        assert_eq!(rows[0].simple_label, "docker-desktop");
        assert_eq!(rows[0].file_status, FileStatus::NotInstalled);
        assert_eq!(
            rows[0].dest.as_deref(),
            Some("~/AppData/Roaming/Docker/settings-store.json")
        );

        // SAFETY: same env_lock() guard as above.
        unsafe { std::env::remove_var("HOME") };
        fs::remove_dir_all(&dir).await.unwrap();
    }
}