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

keyed-adapter.spec.ts

53 lines1.6 KB

1import { test, expect } from './fixtures'
2​
3test.describe('keyedAdapter', () => {
4 test('builds the adapter with the key from the BYOK header', async ({
5 request,
6 testId,
7 }) => {
8 const response = await request.post('/api/keyed-adapter', {
9 headers: { 'x-byok-openai': 'sk-e2e-keyed-1234', 'x-test-id': testId },
10 })
11​
12 expect(response.ok()).toBe(true)
13 const body = await response.json()
14 expect(body.text).toContain('Fender Stratocaster')
15 expect(body.authorization).toBe('Bearer sk-e2e-keyed-1234')
16 })
17​
18 test('answers byok_missing when the request has no key', async ({
19 request,
20 }) => {
21 const response = await request.post('/api/keyed-adapter')
22​
23 expect(response.status()).toBe(401)
24 await expect(response.json()).resolves.toMatchObject({
25 error: { type: 'byok_missing', provider: 'openai' },
26 })
27 })
28})
29​
30test.describe('keyedAdapters', () => {
31 test('builds the adapter of the provider whose key the user sent', async ({
32 request,
33 testId,
34 }) => {
35 const response = await request.post('/api/keyed-adapter?mode=many', {
36 headers: { 'x-byok-anthropic': 'sk-e2e-keyed-5678', 'x-test-id': testId },
37 })
38​
39 expect(response.ok()).toBe(true)
40 const body = await response.json()
41 expect(body.text).toContain('Fender Stratocaster')
42 expect(body.provider).toBe('anthropic')
43 expect(body.credential).toBe('sk-e2e-keyed-5678')
44 })
45​
46 test('answers byok_missing when no provider has a key', async ({
47 request,
48 }) => {
49 const response = await request.post('/api/keyed-adapter?mode=many')
50​
51 expect(response.status()).toBe(401)
52 })
53})