Testing

Running the offline suite

pip install -e ".[dev]"
ruff check src tests
ruff format --check src tests
mypy
pytest -m "not hardware"

All of the above run in CI (.github/workflows/ci.yml) on Linux, Windows, and macOS. There are currently no tests marked hardware — see below.

Test layout

tests/unit/domain/      # pure model validation
tests/unit/registry/    # loader mechanics + golden tests over the real migrated profiles
tests/unit/backend/     # FakeBackend + backend/openocd/* (fake-subprocess-injected)
tests/unit/flashops/    # plan builder, backup writer, restore, full state machine
tests/unit/catalog/     # index parsing, precedence/conflicts, transport (mocked urllib)
tests/unit/userdata/    # ESC/BLE codecs against golden binary fixtures
tests/unit/config/      # settings load/save
tests/unit/cli/         # CLI helpers + command wiring
tests/fixtures/         # golden binary fixtures (see below)

The fake programmer backend

scooterflasher.backend.fake.FakeBackend is a deterministic, in-memory ProgrammingBackend with fault injection:

from scooterflasher.backend.fake import FakeBackend
from scooterflasher.backend.errors import WriteError

backend = FakeBackend(
    mismatched_targets={"some-target-id"},       # identify() reports a mismatch
    verify_mismatch_regions={"firmware"},         # verify_memory() returns False
    protected=True,                                # is_protected() -> True
    fail_on={"write_memory": WriteError("boom")},  # raises once, then clears
)

tests/unit/flashops/test_state_machine.py drives the entire discover→...→report pipeline through FakeBackend: happy path, a missing confirmation, a disconnected adapter, a hardware mismatch (with and without a forced override), a verification failure (asserting it stops before reset), a skipped backup (with and without confirmation), a protected target requiring its own unlock confirmation, and mass-erase user-data restoration. This is the primary coverage for the safety properties the whole project is built around — extend it first when changing state-machine behavior.

Testing the OpenOCD backend without hardware

backend/openocd/runner.py and backend/openocd/backend.py both accept an injectable popen_factory, so tests substitute a fake subprocess that returns scripted stdout/exit codes (and, for read_memory/write_memory, writes/reads the temp files OpenOCD would have) instead of launching a real openocd. This is how the false-success regression tests in tests/unit/backend/openocd/test_runner.py work: they assert that the exact failure modes of the original (non-zero exit with no output, a critical error line with exit code 0, the last line of output not being dropped) are all handled correctly, without needing a real adapter or scooter to reproduce them.

Golden binary fixtures

tests/fixtures/userdata/*.bin are synthetic, hand-constructed byte buffers — never real device data. generate_fixtures.py in that directory is committed alongside them and regenerates them deterministically:

python tests/fixtures/userdata/generate_fixtures.py

Run it and commit the output whenever scooterflasher.userdata.esc or .nrf51_ble's documented layout changes.

Hardware-in-the-loop testing

None exists yet in this repository. Before claiming any tested support status (see support-matrix.md) for a model/target/adapter combination:

  1. Add a test marked @pytest.mark.hardware (excluded from the default pytest -m "not hardware" run and from CI) that exercises the real flow against the real device.
  2. Document the exact hardware used (scooter model, MCU part number if known, adapter, OpenOCD version, host OS) in support-matrix.md.
  3. Only then upgrade the relevant support_status in the device profile YAML from experimental to tested, in the same change as the test and the support-matrix update.

Do not upgrade a support_status on the basis of "it should work" or manual, undocumented testing — the whole point of the status field is that tested means there is a checked-in test plus a documented hardware run behind it.