Skip to content

test: improve coverage for checkpoint sync client (83%) #522

@tcoratger

Description

@tcoratger

Summary

src/lean_spec/subspecs/sync/checkpoint_sync.py has 83% test coverage. While test_checkpoint_sync.py exists, the fetch_finalized_state() error paths and verify_checkpoint_state() validation logic are not fully covered.

Coverage stats:

File Statements Covered Coverage
sync/checkpoint_sync.py 48 9 uncovered 83%

Uncovered lines: 94–103 (HTTP error handling in fetch_finalized_state), 153–155 (exception path in verify_checkpoint_state)

What needs testing

fetch_finalized_state()

  • Happy path: Mock HTTP response with valid SSZ data, verify deserialization
  • Network error: httpx.RequestError wrapped in CheckpointSyncError
  • HTTP error status: httpx.HTTPStatusError (e.g., 404, 500) wrapped in CheckpointSyncError
  • Deserialization error: Corrupt SSZ data raises CheckpointSyncError
  • URL normalization: Trailing slash in URL is stripped before appending endpoint

verify_checkpoint_state()

  • Valid state: Returns True for state with validators
  • No validators: Returns False, logs error
  • Exceeds registry limit: Returns False when validator count > VALIDATOR_REGISTRY_LIMIT
  • Exception during verification: Returns False (catches any exception)
  • State root computation: Verify hash_tree_root is called (logged for debugging)

Why this matters

  • Fast startup: Checkpoint sync enables nodes to start in seconds vs hours. Error handling must be robust
  • Trust boundary: The fetched state comes from an external source. Validation prevents using corrupt state
  • Error clarity: Specific CheckpointSyncError messages help operators diagnose connectivity issues

How to test

Running tests with coverage

uv run pytest tests/lean_spec/subspecs/sync/test_checkpoint_sync.py -v \
  --cov=src/lean_spec/subspecs/sync/checkpoint_sync --cov-report=term-missing

Target: ≥95% line coverage (small module).

Test file location

tests/lean_spec/subspecs/sync/test_checkpoint_sync.py  (extend existing)

Testing tips

  • Use pytest-httpx or mock httpx.AsyncClient for HTTP tests
  • For verify_checkpoint_state(), create a mock State with configurable validator count
  • Test CheckpointSyncError message content for each error path
  • The FINALIZED_STATE_ENDPOINT constant can be used to verify correct URL construction

Using Claude Code subagents

1. code-tester agent — Generate the tests

Extend tests in tests/lean_spec/subspecs/sync/test_checkpoint_sync.py. Cover fetch_finalized_state error paths (httpx.RequestError, HTTPStatusError, corrupt SSZ), verify_checkpoint_state with no validators, exceeding registry limit, and exception during hash_tree_root.

Workflow

  1. Add HTTP error path tests for fetch_finalized_state
  2. Add validation edge cases for verify_checkpoint_state
  3. Run uvx tox -e all-checks to pass all quality checks

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomerstestsScope: Changes to the spec tests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions