summaryrefslogtreecommitdiffstats
path: root/src/interactive/app/tests/journeys_readonly.rs
blob: b6dc57926a45c5ffed3653cc473c19c3f8e3bc33 (plain)
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
use anyhow::Result;
use crosstermion::crossterm::event::KeyCode;
use pretty_assertions::assert_eq;
use std::ffi::OsString;

use crate::interactive::app::tests::utils::into_codes;
use crate::interactive::{
    app::tests::{
        utils::{
            fixture_str, index_by_name, initialized_app_and_terminal_from_fixture, into_keys,
            node_by_index, node_by_name,
        },
        FIXTURE_PATH,
    },
    SortMode,
};

#[test]
fn init_from_pdu_results() -> Result<()> {
    use crate::interactive::app::tests::utils::new_test_terminal;
    let _terminal = new_test_terminal()?;

    Ok(())
}

#[test]
fn simple_user_journey_read_only() -> Result<()> {
    let long_root = "sample-02/dir";
    let short_root = "sample-01";
    let (mut terminal, mut app) =
        initialized_app_and_terminal_from_fixture(&[short_root, long_root])?;

    // POST-INIT
    // after initialization, we expect that...
    {
        assert_eq!(
            app.state.sorting,
            SortMode::SizeDescending,
            "it will sort items in descending order by size"
        );

        assert!(
            app.state.active_traversal.is_none(),
            "it will not think it is still scanning as there is no traversal"
        );

        let first_selected_path = OsString::from(format!("{}/{}", FIXTURE_PATH, long_root));
        assert_eq!(
            node_by_name(&app, &first_selected_path).name,
            first_selected_path,
            "the roots are always listed with the given (possibly long) names",
        );

        assert_eq!(
            node_by_name(&app, fixture_str(short_root)),
            node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
            "it selects the first node in the list",
        );

        assert_eq!(
            app.traversal.root_index,
            app.state.navigation().view_root,
            "the root is the 'virtual' root",
        );
    }

    // SORTING
    {
        // when hitting the M key
        app.process_events(&mut terminal, into_codes("m"))?;
        assert_eq!(
            app.state.sorting,
            SortMode::MTimeDescending,
            "it sets the sort mode to descending by mtime"
        );
        // when hitting the M key again
        app.process_events(&mut terminal, into_codes("m"))?;
        assert_eq!(
            app.state.sorting,
            SortMode::MTimeAscending,
            "it sets the sort mode to ascending by mtime"
        );
        // when hitting the C key
        app.process_events(&mut terminal, into_codes("c"))?;
        assert_eq!(
            app.state.sorting,
            SortMode::CountDescending,
            "it sets the sort mode to descending by count"
        );
        // when hitting the C key again
        app.process_events(&mut terminal, into_codes("c"))?;
        assert_eq!(
            app.state.sorting,
            SortMode::CountAscending,
            "it sets the sort mode to ascending by count"
        );
        assert_eq!(
            node_by_index(&app, app.state.entries[0].index),
            node_by_name(&app, fixture_str(long_root)),
            "it recomputes the cached items"
        );
        // when hitting the S key
        app.process_events(&mut terminal, into_codes("s"))?;
        assert_eq!(
            app.state.sorting,
            SortMode::SizeDescending,
            "it sets the sort mode to descending by size"
        );
        assert_eq!(
            node_by_index(&app, app.state.entries[1].index),
            node_by_name(&app, fixture_str(long_root)),
            "it recomputes the cached items"
        );
        // when hitting the S key again
        app.process_events(&mut terminal, into_codes("s"))?;
        assert_eq!(
            app.state.sorting,
            SortMode::SizeAscending,
            "it sets the sort mode to ascending by size"
        );
        // hit the S key again to get Descending - the rest depends on it
        app.process_events(&mut terminal, into_codes("s"))?;
        assert_eq!(app.state.sorting, SortMode::SizeDescending,);

        assert_eq!(
            node_by_index(&app, app.state.entries[0].index),
            node_by_name(&app, fixture_str(short_root)),
            "it recomputes the cached items"
        );
    }

    // Entry-Navigation
    {
        // when hitting the j key
        app.process_events(&mut terminal, into_codes("j"))?;
        assert_eq!(
            node_by_name(&app, fixture_str(long_root)),
            node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
            "it moves the cursor down and selects the next item based on the current sort mode"
        );
        // when hitting it while there is nowhere to go
        app.process_events(&mut terminal, into_codes("j"))?;
        assert_eq!(
            node_by_name(&app, fixture_str(long_root)),
            node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
            "it stays at the previous position"
        );
        // when hitting the k key
        app.process_events(&mut terminal, into_codes("k"))?;
        assert_eq!(
            node_by_name(&app, fixture_str(short_root)),
            node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
            "it moves the cursor up and selects the next item based on the current sort mode"
        );
        // when hitting the k key again
        app.process_events(&mut terminal, into_codes("k"))?;
        assert_eq!(
            node_by_name(&app, fixture_str(short_root)),
            node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
            "it stays at the current cursor position as there is nowhere to go"
        );
        // when hitting the o key with a directory selected
        app.process_events(&mut terminal, into_codes("o"))?;
        {
            let new_root_idx = index_by_name(&app, fixture_str(short_root));
            assert_eq!(
                new_root_idx,
                app.state.navigation().view_root,
                "it enters the item if it is a directory, changing the root"
            );
            assert_eq!(
                index_by_name(&app, "dir"),
                *app.state.navigation().selected.as_ref().unwrap(),
                "it selects the first item in the directory"
            );

            // when hitting the u key while inside a sub-directory
            app.process_events(&mut terminal, into_codes("u"))?;
            {
                assert_eq!(
                    app.traversal.root_index,
                    app.state.navigation().view_root,
                    "it sets the root to be the (roots) parent directory, being the virtual root"
                );
                assert_eq!(
                    node_by_name(&app, fixture_str(short_root)),
                    node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
                    "changes the selection to the first item in the list of items"
                );
            }
        }
        // when hitting the u key while inside of the root directory
        // We are moving the cursor down just to have a non-default selection
        app.process_events(&mut terminal, into_codes("ju"))?;
        {
            assert_eq!(
                app.traversal.root_index,
                app.state.navigation().view_root,
                "it keeps the root - it can't go further up"
            );
            assert_eq!(
                node_by_name(&app, fixture_str(long_root)),
                node_by_index(&app, *app.state.navigation().selected.as_ref().unwrap()),
                "keeps the previous selection"
            );
        }
    }

    // Deletion
    {
        // when hitting the 'd' key (also move cursor back to start)
        app.process_events(&mut terminal, into_codes("k"))?;
        let previously_selected_index = *app.state.navigation().selected.as_ref().unwrap();
        app.process_events(&mut terminal, into_codes("d"))?;
        {
            assert_eq!(
                Some(1),
                app.window.mark_pane.as_ref().map(|p| p.marked().len()),
                "it marks only a single node",
            );
            assert!(
                app.window.mark_pane.as_ref().map_or(false, |p| p
                    .marked()
                    .contains_key(&previously_selected_index)),
                "it marks the selected node"
            );
            assert_eq!(
                app.state.navigation().selected.as_ref().unwrap().index(),
                app.state.entries[1].index.index(),
                "moves the cursor down one level to facilitate many markings in a row"
            );
        }

        // when hitting the 'd' key again
        {
            app.process_events(&mut terminal, into_codes("d"))?;

            assert_eq!(
                Some(2),
                app.window.mark_pane.as_ref().map(|p| p.marked().len()),
                "it marks the currently selected, second node",
            );

            assert_eq!(
                app.state.navigation().selected.as_ref().unwrap().index(),
                app.state.entries[1].index.index(),
                "it could not advance the cursor, thus the newly marked item is still selected"
            );
        }

        // when hitting the 'd' key once again
        {
            app.process_events(&mut terminal, into_codes("d"))?;

            assert_eq!(
                Some(1),
                app.window.mark_pane.as_ref().map(|p| p.marked().len()),
                "it toggled the previous selected item off",
            );

            assert!(
                app.window.mark_pane.as_ref().map_or(false, |p| p
                    .marked()
                    .contains_key(