/**
* Test fixtures and mock data for MCP server unit tests
*/
import type { Request, Response } from 'express';
import { Writable } from 'stream';
/**
* Mock Census API responses
*/
export const mockCensusResponses = {
// All states response
allStates: [
['NAME', 'P1_001N', 'state'],
['Alabama', '5024279', '01'],
['Alaska', '733391', '02'],
['Arizona', '7151502', '04'],
['California', '39538223', '06'],
['New York', '20201249', '36'],
],
// Single state (California)
california: [
['NAME', 'P1_001N', 'state'],
['California', '39538223', '06'],
],
// Multiple states
multipleStates: [
['NAME', 'P1_001N', 'state'],
['Alabama', '5024279', '01'],
['California', '39538223', '06'],
['New York', '20201249', '36'],
],
// Empty response
empty: [],
// Single state Alabama
alabama: [
['NAME', 'P1_001N', 'state'],
['Alabama', '5024279', '01'],
],
};
/**
* Mock MCP protocol requests
*/
export const mockMcpRequests = {
// Initialize request
initialize: {
jsonrpc: '2.0',
id: 1,
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
capabilities: {
roots: { listChanged: true },
sampling: {},
},
clientInfo: {
name: 'test-client',
version: '1.0.0',
},
},
},
// List tools request
listTools: {
jsonrpc: '2.0',
id: 2,
method: 'tools/list',
params: {},
},
// Call tool request - all states
callToolAllStates: {
jsonrpc: '2.0',
id: 3,
method: 'tools/call',
params: {
name: 'get-population',
arguments: {
states: [0],
},
},
},
// Call tool request - single state
callToolSingleState: {
jsonrpc: '2.0',
id: 4,
method: 'tools/call',
params: {
name: 'get-population',
arguments: {
states: [6],
},
},
},
// Call tool request - multiple states
callToolMultipleStates: {
jsonrpc: '2.0',
id: 5,
method: 'tools/call',
params: {
name: 'get-population',
arguments: {
states: [1, 6, 36],
},
},
},
// Call tool request - invalid tool
callToolInvalid: {
jsonrpc: '2.0',
id: 6,
method: 'tools/call',
params: {
name: 'invalid-tool',
arguments: {},
},
},
// Set logging level request
setLevel: {
jsonrpc: '2.0',
id: 7,
method: 'logging/setLevel',
params: {
level: 'info',
},
},
};
/**
* Creates a mock Express Request object
*/
export function createMockRequest(options: {
headers?: Record<string, string>;
body?: any;
method?: string;
}): Partial<Request> {
return {
headers: options.headers || {},
body: options.body || {},
method: options.method || 'POST',
get: function (name: string) {
return this.headers?.[name.toLowerCase()];
},
} as Partial<Request>;
}
/**
* Creates a mock Express Response object with tracking
*/
export function createMockResponse(): {
res: Partial<Response>;
track: {
statusCode: number | null;
jsonData: any | null;
headers: Record<string, string>;
writeCalls: any[];
endCalled: boolean;
};
} {
const track = {
statusCode: null as number | null,
jsonData: null as any,
headers: {} as Record<string, string>,
writeCalls: [] as any[],
endCalled: false,
};
const res = {
status: function (code: number) {
track.statusCode = code;
return this;
},
json: function (data: any) {
track.jsonData = data;
return this;
},
set: function (field: string, value: string) {
track.headers[field] = value;
return this;
},
setHeader: function (name: string, value: string) {
track.headers[name] = value;
return this;
},
writeHead: function (statusCode: number, headers?: any) {
track.statusCode = statusCode;
if (headers) {
Object.assign(track.headers, headers);
}
return this;
},
write: function (chunk: any) {
track.writeCalls.push(chunk);
return true;
},
end: function (chunk?: any) {
if (chunk) {
track.writeCalls.push(chunk);
}
track.endCalled = true;
return this;
},
// Mock writable stream interface for SSE
writable: true,
// Add minimal stream methods
on: function () {
return this;
},
once: function () {
return this;
},
emit: function () {
return true;
},
removeListener: function () {
return this;
},
} as Partial<Response>;
return { res, track };
}
/**
* Expected formatted Census data output
*/
export const expectedFormattedOutputs = {
allStates: `Census Population Data:
NAME\tP1_001N\tstate
---
Alabama\t5024279\t01
Alaska\t733391\t02
Arizona\t7151502\t04
California\t39538223\t06
New York\t20201249\t36
`,
california: `Census Population Data:
NAME\tP1_001N\tstate
---
California\t39538223\t06
`,
multipleStates: `Census Population Data:
NAME\tP1_001N\tstate
---
Alabama\t5024279\t01
California\t39538223\t06
New York\t20201249\t36
`,
};
/**
* Mock fetch responses
*/
export function createMockFetchResponse(data: any, ok = true, status = 200) {
return {
ok,
status,
json: async () => data,
} as Response;
}
/**
* Mock session IDs for testing
*/
export const mockSessionIds = {
valid: 'test-session-id-123',
valid2: 'test-session-id-456',
invalid: 'invalid-session-id',
};