gitpoulpi-config 1.2.0

GitPoulpi config schema, TOML load/save, defaults, and OS-correct path resolution.
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
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
//! Windows Explorer: shell verbs under `HKCU\Software\Classes` (a folder, a
//! folder's background, a drive), plus the App Paths key that lets
//! `gitpoulpi` typed in the address bar start the console-less launcher. All
//! of it is data over the [`Registry`](crate::desktop::registry::Registry)
//! trait, so it runs and is tested on every OS.
//!
//! Every installation has its own verb with its own colour icon:
//! `...\shell\GitPoulpi` for the primary (the name old builds know) and
//! `...\shell\GitPoulpi.i<8hex>` for the others. With `folder_menu_submenu`
//! the verb sits in a shared cascade instead: the container
//! `...\shell\GitPoulpi.Menu.m<8hex of the normalized name>` (label, no icon,
//! `SubCommands=""`, tag [`MENU_TAG`], no owner) holds the members at
//! `...\GitPoulpi.Menu.m...\shell\i<8hex>`. Membership is the tagged members;
//! the last one out deletes the container, and only if it has no subkeys left
//! (`DeleteIfNoSubkeys`; `DeleteTree` is never used on a container).
//!
//! Ownership: a verb is ours when it carries our id in [`OWNER_TAG`], or,
//! for the primary, the legacy marker `X-GitPoulpi=1` (only the primary's
//! root verb and App Paths carry it, so old builds never see another
//! installation's keys). Anything else is foreign and never written or
//! deleted. Remove deletes our keys, then the parents (`Directory\shell`,
//! ...) only when nothing is left in them, which restores what Install found.
//! Windows 11 shows these verbs only in the classic menu (Show more options).

use std::path::Path;

use super::Env;
use crate::desktop::owner::{decide, Facts, Owner};
use crate::desktop::registry::{RegOp, Values};
use crate::desktop::{State, OWNER_TAG};

const CLASSES_KEY: &str = "Software\\Classes";
const APP_PATHS: &str = "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths";
const MARK_NAME: &str = "X-GitPoulpi";
const MARK_VALUE: &str = "1";
/// The submenu container's tag (value: the normalized name), never an owner.
pub const MENU_TAG: &str = "X-GitPoulpi-Menu";
const MENU_PREFIX: &str = "GitPoulpi.Menu.m";
const LEGACY_VERB: &str = "GitPoulpi";

/// The classes that get the verb.
const CLASSES: [&str; 3] = ["Directory", "Directory\\Background", "Drive"];

/// Parents Install may have created, deepest first within each branch.
const PARENTS: [&str; 6] = [
    "Directory\\Background\\shell",
    "Directory\\Background",
    "Directory\\shell",
    "Directory",
    "Drive\\shell",
    "Drive",
];

fn full(rel: &str) -> String {
    format!("{CLASSES_KEY}\\{rel}")
}

fn s(x: &str) -> String {
    x.to_string()
}

/// The App Paths key for a program name (`gitpoulpi.exe`).
pub fn app_path_key(name: &str) -> String {
    format!("{APP_PATHS}\\{name}")
}

fn app_path_values(helper: &Path) -> Values {
    let dir = helper
        .parent()
        .map(|p| p.display().to_string())
        .unwrap_or_default();
    vec![
        (s(""), helper.display().to_string()),
        (s("Path"), dir),
        (s(MARK_NAME), s(MARK_VALUE)),
    ]
}

/// Writes App Paths `<name>` (like `gitpoulpi.exe`) pointing at `helper`.
pub fn app_path_ops(name: &str, helper: &Path) -> Vec<RegOp> {
    let key = app_path_key(name);
    app_path_values(helper)
        .into_iter()
        .map(|(n, v)| RegOp::Set {
            key: key.clone(),
            name: n,
            value: v,
        })
        .collect()
}

/// Deletes App Paths `<name>`. Callers check the marker first.
pub fn app_path_remove(name: &str) -> RegOp {
    RegOp::DeleteTree(app_path_key(name))
}

fn value<'a>(values: &'a Values, name: &str) -> Option<&'a str> {
    values
        .iter()
        .find(|(n, _)| n.eq_ignore_ascii_case(name))
        .map(|(_, v)| v.as_str())
}

fn marked(values: &Values) -> bool {
    value(values, MARK_NAME) == Some(MARK_VALUE)
}

/// Is the App Paths key `<name>` there and ours? (`None`: absent.)
pub fn app_path_is_ours(
    registry: &dyn crate::desktop::registry::Registry,
    name: &str,
) -> Result<Option<bool>, String> {
    let got = registry.read(&[app_path_key(name)])?;
    Ok(got.into_iter().next().flatten().map(|(v, _)| marked(&v)))
}

/// The submenu a setting names: trimmed and lower-cased, so two installations
/// that spell it differently share one container.
fn normalize(name: &str) -> String {
    name.trim().to_lowercase()
}

/// Where this installation's verbs go.
struct Layout {
    id: String,
    short: String,
    primary: bool,
    sub: Option<Sub>,
}

struct Sub {
    /// The label (as typed, trimmed).
    label: String,
    /// The normalized name the container is tagged with.
    norm: String,
    /// The container's key name under `shell`.
    key_name: String,
}

impl Layout {
    fn of(env: &Env) -> Layout {
        let sub = Some(env.submenu.trim()).filter(|n| !n.is_empty()).map(|n| {
            let norm = normalize(n);
            let hash = crate::installation::fnv1a64(norm.as_bytes());
            Sub {
                label: n.to_string(),
                key_name: format!("{MENU_PREFIX}{:08x}", hash & 0xffff_ffff),
                norm,
            }
        });
        Layout {
            id: env.ctx.id.clone(),
            short: env.ctx.short.clone(),
            primary: env.ctx.is_primary(),
            sub,
        }
    }

    /// The root verb's key name.
    fn verb(&self) -> String {
        if self.primary {
            LEGACY_VERB.to_string()
        } else {
            format!("{LEGACY_VERB}.{}", self.short)
        }
    }

    fn root_rel(&self, class: &str) -> String {
        format!("{class}\\shell\\{}", self.verb())
    }

    fn member_rel(&self, class: &str, container: &str) -> String {
        format!("{class}\\shell\\{container}\\shell\\{}", self.short)
    }

    /// Where the verb for `class` should be.
    fn wanted_rel(&self, class: &str) -> String {
        match &self.sub {
            Some(sub) => self.member_rel(class, &sub.key_name),
            None => self.root_rel(class),
        }
    }

    /// Does the verb carry the legacy marker (the primary's root verb)?
    fn legacy_marker(&self) -> bool {
        self.primary && self.sub.is_none()
    }
}

fn icon_value(env: &Env, helper: &Path) -> String {
    match &env.ico {
        Some(ico) => format!("\"{}\"", ico.display()),
        None => format!("\"{}\",0", helper.with_file_name("gitpoulpi.exe").display()),
    }
}

/// The keys (relative to `HKCU`) and values Install writes for the verbs, the
/// container first. `icon` is the `Icon` value.
fn wanted_keys(lay: &Layout, helper: &Path, label: &str, icon: &str) -> Vec<(String, Values)> {
    let command = format!("\"{}\" --folder \"%V\"", helper.display());
    let mut out = Vec::new();
    for class in CLASSES {
        if let Some(sub) = &lay.sub {
            out.push((
                full(&format!("{class}\\shell\\{}", sub.key_name)),
                vec![
                    (s("MUIVerb"), sub.label.clone()),
                    (s("SubCommands"), s("")),
                    (s(MENU_TAG), sub.norm.clone()),
                ],
            ));
        }
        let key = full(&lay.wanted_rel(class));
        let mut values = vec![
            (s(""), s(label)),
            (s("MUIVerb"), s(label)),
            (s("Icon"), s(icon)),
        ];
        if !lay.id.is_empty() {
            values.push((s(OWNER_TAG), lay.id.clone()));
        }
        if lay.legacy_marker() {
            values.push((s(MARK_NAME), s(MARK_VALUE)));
        }
        out.push((key.clone(), values));
        out.push((format!("{key}\\command"), vec![(s(""), command.clone())]));
    }
    out
}

fn norm(values: &Values) -> Vec<(String, String)> {
    let mut v: Vec<(String, String)> = values
        .iter()
        .map(|(n, x)| (n.to_ascii_lowercase(), x.clone()))
        .collect();
    v.sort();
    v
}

/// Does this verb belong to the running installation (or may it adopt it)?
fn mine(env: &Env, values: &Values) -> bool {
    let facts = Facts {
        tag: value(values, OWNER_TAG),
        legacy_marker: marked(values),
        claimed_by: None,
        names_my_exe: false,
    };
    matches!(decide(&facts, &env.ctx), Owner::Mine | Owner::Adopt)
}

type Read = Option<(Values, bool)>;

/// Reads `keys` into a lookup by lower-cased key.
fn read_map(env: &Env, keys: &[String]) -> Result<std::collections::HashMap<String, Read>, String> {
    if keys.is_empty() {
        return Ok(std::collections::HashMap::new());
    }
    let got = env.registry.read(keys)?;
    Ok(keys
        .iter()
        .map(|k| k.to_ascii_lowercase())
        .zip(got)
        .collect())
}

/// `keys` read and the verbs of every class listed, in one registry call.
#[allow(clippy::type_complexity)]
fn snapshot(
    env: &Env,
    keys: &[String],
) -> Result<(std::collections::HashMap<String, Read>, Vec<Vec<String>>), String> {
    let (got, listed) = env.registry.snapshot(keys, &shell_parents())?;
    let map = keys
        .iter()
        .map(|k| k.to_ascii_lowercase())
        .zip(got)
        .collect();
    Ok((map, listed))
}

fn get<'a>(map: &'a std::collections::HashMap<String, Read>, key: &str) -> &'a Read {
    map.get(&key.to_ascii_lowercase()).unwrap_or(&None)
}

/// Our verbs that are not where the settings say: the root verb after a move
/// into a submenu, a member of another submenu. Relative keys.
fn strays(env: &Env, lay: &Layout) -> Result<Vec<String>, String> {
    let listed = env.registry.subkeys_many(&shell_parents())?;
    strays_from(env, lay, listed)
}

/// The `shell` key of each class: where the verbs are listed.
fn shell_parents() -> Vec<String> {
    CLASSES
        .iter()
        .map(|class| full(&format!("{class}\\shell")))
        .collect()
}

/// [`strays`] from the listing of [`shell_parents`] (one list per class).
fn strays_from(env: &Env, lay: &Layout, listed: Vec<Vec<String>>) -> Result<Vec<String>, String> {
    let mut candidates = Vec::new();
    for (class, names) in CLASSES.iter().zip(listed) {
        let wanted = lay.wanted_rel(class).to_ascii_lowercase();
        for name in names {
            // Both root names: an installation has exactly one verb, so the
            // one of its other role (the plain key as a resigned primary, its
            // own `.i<8hex>` key as the primary) is left over, whatever
            // changed its role.
            let rel = if name.eq_ignore_ascii_case(LEGACY_VERB)
                || name.eq_ignore_ascii_case(&format!("{LEGACY_VERB}.{}", lay.short))
            {
                format!("{class}\\shell\\{name}")
            } else if name
                .get(..MENU_PREFIX.len())
                .is_some_and(|p| p.eq_ignore_ascii_case(MENU_PREFIX))
            {
                lay.member_rel(class, &name)
            } else {
                continue;
            };
            if rel.to_ascii_lowercase() != wanted {
                candidates.push(rel);
            }
        }
    }
    let keys: Vec<String> = candidates.iter().map(|r| full(r)).collect();
    let got = read_map(env, &keys)?;
    Ok(candidates
        .into_iter()
        .filter(|rel| {
            get(&got, &full(rel))
                .as_ref()
                .is_some_and(|(v, _)| mine(env, v))
        })
        .collect())
}

/// The ops that delete `entries` (our verbs, relative keys) and then every
/// container they leave empty. A container is only ever deleted by
/// `DeleteIfEmpty` / `DeleteIfNoSubkeys`, which fail while a member is left.
fn delete_entries(entries: &[String]) -> Vec<RegOp> {
    let mut ops = Vec::new();
    for rel in entries {
        ops.push(RegOp::DeleteTree(full(rel)));
    }
    for rel in entries {
        // `<class>\shell\<container>\shell\<short>`: the container is two up.
        let Some((members, _)) = rel.rsplit_once('\\') else {
            continue;
        };
        let Some(container) = members.strip_suffix("\\shell") else {
            continue;
        };
        let last = container.rsplit('\\').next().unwrap_or_default();
        if !last
            .get(..MENU_PREFIX.len())
            .is_some_and(|p| p.eq_ignore_ascii_case(MENU_PREFIX))
        {
            continue;
        }
        ops.push(RegOp::DeleteIfEmpty(full(members)));
        ops.push(RegOp::DeleteIfNoSubkeys(full(container)));
    }
    ops
}

pub(super) fn state(env: &Env, label: &str) -> Result<State, String> {
    inspect(env, label).map(|seen| seen.state)
}

/// What one read of the registry says: the state, and the data the verdict
/// came from, so a rewrite does not read it again.
struct Seen {
    state: State,
    got: std::collections::HashMap<String, Read>,
    stray: Vec<String>,
}

fn inspect(env: &Env, label: &str) -> Result<Seen, String> {
    let lay = Layout::of(env);
    let app_key = app_path_key("gitpoulpi.exe");
    // The label and icon come from our own verbs, so a language change alone
    // (or a status read without the icon) is not "outdated".
    let probe = wanted_keys(&lay, Path::new(""), "", "");
    let mut keys: Vec<String> = probe.iter().map(|(k, _)| k.clone()).collect();
    keys.push(app_key.clone());
    let (got, listed) = snapshot(env, &keys)?;
    let entry_keys: Vec<String> = CLASSES.iter().map(|c| full(&lay.wanted_rel(c))).collect();
    let at: Vec<&Read> = entry_keys.iter().map(|k| get(&got, k)).collect();
    if at
        .iter()
        .any(|g| g.as_ref().is_some_and(|(v, _)| !mine(env, v)))
    {
        return Ok(Seen {
            state: State::Foreign,
            got,
            stray: Vec::new(),
        });
    }
    let stray = strays_from(env, &lay, listed)?;
    let seen = |state: State, got, stray| Ok(Seen { state, got, stray });
    let have = at.iter().any(|g| g.is_some());
    if !have {
        let state = if stray.is_empty() {
            State::NotInstalled
        } else {
            State::Outdated
        };
        return seen(state, got, stray);
    }
    let Some(helper) = env.helper.as_deref() else {
        return seen(State::Outdated, got, stray);
    };
    let found = at
        .iter()
        .filter_map(|g| g.as_ref())
        .find_map(|(v, _)| value(v, "MUIVerb"))
        .map_or_else(
            || label.to_string(),
            |f| super::current_label(f, label, &env.ctx.name),
        );
    let found_icon = at
        .iter()
        .filter_map(|g| g.as_ref())
        .find_map(|(v, _)| value(v, "Icon"))
        .map(str::to_string);
    let icon = match (&env.ico, found_icon) {
        (None, Some(found)) => found,
        _ => icon_value(env, helper),
    };
    let mut current = stray.is_empty();
    for (key, want) in wanted_keys(&lay, helper, &found, &icon) {
        match get(&got, &key) {
            // The container's label is shared and may be spelled by another
            // installation: only its tag and cascade have to be there.
            Some((vals, _)) if lay.sub.is_some() && value(&want, MENU_TAG).is_some() => {
                current &= value(vals, MENU_TAG) == value(&want, MENU_TAG)
                    && value(vals, "SubCommands").is_some();
            }
            Some((vals, _)) if norm(vals) == norm(&want) => {}
            _ => current = false,
        }
    }
    // App Paths belong to the primary: ours must match, a foreign one is left
    // alone and ignored.
    if lay.primary {
        match get(&got, &app_key) {
            None => current = false,
            Some((vals, _)) if marked(vals) => {
                current &= norm(vals) == norm(&app_path_values(helper));
            }
            Some(_) => {}
        }
    }
    let state = if current {
        State::Installed
    } else {
        State::Outdated
    };
    seen(state, got, stray)
}

/// Rewrites the verbs of an entry that is ours and outdated from the data
/// [`inspect`] already read: one registry call to write, none to read again.
/// `Ok(false)`: nothing to rewrite (current, not installed, or not ours).
pub(super) fn sync(env: &Env, label: &str) -> Result<bool, String> {
    let seen = inspect(env, label)?;
    if seen.state != State::Outdated {
        return Ok(false);
    }
    let ops = plan(env, label, &seen.got, &seen.stray)?;
    env.registry.apply(&ops)?;
    Ok(true)
}

pub(super) fn install(env: &Env, label: &str) -> Result<(), String> {
    let helper = env.helper_path()?;
    let lay = Layout::of(env);
    let app_key = app_path_key("gitpoulpi.exe");
    let wanted = wanted_keys(&lay, helper, label, &icon_value(env, helper));
    let mut keys: Vec<String> = CLASSES.iter().map(|c| full(&lay.wanted_rel(c))).collect();
    keys.extend(
        wanted
            .iter()
            .filter(|(_, v)| value(v, MENU_TAG).is_some())
            .map(|(k, _)| k.clone()),
    );
    keys.push(app_key);
    let (got, listed) = snapshot(env, &keys)?;
    let stray = strays_from(env, &lay, listed)?;
    env.registry.apply(&plan(env, label, &got, &stray)?)
}

/// The ops that write the verbs of `env` (and delete `stray`), given the
/// registry as it was read. `got` holds at least the entry and container keys
/// and the App Paths key.
fn plan(
    env: &Env,
    label: &str,
    got: &std::collections::HashMap<String, Read>,
    stray: &[String],
) -> Result<Vec<RegOp>, String> {
    let helper = env.helper_path()?;
    let lay = Layout::of(env);
    let app_key = app_path_key("gitpoulpi.exe");
    let wanted = wanted_keys(&lay, helper, label, &icon_value(env, helper));
    let containers: Vec<String> = wanted
        .iter()
        .filter(|(_, v)| value(v, MENU_TAG).is_some())
        .map(|(k, _)| k.clone())
        .collect();
    let mut ops = Vec::new();
    for class in CLASSES {
        let key = full(&lay.wanted_rel(class));
        match get(got, &key) {
            Some((v, _)) if !mine(env, v) => {
                return Err(format!("{key} is not ours; not overwriting it"));
            }
            // A value the new verb does not have (the legacy marker after a
            // role change) goes with the key. Otherwise the key is
            // overwritten in place: deleting it first would leave the
            // installation without an entry for a moment.
            Some((v, _)) => {
                let new_values = wanted
                    .iter()
                    .find(|(k, _)| k.eq_ignore_ascii_case(&key))
                    .map(|(_, w)| w);
                let stale =
                    new_values.is_some_and(|w| v.iter().any(|(n, _)| value(w, n).is_none()));
                if stale {
                    ops.push(RegOp::DeleteTree(key));
                }
            }
            None => {}
        }
    }
    for key in &containers {
        if let Some((v, _)) = get(got, key) {
            if value(v, MENU_TAG).is_none() {
                return Err(format!(
                    "{key} is not a GitPoulpi submenu; not overwriting it"
                ));
            }
        }
    }
    for (key, values) in wanted {
        for (name, value) in values {
            ops.push(RegOp::Set {
                key: key.clone(),
                name,
                value,
            });
        }
    }
    // App Paths: only the primary writes it; skip a key somebody else owns.
    if lay.primary && get(got, &app_key).as_ref().is_none_or(|(v, _)| marked(v)) {
        ops.extend(app_path_ops("gitpoulpi.exe", helper));
    }
    // The verbs of the installation's other role go last: the new one exists
    // before the old one is deleted, so there is no moment without an entry.
    ops.extend(delete_entries(stray));
    Ok(ops)
}

/// The installation `env` describes stops being the primary and is not here to
/// rewrite its own verbs: its plain `GitPoulpi` verbs move to its own
/// `GitPoulpi.i<8hex>` names (same values and command, without the legacy
/// marker). The new key is written before the plain one is deleted, so it
/// has an entry at every moment. Its next sync rewrites them. App Paths
/// `gitpoulpi.exe` is the primary's and is removed. Returns whether anything
/// moved.
pub(super) fn demote(env: &Env) -> Result<bool, String> {
    let lay = Layout::of(env);
    let own = format!("{LEGACY_VERB}.{}", lay.short);
    let app_key = app_path_key("gitpoulpi.exe");
    let mut keys = Vec::new();
    for class in CLASSES {
        for rel in [
            format!("{class}\\shell\\{LEGACY_VERB}"),
            format!("{class}\\shell\\{LEGACY_VERB}\\command"),
            format!("{class}\\shell\\{own}"),
        ] {
            keys.push(full(&rel));
        }
    }
    keys.push(app_key.clone());
    let got = read_map(env, &keys)?;
    let mut ops = Vec::new();
    let mut deletes = Vec::new();
    for class in CLASSES {
        let plain = full(&format!("{class}\\shell\\{LEGACY_VERB}"));
        let target = full(&format!("{class}\\shell\\{own}"));
        let Some((values, _)) = get(&got, &plain).as_ref().filter(|(v, _)| mine(env, v)) else {
            continue;
        };
        if get(&got, &target).is_some() {
            // Its own verb exists already: the plain one is only a duplicate.
            deletes.push(RegOp::DeleteTree(plain));
            continue;
        }
        for (name, v) in values {
            if name.eq_ignore_ascii_case(MARK_NAME) {
                continue;
            }
            ops.push(RegOp::Set {
                key: target.clone(),
                name: name.clone(),
                value: v.clone(),
            });
        }
        if !lay.id.is_empty() && value(values, OWNER_TAG).is_none() {
            ops.push(RegOp::Set {
                key: target.clone(),
                name: s(OWNER_TAG),
                value: lay.id.clone(),
            });
        }
        if let Some((cmd, _)) = get(&got, &format!("{plain}\\command")) {
            for (name, v) in cmd {
                ops.push(RegOp::Set {
                    key: format!("{target}\\command"),
                    name: name.clone(),
                    value: v.clone(),
                });
            }
        }
        deletes.push(RegOp::DeleteTree(plain));
    }
    if get(&got, &app_key).as_ref().is_some_and(|(v, _)| marked(v)) {
        deletes.push(app_path_remove("gitpoulpi.exe"));
    }
    if ops.is_empty() && deletes.is_empty() {
        return Ok(false);
    }
    ops.extend(deletes);
    env.registry.apply(&ops)?;
    Ok(true)
}

pub(super) fn remove(env: &Env) -> Result<bool, String> {
    let lay = Layout::of(env);
    let app_key = app_path_key("gitpoulpi.exe");
    let mut entries = strays(env, &lay)?;
    let wanted: Vec<String> = CLASSES.iter().map(|c| lay.wanted_rel(c)).collect();
    let mut keys: Vec<String> = wanted.iter().map(|r| full(r)).collect();
    keys.push(app_key.clone());
    let got = read_map(env, &keys)?;
    for rel in wanted {
        if get(&got, &full(&rel))
            .as_ref()
            .is_some_and(|(v, _)| mine(env, v))
        {
            entries.push(rel);
        }
    }
    let mut ops = delete_entries(&entries);
    let mut any = !entries.is_empty();
    if lay.primary && get(&got, &app_key).as_ref().is_some_and(|(v, _)| marked(v)) {
        ops.push(app_path_remove("gitpoulpi.exe"));
        any = true;
    }
    if !any {
        return Ok(false);
    }
    for p in PARENTS {
        ops.push(RegOp::DeleteIfEmpty(full(p)));
    }
    env.registry.apply(&ops)?;
    Ok(true)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::desktop::folder_menu::{self, Manager};
    use crate::desktop::registry::{MemoryRegistry, Registry};
    use crate::open_in::Os;
    use std::path::PathBuf;
    use std::sync::Arc;

    const LABEL: &str = "Open in GitPoulpi";

    /// The legacy verbs (the primary's), relative to `Software\Classes`.
    const ROOTS: [&str; 3] = [
        "Directory\\shell\\GitPoulpi",
        "Directory\\Background\\shell\\GitPoulpi",
        "Drive\\shell\\GitPoulpi",
    ];

    fn env(reg: Arc<MemoryRegistry>) -> Env {
        Env {
            os: Os::Windows,
            data_home: PathBuf::new(),
            config_home: PathBuf::new(),
            home: PathBuf::new(),
            exe: PathBuf::from("C:/gp/gitpoulpi-tui.exe"),
            helper: Some(PathBuf::from("C:/gp/gitpoulpi-launch.exe")),
            has: Arc::new(|_| false),
            registry: reg,
            icon_png: None,
            refresh_hooks: false,
            ctx: crate::desktop::InstallCtx::legacy(),
            ico: None,
            submenu: String::new(),
        }
    }

    fn set(key: &str, name: &str, value: &str) -> RegOp {
        RegOp::Set {
            key: key.into(),
            name: name.into(),
            value: value.into(),
        }
    }

    #[test]
    fn install_writes_exactly_the_planned_keys_and_values() {
        let reg = Arc::new(MemoryRegistry::new());
        let e = env(reg.clone());
        let done = folder_menu::install(&e, LABEL).unwrap();
        assert_eq!(done, [Manager::Explorer]);
        let mut want: Vec<(String, String, String)> = Vec::new();
        let cmd = "\"C:/gp/gitpoulpi-launch.exe\" --folder \"%V\"";
        let icon = "\"C:/gp/gitpoulpi.exe\",0";
        for root in ROOTS {
            let k = full(root).to_ascii_lowercase();
            for (n, v) in [
                ("", LABEL),
                ("MUIVerb", LABEL),
                ("Icon", icon),
                ("X-GitPoulpi", "1"),
            ] {
                want.push((k.clone(), n.into(), v.into()));
            }
            want.push((format!("{k}\\command"), "".into(), cmd.into()));
        }
        let app = app_path_key("gitpoulpi.exe").to_ascii_lowercase();
        want.push((app.clone(), "".into(), "C:/gp/gitpoulpi-launch.exe".into()));
        want.push((app.clone(), "Path".into(), "C:/gp".into()));
        want.push((app, "X-GitPoulpi".into(), "1".into()));
        // Parents exist as empty keys.
        let mut got: Vec<_> = reg
            .dump()
            .into_iter()
            .filter(|(_, _, v)| v != "<key>")
            .collect();
        got.sort();
        want.sort();
        assert_eq!(got, want);
        assert_eq!(
            folder_menu::status(&e, LABEL).entries[0].state,
            State::Installed
        );
    }

    #[test]
    fn remove_restores_the_registry_with_and_without_a_user_verb() {
        // A user verb already under Directory\shell: the parent stays.
        let reg = Arc::new(MemoryRegistry::new());
        reg.apply(&[
            set(&full("Directory\\shell\\Mine"), "", "x"),
            set(APP_PATHS, "keep", "1"),
        ])
        .unwrap();
        let before = reg.dump();
        let e = env(reg.clone());
        folder_menu::install(&e, LABEL).unwrap();
        assert_ne!(reg.dump(), before);
        assert_eq!(folder_menu::remove(&e).unwrap(), [Manager::Explorer]);
        assert_eq!(reg.dump(), before);
        // No verbs before: nothing after (the parents we created are gone).
        let reg = Arc::new(MemoryRegistry::new());
        reg.apply(&[set(CLASSES_KEY, "keep", "1"), set(APP_PATHS, "keep", "1")])
            .unwrap();
        let before = reg.dump();
        let e = env(reg.clone());
        folder_menu::install(&e, LABEL).unwrap();
        folder_menu::remove(&e).unwrap();
        assert_eq!(reg.dump(), before);
    }

    #[test]
    fn a_foreign_verb_is_untouched_by_install_and_remove() {
        let reg = Arc::new(MemoryRegistry::new());
        let k = full(ROOTS[0]);
        reg.apply(&[set(&k, "", "theirs")]).unwrap();
        let before = reg.dump();
        let e = env(reg.clone());
        assert_eq!(
            folder_menu::status(&e, LABEL).entries[0].state,
            State::Foreign
        );
        assert!(folder_menu::install(&e, LABEL).unwrap().is_empty());
        assert!(folder_menu::remove(&e).unwrap().is_empty());
        assert_eq!(reg.dump(), before);
    }

    #[test]
    fn a_moved_exe_is_outdated_and_a_new_label_alone_is_not() {
        let reg = Arc::new(MemoryRegistry::new());
        let e = env(reg.clone());
        folder_menu::install(&e, LABEL).unwrap();
        let st = folder_menu::status(&e, "Ouvrir dans GitPoulpi");
        assert_eq!(st.entries[0].state, State::Installed);
        let moved = Env {
            helper: Some(PathBuf::from("D:/new/gitpoulpi-launch.exe")),
            ..e.clone()
        };
        assert_eq!(
            folder_menu::status(&moved, LABEL).entries[0].state,
            State::Outdated
        );
        assert_eq!(
            folder_menu::sync(&moved, LABEL).unwrap(),
            [Manager::Explorer]
        );
        assert_eq!(
            folder_menu::status(&moved, LABEL).entries[0].state,
            State::Installed
        );
        let cmd = reg.read(&[format!("{}\\command", full(ROOTS[0]))]).unwrap();
        assert!(cmd[0].as_ref().unwrap().0[0].1.contains("D:/new/"));
    }

    #[test]
    fn app_paths_come_and_go_with_the_menu_and_a_foreign_one_stays() {
        let reg = Arc::new(MemoryRegistry::new());
        let e = env(reg.clone());
        let key = app_path_key("gitpoulpi.exe");
        folder_menu::install(&e, LABEL).unwrap();
        assert!(reg.read(std::slice::from_ref(&key)).unwrap()[0].is_some());
        folder_menu::remove(&e).unwrap();
        assert!(reg.read(std::slice::from_ref(&key)).unwrap()[0].is_none());
        // Somebody else's key of that name survives both.
        reg.apply(&[set(&key, "", "theirs")]).unwrap();
        folder_menu::install(&e, LABEL).unwrap();
        assert_eq!(
            reg.read(std::slice::from_ref(&key)).unwrap()[0]
                .as_ref()
                .unwrap()
                .0[0]
                .1,
            "theirs"
        );
        folder_menu::remove(&e).unwrap();
        assert!(reg.read(&[key]).unwrap()[0].is_some());
    }

    #[test]
    fn no_helper_means_unavailable() {
        let reg = Arc::new(MemoryRegistry::new());
        let e = Env {
            helper: None,
            ..env(reg)
        };
        assert!(folder_menu::status(&e, LABEL).unavailable.is_some());
        assert!(folder_menu::install(&e, LABEL).is_err());
    }

    // ---- per-installation verbs and shared submenus ----

    const A: &str = "aaaaaaaa11111111";
    const B: &str = "bbbbbbbb22222222";

    fn env_for(
        reg: &Arc<MemoryRegistry>,
        id: &str,
        primary: Option<&str>,
        submenu: &str,
        ico: Option<&str>,
    ) -> Env {
        Env {
            helper: Some(PathBuf::from(format!("C:/gp/{id}/gitpoulpi-launch.exe"))),
            ctx: crate::desktop::InstallCtx {
                id: id.to_string(),
                short: crate::installation::short_id(id),
                name: format!("GitPoulpi {id}"),
                primary: primary.map(str::to_string),
                known: Vec::new(),
                alias_claims: Vec::new(),
            },
            ico: ico.map(PathBuf::from),
            submenu: submenu.to_string(),
            ..env(reg.clone())
        }
    }

    fn short(id: &str) -> String {
        crate::installation::short_id(id)
    }

    fn values(reg: &MemoryRegistry, rel: &str) -> Option<Values> {
        reg.read(&[full(rel)]).unwrap().remove(0).map(|(v, _)| v)
    }

    fn val(reg: &MemoryRegistry, rel: &str, name: &str) -> Option<String> {
        values(reg, rel).and_then(|v| value(&v, name).map(str::to_string))
    }

    fn state_of(e: &Env) -> State {
        folder_menu::status(e, LABEL).entries[0].state
    }

    fn state_of_label(e: &Env, label: &str) -> State {
        folder_menu::status(e, label).entries[0].state
    }

    /// The container names under `Directory\shell`.
    fn containers(reg: &MemoryRegistry) -> Vec<String> {
        reg.subkeys(&full("Directory\\shell"))
            .unwrap()
            .into_iter()
            .filter(|n| n.starts_with("gitpoulpi.menu."))
            .collect()
    }

    #[test]
    fn each_installation_writes_its_own_verb_with_its_colour_icon() {
        let reg = Arc::new(MemoryRegistry::new());
        let a = env_for(&reg, A, None, "", Some("C:/a/gitpoulpi-aaa.ico"));
        let b = env_for(&reg, B, None, "", Some("C:/b/gitpoulpi-bbb.ico"));
        folder_menu::install(&a, LABEL).unwrap();
        folder_menu::install(&b, LABEL).unwrap();
        for class in CLASSES {
            for (id, ico) in [(A, "C:/a/gitpoulpi-aaa.ico"), (B, "C:/b/gitpoulpi-bbb.ico")] {
                let rel = format!("{class}\\shell\\GitPoulpi.{}", short(id));
                assert_eq!(val(&reg, &rel, "Icon").unwrap(), format!("\"{ico}\""));
                assert_eq!(val(&reg, &rel, "MUIVerb").unwrap(), LABEL);
                assert_eq!(val(&reg, &rel, OWNER_TAG).unwrap(), id);
                assert_eq!(val(&reg, &rel, MARK_NAME), None);
                assert!(val(&reg, &format!("{rel}\\command"), "")
                    .unwrap()
                    .contains(&format!("C:/gp/{id}/")));
            }
        }
        assert_eq!(state_of(&a), State::Installed);
        assert_eq!(state_of(&b), State::Installed);
        // A new colour is a new .ico: only that installation is outdated, and
        // sync rewrites only its Icon.
        let a2 = Env {
            ico: Some(PathBuf::from("C:/a/gitpoulpi-new.ico")),
            ..a.clone()
        };
        assert_eq!(state_of(&a2), State::Outdated);
        assert_eq!(state_of(&b), State::Installed);
        assert_eq!(folder_menu::sync(&a2, LABEL).unwrap(), [Manager::Explorer]);
        let rel = format!("Directory\\shell\\GitPoulpi.{}", short(A));
        assert_eq!(
            val(&reg, &rel, "Icon").unwrap(),
            "\"C:/a/gitpoulpi-new.ico\""
        );
        assert_eq!(state_of(&a2), State::Installed);
        // A status read without the icon does not call a coloured verb outdated.
        let plain = Env { ico: None, ..a2 };
        assert_eq!(state_of(&plain), State::Installed);
    }

    #[test]
    fn the_primary_keeps_the_legacy_key_and_app_paths() {
        let reg = Arc::new(MemoryRegistry::new());
        reg.apply(&[set(CLASSES_KEY, "keep", "1"), set(APP_PATHS, "keep", "1")])
            .unwrap();
        let before = reg.dump();
        let p = env_for(&reg, A, Some(A), "", Some("C:/a/x.ico"));
        folder_menu::install(&p, LABEL).unwrap();
        for root in ROOTS {
            assert_eq!(val(&reg, root, MARK_NAME).as_deref(), Some("1"));
            assert_eq!(val(&reg, root, OWNER_TAG).as_deref(), Some(A));
            assert_eq!(val(&reg, root, "Icon").as_deref(), Some("\"C:/a/x.ico\""));
        }
        let app = app_path_key("gitpoulpi.exe");
        let got = reg.read(&[app]).unwrap().remove(0).unwrap().0;
        assert!(marked(&got));
        assert_eq!(state_of(&p), State::Installed);
        // Removing the primary takes both away.
        assert_eq!(folder_menu::remove(&p).unwrap(), [Manager::Explorer]);
        assert_eq!(reg.dump(), before);
    }

    #[test]
    fn no_primary_means_no_app_paths_gitpoulpi() {
        let reg = Arc::new(MemoryRegistry::new());
        let a = env_for(&reg, A, None, "", None);
        folder_menu::install(&a, LABEL).unwrap();
        let app = app_path_key("gitpoulpi.exe");
        assert_eq!(reg.read(std::slice::from_ref(&app)).unwrap()[0], None);
        assert!(reg.dump().iter().all(|(k, _, _)| !k.contains("app paths")));
        // Another installation being the primary does not make us write it.
        let b = env_for(&reg, B, Some(A), "", None);
        folder_menu::install(&b, LABEL).unwrap();
        assert_eq!(reg.read(std::slice::from_ref(&app)).unwrap()[0], None);
        assert_eq!(state_of(&b), State::Installed);
        // ...nor remove the primary's.
        let p = env_for(&reg, A, Some(A), "", None);
        folder_menu::install(&p, LABEL).unwrap();
        folder_menu::remove(&b).unwrap();
        assert!(reg.read(&[app]).unwrap()[0].is_some());
    }

    #[test]
    fn two_installations_with_one_submenu_share_one_container() {
        let reg = Arc::new(MemoryRegistry::new());
        let a = env_for(&reg, A, None, "Tools", Some("C:/a/x.ico"));
        // Same name, spelled differently: compared trimmed and lower-cased.
        let b = env_for(&reg, B, Some(B), "  tOOls ", Some("C:/b/x.ico"));
        folder_menu::install(&a, LABEL).unwrap();
        folder_menu::install(&b, LABEL).unwrap();
        let names = containers(&reg);
        assert_eq!(names.len(), 1, "{names:?}");
        for class in CLASSES {
            let c = format!("{class}\\shell\\{}", names[0]);
            assert_eq!(val(&reg, &c, "SubCommands").as_deref(), Some(""));
            assert_eq!(val(&reg, &c, MENU_TAG).as_deref(), Some("tools"));
            // No icon (it would dangle), no owner, no legacy marker.
            for n in ["Icon", OWNER_TAG, MARK_NAME] {
                assert_eq!(val(&reg, &c, n), None, "{n}");
            }
            assert!(val(&reg, &c, "MUIVerb").is_some());
            for (id, ico) in [(A, "C:/a/x.ico"), (B, "C:/b/x.ico")] {
                let m = format!("{c}\\shell\\{}", short(id));
                assert_eq!(val(&reg, &m, OWNER_TAG).as_deref(), Some(id));
                assert_eq!(val(&reg, &m, "Icon").unwrap(), format!("\"{ico}\""));
                assert!(values(&reg, &format!("{m}\\command")).is_some());
            }
            // No root verb of either installation.
            assert!(values(&reg, &format!("{class}\\shell\\GitPoulpi")).is_none());
            assert!(values(&reg, &format!("{class}\\shell\\GitPoulpi.{}", short(A))).is_none());
        }
        assert_eq!(state_of(&a), State::Installed);
        assert_eq!(state_of(&b), State::Installed);
    }

    #[test]
    fn the_last_one_out_removes_the_container_and_not_before() {
        let reg = Arc::new(MemoryRegistry::new());
        reg.apply(&[set(&full("Directory\\shell\\Mine"), "", "x")])
            .unwrap();
        let before = reg.dump();
        let a = env_for(&reg, A, None, "Tools", None);
        let b = env_for(&reg, B, None, "Tools", None);
        folder_menu::install(&a, LABEL).unwrap();
        folder_menu::install(&b, LABEL).unwrap();
        assert_eq!(folder_menu::remove(&a).unwrap(), [Manager::Explorer]);
        let names = containers(&reg);
        assert_eq!(names.len(), 1, "the container stays while B is in it");
        let m = format!("Directory\\shell\\{}\\shell\\{}", names[0], short(B));
        assert!(values(&reg, &m).is_some());
        assert!(values(&reg, &m.replace(&short(B), &short(A))).is_none());
        assert_eq!(state_of(&b), State::Installed);
        // The last one out takes the container and restores the registry.
        assert_eq!(folder_menu::remove(&b).unwrap(), [Manager::Explorer]);
        assert!(containers(&reg).is_empty());
        assert_eq!(reg.dump(), before);
        // A second removal has nothing to do.
        assert!(folder_menu::remove(&b).unwrap().is_empty());
    }

    #[test]
    fn changing_the_submenu_moves_only_my_child() {
        let reg = Arc::new(MemoryRegistry::new());
        let a = env_for(&reg, A, None, "Tools", None);
        let b = env_for(&reg, B, None, "Tools", None);
        folder_menu::install(&a, LABEL).unwrap();
        folder_menu::install(&b, LABEL).unwrap();
        let tools = containers(&reg).remove(0);
        // A moves to another submenu: out of Tools (B stays), into the new one.
        let a2 = Env {
            submenu: "Other".into(),
            ..a.clone()
        };
        assert_eq!(state_of(&a2), State::Outdated);
        assert_eq!(folder_menu::sync(&a2, LABEL).unwrap(), [Manager::Explorer]);
        assert_eq!(state_of(&a2), State::Installed);
        assert_eq!(state_of(&b), State::Installed);
        let names = containers(&reg);
        assert_eq!(names.len(), 2, "{names:?}");
        let in_tools = |id: &str| {
            values(
                &reg,
                &format!("Directory\\shell\\{tools}\\shell\\{}", short(id)),
            )
            .is_some()
        };
        assert!(in_tools(B) && !in_tools(A));
        // A moves to the root: the Other container is gone, Tools stays for B.
        let a3 = Env {
            submenu: String::new(),
            ..a
        };
        assert_eq!(state_of(&a3), State::Outdated);
        folder_menu::sync(&a3, LABEL).unwrap();
        assert_eq!(state_of(&a3), State::Installed);
        assert_eq!(containers(&reg), std::slice::from_ref(&tools));
        assert!(in_tools(B));
        assert!(values(&reg, &format!("Directory\\shell\\GitPoulpi.{}", short(A))).is_some());
        // And B is untouched by the root install of A.
        assert_eq!(state_of(&b), State::Installed);
    }

    #[test]
    fn old_builds_cannot_see_non_primary_keys() {
        let reg = Arc::new(MemoryRegistry::new());
        // Somebody else is the primary; this one has a submenu, the third none.
        let a = env_for(&reg, A, Some(B), "Tools", None);
        let c = env_for(&reg, "cccccccc33333333", Some(B), "", None);
        folder_menu::install(&a, LABEL).unwrap();
        folder_menu::install(&c, LABEL).unwrap();
        // An old build looks at the fixed legacy keys and App Paths, and
        // matches only the exact marker value.
        let old: Vec<String> = ROOTS
            .iter()
            .map(|r| full(r))
            .chain([app_path_key("gitpoulpi.exe")])
            .collect();
        assert!(reg.read(&old).unwrap().iter().all(Option::is_none));
        assert!(
            reg.dump()
                .iter()
                .all(|(_, n, v)| !(n == MARK_NAME && v == MARK_VALUE)),
            "{:?}",
            reg.dump()
        );
        // Their removal never touches the legacy keys either.
        reg.apply(&[set(&full(ROOTS[0]), MARK_NAME, MARK_VALUE)])
            .unwrap();
        folder_menu::remove(&a).unwrap();
        folder_menu::remove(&c).unwrap();
        assert!(values(&reg, ROOTS[0]).is_some());
    }

    #[test]
    fn an_old_primary_verb_without_a_tag_is_adopted_and_tagged() {
        let reg = Arc::new(MemoryRegistry::new());
        let legacy = env(reg.clone());
        folder_menu::install(&legacy, LABEL).unwrap();
        let p = env_for(&reg, A, Some(A), "", None);
        // What an old build wrote carries the marker only.
        assert_eq!(val(&reg, ROOTS[0], OWNER_TAG), None);
        assert_eq!(state_of(&p), State::Outdated);
        folder_menu::sync(&p, LABEL).unwrap();
        assert_eq!(val(&reg, ROOTS[0], OWNER_TAG).as_deref(), Some(A));
        assert_eq!(state_of(&p), State::Installed);
    }

    #[test]
    fn another_installations_verb_is_never_ours() {
        let reg = Arc::new(MemoryRegistry::new());
        let a = env_for(&reg, A, None, "", None);
        folder_menu::install(&a, LABEL).unwrap();
        // The same key names but another owner: foreign for us.
        for class in CLASSES {
            let rel = format!("{class}\\shell\\GitPoulpi.{}", short(A));
            reg.apply(&[set(&full(&rel), OWNER_TAG, B)]).unwrap();
        }
        let before = reg.dump();
        assert_eq!(state_of(&a), State::Foreign);
        assert!(folder_menu::install(&a, LABEL).unwrap().is_empty());
        assert!(folder_menu::remove(&a).unwrap().is_empty());
        assert_eq!(reg.dump(), before);
    }

    // ---- one verb per installation at every moment ----

    /// A registry that applies one operation at a time and checks, after each,
    /// that installation `watched` still has a verb (plain or its own) in every
    /// class: the moment without an entry Explorer would show. It also notes
    /// the keys deleted.
    struct Watch {
        inner: MemoryRegistry,
        watched: String,
        deleted: std::sync::Mutex<Vec<String>>,
        gaps: std::sync::Mutex<Vec<String>>,
    }

    impl Watch {
        fn new(watched: &str) -> Arc<Watch> {
            Arc::new(Watch {
                inner: MemoryRegistry::new(),
                watched: watched.to_string(),
                deleted: Default::default(),
                gaps: Default::default(),
            })
        }

        /// The classes where the watched installation has no verb now.
        fn missing(&self) -> Vec<String> {
            let tagged = |rel: String| {
                self.inner
                    .read(&[full(&rel)])
                    .unwrap()
                    .remove(0)
                    .is_some_and(|(v, _)| value(&v, OWNER_TAG) == Some(self.watched.as_str()))
            };
            CLASSES
                .iter()
                .filter(|c| {
                    let plain = format!("{c}\\shell\\GitPoulpi");
                    let own = format!("{c}\\shell\\GitPoulpi.{}", short(&self.watched));
                    !(tagged(plain) || tagged(own))
                })
                .map(|c| c.to_string())
                .collect()
        }
    }

    impl Registry for Watch {
        fn read(&self, keys: &[String]) -> Result<Vec<Option<(Values, bool)>>, String> {
            self.inner.read(keys)
        }
        fn subkeys(&self, key: &str) -> Result<Vec<String>, String> {
            self.inner.subkeys(key)
        }
        fn apply(&self, ops: &[RegOp]) -> Result<(), String> {
            let had_one = self.missing().is_empty();
            for op in ops {
                if let RegOp::DeleteTree(k) = op {
                    self.deleted.lock().unwrap().push(k.clone());
                }
                self.inner.apply(std::slice::from_ref(op))?;
                if had_one {
                    let missing = self.missing();
                    if !missing.is_empty() {
                        self.gaps
                            .lock()
                            .unwrap()
                            .push(format!("{op:?}: {missing:?}"));
                    }
                }
            }
            Ok(())
        }
    }

    fn env_over(reg: &Arc<Watch>, id: &str, primary: Option<&str>) -> Env {
        Env {
            registry: reg.clone(),
            ..env_for(&Arc::new(MemoryRegistry::new()), id, primary, "", None)
        }
    }

    fn roots_of(reg: &Watch) -> Vec<String> {
        let mut names: Vec<String> = reg
            .inner
            .subkeys(&full("Directory\\shell"))
            .unwrap()
            .into_iter()
            .filter(|n| n.to_ascii_lowercase().starts_with("gitpoulpi"))
            .collect();
        names.sort();
        names.dedup();
        names
    }

    fn own_verb(id: &str) -> String {
        format!("gitpoulpi.{}", short(id))
    }

    #[test]
    fn becoming_the_primary_leaves_one_verb_and_never_none() {
        let reg = Watch::new(A);
        folder_menu::install(&env_over(&reg, A, None), LABEL).unwrap();
        assert_eq!(roots_of(&reg), [own_verb(A)]);
        let primary = env_over(&reg, A, Some(A));
        // Its own verb is the leftover of the old role.
        assert_eq!(state_of(&primary), State::Outdated);
        folder_menu::install(&primary, LABEL).unwrap();
        assert_eq!(roots_of(&reg), ["gitpoulpi"], "the plain verb only");
        assert_eq!(state_of(&primary), State::Installed);
        assert!(reg.gaps.lock().unwrap().is_empty(), "{:?}", reg.gaps);
    }

    #[test]
    fn resigning_leaves_one_verb_written_before_the_plain_one_goes() {
        let reg = Watch::new(A);
        folder_menu::install(&env_over(&reg, A, Some(A)), LABEL).unwrap();
        assert_eq!(roots_of(&reg), ["gitpoulpi"]);
        let own = env_over(&reg, A, None);
        assert_eq!(state_of(&own), State::Outdated);
        folder_menu::install(&own, LABEL).unwrap();
        assert_eq!(roots_of(&reg), [own_verb(A)]);
        assert_eq!(state_of(&own), State::Installed);
        assert!(reg.gaps.lock().unwrap().is_empty(), "{:?}", reg.gaps);
    }

    #[test]
    fn rewriting_a_verb_in_place_never_deletes_it_first() {
        let reg = Watch::new(A);
        let own = env_over(&reg, A, None);
        folder_menu::install(&own, LABEL).unwrap();
        // Outdated (its helper moved) and rewritten: overwritten, not recreated.
        let moved = Env {
            helper: Some(PathBuf::from("C:/elsewhere/gitpoulpi-launch.exe")),
            ..own
        };
        assert_eq!(state_of(&moved), State::Outdated);
        folder_menu::sync(&moved, LABEL).unwrap();
        assert_eq!(state_of(&moved), State::Installed);
        assert!(reg.deleted.lock().unwrap().is_empty(), "{:?}", reg.deleted);
    }

    #[test]
    fn a_launcher_rename_rewrites_the_verb_label_in_place() {
        let reg = Watch::new(A);
        let named = |name: &str| {
            let mut e = env_over(&reg, A, None);
            e.ctx.name = name.to_string();
            e
        };
        let label = |name: &str| format!("Open in {name}");
        let before = named("GitPoulpi (Chocolatey)");
        folder_menu::install(&before, &label("GitPoulpi (Chocolatey)")).unwrap();
        let verb = format!("Directory\\shell\\GitPoulpi.{}", short(A));
        assert_eq!(
            val(&reg.inner, &verb, "MUIVerb").as_deref(),
            Some("Open in GitPoulpi (Chocolatey)")
        );
        // The name changed: the verb is outdated, a sync rewrites it, and the
        // install after it has nothing left to do.
        let after = named("GitPoulpi Test");
        let new_label = label("GitPoulpi Test");
        assert_eq!(state_of_label(&after, &new_label), State::Outdated);
        assert_eq!(
            folder_menu::sync(&after, &new_label).unwrap(),
            [Manager::Explorer]
        );
        for name in ["", "MUIVerb"] {
            assert_eq!(
                val(&reg.inner, &verb, name).as_deref(),
                Some("Open in GitPoulpi Test"),
                "{name:?}"
            );
        }
        assert_eq!(state_of_label(&after, &new_label), State::Installed);
        assert!(folder_menu::install(&after, &new_label).unwrap().is_empty());
        assert!(reg.deleted.lock().unwrap().is_empty());
        assert!(reg.gaps.lock().unwrap().is_empty(), "{:?}", reg.gaps);
        // A language change alone, name unchanged, stays current.
        assert_eq!(
            state_of_label(&after, "Ouvrir dans GitPoulpi Test"),
            State::Installed
        );
    }

    #[test]
    fn the_verb_of_another_installation_is_never_a_leftover() {
        let reg = Watch::new(A);
        // B is the primary with the plain verb; A has its own.
        folder_menu::install(&env_over(&reg, B, Some(B)), LABEL).unwrap();
        folder_menu::install(&env_over(&reg, A, Some(B)), LABEL).unwrap();
        assert_eq!(roots_of(&reg), ["gitpoulpi".to_string(), own_verb(A)]);
        // A is told it is the primary but B still holds the plain key: A's
        // install leaves it alone.
        let a_primary = env_over(&reg, A, Some(A));
        assert_eq!(state_of(&a_primary), State::Foreign);
        assert!(folder_menu::install(&a_primary, LABEL).unwrap().is_empty());
        assert_eq!(roots_of(&reg), ["gitpoulpi".to_string(), own_verb(A)]);
    }

    #[test]
    fn demoting_the_primary_moves_its_verbs_to_its_own_names_without_a_gap() {
        let reg = Watch::new(A);
        let primary = env_over(&reg, A, Some(A));
        folder_menu::install(&primary, LABEL).unwrap();
        assert_eq!(folder_menu::demote(&primary).unwrap(), [Manager::Explorer]);
        assert_eq!(roots_of(&reg), [own_verb(A)]);
        let rel = format!("Directory\\shell\\{}", own_verb(A));
        assert_eq!(val(&reg.inner, &rel, OWNER_TAG).as_deref(), Some(A));
        assert_eq!(val(&reg.inner, &rel, "X-GitPoulpi"), None);
        let command = format!("{rel}\\command");
        assert!(val(&reg.inner, &command, "").is_some());
        assert_eq!(state_of(&env_over(&reg, A, Some(B))), State::Installed);
        assert!(reg.gaps.lock().unwrap().is_empty(), "{:?}", reg.gaps);
        let app = reg.inner.read(&[app_path_key("gitpoulpi.exe")]).unwrap();
        assert!(app[0].is_none(), "App Paths is the primary's");
        // Nothing left to move.
        assert!(folder_menu::demote(&primary).unwrap().is_empty());
    }

    #[test]
    fn uninstalling_the_primary_removes_both_its_verbs_and_only_its_own() {
        let reg = Watch::new(A);
        folder_menu::install(&env_over(&reg, B, Some(A)), LABEL).unwrap();
        folder_menu::install(&env_over(&reg, A, Some(A)), LABEL).unwrap();
        // The orphan an old build left: its own key next to the plain one.
        for class in CLASSES {
            let rel = format!("{class}\\shell\\{}", own_verb(A));
            reg.apply(&[
                set(&full(&rel), OWNER_TAG, A),
                set(&full(&format!("{rel}\\command")), "", "x"),
            ])
            .unwrap();
        }
        assert_eq!(roots_of(&reg).len(), 3, "{:?}", roots_of(&reg));
        let primary = env_over(&reg, A, Some(A));
        assert_eq!(folder_menu::remove(&primary).unwrap(), [Manager::Explorer]);
        assert_eq!(roots_of(&reg), [own_verb(B)]);
        assert_eq!(state_of(&env_over(&reg, B, Some(A))), State::Installed);
    }

    // ---- a colour change is cheap ----

    /// A registry that counts its calls (each one is a PowerShell process on
    /// Windows) and keeps what was applied.
    struct Counting {
        inner: MemoryRegistry,
        calls: std::sync::Mutex<Vec<&'static str>>,
    }

    impl Counting {
        fn new() -> Arc<Counting> {
            Arc::new(Counting {
                inner: MemoryRegistry::new(),
                calls: Default::default(),
            })
        }

        fn note(&self, what: &'static str) {
            self.calls.lock().unwrap().push(what);
        }

        fn take(&self) -> Vec<&'static str> {
            std::mem::take(&mut *self.calls.lock().unwrap())
        }
    }

    impl Registry for Counting {
        fn read(&self, keys: &[String]) -> Result<Vec<Option<(Values, bool)>>, String> {
            self.note("read");
            self.inner.read(keys)
        }
        fn subkeys(&self, key: &str) -> Result<Vec<String>, String> {
            self.note("subkeys");
            self.inner.subkeys(key)
        }
        fn subkeys_many(&self, keys: &[String]) -> Result<Vec<Vec<String>>, String> {
            self.note("subkeys_many");
            keys.iter().map(|k| self.inner.subkeys(k)).collect()
        }
        #[allow(clippy::type_complexity)]
        fn snapshot(
            &self,
            keys: &[String],
            parents: &[String],
        ) -> Result<(Vec<Option<(Values, bool)>>, Vec<Vec<String>>), String> {
            self.note("snapshot");
            Ok((
                self.inner.read(keys)?,
                parents
                    .iter()
                    .map(|k| self.inner.subkeys(k))
                    .collect::<Result<_, _>>()?,
            ))
        }
        fn apply(&self, ops: &[RegOp]) -> Result<(), String> {
            self.note("apply");
            self.inner.apply(ops)
        }
    }

    #[test]
    fn a_colour_change_is_at_most_two_registry_calls_and_rewrites_only_its_own_icon() {
        let reg = Counting::new();
        let over = |id: &str, ico: &str| Env {
            registry: reg.clone(),
            ..env_for(&Arc::new(MemoryRegistry::new()), id, None, "", Some(ico))
        };
        folder_menu::install(&over(A, "C:/a/gitpoulpi-old.ico"), LABEL).unwrap();
        folder_menu::install(&over(B, "C:/b/gitpoulpi-b.ico"), LABEL).unwrap();
        let before = reg.inner.dump();
        reg.take();

        let changed = folder_menu::sync(&over(A, "C:/a/gitpoulpi-new.ico"), LABEL).unwrap();
        assert_eq!(changed, [Manager::Explorer]);
        let calls = reg.take();
        assert_eq!(
            calls,
            ["snapshot", "apply"],
            "each call is a PowerShell process"
        );

        // Exactly this installation's three Icon values moved.
        let after = reg.inner.dump();
        let moved: Vec<_> = after.iter().filter(|e| !before.contains(e)).collect();
        assert_eq!(moved.len(), CLASSES.len(), "{moved:?}");
        for (key, name, value) in moved {
            assert!(key
                .to_ascii_lowercase()
                .ends_with(&format!("gitpoulpi.{}", short(A))));
            assert_eq!(name, "Icon");
            assert_eq!(value, "\"C:/a/gitpoulpi-new.ico\"");
        }
        assert_eq!(after.len(), before.len());
        // Nothing to do the second time: reads only, no write.
        assert!(folder_menu::sync(&over(A, "C:/a/gitpoulpi-new.ico"), LABEL)
            .unwrap()
            .is_empty());
        assert!(!reg.take().contains(&"apply"));
    }
}