aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-integration-tests/tests/tabs.rs
blob: 7ba3194a7ddf215b0d7a1339ae458f43722dd69d (plain) (blame)
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
#![cfg(unix)]

use insta::assert_snapshot;
use zellij_integration_tests::{
    claim_first_terminal_and_wait_for_prompt, col, keys, normalized, start_zellij, FakePtyHandle,
    GridSnapshot, TestSession, PROMPT,
};

fn tabs_in_order(grid_snapshot: &GridSnapshot, labels: &[&str]) -> bool {
    let mut search_from = 0;
    for label in labels {
        match grid_snapshot.text[search_from..].find(label) {
            Some(offset) => search_from += offset + label.len(),
            None => return false,
        }
    }
    true
}

fn open_new_tab_and_wait_for_prompt(zellij: &TestSession, expected_tab: &str) -> FakePtyHandle {
    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('n'));
    let terminal = zellij.expect_pty_spawn();
    terminal.output(PROMPT);
    let expected_tab = expected_tab.to_owned();
    zellij.wait_until("new tab opened with prompt", move |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains(&expected_tab)
            && grid_snapshot.cursor_is_at(col(3).row(2))
    });
    terminal
}

fn label_first_tab_pane(zellij: &TestSession, terminal: &FakePtyHandle, marker: &str) {
    terminal.output(marker.as_bytes());
    let marker = marker.to_owned();
    zellij.wait_until("first tab pane labelled", move |grid_snapshot| {
        grid_snapshot.contains(&marker)
    });
}

fn open_marked_tab(zellij: &TestSession, expected_tab: &str, marker: &str) -> FakePtyHandle {
    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('n'));
    let terminal = zellij.expect_pty_spawn();
    terminal.output(PROMPT);
    terminal.output(marker.as_bytes());
    let expected_tab = expected_tab.to_owned();
    let marker = marker.to_owned();
    zellij.wait_until("new marked tab opened", move |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains(&expected_tab)
            && grid_snapshot.contains(&marker)
    });
    terminal
}

#[test]
fn go_to_previous_tab() {
    let mut zellij = start_zellij();
    let first_terminal = claim_first_terminal_and_wait_for_prompt(&zellij);
    label_first_tab_pane(&zellij, &first_terminal, "oneone");
    open_marked_tab(&zellij, "Tab #2", "twotwo");

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('h'));
    zellij.send_stdin(&keys::ENTER);

    let grid_snapshot = zellij.wait_until("previous tab focused", |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains("Tab #2")
            && grid_snapshot.contains("oneone")
            && !grid_snapshot.contains("twotwo")
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn go_to_next_tab() {
    let mut zellij = start_zellij();
    let first_terminal = claim_first_terminal_and_wait_for_prompt(&zellij);
    label_first_tab_pane(&zellij, &first_terminal, "oneone");
    open_marked_tab(&zellij, "Tab #2", "twotwo");

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('h'));
    zellij.wait_until("previous tab focused", |grid_snapshot| {
        grid_snapshot.contains("oneone") && !grid_snapshot.contains("twotwo")
    });

    zellij.send_stdin(&keys::key('l'));
    zellij.send_stdin(&keys::ENTER);

    let grid_snapshot = zellij.wait_until("next tab focused again", |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains("Tab #2")
            && grid_snapshot.contains("twotwo")
            && !grid_snapshot.contains("oneone")
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn go_to_tab_by_number() {
    let mut zellij = start_zellij();
    let first_terminal = claim_first_terminal_and_wait_for_prompt(&zellij);
    label_first_tab_pane(&zellij, &first_terminal, "oneone");
    open_marked_tab(&zellij, "Tab #2", "twotwo");

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('1'));

    let grid_snapshot = zellij.wait_until("first tab focused by number", |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains("oneone")
            && !grid_snapshot.contains("twotwo")
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn toggle_tab_returns_to_last_used() {
    let mut zellij = start_zellij();
    let first_terminal = claim_first_terminal_and_wait_for_prompt(&zellij);
    label_first_tab_pane(&zellij, &first_terminal, "oneone");
    open_marked_tab(&zellij, "Tab #2", "twotwo");

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::TAB);
    zellij.send_stdin(&keys::ENTER);

    let grid_snapshot = zellij.wait_until("toggled back to the first tab", |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains("Tab #2")
            && grid_snapshot.contains("oneone")
            && !grid_snapshot.contains("twotwo")
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn toggle_active_sync_tab() {
    let mut zellij = start_zellij();
    let first_terminal = claim_first_terminal_and_wait_for_prompt(&zellij);

    zellij.send_stdin(&keys::ctrl('p'));
    zellij.send_stdin(&keys::key('n'));
    let second_terminal = zellij.expect_pty_spawn();
    second_terminal.output(PROMPT);
    zellij.wait_until("second pane spawned in first tab", |grid_snapshot| {
        grid_snapshot.status_bar_appears() && grid_snapshot.contains("Pane #2")
    });

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('s'));

    let grid_snapshot = zellij.wait_until("tab marked as syncing", |grid_snapshot| {
        grid_snapshot.status_bar_appears() && grid_snapshot.contains("SYNC")
    });
    assert_snapshot!(normalized(&grid_snapshot));

    zellij.send_stdin(&keys::ENTER);
    zellij.send_stdin(b"synced-input");

    let first_stdin = first_terminal.wait_for_stdin("synced input reached first pane", |stdin| {
        stdin.windows(12).any(|window| window == b"synced-input")
    });
    let second_stdin = second_terminal
        .wait_for_stdin("synced input reached second pane", |stdin| {
            stdin.windows(12).any(|window| window == b"synced-input")
        });
    assert!(first_stdin
        .windows(12)
        .any(|window| window == b"synced-input"));
    assert!(second_stdin
        .windows(12)
        .any(|window| window == b"synced-input"));
    zellij.quit();
}

#[test]
fn break_pane_into_new_tab() {
    let mut zellij = start_zellij();
    claim_first_terminal_and_wait_for_prompt(&zellij);

    zellij.send_stdin(&keys::ctrl('p'));
    zellij.send_stdin(&keys::key('n'));
    let second_terminal = zellij.expect_pty_spawn();
    second_terminal.output(PROMPT);
    zellij.wait_until("second pane spawned in first tab", |grid_snapshot| {
        grid_snapshot.status_bar_appears() && grid_snapshot.contains("Pane #2")
    });

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('b'));

    let grid_snapshot = zellij.wait_until("focused pane broken into a new tab", |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains("Tab #2")
            && grid_snapshot.contains("Pane #2")
            && !grid_snapshot.contains("Pane #1")
            && grid_snapshot.cursor_is_at(col(3).row(2))
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn break_pane_to_the_right() {
    let mut zellij = start_zellij();
    let first_terminal = claim_first_terminal_and_wait_for_prompt(&zellij);
    label_first_tab_pane(&zellij, &first_terminal, "alpha");

    zellij.send_stdin(&keys::ctrl('p'));
    zellij.send_stdin(&keys::key('n'));
    let beta_terminal = zellij.expect_pty_spawn();
    beta_terminal.output(PROMPT);
    beta_terminal.output(b"beta");
    zellij.wait_until("second pane spawned in first tab", |grid_snapshot| {
        grid_snapshot.contains("beta") && grid_snapshot.contains("Pane #2")
    });

    open_marked_tab(&zellij, "Tab #2", "gamma");

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('h'));
    zellij.wait_until("back on the first tab with its panes", |grid_snapshot| {
        grid_snapshot.contains("beta") && !grid_snapshot.contains("gamma")
    });

    zellij.send_stdin(&keys::key(']'));

    let grid_snapshot = zellij.wait_until("beta pane moved into the right tab", |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains("beta")
            && grid_snapshot.contains("gamma")
            && !grid_snapshot.contains("alpha")
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn break_pane_to_the_left() {
    let mut zellij = start_zellij();
    let first_terminal = claim_first_terminal_and_wait_for_prompt(&zellij);
    label_first_tab_pane(&zellij, &first_terminal, "alpha");

    open_marked_tab(&zellij, "Tab #2", "beta");

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('['));

    let grid_snapshot = zellij.wait_until("beta pane moved into the left tab", |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains("alpha")
            && grid_snapshot.contains("beta")
            && !grid_snapshot.contains("Tab #2")
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn open_new_tab() {
    let mut zellij = start_zellij();
    claim_first_terminal_and_wait_for_prompt(&zellij);
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #2");
    let grid_snapshot = zellij.wait_until("second tab steady in normal mode", |grid_snapshot| {
        grid_snapshot.status_bar_appears()
            && grid_snapshot.contains("Tab #2")
            && grid_snapshot.cursor_is_at(col(3).row(2))
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn close_tab() {
    let mut zellij = start_zellij();
    claim_first_terminal_and_wait_for_prompt(&zellij);
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #2");

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('x'));

    let grid_snapshot = zellij.wait_until(
        "second tab closed, only first tab remains",
        |grid_snapshot| {
            grid_snapshot.status_bar_appears()
                && grid_snapshot.contains("Tab #1")
                && !grid_snapshot.contains("Tab #2")
                && grid_snapshot.cursor_is_at(col(3).row(2))
        },
    );
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn undo_rename_tab() {
    let mut zellij = start_zellij();
    claim_first_terminal_and_wait_for_prompt(&zellij);

    zellij.send_stdin(&keys::ctrl('t'));
    zellij.send_stdin(&keys::key('r'));
    zellij.send_stdin(b"aa");
    zellij.send_stdin(&keys::ESC);
    zellij.send_stdin(&keys::ESC);

    let grid_snapshot = zellij.wait_until("tab name reverted to default", |grid_snapshot| {
        grid_snapshot.contains("Tab #1") && grid_snapshot.contains("LOCK")
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn move_tab_to_left() {
    let mut zellij = start_zellij();
    claim_first_terminal_and_wait_for_prompt(&zellij);
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #2");
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #3");

    zellij.send_stdin(&keys::alt('i'));

    let grid_snapshot = zellij.wait_until("third tab moved one position left", |grid_snapshot| {
        tabs_in_order(grid_snapshot, &["Tab #1", "Tab #3", "Tab #2"])
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn move_tab_to_right() {
    let mut zellij = start_zellij();
    claim_first_terminal_and_wait_for_prompt(&zellij);
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #2");
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #3");

    zellij.send_stdin(&keys::alt('h'));
    zellij.send_stdin(&keys::alt('o'));

    let grid_snapshot = zellij.wait_until("second tab moved one position right", |grid_snapshot| {
        tabs_in_order(grid_snapshot, &["Tab #1", "Tab #3", "Tab #2"])
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn move_tab_to_left_until_it_wraps_around() {
    let mut zellij = start_zellij();
    claim_first_terminal_and_wait_for_prompt(&zellij);
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #2");
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #3");

    zellij.send_stdin(&keys::alt('i'));
    zellij.wait_until("third tab moved one position left", |grid_snapshot| {
        tabs_in_order(grid_snapshot, &["Tab #1", "Tab #3", "Tab #2"])
    });
    zellij.send_stdin(&keys::alt('i'));
    zellij.wait_until("third tab moved to the beginning", |grid_snapshot| {
        tabs_in_order(grid_snapshot, &["Tab #3", "Tab #1", "Tab #2"])
    });
    zellij.send_stdin(&keys::alt('i'));

    let grid_snapshot = zellij.wait_until("third tab wrapped to the end", |grid_snapshot| {
        tabs_in_order(grid_snapshot, &["Tab #1", "Tab #2", "Tab #3"])
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}

#[test]
fn move_tab_to_right_until_it_wraps_around() {
    let mut zellij = start_zellij();
    claim_first_terminal_and_wait_for_prompt(&zellij);
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #2");
    open_new_tab_and_wait_for_prompt(&zellij, "Tab #3");

    zellij.send_stdin(&keys::alt('o'));

    let grid_snapshot = zellij.wait_until("third tab wrapped to the beginning", |grid_snapshot| {
        tabs_in_order(grid_snapshot, &["Tab #3", "Tab #1", "Tab #2"])
    });
    assert_snapshot!(normalized(&grid_snapshot));
    zellij.quit();
}