aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-integration-tests/tests/no_focus.rs
blob: 5722a019b2bed958a960223d27ea34f619c8f718 (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
#![cfg(unix)]

use std::time::Instant;
use zellij_integration_tests::{
    claim_first_terminal_and_wait_for_prompt, default_timeout, start_zellij, FakePtyHandle, PROMPT,
};
use zellij_utils::cli::CliAction;

fn no_focus_new_pane_action(command: &[&str]) -> CliAction {
    CliAction::NewPane {
        direction: None,
        command: command.iter().map(|part| part.to_string()).collect(),
        plugin: None,
        cwd: None,
        floating: false,
        in_place: false,
        close_replaced_pane: false,
        pane_id: None,
        name: None,
        close_on_exit: false,
        start_suspended: false,
        configuration: None,
        skip_plugin_cache: false,
        x: None,
        y: None,
        width: None,
        height: None,
        pinned: None,
        stacked: false,
        blocking: false,
        block_until_exit_success: false,
        block_until_exit_failure: false,
        block_until_exit: false,
        unblock_condition: None,
        near_current_pane: false,
        no_focus: true,
        borderless: None,
        tab_id: None,
    }
}

fn no_focus_new_tab_action() -> CliAction {
    CliAction::NewTab {
        name: None,
        layout: None,
        layout_string: None,
        layout_dir: None,
        cwd: None,
        initial_command: vec![],
        initial_plugin: None,
        close_on_exit: false,
        start_suspended: false,
        block_until_exit_success: false,
        block_until_exit_failure: false,
        block_until_exit: false,
        no_focus: true,
    }
}

fn wait_until_stdin_contains(handle: &FakePtyHandle, needle: &[u8]) {
    let deadline = Instant::now() + default_timeout();
    loop {
        let bytes = handle.stdin_bytes();
        if bytes.windows(needle.len()).any(|window| window == needle) {
            return;
        }
        if Instant::now() >= deadline {
            panic!(
                "timed out waiting for pane to receive {:?}, received {:?}",
                String::from_utf8_lossy(needle),
                String::from_utf8_lossy(&bytes),
            );
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
}

fn stdin_contains(handle: &FakePtyHandle, needle: &[u8]) -> bool {
    handle
        .stdin_bytes()
        .windows(needle.len())
        .any(|window| window == needle)
}

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

    zellij.run_cli_action(no_focus_new_pane_action(&["unfocused-command"]));
    let unfocused_pane = zellij.expect_pty_spawn();
    unfocused_pane.output(PROMPT);
    zellij.wait_until(
        "second pane appeared alongside the first",
        |grid_snapshot| grid_snapshot.text.matches("$").count() >= 2,
    );

    let probe = b"nofocusprobe";
    zellij.send_stdin(probe);
    wait_until_stdin_contains(&original_pane, probe);
    assert!(
        !stdin_contains(&unfocused_pane, probe),
        "input leaked into the pane that was opened with --no-focus"
    );
    zellij.quit();
}

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

    let mut focusing_action = no_focus_new_pane_action(&["focused-command"]);
    if let CliAction::NewPane { no_focus, .. } = &mut focusing_action {
        *no_focus = false;
    }
    zellij.run_cli_action(focusing_action);
    let focused_pane = zellij.expect_pty_spawn();
    focused_pane.output(PROMPT);
    zellij.wait_until(
        "second pane appeared alongside the first",
        |grid_snapshot| grid_snapshot.text.matches("$").count() >= 2,
    );

    let probe = b"focusprobe";
    zellij.send_stdin(probe);
    wait_until_stdin_contains(&focused_pane, probe);
    assert!(
        !stdin_contains(&original_pane, probe),
        "input reached the original pane even though the new pane should have taken focus"
    );
    zellij.quit();
}

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

    zellij.run_cli_action(no_focus_new_tab_action());
    let new_tab_pane = zellij.expect_pty_spawn();
    new_tab_pane.output(PROMPT);
    zellij.wait_until("a second tab exists in the tab bar", |grid_snapshot| {
        grid_snapshot.text.contains("Tab #2")
    });

    let probe = b"nofocusprobe";
    zellij.send_stdin(probe);
    wait_until_stdin_contains(&original_pane, probe);
    assert!(
        !stdin_contains(&new_tab_pane, probe),
        "input leaked into the tab that was created with --no-focus"
    );
    zellij.quit();
}