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

billing.test.ts

136 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 })
90 })
91​
92 it('preserves a zero billed quantity', () => {
93 expect(buildFalUsage(0)).toEqual({
94 promptTokens: 0,
95 completionTokens: 0,
96 totalTokens: 0,
97 billed: { quantity: 0, unit: 'units' },
98 })
99 })
100})
101​
102describe('createBillingFetch', () => {
103 it('records the header and returns the response with its body intact', async () => {
104 const mockResponse = resultResponse(
105 {
106 'x-fal-request-id': 'req-fetch-1',
107 'x-fal-billable-units': '7',
108 'content-type': 'application/json',
109 },
110 JSON.stringify({ ok: true }),
111 )
112 // Inject the underlying fetch directly — no global to stub or restore.
113 const baseFetch = vi.fn().mockResolvedValue(mockResponse)
114​
115 const billingFetch = createBillingFetch(baseFetch)
116 const returned = await billingFetch(
117 'https://queue.fal.run/fal-ai/flux/requests/req-fetch-1',
118 )
119​
120 expect(returned).toBe(mockResponse)
121 expect(baseFetch).toHaveBeenCalledTimes(1)
122 // Reading headers must not consume the body — the fal client still parses it.
123 await expect(returned.json()).resolves.toEqual({ ok: true })
124 expect(takeBillableUnits('req-fetch-1')).toBe(7)
125 })
126​
127 it('passes through responses without the billable-units header', async () => {
128 const mockResponse = resultResponse({ 'x-fal-request-id': 'req-fetch-2' })
129 const baseFetch = vi.fn().mockResolvedValue(mockResponse)
130​
131 const billingFetch = createBillingFetch(baseFetch)
132 await billingFetch('https://queue.fal.run/fal-ai/flux/requests/req-fetch-2')
133​
134 expect(takeBillableUnits('req-fetch-2')).toBeUndefined()
135 })
136})