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

202 lines
7.3 KiB
Markdown

# 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-id``chat_id` in request body
- `--temperature``temperature` in request body
- `--max-tokens``max_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)
4. **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)
5. **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
6. **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)
7. **test_options_not_in_body_when_not_provided**
- Validates optional fields NOT included when flag omitted
- Prevents polluting request body with null/default values
8. **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`
9. **test_request_body_has_core_fields**
- Verifies mandatory fields always present:
- `model` (required)
- `messages` (required array)
- `stream` (required boolean)
10. **test_all_options_with_system_prompt**
- Integration test combining options with system prompt
- Validates request structure preserves all components
## Test Architecture
### Mocking Strategy
```python
@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`:
```python
# 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
```bash
cd /home/setup/openwebui-cli
.venv/bin/pytest tests/test_chat_request_options.py -v
```
### With Coverage Report
```bash
.venv/bin/pytest tests/test_chat_request_options.py -v \
--cov=openwebui_cli.commands.chat \
--cov-report=term-missing
```
### All Chat Tests (Integration)
```bash
.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
- [x] Complete test file: `tests/test_chat_request_options.py` (375 lines)
- [x] All 10 tests passing
- [x] Tests capture and verify request body
- [x] Coverage for individual options
- [x] Coverage for combined options
- [x] Coverage for edge cases and special characters
- [x] Integration with existing test suite
- [x] No regressions in existing tests
- [x] 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.