11/**
22 * @file Tests for OCPP20IncomingRequestService GetLog
3- * @description Unit tests for OCPP 2.0.1 GetLog command handling (K01 ) and
3+ * @description Unit tests for OCPP 2.0.1 GetLog command handling (N01 ) and
44 * LogStatusNotification lifecycle simulation per N01
55 */
66
@@ -17,11 +17,13 @@ import {
1717 type OCPP20GetLogRequest ,
1818 type OCPP20GetLogResponse ,
1919 OCPP20IncomingRequestCommand ,
20+ OCPP20RequestCommand ,
2021 OCPPVersion ,
2122 UploadLogStatusEnumType ,
2223} from '../../../../src/types/index.js'
23- import { Constants } from '../../../../src/utils/index.js'
24+ import { Constants , logger } from '../../../../src/utils/index.js'
2425import {
26+ createLoggerMocks ,
2527 flushMicrotasks ,
2628 standardCleanup ,
2729 withMockTimers ,
@@ -30,7 +32,24 @@ import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConsta
3032import { createMockChargingStation } from '../../helpers/StationHelpers.js'
3133import { createMockStationWithRequestTracking } from './OCPP20TestUtils.js'
3234
33- await describe ( 'K01 - GetLog' , async ( ) => {
35+ interface LogUploadPlumbing {
36+ clearActiveLogUpload : ( chargingStation : ChargingStation , requestId : number ) => void
37+ getOrCreateStationState : ( chargingStation : ChargingStation ) => LogUploadStateShape
38+ simulateLogUploadLifecycle : ( chargingStation : ChargingStation , requestId : number ) => Promise < void >
39+ stationsState : WeakMap < ChargingStation , LogUploadStateShape >
40+ }
41+
42+ interface LogUploadStateShape {
43+ activeLogUploadAbortController ?: AbortController
44+ activeLogUploadRequestId ?: number
45+ activeLogUploadStatus ?: UploadLogStatusEnumType
46+ stopped ?: boolean
47+ }
48+
49+ const asPlumbing = ( service : OCPP20IncomingRequestService ) : LogUploadPlumbing =>
50+ service as unknown as LogUploadPlumbing
51+
52+ await describe ( 'N01 - GetLog' , async ( ) => {
3453 let station : ChargingStation
3554 let testableService : ReturnType < typeof createTestableIncomingRequestService >
3655
@@ -145,6 +164,25 @@ await describe('K01 - GetLog', async () => {
145164 assert . strictEqual ( simulateMock . mock . callCount ( ) , 0 )
146165 } )
147166
167+ await it ( 'should call simulateLogUploadLifecycle when GET_LOG event emitted with AcceptedCanceled response' , ( ) => {
168+ const request : OCPP20GetLogRequest = {
169+ log : {
170+ remoteLocation : 'https://csms.example.com/logs' ,
171+ } ,
172+ logType : LogEnumType . DiagnosticsLog ,
173+ requestId : 12 ,
174+ }
175+ const response : OCPP20GetLogResponse = {
176+ filename : 'simulator-log.txt' ,
177+ status : LogStatusEnumType . AcceptedCanceled ,
178+ }
179+
180+ service . emit ( OCPP20IncomingRequestCommand . GET_LOG , station , request , response )
181+
182+ assert . strictEqual ( simulateMock . mock . callCount ( ) , 1 )
183+ assert . strictEqual ( simulateMock . mock . calls [ 0 ] . arguments [ 1 ] , 12 )
184+ } )
185+
148186 await it ( 'should handle simulateLogUploadLifecycle rejection gracefully' , async ( ) => {
149187 mock . method (
150188 service as unknown as {
@@ -277,4 +315,143 @@ await describe('K01 - GetLog', async () => {
277315 } )
278316 } )
279317 } )
318+
319+ await describe ( 'N01 - GetLog supersession (N01.FR.12/FR.20)' , async ( ) => {
320+ await it ( 'should return AcceptedCanceled, abort the prior upload, and notify with the previous requestId' , ( ) => {
321+ // Arrange: seed an in-flight prior log upload (requestId 1)
322+ const { sentRequests, station : trackingStation } = createMockStationWithRequestTracking ( )
323+ const service = new OCPP20IncomingRequestService ( )
324+ const testable = createTestableIncomingRequestService ( service )
325+ const state = asPlumbing ( service ) . getOrCreateStationState ( trackingStation )
326+ const priorAbortController = new AbortController ( )
327+ state . activeLogUploadAbortController = priorAbortController
328+ state . activeLogUploadRequestId = 1
329+
330+ // Act: a new GetLog supersedes the in-flight upload
331+ const request : OCPP20GetLogRequest = {
332+ log : { remoteLocation : 'ftp://logs.example.com/' } ,
333+ logType : LogEnumType . DiagnosticsLog ,
334+ requestId : 2 ,
335+ }
336+ const response = testable . handleRequestGetLog ( trackingStation , request )
337+
338+ // Assert: superseded response, prior controller aborted, state reset
339+ assert . deepStrictEqual ( response , {
340+ filename : 'simulator-log.txt' ,
341+ status : LogStatusEnumType . AcceptedCanceled ,
342+ } )
343+ assert . strictEqual ( priorAbortController . signal . aborted , true )
344+ assert . strictEqual ( state . activeLogUploadRequestId , undefined )
345+ assert . strictEqual ( state . activeLogUploadAbortController , undefined )
346+ // Terminal LogStatusNotification(AcceptedCanceled) carries the PREVIOUS requestId (1), not the new one (2)
347+ assert . strictEqual ( sentRequests . length , 1 )
348+ assert . strictEqual ( sentRequests [ 0 ] . command , OCPP20RequestCommand . LOG_STATUS_NOTIFICATION )
349+ assert . deepStrictEqual ( sentRequests [ 0 ] . payload , {
350+ requestId : 1 ,
351+ status : UploadLogStatusEnumType . AcceptedCanceled ,
352+ } )
353+ } )
354+
355+ await it ( 'should swallow a failed AcceptedCanceled notification and still return AcceptedCanceled' , async t => {
356+ // Arrange: force the terminal LogStatusNotification send to reject
357+ const { station : trackingStation } = createMockStationWithRequestTracking ( )
358+ const service = new OCPP20IncomingRequestService ( )
359+ const testable = createTestableIncomingRequestService ( service )
360+ const { errorMock } = createLoggerMocks ( t , logger )
361+ ; (
362+ trackingStation . ocppRequestService as unknown as {
363+ requestHandler : ( ...args : unknown [ ] ) => Promise < unknown >
364+ }
365+ ) . requestHandler = async ( ) => Promise . reject ( new Error ( 'notification send failed' ) )
366+ const state = asPlumbing ( service ) . getOrCreateStationState ( trackingStation )
367+ state . activeLogUploadAbortController = new AbortController ( )
368+ state . activeLogUploadRequestId = 1
369+
370+ // Act
371+ const request : OCPP20GetLogRequest = {
372+ log : { remoteLocation : 'ftp://logs.example.com/' } ,
373+ logType : LogEnumType . DiagnosticsLog ,
374+ requestId : 2 ,
375+ }
376+ const response = testable . handleRequestGetLog ( trackingStation , request )
377+
378+ // Assert: handler still returns AcceptedCanceled synchronously; the rejection is not thrown
379+ assert . strictEqual ( response . status , LogStatusEnumType . AcceptedCanceled )
380+ // The rejection is swallowed by the .catch and logged at error
381+ await flushMicrotasks ( )
382+ assert . strictEqual ( errorMock . mock . calls . length , 1 )
383+ } )
384+ } )
385+
386+ await describe ( 'N01 - log upload lifecycle abort boundary' , async ( ) => {
387+ await it ( 'should exit at the interruptibleSleep boundary and not emit Uploaded when aborted mid-flight' , async t => {
388+ await withMockTimers ( t , [ 'setTimeout' ] , async ( ) => {
389+ // Arrange
390+ const { sentRequests, station : trackingStation } = createMockStationWithRequestTracking ( )
391+ const service = new OCPP20IncomingRequestService ( )
392+ const plumbing = asPlumbing ( service )
393+
394+ // Act: drive the lifecycle; it emits Uploading then parks at interruptibleSleep
395+ const lifecycle = plumbing . simulateLogUploadLifecycle ( trackingStation , 77 )
396+ await flushMicrotasks ( )
397+ assert . strictEqual ( sentRequests . length , 1 )
398+ assert . strictEqual ( sentRequests [ 0 ] . payload . status , UploadLogStatusEnumType . Uploading )
399+
400+ // Abort mid-flight at the sleep boundary. The requestId stays claimed so
401+ // the abort signal — not the requestId guard — is what suppresses Uploaded.
402+ const state = plumbing . stationsState . get ( trackingStation )
403+ state ?. activeLogUploadAbortController ?. abort ( )
404+
405+ // Advance past the step delay (LOG_UPLOAD_STEP_DELAY_MS) and settle the lifecycle
406+ t . mock . timers . tick ( 1000 )
407+ await flushMicrotasks ( )
408+ await lifecycle
409+
410+ // Uploaded must NOT be emitted — the lifecycle exited at the boundary
411+ assert . strictEqual ( sentRequests . length , 1 )
412+ assert . strictEqual (
413+ sentRequests . some ( request => request . payload . status === UploadLogStatusEnumType . Uploaded ) ,
414+ false
415+ )
416+ // The finally block ran: clearActiveLogUpload reset the claimed requestId
417+ assert . strictEqual ( state ?. activeLogUploadRequestId , undefined )
418+ } )
419+ } )
420+ } )
421+
422+ await describe ( 'clearActiveLogUpload' , async ( ) => {
423+ await it ( 'should log at debug and preserve state for a superseded (non-matching) requestId' , t => {
424+ // Arrange
425+ const service = new OCPP20IncomingRequestService ( )
426+ const plumbing = asPlumbing ( service )
427+ const debugMock = t . mock . method ( logger , 'debug' )
428+ const state = plumbing . getOrCreateStationState ( station )
429+ state . activeLogUploadRequestId = 200
430+
431+ // Act: a stale/superseded completion for requestId 199
432+ plumbing . clearActiveLogUpload ( station , 199 )
433+
434+ // Assert: else-branch fired the debug log and left state untouched
435+ assert . strictEqual ( debugMock . mock . callCount ( ) , 1 )
436+ assert . strictEqual ( state . activeLogUploadRequestId , 200 )
437+ } )
438+
439+ await it ( 'should reset state and not log for a matching requestId' , t => {
440+ // Arrange
441+ const service = new OCPP20IncomingRequestService ( )
442+ const plumbing = asPlumbing ( service )
443+ const debugMock = t . mock . method ( logger , 'debug' )
444+ const state = plumbing . getOrCreateStationState ( station )
445+ state . activeLogUploadAbortController = new AbortController ( )
446+ state . activeLogUploadRequestId = 200
447+
448+ // Act: the active upload completes cleanly
449+ plumbing . clearActiveLogUpload ( station , 200 )
450+
451+ // Assert: state cleared, no "Ignoring" debug log
452+ assert . strictEqual ( state . activeLogUploadRequestId , undefined )
453+ assert . strictEqual ( state . activeLogUploadAbortController , undefined )
454+ assert . strictEqual ( debugMock . mock . callCount ( ) , 0 )
455+ } )
456+ } )
280457} )
0 commit comments