|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +from deepset_mcp.api.pipeline.models import ( |
| 4 | + PipelineLog, |
| 5 | + PipelineLogList, |
| 6 | + PipelineValidationResult, |
| 7 | + ValidationError, |
| 8 | +) |
| 9 | +from deepset_mcp.tools.formatting_utils import ( |
| 10 | + pipeline_logs_to_llm_readable_string, |
| 11 | + validation_result_to_llm_readable_string, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def test_pipeline_logs_to_llm_readable_string_with_logs() -> None: |
| 16 | + """Test formatting pipeline logs with data.""" |
| 17 | + log1 = PipelineLog( |
| 18 | + log_id="log1", |
| 19 | + message="Pipeline started successfully", |
| 20 | + logged_at=datetime(2023, 1, 1, 12, 0, 0), |
| 21 | + level="info", |
| 22 | + origin="querypipeline", |
| 23 | + exceptions=None, |
| 24 | + extra_fields={}, |
| 25 | + ) |
| 26 | + log2 = PipelineLog( |
| 27 | + log_id="log2", |
| 28 | + message="Error processing document", |
| 29 | + logged_at=datetime(2023, 1, 1, 12, 1, 30), |
| 30 | + level="error", |
| 31 | + origin="querypipeline", |
| 32 | + exceptions="ValueError: Invalid document format", |
| 33 | + extra_fields={"component": "document_reader", "file_name": "test.pdf"}, |
| 34 | + ) |
| 35 | + |
| 36 | + logs = PipelineLogList(data=[log1, log2], has_more=True, total=5) |
| 37 | + |
| 38 | + result = pipeline_logs_to_llm_readable_string(logs, "test-pipeline") |
| 39 | + |
| 40 | + # Check basic structure |
| 41 | + assert "### Logs for Pipeline 'test-pipeline'" in result |
| 42 | + assert "**Total Logs:** 5" in result |
| 43 | + assert "**Showing:** 2 entries" in result |
| 44 | + assert "**Has More:** Yes" in result |
| 45 | + |
| 46 | + # Check log entry 1 |
| 47 | + assert "**Log Entry 1**" in result |
| 48 | + assert "**Timestamp:** January 01, 2023 12:00:00 PM" in result |
| 49 | + assert "**Level:** info" in result |
| 50 | + assert "**Origin:** querypipeline" in result |
| 51 | + assert "**Message:** Pipeline started successfully" in result |
| 52 | + |
| 53 | + # Check log entry 2 |
| 54 | + assert "**Log Entry 2**" in result |
| 55 | + assert "**Timestamp:** January 01, 2023 12:01:30 PM" in result |
| 56 | + assert "**Level:** error" in result |
| 57 | + assert "**Message:** Error processing document" in result |
| 58 | + assert "**Exceptions:** ValueError: Invalid document format" in result |
| 59 | + assert "component: document_reader" in result |
| 60 | + assert "file_name: test.pdf" in result |
| 61 | + |
| 62 | + # Check pagination note |
| 63 | + assert "*Note: There are more log entries available. Adjust the limit parameter to see more.*" in result |
| 64 | + |
| 65 | + |
| 66 | +def test_pipeline_logs_to_llm_readable_string_empty() -> None: |
| 67 | + """Test formatting empty pipeline logs.""" |
| 68 | + logs = PipelineLogList(data=[], has_more=False, total=0) |
| 69 | + |
| 70 | + result = pipeline_logs_to_llm_readable_string(logs, "empty-pipeline") |
| 71 | + |
| 72 | + assert result == "No logs found for pipeline 'empty-pipeline'." |
| 73 | + |
| 74 | + |
| 75 | +def test_pipeline_logs_to_llm_readable_string_empty_with_filter() -> None: |
| 76 | + """Test formatting empty pipeline logs with level filter.""" |
| 77 | + logs = PipelineLogList(data=[], has_more=False, total=0) |
| 78 | + |
| 79 | + result = pipeline_logs_to_llm_readable_string(logs, "test-pipeline", level="error") |
| 80 | + |
| 81 | + assert result == "No logs found for pipeline 'test-pipeline' (filtered by level: error)." |
| 82 | + |
| 83 | + |
| 84 | +def test_pipeline_logs_to_llm_readable_string_with_level_filter() -> None: |
| 85 | + """Test formatting pipeline logs with level filter applied.""" |
| 86 | + log = PipelineLog( |
| 87 | + log_id="log1", |
| 88 | + message="Critical error", |
| 89 | + logged_at=datetime(2023, 1, 1, 12, 0, 0), |
| 90 | + level="error", |
| 91 | + origin="querypipeline", |
| 92 | + exceptions=None, |
| 93 | + extra_fields={}, |
| 94 | + ) |
| 95 | + |
| 96 | + logs = PipelineLogList(data=[log], has_more=False, total=1) |
| 97 | + |
| 98 | + result = pipeline_logs_to_llm_readable_string(logs, "test-pipeline", level="error") |
| 99 | + |
| 100 | + assert "### Logs for Pipeline 'test-pipeline'" in result |
| 101 | + assert "**Filter Applied:** Level = error" in result |
| 102 | + assert "**Total Logs:** 1" in result |
| 103 | + assert "**Showing:** 1 entries" in result |
| 104 | + assert "**Has More:** No" in result |
| 105 | + assert "**Message:** Critical error" in result |
| 106 | + |
| 107 | + |
| 108 | +def test_pipeline_logs_to_llm_readable_string_no_pagination_note() -> None: |
| 109 | + """Test that pagination note is not shown when has_more is False.""" |
| 110 | + log = PipelineLog( |
| 111 | + log_id="log1", |
| 112 | + message="Test message", |
| 113 | + logged_at=datetime(2023, 1, 1, 12, 0, 0), |
| 114 | + level="info", |
| 115 | + origin="querypipeline", |
| 116 | + exceptions=None, |
| 117 | + extra_fields={}, |
| 118 | + ) |
| 119 | + |
| 120 | + logs = PipelineLogList(data=[log], has_more=False, total=1) |
| 121 | + |
| 122 | + result = pipeline_logs_to_llm_readable_string(logs, "test-pipeline") |
| 123 | + |
| 124 | + assert "*Note: There are more log entries available" not in result |
| 125 | + |
| 126 | + |
| 127 | +def test_validation_result_to_llm_readable_string_valid() -> None: |
| 128 | + """Test formatting valid validation result.""" |
| 129 | + result = PipelineValidationResult(valid=True, errors=[]) |
| 130 | + |
| 131 | + formatted = validation_result_to_llm_readable_string(result) |
| 132 | + |
| 133 | + assert "The provided pipeline configuration is valid." in formatted |
| 134 | + assert "Validation Errors" not in formatted |
| 135 | + |
| 136 | + |
| 137 | +def test_validation_result_to_llm_readable_string_invalid() -> None: |
| 138 | + """Test formatting invalid validation result.""" |
| 139 | + errors = [ |
| 140 | + ValidationError(code="YAML_ERROR", message="Syntax error in YAML"), |
| 141 | + ValidationError(code="COMPONENT_ERROR", message="Unknown component type"), |
| 142 | + ] |
| 143 | + result = PipelineValidationResult(valid=False, errors=errors) |
| 144 | + |
| 145 | + formatted = validation_result_to_llm_readable_string(result) |
| 146 | + |
| 147 | + assert "The provided pipeline configuration is invalid." in formatted |
| 148 | + assert "**Validation Errors**" in formatted |
| 149 | + assert "Error 1" in formatted |
| 150 | + assert "- Code: YAML_ERROR" in formatted |
| 151 | + assert "- Message: Syntax error in YAML" in formatted |
| 152 | + assert "Error 2" in formatted |
| 153 | + assert "- Code: COMPONENT_ERROR" in formatted |
| 154 | + assert "- Message: Unknown component type" in formatted |
0 commit comments