Fix: only log USB event once per debounce window in SerialPortObserver - #106
Conversation
Duplicate log lines were seen for a single physical USB plug event because the underlying usb lib can fire multiple native events (e.g. connect twice for a composite USB-serial adapter) close together. The log statement fired on every raw event, before the debounce timer had a chance to collapse them, even though only one actual scan ran. Now the log only fires when opening a new debounce window (i.e. when no rescan is already pending), and rescanTimer is reset once the scan runs so a later, separate USB event still logs correctly.
📝 WalkthroughWalkthroughUSB event handling now suppresses duplicate delayed-rescan logs while a rescan timer is active. The timer resets when the scan starts. Tests cover repeated events during and after the debounce window. ChangesUSB rescan logging
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/unit/device/transport/serialPortObserver.spec.ts (1)
291-305: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAssert the number of delayed scans.
These tests assert only
mockLogger.debug. The first test can pass even if both timers execute later. The second test can pass if the later event logs but its replacement timer never runs.Record the
SerialPort.listcall count afterobserver.start(). Advance the fake clock after each event sequence. Assert one additional scan in the first test and two additional scans in the second test.Proposed test strengthening
const observer = createObserver(); await observer.start(); + const listCallsAfterStart = vi.mocked(SerialPort.list).mock.calls.length; const onUsbEvent = getRegisteredUsbEventHandler(); onUsbEvent(); onUsbEvent(); expect(mockLogger.debug).toHaveBeenCalledTimes(1); + await vi.advanceTimersByTimeAsync(1000); + expect(vi.mocked(SerialPort.list)).toHaveBeenCalledTimes(listCallsAfterStart + 1);Apply the same baseline in the later-event test. Advance the second timer and assert
listCallsAfterStart + 2.Also applies to: 307-322
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unit/device/transport/serialPortObserver.spec.ts` around lines 291 - 305, Strengthen the USB debounce tests by asserting delayed scans, not only debug logging. In each test around observer.start, record the SerialPort.list call count after startup, advance the fake clock after each event sequence, and assert the first sequence adds one scan while the later-event replacement-timer sequence adds two scans. Keep the existing log assertions and event setup unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/unit/device/transport/serialPortObserver.spec.ts`:
- Around line 291-305: Strengthen the USB debounce tests by asserting delayed
scans, not only debug logging. In each test around observer.start, record the
SerialPort.list call count after startup, advance the fake clock after each
event sequence, and assert the first sequence adds one scan while the
later-event replacement-timer sequence adds two scans. Keep the existing log
assertions and event setup unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 54dd959c-cb78-4d94-b0f0-a52af3d65396
📒 Files selected for processing (2)
src/device/transport/serialPortObserver.tstests/unit/device/transport/serialPortObserver.spec.ts
Duplicate log lines were seen for a single physical USB plug event because the underlying usb lib can fire multiple native events (e.g. connect twice for a composite USB-serial adapter) close together. The log statement fired on every raw event, before the debounce timer had a chance to collapse them, even though only one actual scan ran.
Now the log only fires when opening a new debounce window (i.e. when no rescan is already pending), and rescanTimer is reset once the scan runs so a later, separate USB event still logs correctly.
Summary by CodeRabbit
Bug Fixes
Tests