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

api.chat.test.ts

39 lines1.0 KB

1import assert from 'node:assert/strict'
2import { test } from 'node:test'
3import { POST } from './api.chat'
4​
5test('POST without x-byok-openrouter returns 401 byok_missing', async (t) => {
6 const previousKey = process.env.OPENROUTER_API_KEY
7 t.after(() => {
8 if (previousKey === undefined) {
9 delete process.env.OPENROUTER_API_KEY
10 } else {
11 process.env.OPENROUTER_API_KEY = previousKey
12 }
13 })
14 delete process.env.OPENROUTER_API_KEY
15​
16 const response = await POST({
17 request: new Request('http://localhost/api/chat', {
18 method: 'POST',
19 headers: { 'content-type': 'application/json' },
20 body: JSON.stringify({
21 threadId: 'thread-1',
22 runId: 'run-1',
23 messages: [{ id: 'm1', role: 'user', content: 'hello' }],
24 tools: [],
25 context: [],
26 }),
27 }),
28 })
29​
30 assert.equal(response.status, 401)
31 const body: unknown = await response.json()
32 assert.deepEqual(body, {
33 error: {
34 type: 'byok_missing',
35 provider: 'openrouter',
36 message: 'Missing openrouter API key',
37 },
38 })
39})