-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexpress-body-order.test.ts
More file actions
70 lines (57 loc) · 2.16 KB
/
express-body-order.test.ts
File metadata and controls
70 lines (57 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import express from "express";
import request from "supertest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { AbstractAgent } from "@ag-ui/client";
import { createCopilotEndpointExpress } from "../express";
import { CopilotRuntime } from "../runtime";
const handleRunAgentMock = vi.fn();
vi.mock("../handlers/handle-run", () => ({
handleRunAgent: (...args: unknown[]) => handleRunAgentMock(...args),
}));
const createRuntime = () =>
new CopilotRuntime({
agents: {
agent: {
clone: () => ({
execute: async () => ({ events: [] }),
}),
} as unknown as AbstractAgent,
},
});
describe("createCopilotEndpointExpress with body parsers", () => {
beforeEach(() => {
handleRunAgentMock.mockReset();
handleRunAgentMock.mockImplementation(async ({ request }: { request: Request }) => {
const body = await request.json();
return new Response(JSON.stringify({ body }), {
headers: { "content-type": "application/json" },
});
});
});
afterEach(() => {
vi.clearAllMocks();
});
const sendRunRequest = (app: express.Express) =>
request(app)
.post("/agent/agent/run")
.set("Content-Type", "application/json")
.send({ hello: "world" });
it("handles requests when CopilotKit router is registered before express.json()", async () => {
const app = express();
app.use(createCopilotEndpointExpress({ runtime: createRuntime(), basePath: "/" }));
app.use(express.json());
const response = await sendRunRequest(app);
expect(response.status).toBe(200);
expect(response.body).toEqual({ body: { hello: "world" } });
expect(handleRunAgentMock).toHaveBeenCalledTimes(1);
});
it("handles requests when express.json() runs before the CopilotKit router", async () => {
const app = express();
app.use(express.json());
app.use(createCopilotEndpointExpress({ runtime: createRuntime(), basePath: "/" }));
const response = await sendRunRequest(app);
expect(response.status).toBe(200);
expect(response.body).toEqual({ body: { hello: "world" } });
expect(handleRunAgentMock).toHaveBeenCalledTimes(1);
});
});