mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 06:10:44 +00:00
test: share usage accumulator fixtures
This commit is contained in:
@@ -7,32 +7,44 @@ import {
|
||||
toNormalizedUsage,
|
||||
} from "./usage-accumulator.js";
|
||||
|
||||
type UsageInput = NonNullable<Parameters<typeof mergeUsageIntoAccumulator>[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(
|
||||
|
||||
Reference in New Issue
Block a user