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

extract-vercel-pricing.worker.test.ts

165 lines4.8 KB

1import { requestUrl } from '#/lib/text.ts'
2import { afterEach, describe, expect, it, vi } from 'vitest'
3import { env } from 'cloudflare:test'
4import { eq } from 'drizzle-orm'
5​
6import { getDb } from '../../db/index.ts'
7import { changes, models, providers } from '../../db/schema.ts'
8import { modelDbId } from './poll-models.ts'
9import { extractVercelPricing } from './extract-vercel-pricing.ts'
10import { VERCEL_MODELS_URL } from '../providers/adapters/vercel.ts'
11​
12const RAW = 'alibaba/qwen-3-235b'
13​
14function json(body: unknown, status = 200): Response {
15 return new Response(JSON.stringify(body), {
16 status,
17 headers: { 'content-type': 'application/json' },
18 })
20​
21function endpoints(second: Record<string, unknown>) {
22 return {
23 data: {
24 endpoints: [
25 {
26 provider_name: 'deepinfra',
27 status: 0,
28 pricing: { prompt: '0.00000009', completion: '0.00000055' },
29 },
30 second,
31 ],
32 },
33 }
35​
36describe('extractVercelPricing', () => {
37 afterEach(() => {
38 vi.unstubAllGlobals()
39 })
40​
41 it('writes a range, keeps it when the fetch fails, and clears it when a route has no price', async () => {
42 const db = getDb(env)
43 await db.insert(providers).values({
44 id: 'vercel',
45 displayName: 'Vercel',
46 specSourceUrl: 'https://ai-gateway.vercel.sh/v1/models',
47 })
48 const id = modelDbId('vercel', RAW)
49 const now = 1_781_150_000
50 await db.insert(models).values({
51 id,
52 providerId: 'vercel',
53 rawId: RAW,
54 activity: 'chat',
55 firstSeenAt: now,
56 lastSeenAt: now,
57 })
58 const listing = {
59 data: [{ id: RAW, pricing: { varies_by_provider: true } }],
60 }
61 vi.stubGlobal('fetch', async (input: RequestInfo | URL) => {
62 const url = requestUrl(input)
63 if (url === VERCEL_MODELS_URL) return json(listing)
64 if (url.endsWith('/endpoints')) {
65 return json(
66 endpoints({
67 provider_name: 'vertex',
68 status: 1,
69 pricing: { prompt: '0.00000022', completion: '0.00000088' },
70 }),
71 )
72 }
73 return json({ error: 'no' }, 404)
74 })
75 const first = await extractVercelPricing({
76 db,
77 kv: env.SCHEMA_CACHE,
78 secrets: {},
79 })
80 expect(first).toMatchObject({
81 candidates: 1,
82 fetched: 1,
83 ranged: 0,
84 refused: 1,
85 })
86 // One available route is not a range.
87 expect(
88 (await db.select().from(models).where(eq(models.id, id)))[0]
89 ?.pricingRange,
90 ).toBeNull()
91​
92 vi.stubGlobal('fetch', async (input: RequestInfo | URL) => {
93 const url = requestUrl(input)
94 if (url === VERCEL_MODELS_URL) return json(listing)
95 if (url.endsWith('/endpoints')) {
96 return json(
97 endpoints({
98 provider_name: 'vertex',
99 status: 0,
100 pricing: { prompt: '0.00000022', completion: '0.00000088' },
101 }),
102 )
103 }
104 return json({}, 500)
105 })
106 const ranged = await extractVercelPricing({
107 db,
108 kv: env.SCHEMA_CACHE,
109 secrets: {},
110 })
111 expect(ranged).toMatchObject({ fetched: 1, ranged: 1, fetchFailed: 0 })
112 const stored = (await db.select().from(models).where(eq(models.id, id)))[0]
113 expect(stored?.pricingRange?.routes.map((route) => route.provider)).toEqual(
114 ['deepinfra', 'vertex'],
115 )
116 expect(stored?.pricingRange?.routes[0]?.card).toBeTruthy()
117 expect(stored?.factSources).toMatchObject({
118 pricingRange: { path: 'data.endpoints[status=0].pricing' },
119 })
120 const updated = await db
121 .select()
122 .from(changes)
123 .where(eq(changes.subjectId, id))
124 expect(updated.length).toBeGreaterThan(0)
125​
126 vi.stubGlobal('fetch', async (input: RequestInfo | URL) => {
127 const url = requestUrl(input)
128 if (url === VERCEL_MODELS_URL) return json(listing)
129 return json({ error: 'down' }, 503)
130 })
131 const failed = await extractVercelPricing({
132 db,
133 kv: env.SCHEMA_CACHE,
134 secrets: {},
135 })
136 expect(failed).toMatchObject({ fetchFailed: 1, fetched: 0 })
137 const kept = (await db.select().from(models).where(eq(models.id, id)))[0]
138 expect(kept?.pricingRange?.routes).toHaveLength(2)
139​
140 vi.stubGlobal('fetch', async (input: RequestInfo | URL) => {
141 const url = requestUrl(input)
142 if (url === VERCEL_MODELS_URL) return json(listing)
143 if (url.endsWith('/endpoints')) {
144 return json(
145 endpoints({
146 provider_name: 'vertex',
147 status: 0,
148 pricing: { prompt: '0', completion: '0' },
149 }),
150 )
151 }
152 return json({}, 500)
153 })
154 const refused = await extractVercelPricing({
155 db,
156 kv: env.SCHEMA_CACHE,
157 secrets: {},
158 })
159 expect(refused).toMatchObject({ fetched: 1, refused: 1, ranged: 0 })
160 expect(
161 (await db.select().from(models).where(eq(models.id, id)))[0]
162 ?.pricingRange,
163 ).toBeNull()
164 })
165})