knotra 0.34.0

knotra: multi-project VCS dashboard and bulk-operation tool.
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
//! Application configuration: loading, validation, and persistence.
//!
//! Config is stored as TOML in `~/.config/knotra/config.toml`.
//! Workspace definitions live in `~/.config/knotra/workspaces/`.
//! Operation history lives in `~/.local/share/knotra/history/`.

use knotra_ui::{ThemeChoice, i18n::Locale};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum DashboardGrouping {
    #[default]
    Attention,
    ProjectGroup,
    None,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum DashboardSort {
    #[default]
    Recommended,
    NameAscending,
}

/// Application-level configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AppConfig {
    /// UI display locale.
    pub locale: Locale,
    /// RFC-070 D1: replaces `dark_theme: bool`. `load_config` resolves an
    /// older file's `dark_theme` into this field before the normal typed
    /// parse runs — see `load_config`'s own doc comment — so this field
    /// itself never needs to know the old key existed.
    pub theme: ThemeChoice,
    /// Background refresh interval in seconds (0 = manual only). RFC-073
    /// D12: always one of [`REFRESH_AUTO_STEPS`] once loaded, the same
    /// "select, not a typed number" guarantee D11/D12 give `zoom_percent`'s
    /// own [`ZOOM_STEPS`] — `load_config` snaps any other saved value to
    /// the nearest step.
    pub refresh_interval_secs: u32,
    /// Maximum concurrent repository status reads. RFC-073 D12: always one
    /// of [`SIMULTANEOUS_READS_STEPS`] once loaded.
    pub max_concurrent_reads: usize,
    /// Path to the preferred external editor.
    pub external_editor: Option<String>,
    /// Path to the preferred external merge tool.
    pub external_merge_tool: Option<String>,
    /// Maximum number of operation log entries to keep in memory. RFC-073
    /// D12: always one of [`LOG_SIZE_STEPS`] once loaded. Amendment A5 §2:
    /// takes effect at once in both directions, no restart needed — a
    /// *smaller* value truncates the in-memory list immediately
    /// (`app/misc.rs::handle_settings`'s own `MaxLogEntriesChanged` arm);
    /// a *larger* one re-reads the new window from disk as a `Task`, off
    /// the update path, and merges it back in
    /// (`app/background/mod.rs::history_logs_reloaded`).
    pub max_log_entries: usize,
    /// Enable file-system event monitoring (off by default).
    pub fs_watch_enabled: bool,
    /// Debounce interval in seconds before a FS event triggers a refresh.
    /// RFC-073 D12: always one of [`FS_DEBOUNCE_STEPS`] once loaded.
    pub fs_debounce_secs: u32,
    /// Dashboard section grouping.
    pub dashboard_grouping: DashboardGrouping,
    /// Dashboard project ordering.
    pub dashboard_sort: DashboardSort,
    /// Whether the In progress attention section is collapsed.
    pub dashboard_in_progress_collapsed: bool,
    /// Whether the All set attention section is collapsed.
    pub dashboard_all_set_collapsed: bool,
    /// Parent of the folder most recently added as a project (Task 102) —
    /// "Browse…" starts there next time, if it still exists.
    pub last_project_parent: Option<PathBuf>,
    /// RFC-071 D11: iced's application `scale_factor` is `zoom_percent /
    /// 100`. Always one of [`ZOOM_STEPS`] once loaded — `load_config` snaps
    /// any other saved value to the nearest step (`nearest_zoom_step`), so
    /// every other reader of this field can treat it as pre-validated.
    pub zoom_percent: u16,
}

/// RFC-071 D11: the only four zoom percentages the app allows, ascending.
/// `Settings`' own "Zoom" select offers exactly these, in this order, and
/// [`zoom_step_up`]/[`zoom_step_down`] step through this same list.
pub const ZOOM_STEPS: [u16; 4] = [100, 115, 130, 150];

/// RFC-071 D11: snaps `value` to the nearest of [`ZOOM_STEPS`] — ties
/// favour the lower step (the direction closer to 100%, the least visually
/// disruptive way to round when a saved value cannot be trusted). Used by
/// `load_config` to recover a `config.toml` whose `zoom_percent` holds a
/// value outside the allowed set (an older knotra version did not exist to
/// write one, but a hand-edited file could).
fn nearest_zoom_step(value: u16) -> u16 {
    let mut best = ZOOM_STEPS[0];
    let mut best_diff = (i32::from(best) - i32::from(value)).abs();
    for &step in &ZOOM_STEPS[1..] {
        let diff = (i32::from(step) - i32::from(value)).abs();
        if diff < best_diff {
            best = step;
            best_diff = diff;
        }
    }
    best
}

/// RFC-071 D11: one step up in [`ZOOM_STEPS`], saturating at the top —
/// `Ctrl+Plus`/`Ctrl+=`.
#[must_use]
pub fn zoom_step_up(current: u16) -> u16 {
    let idx = ZOOM_STEPS
        .iter()
        .position(|&s| s == current)
        .expect("zoom_percent is always one of ZOOM_STEPS once loaded");
    ZOOM_STEPS[(idx + 1).min(ZOOM_STEPS.len() - 1)]
}

/// RFC-071 D11: one step down in [`ZOOM_STEPS`], saturating at the bottom —
/// `Ctrl+Minus`.
#[must_use]
pub fn zoom_step_down(current: u16) -> u16 {
    let idx = ZOOM_STEPS
        .iter()
        .position(|&s| s == current)
        .expect("zoom_percent is always one of ZOOM_STEPS once loaded");
    ZOOM_STEPS[idx.saturating_sub(1)]
}

// --- RFC-073 D12: the four number settings, as selects -------------------

/// "Refresh automatically": off, or one of four intervals, in seconds.
pub const REFRESH_AUTO_STEPS: [u32; 5] = [0, 30, 60, 300, 900];

/// "Simultaneous reads" (was "Max concurrent reads").
pub const SIMULTANEOUS_READS_STEPS: [usize; 4] = [2, 4, 8, 16];

/// "Operation log size" (was "Max operation log entries").
pub const LOG_SIZE_STEPS: [usize; 4] = [100, 200, 500, 1000];

/// "Change detection interval" (FS-watch debounce), in seconds.
pub const FS_DEBOUNCE_STEPS: [u32; 3] = [1, 2, 5];

/// Snaps `value` to the nearest of `steps` (ascending) — ties favour the
/// lower step, the same rule [`nearest_zoom_step`] uses. Shared by every
/// `u32`-typed stepped setting D12 adds; `nearest_zoom_step` itself predates
/// this and is left as its own `u16` function rather than folded in, since
/// zoom is RFC-071's, not D12's.
fn nearest_step_u32(steps: &[u32], value: u32) -> u32 {
    let mut best = steps[0];
    let mut best_diff = best.abs_diff(value);
    for &step in &steps[1..] {
        let diff = step.abs_diff(value);
        if diff < best_diff {
            best = step;
            best_diff = diff;
        }
    }
    best
}

/// [`nearest_step_u32`], for the two `usize`-typed stepped settings — a
/// separate function, not a generic one, because `usize` has no portable
/// lossless conversion to a common signed type `u32` could share (its
/// width is platform-defined), and both settings' own `AppConfig` fields
/// are `usize` already, for reasons outside D12's scope to change.
fn nearest_step_usize(steps: &[usize], value: usize) -> usize {
    let mut best = steps[0];
    let mut best_diff = best.abs_diff(value);
    for &step in &steps[1..] {
        let diff = step.abs_diff(value);
        if diff < best_diff {
            best = step;
            best_diff = diff;
        }
    }
    best
}

impl Default for AppConfig {
    fn default() -> Self {
        AppConfig {
            locale: Locale::En,
            theme: ThemeChoice::default(),
            refresh_interval_secs: 60,
            max_concurrent_reads: 8,
            external_editor: None,
            external_merge_tool: None,
            max_log_entries: 200,
            fs_watch_enabled: false,
            fs_debounce_secs: 2,
            dashboard_grouping: DashboardGrouping::default(),
            dashboard_sort: DashboardSort::default(),
            dashboard_in_progress_collapsed: false,
            dashboard_all_set_collapsed: true,
            last_project_parent: None,
            zoom_percent: 100,
        }
    }
}

/// Paths used by the application.
#[derive(Debug, Clone)]
pub struct AppPaths {
    pub config_file: PathBuf,
    pub workspaces_dir: PathBuf,
    pub history_dir: PathBuf,
}

impl AppPaths {
    /// Resolves OS-standard config/data directories, degrading — never
    /// failing — to the current working directory when either cannot be
    /// determined (Handoff 033 Task B). The fallback used to be silent: a
    /// user whose environment could not resolve `config_dir()` had their
    /// settings written to `./knotra/config.toml` in whatever directory
    /// knotra happened to be launched from, with no indication anything
    /// unusual had happened. It still falls back — knotra's documented
    /// contract is defaults-plus-warning, never a failed start — but now
    /// says so, and where, mirroring [`crate::config::load_config`]'s own
    /// `(value, Option<String>)` shape rather than introducing a second one.
    pub fn resolve() -> (Self, Option<String>) {
        Self::resolve_from(dirs::config_dir(), dirs::data_local_dir())
    }

    /// The pure decision `resolve` makes, with `dirs::config_dir()`/
    /// `dirs::data_local_dir()`'s results passed in rather than read
    /// directly — a private seam so the warning-producing branch is
    /// testable without `dirs::` being injectable itself (Handoff 034 Item
    /// 2: `resolve` alone could exercise only the "both resolve" case on
    /// any real machine, leaving the actual deliverable of Task B —
    /// producing a correct warning — with no coverage at all).
    fn resolve_from(
        config_root: Option<PathBuf>,
        data_root: Option<PathBuf>,
    ) -> (Self, Option<String>) {
        let mut warnings = Vec::new();

        let config_root = config_root.unwrap_or_else(|| {
            let fallback = PathBuf::from(".");
            warnings.push(format!(
                "Could not determine the OS config directory; using \"{}\" \
                 (the current directory) instead. Settings will be written \
                 to a different place depending on where knotra is launched \
                 from.",
                fallback.display()
            ));
            fallback
        });
        let config_base = config_root.join("knotra");

        let data_root = data_root.unwrap_or_else(|| {
            let fallback = PathBuf::from(".");
            warnings.push(format!(
                "Could not determine the OS data directory; using \"{}\" \
                 (the current directory) instead for operation history.",
                fallback.display()
            ));
            fallback
        });
        let data_base = data_root.join("knotra");

        let paths = AppPaths {
            config_file: config_base.join("config.toml"),
            workspaces_dir: config_base.join("workspaces"),
            history_dir: data_base.join("history"),
        };

        // Joined with a bare newline — distinct from `app::init`'s "\n\n"
        // join of *its* two independent startup warnings (path resolution
        // and config parsing). Both are defensible; this is the one this
        // function commits to, pinned by the tests below rather than left
        // to whichever separator happened to be typed (Handoff 034 §2).
        let warning = (!warnings.is_empty()).then(|| warnings.join("\n"));
        (paths, warning)
    }

    #[cfg(test)]
    pub fn under(base: PathBuf) -> Self {
        AppPaths {
            config_file: base.join("config").join("config.toml"),
            workspaces_dir: base.join("config").join("workspaces"),
            history_dir: base.join("data").join("history"),
        }
    }
}

/// RFC-070 D1: resolves `theme`/`dark_theme` in a raw, already-parsed TOML
/// table before the normal typed parse runs, so `AppConfig::theme` itself
/// can stay a plain, strictly-typed field with no knowledge of the
/// compatibility rule. Mutates `table` in place — removes `dark_theme`
/// unconditionally (a stray key an older save left behind, never written
/// again after this loads and the file is next saved) and leaves `theme`
/// set to a value that always parses.
///
/// - `theme` present and valid: wins, untouched.
/// - `theme` present but unrecognized: replaced with the default, and a
///   warning is returned — every *other* field in the same file still
///   loads normally, since this runs before the typed parse, not as a
///   side effect of it failing.
/// - `theme` absent, `dark_theme` present: maps `true`/`false` to
///   `Dark`/`Light`.
/// - Neither present: the default.
fn resolve_theme_key(
    table: &mut toml::value::Table,
    config_path: &std::path::Path,
) -> Option<String> {
    let raw_theme = table.remove("theme");
    let raw_dark_theme = table.remove("dark_theme").and_then(|v| v.as_bool());

    let (resolved, warning) = match raw_theme {
        Some(value) => {
            let shown = value
                .as_str()
                .map(str::to_owned)
                .unwrap_or_else(|| value.to_string());
            match value.try_into::<ThemeChoice>() {
                Ok(choice) => (choice, None),
                Err(_) => (
                    ThemeChoice::default(),
                    Some(format!(
                        "Unknown theme {shown:?} in config.toml (using default); Path: {}",
                        config_path.display()
                    )),
                ),
            }
        }
        None => (
            match raw_dark_theme {
                Some(true) => ThemeChoice::Dark,
                Some(false) => ThemeChoice::Light,
                None => ThemeChoice::default(),
            },
            None,
        ),
    };

    table.insert(
        "theme".to_owned(),
        toml::Value::try_from(resolved).expect("ThemeChoice always serializes to a TOML string"),
    );
    warning
}

/// Load configuration from disk, falling back to defaults on any error.
///
/// A missing `config.toml` is "no config yet" only once the directory that
/// should contain it is confirmed genuinely absent (or `config_file` has no
/// parent to blame at all) — `NotFound` alone cannot carry that distinction
/// on every platform, for the same reason `persistence::delete_workspace_file`
/// cannot trust it either (Task 081/082): a config directory blocked by a
/// plain file can report the same `NotFound` kind reading `config.toml`
/// would report if the directory had simply never been created. A blocked
/// directory instead takes the "cannot read" path below, message and all.
///
/// RFC-070 D1: the text is parsed into a raw `toml::Table` first, so
/// [`resolve_theme_key`] can run its compatibility/validation step on the
/// `theme`/`dark_theme` keys before the normal typed parse — a TOML-syntax
/// error (the table itself cannot be parsed) and a field-shape error (some
/// *other* field, e.g. `dashboard_grouping`, holds a value `AppConfig`
/// cannot deserialize) share the same "config.toml parse error" message,
/// unchanged from before this RFC; only `theme` gets its own, narrower
/// fallback.
pub fn load_config(paths: &AppPaths) -> (AppConfig, Option<String>) {
    let parse_error = |e: &dyn std::fmt::Display| -> (AppConfig, Option<String>) {
        let msg = format!(
            "config.toml parse error (using defaults): {e}\nPath: {}",
            paths.config_file.display()
        );
        (AppConfig::default(), Some(msg))
    };

    match std::fs::read_to_string(&paths.config_file) {
        Ok(text) => match text.parse::<toml::value::Table>() {
            Ok(mut table) => {
                let theme_warning = resolve_theme_key(&mut table, &paths.config_file);
                match table.try_into::<AppConfig>() {
                    Ok(mut cfg) => {
                        // RFC-071 D11: an older file has no `zoom_percent`
                        // at all, which `#[serde(default)]` already loads
                        // as `AppConfig::default()`'s 100 — already one of
                        // `ZOOM_STEPS`, so this snap is a no-op for it.
                        // Only a saved value outside the allowed set (a
                        // hand-edited file) is actually moved.
                        cfg.zoom_percent = nearest_zoom_step(cfg.zoom_percent);
                        // RFC-073 D12: the same snap, for the four settings
                        // that became selects. An older file's own defaults
                        // (60/8/200/2) are already steps, so this moves
                        // only a value typed before D12 turned the field
                        // into a select (0.33 and earlier let anything
                        // through) or one from a hand-edited file.
                        //
                        // Amendment A5 §1: "Off" (0) is its own choice, not
                        // the lower end of the interval scale — snapping it
                        // against the whole table (`nearest_step_u32`'s
                        // plain call) would read a typed `1` as "closer to
                        // Off than to 30s" and silently turn automatic
                        // refresh off, the opposite of what a value that
                        // small asked for. 0 stays 0 exactly; any other
                        // value snaps among the non-zero choices only.
                        cfg.refresh_interval_secs = if cfg.refresh_interval_secs == 0 {
                            0
                        } else {
                            nearest_step_u32(&REFRESH_AUTO_STEPS[1..], cfg.refresh_interval_secs)
                        };
                        cfg.max_concurrent_reads =
                            nearest_step_usize(&SIMULTANEOUS_READS_STEPS, cfg.max_concurrent_reads);
                        cfg.max_log_entries =
                            nearest_step_usize(&LOG_SIZE_STEPS, cfg.max_log_entries);
                        cfg.fs_debounce_secs =
                            nearest_step_u32(&FS_DEBOUNCE_STEPS, cfg.fs_debounce_secs);
                        (cfg, theme_warning)
                    }
                    Err(e) => parse_error(&e),
                }
            }
            Err(e) => parse_error(&e),
        },
        Err(e)
            if e.kind() == std::io::ErrorKind::NotFound
                && paths
                    .config_file
                    .parent()
                    .is_none_or(crate::persistence::treat_missing_directory_as_absent) =>
        {
            (AppConfig::default(), None)
        }
        Err(e) => {
            let msg = format!(
                "Cannot read config file (using defaults): {e}\nPath: {}",
                paths.config_file.display()
            );
            (AppConfig::default(), Some(msg))
        }
    }
}

/// Persist configuration to disk, atomically (Handoff 033 Task A) — this is
/// called on every dashboard Group/Sort change and section collapse, not
/// only an explicit Save, so a bare truncating write is a real exposure
/// rather than a theoretical one.
pub fn save_config(config: &AppConfig, paths: &AppPaths) -> Result<(), String> {
    if let Some(parent) = paths.config_file.parent() {
        std::fs::create_dir_all(parent).map_err(|e| format!("cannot create config dir: {e}"))?;
    }
    let text = toml::to_string_pretty(config).map_err(|e| format!("serialization error: {e}"))?;
    crate::atomic_write::write(&paths.config_file, text)
        .map_err(|e| format!("cannot write config.toml: {e}"))
}

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

    /// The regression this whole task exists to prevent from becoming
    /// silent: on a normal development machine, `dirs::config_dir()` and
    /// `dirs::data_local_dir()` both resolve, so `resolve()` must produce no
    /// warning at all — the CI/dev environment this test runs in is exactly
    /// such a machine.
    #[test]
    fn resolve_produces_no_warning_on_a_normal_machine() {
        let (_paths, warning) = AppPaths::resolve();
        assert_eq!(warning, None);
    }

    #[test]
    fn resolve_from_both_present_produces_no_warning() {
        let (_paths, warning) =
            AppPaths::resolve_from(Some(PathBuf::from("/cfg")), Some(PathBuf::from("/data")));
        assert_eq!(warning, None);
    }

    /// Handoff 034 Item 2: the warning-producing branch — the entire
    /// deliverable of Task B — had no coverage before this test existed.
    #[test]
    fn resolve_from_missing_config_dir_names_the_config_directory() {
        let (paths, warning) = AppPaths::resolve_from(None, Some(PathBuf::from("/data")));
        let warning = warning.expect("a warning must be produced");
        assert!(
            warning.contains("config directory"),
            "warning must name which directory failed: {warning:?}"
        );
        assert!(!warning.contains("data directory"), "{warning:?}");
        assert_eq!(paths.config_file, PathBuf::from("./knotra/config.toml"));
    }

    #[test]
    fn resolve_from_missing_data_dir_names_the_data_directory() {
        let (paths, warning) = AppPaths::resolve_from(Some(PathBuf::from("/cfg")), None);
        let warning = warning.expect("a warning must be produced");
        assert!(
            warning.contains("data directory"),
            "warning must name which directory failed: {warning:?}"
        );
        assert!(!warning.contains("config directory"), "{warning:?}");
        assert_eq!(paths.history_dir, PathBuf::from("./knotra/history"));
    }

    /// Pins the join form this function commits to — a bare `"\n"` — since
    /// nothing else would catch it changing silently.
    #[test]
    fn resolve_from_both_missing_joins_both_messages_with_a_newline() {
        let (_paths, warning) = AppPaths::resolve_from(None, None);
        let warning = warning.expect("a warning must be produced");
        let lines: Vec<&str> = warning.split('\n').collect();
        assert_eq!(
            lines.len(),
            2,
            "both messages must be present, joined by a single newline: {warning:?}"
        );
        assert!(lines[0].contains("config directory"), "{warning:?}");
        assert!(lines[1].contains("data directory"), "{warning:?}");
    }

    // Task 083: no dedicated `load_config` tests existed before this task —
    // only `resolve`/`resolve_from` above, plus indirect round-trip coverage
    // in `tests.rs`. Both cases below are new, not duplicates of anything
    // pre-existing.

    /// The first-run case: the config directory was never created at all.
    /// `load_config` must still read this as "no config yet" — unchanged
    /// behaviour after Task 083's fix, not a new guarantee.
    #[test]
    fn load_config_with_no_config_directory_uses_defaults_and_no_warning() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf()); // config dir never created

        let (loaded, warning) = load_config(&paths);

        assert_eq!(warning, None);
        assert_eq!(
            loaded.refresh_interval_secs,
            AppConfig::default().refresh_interval_secs
        );
        assert_eq!(loaded.theme, AppConfig::default().theme);
    }

    /// Task 104: pinned by name — a brand-new installation (no settings
    /// file at all) must load `Light`, not merely "the current default"
    /// (already covered, genericly, by the assertion just above).
    #[test]
    fn load_config_with_no_settings_file_loads_as_light() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf()); // config dir never created

        let (loaded, warning) = load_config(&paths);

        assert_eq!(warning, None);
        assert_eq!(loaded.theme, ThemeChoice::Light);
    }

    /// Task 083: a config directory blocked by a plain file must not read
    /// as "no config yet" — the same ambiguity Tasks 081/082 removed from
    /// `delete_workspace_file` and the two loaders in `persistence.rs`.
    #[test]
    fn load_config_with_a_blocked_config_directory_reports_and_uses_defaults() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let blocked = tmp.path().join("config");
        std::fs::write(&blocked, "not a directory").expect("blocking file");
        let paths = AppPaths {
            config_file: blocked.join("config.toml"),
            workspaces_dir: blocked.join("workspaces"),
            history_dir: tmp.path().join("data").join("history"),
        };

        let (loaded, warning) = load_config(&paths);

        assert!(warning.is_some(), "a blocked directory must be reported");
        assert_eq!(
            loaded.refresh_interval_secs,
            AppConfig::default().refresh_interval_secs
        );
        assert_eq!(loaded.theme, AppConfig::default().theme);
    }

    /// Task 102: `last_project_parent` is new. A `config.toml` written before
    /// it existed has no such key at all — `#[serde(default)]` must still
    /// load it cleanly, with the field empty, not as a parse error.
    ///
    /// RFC-070 D1: also this RFC's own `dark_theme = true` → `Dark`
    /// compatibility case — the same file this test already wrote before
    /// `theme` existed is exactly an "older file" in D1's sense, so this
    /// test carries both assertions rather than a second file being written
    /// for the same scenario.
    #[test]
    fn load_config_without_last_project_parent_key_loads_it_as_none() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf());
        std::fs::create_dir_all(paths.config_file.parent().unwrap()).expect("config dir");
        std::fs::write(&paths.config_file, "dark_theme = true\n").expect("older config.toml");

        let (loaded, warning) = load_config(&paths);

        assert_eq!(warning, None);
        assert_eq!(loaded.last_project_parent, None);
        assert_eq!(loaded.theme, ThemeChoice::Dark);
    }

    /// RFC-070 D1: the other half of the `dark_theme` compatibility
    /// mapping — `false` maps to `Light`, not merely "not `Dark`" (a
    /// distinction worth its own test: a bug that always produced `Light`
    /// regardless of `dark_theme`'s value would pass a test that only
    /// checked `true`).
    #[test]
    fn load_config_with_only_dark_theme_false_maps_to_light() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf());
        std::fs::create_dir_all(paths.config_file.parent().unwrap()).expect("config dir");
        std::fs::write(&paths.config_file, "dark_theme = false\n").expect("older config.toml");

        let (loaded, warning) = load_config(&paths);

        assert_eq!(warning, None);
        assert_eq!(loaded.theme, ThemeChoice::Light);
    }

    /// RFC-070 D1: `theme` wins when both keys are present — an older
    /// `dark_theme` left behind by an earlier save must not override a
    /// `theme` the user has since explicitly chosen (`save_config` itself
    /// never writes both, but a hand-edited or partially-migrated file
    /// could).
    #[test]
    fn load_config_prefers_theme_over_a_present_dark_theme() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf());
        std::fs::create_dir_all(paths.config_file.parent().unwrap()).expect("config dir");
        std::fs::write(
            &paths.config_file,
            "theme = \"high_contrast_light\"\ndark_theme = true\n",
        )
        .expect("mixed-key config.toml");

        let (loaded, warning) = load_config(&paths);

        assert_eq!(warning, None);
        assert_eq!(loaded.theme, ThemeChoice::HighContrastLight);
    }

    /// RFC-070 D1: an unrecognized `theme` value falls back to the
    /// default, with a warning — through `load_config`'s existing
    /// `(value, Option<String>)` shape, not a hard parse failure that would
    /// also discard every other field in the same file. `refresh_interval_
    /// secs` here is the proof: a real, non-default value the user chose,
    /// which must still load even though `theme` itself did not.
    #[test]
    fn load_config_with_an_unknown_theme_value_falls_back_with_a_warning() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf());
        std::fs::create_dir_all(paths.config_file.parent().unwrap()).expect("config dir");
        std::fs::write(
            &paths.config_file,
            "theme = \"neon\"\nrefresh_interval_secs = 300\n",
        )
        .expect("config.toml with an unknown theme value");

        let (loaded, warning) = load_config(&paths);

        let warning = warning.expect("an unknown theme value must be reported");
        assert!(warning.contains("neon"), "{warning:?}");
        assert_eq!(loaded.theme, AppConfig::default().theme);
        // RFC-073 D12: 300 is already one of `REFRESH_AUTO_STEPS` — this
        // line proves the theme fallback does not disturb an unrelated,
        // already-valid field, not anything about the snap itself (see
        // `load_config_snaps_every_stepped_setting_to_its_nearest_choice`
        // for that).
        assert_eq!(loaded.refresh_interval_secs, 300);
    }

    /// RFC-070 D1: every saved form round-trips — `save_config` then
    /// `load_config` returns the same `ThemeChoice`, for each of the four.
    #[test]
    fn every_theme_choice_round_trips_through_save_and_load() {
        for choice in [
            ThemeChoice::Light,
            ThemeChoice::Dark,
            ThemeChoice::HighContrastLight,
            ThemeChoice::HighContrastDark,
        ] {
            let tmp = tempfile::TempDir::new().expect("tempdir");
            let paths = AppPaths::under(tmp.path().to_path_buf());
            let config = AppConfig {
                theme: choice,
                ..AppConfig::default()
            };

            save_config(&config, &paths).expect("save_config");
            let (loaded, warning) = load_config(&paths);

            assert_eq!(warning, None, "{choice:?}: {warning:?}");
            assert_eq!(loaded.theme, choice);
        }
    }

    /// RFC-070 D1: `save_config` writes `theme` only — an older
    /// `dark_theme` key never reappears once a file has been through one
    /// save, even though the saved form happens to still parse as valid
    /// TOML if it did.
    #[test]
    fn save_config_writes_theme_only_not_dark_theme() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf());
        // Task 104: an explicit theme, not `AppConfig::default()` — this
        // test is about which *key* is written, not which value the
        // default happens to be, and must not break again the next time
        // the default theme changes.
        let config = AppConfig {
            theme: ThemeChoice::Dark,
            ..AppConfig::default()
        };

        save_config(&config, &paths).expect("save_config");
        let text = std::fs::read_to_string(&paths.config_file).expect("read saved config.toml");

        assert!(text.contains("theme = \"dark\""), "{text:?}");
        assert!(!text.contains("dark_theme"), "{text:?}");
    }

    // RFC-071 D11: zoom tests.

    #[test]
    fn zoom_percent_defaults_to_100() {
        assert_eq!(AppConfig::default().zoom_percent, 100);
    }

    /// D11: "an older settings file loads as 100" — a file written before
    /// this field existed has no `zoom_percent` key at all.
    #[test]
    fn load_config_without_zoom_percent_key_loads_as_100() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf());
        std::fs::create_dir_all(paths.config_file.parent().unwrap()).expect("config dir");
        std::fs::write(&paths.config_file, "theme = \"dark\"\n").expect("older config.toml");

        let (loaded, warning) = load_config(&paths);

        assert_eq!(warning, None);
        assert_eq!(loaded.zoom_percent, 100);
    }

    /// D11: "an unknown value loads as the nearest allowed one" — 140 sits
    /// exactly between 130 and 150 (both 10 away); `nearest_zoom_step`'s own
    /// documented tie-break (favour the lower step) resolves it to 130, not
    /// 150 — the case a naive `min_by_key` over `Iterator` would get wrong,
    /// since `Iterator::min_by_key` returns the *last* minimum on a tie.
    #[test]
    fn load_config_with_an_unknown_zoom_percent_snaps_to_the_nearest_step() {
        let cases: &[(u16, u16)] = &[
            (0, 100),
            (107, 100),
            (108, 115),
            (140, 130), // exact tie: 130 (favoured) vs 150
            (999, 150),
        ];
        for &(saved, expected) in cases {
            let tmp = tempfile::TempDir::new().expect("tempdir");
            let paths = AppPaths::under(tmp.path().to_path_buf());
            std::fs::create_dir_all(paths.config_file.parent().unwrap()).expect("config dir");
            std::fs::write(&paths.config_file, format!("zoom_percent = {saved}\n"))
                .expect("config.toml with an out-of-range zoom_percent");

            let (loaded, warning) = load_config(&paths);

            assert_eq!(warning, None, "zoom_percent={saved}: {warning:?}");
            assert_eq!(
                loaded.zoom_percent, expected,
                "zoom_percent={saved} should snap to {expected}"
            );
        }
    }

    /// D11: every allowed step round-trips through save and load unchanged.
    #[test]
    fn every_zoom_step_round_trips_through_save_and_load() {
        for &step in &ZOOM_STEPS {
            let tmp = tempfile::TempDir::new().expect("tempdir");
            let paths = AppPaths::under(tmp.path().to_path_buf());
            let config = AppConfig {
                zoom_percent: step,
                ..AppConfig::default()
            };

            save_config(&config, &paths).expect("save_config");
            let (loaded, warning) = load_config(&paths);

            assert_eq!(warning, None, "{step}: {warning:?}");
            assert_eq!(loaded.zoom_percent, step);
        }
    }

    #[test]
    fn zoom_step_up_steps_through_zoom_steps_and_saturates_at_the_top() {
        assert_eq!(zoom_step_up(100), 115);
        assert_eq!(zoom_step_up(115), 130);
        assert_eq!(zoom_step_up(130), 150);
        assert_eq!(zoom_step_up(150), 150);
    }

    #[test]
    fn zoom_step_down_steps_through_zoom_steps_and_saturates_at_the_bottom() {
        assert_eq!(zoom_step_down(150), 130);
        assert_eq!(zoom_step_down(130), 115);
        assert_eq!(zoom_step_down(115), 100);
        assert_eq!(zoom_step_down(100), 100);
    }

    // RFC-073 D12: the four stepped settings — "a saved value that is not
    // a choice loads as the nearest one, ties going downward, the same as
    // zoom".

    /// The exact-tie case for each of the four, by analogy with zoom's own
    /// 140→130 case: a value exactly between two steps snaps to the lower
    /// one.
    #[test]
    fn load_config_snaps_every_stepped_setting_to_its_nearest_choice() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let paths = AppPaths::under(tmp.path().to_path_buf());
        std::fs::create_dir_all(paths.config_file.parent().unwrap()).expect("config dir");
        std::fs::write(
            &paths.config_file,
            "refresh_interval_secs = 180\nmax_concurrent_reads = 6\nmax_log_entries = 350\nfs_debounce_secs = 4\n",
        )
        .expect("config.toml with off-grid values");

        let (loaded, warning) = load_config(&paths);

        assert_eq!(warning, None, "{warning:?}");
        // 180 is exactly between 60 and 300 (both 120 away): lower wins.
        assert_eq!(loaded.refresh_interval_secs, 60);
        // 6 is exactly between 4 and 8 (both 2 away): lower wins.
        assert_eq!(loaded.max_concurrent_reads, 4);
        // 350 is closer to 200 (150 away) than to 500 (150 away): exact
        // tie, lower wins.
        assert_eq!(loaded.max_log_entries, 200);
        // 4 is closer to 5 (1 away) than to 2 (2 away).
        assert_eq!(loaded.fs_debounce_secs, 5);
    }

    /// Amendment A5 §1: 0 ("Off") stays 0 exactly, and a non-zero value
    /// never snaps down to it — only among the non-zero choices. Before
    /// this fix, `nearest_step_u32` snapped `1` to `0` (`REFRESH_AUTO_
    /// STEPS`'s own first entry), reading a small typed interval as "turn
    /// refresh off", the opposite of the saved intent.
    #[test]
    fn a_non_zero_refresh_interval_never_snaps_to_off() {
        let cases: [(u32, u32); 7] = [
            (0, 0),
            (1, 30),
            (10, 30),
            (15, 30),
            (45, 30), // exactly between 30 and 60: lower wins.
            (46, 60),
            (2000, 900),
        ];
        for (saved, expected) in cases {
            let tmp = tempfile::TempDir::new().expect("tempdir");
            let paths = AppPaths::under(tmp.path().to_path_buf());
            std::fs::create_dir_all(paths.config_file.parent().unwrap()).expect("config dir");
            std::fs::write(
                &paths.config_file,
                format!("refresh_interval_secs = {saved}\n"),
            )
            .expect("write config.toml");

            let (loaded, warning) = load_config(&paths);

            assert_eq!(warning, None, "{saved}: {warning:?}");
            assert_eq!(
                loaded.refresh_interval_secs, expected,
                "saved {saved} must load as {expected}"
            );
        }
    }

    #[test]
    fn every_refresh_auto_step_round_trips_through_save_and_load() {
        for &step in &REFRESH_AUTO_STEPS {
            let tmp = tempfile::TempDir::new().expect("tempdir");
            let paths = AppPaths::under(tmp.path().to_path_buf());
            let config = AppConfig {
                refresh_interval_secs: step,
                ..AppConfig::default()
            };

            save_config(&config, &paths).expect("save_config");
            let (loaded, warning) = load_config(&paths);

            assert_eq!(warning, None, "{step}: {warning:?}");
            assert_eq!(loaded.refresh_interval_secs, step);
        }
    }

    #[test]
    fn every_simultaneous_reads_step_round_trips_through_save_and_load() {
        for &step in &SIMULTANEOUS_READS_STEPS {
            let tmp = tempfile::TempDir::new().expect("tempdir");
            let paths = AppPaths::under(tmp.path().to_path_buf());
            let config = AppConfig {
                max_concurrent_reads: step,
                ..AppConfig::default()
            };

            save_config(&config, &paths).expect("save_config");
            let (loaded, warning) = load_config(&paths);

            assert_eq!(warning, None, "{step}: {warning:?}");
            assert_eq!(loaded.max_concurrent_reads, step);
        }
    }

    #[test]
    fn every_log_size_step_round_trips_through_save_and_load() {
        for &step in &LOG_SIZE_STEPS {
            let tmp = tempfile::TempDir::new().expect("tempdir");
            let paths = AppPaths::under(tmp.path().to_path_buf());
            let config = AppConfig {
                max_log_entries: step,
                ..AppConfig::default()
            };

            save_config(&config, &paths).expect("save_config");
            let (loaded, warning) = load_config(&paths);

            assert_eq!(warning, None, "{step}: {warning:?}");
            assert_eq!(loaded.max_log_entries, step);
        }
    }

    #[test]
    fn every_fs_debounce_step_round_trips_through_save_and_load() {
        for &step in &FS_DEBOUNCE_STEPS {
            let tmp = tempfile::TempDir::new().expect("tempdir");
            let paths = AppPaths::under(tmp.path().to_path_buf());
            let config = AppConfig {
                fs_debounce_secs: step,
                ..AppConfig::default()
            };

            save_config(&config, &paths).expect("save_config");
            let (loaded, warning) = load_config(&paths);

            assert_eq!(warning, None, "{step}: {warning:?}");
            assert_eq!(loaded.fs_debounce_secs, step);
        }
    }
}