Repository navigation
test: use shared response test helpers - #1930
Conversation
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
SummaryThe following content is AI-generated and provides a summary of the pull request: Refactor: Use Shared Test Helpers for UI Server and OCPP 2.0 Response Service TestsRefactor♻️ This PR consolidates duplicated test utilities across UI server and OCPP 2.0 response-service test suites into shared helpers. No production behavior is changed. Key cleanups:
Changes
GitHub Issues
PR Bot InformationVersion:
|
There was a problem hiding this comment.
The PR is well-structured and achieves its goal of eliminating code duplication. There is one minor issue: the emitRequest helper in TestableUIHttpServer uses Reflect.get to access httpServer unnecessarily, since the field is already protected and directly accessible — the existing TestableUIWebSocketServer equivalent shows the correct pattern.
PR Bot Information
Version: 1.26.11
- Correlation ID:
029a2b83-48ee-40a5-bea4-70dab5d30c90 - Agent Instructions:
- File Content Strategy: Full file content
- Event Trigger:
pull_request.opened - LLM:
anthropic--claude-4.6-sonnet
| public emitRequest (req: IncomingMessage, res: MockServerResponse): void { | ||
| const httpServer = Reflect.get(this, 'httpServer') as { | ||
| emit: (eventName: string, req: IncomingMessage, res: MockServerResponse) => boolean | ||
| } | ||
| httpServer.emit('request', req, res) | ||
| } |
There was a problem hiding this comment.
Unnecessary indirection: httpServer is protected on AbstractUIServer, so TestableUIHttpServer can access it directly as this.httpServer — no Reflect.get needed. Compare with TestableUIWebSocketServer.emitRequest in UIServerTestUtils.ts (line 72) which uses this.httpServer.emit(...) directly.
| public emitRequest (req: IncomingMessage, res: MockServerResponse): void { | |
| const httpServer = Reflect.get(this, 'httpServer') as { | |
| emit: (eventName: string, req: IncomingMessage, res: MockServerResponse) => boolean | |
| } | |
| httpServer.emit('request', req, res) | |
| } | |
| public emitRequest (req: IncomingMessage, res: MockServerResponse): void { | |
| this.httpServer.emit('request', req, res) | |
| } | |
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
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
Aligns OCPP 2.0 ResponseService test wiring with the established pattern used by RequestService and VariableManager: - New src/charging-station/ocpp/2.0/__testable__/OCPP20ResponseServiceTestable.ts exporting createTestableResponseService + TestableOCPP20ResponseService; mirrors OCPP20RequestServiceTestable.ts structure (function declarations, file-level JSDoc with @example, double-cast through unknown). - __testable__/index.ts re-exports the new module. - OCPP20ResponseService-TransactionEvent.test.ts and -ForceTxOnInvalid.test.ts now import createTestableResponseService from __testable__/index.js; call sites renamed (drops redundant OCPP20 prefix, consistent with createTestableRequestService). - OCPP20ResponseServiceTestUtils.ts now contains only the test-only buildTransactionEventRequest payload builder; adopts @file header and arrow-style export to harmonize with MockFactories.ts neighbour. Test-only refactor; no production behavior change. Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
Centralizes the two patterns used by OCPP 2.0 IncomingRequest tests to inject mock OCPPAuthService instances into the factory cache: - injectMockAuthService(station, service) — wraps the station-id lookup + OCPPAuthServiceFactory.setInstanceForTesting plumbing; replaces the duplicated boilerplate in ClearCache.test.ts and LocalAuthList.test.ts. - withThrowingAuthServiceFactory(errorMessage, fn) — scoped monkey-patch + try/finally restore for the 3 sites that must exercise a failing getInstance (factory unavailable, no Authorization Cache support). Required because setInstanceForTesting only injects a returned instance; it cannot make getInstance itself throw. Side effect of the helper adoption: - LocalAuthList.test.ts drops the suite-scoped originalGetInstance save and afterEach restore — no longer needed since every monkey-patch is now scoped to its own test via withThrowingAuthServiceFactory. - ClearCache.test.ts drops the local try/finally on the no-cache-support test for the same reason. Test-only refactor; no production behavior change. Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
V2-1: harmonize JSDoc verbs across the auth injection helpers — `inject*` docstrings now start with 'Inject' to match the function name, and the local `setupMockAuthService` wrappers (which build the mock + inject it) now start with 'Configure and inject' to reflect their broader role. Avoids the verb collision between the shared `injectMockAuthService` and the local `setupMockAuthService` helpers. V2-2: drop the dead `OCPPAuthService` return type from LocalAuthList's local `setupMockAuthService` — no call site captured the return value. Also drops the now-unused `OCPPAuthService` type import. Test-only refactor; no production behavior change. Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
Moves the `afterEach` block below `beforeEach` in ClearCache.test.ts to match the before-then-after ordering used by LocalAuthList, TransactionEvent and ForceTxOnInvalid test files. Cosmetic; no behavior change. Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
PR Checklist
Does this PR introduce a breaking change?
Description
This PR cleans up test helpers and removes test-side monkey-patching of internal factories:
await once(res, 'finish')waits with the existingawaitFinish()/drainResponses()helpers; add a localemitRequest()wrapper inUIHttpServer.test.tsfor access-gate request emission.buildTransactionEventRequestpayloadbuilder into
OCPP20ResponseServiceTestUtils.ts, and move thecreateTestableResponseServicewrapper into the standard
src/charging-station/ocpp/2.0/__testable__/location to match theOCPP20RequestService/OCPP20VariableManagerprecedent.Object.assign(OCPPAuthServiceFactory, …)to the propersetInstanceForTesting/clearAllInstancesinjection API, plus two shared helpersinjectMockAuthServiceandwithThrowingAuthServiceFactoryinMockFactories.ts.No production behavior is changed.
Validation
pnpm formatpnpm lintpnpm typecheckGIT_MASTER=1 git diff --check origin/main..HEADpnpm exec cross-env NODE_ENV=test node --import tsx --test --test-force-exit tests/charging-station/ui-server/UIHttpServer.test.ts tests/charging-station/ui-server/UIMetricsEndpoint.test.ts tests/charging-station/ui-server/UIMCPServer.test.ts tests/charging-station/ui-server/UIWebSocketServer.test.tspnpm exec cross-env NODE_ENV=test node --import tsx --test --test-force-exit tests/charging-station/ocpp/2.0/OCPP20ResponseService-TransactionEvent.test.ts tests/charging-station/ocpp/2.0/OCPP20ResponseService-ForceTxOnInvalid.test.ts tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-ClearCache.test.ts tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-LocalAuthList.test.ts