diff --git a/libs/partners/robocorp/tests/unit_tests/_openapi2.fixture.json b/libs/partners/robocorp/tests/unit_tests/_openapi2.fixture.json new file mode 100644 index 0000000000000..7b8480442c074 --- /dev/null +++ b/libs/partners/robocorp/tests/unit_tests/_openapi2.fixture.json @@ -0,0 +1,387 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Robocorp Action Server", + "version": "0.1.0" + }, + "servers": [ + { + "url": "https://hosted-actions.onrender.com" + } + ], + "paths": { + "/api/actions/google-sheet-gmail/get-google-spreadsheet-schema/run": { + "post": { + "summary": "Get Google Spreadsheet Schema", + "description": "Action to get necessary information to be able to work with a Google Sheet Spreadsheets correctly.\nUse this action minimum once before anything else, to learn about the structure\nof the Spreadsheet. Method will return the first few rows of each Sheet as an example.", + "operationId": "get_google_spreadsheet_schema", + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": {}, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "type": "string", + "title": "Response Get Google Spreadsheet Schema", + "description": "Names of the sheets, and a couple of first rows from each sheet to explain the context." + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + }, + "security": [ + { + "HTTPBearer": [] + } + ] + } + }, + "/api/actions/google-sheet-gmail/create-new-google-sheet/run": { + "post": { + "summary": "Create New Google Sheet", + "description": "Creates a new empty Sheet in user's Google Spreadsheet.", + "operationId": "create_new_google_sheet", + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string", + "title": "Name", + "description": "Name of the Sheet. You must refer to this Sheet name later when adding or reading date from the Sheet." + } + }, + "type": "object", + "required": [ + "name" + ] + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "type": "string", + "title": "Response Create New Google Sheet", + "description": "True if operation was success, and False if it failed." + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + }, + "security": [ + { + "HTTPBearer": [] + } + ] + } + }, + "/api/actions/google-sheet-gmail/add-sheet-rows/run": { + "post": { + "summary": "Add Sheet Rows", + "description": "Action to add multiple rows to the Google sheet. Get the sheets with get_google_spreadsheet_schema if you don't know\nthe names or data structure. Make sure the values are in correct columns (needs to be ordered the same as in the sample).\nStrictly adhere to the schema.", + "operationId": "add_sheet_rows", + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "sheet": { + "type": "string", + "title": "Sheet", + "description": "Name of the sheet where the data is added to" + }, + "rows_to_add": { + "properties": { + "rows": { + "items": { + "properties": { + "columns": { + "items": { + "type": "string" + }, + "type": "array", + "title": "Columns", + "description": "The columns that make up the row" + } + }, + "type": "object", + "required": [ + "columns" + ], + "title": "Row" + }, + "type": "array", + "title": "Rows", + "description": "The rows that need to be added" + } + }, + "type": "object", + "required": [ + "rows" + ], + "title": "Rows To Add", + "description": "the rows to be added to the sheet" + } + }, + "type": "object", + "required": [ + "sheet", + "rows_to_add" + ] + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "type": "string", + "title": "Response Add Sheet Rows", + "description": "The result of the operation." + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + }, + "security": [ + { + "HTTPBearer": [] + } + ] + } + }, + "/api/actions/google-sheet-gmail/get-sheet-contents/run": { + "post": { + "summary": "Get Sheet Contents", + "description": "Get all content from the chosen Google Spreadsheet Sheet.", + "operationId": "get_sheet_contents", + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "sheet": { + "type": "string", + "title": "Sheet", + "description": "Name of the sheet from which to get the data" + } + }, + "type": "object", + "required": [ + "sheet" + ] + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "type": "string", + "title": "Response Get Sheet Contents", + "description": "Sheet data as string, rows separated by newlines" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + }, + "security": [ + { + "HTTPBearer": [] + } + ] + } + }, + "/api/actions/google-sheet-gmail/send-email-via-gmail/run": { + "post": { + "summary": "Send Email Via Gmail", + "description": "Sends an email using Gmail SMTP with an App Password for authentication.", + "operationId": "send_email_via_gmail", + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "subject": { + "type": "string", + "title": "Subject", + "description": "Email subject" + }, + "body": { + "type": "string", + "title": "Body", + "description": "Email body content" + }, + "recipient": { + "type": "string", + "title": "Recipient", + "description": "Recipient email address" + } + }, + "type": "object", + "required": [ + "subject", + "body", + "recipient" + ] + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "type": "string", + "title": "Response Send Email Via Gmail", + "description": "Information if the send was successfull or not" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + }, + "security": [ + { + "HTTPBearer": [] + } + ], + "x-openai-isConsequential": true + } + } + }, + "components": { + "securitySchemes": { + "HTTPBearer": { + "type": "http", + "scheme": "bearer" + } + }, + "schemas": { + "HTTPValidationError": { + "properties": { + "errors": { + "items": { + "$ref": "#/components/schemas/ValidationError" + }, + "type": "array", + "title": "Errors" + } + }, + "type": "object", + "title": "HTTPValidationError" + }, + "ValidationError": { + "properties": { + "loc": { + "items": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "integer" + } + ] + }, + "type": "array", + "title": "Location" + }, + "msg": { + "type": "string", + "title": "Message" + }, + "type": { + "type": "string", + "title": "Error Type" + } + }, + "type": "object", + "required": [ + "loc", + "msg", + "type" + ], + "title": "ValidationError" + } + } + } +} \ No newline at end of file diff --git a/libs/partners/robocorp/tests/unit_tests/test_common.py b/libs/partners/robocorp/tests/unit_tests/test_common.py index fe7479bcf2263..ac4e1da8c3214 100644 --- a/libs/partners/robocorp/tests/unit_tests/test_common.py +++ b/libs/partners/robocorp/tests/unit_tests/test_common.py @@ -22,3 +22,4 @@ def test_reduce_openapi_spec(self) -> None: self.assertEqual(len(output.endpoints), 8) self.assertEqual(output.endpoints[0][1], openapi_endpoint_doc_mock) + diff --git a/libs/partners/robocorp/tests/unit_tests/test_toolkits.py b/libs/partners/robocorp/tests/unit_tests/test_toolkits.py index f3efec4289c1f..9be1c0dd37489 100644 --- a/libs/partners/robocorp/tests/unit_tests/test_toolkits.py +++ b/libs/partners/robocorp/tests/unit_tests/test_toolkits.py @@ -1,9 +1,41 @@ """Test toolkit integration.""" +from pathlib import Path +from unittest.mock import patch, MagicMock + from langchain_robocorp.toolkits import ActionServerToolkit from ._fixtures import FakeChatLLMT - +import json def test_initialization() -> None: """Test toolkit initialization.""" ActionServerToolkit(url="http://localhost", llm=FakeChatLLMT()) + + +def test_get_tools_success() -> None: + # Setup + toolkit_instance = ActionServerToolkit(url="http://example.com", api_key="dummy_key") + + fixture_path = Path(__file__).with_name("_openapi2.fixture.json") + + with patch("langchain_robocorp.toolkits.requests.get") as mocked_get, fixture_path.open("r") as f: + data = json.load(f) # Using json.load directly on the file object + mocked_response = MagicMock() + mocked_response.json.return_value = data + mocked_response.status_code = 200 + mocked_response.headers = {'Content-Type': 'application/json'} + mocked_get.return_value = mocked_response + + # Execute + tools = toolkit_instance.get_tools() + + # Verify + assert len(tools) == 5 + + assert tools[2].name == 'robocorp_action_server_add_sheet_rows' + assert tools[2].description == '''Action to add multiple rows to the Google sheet. Get the sheets with get_google_spreadsheet_schema if you don't know +the names or data structure. Make sure the values are in correct columns (needs to be ordered the same as in the sample). +Strictly adhere to the schema.. The tool must be invoked with a complete sentence starting with "Add Sheet Rows" and additional information on Name of the sheet where the data is added to, the rows to be added to the sheet.''' + rows_to_add = tools[2].args['rows_to_add'] + print(repr(rows_to_add)) + 1/0