|
| 1 | +// npx vitest run src/__tests__/prompt-cache-ttl.test.ts |
| 2 | + |
| 3 | +import { describe, it, expect } from "vitest" |
| 4 | + |
| 5 | +import { anthropicModels, anthropicProviderDefinition } from "../providers/anthropic.js" |
| 6 | +import { vertexModels } from "../providers/vertex.js" |
| 7 | +import { openRouterDefaultModelInfo } from "../providers/openrouter.js" |
| 8 | +import { |
| 9 | + vercelAiGatewayDefaultModelId, |
| 10 | + VERCEL_AI_GATEWAY_PROMPT_CACHING_MODELS, |
| 11 | +} from "../providers/vercel-ai-gateway.js" |
| 12 | +import { |
| 13 | + DEFAULT_PROMPT_CACHE_TTL, |
| 14 | + modelInfoSchema, |
| 15 | + promptCacheTtlSchema, |
| 16 | + promptCacheTtlValues, |
| 17 | + type ModelInfo, |
| 18 | +} from "../model.js" |
| 19 | +import { baseProviderSettingsShape } from "../providers/common.js" |
| 20 | +import { providerIdentifiers } from "../provider-identifiers.js" |
| 21 | +import { globalSettingsSchema } from "../global-settings.js" |
| 22 | + |
| 23 | +/** |
| 24 | + * Catalog invariant for the request-side TTL knob (ADR-0006 phase 2, PR-C5b). |
| 25 | + * |
| 26 | + * Emission is gated by `ModelInfo.promptCacheTtlSupport`, so a model that can |
| 27 | + * report a 1h cache write but carries no capability array would silently never |
| 28 | + * emit the TTL the user asked for. These tests pin both directions: capable |
| 29 | + * catalogs advertise it, and catalogs that cannot know stay absent (fail closed). |
| 30 | + */ |
| 31 | +describe("prompt cache TTL schema", () => { |
| 32 | + it("accepts exactly the 5m and 1h classes", () => { |
| 33 | + expect(promptCacheTtlValues).toEqual(["5m", "1h"]) |
| 34 | + expect(promptCacheTtlSchema.parse("5m")).toBe("5m") |
| 35 | + expect(promptCacheTtlSchema.parse("1h")).toBe("1h") |
| 36 | + expect(promptCacheTtlSchema.safeParse("30m").success).toBe(false) |
| 37 | + expect(promptCacheTtlSchema.safeParse("12h").success).toBe(false) |
| 38 | + }) |
| 39 | + |
| 40 | + it("defaults to undefined so an unset knob emits no ttl key", () => { |
| 41 | + expect(DEFAULT_PROMPT_CACHE_TTL).toBeUndefined() |
| 42 | + }) |
| 43 | + |
| 44 | + it("is optional on ModelInfo", () => { |
| 45 | + const parsed = modelInfoSchema.parse({ contextWindow: 200_000, supportsPromptCache: true }) |
| 46 | + expect(parsed.promptCacheTtlSupport).toBeUndefined() |
| 47 | + |
| 48 | + const withCapability = modelInfoSchema.parse({ |
| 49 | + contextWindow: 200_000, |
| 50 | + supportsPromptCache: true, |
| 51 | + promptCacheTtlSupport: ["5m", "1h"], |
| 52 | + }) |
| 53 | + expect(withCapability.promptCacheTtlSupport).toEqual(["5m", "1h"]) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +describe("prompt cache TTL knob location", () => { |
| 58 | + it("lives in provider settings so handlers can see it", () => { |
| 59 | + // ApiHandlerOptions = Omit<ProviderSettings, "apiProvider">, so only a |
| 60 | + // provider-settings key reaches a handler (ADR-0006 decision item 2). |
| 61 | + expect(baseProviderSettingsShape.promptCacheTtl).toBeDefined() |
| 62 | + |
| 63 | + const parsed = anthropicProviderDefinition.schema.safeParse({ |
| 64 | + apiProvider: providerIdentifiers.anthropic, |
| 65 | + promptCacheTtl: "1h", |
| 66 | + }) |
| 67 | + expect(parsed.success).toBe(true) |
| 68 | + expect(parsed.success && parsed.data.promptCacheTtl).toBe("1h") |
| 69 | + }) |
| 70 | + |
| 71 | + it("is not a global setting", () => { |
| 72 | + const shape = globalSettingsSchema.shape as Record<string, unknown> |
| 73 | + expect(shape.promptCacheTtl).toBeUndefined() |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +describe("prompt cache TTL capability catalog", () => { |
| 78 | + it("every Anthropic entry with prompt caching advertises both TTL classes", () => { |
| 79 | + const missing = Object.entries(anthropicModels as Record<string, ModelInfo>) |
| 80 | + .filter(([, info]) => info.supportsPromptCache && !info.promptCacheTtlSupport?.includes("1h")) |
| 81 | + .map(([id]) => id) |
| 82 | + |
| 83 | + expect(missing).toEqual([]) |
| 84 | + }) |
| 85 | + |
| 86 | + it("leaves the Vertex catalog without capability so it fails closed", () => { |
| 87 | + // PR-C5b scopes the catalog capability to the Anthropic entries; the Vertex |
| 88 | + // catalog is populated separately, so its models stay 5m-only for now. |
| 89 | + const withCapability = Object.entries(vertexModels as Record<string, ModelInfo>) |
| 90 | + .filter(([, info]) => info.promptCacheTtlSupport !== undefined) |
| 91 | + .map(([id]) => id) |
| 92 | + |
| 93 | + expect(withCapability).toEqual([]) |
| 94 | + }) |
| 95 | + |
| 96 | + it("leaves dynamic-catalog defaults without capability so they fail closed", () => { |
| 97 | + // OpenRouter and the gateway catalogs are fetched at runtime and carry no TTL |
| 98 | + // capability data, which ADR-0006 maps to "5m only" (invariant I2). |
| 99 | + expect(openRouterDefaultModelInfo.promptCacheTtlSupport).toBeUndefined() |
| 100 | + expect(VERCEL_AI_GATEWAY_PROMPT_CACHING_MODELS.has(vercelAiGatewayDefaultModelId)).toBe(true) |
| 101 | + }) |
| 102 | +}) |
0 commit comments