Skip to content

test: nonce manager unit tests, docker-compose, and smoke test#3

Open
HaraldeRoessler wants to merge 3 commits intoMoltyCel:mainfrom
HaraldeRoessler:test/nonce-manager-docker-smoke
Open

test: nonce manager unit tests, docker-compose, and smoke test#3
HaraldeRoessler wants to merge 3 commits intoMoltyCel:mainfrom
HaraldeRoessler:test/nonce-manager-docker-smoke

Conversation

@HaraldeRoessler
Copy link
Copy Markdown
Contributor

Summary

  • 7 unit tests for app/nonce_manager.py (introduced in PR fix: async Web3 calls + nonce manager to prevent event loop blocking #2) covering sequential nonces, concurrent safety, reset/refetch, and per-address independence
  • docker-compose.yml for one-command local dev setup (PostgreSQL 16 + API)
  • scripts/smoke_test.sh hitting key endpoints with pass/fail reporting

How to use

Run unit tests:

pip install pytest pytest-asyncio
python -m pytest tests/test_nonce_manager.py -v

Start local dev environment:

docker compose up --build
# API at http://localhost:8000, PostgreSQL at localhost:5432

Run smoke test:

# Against local docker-compose
./scripts/smoke_test.sh http://localhost:8000 dev_test_key_local

# Against production
./scripts/smoke_test.sh https://api.moltrust.ch your_api_key

Test results (local)

All 7 nonce manager tests pass:

tests/test_nonce_manager.py::test_first_call_fetches_from_chain PASSED
tests/test_nonce_manager.py::test_second_call_increments_locally PASSED
tests/test_nonce_manager.py::test_sequential_calls_produce_unique_nonces PASSED
tests/test_nonce_manager.py::test_different_addresses_are_independent PASSED
tests/test_nonce_manager.py::test_reset_forces_refetch PASSED
tests/test_nonce_manager.py::test_reset_nonexistent_address_is_noop PASSED
tests/test_nonce_manager.py::test_concurrent_calls_get_unique_nonces PASSED

Depends on #2

Generated with Claude Code

HaraldeRoessler and others added 3 commits April 1, 2026 14:51
All synchronous Web3 RPC calls (get_transaction_count, gas_price,
send_raw_transaction, wait_for_transaction_receipt, is_connected) were
blocking the FastAPI async event loop, causing latency spikes for all
concurrent users.

Changes:
- Add app/nonce_manager.py with per-address asyncio.Lock to prevent
  nonce collisions when multiple async handlers submit transactions
  concurrently from the same wallet
- Wrap all blocking Web3 calls in asyncio.to_thread() across main.py,
  erc8004.py, and provenance/anchor.py
- Convert erc8004.py functions (post_reputation_feedback,
  register_onchain_agent, get_onchain_reputation) from sync to async
  and update all call sites with await
- Replace music VC anchoring via cast CLI subprocess with web3.py,
  eliminating private key exposure in process environment

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add tests/test_nonce_manager.py with 7 tests covering: first fetch,
  local increment, sequential uniqueness, per-address independence,
  reset/refetch, and concurrent nonce safety (20 parallel requests)
- Add docker-compose.yml for one-command local dev (PostgreSQL + API)
- Add scripts/smoke_test.sh hitting key endpoints (health, identity,
  reputation, credentials, auth enforcement)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Found during local testing:
- set -u breaks with empty arrays in bash, changed to set -eo pipefail
- ((PASS++)) returns exit code 1 when PASS=0 under set -e, use arithmetic
- Endpoint expectations adjusted to match actual API behavior
- Unique agent names per run to avoid 409 duplicate detection
- Auth test uses separate curl without valid key header
- docker-compose: add DB_HOST, DID_PRIVATE_KEY_HEX for test signing,
  env_file for optional Dilithium keys, remove obsolete version field

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@HaraldeRoessler
Copy link
Copy Markdown
Contributor Author

Merge order

Depends on #1 (needs requirements.txt and Dockerfile). Merge after #1.

Sequence: #1#3#2#4#7

Update: Smoke test and docker-compose fixed based on local testing — all 7 tests pass.

HaraldeRoessler added a commit to HaraldeRoessler/moltrust-api that referenced this pull request Apr 30, 2026
First-cut CI for moltrust-api — three lightweight jobs that catch the
most-common breakage without needing a running API stack:

  install         pip install -r requirements.txt on Python 3.12
                  (matches Dockerfile). Flags dep conflicts.
  syntax          python -m compileall on every source dir.
                  Catches SyntaxError before deploy.
  pytest-collect  pytest --collect-only on root test_*.py files.
                  Catches test ImportError without booting sandbox.

Deliberately omitted (each is a follow-up of its own):

  - lint (ruff/flake8): existing code may have legacy style issues
    that would block this PR; better added in a dedicated PR.
  - test execution: existing tests target localhost:8005 and need
    the API + Postgres + Web3 stack online. PR MoltyCel#3 adds
    docker-compose.yml + scripts/smoke_test.sh which would slot in
    as a fourth job once merged.

Closes (in part) MoltyCel#9 "RFC: CI/CD pipeline and automated testing".

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant