-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.ts
More file actions
114 lines (93 loc) · 3.7 KB
/
server.test.ts
File metadata and controls
114 lines (93 loc) · 3.7 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { describe, expect, test } from "bun:test";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
import { createServer } from "./server.js";
async function createTestClient() {
const server = createServer();
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
const client = new Client({ name: "test-client", version: "1.0.0" });
await Promise.all([
client.connect(clientTransport),
server.connect(serverTransport),
]);
return { client, server };
}
describe("PR Quiz MCP Server", () => {
test("lists tools including generate-quiz and start-quiz", async () => {
const { client } = await createTestClient();
const { tools } = await client.listTools();
const toolNames = tools.map((t) => t.name);
expect(toolNames).toContain("generate-quiz");
expect(toolNames).toContain("start-quiz");
});
test("generate-quiz rejects non-GitHub URLs", async () => {
const { client } = await createTestClient();
const result = await client.callTool({
name: "generate-quiz",
arguments: { source: "https://gitlab.com/owner/repo/merge_requests/123" },
});
expect(result.isError).toBe(true);
const textContent = result.content[0];
if (textContent.type === "text") {
expect(textContent.text).toContain("Invalid GitHub PR URL");
}
});
test("start-quiz returns quiz JSON", async () => {
const { client } = await createTestClient();
const quiz = {
prUrl: "https://github.com/owner/repo/pull/123",
prTitle: "Test PR",
diff: "diff --git a/test.ts b/test.ts\n+const add = (a, b) => a + b;",
comparisons: [
{
id: 1,
file: "test.ts",
lineRange: "L1-L5",
context: "A function that adds numbers",
question: "Which implementation is clearer?",
language: "typescript",
optionA: {
code: "const add = (a, b) => a + b;",
reasoning: "Concise arrow function syntax",
},
optionB: {
code: "function add(a: number, b: number): number {\n return a + b;\n}",
reasoning: "Explicit types and traditional syntax",
},
},
],
};
const result = await client.callTool({
name: "start-quiz",
arguments: { quiz },
});
expect(result.content).toBeArray();
const textContent = result.content[0];
expect(textContent.type).toBe("text");
if (textContent.type === "text") {
const returnedQuiz = JSON.parse(textContent.text);
expect(returnedQuiz.prTitle).toBe("Test PR");
expect(returnedQuiz.diff).toContain("diff --git");
expect(returnedQuiz.comparisons).toHaveLength(1);
expect(returnedQuiz.comparisons[0].optionA.code).toContain("add");
}
});
test("start-quiz tool has UI metadata", async () => {
const { client } = await createTestClient();
const { tools } = await client.listTools();
const startQuizTool = tools.find((t) => t.name === "start-quiz");
expect(startQuizTool?._meta?.ui?.resourceUri).toBe("ui://quiz/app.html");
});
test("UI resource is served", async () => {
const { client } = await createTestClient();
const { resources } = await client.listResources();
const uiResource = resources.find((r) => r.uri === "ui://quiz/app.html");
expect(uiResource).toBeDefined();
const result = await client.readResource({ uri: "ui://quiz/app.html" });
expect(result.contents).toBeArray();
expect(result.contents[0].mimeType).toBe("text/html;profile=mcp-app");
if ("text" in result.contents[0]) {
expect(result.contents[0].text).toContain("<!DOCTYPE html>");
}
});
});