-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathlifecycle.test.ts
More file actions
139 lines (119 loc) · 3.72 KB
/
Copy pathlifecycle.test.ts
File metadata and controls
139 lines (119 loc) · 3.72 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { testContext } from '../../connection/testing';
import { ServerInitializeScenario } from './lifecycle';
import { connectToServer } from '../../connection/sdk-client';
vi.mock('../../connection/sdk-client', async (importOriginal) => {
const actual =
await importOriginal<typeof import('../../connection/sdk-client')>();
return {
...actual,
connectToServer: vi.fn()
};
});
describe('ServerInitializeScenario', () => {
const serverUrl = 'http://localhost:3000/mcp';
const closeMock = vi.fn().mockResolvedValue(undefined);
const fetchMock = vi.fn();
beforeEach(() => {
vi.clearAllMocks();
vi.stubGlobal('fetch', fetchMock);
vi.mocked(connectToServer).mockResolvedValue({
client: {} as any,
close: closeMock
});
});
afterEach(() => {
vi.unstubAllGlobals();
});
it('returns INFO when the server does not provide an MCP-Session-Id header', async () => {
fetchMock.mockResolvedValue(new Response(null));
const checks = await new ServerInitializeScenario().run(
testContext(serverUrl)
);
expect(connectToServer).toHaveBeenCalledWith(serverUrl);
expect(closeMock).toHaveBeenCalled();
expect(fetchMock).toHaveBeenCalledWith(
serverUrl,
expect.objectContaining({
method: 'POST'
})
);
expect(checks).toHaveLength(2);
expect(checks[0]?.id).toBe('server-initialize');
expect(checks[0]?.status).toBe('SUCCESS');
expect(checks[1]).toMatchObject({
id: 'server-session-id-visible-ascii',
status: 'INFO',
details: {
message:
'Server did not provide an MCP-Session-Id header (session ID is optional)'
}
});
});
it('returns SUCCESS when the server provides a visible ASCII session ID', async () => {
fetchMock.mockResolvedValue(
new Response(null, {
headers: {
'mcp-session-id': 'session-123_ABC'
}
})
);
const checks = await new ServerInitializeScenario().run(
testContext(serverUrl)
);
expect(checks[1]).toMatchObject({
id: 'server-session-id-visible-ascii',
status: 'SUCCESS',
details: {
sessionId: 'session-123_ABC'
}
});
});
it('returns FAILURE when the server provides a non-ASCII session ID', async () => {
fetchMock.mockResolvedValue(
new Response(null, {
headers: {
'mcp-session-id': 'session-123-é'
}
})
);
const checks = await new ServerInitializeScenario().run(
testContext(serverUrl)
);
expect(checks[1]).toMatchObject({
id: 'server-session-id-visible-ascii',
status: 'FAILURE',
errorMessage:
'Session ID contains characters outside visible ASCII range (0x21-0x7E)',
details: {
sessionId: 'session-123-é'
}
});
});
it('sends an HTTP DELETE with the issued session ID to terminate the raw session', async () => {
fetchMock.mockResolvedValue(
new Response(null, {
headers: {
'mcp-session-id': 'session-123_ABC'
}
})
);
await new ServerInitializeScenario().run(testContext(serverUrl));
expect(fetchMock).toHaveBeenCalledWith(
serverUrl,
expect.objectContaining({
method: 'DELETE',
headers: expect.objectContaining({
'mcp-session-id': 'session-123_ABC'
})
})
);
});
it('does not send DELETE when the server did not assign a session ID', async () => {
fetchMock.mockResolvedValue(new Response(null));
await new ServerInitializeScenario().run(testContext(serverUrl));
const deleteCalls = fetchMock.mock.calls.filter(
([, init]) => (init as RequestInit | undefined)?.method === 'DELETE'
);
expect(deleteCalls).toHaveLength(0);
});
});