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
|
#![cfg(unix)]
use insta::assert_snapshot;
use zellij_integration_tests::{
keys, normalized, GridSnapshot, NestedHarness, PROMPT, TERMINAL_SIZE,
};
const ARROW_DOWN: &[u8] = b"\x1b[B";
fn last_line_contains(grid_snapshot: &GridSnapshot, needle: &str) -> bool {
grid_snapshot
.lines()
.last()
.map_or(false, |last_line| last_line.contains(needle))
}
fn modal_visible(grid_snapshot: &GridSnapshot) -> bool {
grid_snapshot.contains("Nested Zellij session detected")
}
fn grid_contains_line_starting_with(grid_snapshot: &GridSnapshot, prefix: &str) -> bool {
grid_snapshot
.lines()
.iter()
.any(|line| line.trim_start().starts_with(prefix))
}
fn wait_for_modal(nested: &NestedHarness) -> GridSnapshot {
nested.wait_for_guest_to_announce();
nested.wait_for_host_to_acknowledge_guest();
nested
.host
.wait_until("host shows the guest-detected modal", modal_visible)
}
fn focus_guest_binding_config() -> &'static str {
r#"keybinds {
normal {
bind "Ctrl y" { FocusGuestSession; }
}
}"#
}
fn modal_shortcut_binding_config() -> &'static str {
r#"keybinds {
normal {
bind "Ctrl y" { ToggleHostFullscreen; }
bind "Ctrl u" { FocusHostSession; }
bind "Ctrl i" { FocusGuestSession; }
}
}"#
}
#[test]
fn modal_renders_expected_ui() {
let mut nested =
NestedHarness::start_with_host_config(TERMINAL_SIZE, modal_shortcut_binding_config());
let host_with_modal = wait_for_modal(&nested);
assert!(host_with_modal.contains("What would you like to do?"));
assert!(host_with_modal.contains("Zoom in and control this session"));
assert!(host_with_modal.contains("Control this session on focus"));
assert!(host_with_modal.contains("<Ctrl y>"));
assert!(host_with_modal.contains("<Ctrl u>"));
assert!(host_with_modal.contains("<↓↑> select"));
let steady_modal = nested.host.wait_until(
"guest-detected modal fully rendered with all options and keybindings",
|host_grid| {
modal_visible(host_grid)
&& host_grid.contains("What would you like to do?")
&& host_grid.contains("Control this session on focus")
&& host_grid.contains("<Ctrl y>")
&& host_grid.contains("<Ctrl u>")
&& host_grid.contains("<↓↑> select")
},
);
assert_snapshot!(normalized(&steady_modal));
nested.guest.wait_for_app_load();
nested.guest.quit();
nested.host.quit();
}
#[test]
fn modal_selection_moves_with_arrow_keys() {
let mut nested =
NestedHarness::start_with_host_config(TERMINAL_SIZE, modal_shortcut_binding_config());
wait_for_modal(&nested);
let first_selection = nested
.host
.wait_until("first option selected on initial render", |host_grid| {
grid_contains_line_starting_with(host_grid, "> 1.")
});
assert!(grid_contains_line_starting_with(&first_selection, "> 1."));
assert!(!grid_contains_line_starting_with(&first_selection, "> 2."));
nested.host.send_stdin(ARROW_DOWN);
let second_selection = nested.host.wait_until(
"selection moved to the second option after arrow down",
|host_grid| grid_contains_line_starting_with(host_grid, "> 2."),
);
assert!(grid_contains_line_starting_with(&second_selection, "> 2."));
assert!(!grid_contains_line_starting_with(&second_selection, "> 1."));
nested.guest.wait_for_app_load();
nested.guest.quit();
nested.host.quit();
}
#[test]
fn modal_appears_on_announce_and_occludes_guest_content() {
let mut nested = NestedHarness::start(TERMINAL_SIZE);
let host_with_modal = wait_for_modal(&nested);
assert!(modal_visible(&host_with_modal));
assert!(host_with_modal.contains("Zoom in and control this session"));
assert!(host_with_modal.contains("Control this session on focus"));
assert_eq!(
nested.focus_gained_count(),
0,
"the modal must not enter passthrough by itself"
);
nested.guest.wait_for_app_load();
nested.guest.quit();
nested.host.quit();
}
#[test]
fn selecting_descend_enters_passthrough() {
let mut nested = NestedHarness::start(TERMINAL_SIZE);
wait_for_modal(&nested);
let descended = nested.mark_host_to_guest();
nested.host.send_stdin(ARROW_DOWN);
nested.host.send_stdin(b"\r");
nested.wait_for_host_to_descend_into_guest_after(descended);
nested
.host
.wait_until("modal dismissed after descend", |host_grid| {
!modal_visible(host_grid)
});
nested.guest.wait_for_app_load();
nested.host.send_stdin(&keys::alt('n'));
let guest_new_pane = nested.guest.expect_pty_spawn();
guest_new_pane.output(PROMPT);
nested.guest.wait_until(
"guest spawned a pane from the passed-through key after descend",
|guest_grid| guest_grid.contains("Pane #2"),
);
nested.guest.quit();
nested.wait_for_host_to_reclaim_focus_after_guest_exit();
nested.host.quit();
}
#[test]
fn digit_one_zooms_and_enters_passthrough() {
let mut nested = NestedHarness::start(TERMINAL_SIZE);
wait_for_modal(&nested);
let zoomed = nested.mark_host_to_guest();
nested.host.send_stdin(b"1");
nested.wait_for_host_to_descend_into_guest_after(zoomed);
nested
.host
.wait_until("modal dismissed after zoom", |host_grid| {
!modal_visible(host_grid)
});
nested.guest.wait_for_app_load();
nested.host.send_stdin(&keys::alt('n'));
let guest_new_pane = nested.guest.expect_pty_spawn();
guest_new_pane.output(PROMPT);
nested.guest.wait_until(
"guest spawned a pane from the passed-through key after zoom",
|guest_grid| guest_grid.contains("Pane #2"),
);
nested.guest.quit();
nested.wait_for_host_to_reclaim_focus_after_guest_exit();
nested.host.quit();
}
#[test]
fn focus_guest_session_redescends_after_ascending() {
let mut nested =
NestedHarness::start_with_host_config(TERMINAL_SIZE, focus_guest_binding_config());
wait_for_modal(&nested);
let descended = nested.mark_host_to_guest();
nested.host.send_stdin(b"2");
nested.wait_for_host_to_descend_into_guest_after(descended);
nested
.host
.wait_until("modal answered", |host_grid| !modal_visible(host_grid));
nested.guest.wait_for_app_load();
let ascended = nested.mark_host_to_guest();
nested.host.send_stdin(&keys::ctrl('o'));
nested.guest.wait_until(
"guest entered session mode before the ascend key",
|guest_grid| last_line_contains(guest_grid, "SESSION"),
);
nested.host.send_stdin(b"]");
nested.wait_for_host_to_ascend_from_guest_after(ascended);
let redescended = nested.mark_host_to_guest();
nested.host.send_stdin(&keys::ctrl('y'));
nested.wait_for_host_to_descend_into_guest_after(redescended);
nested.host.send_stdin(&keys::alt('n'));
let guest_new_pane = nested.guest.expect_pty_spawn();
guest_new_pane.output(PROMPT);
nested.guest.wait_until(
"guest spawned a pane from the passed-through key after FocusGuestSession re-descended",
|guest_grid| guest_grid.contains("Pane #2"),
);
nested.guest.quit();
nested.wait_for_host_to_reclaim_focus_after_guest_exit();
nested.host.quit();
}
#[test]
fn two_clients_answer_their_own_modals_independently() {
let mut nested = NestedHarness::start(TERMINAL_SIZE);
let first_with_modal = wait_for_modal(&nested);
assert!(modal_visible(&first_with_modal));
let second = nested.host.attach_client(TERMINAL_SIZE);
let second_with_modal =
second.wait_until("the second client also sees the guest modal", modal_visible);
assert!(modal_visible(&second_with_modal));
let descended = nested.mark_host_to_guest();
nested.host.send_stdin(b"2");
nested.wait_for_host_to_descend_into_guest_after(descended);
nested.host.wait_until(
"the first client's modal is dismissed after it descends",
|host_grid| !modal_visible(host_grid),
);
second.wait_until(
"the second client's modal stays up while the first client descended",
modal_visible,
);
nested.guest.wait_for_app_load();
second.quit();
nested.guest.quit();
nested.wait_for_host_to_reclaim_focus_after_guest_exit();
nested.host.quit();
}
#[test]
fn host_stops_pinging_a_frozen_guest_after_answering_the_modal() {
let mut nested = NestedHarness::start(TERMINAL_SIZE);
wait_for_modal(&nested);
nested.guest.wait_for_app_load();
let descended = nested.mark_host_to_guest();
nested.host.send_stdin(b"2");
nested.wait_for_host_to_descend_into_guest_after(descended);
nested
.host
.wait_until("modal answered", |host_grid| !modal_visible(host_grid));
nested.freeze_guest();
nested.assert_host_stops_pinging_frozen_guest();
nested.guest.quit();
nested.host.quit();
}
|