


























@@ -80,6 +80,30 @@ function createCapturingStreamFn(result = "stream") {
8080};
8181}
828283+function callArg(mock: { mock: { calls: unknown[][] } }, callIndex: number, argIndex: number) {
84+const call = mock.mock.calls[callIndex];
85+expect(call).toBeDefined();
86+return call?.[argIndex];
87+}
88+89+function fetchInit(fetchMock: { mock: { calls: unknown[][] } }, callIndex = 0): RequestInit {
90+const init = callArg(fetchMock, callIndex, 1);
91+expect(typeof init).toBe("object");
92+expect(init).not.toBeNull();
93+return init as RequestInit;
94+}
95+96+function streamContext(streamFn: { mock: { calls: unknown[][] } }, callIndex = 0) {
97+return callArg(streamFn, callIndex, 1) as {
98+systemPrompt?: unknown;
99+tools?: unknown;
100+};
101+}
102+103+function streamOptions(streamFn: { mock: { calls: unknown[][] } }, callIndex = 0) {
104+return callArg(streamFn, callIndex, 2) as Record<string, unknown>;
105+}
106+83107function preparePromptCacheStream(params: {
84108fetchMock: ReturnType<typeof vi.fn>;
85109now: number;
@@ -140,38 +164,28 @@ describe("google prompt cache", () => {
140164);
141165142166expect(fetchMock).toHaveBeenCalledTimes(1);
143-expect(fetchMock).toHaveBeenCalledWith(
167+expect(callArg(fetchMock, 0, 0)).toBe(
144168"https://generativelanguage.googleapis.com/v1beta/cachedContents",
145-expect.objectContaining({
146-method: "POST",
147-headers: expect.objectContaining({
148-"x-goog-api-key": "gemini-api-key",
149-"X-Provider": "google",
150-}),
151-}),
152169);
153-const createBody = JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body)) as Record<
154-string,
155-unknown
156->;
170+const createInit = fetchInit(fetchMock);
171+expect(createInit.method).toBe("POST");
172+const createHeaders = createInit.headers as Record<string, string>;
173+expect(createHeaders["x-goog-api-key"]).toBe("gemini-api-key");
174+expect(createHeaders["X-Provider"]).toBe("google");
175+expect(typeof createInit.body).toBe("string");
176+const createBody = JSON.parse(createInit.body as string) as Record<string, unknown>;
157177expect(createBody).toEqual({
158178model: "models/gemini-3.1-pro-preview",
159179ttl: "3600s",
160180systemInstruction: {
161181parts: [{ text: "Follow policy." }],
162182},
163183});
164-expect(innerStreamFn).toHaveBeenCalledWith(
165-expect.anything(),
166-expect.objectContaining({
167-systemPrompt: undefined,
168-tools: expect.any(Array),
169-}),
170-expect.objectContaining({ temperature: 0.2 }),
171-);
172-expect(getCapturedPayload()).toMatchObject({
173-cachedContent: "cachedContents/system-cache-1",
174-});
184+expect(innerStreamFn).toHaveBeenCalledTimes(1);
185+expect(streamContext(innerStreamFn).systemPrompt).toBeUndefined();
186+expect(Array.isArray(streamContext(innerStreamFn).tools)).toBe(true);
187+expect(streamOptions(innerStreamFn).temperature).toBe(0.2);
188+expect(getCapturedPayload()?.cachedContent).toBe("cachedContents/system-cache-1");
175189expect(entries).toHaveLength(1);
176190expect(entries[0]?.customType).toBe("openclaw.google-prompt-cache");
177191expect((entries[0]?.data as { status?: string; cachedContent?: string })?.status).toBe("ready");
@@ -209,14 +223,10 @@ describe("google prompt cache", () => {
209223);
210224211225expect(fetchMock).not.toHaveBeenCalled();
212-expect(innerStreamFn).toHaveBeenCalledWith(
213-expect.anything(),
214-expect.objectContaining({ systemPrompt: undefined }),
215-expect.any(Object),
216-);
217-expect(getCapturedPayload()).toMatchObject({
218-cachedContent: "cachedContents/system-cache-2",
219-});
226+expect(innerStreamFn).toHaveBeenCalledTimes(1);
227+expect(streamContext(innerStreamFn).systemPrompt).toBeUndefined();
228+expect(typeof streamOptions(innerStreamFn)).toBe("object");
229+expect(getCapturedPayload()?.cachedContent).toBe("cachedContents/system-cache-2");
220230});
221231222232it("refreshes an about-to-expire cache entry instead of creating a new one", async () => {
@@ -267,15 +277,11 @@ describe("google prompt cache", () => {
267277expect(String(fetchMock.mock.calls[0]?.[0])).toBe(
268278"https://generativelanguage.googleapis.com/v1beta/cachedContents/system-cache-3?updateMask=ttl",
269279);
270-expect(fetchMock.mock.calls[0]?.[1]).toMatchObject({ method: "PATCH" });
271-expect(innerStreamFn).toHaveBeenCalledWith(
272-expect.anything(),
273-expect.objectContaining({ systemPrompt: undefined }),
274-expect.any(Object),
275-);
276-expect(getCapturedPayload()).toMatchObject({
277-cachedContent: "cachedContents/system-cache-3",
278-});
280+expect(fetchInit(fetchMock).method).toBe("PATCH");
281+expect(innerStreamFn).toHaveBeenCalledTimes(1);
282+expect(streamContext(innerStreamFn).systemPrompt).toBeUndefined();
283+expect(typeof streamOptions(innerStreamFn)).toBe("object");
284+expect(getCapturedPayload()?.cachedContent).toBe("cachedContents/system-cache-3");
279285});
280286281287it("stays out of the way when cachedContent is already configured explicitly", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。