File size: 29,417 Bytes
6b86af2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | # -*- coding: utf-8 -*-
"""
Unit tests for network error classification system.
Tests classify_network_error(), format_error_for_user(), and get_short_error_message().
"""
import socket
import pytest
import httpx
from kiro.network_errors import (
ErrorCategory,
NetworkErrorInfo,
classify_network_error,
format_error_for_user,
get_short_error_message
)
class TestClassifyNetworkErrorDNS:
"""Tests for DNS resolution error classification."""
def test_dns_error_with_socket_gaierror_windows(self):
"""
What it does: Verifies DNS errors are classified correctly on Windows.
Purpose: Ensure socket.gaierror with errno 11001 is detected as DNS_RESOLUTION (issue #53).
"""
print("Setup: Creating ConnectError with socket.gaierror (Windows errno 11001)...")
dns_error = socket.gaierror(11001, "getaddrinfo failed")
connect_error = httpx.ConnectError("All connection attempts failed")
connect_error.__cause__ = dns_error
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is DNS_RESOLUTION...")
print(f"Comparing category: Expected {ErrorCategory.DNS_RESOLUTION}, Got {error_info.category}")
assert error_info.category == ErrorCategory.DNS_RESOLUTION
assert "DNS resolution failed" in error_info.user_message
assert "cannot resolve" in error_info.user_message.lower()
assert error_info.is_retryable is True
assert error_info.suggested_http_code == 502
assert "11001" in error_info.technical_details
def test_dns_error_with_socket_gaierror_unix(self):
"""
What it does: Verifies DNS errors are classified correctly on Unix.
Purpose: Ensure socket.gaierror with Unix errno is detected as DNS_RESOLUTION.
"""
print("Setup: Creating ConnectError with socket.gaierror (Unix errno -2)...")
dns_error = socket.gaierror(-2, "Name or service not known")
connect_error = httpx.ConnectError("Connection failed")
connect_error.__cause__ = dns_error
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is DNS_RESOLUTION...")
assert error_info.category == ErrorCategory.DNS_RESOLUTION
assert "DNS" in error_info.user_message
assert "-2" in error_info.technical_details
def test_dns_error_includes_troubleshooting_steps(self):
"""
What it does: Verifies DNS errors include actionable troubleshooting steps.
Purpose: Ensure users get clear guidance on fixing DNS issues.
"""
print("Setup: Creating DNS error...")
dns_error = socket.gaierror(11001, "getaddrinfo failed")
connect_error = httpx.ConnectError("Connection failed")
connect_error.__cause__ = dns_error
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Troubleshooting steps present...")
steps = error_info.troubleshooting_steps
assert len(steps) >= 3
assert any("DNS" in step for step in steps)
assert any("8.8.8.8" in step or "1.1.1.1" in step for step in steps)
assert any("VPN" in step for step in steps)
assert any("firewall" in step.lower() or "antivirus" in step.lower() for step in steps)
def test_dns_error_technical_details_include_errno(self):
"""
What it does: Verifies technical details include errno for debugging.
Purpose: Ensure developers can identify specific DNS error codes.
"""
print("Setup: Creating DNS error with specific errno...")
dns_error = socket.gaierror(11001, "getaddrinfo failed")
connect_error = httpx.ConnectError("Failed")
connect_error.__cause__ = dns_error
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Technical details include errno...")
assert "errno" in error_info.technical_details.lower()
assert "11001" in error_info.technical_details
class TestClassifyNetworkErrorConnection:
"""Tests for connection error classification."""
def test_connection_refused_error(self):
"""
What it does: Verifies connection refused errors are classified correctly.
Purpose: Ensure "Connection refused" is detected as CONNECTION_REFUSED.
"""
print("Setup: Creating ConnectError with 'Connection refused'...")
connect_error = httpx.ConnectError("Connection refused")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is CONNECTION_REFUSED...")
print(f"Comparing category: Expected {ErrorCategory.CONNECTION_REFUSED}, Got {error_info.category}")
assert error_info.category == ErrorCategory.CONNECTION_REFUSED
assert "Connection refused" in error_info.user_message
assert "not accepting connections" in error_info.user_message
assert error_info.is_retryable is True
assert error_info.suggested_http_code == 502
def test_connection_refused_with_econnrefused(self):
"""
What it does: Verifies ECONNREFUSED is detected as CONNECTION_REFUSED.
Purpose: Ensure Unix-style error codes are recognized.
"""
print("Setup: Creating ConnectError with ECONNREFUSED...")
connect_error = httpx.ConnectError("[Errno 111] ECONNREFUSED")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is CONNECTION_REFUSED...")
assert error_info.category == ErrorCategory.CONNECTION_REFUSED
def test_connection_reset_error(self):
"""
What it does: Verifies connection reset errors are classified correctly.
Purpose: Ensure "Connection reset" is detected as CONNECTION_RESET.
"""
print("Setup: Creating ConnectError with 'Connection reset'...")
connect_error = httpx.ConnectError("Connection reset by peer")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is CONNECTION_RESET...")
print(f"Comparing category: Expected {ErrorCategory.CONNECTION_RESET}, Got {error_info.category}")
assert error_info.category == ErrorCategory.CONNECTION_RESET
assert "Connection reset" in error_info.user_message
assert "closed the connection" in error_info.user_message
assert error_info.is_retryable is True
def test_connection_reset_with_econnreset(self):
"""
What it does: Verifies ECONNRESET is detected as CONNECTION_RESET.
Purpose: Ensure Unix-style error codes are recognized.
"""
print("Setup: Creating ConnectError with ECONNRESET...")
connect_error = httpx.ConnectError("[Errno 104] ECONNRESET")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is CONNECTION_RESET...")
assert error_info.category == ErrorCategory.CONNECTION_RESET
def test_network_unreachable_error(self):
"""
What it does: Verifies network unreachable errors are classified correctly.
Purpose: Ensure "Network is unreachable" is detected as NETWORK_UNREACHABLE.
"""
print("Setup: Creating ConnectError with 'Network is unreachable'...")
connect_error = httpx.ConnectError("Network is unreachable")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is NETWORK_UNREACHABLE...")
print(f"Comparing category: Expected {ErrorCategory.NETWORK_UNREACHABLE}, Got {error_info.category}")
assert error_info.category == ErrorCategory.NETWORK_UNREACHABLE
assert "Network unreachable" in error_info.user_message
assert error_info.is_retryable is True
def test_network_unreachable_with_no_route_to_host(self):
"""
What it does: Verifies "No route to host" is detected as NETWORK_UNREACHABLE.
Purpose: Ensure alternative error messages are recognized.
"""
print("Setup: Creating ConnectError with 'No route to host'...")
connect_error = httpx.ConnectError("No route to host")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is NETWORK_UNREACHABLE...")
assert error_info.category == ErrorCategory.NETWORK_UNREACHABLE
def test_network_unreachable_with_enetunreach(self):
"""
What it does: Verifies ENETUNREACH is detected as NETWORK_UNREACHABLE.
Purpose: Ensure Unix-style error codes are recognized.
"""
print("Setup: Creating ConnectError with ENETUNREACH...")
connect_error = httpx.ConnectError("[Errno 101] ENETUNREACH")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is NETWORK_UNREACHABLE...")
assert error_info.category == ErrorCategory.NETWORK_UNREACHABLE
def test_generic_connect_error_classified_as_unknown(self):
"""
What it does: Verifies generic connection errors fall back to UNKNOWN.
Purpose: Ensure unrecognized errors have a fallback category.
"""
print("Setup: Creating generic ConnectError...")
connect_error = httpx.ConnectError("All connection attempts failed")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is UNKNOWN...")
print(f"Comparing category: Expected {ErrorCategory.UNKNOWN}, Got {error_info.category}")
assert error_info.category == ErrorCategory.UNKNOWN
assert "Connection failed" in error_info.user_message
assert error_info.is_retryable is True
assert error_info.suggested_http_code == 502
class TestClassifyNetworkErrorTimeout:
"""Tests for timeout error classification."""
def test_connect_timeout_error(self):
"""
What it does: Verifies ConnectTimeout is classified correctly.
Purpose: Ensure TCP handshake timeouts are detected as TIMEOUT_CONNECT.
"""
print("Setup: Creating ConnectTimeout...")
timeout_error = httpx.ConnectTimeout("Connection timeout")
print("Action: Classifying error...")
error_info = classify_network_error(timeout_error)
print("Verification: Category is TIMEOUT_CONNECT...")
print(f"Comparing category: Expected {ErrorCategory.TIMEOUT_CONNECT}, Got {error_info.category}")
assert error_info.category == ErrorCategory.TIMEOUT_CONNECT
assert "Connection timeout" in error_info.user_message
assert "did not respond" in error_info.user_message
assert error_info.is_retryable is True
assert error_info.suggested_http_code == 504
def test_connect_timeout_includes_troubleshooting(self):
"""
What it does: Verifies connect timeout includes troubleshooting steps.
Purpose: Ensure users get guidance on fixing timeout issues.
"""
print("Setup: Creating ConnectTimeout...")
timeout_error = httpx.ConnectTimeout("Timeout")
print("Action: Classifying error...")
error_info = classify_network_error(timeout_error)
print("Verification: Troubleshooting steps present...")
steps = error_info.troubleshooting_steps
assert len(steps) >= 2
assert any("internet connection" in step.lower() for step in steps)
assert any("server" in step.lower() or "overloaded" in step.lower() for step in steps)
def test_read_timeout_error(self):
"""
What it does: Verifies ReadTimeout is classified correctly.
Purpose: Ensure read timeouts are detected as TIMEOUT_READ.
"""
print("Setup: Creating ReadTimeout...")
timeout_error = httpx.ReadTimeout("Read timeout")
print("Action: Classifying error...")
error_info = classify_network_error(timeout_error)
print("Verification: Category is TIMEOUT_READ...")
print(f"Comparing category: Expected {ErrorCategory.TIMEOUT_READ}, Got {error_info.category}")
assert error_info.category == ErrorCategory.TIMEOUT_READ
assert "Read timeout" in error_info.user_message
assert "stopped responding" in error_info.user_message
assert error_info.is_retryable is True
assert error_info.suggested_http_code == 504
def test_read_timeout_includes_troubleshooting(self):
"""
What it does: Verifies read timeout includes troubleshooting steps.
Purpose: Ensure users get guidance on fixing read timeout issues.
"""
print("Setup: Creating ReadTimeout...")
timeout_error = httpx.ReadTimeout("Timeout")
print("Action: Classifying error...")
error_info = classify_network_error(timeout_error)
print("Verification: Troubleshooting steps present...")
steps = error_info.troubleshooting_steps
assert len(steps) >= 2
assert any("server" in step.lower() or "processing" in step.lower() for step in steps)
def test_generic_timeout_error(self):
"""
What it does: Verifies generic TimeoutException is classified as TIMEOUT_READ.
Purpose: Ensure unspecified timeouts have a fallback.
"""
print("Setup: Creating generic TimeoutException...")
timeout_error = httpx.TimeoutException("Timeout")
print("Action: Classifying error...")
error_info = classify_network_error(timeout_error)
print("Verification: Category is TIMEOUT_READ...")
print(f"Comparing category: Expected {ErrorCategory.TIMEOUT_READ}, Got {error_info.category}")
assert error_info.category == ErrorCategory.TIMEOUT_READ
assert "timeout" in error_info.user_message.lower()
assert error_info.is_retryable is True
class TestClassifyNetworkErrorSSL:
"""Tests for SSL/TLS error classification."""
def test_ssl_error_detection(self):
"""
What it does: Verifies SSL errors are classified correctly.
Purpose: Ensure SSL/TLS errors are detected as SSL_ERROR.
"""
print("Setup: Creating ConnectError with SSL in message...")
connect_error = httpx.ConnectError("SSL handshake failed")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is SSL_ERROR...")
print(f"Comparing category: Expected {ErrorCategory.SSL_ERROR}, Got {error_info.category}")
assert error_info.category == ErrorCategory.SSL_ERROR
assert "SSL/TLS error" in error_info.user_message
assert "secure connection" in error_info.user_message
assert error_info.is_retryable is False
assert error_info.suggested_http_code == 502
def test_tls_error_detection(self):
"""
What it does: Verifies TLS errors are detected as SSL_ERROR.
Purpose: Ensure TLS keyword is recognized.
"""
print("Setup: Creating ConnectError with TLS in message...")
connect_error = httpx.ConnectError("TLS connection failed")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is SSL_ERROR...")
assert error_info.category == ErrorCategory.SSL_ERROR
def test_certificate_error_detection(self):
"""
What it does: Verifies certificate errors are detected as SSL_ERROR.
Purpose: Ensure certificate keyword is recognized.
"""
print("Setup: Creating ConnectError with certificate in message...")
connect_error = httpx.ConnectError("Certificate verification failed")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Category is SSL_ERROR...")
assert error_info.category == ErrorCategory.SSL_ERROR
def test_ssl_error_includes_troubleshooting(self):
"""
What it does: Verifies SSL errors include troubleshooting steps.
Purpose: Ensure users get guidance on fixing SSL issues.
"""
print("Setup: Creating SSL error...")
connect_error = httpx.ConnectError("SSL error")
print("Action: Classifying error...")
error_info = classify_network_error(connect_error)
print("Verification: Troubleshooting steps present...")
steps = error_info.troubleshooting_steps
assert len(steps) >= 2
assert any("certificate" in step.lower() for step in steps)
assert any("date" in step.lower() or "time" in step.lower() for step in steps)
class TestClassifyNetworkErrorProxy:
"""Tests for proxy error classification."""
def test_proxy_error_detection(self):
"""
What it does: Verifies proxy errors are classified correctly.
Purpose: Ensure ProxyError is detected as PROXY_ERROR.
"""
print("Setup: Creating ProxyError...")
proxy_error = httpx.ProxyError("Proxy connection failed")
print("Action: Classifying error...")
error_info = classify_network_error(proxy_error)
print("Verification: Category is PROXY_ERROR...")
print(f"Comparing category: Expected {ErrorCategory.PROXY_ERROR}, Got {error_info.category}")
assert error_info.category == ErrorCategory.PROXY_ERROR
assert "Proxy" in error_info.user_message
assert "cannot connect through" in error_info.user_message
assert error_info.is_retryable is True
assert error_info.suggested_http_code == 502
def test_proxy_error_includes_troubleshooting(self):
"""
What it does: Verifies proxy errors include troubleshooting steps.
Purpose: Ensure users get guidance on fixing proxy issues.
"""
print("Setup: Creating ProxyError...")
proxy_error = httpx.ProxyError("Proxy failed")
print("Action: Classifying error...")
error_info = classify_network_error(proxy_error)
print("Verification: Troubleshooting steps present...")
steps = error_info.troubleshooting_steps
assert len(steps) >= 2
assert any("HTTP_PROXY" in step or "HTTPS_PROXY" in step for step in steps)
assert any("proxy" in step.lower() for step in steps)
class TestClassifyNetworkErrorRedirects:
"""Tests for redirect error classification."""
def test_too_many_redirects_error(self):
"""
What it does: Verifies TooManyRedirects is classified correctly.
Purpose: Ensure redirect loops are detected as TOO_MANY_REDIRECTS.
"""
print("Setup: Creating TooManyRedirects...")
redirect_error = httpx.TooManyRedirects("Too many redirects")
print("Action: Classifying error...")
error_info = classify_network_error(redirect_error)
print("Verification: Category is TOO_MANY_REDIRECTS...")
print(f"Comparing category: Expected {ErrorCategory.TOO_MANY_REDIRECTS}, Got {error_info.category}")
assert error_info.category == ErrorCategory.TOO_MANY_REDIRECTS
assert "redirect" in error_info.user_message.lower()
assert "loop" in error_info.user_message.lower()
assert error_info.is_retryable is False
assert error_info.suggested_http_code == 502
class TestClassifyNetworkErrorGeneric:
"""Tests for generic error classification."""
def test_generic_request_error_classified_as_unknown(self):
"""
What it does: Verifies generic RequestError falls back to UNKNOWN.
Purpose: Ensure unrecognized httpx errors have a fallback.
"""
print("Setup: Creating generic RequestError...")
request_error = httpx.RequestError("Unknown network error")
print("Action: Classifying error...")
error_info = classify_network_error(request_error)
print("Verification: Category is UNKNOWN...")
print(f"Comparing category: Expected {ErrorCategory.UNKNOWN}, Got {error_info.category}")
assert error_info.category == ErrorCategory.UNKNOWN
assert "unexpected error" in error_info.user_message.lower()
assert error_info.is_retryable is True
assert error_info.suggested_http_code == 502
def test_non_httpx_error_classified_as_unknown(self):
"""
What it does: Verifies non-httpx errors fall back to UNKNOWN.
Purpose: Ensure graceful handling of unexpected exception types.
"""
print("Setup: Creating generic Exception...")
generic_error = Exception("Something went wrong")
print("Action: Classifying error...")
error_info = classify_network_error(generic_error)
print("Verification: Category is UNKNOWN...")
print(f"Comparing category: Expected {ErrorCategory.UNKNOWN}, Got {error_info.category}")
assert error_info.category == ErrorCategory.UNKNOWN
assert error_info.suggested_http_code == 500
class TestFormatErrorForUser:
"""Tests for format_error_for_user() function."""
def test_format_openai_includes_troubleshooting(self):
"""
What it does: Verifies OpenAI format includes troubleshooting steps.
Purpose: Ensure users get actionable guidance in API responses.
"""
print("Setup: Creating NetworkErrorInfo...")
error_info = NetworkErrorInfo(
category=ErrorCategory.DNS_RESOLUTION,
user_message="DNS failed",
troubleshooting_steps=["Step 1", "Step 2"],
technical_details="Technical info",
is_retryable=True,
suggested_http_code=502
)
print("Action: Formatting for OpenAI...")
formatted = format_error_for_user(error_info, format_type="openai", include_troubleshooting=True)
print("Verification: OpenAI format structure...")
assert "error" in formatted
assert "message" in formatted["error"]
assert "type" in formatted["error"]
assert "code" in formatted["error"]
assert formatted["error"]["type"] == "connectivity_error"
assert formatted["error"]["code"] == "dns_resolution"
assert "Step 1" in formatted["error"]["message"]
assert "Step 2" in formatted["error"]["message"]
def test_format_openai_without_troubleshooting(self):
"""
What it does: Verifies OpenAI format can exclude troubleshooting.
Purpose: Ensure flexibility in error message verbosity.
"""
print("Setup: Creating NetworkErrorInfo...")
error_info = NetworkErrorInfo(
category=ErrorCategory.TIMEOUT_CONNECT,
user_message="Connection timeout",
troubleshooting_steps=["Step 1"],
technical_details="Technical info",
is_retryable=True,
suggested_http_code=504
)
print("Action: Formatting for OpenAI without troubleshooting...")
formatted = format_error_for_user(error_info, format_type="openai", include_troubleshooting=False)
print("Verification: No troubleshooting steps in message...")
assert "Step 1" not in formatted["error"]["message"]
assert formatted["error"]["message"] == "Connection timeout"
def test_format_anthropic_structure(self):
"""
What it does: Verifies Anthropic format structure.
Purpose: Ensure compatibility with Anthropic API error format.
"""
print("Setup: Creating NetworkErrorInfo...")
error_info = NetworkErrorInfo(
category=ErrorCategory.CONNECTION_REFUSED,
user_message="Connection refused",
troubleshooting_steps=["Step 1"],
technical_details="Technical info",
is_retryable=True,
suggested_http_code=502
)
print("Action: Formatting for Anthropic...")
formatted = format_error_for_user(error_info, format_type="anthropic")
print("Verification: Anthropic format structure...")
assert "type" in formatted
assert formatted["type"] == "error"
assert "error" in formatted
assert "type" in formatted["error"]
assert "message" in formatted["error"]
assert formatted["error"]["type"] == "connectivity_error"
def test_format_generic_includes_technical_details(self):
"""
What it does: Verifies generic format includes technical details.
Purpose: Ensure debugging information is available in generic format.
"""
print("Setup: Creating NetworkErrorInfo...")
error_info = NetworkErrorInfo(
category=ErrorCategory.SSL_ERROR,
user_message="SSL error",
troubleshooting_steps=[],
technical_details="ConnectError: SSL handshake failed",
is_retryable=False,
suggested_http_code=502
)
print("Action: Formatting with generic format...")
formatted = format_error_for_user(error_info, format_type="generic")
print("Verification: Technical details present...")
assert "technical_details" in formatted["error"]
assert formatted["error"]["technical_details"] == "ConnectError: SSL handshake failed"
class TestGetShortErrorMessage:
"""Tests for get_short_error_message() function."""
def test_short_message_no_brackets(self):
"""
What it does: Verifies short message doesn't include brackets.
Purpose: Ensure clean log output without category brackets.
"""
print("Setup: Creating NetworkErrorInfo...")
error_info = NetworkErrorInfo(
category=ErrorCategory.DNS_RESOLUTION,
user_message="DNS resolution failed",
troubleshooting_steps=[],
technical_details="Technical info",
is_retryable=True,
suggested_http_code=502
)
print("Action: Getting short message...")
short_msg = get_short_error_message(error_info)
print("Verification: No brackets in message...")
print(f"Short message: {short_msg}")
assert short_msg == "DNS resolution failed"
assert "[" not in short_msg
assert "]" not in short_msg
def test_short_message_different_categories(self):
"""
What it does: Verifies short messages for different error categories.
Purpose: Ensure consistent format across all error types.
"""
print("Setup: Creating multiple NetworkErrorInfo instances...")
errors = [
NetworkErrorInfo(ErrorCategory.TIMEOUT_CONNECT, "Timeout", [], "Tech", True, 504),
NetworkErrorInfo(ErrorCategory.CONNECTION_REFUSED, "Refused", [], "Tech", True, 502),
NetworkErrorInfo(ErrorCategory.UNKNOWN, "Unknown", [], "Tech", True, 502),
]
print("Action: Getting short messages...")
for error_info in errors:
short_msg = get_short_error_message(error_info)
print(f"Verification: {error_info.category} -> {short_msg}")
assert short_msg == error_info.user_message
assert "[" not in short_msg
class TestNetworkErrorInfoDataclass:
"""Tests for NetworkErrorInfo dataclass."""
def test_network_error_info_creation(self):
"""
What it does: Verifies NetworkErrorInfo can be created with all fields.
Purpose: Ensure dataclass structure is correct.
"""
print("Setup: Creating NetworkErrorInfo...")
error_info = NetworkErrorInfo(
category=ErrorCategory.DNS_RESOLUTION,
user_message="Test message",
troubleshooting_steps=["Step 1", "Step 2"],
technical_details="Technical details",
is_retryable=True,
suggested_http_code=502
)
print("Verification: All fields accessible...")
assert error_info.category == ErrorCategory.DNS_RESOLUTION
assert error_info.user_message == "Test message"
assert len(error_info.troubleshooting_steps) == 2
assert error_info.technical_details == "Technical details"
assert error_info.is_retryable is True
assert error_info.suggested_http_code == 502
|