magi-code 0.77.1

Repository-aware CLI coding agent for terminal work
Documentation
use super::super::*;
use super::support::*;

#[test]
fn alt_c_cancels_startup_prompt_from_non_prompt_focus() {
    let mut state = state_with_activity();
    state.focus_activity_tree();
    state.startup_prompt_queued = true;

    assert_eq!(
        handle_key(
            modified_key(KeyCode::Char('c'), KeyModifiers::ALT),
            &mut state,
        ),
        InputAction::CancelStartupPrompt
    );
}

#[test]
fn alt_c_cancels_active_prompt_before_modal_handling() {
    let mut state = state_with_activity();
    state.focus_activity_tree();
    state.start_running_prompt("running prompt".to_string());
    state.open_system_prompt_modal("modal content".to_string());

    assert_eq!(
        handle_key(
            modified_key(KeyCode::Char('c'), KeyModifiers::ALT),
            &mut state,
        ),
        InputAction::CancelRunningPrompt
    );
    assert!(state.system_prompt_modal_visible());
}

#[test]
fn alt_c_cancels_queued_startup_prompt_before_modal_handling() {
    let mut state = state_with_activity();
    state.focus_activity_tree();
    state.startup_prompt_queued = true;
    state.open_system_prompt_modal("modal content".to_string());

    assert_eq!(
        handle_key(
            modified_key(KeyCode::Char('c'), KeyModifiers::ALT),
            &mut state,
        ),
        InputAction::CancelStartupPrompt
    );
    assert!(state.system_prompt_modal_visible());
}

#[test]
fn alt_c_keeps_modal_handling_and_does_not_collapse_activity_without_cancellation() {
    let mut state = state_with_parent_and_child();
    let parent = ActivityId::new("parent");
    state.expanded.insert(parent.clone());
    state.open_system_prompt_modal("modal content".to_string());

    assert_eq!(
        handle_key(
            modified_key(KeyCode::Char('c'), KeyModifiers::ALT),
            &mut state,
        ),
        InputAction::None
    );
    assert!(state.system_prompt_modal_visible());
    assert!(state.expanded.contains(&parent));
}

#[test]
fn alt_c_cancels_running_prompt_without_editing_prompt() {
    let mut state = MissionControlState {
        focus_pane: crate::tui::state::TuiFocusPane::Prompt,
        prompt_editor: crate::tui::state::PromptEditor::new("draft", 5),
        ..Default::default()
    };
    state.start_running_prompt("active".to_string());

    assert!(matches!(
        handle_key(
            modified_key(KeyCode::Char('c'), KeyModifiers::ALT),
            &mut state
        ),
        InputAction::CancelRunningPrompt
    ));
    assert_eq!(state.prompt_plain_text(), "draft");
    assert_eq!(state.prompt_cursor(), 5);
}

#[test]
fn alt_c_running_prompt_takes_priority_over_activity_collapse() {
    let mut state = state_with_parent_and_child();
    state.start_running_prompt("active".to_string());
    assert_eq!(state.visible_nodes().len(), 2);

    assert!(matches!(
        handle_key(
            modified_key(KeyCode::Char('c'), KeyModifiers::ALT),
            &mut state
        ),
        InputAction::CancelRunningPrompt
    ));
    assert_eq!(state.visible_nodes().len(), 2);
}

#[test]
fn alt_c_idle_prompt_focus_returns_startup_cancel_intent_without_mutating_prompt() {
    let mut state = MissionControlState {
        focus_pane: crate::tui::state::TuiFocusPane::Prompt,
        prompt_editor: crate::tui::state::PromptEditor::new("draft", 5),
        startup_prompt_queued: true,
        ..Default::default()
    };

    assert!(matches!(
        handle_key(
            modified_key(KeyCode::Char('c'), KeyModifiers::ALT),
            &mut state
        ),
        InputAction::CancelStartupPrompt
    ));
    assert_eq!(state.prompt_plain_text(), "draft");
    assert_eq!(state.prompt_cursor(), 5);
    assert!(state.transcript.is_empty());
}

#[test]
fn alt_c_release_does_not_cancel_running_prompt() {
    let mut state = MissionControlState::default();
    state.start_running_prompt("active".to_string());
    let key = KeyEvent::new_with_kind(
        KeyCode::Char('c'),
        KeyModifiers::ALT,
        crossterm::event::KeyEventKind::Release,
    );

    assert!(matches!(handle_key(key, &mut state), InputAction::None));
}