From ab4eb5aa94a86300f76a345be6135d3167c71eb1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 19 Apr 2026 04:12:37 +0100 Subject: [PATCH] test: share anthropic cache payload fixture --- src/agents/anthropic-vertex-stream.test.ts | 110 ++++++--------------- 1 file changed, 32 insertions(+), 78 deletions(-) diff --git a/src/agents/anthropic-vertex-stream.test.ts b/src/agents/anthropic-vertex-stream.test.ts index 8e9717f718d..ef10048cf9a 100644 --- a/src/agents/anthropic-vertex-stream.test.ts +++ b/src/agents/anthropic-vertex-stream.test.ts @@ -90,6 +90,34 @@ function captureCacheBoundaryPayloadHook(onPayload: PayloadHook) { return { model, onPayload: transportOptions.onPayload }; } +function buildExpectedCacheBoundaryPayload(messageText: string) { + return { + system: [ + { + type: "text", + text: "Stable prefix", + cache_control: { type: "ephemeral" }, + }, + { + type: "text", + text: "Dynamic suffix", + }, + ], + messages: [ + { + role: "user", + content: [ + { + type: "text", + text: messageText, + cache_control: { type: "ephemeral" }, + }, + ], + }, + ], + }; +} + describe("createAnthropicVertexStreamFn", () => { beforeAll(async () => { ({ createAnthropicVertexStreamFn, createAnthropicVertexStreamFnForModel } = @@ -205,59 +233,9 @@ describe("createAnthropicVertexStreamFn", () => { const nextPayload = await transportPayloadHook?.(payload, model); - expect(onPayload).toHaveBeenCalledWith( - { - system: [ - { - type: "text", - text: "Stable prefix", - cache_control: { type: "ephemeral" }, - }, - { - type: "text", - text: "Dynamic suffix", - }, - ], - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Hello", - cache_control: { type: "ephemeral" }, - }, - ], - }, - ], - }, - model, - ); - expect(nextPayload).toEqual({ - system: [ - { - type: "text", - text: "Stable prefix", - cache_control: { type: "ephemeral" }, - }, - { - type: "text", - text: "Dynamic suffix", - }, - ], - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Hello", - cache_control: { type: "ephemeral" }, - }, - ], - }, - ], - }); + const expectedPayload = buildExpectedCacheBoundaryPayload("Hello"); + expect(onPayload).toHaveBeenCalledWith(expectedPayload, model); + expect(nextPayload).toEqual(expectedPayload); }); it("reapplies Anthropic cache-boundary shaping when payload hooks return a fresh payload", async () => { @@ -285,31 +263,7 @@ describe("createAnthropicVertexStreamFn", () => { model, ); - expect(nextPayload).toEqual({ - system: [ - { - type: "text", - text: "Stable prefix", - cache_control: { type: "ephemeral" }, - }, - { - type: "text", - text: "Dynamic suffix", - }, - ], - messages: [ - { - role: "user", - content: [ - { - type: "text", - text: "Hello again", - cache_control: { type: "ephemeral" }, - }, - ], - }, - ], - }); + expect(nextPayload).toEqual(buildExpectedCacheBoundaryPayload("Hello again")); }); it("omits maxTokens when neither the model nor request provide a finite limit", () => {