eslint fixes in tests (#1418)
This commit is contained in:
parent
55a28ed645
commit
bf6f16c8be
1 changed files with 219 additions and 184 deletions
|
|
@ -26,7 +26,7 @@ async function getFreePort() {
|
|||
srv.listen(0, () => {
|
||||
const address = srv.address()!;
|
||||
if (typeof address === 'string') {
|
||||
throw new Error('Unexpected address type: ' + typeof address);
|
||||
throw new TypeError('Unexpected address type: ' + typeof address);
|
||||
}
|
||||
const port = (address as AddressInfo).port;
|
||||
srv.close(_err => res(port));
|
||||
|
|
@ -152,12 +152,13 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
/**
|
||||
* Helper to create and start test HTTP server with MCP setup
|
||||
*/
|
||||
async function createTestServer(config: TestServerConfig = { sessionIdGenerator: () => randomUUID() }): Promise<{
|
||||
async function createTestServer(config?: TestServerConfig): Promise<{
|
||||
server: Server;
|
||||
transport: NodeStreamableHTTPServerTransport;
|
||||
mcpServer: McpServer;
|
||||
baseUrl: URL;
|
||||
}> {
|
||||
config ??= { sessionIdGenerator: () => randomUUID() };
|
||||
const mcpServer = new McpServer({ name: 'test-server', version: '1.0.0' }, { capabilities: { logging: {} } });
|
||||
|
||||
mcpServer.tool(
|
||||
|
|
@ -182,11 +183,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
const server = createServer(async (req, res) => {
|
||||
try {
|
||||
if (config.customRequestHandler) {
|
||||
await config.customRequestHandler(req, res);
|
||||
} else {
|
||||
await transport.handleRequest(req, res);
|
||||
}
|
||||
await (config.customRequestHandler ? config.customRequestHandler(req, res) : transport.handleRequest(req, res));
|
||||
} catch (error) {
|
||||
console.error('Error handling request:', error);
|
||||
if (!res.headersSent) res.writeHead(500).end();
|
||||
|
|
@ -299,7 +296,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32600, /Server already initialized/);
|
||||
expectErrorResponse(errorData, -32_600, /Server already initialized/);
|
||||
});
|
||||
|
||||
it('should reject batch initialize request', async () => {
|
||||
|
|
@ -320,7 +317,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32600, /Only one initialization request is allowed/);
|
||||
expectErrorResponse(errorData, -32_600, /Only one initialization request is allowed/);
|
||||
});
|
||||
|
||||
it('should handle post requests via sse response correctly', async () => {
|
||||
|
|
@ -338,7 +335,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
const dataLine = eventLines.find(line => line.startsWith('data:'));
|
||||
expect(dataLine).toBeDefined();
|
||||
|
||||
const eventData = JSON.parse(dataLine!.substring(5));
|
||||
const eventData = JSON.parse(dataLine!.slice(5));
|
||||
expect(eventData).toMatchObject({
|
||||
jsonrpc: '2.0',
|
||||
result: expect.objectContaining({
|
||||
|
|
@ -376,7 +373,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
const dataLine = eventLines.find(line => line.startsWith('data:'));
|
||||
expect(dataLine).toBeDefined();
|
||||
|
||||
const eventData = JSON.parse(dataLine!.substring(5));
|
||||
const eventData = JSON.parse(dataLine!.slice(5));
|
||||
expect(eventData).toMatchObject({
|
||||
jsonrpc: '2.0',
|
||||
result: {
|
||||
|
|
@ -436,7 +433,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
const dataLine = eventLines.find(line => line.startsWith('data:'));
|
||||
expect(dataLine).toBeDefined();
|
||||
|
||||
const eventData = JSON.parse(dataLine!.substring(5));
|
||||
const eventData = JSON.parse(dataLine!.slice(5));
|
||||
|
||||
expect(eventData).toMatchObject({
|
||||
jsonrpc: '2.0',
|
||||
|
|
@ -469,7 +466,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = (await response.json()) as JSONRPCErrorResponse;
|
||||
expectErrorResponse(errorData, -32000, /Bad Request/);
|
||||
expectErrorResponse(errorData, -32_000, /Bad Request/);
|
||||
expect(errorData.id).toBeNull();
|
||||
});
|
||||
|
||||
|
|
@ -482,7 +479,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(404);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32001, /Session not found/);
|
||||
expectErrorResponse(errorData, -32_001, /Session not found/);
|
||||
});
|
||||
|
||||
it('should establish standalone SSE stream and receive server-initiated messages', async () => {
|
||||
|
|
@ -519,7 +516,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
const dataLine = eventLines.find(line => line.startsWith('data:'));
|
||||
expect(dataLine).toBeDefined();
|
||||
|
||||
const eventData = JSON.parse(dataLine!.substring(5));
|
||||
const eventData = JSON.parse(dataLine!.slice(5));
|
||||
expect(eventData).toMatchObject({
|
||||
jsonrpc: '2.0',
|
||||
method: 'notifications/message',
|
||||
|
|
@ -587,7 +584,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
// Should be rejected
|
||||
expect(secondStream.status).toBe(409); // Conflict
|
||||
const errorData = await secondStream.json();
|
||||
expectErrorResponse(errorData, -32000, /Only one SSE stream is allowed per session/);
|
||||
expectErrorResponse(errorData, -32_000, /Only one SSE stream is allowed per session/);
|
||||
});
|
||||
|
||||
it('should reject GET requests without Accept: text/event-stream header', async () => {
|
||||
|
|
@ -605,7 +602,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(406);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32000, /Client must accept text\/event-stream/);
|
||||
expectErrorResponse(errorData, -32_000, /Client must accept text\/event-stream/);
|
||||
});
|
||||
|
||||
it('should reject POST requests without proper Accept header', async () => {
|
||||
|
|
@ -624,7 +621,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(406);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32000, /Client must accept both application\/json and text\/event-stream/);
|
||||
expectErrorResponse(errorData, -32_000, /Client must accept both application\/json and text\/event-stream/);
|
||||
});
|
||||
|
||||
it('should reject unsupported Content-Type', async () => {
|
||||
|
|
@ -643,7 +640,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(415);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32000, /Content-Type must be application\/json/);
|
||||
expectErrorResponse(errorData, -32_000, /Content-Type must be application\/json/);
|
||||
});
|
||||
|
||||
it('should handle JSON-RPC batch notification messages with 202 response', async () => {
|
||||
|
|
@ -701,7 +698,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32700, /Parse error/);
|
||||
expectErrorResponse(errorData, -32_700, /Parse error/);
|
||||
});
|
||||
|
||||
it('should include error data in parse error response for unexpected errors', async () => {
|
||||
|
|
@ -721,7 +718,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = (await response.json()) as JSONRPCErrorResponse;
|
||||
expectErrorResponse(errorData, -32700, /Parse error/);
|
||||
expectErrorResponse(errorData, -32_700, /Parse error/);
|
||||
// The error message should contain details about what went wrong
|
||||
expect(errorData.error.message).toContain('Invalid JSON');
|
||||
});
|
||||
|
|
@ -759,7 +756,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32000, /Server not initialized/);
|
||||
expectErrorResponse(errorData, -32_000, /Server not initialized/);
|
||||
|
||||
// Cleanup
|
||||
await stopTestServer({ server: uninitializedServer, transport: uninitializedTransport });
|
||||
|
|
@ -878,7 +875,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(404);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32001, /Session not found/);
|
||||
expectErrorResponse(errorData, -32_001, /Session not found/);
|
||||
});
|
||||
|
||||
describe('protocol version header validation', () => {
|
||||
|
|
@ -926,7 +923,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version: .+ \(supported versions: .+\)/);
|
||||
expectErrorResponse(errorData, -32_000, /Bad Request: Unsupported protocol version: .+ \(supported versions: .+\)/);
|
||||
});
|
||||
|
||||
it('should accept when protocol version differs from negotiated version', async () => {
|
||||
|
|
@ -963,7 +960,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version/);
|
||||
expectErrorResponse(errorData, -32_000, /Bad Request: Unsupported protocol version/);
|
||||
});
|
||||
|
||||
it('should reject unsupported protocol version on DELETE requests', async () => {
|
||||
|
|
@ -980,7 +977,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
expect(response.status).toBe(400);
|
||||
const errorData = await response.json();
|
||||
expectErrorResponse(errorData, -32000, /Bad Request: Unsupported protocol version/);
|
||||
expectErrorResponse(errorData, -32_000, /Bad Request: Unsupported protocol version/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1032,7 +1029,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
const dataLine = eventLines.find(line => line.startsWith('data:'));
|
||||
expect(dataLine).toBeDefined();
|
||||
|
||||
const eventData = JSON.parse(dataLine!.substring(5));
|
||||
const eventData = JSON.parse(dataLine!.slice(5));
|
||||
expect(eventData).toMatchObject({
|
||||
jsonrpc: '2.0',
|
||||
result: {
|
||||
|
|
@ -1068,7 +1065,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
const dataLine = eventLines.find(line => line.startsWith('data:'));
|
||||
expect(dataLine).toBeDefined();
|
||||
|
||||
const eventData = JSON.parse(dataLine!.substring(5));
|
||||
const eventData = JSON.parse(dataLine!.slice(5));
|
||||
expect(eventData).toMatchObject({
|
||||
jsonrpc: '2.0',
|
||||
result: {
|
||||
|
|
@ -1183,11 +1180,11 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
const result = await createTestServer({
|
||||
customRequestHandler: async (req, res) => {
|
||||
try {
|
||||
if (parsedBody !== null) {
|
||||
if (parsedBody === null) {
|
||||
await transport.handleRequest(req, res);
|
||||
} else {
|
||||
await transport.handleRequest(req, res, parsedBody);
|
||||
parsedBody = null; // Reset after use
|
||||
} else {
|
||||
await transport.handleRequest(req, res);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error handling request:', error);
|
||||
|
|
@ -2334,7 +2331,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
|
||||
// Verify we received the notification that was sent while disconnected
|
||||
expect(allText).toContain('Missed while disconnected');
|
||||
}, 15000);
|
||||
}, 15_000);
|
||||
});
|
||||
|
||||
// Test onsessionclosed callback
|
||||
|
|
@ -2508,8 +2505,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
initializationOrder.push('async-start');
|
||||
// Simulate async operation
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
initializationOrder.push('async-end');
|
||||
initializationOrder.push(sessionId);
|
||||
initializationOrder.push('async-end', sessionId);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -2563,8 +2559,7 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
|
|||
closureOrder.push('async-close-start');
|
||||
// Simulate async operation
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
closureOrder.push('async-close-end');
|
||||
closureOrder.push(sessionId);
|
||||
closureOrder.push('async-close-end', sessionId);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue