添加全局 Copilot 聊天框组件
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { streamCopilotChat } from "./chatStream";
|
||||
import { ReadableStream } from "stream/web";
|
||||
import { TextEncoder, TextDecoder } from "util";
|
||||
|
||||
if (!globalThis.ReadableStream) {
|
||||
// @ts-expect-error test polyfill
|
||||
globalThis.ReadableStream = ReadableStream;
|
||||
}
|
||||
if (!globalThis.TextEncoder) {
|
||||
// @ts-expect-error test polyfill
|
||||
globalThis.TextEncoder = TextEncoder;
|
||||
}
|
||||
if (!globalThis.TextDecoder) {
|
||||
// @ts-expect-error test polyfill
|
||||
globalThis.TextDecoder = TextDecoder;
|
||||
}
|
||||
|
||||
jest.mock("@/lib/apiFetch", () => ({
|
||||
apiFetch: jest.fn(),
|
||||
}));
|
||||
|
||||
const { apiFetch } = jest.requireMock("@/lib/apiFetch") as {
|
||||
apiFetch: jest.Mock;
|
||||
};
|
||||
|
||||
const makeStream = (chunks: string[]) =>
|
||||
new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
const encoder = new TextEncoder();
|
||||
chunks.forEach((chunk) => controller.enqueue(encoder.encode(chunk)));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
describe("streamCopilotChat", () => {
|
||||
beforeEach(() => {
|
||||
apiFetch.mockReset();
|
||||
});
|
||||
|
||||
it("parses token and done events from chunked SSE", async () => {
|
||||
apiFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
body: makeStream([
|
||||
'event: token\ndata: {"conversationId":"c1","content":"he"}\n\n',
|
||||
'event: token\ndata: {"conversationId":"c1","content":"llo"}\n\n',
|
||||
'event: done\ndata: {"conversationId":"c1"}\n\n',
|
||||
]),
|
||||
});
|
||||
|
||||
const events: Array<{ type: string; content?: string; conversationId?: string }> = [];
|
||||
|
||||
await streamCopilotChat({
|
||||
message: "hi",
|
||||
onEvent: (event) => events.push(event),
|
||||
});
|
||||
|
||||
expect(events).toEqual([
|
||||
{ type: "token", conversationId: "c1", content: "he" },
|
||||
{ type: "token", conversationId: "c1", content: "llo" },
|
||||
{ type: "done", conversationId: "c1" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("emits error when response is not ok", async () => {
|
||||
apiFetch.mockResolvedValue({
|
||||
ok: false,
|
||||
body: null,
|
||||
text: async () => "bad request",
|
||||
});
|
||||
|
||||
const events: Array<{ type: string; message?: string; detail?: string }> = [];
|
||||
await streamCopilotChat({
|
||||
message: "hi",
|
||||
onEvent: (event) => events.push(event),
|
||||
});
|
||||
|
||||
expect(events).toEqual([
|
||||
{ type: "error", message: "stream request failed", detail: "bad request" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user