async def test_run_session_with_name_attempts_state_restore(
    mock_display_initial_user_prompt,
    mock_initialize_repo,
    mock_display_init_anim,
    mock_display_runtime_init,
    mock_agent_controller_init,
    mock_restore_from_session,
    mock_handle_commands,
    mock_read_prompt_input,
    mock_cleanup_session,
    mock_run_agent_until_done,
    mock_add_mcp_tools,
    mock_create_memory,
    mock_create_runtime,
    mock_create_agent,
    mock_generate_sid,
    mock_config,  # Fixture
    mock_settings_store,  # Fixture
):
    """Test run_session with a session_name attempts to restore state and passes it to AgentController."""
    loop = asyncio.get_running_loop()
    test_session_name = 'my_restore_test_session'
    expected_sid = f'sid_for_{test_session_name}'

    mock_generate_sid.return_value = expected_sid

    mock_agent = AsyncMock()
    mock_create_agent.return_value = mock_agent

    mock_runtime = AsyncMock()
    mock_runtime.event_stream = MagicMock()  # This is the EventStream instance
    mock_runtime.event_stream.sid = expected_sid
    mock_runtime.event_stream.file_store = (
        MagicMock()
    )  # Mock the file_store attribute on the EventStream
    mock_create_runtime.return_value = mock_runtime

    # This is what State.restore_from_session will return
    mock_loaded_state = MagicMock(spec=State)
    mock_restore_from_session.return_value = mock_loaded_state

    # AgentController.__init__ should not return a value (it's __init__)
    mock_agent_controller_init.return_value = None

    # To make run_session exit cleanly after one loop
    mock_read_prompt_input.return_value = '/exit'
    mock_handle_commands.return_value = (
        True,
        False,
        False,
    )  # close_repl, reload_microagents, new_session_requested

    # Mock other functions called by run_session to avoid side effects
    mock_initialize_repo.return_value = '/mocked/repo/dir'
    mock_create_memory.return_value = AsyncMock()  # Memory instance

    await cli.run_session(
        loop,
        mock_config,
        mock_settings_store,  # This is FileSettingsStore, not directly used for restore in this path
        '/test/dir',
        task_content=None,
        session_name=test_session_name,
    )

    mock_generate_sid.assert_called_once_with(mock_config, test_session_name)

    # State.restore_from_session is called from within core.setup.create_controller,
    # which receives the runtime object (and thus its event_stream with sid and file_store).
    mock_restore_from_session.assert_called_once_with(
        expected_sid, mock_runtime.event_stream.file_store
    )

    # Check that AgentController was initialized with the loaded state
    mock_agent_controller_init.assert_called_once()
    args, kwargs = mock_agent_controller_init.call_args
    assert kwargs.get('initial_state') == mock_loaded_state
Test run_session with a session_name attempts to restore state and passes it to AgentController.
tests/unit/test_cli.py