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

chat.test.e2e.ts

42 lines1.4 KB

1import assert from 'node:assert/strict'
2import { describe, it } from 'node:test'
3import { render } from 'remix/middleware/render'
4import { createRouter } from 'remix/router'
5​
6import chatController from './actions/chat/controller.ts'
7import { routes } from './routes.ts'
8​
9// ponytail: map only the chat controller. The app router loads Chat, and
10// @tanstack/ai-remix source has extensionless imports Node cannot resolve.
11// `RouterTypes` types every controller with the app context, which the render
12// middleware adds, so the router needs it. No `assets`: chat renders no UI.
13const router = createRouter({ middleware: [render()] })
14router.map(routes.chat, chatController)
15​
16const chatUrl = 'http://localhost' + routes.chat.stream.href()
17​
18function postChat(body: string) {
19 return router.fetch(
20 new Request(chatUrl, {
21 method: 'POST',
22 headers: { 'content-type': 'application/json' },
23 body,
24 }),
25 )
27​
28describe('chat', () => {
29 it('returns 400 when the POST body is not JSON', async () => {
30 const response = await postChat('not-json')
31 assert.equal(response.status, 400)
32 assert.equal(await response.text(), 'Bad request')
33 })
34​
35 it('returns 400 when JSON is not a chat request', async () => {
36 const response = await postChat(
37 JSON.stringify({ messages: [{ role: 'user', content: 'hi' }] }),
38 )
39 assert.equal(response.status, 400)
40 assert.match(await response.text(), /RunAgentInput/)
41 })
42})