/**
* Integration tests for HackerNews MCP Server tools
* These tests mock the HN API to test tool handlers
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
import { handleGetFrontPage } from "../../src/tools/get-front-page.js";
import { handleGetPost } from "../../src/tools/get-post.js";
import { handleGetUser } from "../../src/tools/get-user.js";
import { handleSearchPosts } from "../../src/tools/search-posts.js";
describe("Integration Tests", () => {
describe("search_posts tool", () => {
it("should search posts with valid parameters", async () => {
const result = await handleSearchPosts({
query: "test",
tags: ["story"],
minPoints: 10,
page: 0,
hitsPerPage: 5,
});
expect(result).toHaveProperty("content");
expect(result.content).toBeInstanceOf(Array);
expect(result.content[0]).toHaveProperty("type", "text");
});
it("should reject invalid parameters", async () => {
try {
await handleSearchPosts({
minPoints: -5, // Invalid: negative
});
expect.fail("Should have thrown ValidationError");
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
});
});
describe("get_front_page tool", () => {
it("should retrieve front page posts", async () => {
const result = await handleGetFrontPage({
page: 0,
hitsPerPage: 10,
});
expect(result).toHaveProperty("content");
expect(result.content).toBeInstanceOf(Array);
});
});
describe("get_post tool", () => {
it("should retrieve post by ID", async () => {
const result = await handleGetPost({
postId: "1", // Valid HN post ID
});
expect(result).toHaveProperty("content");
});
it("should reject invalid post ID format", async () => {
try {
await handleGetPost({
postId: "invalid",
});
expect.fail("Should have thrown ValidationError");
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
});
});
describe("get_user tool", () => {
it("should retrieve user profile", async () => {
const result = await handleGetUser({
username: "pg",
});
expect(result).toHaveProperty("content");
});
it("should reject invalid username", async () => {
try {
await handleGetUser({
username: "a".repeat(20), // Too long
});
expect.fail("Should have thrown ValidationError");
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
});
});
});