openwebui-cli/docs/internals/TESTING_SUMMARY.md
2025-12-01 04:24:51 +01:00

7.3 KiB

OpenWebUI CLI - Request Body Options Testing Summary

Project

Repository: /home/setup/openwebui-cli Target Module: openwebui_cli/commands/chat.py (send command) Test File: tests/test_chat_request_options.py

Objective

Verify that optional request body parameters are correctly populated when CLI flags are provided:

  • --chat-idchat_id in request body
  • --temperaturetemperature in request body
  • --max-tokensmax_tokens in request body

Implementation Summary

Test Suite: 10 Tests, 100% Pass Rate

Individual Option Tests (3 tests)

  1. test_chat_id_in_body

    • Validates: --chat-id my-chat-123 populates body["chat_id"]
    • Assertion: body["chat_id"] == "my-chat-123"
  2. test_temperature_in_body

    • Validates: --temperature 0.7 populates body["temperature"]
    • Assertion: body["temperature"] == 0.7 (float type)
  3. test_max_tokens_in_body

    • Validates: --max-tokens 1000 populates body["max_tokens"]
    • Assertion: body["max_tokens"] == 1000 (int type)

Combined Options Test (1 test)

  1. test_all_options_combined
    • Tests all three flags together: --chat-id, --temperature, --max-tokens
    • Verifies all values present and correctly typed in single request

Value Validation Tests (2 tests)

  1. test_temperature_with_different_values

    • Range test: [0.0, 0.3, 1.0, 1.5, 2.0]
    • Ensures float parsing works across valid temperature spectrum
  2. test_max_tokens_with_different_values

    • Range test: [100, 500, 1000, 4000, 8000]
    • Ensures int parsing works across typical token limits

Edge Cases & Integration (4 tests)

  1. test_options_not_in_body_when_not_provided

    • Validates optional fields NOT included when flag omitted
    • Prevents polluting request body with null/default values
  2. test_chat_id_with_special_characters

    • Tests UUID-style IDs: uuid-12345-67890-abcdef
    • Tests timestamp-style IDs: chat_2025_01_01_001
    • Tests conversational IDs: conversation-abc123xyz
  3. test_request_body_has_core_fields

    • Verifies mandatory fields always present:
      • model (required)
      • messages (required array)
      • stream (required boolean)
  4. test_all_options_with_system_prompt

    • Integration test combining options with system prompt
    • Validates request structure preserves all components

Test Architecture

Mocking Strategy

@patch('openwebui_cli.commands.chat.create_client')
def test_chat_id_in_body(mock_create_client):
    # Mock HTTP client
    mock_http_client = MagicMock()
    mock_http_client.post.return_value = mock_response
    mock_create_client.return_value = mock_http_client
    
    # Execute CLI command
    result = runner.invoke(app, [...args...])
    
    # Capture and verify request body
    call_args = mock_http_client.post.call_args
    body = call_args.kwargs["json"]
    assert body["chat_id"] == "expected_value"

Helper Functions

  • _create_mock_client() - Factory pattern for consistent mock setup
  • Reusable pytest fixtures: mock_config, mock_keyring

Test Results

============================= test session starts ==============================
platform linux -- Python 3.12.3, pytest-9.0.1, pluggy-1.6.0
cachedir: .pytest_cache
rootdir: /home/setup/openwebui-cli
configfile: pyproject.toml
collected 10 items

tests/test_chat_request_options.py::test_chat_id_in_body PASSED          [ 10%]
tests/test_chat_request_options.py::test_temperature_in_body PASSED      [ 20%]
tests/test_chat_request_options.py::test_max_tokens_in_body PASSED       [ 30%]
tests/test_chat_request_options.py::test_all_options_combined PASSED     [ 40%]
tests/test_chat_request_options.py::test_temperature_with_different_values PASSED [ 50%]
tests/test_chat_request_options.py::test_max_tokens_with_different_values PASSED [ 60%]
tests/test_chat_request_options.py::test_options_not_in_body_when_not_provided PASSED [ 70%]
tests/test_chat_request_options.py::test_chat_id_with_special_characters PASSED [ 80%]
tests/test_chat_request_options.py::test_request_body_has_core_fields PASSED [ 90%]
tests/test_chat_request_options.py::test_all_options_with_system_prompt PASSED [100%]

============================== 10 passed in 0.71s ==============================

Code Coverage

The tests provide coverage for the request body population logic in chat.py:

# Lines 98-102: Core body initialization
body: dict[str, Any] = {
    "model": effective_model,
    "messages": messages,
    "stream": not no_stream and config.defaults.stream,
}

# Lines 104-107: Conditional parameter population
if temperature is not None:
    body["temperature"] = temperature
if max_tokens is not None:
    body["max_tokens"] = max_tokens

# Lines 120-122: Chat ID population
if chat_id:
    body["chat_id"] = chat_id

Integration Testing

All new tests pass alongside existing chat tests:

  • Existing tests: 7 tests in test_chat.py - All PASS
  • New tests: 10 tests in test_chat_request_options.py - All PASS
  • Total: 17 tests PASS
  • Regression: 0 failures

Existing Test Compatibility

The new test suite does not conflict with:

  • test_chat_send_streaming - Uses different assertion patterns
  • test_chat_send_no_stream - Similar mocking, complementary focus
  • test_chat_send_with_system_prompt - Shares system prompt test pattern
  • test_chat_send_with_history_file - Independent test scope
  • test_chat_send_stdin - Independent test scope
  • test_chat_send_json_output - Independent test scope
  • test_chat_send_with_rag_context - Already validates body capture pattern

Running Tests

Quick Test Run

cd /home/setup/openwebui-cli
.venv/bin/pytest tests/test_chat_request_options.py -v

With Coverage Report

.venv/bin/pytest tests/test_chat_request_options.py -v \
  --cov=openwebui_cli.commands.chat \
  --cov-report=term-missing

All Chat Tests (Integration)

.venv/bin/pytest tests/test_chat.py tests/test_chat_request_options.py -v

Key Testing Insights

  1. Type Safety - Tests verify correct Python types (int vs float)
  2. Conditional Logic - Tests confirm optional fields only included when specified
  3. CLI Argument Parsing - Tests validate Typer correctly parses string arguments to correct types
  4. Mock Isolation - Tests use mocks to avoid HTTP dependencies while capturing request intent
  5. Real-World Scenarios - Tests include special characters and ID formats used in practice

Deliverables Checklist

  • Complete test file: tests/test_chat_request_options.py (375 lines)
  • All 10 tests passing
  • Tests capture and verify request body
  • Coverage for individual options
  • Coverage for combined options
  • Coverage for edge cases and special characters
  • Integration with existing test suite
  • No regressions in existing tests
  • Documentation and summary

Files Modified/Created

  • Created: /home/setup/openwebui-cli/tests/test_chat_request_options.py
  • No modifications to source code (tests only)
  • No modifications to existing tests

Conclusion

The test suite comprehensively validates request body population for all three optional parameters (--chat-id, --temperature, --max-tokens) across individual, combined, and edge case scenarios. All 10 tests pass successfully with zero regressions.