chatRequest, readChatStream, withSession
Build /api/chat requests, read the streamed answer, carry a session.
Three helpers for tests of createChatHandler: chatRequest builds a request as the cloud adapter sends it, readChatStream reads the answer to the end, and withSession starts a session the way the page does.
Import
import { chatRequest, readChatStream, withSession } from "@webmcp-agents/testing/server";
Example
const send = await withSession(handler);
const req = chatRequest({ messages: ["hi"], tools: ["add_to_cart"] });
const parts = await readChatStream(await send(req));
expect(parts).toContainEqual(
expect.objectContaining({ type: "tool-call", name: "add_to_cart" }),
);
Options
chatRequest(body, options?): body.messages takes strings (user messages) or AgentMessages, body.tools names or descriptors; anything else in body is sent as is.
| Name | Type | Default | Description |
|---|---|---|---|
origin |
string |
none | The Origin header: another site’s gets 403. |
headers |
Record<string, string> |
none | More headers. |
url |
string |
http://localhost/api/chat |
The URL. |
Returns
chatRequest: a Request. readChatStream: the parts (text-delta, reasoning, tool-call, error, finish, else other), each with its raw chunk; it rejects with a sentence for a non-stream answer. withSession: a send(req) that carries the cookie.
Also exported
CHAT_URL:"http://localhost/api/chat", the default URL of achatRequest.ChatRequestBody: whatchatRequestputs in the body.ChatStreamPart: one part of the answer, asreadChatStreamreturns it.ChatHandler:(req: Request) => Response | Promise<Response>, whatwithSessionwraps.