def test_should_have_action_execute_true_when_power_mode_is_active(mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), mock_agent) message_handler.process_message(clai_power_state()) action = message_handler.process_message(ANY_COMMAND_MESSAGE) assert action.origin_command == ANY_COMMAND_MESSAGE.command assert action.execute
def test_should_return_an_error_when_selected_is_empty(mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["nlc2cmd"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('') action = message_handler.process_message(select_agent) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == select_agent.command assert action.execute assert action.description == create_error_select('').description assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def test_should_return_the_list_of_plugins_with_default_selected_when_the_server_received_plugins_no_selected( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=NO_SELECTED, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(clai_plugins_state()) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == 'clai skills' assert action.execute assert action.description == expected_description(ALL_PLUGINS, NO_SELECTED.default)
def test_should_return_the_list_with_the_new_selected_values_if_exists_and_is_installed( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS_WITH_TAR_INSTALLED, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('nlc2cmd') action = message_handler.process_message(select_agent) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == select_agent.command assert action.execute assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def test_should_return_the_install_command_when_the_new_plugin_is_not_installed_yet( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["nlc2cmd"]), autospec=True) mocker.patch.object(ConfigStorage, 'store_config', return_value=None, autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) select_agent = clai_select_state('nlc2cmd') action = message_handler.process_message(select_agent) assert action.suggested_command == "$CLAI_PATH/fileExist.sh nlc2cmd" assert action.origin_command == select_agent.command assert message_handler.agent_datasource.get_current_plugin_name( select_agent.user_name) == ['nlc2cmd']
def test_should_return_the_list_without_any_selected_plugin_when_default_doesnt_exist( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mocker.patch.object(AgentDatasource, 'all_plugins', return_value=ALL_PLUGINS, autospec=True) mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig( default="", default_orchestrator="max_orchestrator"), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(clai_plugins_state()) assert action.suggested_command == NOOP_COMMAND assert action.origin_command == 'clai skills' assert action.execute assert action.description == expected_description(ALL_PLUGINS, '')
def test_should_not_change_power_variable_when_active_power_mode_and_it_already_disable( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), mock_agent) action = message_handler.process_message(clai_power_disabled_state()) assert action.suggested_command == NOOP_COMMAND assert action.description == "You have manual mode already enable, use clai auto to activate it" assert not message_handler.server_status_datasource.is_power()
class ClaiServer: # pylint: disable=too-many-arguments def __init__(self, server_status_datasource: ServerStatusDatasource = current_status_datasource, connector: ServerConnector = SocketServerConnector(current_status_datasource), remote_storage: ActionRemoteStorage = action_remote_storage, agent_datasource=AgentDatasource(), stats=stats_tracker ): self.connector = connector self.agent_datasource = agent_datasource self.server_status_datasource = server_status_datasource self.message_handler = MessageHandler(server_status_datasource, agent_datasource=agent_datasource) self.remote_storage = remote_storage self.stats_tracker = stats def init_server(self): self.message_handler.init_server() self.server_status_datasource.running = True self.remote_storage.start(self.agent_datasource) self.stats_tracker.start(self.agent_datasource) @staticmethod def serialize_message(data) -> State: StateDTO.update_forward_refs() dto = StateDTO(**json.loads(data)) return State( command_id=dto.command_id, user_name=dto.user_name, command=dto.command, root=dto.root, processes=dto.processes, file_changes=dto.file_changes, network=dto.network, result_code=dto.result_code, stderr=dto.stderr ) def create_socket(self, host, port): self.connector.create_socket(host, port) def listen_client_sockets(self): self.connector.loop(self.process_message) self.remote_storage.wait() self.stats_tracker.wait() def process_message(self, received_data: bytes) -> Action: message = self.serialize_message(received_data) return self.message_handler.process_message(message)
def test_should_desactive_the_power_mode_when_use_the_command_clai_power_disable( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), mock_agent) message_handler.server_status_datasource.set_power(True) action = message_handler.process_message(clai_power_disabled_state()) assert action.suggested_command == NOOP_COMMAND assert action.description == 'You have enable the manual mode' assert not message_handler.server_status_datasource.is_power()
def test_should_active_the_power_mode_when_use_the_command_clai_power(mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), mock_agent) action = message_handler.process_message(clai_power_state()) assert action.suggested_command == NOOP_COMMAND assert action.description == "You have enabled the auto mode" assert action.origin_command == 'clai auto' assert action.execute assert message_handler.server_status_datasource.is_power()
def test_should_return_valid_action_if_the_select_agent_return_none(mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) mock_agent.execute.return_value = None mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(command_state()) assert action.suggested_command is action.origin_command assert action.origin_command == command_state().command assert not action.execute assert not action.description
def test_should_return_the_suggestion_from_agent_ignoring_confidence_if_is_name_agent_command( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) action_to_execute = Action(suggested_command="command", confidence=0.0) mock_agent.execute.return_value = action_to_execute mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(COMMAND_NAME_AGENT_STATE) assert action.suggested_command == action_to_execute.suggested_command assert action.origin_command == command_state().command assert not action.execute assert not action.description
def test_should_return_empty_action_from_selected_agent_when_the_command_goes_to_the_agent_and_not_confidence( mocker): mock_agent = create_mock_agent() mocker.patch.object(AgentDatasource, 'get_instances', return_value=[mock_agent], autospec=True) action_to_execute = Action(suggested_command="command", confidence=0.1) mock_agent.execute.return_value = action_to_execute mocker.patch.object(ConfigStorage, 'read_config', return_value=PluginConfig(selected=["demo_agent"]), autospec=True) message_handler = MessageHandler(ServerStatusDatasource(), AgentDatasource()) action = message_handler.process_message(command_state()) assert action.suggested_command is action.origin_command assert action.origin_command == command_state().command assert not action.execute assert not action.description