From bf5b6cba701066e2b4e8b3695ff95a6a18bac84f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 19 Apr 2026 03:37:25 +0100 Subject: [PATCH] test: share usage accumulator fixtures --- .../usage-accumulator.test.ts | 119 ++++++------------ 1 file changed, 40 insertions(+), 79 deletions(-) diff --git a/src/agents/pi-embedded-runner/usage-accumulator.test.ts b/src/agents/pi-embedded-runner/usage-accumulator.test.ts index bd4550d79d7..fc0a7f29ec0 100644 --- a/src/agents/pi-embedded-runner/usage-accumulator.test.ts +++ b/src/agents/pi-embedded-runner/usage-accumulator.test.ts @@ -7,32 +7,44 @@ import { toNormalizedUsage, } from "./usage-accumulator.js"; +type UsageInput = NonNullable[1]>; + +const FIRST_USAGE: UsageInput = { + input: 100, + output: 50, + cacheRead: 80_000, + cacheWrite: 5_000, + total: 85_150, +}; + +const SECOND_USAGE: UsageInput = { + input: 120, + output: 30, + cacheRead: 82_000, + cacheWrite: 0, + total: 82_150, +}; + +const FINAL_USAGE: UsageInput = { + input: 150, + output: 40, + cacheRead: 84_000, + cacheWrite: 0, + total: 84_190, +}; + +function createAccumulatorWithUsage(...usages: UsageInput[]) { + const acc = createUsageAccumulator(); + for (const usage of usages) { + mergeUsageIntoAccumulator(acc, usage); + } + return acc; +} + describe("usage-accumulator", () => { describe("mergeUsageIntoAccumulator", () => { it("accumulates usage across multiple API calls", () => { - const acc = createUsageAccumulator(); - - mergeUsageIntoAccumulator(acc, { - input: 100, - output: 50, - cacheRead: 80_000, - cacheWrite: 5_000, - total: 85_150, - }); - mergeUsageIntoAccumulator(acc, { - input: 120, - output: 30, - cacheRead: 82_000, - cacheWrite: 0, - total: 82_150, - }); - mergeUsageIntoAccumulator(acc, { - input: 150, - output: 40, - cacheRead: 84_000, - cacheWrite: 0, - total: 84_190, - }); + const acc = createAccumulatorWithUsage(FIRST_USAGE, SECOND_USAGE, FINAL_USAGE); expect(acc.input).toBe(370); expect(acc.output).toBe(120); @@ -42,22 +54,7 @@ describe("usage-accumulator", () => { }); it("stores the exact final call snapshot", () => { - const acc = createUsageAccumulator(); - - mergeUsageIntoAccumulator(acc, { - input: 100, - output: 50, - cacheRead: 80_000, - cacheWrite: 5_000, - total: 85_150, - }); - mergeUsageIntoAccumulator(acc, { - input: 150, - output: 40, - cacheRead: 84_000, - cacheWrite: 0, - total: 84_190, - }); + const acc = createAccumulatorWithUsage(FIRST_USAGE, FINAL_USAGE); expect(acc.lastInput).toBe(150); expect(acc.lastOutput).toBe(40); @@ -134,22 +131,7 @@ describe("usage-accumulator", () => { describe("toLastCallUsage", () => { it("returns the exact final call snapshot", () => { - const acc = createUsageAccumulator(); - - mergeUsageIntoAccumulator(acc, { - input: 100, - output: 50, - cacheRead: 80_000, - cacheWrite: 5_000, - total: 85_150, - }); - mergeUsageIntoAccumulator(acc, { - input: 150, - output: 40, - cacheRead: 84_000, - cacheWrite: 0, - total: 84_190, - }); + const acc = createAccumulatorWithUsage(FIRST_USAGE, FINAL_USAGE); expect(toLastCallUsage(acc)).toEqual({ input: 150, @@ -197,14 +179,7 @@ describe("usage-accumulator", () => { }); it("falls back to the accumulator when assistant usage is missing", () => { - const acc = createUsageAccumulator(); - mergeUsageIntoAccumulator(acc, { - input: 150, - output: 40, - cacheRead: 84_000, - cacheWrite: 0, - total: 84_190, - }); + const acc = createAccumulatorWithUsage(FINAL_USAGE); expect(resolveLastCallUsage(undefined, acc)).toEqual({ input: 150, @@ -216,14 +191,7 @@ describe("usage-accumulator", () => { }); it("falls back when assistant usage exists but is unusable", () => { - const acc = createUsageAccumulator(); - mergeUsageIntoAccumulator(acc, { - input: 150, - output: 40, - cacheRead: 84_000, - cacheWrite: 0, - total: 84_190, - }); + const acc = createAccumulatorWithUsage(FINAL_USAGE); expect(resolveLastCallUsage({ responseId: "abc" } as never, acc)).toEqual({ input: 150, @@ -235,14 +203,7 @@ describe("usage-accumulator", () => { }); it("keeps an explicit zero-usage raw snapshot instead of falling back", () => { - const acc = createUsageAccumulator(); - mergeUsageIntoAccumulator(acc, { - input: 150, - output: 40, - cacheRead: 84_000, - cacheWrite: 0, - total: 84_190, - }); + const acc = createAccumulatorWithUsage(FINAL_USAGE); expect( resolveLastCallUsage(