Skip to content

Commit 55922d9

Browse files
committed
test(usage): pin the preview truncation boundary exactly
The bounded-payload case asserted only that the preview was a string of at most 65 characters, so an empty preview, a 63-character preview, and a 64-character preview with a spurious ellipsis all passed. The truncation boundary is now pinned at both sides: a 64-character counter is kept whole and a 65-character counter keeps its first 64 characters plus an ellipsis. Both cases fail on the previous head when the cap is off by one, and the 64-character case fails when the comparison is tightened to >=.
1 parent d9b68e7 commit 55922d9

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,37 @@ describe("normalizeChatUsage", () => {
308308
expect(meta).not.toHaveProperty("value")
309309
expect(meta.type).toBe("string")
310310
expect(meta.length).toBe(1_000_000)
311-
expect(typeof meta.preview).toBe("string")
312-
expect((meta.preview as string).length).toBeLessThanOrEqual(65)
311+
expect(meta.preview).toBe(`${"9".repeat(64)}…`)
313312
expect(JSON.stringify(meta).length).toBeLessThan(200)
314313
} finally {
315314
warn.mockRestore()
316315
}
317316
})
318317

318+
it.each([
319+
["exactly at the cap", 64, "9".repeat(64)],
320+
["one past the cap", 65, `${"9".repeat(64)}…`],
321+
])("keeps a preview %s without truncating or over-truncating", async (_name, length, expected) => {
322+
vi.resetModules()
323+
const { CompactLogger: FreshCompactLogger } = await import("../../../utils/logging/CompactLogger")
324+
const { normalizeChatUsage: freshNormalize } = await import("../chat-usage-normalization")
325+
const warn = vi.spyOn(FreshCompactLogger.prototype, "warn").mockImplementation(() => {})
326+
327+
try {
328+
freshNormalize({ prompt_tokens: 120, prompt_cache_miss_tokens: "9".repeat(length) })
329+
330+
const [, meta] = warn.mock.calls[0] as [string, Record<string, unknown>]
331+
332+
// A length-only assertion cannot tell an empty preview from a full
333+
// one, nor 64 characters from 65, so the boundary is pinned exactly.
334+
expect(meta.preview).toBe(expected)
335+
expect(meta.length).toBe(length)
336+
expect(meta).not.toHaveProperty("value")
337+
} finally {
338+
warn.mockRestore()
339+
}
340+
})
341+
319342
it.each([
320343
["a boolean", true, { type: "boolean" }],
321344
["an object", { tokens: 30 }, { type: "object" }],

0 commit comments

Comments
 (0)