Skip to content

Development and Testing

farzad edited this page Oct 7, 2026 · 2 revisions

Development and Testing

Keep contributions in the project's spirit: clear over clever, and reuse the existing patterns (see Architecture). CONTRIBUTING.md is the canonical guide; this page expands on it.

Setup

git clone https://github.com/farzadnadiri/peykan.git
cd peykan
python -m venv .venv
.venv\Scripts\activate          # Linux/macOS: source .venv/bin/activate
pip install -e ".[dev]"

Before opening a PR

ruff check .
mypy src
pytest -q

All three must pass. CI (.github/workflows/ci.yml) runs them on Python 3.10–3.13, and a package job builds the sdist and wheel, runs twine check, installs the wheel in a clean environment and runs peykan from outside the checkout (this catches files missing from the package).

requirements.txt pins the dependencies CI and the Docker image install. Every pin must install on Python 3.10, the minimum supported version; regenerate it with 3.10, or check each pin's Requires-Python.

Where new code goes

  • Protocol logic in plain modules (obd.py, j1939.py, uds.py, logs.py, diagnostics.py), with no MCP or CLI awareness, so it's testable without a bus.
  • Tools in server/mcp_server.py, result models in server/schemas.py.
  • Anything that sends on the bus goes through the guard: bus = guard.wrap(raw_bus, "purpose", write=...), with write=True for anything that changes ECU or bus state, and shutdown_bus(raw_bus) in finally. Never call a raw bus's send from a tool or CLI command. Return status="blocked" on TransmitBlocked.
  • Listener threads each get their own make_bus() instance.
  • Simulator behaviour in simulator/: driving/cabin state in state.py, faults in faults.py.
  • CLI in cli.py; mirror new tools there where it makes sense. Print data to stdout; logs go to stderr.

Adding an MCP tool

  1. Protocol logic in a plain module, with unit tests.
  2. A result model in server/schemas.py with a status field.
  3. The tool in create_app(): passive tools read live_state.frames_since(...); request/response tools use a guarded bus as above. Cap duration_s / timeout_s with _capped_duration. Accept IDs as IntLike and parse them with parse_int (or j1939.resolve_pgn), so models can pass hex strings.
  4. If it's useful from the terminal, a matching CLI command.
  5. Tests: unit tests for the logic; for the tool, tests/test_server_app.py calls tools in-process via _call(app, name, args), and the end-to-end UDS tests show how to run a simulated ECU on a private virtual channel.

Test map

File Covers
test_dbc.py, test_decode_roundtrip.py DBC loading and encode/decode
test_state.py Driving and cabin model; every broadcast signal is modelled and encodable
test_faults.py, test_fault_scenarios.py Presets, activation, state effects across protocols
test_obd.py OBD encode/decode, live PIDs, reply matching
test_diagnostics.py DBC diagnostic protocol; service IDs match the DBC; read/write classification
test_j1939*.py IDs, catalog, simulator, CLI, BAM transport, multi-ECU DM1
test_uds.py ECU protocol logic and end-to-end ISO-TP over a virtual bus
test_logs.py All log formats, analysis, signals, path confinement, replay, recording
test_safety.py Transmit policy, allowlist, audit log, simulator refusal on hardware
test_server_app.py Tools, prompts, routes, CORS, DNS-rebinding protection, transports
test_cli.py, test_cli_features.py CLI commands
test_parsing.py ID and payload parsing
test_logging.py, test_rename_compat.py Logs on stderr; old MCP_CAN_* / mcp-can compatibility

Live check

Unit tests use fake or virtual buses in-process. Before a release it's worth a real run: peykan demo, then connect MCP Inspector (npx @modelcontextprotocol/inspector → http://localhost:6278/sse) or ollmcp and call a few tools.

Reporting issues

Open a GitHub issue with your OS, Python version, peykan version, and whether you're on the virtual bus or a real adapter (PEYKAN_CAN_INTERFACE). For hardware issues, a transmit log (PEYKAN_TRANSMIT_LOG_PATH) helps.

Clone this wiki locally