transportLogic.test.vitest.ts•7.2 kB
// Using vitest globals - see vitest.config.ts globals: true
describe("Transport Logic Tests", () => {
describe("Transport Selection", () => {
const selectTransport = (transportType: string | undefined) => {
const type = transportType || "stdio";
if (type === "sse") {
return {
selected: "streamable",
fallback: false,
message:
"SSE transport - using StreamableHTTPServerTransport via HTTP endpoint",
};
} else if (type === "http" || type === "streamable") {
return {
selected: "streamable",
fallback: false,
message:
"HTTP transport - individual requests will create their own transports",
};
} else if (type === "streaming") {
return {
selected: "stdio",
fallback: true,
reason: "Streaming transport not currently implemented",
};
} else {
return {
selected: "stdio",
fallback: false,
message: "Using stdio transport",
};
}
};
it("should select stdio by default", () => {
const result = selectTransport(undefined);
expect(result.selected).toBe("stdio");
expect(result.fallback).toBe(false);
});
it("should select streamable for http transport", () => {
const result = selectTransport("http");
expect(result.selected).toBe("streamable");
expect(result.fallback).toBe(false);
});
it("should select streamable for streamable transport", () => {
const result = selectTransport("streamable");
expect(result.selected).toBe("streamable");
expect(result.fallback).toBe(false);
});
it("should select streamable for SSE", () => {
const result = selectTransport("sse");
expect(result.selected).toBe("streamable");
expect(result.fallback).toBe(false);
expect(result.message).toContain(
"SSE transport - using StreamableHTTPServerTransport"
);
});
it("should fallback to stdio for streaming", () => {
const result = selectTransport("streaming");
expect(result.selected).toBe("stdio");
expect(result.fallback).toBe(true);
expect(result.reason).toContain(
"Streaming transport not currently implemented"
);
});
});
describe("Session Validation", () => {
const isInitializeRequest = (body: unknown): boolean => {
if (!body || typeof body !== "object") return false;
const jsonRpcBody = body as {
jsonrpc?: string;
method?: string;
id?: string | number;
};
return (
jsonRpcBody.jsonrpc === "2.0" &&
jsonRpcBody.method === "initialize" &&
(typeof jsonRpcBody.id === "string" ||
typeof jsonRpcBody.id === "number")
);
};
const shouldAllowRequest = (
sessionId: string | undefined,
body: unknown,
sessions: Set<string>
): boolean => {
// Allow initialize requests without session
if (!sessionId && isInitializeRequest(body)) {
return true;
}
// Allow requests with valid session
if (sessionId && sessions.has(sessionId)) {
return true;
}
// Reject everything else
return false;
};
it("should identify valid initialize requests", () => {
expect(
isInitializeRequest({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {},
})
).toBe(true);
expect(
isInitializeRequest({
jsonrpc: "2.0",
id: "init-1",
method: "initialize",
params: {},
})
).toBe(true);
});
it("should reject invalid initialize requests", () => {
expect(isInitializeRequest(null)).toBe(false);
expect(isInitializeRequest({})).toBe(false);
expect(isInitializeRequest({ method: "initialize" })).toBe(false);
expect(
isInitializeRequest({ jsonrpc: "2.0", method: "tools/call" })
).toBe(false);
});
it("should allow initialize without session", () => {
const sessions = new Set<string>();
const body = { jsonrpc: "2.0", id: 1, method: "initialize" };
expect(shouldAllowRequest(undefined, body, sessions)).toBe(true);
});
it("should reject non-initialize without session", () => {
const sessions = new Set<string>();
const body = { jsonrpc: "2.0", id: 1, method: "tools/call" };
expect(shouldAllowRequest(undefined, body, sessions)).toBe(false);
});
it("should allow requests with valid session", () => {
const sessions = new Set(["session-123"]);
const body = { jsonrpc: "2.0", id: 1, method: "tools/call" };
expect(shouldAllowRequest("session-123", body, sessions)).toBe(true);
});
it("should reject requests with invalid session", () => {
const sessions = new Set(["session-123"]);
const body = { jsonrpc: "2.0", id: 1, method: "tools/call" };
expect(shouldAllowRequest("wrong-session", body, sessions)).toBe(false);
});
});
describe("Accept Header Validation", () => {
const validateAcceptHeader = (headers: Record<string, string>): boolean => {
const accept = headers["accept"] || headers["Accept"] || "";
return (
accept.includes("application/json") &&
accept.includes("text/event-stream")
);
};
it("should accept valid headers", () => {
expect(
validateAcceptHeader({
Accept: "application/json, text/event-stream",
})
).toBe(true);
expect(
validateAcceptHeader({
accept: "application/json, text/event-stream",
})
).toBe(true);
});
it("should reject missing event-stream", () => {
expect(
validateAcceptHeader({
Accept: "application/json",
})
).toBe(false);
});
it("should reject missing json", () => {
expect(
validateAcceptHeader({
Accept: "text/event-stream",
})
).toBe(false);
});
it("should reject empty headers", () => {
expect(validateAcceptHeader({})).toBe(false);
});
});
describe("Environment Validation", () => {
const validateRequiredEnvVars = (
env: Record<string, string | undefined>
): string[] => {
const required = [
"GOOGLE_GEMINI_API_KEY",
"MCP_SERVER_HOST",
"MCP_SERVER_PORT",
"MCP_CONNECTION_TOKEN",
];
return required.filter((key) => !env[key]);
};
it("should pass with all required vars", () => {
const env = {
GOOGLE_GEMINI_API_KEY: "key",
MCP_SERVER_HOST: "localhost",
MCP_SERVER_PORT: "8080",
MCP_CONNECTION_TOKEN: "token",
};
expect(validateRequiredEnvVars(env)).toEqual([]);
});
it("should identify missing vars", () => {
const env = {
GOOGLE_GEMINI_API_KEY: "key",
MCP_SERVER_HOST: "localhost",
};
const missing = validateRequiredEnvVars(env);
expect(missing).toContain("MCP_SERVER_PORT");
expect(missing).toContain("MCP_CONNECTION_TOKEN");
});
});
});