Skip to content

Commit 9cdcbee

Browse files
committed
test(openai-native): cover resolved streaming tiers
1 parent 4ca1971 commit 9cdcbee

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,78 @@ describe("OpenAiNativeHandler", () => {
145145
},
146146
)
147147

148+
it("prices SDK stream usage using the service tier resolved by OpenAI", async () => {
149+
mockResponsesCreate.mockResolvedValue({
150+
async *[Symbol.asyncIterator]() {
151+
yield {
152+
type: "response.done",
153+
response: {
154+
[SERVICE_TIER_KEY]: OpenAiServiceTier.Priority,
155+
usage: { input_tokens: 100, output_tokens: 20 },
156+
},
157+
}
158+
},
159+
})
160+
handler = new OpenAiNativeHandler({
161+
...mockOptions,
162+
apiModelId: "gpt-5.6-sol",
163+
openAiNativeServiceTier: OpenAiServiceTier.Default,
164+
})
165+
166+
const chunks = []
167+
for await (const chunk of handler.createMessage(systemPrompt, messages)) {
168+
chunks.push(chunk)
169+
}
170+
171+
expect(chunks).toContainEqual(
172+
expect.objectContaining({
173+
type: "usage",
174+
inputTokens: 100,
175+
outputTokens: 20,
176+
totalCost: 0.00275,
177+
}),
178+
)
179+
})
180+
181+
it("requests the configured tier but prices manual SSE fallback usage using OpenAI's resolved tier", async () => {
182+
mockResponsesCreate.mockRejectedValue(new Error("SDK not available"))
183+
const mockFetch = vitest.fn().mockResolvedValue({
184+
ok: true,
185+
body: new ReadableStream({
186+
start(controller) {
187+
controller.enqueue(
188+
new TextEncoder().encode(
189+
`data: ${JSON.stringify({
190+
type: "response.done",
191+
response: {
192+
[SERVICE_TIER_KEY]: OpenAiServiceTier.Priority,
193+
usage: { input_tokens: 100, output_tokens: 20 },
194+
},
195+
})}\n\n`,
196+
),
197+
)
198+
controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"))
199+
controller.close()
200+
},
201+
}),
202+
})
203+
global.fetch = mockFetch as typeof fetch
204+
handler = new OpenAiNativeHandler({
205+
...mockOptions,
206+
apiModelId: "gpt-5.6-sol",
207+
openAiNativeServiceTier: OpenAiServiceTier.Default,
208+
})
209+
210+
const chunks = []
211+
for await (const chunk of handler.createMessage(systemPrompt, messages)) {
212+
chunks.push(chunk)
213+
}
214+
215+
const [, request] = mockFetch.mock.calls[0]
216+
expect(JSON.parse(request.body)).toMatchObject({ [SERVICE_TIER_KEY]: OpenAiServiceTier.Default })
217+
expect(chunks).toContainEqual(expect.objectContaining({ type: "usage", totalCost: 0.00275 }))
218+
})
219+
148220
it("should handle streaming responses via Responses API", async () => {
149221
// Mock fetch for Responses API fallback
150222
const mockFetch = vitest.fn().mockResolvedValue({

0 commit comments

Comments
 (0)