Просмотр исходного кода

test: add regression test for setCacheKey option

Aiden Cline 2 месяцев назад
Родитель
Сommit
f9d0850c5e
1 измененных файлов с 70 добавлено и 0 удалено
  1. 70 0
      packages/opencode/test/provider/transform.test.ts

+ 70 - 0
packages/opencode/test/provider/transform.test.ts

@@ -3,6 +3,76 @@ import { ProviderTransform } from "../../src/provider/transform"
 
 const OUTPUT_TOKEN_MAX = 32000
 
+describe("ProviderTransform.options - setCacheKey", () => {
+  const sessionID = "test-session-123"
+
+  const mockModel = {
+    id: "anthropic/claude-3-5-sonnet",
+    providerID: "anthropic",
+    api: {
+      id: "claude-3-5-sonnet-20241022",
+      url: "https://api.anthropic.com",
+      npm: "@ai-sdk/anthropic",
+    },
+    name: "Claude 3.5 Sonnet",
+    capabilities: {
+      temperature: true,
+      reasoning: false,
+      attachment: true,
+      toolcall: true,
+      input: { text: true, audio: false, image: true, video: false, pdf: true },
+      output: { text: true, audio: false, image: false, video: false, pdf: false },
+      interleaved: false,
+    },
+    cost: {
+      input: 0.003,
+      output: 0.015,
+      cache: { read: 0.0003, write: 0.00375 },
+    },
+    limit: {
+      context: 200000,
+      output: 8192,
+    },
+    status: "active",
+    options: {},
+    headers: {},
+  } as any
+
+  test("should set promptCacheKey when providerOptions.setCacheKey is true", () => {
+    const result = ProviderTransform.options(mockModel, sessionID, { setCacheKey: true })
+    expect(result.promptCacheKey).toBe(sessionID)
+  })
+
+  test("should not set promptCacheKey when providerOptions.setCacheKey is false", () => {
+    const result = ProviderTransform.options(mockModel, sessionID, { setCacheKey: false })
+    expect(result.promptCacheKey).toBeUndefined()
+  })
+
+  test("should not set promptCacheKey when providerOptions is undefined", () => {
+    const result = ProviderTransform.options(mockModel, sessionID, undefined)
+    expect(result.promptCacheKey).toBeUndefined()
+  })
+
+  test("should not set promptCacheKey when providerOptions does not have setCacheKey", () => {
+    const result = ProviderTransform.options(mockModel, sessionID, {})
+    expect(result.promptCacheKey).toBeUndefined()
+  })
+
+  test("should set promptCacheKey for openai provider regardless of setCacheKey", () => {
+    const openaiModel = {
+      ...mockModel,
+      providerID: "openai",
+      api: {
+        id: "gpt-4",
+        url: "https://api.openai.com",
+        npm: "@ai-sdk/openai",
+      },
+    }
+    const result = ProviderTransform.options(openaiModel, sessionID, {})
+    expect(result.promptCacheKey).toBe(sessionID)
+  })
+})
+
 describe("ProviderTransform.maxOutputTokens", () => {
   test("returns 32k when modelLimit > 32k", () => {
     const modelLimit = 100000