fix: tolerate omitted cache_read_tokens in live Surplus sales

This commit is contained in:
david
2026-08-28 12:06:43 +02:00
parent 59cac438f2
commit 0ed594f6ba
2 changed files with 20 additions and 2 deletions

View File

@@ -582,7 +582,7 @@ function parseSale(value: unknown, index: number): SurplusSale {
inputTokens: nonNegativeSafeInteger(item.input_tokens, `Surplus sale ${index}.input_tokens`),
outputTokens: nonNegativeSafeInteger(item.output_tokens, `Surplus sale ${index}.output_tokens`),
cacheReadTokens: nonNegativeSafeInteger(
item.cache_read_tokens,
item.cache_read_tokens ?? 0,
`Surplus sale ${index}.cache_read_tokens`,
),
effectiveInputUsdPer1m: nonNegativeNumber(

View File

@@ -334,6 +334,25 @@ describe("SurplusClient", () => {
);
expect(earnings.recentSales[0].createdAtMs).toBe(1787831146968);
});
it("defaults omitted cache_read_tokens to zero as the live API sends", async () => {
const sparse = saleRow("sale-sparse");
delete sparse.cache_read_tokens;
delete sparse.tx_hash;
const client = new SurplusClient({
apiBaseUrl: "https://api.surplusintelligence.ai",
sellerApiKey: "si_seller_test",
timeoutMs: 1_000,
fetchFn: async () => Response.json(earningsEnvelope([sparse])),
});
const earnings = await client.getEarnings();
expect(earnings.recentSales[0]).toMatchObject({
id: "sale-sparse",
cacheReadTokens: 0,
transactionHash: null,
});
});
it("returns the deduplicated observed sale window in deterministic sale-id order", async () => {
const duplicate = saleRow("sale-a", {
@@ -384,7 +403,6 @@ describe("SurplusClient", () => {
"seller_cost_usdc",
"input_tokens",
"output_tokens",
"cache_read_tokens",
"effective_input_per_1m",
"effective_output_per_1m",
];