pi-mono/packages/coding-agent/test/export-html-cost.test.ts
Vegard Stikbakke cae8d69e57
fix(ai): use OpenRouter-reported total cost (#10286)
Use authoritative OpenRouter billing for Chat Completions and Anthropic Messages while retaining catalog component estimates and fallback behavior.

Fixes #9980
2026-10-22 11:01:39 +02:00

52 lines
1.6 KiB
TypeScript

import { readFileSync } from "node:fs";
import { runInNewContext } from "node:vm";
import { describe, expect, it } from "vitest";
type ExportStats = {
cost: {
input: number;
output: number;
cacheRead: number;
cacheWrite: number;
total: number;
};
};
describe("export HTML cost", () => {
it("uses recorded totals while falling back to component sums for old sessions", () => {
// Regression test for #9980: provider-reported totals may differ from catalog-estimated components.
const templateJs = readFileSync(new URL("../src/core/export-html/template.js", import.meta.url), "utf-8");
const functionStart = templateJs.indexOf("function computeStats(entryList)");
const functionEnd = templateJs.indexOf("const globalStats = computeStats(entries);");
expect(functionStart).toBeGreaterThanOrEqual(0);
expect(functionEnd).toBeGreaterThan(functionStart);
const context: { result?: ExportStats } = {};
runInNewContext(
`${templateJs.slice(functionStart, functionEnd)}
result = computeStats(${JSON.stringify([
{
type: "message",
message: {
role: "assistant",
content: [],
usage: { cost: { input: 0.1, output: 0.2, cacheRead: 0, cacheWrite: 0, total: 0.9 } },
},
},
{
type: "message",
message: {
role: "assistant",
content: [],
usage: { cost: { input: 0.4, output: 0.1, cacheRead: 0, cacheWrite: 0 } },
},
},
])});`,
context,
);
expect(context.result?.cost.input).toBeCloseTo(0.5);
expect(context.result?.cost.output).toBeCloseTo(0.3);
expect(context.result?.cost.total).toBeCloseTo(1.4);
});
});