1use crate::backend::obj::*;
9use crate::backend::service::{self, WebCallType, WebReturnType};
10
11use super::super::AppState;
12use super::layout_helpers::{
13 queue_source_layout_delete, queue_target_layout_insert, queue_target_layout_split,
14 queue_target_stack_push, setup_torn_off_stack_layout,
15};
16use super::redock_origin::{self, Resolution};
17use super::reducer_helpers::{dispatch_to_reducer, publish_events};
18
19pub(crate) async fn handle_tear_off_block(state: &AppState, call: &WebCallType) -> WebReturnType {
31 let store = &state.mstore;
32 let args = &call.args;
33 let block_id: String = match service::get_arg(args, 0) {
34 Ok(v) => v,
35 Err(e) => return WebReturnType::error(e),
36 };
37 let source_tab_id: String = match service::get_arg(args, 1) {
38 Ok(v) => v,
39 Err(e) => return WebReturnType::error(e),
40 };
41 let source_ws_id: String = match service::get_arg(args, 2) {
42 Ok(v) => v,
43 Err(e) => return WebReturnType::error(e),
44 };
45 let auto_close: bool = service::get_arg(args, 3).unwrap_or(true);
46 let members: Vec<String> = service::get_optional_arg(args, 4).unwrap_or(None).unwrap_or_default();
51 let order = crate::sagas::tear_off_block::tear_off_order(&block_id, &members);
52 tracing::info!(block_id = %block_id, members = order.len(), source_tab = %source_tab_id, source_ws = %source_ws_id, "[dnd:svc] TearOffBlock via saga");
53 let origin = redock_origin::record_for(state, &source_tab_id, &source_ws_id, &order).await;
55 let saga_result = crate::sagas::tear_off_block::run_stack(
56 state,
57 block_id.clone(),
58 members.clone(),
59 source_tab_id.clone(),
60 source_ws_id.clone(),
61 )
62 .await;
63 let (new_ws_oid, new_tab_oid) = match saga_result {
64 Ok(value) => {
65 let new_ws_oid = value
66 .get("new_workspace_id")
67 .and_then(|v| v.as_str())
68 .unwrap_or_default()
69 .to_string();
70 let new_tab_oid = value
71 .get("new_tab_id")
72 .and_then(|v| v.as_str())
73 .unwrap_or_default()
74 .to_string();
75 (new_ws_oid, new_tab_oid)
76 }
77 Err(reason) => return WebReturnType::error(reason),
78 };
79 redock_origin::remember(state, &order, origin.as_ref()).await;
80 super::pin::mark_floating_window(state, &new_ws_oid).await;
81
82 if let Err(e) = setup_torn_off_stack_layout(state, &new_tab_oid, &block_id, &members).await {
86 tracing::warn!(new_tab = %new_tab_oid, "TearOffBlock: layout setup failed: {} (block in tab but layout malformed)", e);
87 }
88 for member in &order {
95 if let Err(e) = queue_source_layout_delete(state, &source_tab_id, member).await {
96 tracing::warn!(source_tab = %source_tab_id, block = %member, "TearOffBlock: source layout delete-action enqueue failed: {}", e);
97 }
98 }
99
100 if auto_close {
105 let should_close = match store.must_get::<Tab>(&source_tab_id) {
106 Ok(t) => t.blockids.is_empty(),
107 Err(_) => false,
108 };
109 if should_close {
110 let total_tabs = match store.must_get::<Workspace>(&source_ws_id) {
111 Ok(ws) => ws.tabids.len() + ws.pinnedtabids.len(),
112 Err(_) => 0,
113 };
114 if total_tabs > 1 {
115 tracing::info!(source_tab = %source_tab_id, "[dnd:svc] auto-closing empty source tab after TearOffBlock");
116 let close_events = dispatch_to_reducer(
117 state,
118 agentmux_common::ipc::Command::DeleteTab {
119 workspace_id: source_ws_id.clone(),
120 tab_id: source_tab_id.clone(),
121 force: false,
124 },
125 )
126 .await;
127 for ev in &close_events {
128 let _ = crate::persist_subscriber::apply_event_to_mstore(ev, store);
129 }
130 publish_events(state, &close_events);
131 }
132 }
133 }
134
135 let updates = torn_off_updates(state, &source_tab_id, &source_ws_id, &new_ws_oid);
136 WebReturnType::success_data_updates(
137 serde_json::to_value(&new_ws_oid).unwrap_or_default(),
138 updates,
139 )
140}
141
142fn torn_off_updates(state: &AppState, source_tab_id: &str, source_ws_id: &str, new_ws_id: &str) -> Vec<MuxObjUpdate> {
144 let store = &state.mstore;
145 let mut updates = Vec::new();
146 if let Ok(tab) = store.must_get::<Tab>(source_tab_id) {
147 updates.push(MuxObjUpdate {
148 updatetype: "update".into(),
149 otype: OTYPE_TAB.to_string(),
150 oid: source_tab_id.to_string(),
151 obj: Some(mux_obj_to_value(&tab)),
152 });
153 }
154 for ws_id in [source_ws_id, new_ws_id] {
155 if let Ok(ws) = store.must_get::<Workspace>(ws_id) {
156 updates.push(MuxObjUpdate {
157 updatetype: "update".into(),
158 otype: OTYPE_WORKSPACE.to_string(),
159 oid: ws_id.to_string(),
160 obj: Some(mux_obj_to_value(&ws)),
161 });
162 }
163 }
164 updates
165}
166
167struct RedockArgs {
169 block_id: String,
170 source_tab_id: String,
171 source_ws_id: String,
172 target_tab_id: String,
173 target_ws_id: String,
174 target_block_id: Option<String>,
177 direction: Option<u8>,
178 as_tab: bool,
182 members: Vec<String>,
185 active_block_id: String,
187 to_origin: bool,
191}
192
193impl RedockArgs {
194 fn parse(args: &[serde_json::Value]) -> Result<Self, String> {
195 let block_id: String = service::get_arg(args, 0)?;
196 Ok(Self {
197 source_tab_id: service::get_arg(args, 1)?,
198 source_ws_id: service::get_arg(args, 2)?,
199 target_tab_id: service::get_arg(args, 3)?,
200 target_ws_id: service::get_arg(args, 4)?,
201 target_block_id: service::get_optional_arg(args, 5).unwrap_or(None),
202 direction: service::get_optional_arg(args, 6).unwrap_or(None),
203 as_tab: service::get_optional_arg(args, 7).unwrap_or(None).unwrap_or(false),
204 members: service::get_optional_arg(args, 8).unwrap_or(None).unwrap_or_default(),
205 active_block_id: service::get_optional_arg(args, 9).unwrap_or(None).unwrap_or_else(|| block_id.clone()),
206 to_origin: service::get_optional_arg(args, 10).unwrap_or(None).unwrap_or(false),
207 block_id,
208 })
209 }
210}
211
212pub(crate) async fn handle_redock_floating_pane(state: &AppState, call: &WebCallType) -> WebReturnType {
221 let mut a = match RedockArgs::parse(&call.args) {
222 Ok(a) => a,
223 Err(e) => return WebReturnType::error(e),
224 };
225 let origin = if a.to_origin {
226 let (tab, ws, origin) = redock_origin::target_for(state, &a.block_id, a.target_tab_id.clone(), a.target_ws_id.clone()).await;
227 a.target_tab_id = tab;
228 a.target_ws_id = ws;
229 origin
230 } else {
231 Resolution::Fallback
232 };
233 tracing::info!(
234 block_id = %a.block_id,
235 source_tab = %a.source_tab_id,
236 source_ws = %a.source_ws_id,
237 target_tab = %a.target_tab_id,
238 target_ws = %a.target_ws_id,
239 target_block_id = ?a.target_block_id,
240 direction = ?a.direction,
241 "[dnd:svc] RedockFloatingPane via saga"
242 );
243 let saga_result = crate::sagas::redock_floating_pane::run(
244 state,
245 a.block_id.clone(),
246 a.source_tab_id.clone(),
247 a.source_ws_id.clone(),
248 a.target_tab_id.clone(),
249 a.target_ws_id.clone(),
250 None,
251 )
252 .await;
253 if let Err(reason) = saga_result {
254 return WebReturnType::error(reason);
255 }
256
257 let was_minimized = super::layout_intents::block_is_minimized(state, &a.source_tab_id, &a.block_id).await;
260
261 if let Err(e) = queue_source_layout_delete(state, &a.source_tab_id, &a.block_id).await {
265 tracing::error!(
266 source_tab = %a.source_tab_id,
267 "RedockFloatingPane: source layout delete failed — moving the block back: {}",
268 e
269 );
270 put_redocked_block_back(state, &a.block_id, &a.target_tab_id, &a.source_tab_id).await;
271 return WebReturnType::error(format!("redock layout delete failed: {e}"));
272 }
273 if let Err(e) = place_in_target(state, &a, &origin).await {
274 tracing::error!(
277 target_tab = %a.target_tab_id,
278 "RedockFloatingPane: target layout action failed — moving the block back: {}",
279 e
280 );
281 put_redocked_block_back(state, &a.block_id, &a.target_tab_id, &a.source_tab_id).await;
282 return WebReturnType::error(format!("redock layout action failed: {e}"));
283 }
284 if was_minimized && !a.as_tab {
286 if let Err(e) = super::layout_intents::minimize_block_in_layout(state, &a.target_tab_id, &a.block_id).await {
287 tracing::warn!(target_tab = %a.target_tab_id, "RedockFloatingPane: keeping the moved chip minimized failed: {}", e);
288 }
289 }
290
291 let stack_failure = dock_stack_members(state, &a).await;
292
293 redock_origin::clear(state, &[vec![a.block_id.clone()], a.members.clone()].concat()).await;
295 let updates = publish_redock_updates(state, &a.source_tab_id, &a.target_tab_id);
296
297 if let Some(reason) = stack_failure {
298 return WebReturnType::error(reason);
299 }
300 WebReturnType::success_data_updates(
301 serde_json::json!({
302 "redocked": true,
303 "block_id": a.block_id,
304 "target_tab_id": a.target_tab_id,
305 "target_workspace_id": a.target_ws_id,
306 "placed": origin.label(),
307 }),
308 updates,
309 )
310}
311
312async fn place_in_target(state: &AppState, a: &RedockArgs, origin: &Resolution) -> Result<(), String> {
316 let (tab, block) = (&a.target_tab_id, &a.block_id);
317 match (origin, a.target_block_id.as_deref(), a.direction) {
318 (Resolution::StackMate(mate), _, _) => redock_as_tab(state, tab, block, mate, true).await,
319 (Resolution::Split(anchor), _, _) => redock_origin::place_at_anchor(state, tab, block, anchor).await,
320 (_, Some(target), _) if a.as_tab => redock_as_tab(state, tab, block, target, true).await,
321 (_, Some(target), Some(dir)) => queue_target_layout_split(state, tab, block, target, dir).await,
322 _ => queue_target_layout_insert(state, tab, block).await,
323 }
324}
325
326async fn dock_stack_members(state: &AppState, a: &RedockArgs) -> Option<String> {
330 for member in a.members.iter().filter(|m| **m != a.block_id) {
331 if let Err(reason) = redock_stack_member(
332 state,
333 member,
334 &a.block_id,
335 &a.source_tab_id,
336 &a.source_ws_id,
337 &a.target_tab_id,
338 &a.target_ws_id,
339 *member == a.active_block_id,
340 )
341 .await
342 {
343 return Some(reason);
344 }
345 }
346 None
347}
348
349fn publish_redock_updates(state: &AppState, source_tab_id: &str, target_tab_id: &str) -> Vec<MuxObjUpdate> {
351 let updates = redock_updates(state, source_tab_id, target_tab_id);
352 state.event_bus.broadcast_mux_obj_updates(&updates);
362 updates
363}
364
365fn redock_updates(state: &AppState, source_tab_id: &str, target_tab_id: &str) -> Vec<MuxObjUpdate> {
371 let store = &state.mstore;
372 let mut updates = Vec::new();
373 for tab_id in [source_tab_id, target_tab_id] {
374 let Ok(tab) = store.must_get::<Tab>(tab_id) else { continue };
375 if let Ok(layout) = store.must_get::<LayoutState>(&tab.layoutstate) {
376 updates.push(MuxObjUpdate {
377 updatetype: "update".into(),
378 otype: OTYPE_LAYOUT.to_string(),
379 oid: tab.layoutstate.clone(),
380 obj: Some(mux_obj_to_value(&layout)),
381 });
382 }
383 updates.push(MuxObjUpdate {
384 updatetype: "update".into(),
385 otype: OTYPE_TAB.to_string(),
386 oid: tab_id.to_string(),
387 obj: Some(mux_obj_to_value(&tab)),
388 });
389 }
390 updates
391}
392
393async fn put_redocked_block_back(state: &AppState, block_id: &str, target_tab_id: &str, source_tab_id: &str) {
398 super::reducer_helpers::compensate_via_reducer(
399 state,
400 agentmux_common::ipc::Command::MoveBlock {
401 block_id: block_id.to_string(),
402 src_tab_id: target_tab_id.to_string(),
403 dst_tab_id: source_tab_id.to_string(),
404 dst_index: u32::MAX,
405 },
406 &state.mstore,
407 )
408 .await;
409 let placed = state
410 .srv_state
411 .lock()
412 .await
413 .tabs
414 .get(source_tab_id)
415 .and_then(|t| t.rootnode.as_ref())
416 .is_some_and(|root| crate::backend::layout::find_leaf_containing_block(root, block_id).is_some());
417 if !placed {
418 if let Err(e) = queue_target_layout_insert(state, source_tab_id, block_id).await {
419 tracing::error!(source_tab = %source_tab_id, "RedockFloatingPane: putting the block back in the source layout failed: {}", e);
420 }
421 }
422}
423
424#[allow(clippy::too_many_arguments)]
429async fn redock_stack_member(
430 state: &AppState,
431 block_id: &str,
432 pane_block_id: &str,
433 source_tab_id: &str,
434 source_ws_id: &str,
435 target_tab_id: &str,
436 target_ws_id: &str,
437 activate: bool,
438) -> Result<(), String> {
439 crate::sagas::redock_floating_pane::run(
440 state,
441 block_id.to_string(),
442 source_tab_id.to_string(),
443 source_ws_id.to_string(),
444 target_tab_id.to_string(),
445 target_ws_id.to_string(),
446 None,
447 )
448 .await?;
449 if let Err(e) = queue_source_layout_delete(state, source_tab_id, block_id).await {
450 put_redocked_block_back(state, block_id, target_tab_id, source_tab_id).await;
451 return Err(format!("redock layout delete failed: {e}"));
452 }
453 if let Err(e) = redock_as_tab(state, target_tab_id, block_id, pane_block_id, activate).await {
454 put_redocked_block_back(state, block_id, target_tab_id, source_tab_id).await;
455 return Err(format!("redock layout action failed: {e}"));
456 }
457 Ok(())
458}
459
460pub(super) async fn redock_as_tab(
466 state: &AppState,
467 target_tab_id: &str,
468 block_id: &str,
469 target_block_id: &str,
470 activate: bool,
471) -> Result<(), String> {
472 let events = dispatch_to_reducer(
473 state,
474 agentmux_common::ipc::Command::LayoutStackPush {
475 tab_id: target_tab_id.to_string(),
476 target_block_id: target_block_id.to_string(),
477 block_id: block_id.to_string(),
478 activate,
479 correlation_id: String::new(),
480 },
481 )
482 .await;
483 if let Some(msg) = events.iter().find_map(|e| match e {
484 agentmux_common::ipc::Event::Error { message, .. } => Some(message.clone()),
485 _ => None,
486 }) {
487 return Err(msg);
488 }
489 for ev in &events {
490 let _ = crate::persist_subscriber::apply_event_to_mstore(ev, &state.mstore);
491 }
492 publish_events(state, &events);
493 queue_target_stack_push(state, target_tab_id, block_id, target_block_id, activate).await
494}
495
496pub(crate) async fn handle_tear_off_tab(state: &AppState, call: &WebCallType) -> WebReturnType {
502 let store = &state.mstore;
503 let args = &call.args;
504 let tab_id: String = match service::get_arg(args, 0) {
505 Ok(v) => v,
506 Err(e) => return WebReturnType::error(e),
507 };
508 let source_ws_id: String = match service::get_arg(args, 1) {
509 Ok(v) => v,
510 Err(e) => return WebReturnType::error(e),
511 };
512 tracing::info!(tab_id = %tab_id, source_ws = %source_ws_id, "[dnd:svc] TearOffTab via saga");
513 match crate::sagas::tear_off_tab::run(state, tab_id, source_ws_id.clone()).await {
514 Ok(saga_result) => {
515 let new_ws_oid = saga_result
516 .get("new_workspace_id")
517 .and_then(|v| v.as_str())
518 .unwrap_or_default()
519 .to_string();
520 let mut updates = Vec::new();
521 if let Ok(src_ws) = store.must_get::<Workspace>(&source_ws_id) {
522 updates.push(MuxObjUpdate {
523 updatetype: "update".into(),
524 otype: OTYPE_WORKSPACE.to_string(),
525 oid: source_ws_id.clone(),
526 obj: Some(mux_obj_to_value(&src_ws)),
527 });
528 }
529 if let Ok(new_ws) = store.must_get::<Workspace>(&new_ws_oid) {
530 updates.push(MuxObjUpdate {
531 updatetype: "update".into(),
532 otype: OTYPE_WORKSPACE.to_string(),
533 oid: new_ws_oid.clone(),
534 obj: Some(mux_obj_to_value(&new_ws)),
535 });
536 }
537 WebReturnType::success_data_updates(
538 serde_json::to_value(&new_ws_oid).unwrap_or_default(),
539 updates,
540 )
541 }
542 Err(reason) => WebReturnType::error(reason),
543 }
544}
545
546#[cfg(test)]
547mod tests {
548 use super::*;
551 use crate::server::tests::{dispatch_apply, test_state};
552 use agentmux_common::ipc::{Command, Event};
553
554 fn call(method: &str, args: Vec<serde_json::Value>) -> WebCallType {
555 WebCallType { service: "workspace".into(), method: method.into(), uicontext: None, args }
556 }
557
558 async fn two_tab_pane_and_a_neighbour(state: &AppState) -> (String, String, String, String, String) {
561 let ws_id = dispatch_apply(state, Command::CreateWorkspace { name: "w".into() })
562 .await
563 .iter()
564 .find_map(|e| match e {
565 Event::WorkspaceCreated { workspace_id, .. } => Some(workspace_id.clone()),
566 _ => None,
567 })
568 .unwrap();
569 let tab_id = dispatch_apply(state, Command::CreateTab { workspace_id: ws_id.clone(), name: "t".into() })
570 .await
571 .iter()
572 .find_map(|e| match e {
573 Event::TabCreated { tab_id, .. } => Some(tab_id.clone()),
574 _ => None,
575 })
576 .unwrap();
577 let mut ids = Vec::new();
578 for _ in 0..3 {
579 let id = dispatch_apply(state, Command::CreateBlock { tab_id: tab_id.clone(), meta: serde_json::Value::Null })
580 .await
581 .iter()
582 .find_map(|e| match e {
583 Event::BlockCreated { block_id, .. } => Some(block_id.clone()),
584 _ => None,
585 })
586 .unwrap();
587 ids.push(id);
588 }
589 let (a, b, c) = (ids[0].clone(), ids[1].clone(), ids[2].clone());
590 dispatch_apply(
591 state,
592 Command::LayoutSetTree {
593 tab_id: tab_id.clone(),
594 new_tree: Some(agentmux_common::LayoutNode {
595 id: "root".into(),
596 children: vec![
597 agentmux_common::LayoutNode {
598 id: "pane-src".into(),
599 data: Some(agentmux_common::LayoutNodeData {
600 block_id: a.clone(),
601 block_stack: vec![a.clone(), b.clone()],
602 active_block_id: a.clone(),
603 ..Default::default()
604 }),
605 ..Default::default()
606 },
607 agentmux_common::LayoutNode {
608 id: "pane-other".into(),
609 data: Some(agentmux_common::LayoutNodeData { block_id: c.clone(), ..Default::default() }),
610 ..Default::default()
611 },
612 ],
613 ..Default::default()
614 }),
615 correlation_id: String::new(),
616 slices: None,
617 },
618 )
619 .await;
620 (ws_id, tab_id, a, b, c)
621 }
622
623 async fn tear_off(state: &AppState, block: &str, tab_id: &str, ws_id: &str) -> String {
624 let ret = handle_tear_off_block(
625 state,
626 &call("TearOffBlock", vec![block.into(), tab_id.into(), ws_id.into(), true.into()]),
627 )
628 .await;
629 assert!(ret.error.is_none(), "TearOffBlock failed: {:?}", ret.error);
630 ret.data.as_ref().and_then(|v| v.as_str()).expect("new workspace id").to_string()
631 }
632
633 async fn src_pane_members(state: &AppState, tab_id: &str) -> (Vec<String>, String, Vec<String>) {
634 let srv = state.srv_state.lock().await;
635 let tab = &srv.tabs[tab_id];
636 let root = tab.rootnode.as_ref().expect("source tree");
637 let src = crate::backend::layout::find_node_by_id(root, "pane-src").expect("source pane survives");
638 let data = src.data.as_ref().unwrap();
639 (crate::backend::layout::leaf_members(data), data.block_id.clone(), tab.block_ids.clone())
640 }
641
642 #[tokio::test]
643 async fn tearing_off_a_background_tab_keeps_its_pane_and_siblings_in_the_backend_tree() {
644 let state = test_state();
645 let (ws_id, tab_id, a, b, c) = two_tab_pane_and_a_neighbour(&state).await;
646
647 tear_off(&state, &b, &tab_id, &ws_id).await;
648
649 let (members, visible, blocks) = src_pane_members(&state, &tab_id).await;
650 assert_eq!(members, vec![a.clone()], "only the torn-off tab left the pane");
651 assert_eq!(visible, a);
652 assert!(blocks.contains(&a) && blocks.contains(&c) && !blocks.contains(&b));
653 let tab = state.mstore.must_get::<Tab>(&tab_id).unwrap();
655 let layout = state.mstore.must_get::<LayoutState>(&tab.layoutstate).unwrap();
656 assert!(
657 layout.pendingbackendactions.unwrap_or_default().iter().any(|x| x.actiontype == "delete" && x.blockid == b),
658 "queued frontend delete"
659 );
660 }
661
662 #[tokio::test]
663 async fn tearing_off_the_visible_tab_makes_its_neighbour_visible() {
664 let state = test_state();
665 let (ws_id, tab_id, a, b, _c) = two_tab_pane_and_a_neighbour(&state).await;
666
667 tear_off(&state, &a, &tab_id, &ws_id).await;
668
669 let (members, visible, _) = src_pane_members(&state, &tab_id).await;
670 assert_eq!(members, vec![b.clone()]);
671 assert_eq!(visible, b, "the right-hand neighbour becomes visible");
672 }
673
674 #[tokio::test]
675 async fn a_redock_whose_layout_step_fails_puts_the_block_back_where_it_was() {
676 let state = test_state();
677 let (ws_id, tab_id, _a, b, _c) = two_tab_pane_and_a_neighbour(&state).await;
678 let new_ws = tear_off(&state, &b, &tab_id, &ws_id).await;
679 let floater_tab = state.srv_state.lock().await.workspaces[&new_ws].tab_ids[0].clone();
680
681 let ret = handle_redock_floating_pane(
684 &state,
685 &call(
686 "RedockFloatingPane",
687 vec![
688 b.clone().into(),
689 floater_tab.clone().into(),
690 new_ws.into(),
691 tab_id.clone().into(),
692 ws_id.into(),
693 "no-such-block".into(),
694 serde_json::Value::Null,
695 true.into(),
696 ],
697 ),
698 )
699 .await;
700 assert!(ret.error.is_some(), "the redock reports the failure");
701
702 let s = state.srv_state.lock().await;
703 let holds = |tab: &str| {
704 s.tabs[tab].rootnode.as_ref().is_some_and(|r| crate::backend::layout::find_leaf_containing_block(r, &b).is_some())
705 };
706 assert_eq!(s.blocks[&b].tab_id, floater_tab, "the block is back in its own tab");
707 assert!(s.tabs[&floater_tab].block_ids.contains(&b));
708 assert!(holds(&floater_tab), "and in that tab's layout");
709 assert!(!s.tabs[&tab_id].block_ids.contains(&b), "the target tab doesn't own it");
710 assert!(!holds(&tab_id), "and doesn't show it");
711 }
712
713 #[tokio::test]
714 async fn redock_as_tab_puts_a_torn_off_tab_back_into_its_pane() {
715 let state = test_state();
716 let (ws_id, tab_id, a, b, _c) = two_tab_pane_and_a_neighbour(&state).await;
717 let new_ws = tear_off(&state, &b, &tab_id, &ws_id).await;
718 let floater_tab = state.srv_state.lock().await.workspaces[&new_ws].tab_ids[0].clone();
719
720 let ret = handle_redock_floating_pane(
722 &state,
723 &call(
724 "RedockFloatingPane",
725 vec![
726 b.clone().into(),
727 floater_tab.into(),
728 new_ws.into(),
729 tab_id.clone().into(),
730 ws_id.into(),
731 a.clone().into(),
732 serde_json::Value::Null,
733 true.into(),
734 ],
735 ),
736 )
737 .await;
738 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
739
740 let (members, visible, blocks) = src_pane_members(&state, &tab_id).await;
741 assert_eq!(members, vec![a, b.clone()], "back in its pane's stack, not a split");
742 assert_eq!(visible, b, "and visible");
743 assert!(blocks.contains(&b), "and back in the tab");
744 let tab = state.mstore.must_get::<Tab>(&tab_id).unwrap();
745 let layout = state.mstore.must_get::<LayoutState>(&tab.layoutstate).unwrap();
746 assert!(
747 layout.pendingbackendactions.unwrap_or_default().iter().any(|x| x.actiontype == "stackpush" && x.blockid == b),
748 "the window showing the pane is told to add the pill"
749 );
750 }
751
752 async fn tear_off_pane(state: &AppState, block: &str, members: &[String], tab_id: &str, ws_id: &str) -> String {
754 let ret = handle_tear_off_block(
755 state,
756 &call(
757 "TearOffBlock",
758 vec![block.into(), tab_id.into(), ws_id.into(), true.into(), serde_json::json!(members)],
759 ),
760 )
761 .await;
762 assert!(ret.error.is_none(), "TearOffBlock failed: {:?}", ret.error);
763 ret.data.as_ref().and_then(|v| v.as_str()).expect("new workspace id").to_string()
764 }
765
766 async fn floater_pane(state: &AppState, new_ws: &str) -> (String, Vec<String>, String, Vec<String>) {
767 let srv = state.srv_state.lock().await;
768 let tab_id = srv.workspaces[new_ws].tab_ids[0].clone();
769 let tab = &srv.tabs[&tab_id];
770 let root = tab.rootnode.as_ref().expect("floater tree");
771 let data = root.data.as_ref().expect("one leaf");
772 (tab_id, crate::backend::layout::leaf_members(data), data.active_block_id.clone(), tab.block_ids.clone())
773 }
774
775 #[tokio::test]
776 async fn tearing_off_a_whole_pane_moves_every_tab_and_the_pane_with_them() {
777 let state = test_state();
778 let (ws_id, tab_id, a, b, c) = two_tab_pane_and_a_neighbour(&state).await;
779
780 let new_ws = tear_off_pane(&state, &b, &[a.clone(), b.clone()], &tab_id, &ws_id).await;
782
783 let (_, members, active, floater_blocks) = floater_pane(&state, &new_ws).await;
784 assert_eq!(members, vec![a.clone(), b.clone()], "the pane keeps its tab order");
785 assert_eq!(active, b, "and its visible tab");
786 assert_eq!(floater_blocks.first(), Some(&b), "the visible block is the floater tab's first");
787 assert_eq!(floater_blocks.len(), 2);
788
789 let srv = state.srv_state.lock().await;
791 let src = &srv.tabs[&tab_id];
792 assert_eq!(src.block_ids, vec![c.clone()]);
793 let root = src.rootnode.as_ref().unwrap();
794 assert!(crate::backend::layout::find_node_by_id(root, "pane-src").is_none(), "the pane left");
795 assert!(crate::backend::layout::find_leaf_containing_block(root, &c).is_some());
796 drop(srv);
797 let tab = state.mstore.must_get::<Tab>(&tab_id).unwrap();
798 let layout = state.mstore.must_get::<LayoutState>(&tab.layoutstate).unwrap();
799 let deletes: Vec<_> = layout
800 .pendingbackendactions
801 .unwrap_or_default()
802 .into_iter()
803 .filter(|x| x.actiontype == "delete")
804 .map(|x| x.blockid)
805 .collect();
806 assert!(deletes.contains(&a) && deletes.contains(&b), "the source window is told to drop both: {deletes:?}");
807 }
808
809 #[tokio::test]
810 async fn a_whole_pane_tear_off_with_a_member_from_another_tab_moves_nothing() {
811 let state = test_state();
812 let (ws_id, tab_id, a, b, _c) = two_tab_pane_and_a_neighbour(&state).await;
813 let ret = handle_tear_off_block(
814 &state,
815 &call(
816 "TearOffBlock",
817 vec![
818 b.clone().into(),
819 tab_id.clone().into(),
820 ws_id.into(),
821 true.into(),
822 serde_json::json!([a.clone(), b.clone(), "ghost-block"]),
823 ],
824 ),
825 )
826 .await;
827 assert!(ret.error.is_some(), "an unknown member is rejected up front");
828 let srv = state.srv_state.lock().await;
829 assert!(srv.tabs[&tab_id].block_ids.contains(&a) && srv.tabs[&tab_id].block_ids.contains(&b));
830 assert_eq!(srv.workspaces.len(), 1, "no workspace was left behind");
831 }
832
833 #[tokio::test]
834 async fn redocking_a_torn_off_pane_puts_every_tab_back_in_order_with_the_same_visible_one() {
835 let state = test_state();
836 let (ws_id, tab_id, a, b, c) = two_tab_pane_and_a_neighbour(&state).await;
837 let new_ws = tear_off_pane(&state, &b, &[a.clone(), b.clone()], &tab_id, &ws_id).await;
838 let (floater_tab, ..) = floater_pane(&state, &new_ws).await;
839
840 let ret = handle_redock_floating_pane(
842 &state,
843 &call(
844 "RedockFloatingPane",
845 vec![
846 a.clone().into(),
847 floater_tab.clone().into(),
848 new_ws.clone().into(),
849 tab_id.clone().into(),
850 ws_id.into(),
851 c.clone().into(),
852 1.into(),
853 serde_json::Value::Null,
854 serde_json::json!([a.clone(), b.clone()]),
855 b.clone().into(),
856 ],
857 ),
858 )
859 .await;
860 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
861
862 let srv = state.srv_state.lock().await;
863 let root = srv.tabs[&tab_id].rootnode.as_ref().unwrap();
864 let pane = crate::backend::layout::find_leaf_containing_block(root, &a).expect("the pane is back");
865 let data = pane.data.as_ref().unwrap();
866 assert_eq!(crate::backend::layout::leaf_members(data), vec![a.clone(), b.clone()], "same tab order");
867 assert_eq!(data.block_id, b, "same visible tab");
868 assert!(srv.tabs[&floater_tab].block_ids.is_empty(), "nothing is left in the floater");
869 assert!(srv.tabs[&tab_id].block_ids.contains(&a) && srv.tabs[&tab_id].block_ids.contains(&b));
870 }
871
872 async fn open_window_on(state: &AppState, ws_id: &str) {
876 state.srv_state.lock().await.windows.insert(
877 "w1".into(),
878 crate::state::WindowRecord { window_id: "w1".into(), workspace_id: ws_id.into() },
879 );
880 }
881
882 async fn new_tab(state: &AppState, ws_id: &str) -> String {
883 dispatch_apply(state, Command::CreateTab { workspace_id: ws_id.into(), name: "fallback".into() })
884 .await
885 .iter()
886 .find_map(|e| match e {
887 Event::TabCreated { tab_id, .. } => Some(tab_id.clone()),
888 _ => None,
889 })
890 .unwrap()
891 }
892
893 async fn redock_by_button(state: &AppState, block: &str, floater_tab: &str, floater_ws: &str, fallback_tab: &str, fallback_ws: &str) -> WebReturnType {
895 handle_redock_floating_pane(
896 state,
897 &call(
898 "RedockFloatingPane",
899 vec![
900 block.into(),
901 floater_tab.into(),
902 floater_ws.into(),
903 fallback_tab.into(),
904 fallback_ws.into(),
905 serde_json::Value::Null,
906 serde_json::Value::Null,
907 false.into(),
908 serde_json::json!([block]),
909 block.into(),
910 true.into(),
911 ],
912 ),
913 )
914 .await
915 }
916
917 async fn leaf_blocks(state: &AppState, tab_id: &str) -> Vec<String> {
918 fn walk(n: &agentmux_common::LayoutNode, out: &mut Vec<String>) {
919 if let Some(d) = n.data.as_ref() {
920 out.push(d.block_id.clone());
921 }
922 n.children.iter().for_each(|c| walk(c, out));
923 }
924 let srv = state.srv_state.lock().await;
925 let mut out = Vec::new();
926 if let Some(root) = srv.tabs[tab_id].rootnode.as_ref() {
927 walk(root, &mut out);
928 }
929 out
930 }
931
932 #[tokio::test]
933 async fn the_button_puts_a_torn_off_pane_back_beside_its_neighbour_not_in_the_fallback_tab() {
934 let state = test_state();
935 let (ws_id, tab_id, a, _b, c) = two_tab_pane_and_a_neighbour(&state).await;
936 open_window_on(&state, &ws_id).await;
937 let fallback_tab = new_tab(&state, &ws_id).await;
938 let new_ws = tear_off(&state, &c, &tab_id, &ws_id).await;
939 let floater_tab = state.srv_state.lock().await.workspaces[&new_ws].tab_ids[0].clone();
940 let recorded = state.mstore.must_get::<Block>(&c).unwrap().meta.contains_key(redock_origin::META_KEY);
941 assert!(recorded, "the tear-off recorded where the pane was");
942
943 let ret = redock_by_button(&state, &c, &floater_tab, &new_ws, &fallback_tab, &ws_id).await;
944 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
945
946 let data = ret.data.unwrap();
947 assert_eq!(data["placed"], "neighbour");
948 assert_eq!(data["target_tab_id"], tab_id.as_str());
949 assert_eq!(leaf_blocks(&state, &tab_id).await, vec![a, c.clone()], "after its old neighbour, as it was");
950 assert!(leaf_blocks(&state, &fallback_tab).await.is_empty(), "nothing went to the fallback tab");
951 let cleared = !state.mstore.must_get::<Block>(&c).unwrap().meta.contains_key(redock_origin::META_KEY);
952 assert!(cleared, "a docked pane keeps no origin");
953 }
954
955 #[tokio::test]
956 async fn the_button_falls_back_to_the_named_tab_when_the_origin_window_is_gone() {
957 let state = test_state();
958 let (ws_id, tab_id, _a, _b, c) = two_tab_pane_and_a_neighbour(&state).await;
959 let fallback_tab = new_tab(&state, &ws_id).await;
961 let new_ws = tear_off(&state, &c, &tab_id, &ws_id).await;
962 let floater_tab = state.srv_state.lock().await.workspaces[&new_ws].tab_ids[0].clone();
963
964 let ret = redock_by_button(&state, &c, &floater_tab, &new_ws, &fallback_tab, &ws_id).await;
965 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
966
967 let data = ret.data.unwrap();
968 assert_eq!(data["placed"], "fallback");
969 assert_eq!(data["target_tab_id"], fallback_tab.as_str());
970 assert_eq!(leaf_blocks(&state, &fallback_tab).await, vec![c]);
971 }
972
973 #[tokio::test]
974 async fn a_redock_that_names_its_own_target_ignores_the_origin() {
975 let state = test_state();
976 let (ws_id, tab_id, _a, _b, c) = two_tab_pane_and_a_neighbour(&state).await;
977 open_window_on(&state, &ws_id).await;
978 let other_tab = new_tab(&state, &ws_id).await;
979 let new_ws = tear_off(&state, &c, &tab_id, &ws_id).await;
980 let floater_tab = state.srv_state.lock().await.workspaces[&new_ws].tab_ids[0].clone();
981
982 let ret = handle_redock_floating_pane(
984 &state,
985 &call(
986 "RedockFloatingPane",
987 vec![c.clone().into(), floater_tab.into(), new_ws.into(), other_tab.clone().into(), ws_id.into()],
988 ),
989 )
990 .await;
991 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
992 assert_eq!(ret.data.unwrap()["placed"], "fallback");
993 assert_eq!(leaf_blocks(&state, &other_tab).await, vec![c], "it went where it was dropped");
994 }
995
996 async fn two_tabs(state: &AppState, src: &[bool], dst: &[bool]) -> (String, String, Vec<String>, String, Vec<String>) {
1004 let ws_id = dispatch_apply(state, Command::CreateWorkspace { name: "w".into() })
1005 .await
1006 .iter()
1007 .find_map(|e| match e {
1008 Event::WorkspaceCreated { workspace_id, .. } => Some(workspace_id.clone()),
1009 _ => None,
1010 })
1011 .unwrap();
1012 let mut tabs = Vec::new();
1013 for (name, panes) in [("src", src), ("dst", dst)] {
1014 let tab_id = new_tab(state, &ws_id).await;
1015 let mut blocks = Vec::new();
1016 for _ in panes {
1017 let ev = dispatch_apply(state, Command::CreateBlock { tab_id: tab_id.clone(), meta: serde_json::Value::Null }).await;
1018 blocks.push(
1019 ev.iter()
1020 .find_map(|e| match e {
1021 Event::BlockCreated { block_id, .. } => Some(block_id.clone()),
1022 _ => None,
1023 })
1024 .unwrap(),
1025 );
1026 }
1027 let pane = |i: usize| agentmux_common::LayoutNode {
1028 id: format!("{name}-{i}"),
1029 data: Some(agentmux_common::LayoutNodeData { block_id: blocks[i].clone(), ..Default::default() }),
1030 minimized: panes[i],
1031 ..Default::default()
1032 };
1033 let tree = match panes.len() {
1034 0 => None,
1035 1 => Some(pane(0)),
1036 n => Some(agentmux_common::LayoutNode { id: format!("{name}-root"), children: (0..n).map(pane).collect(), ..Default::default() }),
1037 };
1038 dispatch_apply(
1039 state,
1040 Command::LayoutSetTree { tab_id: tab_id.clone(), new_tree: tree, correlation_id: String::new(), slices: None },
1041 )
1042 .await;
1043 tabs.push((tab_id, blocks));
1044 }
1045 let (dst_tab, dst_blocks) = tabs.pop().unwrap();
1046 let (src_tab, src_blocks) = tabs.pop().unwrap();
1047 (ws_id, src_tab, src_blocks, dst_tab, dst_blocks)
1048 }
1049
1050 async fn redock_dragged(state: &AppState, block: &str, src_tab: &str, dst_tab: &str, ws_id: &str, onto: Option<&str>) -> WebReturnType {
1053 let mut args: Vec<serde_json::Value> =
1054 vec![block.into(), src_tab.into(), ws_id.into(), dst_tab.into(), ws_id.into()];
1055 if let Some(target) = onto {
1056 args.extend([target.into(), serde_json::Value::Null, true.into()]);
1057 }
1058 handle_redock_floating_pane(state, &call("RedockFloatingPane", args)).await
1059 }
1060
1061 async fn is_chip(state: &AppState, tab_id: &str, block: &str) -> Option<bool> {
1063 let srv = state.srv_state.lock().await;
1064 let root = srv.tabs[tab_id].rootnode.as_ref()?;
1065 crate::backend::layout::find_leaf_containing_block(root, block).map(|leaf| leaf.minimized)
1066 }
1067
1068 fn queued_minimize_actions(state: &AppState, tab_id: &str) -> Vec<String> {
1070 let tab = state.mstore.must_get::<Tab>(tab_id).unwrap();
1071 let layout = state.mstore.must_get::<LayoutState>(&tab.layoutstate).unwrap();
1072 layout
1073 .pendingbackendactions
1074 .unwrap_or_default()
1075 .iter()
1076 .filter(|a| a.actiontype == "minimize")
1077 .map(|a| a.blockid.clone())
1078 .collect()
1079 }
1080
1081 #[tokio::test]
1082 async fn a_chip_redocked_into_a_tab_with_an_expanded_pane_stays_a_chip() {
1083 let state = test_state();
1084 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[true, false], &[false]).await;
1085 let chip = src[0].clone();
1086 assert_eq!(is_chip(&state, &src_tab, &chip).await, Some(true));
1087
1088 let ret = redock_dragged(&state, &chip, &src_tab, &dst_tab, &ws_id, None).await;
1089 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1090
1091 assert_eq!(is_chip(&state, &dst_tab, &chip).await, Some(true), "it arrives as a chip");
1092 assert_eq!(is_chip(&state, &dst_tab, &dst[0]).await, Some(false), "the pane already there still shows");
1093 assert_eq!(is_chip(&state, &src_tab, &chip).await, None, "the source tab no longer shows it");
1094 assert_eq!(state.srv_state.lock().await.blocks[&chip].tab_id, dst_tab, "and the target tab owns it");
1095 assert_eq!(queued_minimize_actions(&state, &dst_tab), vec![chip], "the window showing the target is told too");
1096 }
1097
1098 #[tokio::test]
1099 async fn a_chip_redocked_into_an_empty_tab_arrives_expanded() {
1100 let state = test_state();
1101 let (ws_id, src_tab, src, dst_tab, _dst) = two_tabs(&state, &[true, false], &[]).await;
1102 let chip = src[0].clone();
1103
1104 let ret = redock_dragged(&state, &chip, &src_tab, &dst_tab, &ws_id, None).await;
1105 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1106
1107 assert_eq!(is_chip(&state, &dst_tab, &chip).await, Some(false), "it arrives expanded");
1110 assert!(queued_minimize_actions(&state, &dst_tab).is_empty(), "nothing tells the window to minimize it");
1111 assert_eq!(is_chip(&state, &src_tab, &chip).await, None, "the source tab no longer shows it");
1112 }
1113
1114 #[tokio::test]
1115 async fn a_chip_redocked_into_a_tab_of_nothing_but_chips_arrives_expanded() {
1116 let state = test_state();
1119 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[true, false], &[true, true]).await;
1120 let chip = src[0].clone();
1121
1122 let ret = redock_dragged(&state, &chip, &src_tab, &dst_tab, &ws_id, None).await;
1123 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1124
1125 assert_eq!(is_chip(&state, &dst_tab, &chip).await, Some(false), "it arrives expanded");
1126 assert_eq!(is_chip(&state, &dst_tab, &dst[0]).await, Some(true), "the chips there are untouched");
1127 assert_eq!(is_chip(&state, &dst_tab, &dst[1]).await, Some(true));
1128 assert!(queued_minimize_actions(&state, &dst_tab).is_empty(), "nothing tells the window to minimize it");
1129 assert_eq!(is_chip(&state, &src_tab, &chip).await, None, "the source tab no longer shows it");
1130 assert_eq!(state.srv_state.lock().await.blocks[&chip].tab_id, dst_tab, "and the target tab owns it");
1131 }
1132
1133 #[tokio::test]
1134 async fn an_expanded_pane_redocked_into_a_tab_of_nothing_but_chips_stays_expanded() {
1135 let state = test_state();
1136 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[false, false], &[true]).await;
1137 let pane = src[0].clone();
1138
1139 let ret = redock_dragged(&state, &pane, &src_tab, &dst_tab, &ws_id, None).await;
1140 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1141
1142 assert_eq!(is_chip(&state, &dst_tab, &pane).await, Some(false));
1143 assert_eq!(is_chip(&state, &dst_tab, &dst[0]).await, Some(true), "the chip there is still a chip");
1144 }
1145
1146 #[tokio::test]
1147 async fn an_expanded_pane_redocked_to_another_tab_stays_expanded() {
1148 let state = test_state();
1149 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[false, false], &[false]).await;
1150 let pane = src[0].clone();
1151
1152 let ret = redock_dragged(&state, &pane, &src_tab, &dst_tab, &ws_id, None).await;
1153 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1154
1155 assert_eq!(is_chip(&state, &dst_tab, &pane).await, Some(false));
1156 assert_eq!(is_chip(&state, &dst_tab, &dst[0]).await, Some(false));
1157 assert!(queued_minimize_actions(&state, &dst_tab).is_empty());
1158 }
1159
1160 #[tokio::test]
1161 async fn a_chip_dropped_as_a_tab_of_an_expanded_pane_takes_that_panes_state() {
1162 let state = test_state();
1163 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[true, false], &[false, true]).await;
1164 let chip = src[0].clone();
1165
1166 let ret = redock_dragged(&state, &chip, &src_tab, &dst_tab, &ws_id, Some(&dst[0])).await;
1167 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1168
1169 assert_eq!(is_chip(&state, &dst_tab, &chip).await, Some(false), "shown with the pane it joined");
1171 assert_eq!(is_chip(&state, &dst_tab, &dst[0]).await, Some(false), "that pane is still expanded");
1172 assert_eq!(is_chip(&state, &dst_tab, &dst[1]).await, Some(true), "the other chip is still a chip");
1173 assert!(queued_minimize_actions(&state, &dst_tab).is_empty());
1174 }
1175
1176 #[tokio::test]
1177 async fn a_pane_dropped_as_a_tab_of_a_chip_takes_the_chips_state() {
1178 let state = test_state();
1179 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[false, false], &[true, false]).await;
1180 let pane = src[0].clone();
1181
1182 let ret = redock_dragged(&state, &pane, &src_tab, &dst_tab, &ws_id, Some(&dst[0])).await;
1183 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1184
1185 assert_eq!(is_chip(&state, &dst_tab, &pane).await, Some(true), "it is a tab of the chip, so it is shown as one");
1186 assert_eq!(is_chip(&state, &dst_tab, &dst[0]).await, Some(true), "the chip is still a chip");
1187 assert_eq!(is_chip(&state, &dst_tab, &dst[1]).await, Some(false));
1188 assert!(queued_minimize_actions(&state, &dst_tab).is_empty(), "no minimize of its own was sent");
1189 }
1190
1191 async fn redock_with(state: &AppState, args: Vec<serde_json::Value>) -> WebReturnType {
1193 handle_redock_floating_pane(state, &call("RedockFloatingPane", args)).await
1194 }
1195
1196 #[tokio::test]
1197 async fn a_redock_missing_an_argument_is_refused_and_moves_nothing() {
1198 let state = test_state();
1199 let (ws_id, src_tab, src, dst_tab, _dst) = two_tabs(&state, &[false, false], &[false]).await;
1200 let pane = src[0].clone();
1201
1202 let ret = redock_with(&state, vec![pane.clone().into(), src_tab.clone().into(), ws_id.clone().into(), dst_tab.clone().into()]).await;
1203
1204 assert!(!ret.success);
1205 assert_eq!(ret.error.as_deref(), Some("missing argument at index 4"));
1206 assert_eq!(state.srv_state.lock().await.blocks[&pane].tab_id, src_tab, "the block is still in its tab");
1207 assert_eq!(is_chip(&state, &src_tab, &pane).await, Some(false), "and still in its tree");
1208 }
1209
1210 #[tokio::test]
1211 async fn a_redock_of_a_block_that_does_not_exist_is_an_error_and_leaves_the_tabs_alone() {
1212 let state = test_state();
1213 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[false], &[false]).await;
1214
1215 let ret = redock_dragged(&state, "no-such-block", &src_tab, &dst_tab, &ws_id, None).await;
1216
1217 assert!(!ret.success);
1218 assert!(ret.error.is_some());
1219 assert_eq!(is_chip(&state, &src_tab, &src[0]).await, Some(false), "the source pane is untouched");
1220 assert_eq!(is_chip(&state, &dst_tab, &dst[0]).await, Some(false), "the target pane is untouched");
1221 }
1222
1223 #[tokio::test]
1224 async fn a_drop_with_a_pane_and_a_direction_splits_beside_that_pane() {
1225 let state = test_state();
1226 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[false, false], &[false]).await;
1227 let pane = src[0].clone();
1228
1229 let ret = redock_with(
1230 &state,
1231 vec![pane.clone().into(), src_tab.clone().into(), ws_id.clone().into(), dst_tab.clone().into(), ws_id.clone().into(), dst[0].clone().into(), 1.into()],
1232 )
1233 .await;
1234 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1235
1236 assert_eq!(leaf_blocks(&state, &dst_tab).await, vec![dst[0].clone(), pane.clone()]);
1238 assert_eq!(is_chip(&state, &src_tab, &pane).await, None);
1239 assert_eq!(state.srv_state.lock().await.blocks[&pane].tab_id, dst_tab);
1240 }
1241
1242 #[tokio::test]
1243 async fn a_direction_that_is_not_a_number_is_ignored_and_the_block_is_inserted() {
1244 let state = test_state();
1245 let (ws_id, src_tab, src, dst_tab, dst) = two_tabs(&state, &[false, false], &[false]).await;
1246 let pane = src[0].clone();
1247
1248 let ret = redock_with(
1249 &state,
1250 vec![pane.clone().into(), src_tab.clone().into(), ws_id.clone().into(), dst_tab.clone().into(), ws_id.clone().into(), dst[0].clone().into(), "left".into()],
1251 )
1252 .await;
1253 assert!(ret.error.is_none(), "redock failed: {:?}", ret.error);
1254
1255 let mut docked = leaf_blocks(&state, &dst_tab).await;
1256 docked.sort();
1257 let mut expected = vec![dst[0].clone(), pane.clone()];
1258 expected.sort();
1259 assert_eq!(docked, expected, "both panes show in the target tab");
1260 }
1261
1262 #[tokio::test]
1263 async fn a_successful_redock_answers_with_where_the_block_went_and_updates_for_both_tabs() {
1264 let state = test_state();
1265 let (ws_id, src_tab, src, dst_tab, _dst) = two_tabs(&state, &[false, false], &[false]).await;
1266 let pane = src[0].clone();
1267
1268 let ret = redock_dragged(&state, &pane, &src_tab, &dst_tab, &ws_id, None).await;
1269
1270 assert!(ret.success);
1271 let data = ret.data.expect("the answer carries data");
1272 assert_eq!(data["redocked"], true);
1273 assert_eq!(data["block_id"], pane.as_str());
1274 assert_eq!(data["target_tab_id"], dst_tab.as_str());
1275 assert_eq!(data["target_workspace_id"], ws_id.as_str());
1276 assert_eq!(data["placed"], "fallback");
1277 let updates = ret.updates.expect("the answer carries updates");
1278 for tab in [&src_tab, &dst_tab] {
1279 assert!(updates.iter().any(|u| u.otype == OTYPE_TAB && &u.oid == tab), "a tab update for {tab}");
1280 assert!(updates.iter().any(|u| u.otype == OTYPE_LAYOUT), "layout updates come with them");
1281 }
1282 }
1283}