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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
|
use super::*;
use std::sync::{Arc, Mutex};
use crate::client::endpoint::{
ClientEndpointId, ClientEndpointStatus, EndpointNegotiation, EndpointRegistry,
EndpointTransport, ProfileId, SavedSshEndpoint,
};
#[derive(Clone)]
struct CapturingEndpointTransport(Arc<Mutex<Vec<crate::protocol::ClientMessage>>>);
impl EndpointTransport for CapturingEndpointTransport {
fn send(&mut self, message: &crate::protocol::ClientMessage) -> std::io::Result<()> {
self.0.lock().unwrap().push(message.clone());
Ok(())
}
}
fn lifecycle_negotiation() -> EndpointNegotiation {
EndpointNegotiation::new(
vec!["client_shell.surface.set".into()],
vec![
crate::protocol::endpoint::SURFACE_INTEREST_CAPABILITY.into(),
crate::protocol::endpoint::PRESENTATION_EFFECTS_FENCE_CAPABILITY.into(),
],
)
}
fn lifecycle_resize() -> crate::protocol::ClientMessage {
crate::protocol::ClientMessage::ClientShellResize {
cell_width_px: 8,
cell_height_px: 16,
surface_size: crate::protocol::ClientSurfaceSize { cols: 80, rows: 24 },
pixel_mouse: false,
}
}
fn request_active_surface(server: &mut HeadlessServer, client_id: u64, request_id: &str) {
let boot_id = server.client_shell_boot_id.clone();
assert!(
server.handle_server_event(ServerEvent::ClientShellEndpointRequest {
client_id,
boot_id,
request: Box::new(api::schema::Request {
id: request_id.into(),
method: api::schema::Method::ClientShellSurfaceSet(
api::schema::ClientShellSurfaceSetParams { active: true },
),
}),
})
);
}
#[tokio::test]
async fn metadata_only_shell_is_isolated_until_surface_activation() {
let mut server = test_headless_server();
let mut input_rx = install_focused_test_runtime(&mut server, b"");
let pane_id = server.app.session_snapshot().focused_pane_id.unwrap();
let workspace_id = server.app.session_snapshot().focused_workspace_id.unwrap();
let original_size = server.effective_size;
let (writer, control_rx, render_rx) = test_client_writer();
let client_id = 52;
assert!(
server.handle_server_event(ServerEvent::ClientShellConnected {
surface_reuse: false,
surface_delta: false,
client_id,
surface_cols: 101,
surface_rows: 37,
cell_width_px: 9,
cell_height_px: 18,
pixel_mouse: true,
direct_graphics: false,
endpoint_keybindings: true,
mouse_capture: true,
surface_active: false,
writer,
})
);
let _ = client_shell_snapshot(&control_rx);
assert_eq!(server.foreground_client_id, None);
assert_eq!(server.effective_size, original_size);
server.render_and_stream();
assert!(render_rx.try_recv().is_err());
assert!(server.clients[&client_id]
.render_state
.last_pane_surface()
.is_none());
assert!(
!server.handle_server_event(ServerEvent::ClientShellPaneInput {
client_id,
pane_id,
events: vec![crate::protocol::ClientPaneInputEvent::Paste(
"blocked".into()
)],
})
);
assert!(input_rx.try_recv().is_err());
let boot_id = server.client_shell_boot_id.clone();
assert!(
!server.handle_server_event(ServerEvent::ClientShellEndpointRequest {
client_id,
boot_id: boot_id.clone(),
request: Box::new(api::schema::Request {
id: "inactive-mutation".into(),
method: api::schema::Method::WorkspaceFocus(api::schema::WorkspaceTarget {
workspace_id,
}),
}),
})
);
let ServerMessage::ClientShellEndpointResponseChunk { data, .. } =
read_server_message(control_rx.recv().expect("inactive mutation response"))
else {
panic!("expected endpoint response");
};
let error = serde_json::from_slice::<api::schema::ErrorResponse>(&data).unwrap();
assert_eq!(error.error.code, "surface_inactive");
assert!(
server.send_to_client_shells(ServerMessage::SemanticNotification(
crate::protocol::SemanticNotification {
kind: crate::protocol::SemanticNotificationKind::Custom,
title: "metadata event".into(),
body: None,
sound: None,
agent: None,
workspace_id: None,
tab_id: None,
pane_id: None,
position: None,
},
))
);
assert!(matches!(
read_server_message(control_rx.recv().expect("metadata notification")),
ServerMessage::SemanticNotification(_)
));
assert!(
server.handle_server_event(ServerEvent::ClientShellEndpointRequest {
client_id,
boot_id: boot_id.clone(),
request: Box::new(api::schema::Request {
id: "activate-surface".into(),
method: api::schema::Method::ClientShellSurfaceSet(
api::schema::ClientShellSurfaceSetParams { active: true },
),
}),
})
);
let ServerMessage::ClientShellEndpointResponseChunk { data, .. } =
read_server_message(control_rx.recv().expect("surface activation response"))
else {
panic!("expected typed surface activation response");
};
let activation_ack = serde_json::from_slice::<api::schema::SuccessResponse>(&data).unwrap();
let api::schema::ResponseResult::ClientShellSurfaceSet {
active: true,
projection_revision: activation_floor,
} = activation_ack.result
else {
panic!("expected typed surface activation result");
};
assert_eq!(server.foreground_client_id, Some(client_id));
assert_eq!(server.effective_size, (101, 37));
server.render_and_stream();
let ServerMessage::PaneSurface(surface) =
read_server_message(render_rx.recv().expect("activated surface"))
else {
panic!("expected pane surface");
};
assert_eq!((surface.frame.width, surface.frame.height), (101, 37));
assert!(surface.projection_revision >= activation_floor);
assert_eq!(surface.surface_revision, 1);
server
.clients
.get_mut(&client_id)
.expect("surface client")
.shell_endpoint_command_in_flight = true;
assert!(
server.handle_server_event(ServerEvent::ClientShellEndpointRequest {
client_id,
boot_id: boot_id.clone(),
request: Box::new(api::schema::Request {
id: "deactivate-surface".into(),
method: api::schema::Method::ClientShellSurfaceSet(
api::schema::ClientShellSurfaceSetParams { active: false },
),
}),
})
);
let _ = control_rx.recv().expect("surface deactivation response");
assert!(server.clients.contains_key(&client_id));
let (_, runtime_pane_id) = server.app.parse_pane_id(&surface.panes[0].pane_id).unwrap();
server
.app
.state
.runtime_for_pane_in_workspace(&server.app.terminal_runtimes, 0, runtime_pane_id)
.unwrap()
.test_process_pty_bytes(b"REACTIVATED");
assert!(
server.render_retained_pane_surface_and_stream(&std::collections::HashSet::from([
runtime_pane_id
]))
);
assert!(render_rx.try_recv().is_err());
assert!(
server.handle_server_event(ServerEvent::ClientShellEndpointRequest {
client_id,
boot_id,
request: Box::new(api::schema::Request {
id: "reactivate-surface".into(),
method: api::schema::Method::ClientShellSurfaceSet(
api::schema::ClientShellSurfaceSetParams { active: true },
),
}),
})
);
let data = loop {
let message =
read_server_message(control_rx.recv().expect("surface reactivation response"));
match message {
ServerMessage::ClientShellEndpointResponseChunk {
request_id, data, ..
} if request_id == "reactivate-surface" => break data,
ServerMessage::EndpointControl { .. }
| ServerMessage::ClientShellEndpointResponseChunk { .. } => continue,
other => panic!("unexpected surface reactivation message: {other:?}"),
}
};
let reactivation_ack = serde_json::from_slice::<api::schema::SuccessResponse>(&data).unwrap();
let api::schema::ResponseResult::ClientShellSurfaceSet {
active: true,
projection_revision: reactivation_floor,
} = reactivation_ack.result
else {
panic!("expected typed surface reactivation result");
};
assert!(reactivation_floor > activation_floor);
server.render_and_stream();
let ServerMessage::PaneSurface(surface) =
read_server_message(render_rx.recv().expect("reactivated surface"))
else {
panic!("expected replacement pane surface");
};
assert!(frame_text(&surface.frame).contains("REACTIVATED"));
assert!(surface.projection_revision >= reactivation_floor);
assert_eq!(surface.surface_revision, 2);
shutdown_test_runtimes(&mut server);
}
#[tokio::test]
async fn background_surface_activation_preserves_focused_viewer_geometry() {
let mut server = test_headless_server();
let pane_id = install_shared_view_test_runtime(&mut server);
let (focused_control, _) = connect_test_shell(&mut server, 7, 68, 17);
let _ = focused_control.recv().expect("focused client snapshot");
assert!(server.handle_server_event(ServerEvent::ClientShellFocus {
client_id: 7,
focused: true,
}));
let focused_size = server.app.state.workspaces[0].test_runtimes[&pane_id].current_size();
assert_eq!(focused_size, (17, 67));
let shared_tab_id = server.shell_tab_id_for_client(7).expect("focused tab");
assert_eq!(
server.tab_geometry_controllers.get(&shared_tab_id),
Some(&7)
);
let (writer, background_control, _) = test_client_writer();
assert!(
server.handle_server_event(ServerEvent::ClientShellConnected {
surface_reuse: false,
surface_delta: false,
client_id: 8,
surface_cols: 100,
surface_rows: 35,
cell_width_px: 0,
cell_height_px: 0,
pixel_mouse: false,
direct_graphics: false,
endpoint_keybindings: false,
mouse_capture: false,
surface_active: false,
writer,
})
);
let _ = background_control
.recv()
.expect("background client snapshot");
request_active_surface(&mut server, 8, "activate-background-surface");
let _ = background_control
.recv()
.expect("background surface activation response");
assert_eq!(
server.shell_tab_id_for_client(8).as_deref(),
Some(shared_tab_id.as_str())
);
assert_eq!(server.clients[&7].outer_terminal_focus, Some(true));
assert_eq!(server.clients[&8].outer_terminal_focus, None);
assert_eq!(
server.app.state.workspaces[0].test_runtimes[&pane_id].current_size(),
focused_size,
"surface activation must not transiently resize a focused viewer's tab"
);
assert_eq!(
server.tab_geometry_controllers.get(&shared_tab_id),
Some(&7)
);
assert!(server.handle_server_event(ServerEvent::ClientShellFocus {
client_id: 8,
focused: false,
}));
assert_eq!(server.clients[&7].outer_terminal_focus, Some(true));
assert_eq!(server.clients[&8].outer_terminal_focus, Some(false));
assert_eq!(
server.app.state.workspaces[0].test_runtimes[&pane_id].current_size(),
focused_size
);
assert_eq!(
server.tab_geometry_controllers.get(&shared_tab_id),
Some(&7)
);
request_active_surface(&mut server, 8, "synchronize-background-surface");
let _ = background_control
.recv()
.expect("background presentation synchronization response");
assert_eq!(
server.app.state.workspaces[0].test_runtimes[&pane_id].current_size(),
focused_size
);
assert_eq!(
server.tab_geometry_controllers.get(&shared_tab_id),
Some(&7)
);
shutdown_test_runtimes(&mut server);
}
#[tokio::test]
async fn focused_surface_reassertion_reclaims_tab_geometry() {
let mut server = test_headless_server();
let pane_id = install_shared_view_test_runtime(&mut server);
let (focused_control, _) = connect_test_shell(&mut server, 8, 100, 35);
let _ = focused_control.recv().expect("focused client snapshot");
assert!(server.handle_server_event(ServerEvent::ClientShellFocus {
client_id: 8,
focused: true,
}));
let shared_tab_id = server.shell_tab_id_for_client(8).expect("focused tab");
let (other_control, _) = connect_test_shell(&mut server, 7, 68, 17);
let _ = other_control.recv().expect("other client snapshot");
assert!(server.claim_shell_tab_geometry(7, false));
assert_eq!(
server.app.state.workspaces[0].test_runtimes[&pane_id].current_size(),
(17, 67)
);
request_active_surface(&mut server, 8, "reassert-focused-surface");
let _ = focused_control
.recv()
.expect("focused surface reassertion response");
assert_eq!(server.clients[&8].outer_terminal_focus, Some(true));
assert_eq!(
server.app.state.workspaces[0].test_runtimes[&pane_id].current_size(),
(35, 99)
);
assert_eq!(
server.tab_geometry_controllers.get(&shared_tab_id),
Some(&8)
);
shutdown_test_runtimes(&mut server);
}
#[tokio::test]
async fn presentation_sync_epoch_replays_modes_and_title() {
let mut server = test_headless_server();
let (writer, control_rx, _render_rx) = test_client_writer();
let client_id = 63;
assert!(
server.handle_server_event(ServerEvent::ClientShellConnected {
surface_reuse: false,
surface_delta: false,
client_id,
surface_cols: 80,
surface_rows: 24,
cell_width_px: 8,
cell_height_px: 16,
pixel_mouse: false,
direct_graphics: false,
endpoint_keybindings: true,
mouse_capture: true,
surface_active: true,
writer,
})
);
let _ = client_shell_snapshot(&control_rx);
server.api_window_title = Some("target title".into());
{
let client = server.clients.get_mut(&client_id).unwrap();
client.host_mouse_capture_active = Some(false);
client.host_sgr_pixels_active = Some(false);
client.host_keyboard_report_all_active = Some(false);
}
let boot_id = server.client_shell_boot_id.clone();
assert!(
server.handle_server_event(ServerEvent::ClientShellEndpointRequest {
client_id,
boot_id,
request: Box::new(api::schema::Request {
id: "post-commit-reassert".into(),
method: api::schema::Method::ClientShellSurfaceSet(
api::schema::ClientShellSurfaceSetParams { active: true },
),
}),
})
);
let _ = control_rx
.recv()
.expect("typed surface reassertion acknowledgement");
server.stream_host_mouse_capture_mode();
server.stream_direct_terminal_keyboard_mode();
server.sync_window_title();
assert_eq!(
server.clients[&client_id].host_mouse_capture_active,
Some(true),
"the target mode is sent after, not during, the frozen handoff"
);
assert_eq!(
server.clients[&client_id].host_keyboard_report_all_active,
Some(false)
);
let messages = (0..3)
.map(|_| read_server_message(control_rx.recv().expect("reassertion effect")))
.collect::<Vec<_>>();
assert!(messages
.iter()
.any(|message| matches!(message, ServerMessage::MouseCapture { enabled: true, .. })));
assert!(messages.iter().any(|message| matches!(
message,
ServerMessage::ClientShellKeyboardReportAll { enabled: false }
)));
assert!(messages.iter().any(|message| matches!(
message,
ServerMessage::WindowTitle { title: Some(title) } if title == "target title"
)));
shutdown_test_runtimes(&mut server);
}
fn dispatch_lifecycle_messages(
server: &mut HeadlessServer,
client_id: u64,
messages: Vec<crate::protocol::ClientMessage>,
) {
for message in messages {
let event = match message {
crate::protocol::ClientMessage::ClientShellResize {
cell_width_px,
cell_height_px,
surface_size,
pixel_mouse,
} => ServerEvent::ClientShellResize {
client_id,
cell_width_px,
cell_height_px,
surface_cols: surface_size.cols,
surface_rows: surface_size.rows,
pixel_mouse,
},
crate::protocol::ClientMessage::ClientShellFocus { focused } => {
ServerEvent::ClientShellFocus { client_id, focused }
}
crate::protocol::ClientMessage::ClientShellEndpointRequest { boot_id, request } => {
ServerEvent::ClientShellEndpointRequest {
client_id,
boot_id,
request: Box::new(serde_json::from_str(&request).unwrap()),
}
}
other => panic!("unhandled lifecycle message: {other:?}"),
};
server.handle_server_event(event);
}
}
/// A real two-server/client lifecycle harness. Both source-off and target-on traverse the
/// production HeadlessServer endpoint request path; the client test only routes its emitted wire
/// messages and never authors an acknowledgement, snapshot, or surface response.
#[tokio::test]
async fn two_headless_servers_drive_atomic_endpoint_handoff() {
let mut source_server = test_headless_server();
let _source_input = install_focused_test_runtime(&mut source_server, b"local source");
let (source_writer, source_control, _source_render) = test_client_writer();
let source_client_id = 78;
assert!(
source_server.handle_server_event(ServerEvent::ClientShellConnected {
surface_reuse: false,
surface_delta: false,
client_id: source_client_id,
surface_cols: 80,
surface_rows: 24,
cell_width_px: 8,
cell_height_px: 16,
pixel_mouse: false,
direct_graphics: false,
endpoint_keybindings: true,
mouse_capture: true,
surface_active: true,
writer: source_writer,
})
);
let source_snapshot = client_shell_snapshot(&source_control);
let mut target_server = test_headless_server();
let _target_input = install_focused_test_runtime(&mut target_server, b"remote target");
let (target_writer, target_control, target_render) = test_client_writer();
let target_client_id = 79;
assert!(
target_server.handle_server_event(ServerEvent::ClientShellConnected {
surface_reuse: false,
surface_delta: false,
client_id: target_client_id,
surface_cols: 80,
surface_rows: 24,
cell_width_px: 8,
cell_height_px: 16,
pixel_mouse: false,
direct_graphics: false,
endpoint_keybindings: true,
mouse_capture: true,
surface_active: false,
writer: target_writer,
})
);
let remote_snapshot = client_shell_snapshot(&target_control);
let profile = SavedSshEndpoint {
id: ProfileId::parse("0123456789abcdef0123456789abcdef").unwrap(),
label: "Remote".into(),
target: "dev@example.com".into(),
session: "main".into(),
enabled: true,
};
let target_id = ClientEndpointId::Ssh(profile.id.clone());
let mut shell = crate::client::ClientShellState::new(
crate::client::ClientShellConfig::from_config(&crate::config::Config::default()),
);
shell.set_endpoint_catalog(&[profile]);
shell.set_snapshot(source_snapshot);
shell.set_endpoint_status(&target_id, ClientEndpointStatus::Online);
shell.set_endpoint_snapshot(&target_id, remote_snapshot);
let source_sent = Arc::new(Mutex::new(Vec::new()));
let target_sent = Arc::new(Mutex::new(Vec::new()));
let mut endpoints = EndpointRegistry::new(
CapturingEndpointTransport(source_sent.clone()),
1,
lifecycle_negotiation(),
);
endpoints.insert(
target_id.clone(),
CapturingEndpointTransport(target_sent.clone()),
7,
lifecycle_negotiation(),
false,
);
let mut activation = crate::client::endpoint::PendingEndpointActivation::begin(
&shell,
&mut endpoints,
target_id.clone(),
None,
lifecycle_resize(),
41,
std::time::Instant::now(),
)
.unwrap();
// Route the source-off-first client messages through a second real HeadlessServer. Its
// typed response is the only source acknowledgement supplied to the activation state.
let mut source_release_request_id = None;
for message in std::mem::take(&mut *source_sent.lock().unwrap()) {
match message {
crate::protocol::ClientMessage::ClientShellFocus { focused } => {
assert!(
source_server.handle_server_event(ServerEvent::ClientShellFocus {
client_id: source_client_id,
focused,
})
);
}
crate::protocol::ClientMessage::ClientShellEndpointRequest { boot_id, request } => {
let request = serde_json::from_str::<api::schema::Request>(&request).unwrap();
if matches!(
request.method,
api::schema::Method::ClientShellSurfaceSet(
api::schema::ClientShellSurfaceSetParams { active: false }
)
) {
source_release_request_id = Some(request.id.clone());
}
assert!(source_server.handle_server_event(
ServerEvent::ClientShellEndpointRequest {
client_id: source_client_id,
boot_id,
request: Box::new(request),
}
));
}
other => panic!("unexpected source lifecycle message: {other:?}"),
}
}
let source_release_request_id = source_release_request_id.expect("client source-off request");
let source_release_data = loop {
let message = read_server_message(source_control.recv().expect("source typed release ack"));
match message {
ServerMessage::ClientShellEndpointResponseChunk {
request_id, data, ..
} if request_id == source_release_request_id => break data,
ServerMessage::EndpointControl { .. }
| ServerMessage::MouseCapture { .. }
| ServerMessage::ClientShellKeyboardReportAll { .. }
| ServerMessage::WindowTitle { .. }
| ServerMessage::ClientShellEndpointResponseChunk { .. } => continue,
other => panic!("unexpected source release message: {other:?}"),
}
};
assert_eq!(
activation.receive_response(
&ClientEndpointId::Local,
1,
&source_release_request_id,
&source_release_data,
&mut endpoints,
),
crate::client::endpoint::SurfaceActivationProgress::Pending
);
dispatch_lifecycle_messages(
&mut target_server,
target_client_id,
std::mem::take(&mut *target_sent.lock().unwrap()),
);
assert_eq!(
target_server.clients[&target_client_id].outer_terminal_focus,
Some(true)
);
assert_eq!(
source_server.clients[&source_client_id].outer_terminal_focus,
Some(false)
);
let ServerMessage::ClientShellEndpointResponseChunk {
request_id, data, ..
} = read_server_message(target_control.recv().expect("target typed activation ack"))
else {
panic!("expected target activation acknowledgement");
};
assert_eq!(
activation.receive_response(&target_id, 7, &request_id, &data, &mut endpoints),
crate::client::endpoint::SurfaceActivationProgress::Pending
);
target_server.render_and_stream();
let coherent_snapshot = client_shell_snapshot(&target_control);
let snapshot_progress = activation.receive_snapshot(&target_id, 7, &coherent_snapshot);
shell.set_endpoint_snapshot(&target_id, coherent_snapshot);
assert_eq!(
snapshot_progress,
crate::client::endpoint::SurfaceActivationProgress::Pending
);
let ServerMessage::PaneSurface(coherent_surface) =
read_server_message(target_render.recv().expect("target replacement surface"))
else {
panic!("expected target pane surface");
};
assert_eq!(
activation.receive_surface(&target_id, 7, coherent_surface),
crate::client::endpoint::SurfaceActivationProgress::Ready
);
assert!(matches!(
activation.complete(&mut shell, &mut endpoints),
Ok(crate::client::endpoint::ActivationCompletion::AwaitingPresentationSync {
endpoint,
..
}) if endpoint == target_id
));
assert!(
!endpoints.active_surface_available(),
"target input remains fenced while presentation effects resynchronize"
);
let sync_request = target_sent
.lock()
.unwrap()
.iter()
.find_map(|message| match message {
crate::protocol::ClientMessage::ClientShellEndpointRequest { boot_id, request } => {
let request = serde_json::from_str::<api::schema::Request>(request).ok()?;
request
.id
.ends_with(":presentation-sync")
.then(|| (boot_id.clone(), Box::new(request)))
}
_ => None,
})
.expect("client presentation synchronization request");
assert!(
target_server.handle_server_event(ServerEvent::ClientShellEndpointRequest {
client_id: target_client_id,
boot_id: sync_request.0,
request: sync_request.1,
})
);
let (sync_request_id, sync_data) = loop {
let message = read_server_message(target_control.recv().expect("presentation sync ack"));
if let ServerMessage::ClientShellEndpointResponseChunk {
request_id, data, ..
} = message
{
if request_id.ends_with(":presentation-sync") {
break (request_id, data);
}
}
};
assert_eq!(
activation.receive_response(&target_id, 7, &sync_request_id, &sync_data, &mut endpoints,),
crate::client::endpoint::SurfaceActivationProgress::Pending
);
target_server.render_and_stream();
let sync_snapshot = loop {
let message =
read_server_message(target_control.recv().expect("presentation sync snapshot"));
if let ServerMessage::EndpointControl { kind, data } = message {
if kind == crate::protocol::endpoint::ENDPOINT_SNAPSHOT_KIND {
break serde_json::from_str::<crate::protocol::ClientShellSnapshot>(&data).unwrap();
}
}
};
let sync_progress = activation.receive_snapshot(&target_id, 7, &sync_snapshot);
shell.set_endpoint_snapshot_for_generation(&target_id, 7, Box::new(sync_snapshot));
assert_eq!(
sync_progress,
crate::client::endpoint::SurfaceActivationProgress::Pending
);
let ServerMessage::PaneSurface(sync_surface) =
read_server_message(target_render.recv().expect("presentation sync surface"))
else {
panic!("expected synchronized target surface");
};
assert_eq!(
activation.receive_surface(&target_id, 7, sync_surface),
crate::client::endpoint::SurfaceActivationProgress::Ready
);
assert_eq!(
activation.complete(&mut shell, &mut endpoints),
Ok(crate::client::endpoint::ActivationCompletion::AwaitingPresentationEffects)
);
let effects_token = target_sent
.lock()
.unwrap()
.iter()
.find_map(|message| match message {
crate::protocol::ClientMessage::EndpointControl { kind, data }
if kind == crate::protocol::endpoint::PRESENTATION_EFFECTS_SYNC_KIND =>
{
Some(data.clone())
}
_ => None,
})
.expect("client presentation effects fence");
assert!(
target_server.handle_server_event(ServerEvent::ClientShellPresentationSync {
client_id: target_client_id,
token: effects_token.clone(),
})
);
let mut replayed_mouse = false;
let mut replayed_keyboard = false;
loop {
match read_server_message(target_control.recv().expect("presentation effect or fence")) {
ServerMessage::MouseCapture { .. } => replayed_mouse = true,
ServerMessage::ClientShellKeyboardReportAll { .. } => replayed_keyboard = true,
ServerMessage::EndpointControl { kind, data }
if kind == crate::protocol::endpoint::PRESENTATION_EFFECTS_READY_KIND =>
{
assert_eq!(data, effects_token);
assert_eq!(
activation.receive_presentation_effects_ready(&target_id, 7, &data),
crate::client::endpoint::SurfaceActivationProgress::Ready
);
break;
}
ServerMessage::WindowTitle { .. } => {}
other => panic!("unexpected presentation fence message: {other:?}"),
}
}
assert!(replayed_mouse);
assert!(replayed_keyboard);
assert_eq!(
activation.complete(&mut shell, &mut endpoints),
Ok(crate::client::endpoint::ActivationCompletion::Activated)
);
endpoints.unfreeze_input();
assert_eq!(endpoints.active_id(), &target_id);
assert!(endpoints.active_surface_available());
assert!(shell.endpoint_is_active(&target_id));
target_sent.lock().unwrap().clear();
let mut returning = crate::client::endpoint::PendingEndpointActivation::begin(
&shell,
&mut endpoints,
ClientEndpointId::Local,
None,
lifecycle_resize(),
42,
std::time::Instant::now(),
)
.unwrap();
dispatch_lifecycle_messages(
&mut target_server,
target_client_id,
std::mem::take(&mut *target_sent.lock().unwrap()),
);
loop {
if let ServerMessage::ClientShellEndpointResponseChunk {
request_id, data, ..
} = read_server_message(target_control.recv().unwrap())
{
returning.receive_response(&target_id, 7, &request_id, &data, &mut endpoints);
break;
}
}
dispatch_lifecycle_messages(
&mut source_server,
source_client_id,
std::mem::take(&mut *source_sent.lock().unwrap()),
);
assert_eq!(
source_server.clients[&source_client_id].outer_terminal_focus,
Some(true),
"returning to Local must restore focus without a host focus event"
);
assert_eq!(
target_server.clients[&target_client_id].outer_terminal_focus,
Some(false)
);
shutdown_test_runtimes(&mut source_server);
shutdown_test_runtimes(&mut target_server);
}
|