]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commit
test: harmonize event listener test pattern across OCPP command test files (#1730)
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 16 Mar 2026 21:36:24 +0000 (22:36 +0100)
committerGitHub <noreply@github.com>
Mon, 16 Mar 2026 21:36:24 +0000 (22:36 +0100)
commit8eb7710860a4cdb00e0a4ac86d6b633abf604488
tree273de6f8086985c96c950ce02efa2520abbce329
parentb50f9e5f547632af2187d1df351383d1a1222b8f
test: harmonize event listener test pattern across OCPP command test files (#1730)

* test: harmonize event listener test pattern across OCPP command test files

Add event listener test sections to 7 OCPP incoming request command test
files (4 OCPP 1.6, 3 OCPP 2.0) following the reference pattern from
RequestStopTransaction, TriggerMessage, UpdateFirmware, and GetLog tests.

Each listener section contains: registration test, accepted-fires test,
rejected-not-fires test, and error-graceful test.

Also restructures CustomerInformation to wrap existing listener tests in a
properly named describe block, and adds createOCPP16ListenerStation helper
to OCPP16TestUtils.ts.

Files modified:
- tests/charging-station/ocpp/1.6/OCPP16TestUtils.ts
- tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStartTransaction.test.ts
- tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStopUnlock.test.ts
- tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-TriggerMessage.test.ts
- tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Firmware.test.ts
- tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts
- tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts
- tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CustomerInformation.test.ts
- tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStopTransaction.test.ts

* fix(tests): address PR review — extract shared helper, fix mock cleanup, use flushMicrotasks

- Extract createOCPP20ListenerStation to OCPP20TestUtils.ts, removing
  duplication between RequestStart and RequestStop test files
- Replace inline import('node:test').mock.fn with top-level mock import
  in RemoteStartTransaction tests
- Remove redundant mock.reset() from 3 listener afterEach blocks —
  standardCleanup() already calls mock.restoreAll()
- Replace all await Promise.resolve() with flushMicrotasks() across 5
  OCPP 2.0 test files for more robust async side-effect flushing

* fix(tests): replace remaining await Promise.resolve() with flushMicrotasks()

8 occurrences in 5 files (3 OCPP16, 2 OCPP20) missed in the initial
review fix. Now all listener tests use flushMicrotasks() consistently.

* fix(tests): fix 3 audit findings — JSDoc headers and missing afterEach

- GetBaseReport: move @file JSDoc above first import (style guide §2)
- OCPP20TestUtils: add missing @file/@description header (style guide §2)
- RequestStopTransaction: add afterEach with standardCleanup to listener
  describe block (style guide §3)

* fix(tests): address 5 minor audit findings

- Fix import paths in 7 OCPP 2.0 test files: ../../../../tests/helpers/
  → ../../../helpers/ (correct relative path, consistent with 35 sibling
  files in the same directory)
- Add eventType assertion in RequestStartTransaction listener test to
  verify TransactionEvent(Started) per E02.FR.01
- Add flushMicrotasks() to RequestStopTransaction listener test for
  consistent emit→flush→assert pattern

* refactor(tests): move mock.method to beforeEach and parameterize trigger tests

- Move duplicated mock.method calls into listener beforeEach blocks
  in 5 files (UpdateFirmware, GetLog, GetBaseReport, CustomerInfo,
  Firmware). Rejection tests override inline. Net -147 lines.
- Parameterize OCPP16 + OCPP20 TriggerMessage trigger-fires tests
  using data-driven triggerCases arrays (already done in prior commit,
  this commit includes the Firmware mock cleanup).

* style(tests): remove inconsistent separator comment in RemoteStopUnlock

The listener section had a '// ───' separator not used in any of
the other 10 test files. The await describe block is sufficient.

* docs(tests): add summary line to startTransaction JSDoc

* docs(tests): add event listener testing section to TEST_STYLE_GUIDE

Add §11 documenting the established listener test pattern: emit()
direct, flushMicrotasks(), listenerCount first, accepted/rejected/error
triad, mock.method in beforeEach. Add listener station factories to
§9 mock factories table and flushMicrotasks to §10 utility table.

* docs(tests): fix incorrect mock API in §11 code example

Replace Jest-style mockImplementation() with Node.js test runner
mock.method() override pattern matching actual test code.

* fix(tests): align all 112 test files with TEST_STYLE_GUIDE

- Move @file JSDoc headers above first import in 3 files (GetVariables,
  MessageChannelUtils, Utils)
- Replace await Promise.resolve() with flushMicrotasks() in
  AutomaticTransactionGenerator
- Replace 6 setTimeout(resolve, 50) hacks with flushMicrotasks() in
  ChargingStationWorkerBroadcastChannel
- Document spec traceability prefix exception (G03.FR.xx, G04.INT.xx)
  in TEST_STYLE_GUIDE §1 naming conventions

* docs(tests): align TEST_STYLE_GUIDE with actual test infrastructure

- Fix createMockChargingStation location (ChargingStationTestUtils →
  helpers/StationHelpers)
- Add 7 widely-used factories to §9 table (10+ usages each)
- Remove unused expectAcceptedAuthorization from §9 auth table
- All locations verified against actual exports

* docs(tests): fix guide inconsistencies — deduplicate entries, fix assertion example

- Remove createMockCertificateManager from §10 OCPP 2.0 sub-table
  (already in §9 factory table — no duplication)
- Fix §9 usage example: assert.ok → assert.strictEqual (§7 compliance)
- Remove setupConnectorWithTransaction/clearConnectorTransaction from
  §10 lifecycle table (test setup helpers, not lifecycle utilities)
- Reorder §10: group cleanup/async utilities before spy factories

* docs(tests): fix guide precision — assert.ok scope, test script quotes, restore helpers

- Core Principles: clarify assert.ok is for boolean/existence only
- §5: add missing quotes around glob in test script (matches package.json)
- §10: restore setupConnectorWithTransaction/clearConnectorTransaction
  (27 usages across test suite — should not have been removed)
22 files changed:
tests/TEST_STYLE_GUIDE.md
tests/charging-station/AutomaticTransactionGenerator.test.ts
tests/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.test.ts
tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Firmware.test.ts
tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStartTransaction.test.ts
tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStopUnlock.test.ts
tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-TriggerMessage.test.ts
tests/charging-station/ocpp/1.6/OCPP16TestUtils.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CustomerInformation.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetLog.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetVariables.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RemoteStartAuth.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStopTransaction.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-TriggerMessage.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-UpdateFirmware.test.ts
tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent.test.ts
tests/charging-station/ocpp/2.0/OCPP20TestUtils.ts
tests/charging-station/ocpp/2.0/OCPP20VariableManager.test.ts
tests/utils/MessageChannelUtils.test.ts
tests/utils/Utils.test.ts