Skip to content

Commit 8d1d1e4

Browse files
committed
test(api): pin the cache-key gate on the streaming path and SDK defaults
What: - openai-native-usage.spec.ts: add a createMessage test with codex-mini-latest and a task id, asserting the SDK request body omits prompt_cache_key. The spec now stubs the openai client so the public entry point is drivable. - openai-native.spec.ts: extend the SDK-path header test to also assert the OpenAI constructor's defaultHeaders carry neither session_id nor prompt_cache_key. Why: Both existing assertions stopped one level short of the surface that ships. The builder-level model-gate test cannot see createMessage, which forwards metadata into buildRequestBody, so a key injected anywhere along that path still passed. The per-request header assertion cannot see defaultHeaders, so a restored affinity header there would ship on every request while the test stayed green. Each new assertion was proven to fail against a mutant that plants exactly the value it forbids (streaming-path key injection; session_id and prompt_cache_key in defaultHeaders), and each mutant was killed by the new assertion alone. Impact: Test-only. Zero production lines; prompt_cache_key semantics unchanged. No new ESLint suppressions, no changeset.
1 parent 54504a2 commit 8d1d1e4

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎src/api/providers/__tests__/openai-native-usage.spec.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import { describe, it, expect, beforeEach } from "vitest"
22
import { OpenAiNativeHandler } from "../openai-native"
33
import { OpenAiServiceTier, openAiNativeModels } from "@roo-code/types"
4+
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
5+
6+
// The streaming entry point reaches the SDK client, so stub the client surface the
7+
// handler actually calls. Without this the real SDK would attempt a network request.
8+
const mockResponsesCreate = vitest.fn()
9+
10+
vitest.mock("openai", () => ({
11+
__esModule: true,
12+
default: vitest.fn().mockImplementation(function () {
13+
return {
14+
responses: {
15+
create: mockResponsesCreate,
16+
},
17+
}
18+
}),
19+
}))
420

521
describe("OpenAiNativeHandler - normalizeUsage", () => {
622
let handler: OpenAiNativeHandler
@@ -491,6 +507,32 @@ describe("OpenAiNativeHandler - normalizeUsage", () => {
491507
expect(body.prompt_cache_key).toBeUndefined()
492508
})
493509

510+
it("should omit the key for a non-caching model through the streaming entry point", async () => {
511+
// The builder-level test above cannot see the streaming path: createMessage forwards
512+
// metadata into buildRequestBody, so a key added anywhere along that path would still
513+
// pass it. Drive the public entry point with a task id to pin the model gate there too.
514+
const modelId = "codex-mini-latest"
515+
expect(openAiNativeModels[modelId as keyof typeof openAiNativeModels].supportsPromptCache).toBe(false)
516+
517+
mockResponsesCreate.mockClear()
518+
mockResponsesCreate.mockResolvedValue(asyncStreamFrom([]))
519+
520+
const streamingHandler = new OpenAiNativeHandler({
521+
openAiNativeApiKey: "test-key",
522+
apiModelId: modelId,
523+
})
524+
525+
await collectStream(
526+
streamingHandler.createMessage("You are a helpful assistant.", [{ role: "user", content: "Hello!" }], {
527+
taskId: "task-abc",
528+
}),
529+
)
530+
531+
expect(mockResponsesCreate).toHaveBeenCalledTimes(1)
532+
const [body] = mockResponsesCreate.mock.calls[0]
533+
expect(body).not.toHaveProperty("prompt_cache_key")
534+
})
535+
494536
it("should clamp an over-length task id to OpenAI's 64-character limit", () => {
495537
const longTaskId = "t".repeat(80)
496538
const body = buildRequestBody("gpt-5.1", longTaskId)

‎src/api/providers/__tests__/openai-native.spec.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,7 @@ describe("OpenAiNativeHandler", () => {
15081508

15091509
it("sends the task-scoped prompt_cache_key through the SDK path", async () => {
15101510
mockResponsesCreate.mockResolvedValue(asyncStreamFrom([]))
1511+
;(OpenAI as unknown as ReturnType<typeof vitest.fn>).mockClear()
15111512

15121513
const cacheHandler = new OpenAiNativeHandler({ ...mockOptions, apiModelId: "gpt-5.1" })
15131514

@@ -1521,6 +1522,14 @@ describe("OpenAiNativeHandler", () => {
15211522
expect(bodies[2].prompt_cache_key).toBe("sdk-task-two")
15221523
expect(bodies[0].prompt_cache_retention).toBe("24h")
15231524
expect(mockResponsesCreate.mock.calls[0][1].headers).not.toHaveProperty("session_id")
1525+
1526+
// Per-request headers are only half the surface: the SDK constructor also takes
1527+
// defaultHeaders, and a restored affinity header there would ship on every request
1528+
// while the assertion above still passed. Cache affinity is a body field, so neither
1529+
// the header name nor the body field name may appear in the constructor's defaults.
1530+
const constructorArgs = (OpenAI as unknown as ReturnType<typeof vitest.fn>).mock.calls[0][0]
1531+
expect(constructorArgs.defaultHeaders).not.toHaveProperty("session_id")
1532+
expect(constructorArgs.defaultHeaders).not.toHaveProperty("prompt_cache_key")
15241533
})
15251534

15261535
it("omits the prompt_cache_key through createMessage when no metadata is supplied", async () => {

0 commit comments

Comments
 (0)