Skip to content

Latest commit

 

History

History
421 lines (304 loc) · 9.18 KB

File metadata and controls

421 lines (304 loc) · 9.18 KB

Testing Guide for Yoto Smart Stream

This guide provides comprehensive instructions for testing and validating the Yoto Smart Stream implementation.

Table of Contents

Quick Test

To quickly verify the installation and run all tests:

# Install dependencies
pip install -e ".[dev]"

# Run all tests
pytest

# Run with coverage report
pytest --cov=yoto_smart_stream --cov-report=html

Unit Tests

Icon Management Tests

The icon management module has comprehensive test coverage (96%):

# Run only icon tests
pytest tests/icons/ -v

# Run with coverage
pytest tests/icons/ --cov=yoto_smart_stream.icons --cov-report=term-missing

Test coverage includes:

  • ✅ Data model validation (Pydantic models)
  • ✅ API client methods (mocked HTTP requests)
  • ✅ Service layer business logic
  • ✅ Icon validation (format, size, dimensions)
  • ✅ Error handling
  • ✅ Cache functionality

Example Script Tests

Tests verify that example scripts can be imported and basic functionality works:

# Run example integration tests
pytest tests/test_examples.py -v

These tests verify:

  • ✅ All example scripts import without errors
  • ✅ FastAPI app is properly configured
  • ✅ API routes are registered correctly
  • ✅ Health endpoints work without authentication
  • ✅ EventLogger functionality works

Integration Tests

Testing the API Server

The basic server example includes integration tests that can run without Yoto credentials:

# Run server integration tests
pytest tests/test_examples.py::TestBasicServerStartup -v

These tests verify:

  • Health endpoint (/health) responds correctly
  • Root endpoint (/) returns API information
  • Server can start without authentication (for initial setup)

Starting the Server

To start the API server for manual testing:

# Without authentication (limited functionality)
python examples/basic_server.py

# Or using uvicorn
uvicorn examples.basic_server:app --reload

Visit:

Testing with Mock Data

For testing without actual Yoto credentials, you can:

  1. Test health and info endpoints (no auth required):

    curl http://localhost:8080/health
    curl http://localhost:8080/
  2. Review API schema in the interactive docs:

    • Visit http://localhost:8080/docs
    • Explore all endpoints and their parameters
    • Try the "Try it out" feature (will fail without auth, but shows request format)

Manual Testing

Prerequisites for Full Testing

To test with actual Yoto devices, you need:

  1. Yoto Client ID: Get from yoto.dev
  2. Environment Setup:
    cp .env.example .env
    # Edit .env and add your YOTO_CLIENT_ID

Authentication Flow

First-time authentication:

# Run the simple client example
python examples/simple_client.py

This will:

  1. Prompt you to visit a URL and enter a code
  2. Save your refresh token to .yoto_refresh_token
  3. Display your Yoto players
  4. Test basic player control

Testing MQTT Events

Listen to real-time events from your Yoto players:

# Start the MQTT listener
python examples/mqtt_listener.py

# With file logging
python examples/mqtt_listener.py --log-file

Interact with your Yoto player to see events:

  • Insert/remove cards
  • Press play/pause
  • Change volume
  • Change night light settings
  • Press navigation buttons

Testing Icon Management

The icon management example demonstrates the icon API:

# Review the example code
cat examples/icon_management.py

# To run it (requires access token):
# 1. Authenticate first with simple_client.py
# 2. Extract access token from YotoManager
# 3. Update icon_management.py with token
# 4. Uncomment asyncio.run(main())
# 5. Run: python examples/icon_management.py

Code Quality

Linting

Check code style and quality:

# Run ruff linter
ruff check .

# Auto-fix issues
ruff check --fix .

# Check specific directory
ruff check yoto_smart_stream/

Formatting

Format code with black:

# Format all files
black .

# Check without modifying
black --check .

# Format specific files
black yoto_smart_stream/ tests/

Type Checking

Run mypy for type validation:

# Check all files
mypy yoto_smart_stream/

# Ignore missing imports (for now)
mypy --ignore-missing-imports yoto_smart_stream/

Pre-commit Hooks

Install pre-commit hooks to automatically check code quality:

# Install hooks
pre-commit install

# Run manually on all files
pre-commit run --all-files

Testing Without Yoto Credentials

You can test significant portions of the codebase without Yoto credentials:

1. Unit Tests (No Credentials Required)

All unit tests use mocked HTTP clients and don't require real API access:

pytest tests/icons/ -v
pytest tests/test_examples.py -v

2. Icon Module Testing

# Create a test script to explore icon models
from yoto_smart_stream.icons.models import DisplayIcon, IconUploadRequest

# Create a test icon
icon = DisplayIcon(
    id="test-001",
    name="Test Icon",
    url="https://example.com/icon.png",
    category="test",
    tags=["test"],
    is_public=True
)

print(f"Icon: {icon.name} - {icon.url}")

3. Server Structure Testing

Start the server without credentials to test structure:

# The server will start in limited mode
python examples/basic_server.py

Test these endpoints (no auth required):

  • GET / - API information
  • GET /health - Health check
  • GET /docs - Interactive API documentation

4. Example Code Review

Review example code to understand patterns:

# View example implementations
cat examples/simple_client.py
cat examples/basic_server.py
cat examples/mqtt_listener.py
cat examples/icon_management.py

Test Coverage

Current test coverage statistics:

Module                            Coverage
----------------------------------------
yoto_smart_stream/icons/client.py   100%
yoto_smart_stream/icons/models.py   100%
yoto_smart_stream/icons/service.py   90%
----------------------------------------
Overall Icon Module                  96%

Generate detailed coverage report:

# Generate HTML coverage report
pytest --cov=yoto_smart_stream --cov-report=html

# Open report in browser
# Linux/Mac: open htmlcov/index.html
# Windows: start htmlcov/index.html

Continuous Testing

Watch Mode

For development, you can use pytest-watch to automatically run tests on file changes:

# Install pytest-watch
pip install pytest-watch

# Run in watch mode
ptw -- --cov=yoto_smart_stream

Testing Workflow

Recommended workflow during development:

  1. Make changes to code
  2. Run linter: ruff check --fix .
  3. Format code: black .
  4. Run tests: pytest tests/
  5. Check coverage: pytest --cov=yoto_smart_stream
  6. Commit if all checks pass

Troubleshooting

Common Issues

  1. Import errors in tests:

    # Ensure package is installed in editable mode
    pip install -e .
  2. YOTO_CLIENT_ID not found:

    # Set environment variable
    export YOTO_CLIENT_ID=your_client_id_here
    
    # Or create .env file
    echo "YOTO_CLIENT_ID=your_client_id" > .env
  3. Async test warnings:

    • Ensure pytest-asyncio is installed: pip install pytest-asyncio
    • Check pytest config in pyproject.toml
  4. Coverage not working:

    # Install coverage plugin
    pip install pytest-cov

Getting Help

If you encounter issues:

  1. Check the README.md for setup instructions
  2. Review example code in examples/ directory
  3. Check test implementations in tests/ directory
  4. Open an issue on GitHub with:
    • Python version: python --version
    • Pytest version: pytest --version
    • Error message and stack trace
    • Steps to reproduce

Next Steps

After successful testing:

  1. Review Documentation:

  2. Explore Examples:

    • Study example scripts in examples/ directory
    • Modify examples for your use case
    • Create your own integrations
  3. Implement Features:

    • Add custom audio streaming
    • Create interactive cards
    • Build web UI components
    • Integrate with other services
  4. Contribute:

    • Report bugs and issues
    • Submit pull requests
    • Share your implementations
    • Help improve documentation

Summary

This testing guide covers:

  • ✅ Complete unit test suite (96% coverage)
  • ✅ Integration tests for examples
  • ✅ Manual testing procedures
  • ✅ Code quality checks (linting, formatting, type checking)
  • ✅ Testing without credentials
  • ✅ Continuous testing workflow
  • ✅ Troubleshooting guidance

All tests pass successfully and the implementation is ready for human testing with real Yoto devices!