Files
Netcatty/infrastructure/ai/openAIChatCacheUsage.test.ts
T
陈大猫 df5c033dfa fix(ai): preserve Catty prompt cache and cache usage (#3714)
* fix(ai): preserve Catty prompt cache and provider usage

* fix(ai): gate explicit caching on the official Anthropic endpoint

* fix(ai): resolve provider defaults before enabling prompt caching
2026-10-18 12:20:18 +08:00

29 lines
1.3 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { normalizeOpenAIChatCacheUsage } from './openAIChatCacheUsage';
test('adapts documented DeepSeek cache usage while preserving the rest of the response', () => {
const response = { choices: [], usage: {
prompt_tokens: 1000, prompt_cache_hit_tokens: 900, prompt_cache_miss_tokens: 100,
prompt_tokens_details: { other: 1 }, completion_tokens: 5,
} };
const parsed = JSON.parse(normalizeOpenAIChatCacheUsage(JSON.stringify(response)));
assert.deepEqual(parsed, {
...response, usage: { ...response.usage, prompt_tokens_details: { other: 1, cached_tokens: 900 } },
});
});
test('preserves standard usage and rejects invalid cache counts without breaking stream data', () => {
for (const hit of [-1, 1.5, '900', 1001, null]) {
const data = JSON.stringify({ usage: { prompt_tokens: 1000, prompt_cache_hit_tokens: hit } });
assert.equal(normalizeOpenAIChatCacheUsage(data), data);
}
for (const data of ['[DONE]', 'not JSON', '{"usage":null}', '{"type":"message_start"}']) {
assert.equal(normalizeOpenAIChatCacheUsage(data), data);
}
const data = JSON.stringify({ usage: {
prompt_tokens: 1000, prompt_cache_hit_tokens: 900, prompt_tokens_details: { cached_tokens: 0 },
} });
assert.equal(normalizeOpenAIChatCacheUsage(data), data);
});