Commit 1a4fde6
authored
refactor(token-tracker-http): decompose 238-line trackTokenUsage into testable top-level functions (#4937)
* Initial plan
* refactor: decompose trackTokenUsage into testable sub-functions
Extract the two inner functions from the 238-line trackTokenUsage closure
into top-level functions that accept explicit state:
- initHttpState({ streaming, compressed, contentType, contentEncoding })
Initialises the mutable tracking state object so it can be constructed
independently in tests.
- createChunkHandler(state, { requestId, provider }) → handleDecodedChunk
Returns the decoded-chunk handler; mutates `state` via explicit reference
instead of closing over outer-scope variables.
- wireListeners(proxyRes, decompressor, state, onChunk, onFinalize)
Encapsulates event-listener wiring (decompressed and uncompressed paths).
- finalizeHttpTracking(state, proxyRes, opts)
The former inner `finalizeTracking` closure, now taking all inputs as
explicit parameters so it can be unit-tested with a synthetic state object
and a minimal `{ statusCode }` proxyRes mock.
trackTokenUsage is now ~30 lines (init → create handler → wire → finalize).
The public module API is unchanged; token-tracker.js requires no updates.
New exports: createChunkHandler, finalizeHttpTracking (alongside trackTokenUsage).
New test file: token-tracker-http.unit.test.js — 18 unit tests covering both
sub-functions directly without constructing a full HTTP response stream.
All 1062 existing tests continue to pass.
Closes #4934
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>1 parent e71c006 commit 1a4fde6
2 files changed
Lines changed: 601 additions & 194 deletions
0 commit comments