dort 0.2.0

Experimental, TUI-first local dotfile manager with guarded recovery
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
use super::{
    actions::{self, Output, Pending, Task},
    app::{App, Tab},
    tests::render,
};
use dort_core::{
    context::HostContext,
    journal,
    local::RepositoryRegistry,
    platform::{Architecture, Platform},
    state::WriterLock,
};
use ratatui::crossterm::event::{
    KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
};
use std::{fs, path::PathBuf};
struct Fixture {
    _temp: tempfile::TempDir,
    host: HostContext,
    repo: PathBuf,
}
impl Fixture {
    fn new(managed: bool) -> Self {
        let temp = tempfile::tempdir().unwrap();
        let base = temp.path().canonicalize().unwrap();
        let home = base.join("home");
        fs::create_dir(&home).unwrap();
        let host =
            HostContext::new(home, None, None, Platform::Linux, Architecture::X86_64).unwrap();
        let repo = base.join("repo");
        fs::create_dir_all(repo.join(".git")).unwrap();
        let mut manifest = "schema=1\n".to_owned();
        fs::create_dir_all(host.home.join(".config/editor")).unwrap();
        for name in ["only-a", "only-b", "only-c"] {
            let path = host.home.join(".config/editor").join(name);
            fs::write(path, "ordinary harmless settings\n").unwrap();
            if managed {
                fs::write(repo.join(name), "ordinary source settings\n").unwrap();
                manifest += &format!(
                    "[[files]]\nid='{name}'\nsource='{name}'\ntarget='~/.config/editor/{name}'\nmode='link'\n"
                );
            }
        }
        fs::write(repo.join("dort.toml"), manifest).unwrap();
        let lock = WriterLock::acquire(&host).unwrap();
        RepositoryRegistry::register(&host, &lock, &repo).unwrap();
        Self {
            _temp: temp,
            host,
            repo,
        }
    }
    fn app(&self, dry: bool) -> App {
        let mut app = App::new(dry);
        app.repository = Some(self.repo.clone());
        app.complete(Ok(Output::Data(
            actions::load(&self.host, Some(&self.repo)).unwrap(),
        )));
        app.tab = Tab::Files;
        app
    }
    fn run(&self, app: &mut App, task: Task) -> Option<Task> {
        app.start(matches!(task, Task::Execute(_)));
        let out = actions::perform(task, &self.host, Some(self.repo.clone()), |_| Ok(()))
            .map_err(|e| actions::diagnostic(&e));
        app.complete(out)
    }
}
const SIZE: (u16, u16) = (48, 14);
fn key(app: &mut App, code: KeyCode) -> Option<Task> {
    app.key(KeyEvent::new(code, KeyModifiers::NONE), SIZE.0, SIZE.1)
}
fn text(app: &mut App, s: &str) {
    super::tests::focus_form(app);
    for c in s.chars() {
        assert!(key(app, KeyCode::Char(c)).is_none());
    }
}
fn clear(app: &mut App) {
    super::tests::focus_form(app);
    assert!(
        app.key(
            KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL),
            SIZE.0,
            SIZE.1
        )
        .is_none()
    );
}
fn select(app: &mut App, name: &str) {
    for _ in 0..20 {
        key(app, KeyCode::Up);
    }
    for _ in 0..30 {
        if app.inventory.group.is_none() {
            let matches = if app.discovering {
                app.discovery
                    .as_ref()
                    .and_then(|d| d.report.candidates.get(app.candidate_selected))
                    .is_some_and(|c| c.entry.path.file_name().is_some_and(|p| p == name))
            } else {
                app.data
                    .as_ref()
                    .and_then(|d| d.files.get(app.selected))
                    .is_some_and(|r| r.0 == name)
            };
            if matches {
                return;
            }
        }
        key(app, KeyCode::Char('l')); // New folders start closed; traverse explicitly.
        key(app, KeyCode::Down);
    }
    panic!("file not keyboard reachable: {name}");
}
fn click(app: &mut App, label: &str, offset: u16) -> Option<Task> {
    if label == "Edit item" {
        return super::tests::more_target(app, |t| matches!(t, super::input::Target::BatchEdit));
    }
    let screen = render(app, SIZE.0, SIZE.1);
    if matches!(label, "Done" | "Review all") {
        let (x, y) = super::tests::target_position(app, SIZE.0, SIZE.1, |t| {
            matches!(t, super::input::Target::BatchControl(_, true))
        });
        return app.mouse(
            MouseEvent {
                kind: MouseEventKind::Down(MouseButton::Left),
                column: x,
                row: y,
                modifiers: KeyModifiers::NONE,
            },
            SIZE.0,
            SIZE.1,
        );
    }
    let (x, y) = screen
        .lines()
        .enumerate()
        .find_map(|(y, row)| {
            row.find(label)
                .map(|x| (row[..x].chars().count() as u16 + offset, y as u16))
        })
        .expect("visible mouse target");
    app.mouse(
        MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: x,
            row: y,
            modifiers: KeyModifiers::NONE,
        },
        SIZE.0,
        SIZE.1,
    )
}
fn approve(app: &mut App) -> Task {
    super::tests::approve_ready(app, SIZE.0, SIZE.1).expect("one explicit combined approval")
}
#[test]
fn separate_item_form_retains_edits_and_only_returns_to_combined_review() {
    let f = Fixture::new(false);
    let mut app = f.app(false);
    let paths = [
        f.host.home.join(".config/editor/only-a"),
        f.host.home.join(".config/editor/only-b"),
    ]
    .into_iter()
    .collect();
    app.batch_editor = Some(super::batch_ui::Editor::new(&paths, &f.host.home));
    key(&mut app, KeyCode::Char('j'));
    assert_eq!(app.batch_editor.as_ref().unwrap().item, 1);
    key(&mut app, KeyCode::Char('e'));
    assert!(app.batch_editor.as_ref().unwrap().item_form);
    for (w, h) in [(48, 14), (80, 24), (140, 40)] {
        let screen = render(&app, w, h);
        assert!(screen.contains("Edit item"));
        assert!(!screen.contains("Review all") && screen.contains("Dort / Files /"));
        assert!(!screen.contains("Adopt all") && !screen.contains("Content ·"));
    }
    super::tests::focus_form(&mut app);
    key(&mut app, KeyCode::Tab); // TARGET -> ID, fields only.
    clear(&mut app);
    text(&mut app, "edited-item"); // e is literal text inside the form.
    key(&mut app, KeyCode::Tab);
    clear(&mut app);
    text(&mut app, "files/edited-item");
    assert!(key(&mut app, KeyCode::Enter).is_none()); // Done from any field: list only.
    let editor = app.batch_editor.as_ref().unwrap();
    assert!(!editor.item_form && editor.open);
    assert_eq!(editor.drafts[1].id, "edited-item");
    assert!(app.preview.is_none());
    key(&mut app, KeyCode::Char('k'));
    key(&mut app, KeyCode::Char('e'));
    super::tests::focus_form(&mut app);
    key(&mut app, KeyCode::Tab);
    clear(&mut app);
    text(&mut app, "retained-on-escape");
    key(&mut app, KeyCode::Esc); // Stop editing.
    key(&mut app, KeyCode::Esc);
    assert_eq!(
        app.batch_editor.as_ref().unwrap().drafts[0].id,
        "retained-on-escape"
    );
    let task = key(&mut app, KeyCode::Enter).expect("default combined review remains quick");
    let Task::Batch(dort_core::batch::Request::Adopt(mappings)) = task else {
        panic!("combined adoption only")
    };
    assert_eq!(mappings.len(), 2);
    assert_eq!(mappings[0].id, "retained-on-escape");
    assert_eq!(mappings[1].id, "edited-item");
    assert!(journal::diagnose(&f.host).unwrap().is_empty());
}

#[test]
fn item_form_mouse_done_never_reviews_an_individual_item() {
    let f = Fixture::new(false);
    let mut app = f.app(false);
    let paths = [
        f.host.home.join(".config/editor/only-a"),
        f.host.home.join(".config/editor/only-b"),
    ]
    .into_iter()
    .collect();
    app.batch_editor = Some(super::batch_ui::Editor::new(&paths, &f.host.home));
    key(&mut app, KeyCode::Down);
    assert!(click(&mut app, "Edit item", 1).is_none());
    assert_eq!(app.batch_editor.as_ref().unwrap().item, 1);
    assert!(app.batch_editor.as_ref().unwrap().item_form);
    render(&app, 140, 40);
    let (x, y) = (0..40)
        .flat_map(|y| (0..140).map(move |x| (x, y)))
        .find(|(x, y)| {
            matches!(
                app.hits.target(*x, *y),
                Some(super::input::Target::BatchControl(4, false))
            )
        })
        .unwrap();
    assert!(
        app.mouse(
            MouseEvent {
                kind: MouseEventKind::Down(MouseButton::Left),
                column: x,
                row: y,
                modifiers: KeyModifiers::NONE
            },
            140,
            40
        )
        .is_none()
    );
    assert_eq!(app.batch_editor.as_ref().unwrap().focused, 4);
    clear(&mut app);
    text(&mut app, "mouse-edited");
    assert!(
        app.key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), 47, 13)
            .is_none()
    );
    assert!(app.batch_editor.as_ref().unwrap().item_form);
    assert!(app.preview.is_none());
    assert!(click(&mut app, "Done", 1).is_none());
    assert_eq!(
        app.batch_editor.as_ref().unwrap().drafts[1].id,
        "mouse-edited"
    );
    assert_eq!(app.batch_editor.as_ref().unwrap().item, 1);
    assert!(!app.batch_editor.as_ref().unwrap().item_form);
    assert!(app.preview.is_none());
    let task = click(&mut app, "Review all", 1).unwrap();
    assert!(
        matches!(task, Task::Batch(dort_core::batch::Request::Adopt(items)) if items.len() == 2)
    );
}

#[test]
fn tree_folds_marks_hidden_items_and_batch_editor_retains_real_fields_at_floor() {
    let f = Fixture::new(false);
    let mut app = f.app(false);
    let task = key(&mut app, KeyCode::Char('F')).unwrap();
    f.run(&mut app, task);
    select(&mut app, "only-a");
    assert!(key(&mut app, KeyCode::Char(' ')).is_none());
    select(&mut app, "only-b");
    key(&mut app, KeyCode::Char(' '));
    assert_eq!(app.inventory.candidate_marks.len(), 2);
    key(&mut app, KeyCode::Char('h'));
    assert!(app.inventory.group.is_some());
    assert!(render(&app, 48, 14).contains("2 marked (2 hidden)"));
    key(&mut app, KeyCode::Char('l'));
    select(&mut app, "only-a");
    key(&mut app, KeyCode::Char('/'));
    text(&mut app, "only-a");
    key(&mut app, KeyCode::Enter);
    assert!(render(&app, 48, 14).contains("2 marked (1 hidden)"));
    assert!(key(&mut app, KeyCode::Char('A')).is_none());
    let screen = render(&app, 48, 14);
    assert!(
        screen.contains("TARGET")
            && screen.contains("SOURCE")
            && super::tests::has_action(&app, "Review all")
    );
    for (w, h) in [(48, 14), (80, 24), (140, 40)] {
        super::files::FORMATTED_ROWS.with(|rows| rows.set(0));
        let covered = render(&app, w, h);
        assert!(covered.contains("All editable defaults"));
        assert!(!covered.contains("Content ·") && !covered.contains("Details ·"));
        assert_eq!(
            super::files::FORMATTED_ROWS.with(|rows| rows.get()),
            0,
            "Discover must not render behind the batch editor"
        );
    }
    // Enter reviews all defaults from the list; no footer traversal is needed.
    assert_eq!(app.batch_editor.as_ref().unwrap().focused, 6);
    key(&mut app, KeyCode::Char('e')); // Local item page.
    super::tests::focus_form(&mut app);
    key(&mut app, KeyCode::Tab); // First ID.
    clear(&mut app);
    text(&mut app, "custom-a");
    key(&mut app, KeyCode::Tab);
    clear(&mut app);
    text(&mut app, "files/custom-a");
    // Done retains one edited draft; no per-file acceptance is required.
    key(&mut app, KeyCode::Enter);
    key(&mut app, KeyCode::Tab); // Next list row.
    assert_eq!(app.batch_editor.as_ref().unwrap().item, 1);
    key(&mut app, KeyCode::Esc);
    key(&mut app, KeyCode::Char('A'));
    assert_eq!(app.batch_editor.as_ref().unwrap().drafts[0].id, "custom-a");
    let task = click(&mut app, "Review all", 0).expect("explicit real combined review button");
    f.run(&mut app, task);
    let Pending::Batch(p) = &app.preview.as_ref().unwrap().pending else {
        panic!("combined plan expected")
    };
    assert_eq!(p.items.len(), 2);
    assert!(!p.has_blockers());
    assert!(p.items.iter().any(|p| p.files[0].id == "custom-a"));
    assert!(journal::diagnose(&f.host).unwrap().is_empty());
    key(&mut app, KeyCode::Down);
    assert_eq!(app.preview.as_ref().unwrap().pending.review_len(), Some(2));
    let task = approve(&mut app);
    let refresh = f.run(&mut app, task).unwrap();
    assert!(matches!(refresh, Task::Refresh));
    let discover = f.run(&mut app, refresh).unwrap();
    assert!(matches!(discover, Task::Discover));
    assert_eq!(app.data.as_ref().unwrap().history.len(), 1);
    assert_eq!(app.data.as_ref().unwrap().files.len(), 2);
    assert!(f.run(&mut app, discover).is_none());
    assert!(app.status.contains("Discover refreshed"));
    assert!(!app.batch_editor.as_ref().unwrap().open);
    assert!(app.discovering && app.tab == Tab::Files && !app.show_result);
    assert!(app.receipt.is_some());
    assert!(app.inventory.candidate_marks.is_empty());
    assert!(f.host.home.join(".config/editor/only-a").is_symlink());
    assert!(f.host.home.join(".config/editor/only-b").is_symlink());
    assert!(!f.host.home.join(".config/editor/only-c").is_symlink());
    assert_eq!(journal::diagnose(&f.host).unwrap().len(), 1);
    assert!(f.repo.join("files/custom-a").is_file());
    assert_eq!(
        f.host
            .home
            .join(".config/editor/only-b")
            .canonicalize()
            .unwrap(),
        f.repo.join("files/.config/editor/only-b")
    );
}
#[test]
fn same_named_files_adopt_into_preserved_subtrees_without_moving_existing_mappings() {
    let f = Fixture::new(true);
    for directory in ["alpha", "beta"] {
        let path = f.host.home.join(".config/editor").join(directory);
        fs::create_dir(&path).unwrap();
        fs::write(path.join("init.lua"), "ordinary nested settings\n").unwrap();
    }
    let mut app = f.app(false);
    let task = key(&mut app, KeyCode::Char('F')).unwrap();
    f.run(&mut app, task);
    key(&mut app, KeyCode::Char('/'));
    text(&mut app, "init.lua");
    key(&mut app, KeyCode::Enter);
    key(&mut app, KeyCode::Char(' ')); // Closed .config group marks both matches.
    assert_eq!(app.inventory.candidate_marks.len(), 2);
    key(&mut app, KeyCode::Char('A'));
    let task = key(&mut app, KeyCode::Enter).unwrap();
    f.run(&mut app, task);
    assert!(!app.preview.as_ref().unwrap().pending.blocked());
    let task = approve(&mut app);
    let refresh = f
        .run(&mut app, task)
        .unwrap_or_else(|| panic!("nested adoption failed: {:?}", app.error));
    let discover = f
        .run(&mut app, refresh)
        .unwrap_or_else(|| panic!("nested refresh failed: {:?}", app.error));
    f.run(&mut app, discover);
    for directory in ["alpha", "beta"] {
        let relative = PathBuf::from(".config/editor")
            .join(directory)
            .join("init.lua");
        assert_eq!(
            f.host.home.join(&relative).canonicalize().unwrap(),
            f.repo.join("files").join(&relative)
        );
        assert_eq!(
            fs::read_to_string(f.repo.join("files").join(&relative)).unwrap(),
            "ordinary nested settings\n"
        );
    }
    let manifest =
        dort_core::Manifest::parse(&fs::read_to_string(f.repo.join("dort.toml")).unwrap()).unwrap();
    assert_eq!(manifest.files.len(), 5);
    assert!(
        manifest
            .files
            .iter()
            .any(|m| m.id == "only-a" && m.source.as_path() == std::path::Path::new("only-a"))
    );
    assert!(f.repo.join("only-a").is_file());
}

#[test]
fn selected_apply_keep_delete_use_one_review_bound_to_every_selected_id() {
    for operation in ['U', 'D'] {
        let f = Fixture::new(true);
        let mut app = f.app(false);
        select(&mut app, "only-a");
        key(&mut app, KeyCode::Char(' '));
        select(&mut app, "only-b");
        key(&mut app, KeyCode::Char(' '));
        key(&mut app, KeyCode::Char('/'));
        text(&mut app, "only-a");
        key(&mut app, KeyCode::Enter);
        let task = key(&mut app, KeyCode::Char('A')).unwrap();
        f.run(&mut app, task);
        assert_eq!(app.preview.as_ref().unwrap().pending.review_len(), Some(2));
        let task = approve(&mut app);
        let refresh = f.run(&mut app, task).unwrap();
        f.run(&mut app, refresh);
        assert!(!f.host.home.join(".config/editor/only-c").is_symlink());
        key(&mut app, KeyCode::Esc);
        let task = key(&mut app, KeyCode::Char(operation)).unwrap();
        f.run(&mut app, task);
        if operation == 'D' {
            let screen = render(&app, 140, 40);
            assert!(screen.contains("only-a") && screen.contains("only-b"));
            key(&mut app, KeyCode::F(1));
            assert!(key(&mut app, KeyCode::Enter).is_none());
            assert!(!render(&app, 140, 40).contains("only-a"));
            app.resize(47, 14);
            app.resize(48, 14);
            key(&mut app, KeyCode::Esc);
            assert!(app.preview.is_some());
        }
        let task = approve(&mut app);
        let refresh = f.run(&mut app, task).unwrap();
        f.run(&mut app, refresh);
        assert!(app.inventory.managed_marks.is_empty());
        for id in ["only-a", "only-b"] {
            let path = f.host.home.join(".config/editor").join(id);
            if operation == 'U' {
                assert!(!path.is_symlink());
                assert!(path.is_file());
            } else {
                assert!(!path.exists());
            }
            assert!(f.repo.join(id).is_file());
        }
        let history = journal::diagnose(&f.host).unwrap();
        assert_eq!(history.len(), 2);
        assert!(history.iter().all(|d| d.journal_schema == Some(4)));
        let manifest =
            dort_core::Manifest::parse(&fs::read_to_string(f.repo.join("dort.toml")).unwrap())
                .unwrap();
        assert_eq!(manifest.files.len(), 1);
        assert_eq!(manifest.files[0].id, "only-c");
        // Schema-4 rollback is offered by the real Recovery action guard.
        super::tests::destination(&mut app, super::app::Tab::Recovery, 80, 24);
        let index = app
            .data
            .as_ref()
            .unwrap()
            .history
            .iter()
            .position(|d| d.transaction_id == history.last().unwrap().transaction_id)
            .unwrap();
        for _ in 0..index {
            key(&mut app, KeyCode::Down);
        }
        assert!(app.page_actions().contains(&super::shell::Action::Rollback));
    }
}
#[test]
fn sequential_keep_removes_completed_marks_and_preserves_other_hidden_marks() {
    for first in ['u', 'U'] {
        let f = Fixture::new(true);
        let mut app = f.app(false);
        let task = key(&mut app, KeyCode::Char('a')).unwrap();
        f.run(&mut app, task);
        let task = approve(&mut app);
        let refresh = f.run(&mut app, task).unwrap();
        f.run(&mut app, refresh);
        key(&mut app, KeyCode::Esc);
        select(&mut app, "only-a");
        key(&mut app, KeyCode::Char(' '));
        if first == 'u' {
            select(&mut app, "only-c");
            key(&mut app, KeyCode::Char(' '));
            select(&mut app, "only-a");
        }
        key(&mut app, KeyCode::Char('/'));
        text(&mut app, "only-a");
        key(&mut app, KeyCode::Enter);
        let task = key(&mut app, KeyCode::Char(first)).unwrap();
        f.run(&mut app, task);
        assert!(app.error.is_none());
        let task = approve(&mut app);
        let refresh = f.run(&mut app, task).unwrap();
        f.run(&mut app, refresh);
        assert!(!app.inventory.managed_marks.contains("only-a"));
        assert_eq!(app.inventory.managed_marks.len(), usize::from(first == 'u'));
        if first == 'u' {
            assert!(app.inventory.managed_marks.contains("only-c"));
        }
        key(&mut app, KeyCode::Esc);
        key(&mut app, KeyCode::Delete);
        select(&mut app, "only-b");
        key(&mut app, KeyCode::Char(' '));
        let task = key(&mut app, KeyCode::Char('U')).unwrap();
        assert!(
            matches!(&task, Task::Batch(dort_core::batch::Request::Keep(ids)) if ids.contains(&"only-b".to_owned()) && !ids.contains(&"only-a".to_owned()) && ids.len() == if first == 'u' { 2 } else { 1 })
        );
        f.run(&mut app, task);
        assert!(app.error.is_none());
        let task = approve(&mut app);
        let refresh = f.run(&mut app, task).unwrap();
        f.run(&mut app, refresh);
        assert!(app.error.is_none() && app.inventory.managed_marks.is_empty());
        for id in ["only-a", "only-b"] {
            let target = f.host.home.join(".config/editor").join(id);
            assert!(target.is_file() && !target.is_symlink());
            assert_eq!(
                fs::read_to_string(target).unwrap(),
                "ordinary source settings\n"
            );
            assert!(f.repo.join(id).is_file());
        }
    }
}
#[test]
fn unavailable_inventory_preserves_marks_until_a_healthy_full_refresh() {
    let f = Fixture::new(true);
    let mut app = f.app(false);
    app.inventory.managed_marks = ["only-a".to_owned(), "only-b".to_owned()].into();
    app.inventory.managed.query = "only-a".into();
    let manifest = fs::read_to_string(f.repo.join("dort.toml")).unwrap();
    f.run(&mut app, Task::Refresh);
    assert_eq!(app.inventory.managed_marks.len(), 2);
    fs::write(f.repo.join("dort.toml"), "schema=\"invalid\"\n").unwrap();
    f.run(&mut app, Task::Refresh);
    assert!(app.data.as_ref().unwrap().files_error.is_some());
    assert_eq!(app.inventory.managed_marks.len(), 2);
    fs::write(f.repo.join("dort.toml"), manifest).unwrap();
    f.run(&mut app, Task::Refresh);
    assert_eq!(app.inventory.managed_marks.len(), 2);
    fs::write(f.repo.join("dort.toml"), "schema=1\n").unwrap();
    f.run(&mut app, Task::Refresh);
    assert!(app.data.as_ref().unwrap().files_error.is_none());
    assert!(app.inventory.managed_marks.is_empty());
}
#[test]
fn mouse_tree_controls_use_rendered_geometry_and_never_open_previews_or_approve() {
    let f = Fixture::new(false);
    let mut app = f.app(false);
    let task = key(&mut app, KeyCode::Char('F')).unwrap();
    f.run(&mut app, task);
    select(&mut app, "only-a");
    assert!(click(&mut app, "[ ]", 1).is_none());
    assert_eq!(app.inventory.candidate_marks.len(), 1);
    key(&mut app, KeyCode::Char('h'));
    assert!(render(&app, 48, 14).contains("[>]"));
    assert!(click(&mut app, "[>]", 1).is_none());
    select(&mut app, "only-b");
    assert!(click(&mut app, "[ ] only-b", 1).is_none());
    assert_eq!(app.inventory.candidate_marks.len(), 2);
    assert!(app.preview.is_none());
    assert!(journal::diagnose(&f.host).unwrap().is_empty());
    // Group toggling explicitly marks matching regular descendants, including folded rows.
    key(&mut app, KeyCode::Char('h'));
    key(&mut app, KeyCode::Char(' '));
    assert_eq!(app.inventory.candidate_marks.len(), 3);
    key(&mut app, KeyCode::Char(' '));
    assert!(app.inventory.candidate_marks.is_empty());
}
#[test]
fn blocked_item_never_silently_skips_and_dry_run_cannot_approve_batch() {
    let f = Fixture::new(true);
    let mut app = f.app(false);
    select(&mut app, "only-a");
    key(&mut app, KeyCode::Char(' '));
    select(&mut app, "only-b");
    key(&mut app, KeyCode::Char(' '));
    fs::remove_file(f.host.home.join(".config/editor/only-b")).unwrap();
    std::os::unix::fs::symlink("/foreign", f.host.home.join(".config/editor/only-b")).unwrap();
    let task = key(&mut app, KeyCode::Char('A')).unwrap();
    f.run(&mut app, task);
    assert!(app.preview.as_ref().unwrap().pending.blocked());
    assert_eq!(app.preview.as_ref().unwrap().pending.review_len(), Some(2));
    for size in [(48, 14), (140, 24)] {
        assert!(!render(&app, size.0, size.1).contains("y Confirm"));
    }
    assert!(key(&mut app, KeyCode::Enter).is_none());
    assert!(key(&mut app, KeyCode::Char('y')).is_none());
    assert!(journal::diagnose(&f.host).unwrap().is_empty());
    let mut dry = f.app(true);
    select(&mut dry, "only-a");
    key(&mut dry, KeyCode::Char(' '));
    let task = key(&mut dry, KeyCode::Char('A')).unwrap();
    f.run(&mut dry, task);
    assert!(!dry.preview.as_ref().unwrap().pending.blocked());
    for size in [(48, 14), (140, 24)] {
        assert!(!render(&dry, size.0, size.1).contains("y Confirm"));
    }
    key(&mut dry, KeyCode::Enter);
    text(&mut dry, "yes");
    assert!(key(&mut dry, KeyCode::Enter).is_none());
    assert!(journal::diagnose(&f.host).unwrap().is_empty());
}