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

billing.test.ts

138 lines4.2 KB

1import { describe, expect, it, vi } from 'vitest'
2import {
3 buildFalUsage,
4 createBillingFetch,
5 parseBillableUnits,
6 recordBillableUnitsFromResponse,
7 takeBillableUnits,
8} from '../src/utils/billing'
9​
10function resultResponse(
11 headers: Record<string, string>,
12 body: BodyInit | null = null,
13): Response {
14 return new Response(body, { headers })
16​
17describe('parseBillableUnits', () => {
18 it('parses integer and fractional values', () => {
19 expect(parseBillableUnits('4')).toBe(4)
20 expect(parseBillableUnits('1.5')).toBe(1.5)
21 expect(parseBillableUnits('0')).toBe(0)
22 })
23​
24 it('returns undefined for missing or non-numeric values', () => {
25 expect(parseBillableUnits(null)).toBeUndefined()
26 expect(parseBillableUnits('')).toBeUndefined()
27 expect(parseBillableUnits('not-a-number')).toBeUndefined()
28 })
29})
30​
31describe('recordBillableUnitsFromResponse / takeBillableUnits', () => {
32 it('records billable units keyed by the request id header', () => {
33 recordBillableUnitsFromResponse(
34 resultResponse({
35 'x-fal-request-id': 'req-record-1',
36 'x-fal-billable-units': '6',
37 }),
38 )
39​
40 expect(takeBillableUnits('req-record-1')).toBe(6)
41 })
42​
43 it('removes the entry on read so it is consumed once', () => {
44 recordBillableUnitsFromResponse(
45 resultResponse({
46 'x-fal-request-id': 'req-record-2',
47 'x-fal-billable-units': '3',
48 }),
49 )
50​
51 expect(takeBillableUnits('req-record-2')).toBe(3)
52 expect(takeBillableUnits('req-record-2')).toBeUndefined()
53 })
54​
55 it('ignores responses without a billable-units header', () => {
56 recordBillableUnitsFromResponse(
57 resultResponse({ 'x-fal-request-id': 'req-record-3' }),
58 )
59​
60 expect(takeBillableUnits('req-record-3')).toBeUndefined()
61 })
62​
63 it('ignores responses with billable units but no request id', () => {
64 // No request id means there is nothing to correlate the units against.
65 expect(() =>
66 recordBillableUnitsFromResponse(
67 resultResponse({ 'x-fal-billable-units': '9' }),
68 ),
69 ).not.toThrow()
70 })
71​
72 it('returns undefined for an unknown or missing request id', () => {
73 expect(takeBillableUnits('never-recorded')).toBeUndefined()
74 expect(takeBillableUnits(undefined)).toBeUndefined()
75 })
76})
77​
78describe('buildFalUsage', () => {
79 it('returns undefined when no units were captured', () => {
80 expect(buildFalUsage(undefined)).toBeUndefined()
81 })
82​
83 it('surfaces billable units on a zero-token usage object', () => {
84 expect(buildFalUsage(4)).toEqual({
85 promptTokens: 0,
86 completionTokens: 0,
87 totalTokens: 0,
88 billed: { quantity: 4, unit: 'units' },
89 unitsBilled: 4,
90 })
91 })
92​
93 it('preserves a zero billed quantity', () => {
94 expect(buildFalUsage(0)).toEqual({
95 promptTokens: 0,
96 completionTokens: 0,
97 totalTokens: 0,
98 billed: { quantity: 0, unit: 'units' },
99 unitsBilled: 0,
100 })
101 })
102})
103​
104describe('createBillingFetch', () => {
105 it('records the header and returns the response with its body intact', async () => {
106 const mockResponse = resultResponse(
107 {
108 'x-fal-request-id': 'req-fetch-1',
109 'x-fal-billable-units': '7',
110 'content-type': 'application/json',
111 },
112 JSON.stringify({ ok: true }),
113 )
114 // Inject the underlying fetch directly — no global to stub or restore.
115 const baseFetch = vi.fn().mockResolvedValue(mockResponse)
116​
117 const billingFetch = createBillingFetch(baseFetch)
118 const returned = await billingFetch(
119 'https://queue.fal.run/fal-ai/flux/requests/req-fetch-1',
120 )
121​
122 expect(returned).toBe(mockResponse)
123 expect(baseFetch).toHaveBeenCalledTimes(1)
124 // Reading headers must not consume the body — the fal client still parses it.
125 await expect(returned.json()).resolves.toEqual({ ok: true })
126 expect(takeBillableUnits('req-fetch-1')).toBe(7)
127 })
128​
129 it('passes through responses without the billable-units header', async () => {
130 const mockResponse = resultResponse({ 'x-fal-request-id': 'req-fetch-2' })
131 const baseFetch = vi.fn().mockResolvedValue(mockResponse)
132​
133 const billingFetch = createBillingFetch(baseFetch)
134 await billingFetch('https://queue.fal.run/fal-ai/flux/requests/req-fetch-2')
135​
136 expect(takeBillableUnits('req-fetch-2')).toBeUndefined()
137 })
138})