fix: address PR review — flash state race, error paths, test robustness, nits

- server.py: claim the flash (state=bootloader) before spawning the task so
  the concurrent-request guard is effective immediately; guard the bundled
  image existence in the flash endpoint; single device.info() call per
  endpoint; 400 for refused flashes (409 stays for already-running);
  remove stale nvcurve-wireview-fw-*.bin temp files at startup
- wireview_firmware.py: drain stderr in its own reader thread (a full pipe
  buffer would have hung the flash until timeout); chunked stdout reads
  instead of per-character read(2); BuildStruct field checks in dataclass
  order
- wireview.py: document the intentional 0xF1 overlap between CMD_BOOTLOADER
  and SCREEN_RESUME_UPDATES (different command namespaces)
- tests: check() now fails loudly under pytest (optional import, standalone
  runs unaffected); explicit reader-thread sync in test_enter_bootloader;
  less brittle flash-progress assertion; self-documenting checksum
  corruption in test_hex_parser
- frontend: refreshFirmware in useCallback + effect deps; NaN% guard on
  flash.progress; explicit clear/keep masks in handleClear; WS firmware
  message reuses WireViewFlashProgress via Pick
- docs: clarify that the dfu-util :leave modifier is what makes the device
  exit the bootloader and reboot; document the 400 vs 409 API contract;
  README now mentions per-pin readings, pin-imbalance gauge and fault
  clearing
This commit is contained in:
ARIA committed 2026-10-05 20:40:05 +02:00
1 parent dbb6856a6d
commit 6e87dcf862
8 files changed
+111 -55

No files matched your search

+29 -4
View File
@@ -21,6 +21,11 @@ from unittest import mock
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
try:
import pytest # type: ignore[import-unresolved] # optional: not installed for standalone runs
except ImportError: # standalone run without pytest installed
pytest = None
from nvcurve import wireview as wv # noqa: E402
from nvcurve import wireview_firmware as wf # noqa: E402
@@ -36,6 +41,10 @@ def check(name: str, cond: bool) -> None:
else:
FAIL += 1
print(f" FAIL {name}")
# Fail loudly under pytest (standalone runs rely on the FAIL count
# and main()'s exit code).
if pytest is not None:
pytest.fail(name)
def _hex_line(addr: int, data: bytes, record_type: int = 0) -> str:
@@ -128,8 +137,10 @@ def test_hex_parser():
check("hex segment address", mem2 == {0x100: 0x42})
# A bad checksum must be rejected — a corrupt image must never flash.
# Corrupt the record by forcing its checksum byte to 0x00.
bad = _hex_line(0x0000, bytes([0x00, 0x60]))
bad = bad[:-2] + ("00" if bad[-2:] != "00" else "01")
assert bad[-2:] != "00" # the correct checksum of this line is non-zero
bad = bad[:-2] + "00"
path3 = _write_hex(tmp, [bad])
try:
wf._parse_hex_to_memory_map(path3)
@@ -281,8 +292,10 @@ class _FakeStderr:
def __init__(self, text: str):
self._text = text
def read(self) -> str:
return self._text
def read(self, n: int | None = None) -> str:
# The flasher drains stderr in a read() loop until EOF.
out, self._text = self._text, ""
return out
class _FakeProc:
@@ -341,7 +354,12 @@ def test_dfu_flash():
wf.DfuUtilFlasher.flash(bin_path, 0x08000000,
progress_cb=progress.append)
check("flash no error", True)
check("flash progress", progress == [0.0, 0.25, 0.5, 1.0])
check(
"flash progress",
len(progress) >= 2
and progress[-1] == 1.0
and all(a <= b for a, b in zip(progress, progress[1:], strict=False)),
)
args = popen.call_args.args[0]
check("flash args",
args == ["dfu-util", "-d 0483:df11", "-a", "0",
@@ -410,8 +428,12 @@ def test_enter_bootloader():
master, slave = pty.openpty()
slave_path = os.ttyname(slave)
received: queue.Queue = queue.Queue()
reader_started = threading.Event()
def run():
# Signal that the reader is alive and about to enter its read loop,
# so the test can wait for it before the first command is written.
reader_started.set()
while True:
try:
cmd = os.read(master, 1)
@@ -439,6 +461,9 @@ def test_enter_bootloader():
t = threading.Thread(target=run, daemon=True)
t.start()
try:
# Wait until the reader thread is actually reading, so the first
# command from the device cannot be missed.
check("boot: reader ready", reader_started.wait(timeout=5))
dev = wv.WireViewSerialDevice(slave_path)
check("boot: connect", dev.connect())
check("boot: enter", dev.enter_bootloader())