Files
openclaw/extensions/deepinfra/cache-wrapper.test.ts
Georgi Atsev 9e7c2b356b fix(deepinfra): load all DeepInfra models when user wants to browse t… (#84549)
* fix(deepinfra): load all DeepInfra models when user wants to browse them during onboarding

* docs(deepinfra): align TTS default

* fix(deepinfra): refresh video fallbacks

* fix(deepinfra): share credential-aware catalog discovery

* test(deepinfra): narrow catalog regression types

* test(deepinfra): keep catalog narrowing across callback

* fix(deepinfra): preserve default model in live catalog

* fix(deepinfra): align default model pricing

* fix(deepinfra): keep pixverse as video default

* docs(deepinfra): match video fallback default

* fix(deepinfra): honor config api keys for live catalog

* test(e2e): wait for watchdog stdio close

* test(media): align live harness provider expectation

* fix(deepinfra): always augment custom catalogs

* test(e2e): resolve watchdog commands before spawning

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-05-27 10:43:37 +01:00

121 lines
3.4 KiB
TypeScript

import type { StreamFn } from "@earendil-works/pi-agent-core";
import type { Context, Model } from "@earendil-works/pi-ai";
import { describe, expect, it } from "vitest";
import { createDeepInfraAnthropicCacheWrapper } from "./cache-wrapper.js";
function capturePayload(params: {
modelId: string;
initialPayload: Record<string, unknown>;
}): { captured: Record<string, unknown>; baseCalls: number } {
let captured: Record<string, unknown> = {};
let baseCalls = 0;
const baseStreamFn: StreamFn = (_model, _context, options) => {
baseCalls += 1;
const payload = structuredClone(params.initialPayload);
options?.onPayload?.(payload, _model);
captured = payload;
return {} as ReturnType<StreamFn>;
};
const wrapped = createDeepInfraAnthropicCacheWrapper(baseStreamFn);
void wrapped(
{
api: "openai-completions",
provider: "deepinfra",
id: params.modelId,
reasoning: false,
} as Model<"openai-completions">,
{ messages: [] } as Context,
{} as never,
);
return { captured, baseCalls };
}
describe("createDeepInfraAnthropicCacheWrapper", () => {
it("injects ephemeral cache_control markers on the system message for anthropic/* models", () => {
const { captured, baseCalls } = capturePayload({
modelId: "anthropic/claude-sonnet-4-6",
initialPayload: {
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hi" },
],
},
});
expect(baseCalls).toBe(1);
expect(captured.messages).toEqual([
{
role: "system",
content: [
{
type: "text",
text: "You are a helpful assistant.",
cache_control: { type: "ephemeral" },
},
],
},
{ role: "user", content: "Hi" },
]);
});
it("tags the last block of an array-shaped system message", () => {
const { captured } = capturePayload({
modelId: "anthropic/claude-haiku-4-5",
initialPayload: {
messages: [
{
role: "system",
content: [
{ type: "text", text: "Block one" },
{ type: "text", text: "Block two" },
],
},
{ role: "user", content: "Hi" },
],
},
});
const messages = captured.messages as Array<{ role: string; content: unknown }>;
expect(messages[0]?.content).toEqual([
{ type: "text", text: "Block one" },
{
type: "text",
text: "Block two",
cache_control: { type: "ephemeral" },
},
]);
});
it("matches the anthropic/ prefix case-insensitively", () => {
const { captured } = capturePayload({
modelId: "Anthropic/Claude-Sonnet-4-6",
initialPayload: {
messages: [{ role: "system", content: "sys" }],
},
});
const messages = captured.messages as Array<{ content: unknown }>;
expect(messages[0]?.content).toEqual([
{ type: "text", text: "sys", cache_control: { type: "ephemeral" } },
]);
});
it("does not mutate payloads for non-anthropic model ids", () => {
const initialPayload = {
messages: [
{ role: "system", content: "sys" },
{ role: "user", content: "Hi" },
],
};
const { captured, baseCalls } = capturePayload({
modelId: "deepseek-ai/DeepSeek-V4-Flash",
initialPayload,
});
expect(baseCalls).toBe(1);
expect(captured).toEqual(initialPayload);
});
});