Skip to content

Commit b0567c5

Browse files
committed
test(usage): pin the malformed-counter warning and drop a dead fallback
What: make the alias path a required argument of asTokenCount, add a spec case that pins the warning message and the exact field names it reports, and document why the typeof guard stays in isTokenCount. Why: mutation testing on the helper left four surviving StringLiteral mutants and one NoCoverage mutant on the `path ?? "unknown"` fallback. The fallback was unreachable because every caller passes an alias, and the warning text and field names were unpinned, so a future edit could silently rename the diagnostic that makes a billing regression visible. Impact: no behavior change for valid or malformed counters. The diagnostic is now covered by a test. Mutation score on the helper: 65 killed, 1 survived (provably equivalent typeof guard), 0 no-coverage.
1 parent 65f6249 commit b0567c5

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎src/api/transform/__tests__/chat-usage-normalization.spec.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,28 @@ describe("normalizeChatUsage", () => {
213213
}
214214
})
215215

216+
it("names the exact field that was discarded", async () => {
217+
vi.resetModules()
218+
const { logger: freshLogger } = await import("../../../utils/logging")
219+
const { normalizeChatUsage: freshNormalize } = await import("../chat-usage-normalization")
220+
const warn = vi.spyOn(freshLogger, "warn").mockImplementation(() => {})
221+
222+
try {
223+
freshNormalize({ prompt_tokens: "100", completion_tokens: -1 })
224+
225+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("malformed token counter"), {
226+
path: "prompt_tokens",
227+
value: "100",
228+
})
229+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("malformed token counter"), {
230+
path: "completion_tokens",
231+
value: -1,
232+
})
233+
} finally {
234+
warn.mockRestore()
235+
}
236+
})
237+
216238
it("stays silent for counters the provider simply did not report", () => {
217239
const warn = vi.spyOn(logger, "warn").mockImplementation(() => {})
218240

‎src/api/transform/chat-usage-normalization.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ const readPath = (usage: ChatUsageLike, path: string): unknown => {
9393
* else (missing, null, a string, a float, a negative sentinel) is treated as
9494
* "not reported" so a malformed field falls through to the next alias instead
9595
* of poisoning the total.
96+
*
97+
* `typeof value === "number"` is redundant for the check itself, because
98+
* `Number.isInteger` already rejects every non-number. It is kept so the type
99+
* predicate narrows to `number` and callers can return the value typed.
96100
*/
97101
const isTokenCount = (value: unknown): value is number =>
98102
typeof value === "number" && Number.isInteger(value) && value >= 0
@@ -114,9 +118,9 @@ const warnMalformedCount = (path: string, value: unknown): void => {
114118

115119
const warnedMalformedCounters = new Set<string>()
116120

117-
const asTokenCount = (value: unknown, path?: string): number | undefined => {
121+
const asTokenCount = (value: unknown, path: string): number | undefined => {
118122
if (isTokenCount(value)) return value
119-
if (value !== undefined && value !== null) warnMalformedCount(path ?? "unknown", value)
123+
if (value !== undefined && value !== null) warnMalformedCount(path, value)
120124
return undefined
121125
}
122126

0 commit comments

Comments
 (0)