Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

openrouter-cost.spec.ts

43 lines1.4 KB

1import type { UsageCostBreakdown } from '@tanstack/ai'
2import { test, expect } from './fixtures'
3​
4/**
5 * Verifies that OpenRouter's provider-reported per-request cost reaches
6 * `RUN_FINISHED.usage`. The `/api/openrouter-cost` route drives the OpenRouter
7 * chat adapter against a hand-crafted aimock mount whose stream ends with a
8 * usage-only chunk carrying `cost` / `cost_details` (snake_case on the wire,
9 * camelCased by the SDK parser). The adapter defers RUN_FINISHED until the
10 * stream drains, so that trailing chunk is captured.
11 */
12test.describe('openrouter — per-request cost', () => {
13 test('cost and costDetails reach RUN_FINISHED.usage', async ({ request }) => {
14 const res = await request.post('/api/openrouter-cost')
15 expect(res.ok()).toBe(true)
16​
17 const { ok, usage, error } = (await res.json()) as {
18 ok: boolean
19 error?: string
20 usage?: {
21 promptTokens?: number
22 completionTokens?: number
23 totalTokens?: number
24 cost?: number
25 costDetails?: UsageCostBreakdown
26 }
27 }
28​
29 expect(error ?? null).toBeNull()
30 expect(ok).toBe(true)
31 expect(usage).toMatchObject({
32 promptTokens: 11,
33 completionTokens: 3,
34 totalTokens: 14,
35 cost: 0.0042,
36 costDetails: {
37 upstreamCost: 0.0038,
38 upstreamInputCost: 0.0012,
39 upstreamOutputCost: 0.0026,
40 },
41 })
42 })
43})