mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 06:30:42 +00:00
test: share anthropic payload fixtures
This commit is contained in:
@@ -11,6 +11,26 @@ type TestPayload = {
|
||||
system?: unknown;
|
||||
};
|
||||
|
||||
function textBlock(text: string, cache_control?: { type: "ephemeral"; ttl?: "1h" }) {
|
||||
return {
|
||||
type: "text",
|
||||
text,
|
||||
...(cache_control ? { cache_control } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function boundarySystemPayload(): TestPayload {
|
||||
return {
|
||||
system: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Stable prefix${SYSTEM_PROMPT_CACHE_BOUNDARY}Dynamic lab suffix`,
|
||||
},
|
||||
],
|
||||
messages: [{ role: "user", content: "Hello" }],
|
||||
};
|
||||
}
|
||||
|
||||
describe("anthropic payload policy", () => {
|
||||
it("applies native Anthropic service tier and cache markers without widening cache scope", () => {
|
||||
const policy = resolveAnthropicPayloadPolicy({
|
||||
@@ -45,16 +65,8 @@ describe("anthropic payload policy", () => {
|
||||
|
||||
expect(payload.service_tier).toBe("standard_only");
|
||||
expect(payload.system).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: "Follow policy.",
|
||||
cache_control: { type: "ephemeral", ttl: "1h" },
|
||||
},
|
||||
{
|
||||
type: "text",
|
||||
text: "Use tools carefully.",
|
||||
cache_control: { type: "ephemeral", ttl: "1h" },
|
||||
},
|
||||
textBlock("Follow policy.", { type: "ephemeral", ttl: "1h" }),
|
||||
textBlock("Use tools carefully.", { type: "ephemeral", ttl: "1h" }),
|
||||
]);
|
||||
expect(payload.messages[0]).toEqual({
|
||||
role: "assistant",
|
||||
@@ -91,13 +103,7 @@ describe("anthropic payload policy", () => {
|
||||
applyAnthropicPayloadPolicyToParams(payload, policy);
|
||||
|
||||
expect(payload).not.toHaveProperty("service_tier");
|
||||
expect(payload.system).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: "Follow policy.",
|
||||
cache_control: { type: "ephemeral" },
|
||||
},
|
||||
]);
|
||||
expect(payload.system).toEqual([textBlock("Follow policy.", { type: "ephemeral" })]);
|
||||
expect(payload.messages[0]).toEqual({
|
||||
role: "user",
|
||||
content: [{ type: "text", text: "Hello", cache_control: { type: "ephemeral" } }],
|
||||
@@ -112,28 +118,13 @@ describe("anthropic payload policy", () => {
|
||||
cacheRetention: "long",
|
||||
enableCacheControl: true,
|
||||
});
|
||||
const payload: TestPayload = {
|
||||
system: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Stable prefix${SYSTEM_PROMPT_CACHE_BOUNDARY}Dynamic lab suffix`,
|
||||
},
|
||||
],
|
||||
messages: [{ role: "user", content: "Hello" }],
|
||||
};
|
||||
const payload = boundarySystemPayload();
|
||||
|
||||
applyAnthropicPayloadPolicyToParams(payload, policy);
|
||||
|
||||
expect(payload.system).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: "Stable prefix",
|
||||
cache_control: { type: "ephemeral", ttl: "1h" },
|
||||
},
|
||||
{
|
||||
type: "text",
|
||||
text: "Dynamic lab suffix",
|
||||
},
|
||||
textBlock("Stable prefix", { type: "ephemeral", ttl: "1h" }),
|
||||
textBlock("Dynamic lab suffix"),
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -156,16 +147,8 @@ describe("anthropic payload policy", () => {
|
||||
applyAnthropicPayloadPolicyToParams(payload, policy);
|
||||
|
||||
expect(payload.system).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: "Follow policy.",
|
||||
cache_control: { type: "ephemeral", ttl: "1h" },
|
||||
},
|
||||
{
|
||||
type: "text",
|
||||
text: "Use tools carefully.",
|
||||
cache_control: { type: "ephemeral", ttl: "1h" },
|
||||
},
|
||||
textBlock("Follow policy.", { type: "ephemeral", ttl: "1h" }),
|
||||
textBlock("Use tools carefully.", { type: "ephemeral", ttl: "1h" }),
|
||||
]);
|
||||
expect(payload.messages[0]).toEqual({
|
||||
role: "user",
|
||||
@@ -188,13 +171,7 @@ describe("anthropic payload policy", () => {
|
||||
|
||||
applyAnthropicPayloadPolicyToParams(payload, policy);
|
||||
|
||||
expect(payload.system).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: "Follow policy.",
|
||||
cache_control: { type: "ephemeral" },
|
||||
},
|
||||
]);
|
||||
expect(payload.system).toEqual([textBlock("Follow policy.", { type: "ephemeral" })]);
|
||||
});
|
||||
|
||||
it("strips the boundary even when cache retention is disabled", () => {
|
||||
@@ -205,23 +182,10 @@ describe("anthropic payload policy", () => {
|
||||
cacheRetention: "none",
|
||||
enableCacheControl: true,
|
||||
});
|
||||
const payload: TestPayload = {
|
||||
system: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Stable prefix${SYSTEM_PROMPT_CACHE_BOUNDARY}Dynamic lab suffix`,
|
||||
},
|
||||
],
|
||||
messages: [{ role: "user", content: "Hello" }],
|
||||
};
|
||||
const payload = boundarySystemPayload();
|
||||
|
||||
applyAnthropicPayloadPolicyToParams(payload, policy);
|
||||
|
||||
expect(payload.system).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: "Stable prefix\nDynamic lab suffix",
|
||||
},
|
||||
]);
|
||||
expect(payload.system).toEqual([textBlock("Stable prefix\nDynamic lab suffix")]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user