Skip to content

test(ocpp): cover OCPP 2.0.1 log/firmware lifecycle supersession and cleanup paths - #1997

Merged
jerome-benoit merged 1 commit into
mainfrom
test/ocpp20-lifecycle-supersession-coverage
Jul 11, 2026
Merged

jerome-benoit merged 1 commit into
mainfrom
test/ocpp20-lifecycle-supersession-coverage

Conversation

@jerome-benoit

@jerome-benoit jerome-benoit commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Regression-guard capstone for the OCPP 2.0.1 lifecycle supersession / cleanup work introduced by #1962 and hardened across #1976 / #1981 / #1983 / #1984 / #1991 / #1992 / #1994.

Test-only PR — zero changes under src/. Scope was defined by a per-gap coverage audit, then extended in review to complete the firmware↔log symmetry.

Closes #1974

Per-gap coverage

Gap Action
#1 log — clearActiveLogUpload else-branch +2 tests (superseded id → debug + preserve; matching id → reset + no log; also locks activeLogUploadStatus clearing)
#1 firmware — clearActiveFirmwareUpdate else-branch +2 tests (symmetric to the log pair)
#2 — handleRequestGetLog supersession +2 handler tests + 1 listener test (new-lifecycle-on-AcceptedCanceled)
#3 — handleRequestUpdateFirmware supersession already covered (UpdateFirmware.test.ts L327-368; abort behaviorally locked by the finally-guard test)
#4 — stop() integration +1 test (in-flight AbortControllers aborted + fields cleared)
#5 — log-upload mid-flight abort boundary +1 test (exits at interruptibleSleep, no Uploaded, finally cleanup ran)

Net: +9 tests (GetLog 9→15, PostStopResurrection 14→15, UpdateFirmware 27→29); 0 existing tests weakened or removed; full suite 0 failures (6 pre-existing skips).

Pre-existing test-quality fixes in the touched files (from the review):

  • Both lifecycle-rejection tests (GetLog + UpdateFirmware): replaced a stacked second mock.method(...) with simulateMock.mock.mockImplementation(...) (reconfigure the beforeEach mock, no double-wrap), and added an explicit errorMock assertion so the tests lock "rejection swallowed and logged at error" instead of passing vacuously.
  • GetLog Uploading test: assert.ok(length >= 1) → assert.strictEqual(length, 1).

OCPP 2.0.1 spec grounding (verified against the spec text)

Mutation-sanity (temporary local reverts, not committed) — 9 guarded lines verified

Each revert failed exactly its target test, then was restored:
handleRequestGetLog abort · simulateLogUploadLifecycle !signal.aborted guard · GET_LOG listener || AcceptedCanceled disjunct · resetStationState resetActive*State calls · simulateLogUploadLifecycle finally cleanup · clearActiveFirmwareUpdate else-branch debug · clearActiveFirmwareUpdate matching reset · resetActiveLogUploadState activeLogUploadStatus clear · GET_LOG listener logger.error level.

Note: handleRequestGetLog both aborts and resets the requestId (double guard), so the gap #5 test drives simulateLogUploadLifecycle directly and keeps the requestId claimed, isolating the !signal.aborted guard.

Review (findings-driven, ≥2 parallel reviewers per round; 4 rounds)

  • R1 (correctness + duplication): zero valid findings.
  • R2 (spec + TS + design/harmonization/terminology): K01→N01; supersession describe → N01 - GetLog supersession (N01.FR.12/FR.20); describes promoted to top-level; symmetric abort-controller assertion.
  • R3 (exhaustive ×3): AcceptedCanceled listener test, gap Bump @typescript-eslint/eslint-plugin from 5.24.0 to 5.25.0 #4 field-cleared assertions, gap Update issue templates #5 finally assertion; completed the clearActiveFirmwareUpdate symmetry; rejected a false positive (firmware supersession sends no notification).
  • R4 (exhaustive ×3): fixed the firmware rejection double-mock (symmetry with the GetLog fix), added errorMock assertions to both rejection tests, and the activeLogUploadStatus assertion. Reviewer findings targeting pre-existing UpdateFirmware tests (FR-label rewrites, unrelated assertion styles) were classified out-of-scope for this test-additions PR.

Verification gates

  • pnpm format / pnpm typecheck (0) / pnpm lint (0) clean on edited files.
  • pnpm test — full suite 0 failures (6 pre-existing skips); changed files 50 → 59 tests (+9); no existing test removed.
  • pnpm build — exit 0.

Does this PR introduce a breaking change?

  • Yes
  • No

@hyperspace-pr-bot

Copy link
Copy Markdown
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


PR Checklist

  • Please add a description of your changes.
  • Please ensure title follows conventional commits.
  • We need your changes to come with unit tests in order to keep this project in quality and easy to maintain.
  • If your changes contain any new feature please be sure to document it.
  • Please add a link to the open issue or task that this pull request will resolve.

Does this PR introduce a breaking change?

  • Yes
  • No

Test: OCPP 2.0.1 GetLog Supersession & Log-Upload Abort Coverage

Test-only PR — zero changes under src/. Adds +6 regression-guard tests to lock the OCPP 2.0.1 lifecycle supersession and cleanup behavior introduced by #1962 and hardened across subsequent PRs.

What's covered

OCPP20IncomingRequestService-GetLog.test.ts (+5 tests)

Gap #2 — handleRequestGetLog supersession (N01.FR.12):

  • Returns AcceptedCanceled, aborts the prior activeLogUploadAbortController (signal.aborted === true), resets activeLogUploadRequestId, and sends LogStatusNotification(AcceptedCanceled) carrying the previous requestId.
  • A rejecting LogStatusNotification send is swallowed by .catch (logged at error) — handler still returns AcceptedCanceled, no throw.

Gap #5 — Log-upload lifecycle abort boundary (N01):

  • Drives simulateLogUploadLifecycle to its interruptibleSleep boundary, aborts the captured controller, advances the fake clock, and asserts Uploaded is not emitted.

Gap #1 (log) — clearActiveLogUpload:

  • Superseded (non-matching) requestId → debug log fires and state is preserved.
  • Matching requestId → state is reset and no debug log is emitted.

OCPP20IncomingRequestService-PostStopResurrection.test.ts (+1 test)

Gap #4 — resetStationState abort-on-stop:

  • In-flight firmware and log upload AbortControllers are both aborted (signal.aborted === true) by stop().

Per-gap audit summary

Gap Status Action
#1 firmware clearActiveFirmwareUpdate else-branch ✅ Already covered None
#1 log clearActiveLogUpload else-branch ➕ Added 2 new tests
#2 handleRequestGetLog supersession ➕ Added 2 new tests
#3 handleRequestUpdateFirmware supersession ✅ Already covered None
#4 stop() AbortController abort assertion ➕ Added 1 new test
#5 Log-upload AbortController mid-flight abort boundary ➕ Added 1 new test

Verification

  • pnpm format / pnpm typecheck / pnpm lint — all clean
  • pnpm test — 2996 → 3002 tests, 0 fail, 6 pre-existing skips (delta = +6)
  • pnpm build — exit 0

Closes #1974


  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.27.6

@hyperspace-pr-bot hyperspace-pr-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR adds 6 well-structured regression tests covering the OCPP 2.0.1 GetLog supersession and abort-boundary gaps. The tests are clearly organized with good AAA comments and proper use of the project's helper infrastructure. Three issues were identified: the clearActiveLogUpload tests reuse the outer beforeEach station with a different service instance (isolation risk), an inconsistency between calls.length and the canonical callCount() mock API, and a subtle assumption that the state reset in the supersession test is synchronous that should be made explicit.

PR Bot Information

Version: 1.27.6

Comment on lines +401 to +407
await describe('clearActiveLogUpload', async () => {
await it('should log at debug and preserve state for a superseded (non-matching) requestId', t => {
// Arrange
const service = new OCPP20IncomingRequestService()
const plumbing = asPlumbing(service)
const debugMock = t.mock.method(logger, 'debug')
const state = plumbing.getOrCreateStationState(station)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: station from outer beforeEach is used inside a nested describe that creates its own service instance, but the station belongs to a different service instance.

In the clearActiveLogUpload tests (lines 402–434), station refers to the outer beforeEach-created station, while service is a freshly-constructed new OCPP20IncomingRequestService(). The outer service (used in testableService) and this inner service maintain separate stationsState WeakMaps, so plumbing.getOrCreateStationState(station) seeds state in the inner service's map. However, the outer station was created by the outer beforeEach, making the test dependent on the outer setup surviving into nested describe scope — this is fragile and couples tests to outer lifecycle fixtures.

More critically, the outer station is reset on every afterEach via standardCleanup(), but the inner service is not the one that owns it. If test ordering ever changes or the outer station reference becomes stale, assertions on state.activeLogUploadRequestId could silently pass on a wrong/shared state object.

Consider using a locally-created station inside each clearActiveLogUpload test (same pattern used by PostStopResurrection.test.ts's createStation()) for full isolation:

Suggested change
await describe('clearActiveLogUpload', async () => {
await it('should log at debug and preserve state for a superseded (non-matching) requestId', t => {
// Arrange
const service = new OCPP20IncomingRequestService()
const plumbing = asPlumbing(service)
const debugMock = t.mock.method(logger, 'debug')
const state = plumbing.getOrCreateStationState(station)
await describe('clearActiveLogUpload', async () => {
await it('should log at debug and preserve state for a superseded (non-matching) requestId', t => {
// Arrange
const { station: localStation } = createMockStationWithRequestTracking()
const service = new OCPP20IncomingRequestService()
const plumbing = asPlumbing(service)
const debugMock = t.mock.method(logger, 'debug')
const state = plumbing.getOrCreateStationState(localStation)
state.activeLogUploadRequestId = 200
// Act: a stale/superseded completion for requestId 199
plumbing.clearActiveLogUpload(localStation, 199)
// Assert: else-branch fired the debug log and left state untouched
assert.strictEqual(debugMock.mock.callCount(), 1)
assert.strictEqual(state.activeLogUploadRequestId, 200)
})
await it('should reset state and not log for a matching requestId', t => {
// Arrange
const { station: localStation } = createMockStationWithRequestTracking()
const service = new OCPP20IncomingRequestService()
const plumbing = asPlumbing(service)
const debugMock = t.mock.method(logger, 'debug')
const state = plumbing.getOrCreateStationState(localStation)
state.activeLogUploadAbortController = new AbortController()
state.activeLogUploadRequestId = 200
// Act: the active upload completes cleanly
plumbing.clearActiveLogUpload(localStation, 200)
// Assert: state cleared, no "Ignoring" debug log
assert.strictEqual(state.activeLogUploadRequestId, undefined)
assert.strictEqual(state.activeLogUploadAbortController, undefined)
assert.strictEqual(debugMock.mock.callCount(), 0)
})
})

Double-check suggestion before committing. Edit this comment for amendments.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

status: LogStatusEnumType.AcceptedCanceled,
})
assert.strictEqual(priorAbortController.signal.aborted, true)
assert.strictEqual(state.activeLogUploadRequestId, undefined)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic Error: The assertion on state.activeLogUploadRequestId at line 324 passes even if the supersession path never resets the requestId, because the LogStatusNotification send (which fires the .catch — see line 344) completes asynchronously. The assertion is checked synchronously before any microtasks drain, so activeLogUploadRequestId being undefined at this point is only guaranteed if the reset happens synchronously inside handleRequestGetLog before the notification is fired off.

This is fine if the implementation does reset synchronously, but the test does not verify this assumption with a comment. More importantly, if the implementation is ever changed so that the reset happens in a .then() continuation, this assertion would silently become a false positive.

The test should either add an explicit comment that the reset is synchronous, or add await flushMicrotasks() before this assertion and verify the state holds after async settling.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

assert.strictEqual(response.status, LogStatusEnumType.AcceptedCanceled)
// The rejection is swallowed by the .catch and logged at error
await flushMicrotasks()
assert.strictEqual(errorMock.mock.calls.length, 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic Error: errorMock.mock.calls.length is accessed using .length, but createLoggerMocks returns a LoggerMockResult whose errorMock type has type { mock: { calls: MockCall[] } }. This is consistent. However, in the sibling clearActiveLogUpload tests (lines 414, 433), t.mock.method(logger, 'debug') is used directly to access debugMock.mock.callCount() with the callCount() method — while here errorMock.mock.calls.length uses the .length property. These two inconsistent patterns for checking mock call counts make the test suite harder to maintain and can mask differences in the mock API.

The style guide mandates strict assertions. callCount() is a method on Node's MockFunctionContext and is the canonical API. Should use .mock.callCount() consistently rather than mixing .mock.calls.length.

Suggested change
assert.strictEqual(errorMock.mock.calls.length, 1)
assert.strictEqual(errorMock.mock.callCount(), 1)

Double-check suggestion before committing. Edit this comment for amendments.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

@jerome-benoit
jerome-benoit force-pushed the test/ocpp20-lifecycle-supersession-coverage branch 3 times, most recently from c53ba4c to 93af283 Compare July 11, 2026 22:34
@jerome-benoit jerome-benoit changed the title test(ocpp): cover OCPP 2.0.1 GetLog supersession and log-upload abort paths test(ocpp): cover OCPP 2.0.1 log/firmware lifecycle supersession and cleanup paths Jul 11, 2026
@jerome-benoit
jerome-benoit force-pushed the test/ocpp20-lifecycle-supersession-coverage branch from 93af283 to 6556ced Compare July 11, 2026 22:52
@jerome-benoit
jerome-benoit force-pushed the test/ocpp20-lifecycle-supersession-coverage branch from 6556ced to a131b61 Compare July 11, 2026 23:03
@jerome-benoit
jerome-benoit merged commit aa8e402 into main Jul 11, 2026
44 checks passed
@jerome-benoit
jerome-benoit deleted the test/ocpp20-lifecycle-supersession-coverage branch October 29, 2026 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant