/**
* Contract Tests: get-latest-posts tool
*
* Tests input validation and output schema compliance for the get-latest-posts tool.
* These tests verify the tool's contract without making real API calls.
*/
import { describe, expect, it } from "vitest";
import {
GetLatestPostsInputSchema,
getLatestPostsHandler,
getLatestPostsTool,
} from "../../src/tools/get-latest-posts.js";
describe("get-latest-posts tool contract", () => {
describe("Input Validation", () => {
it("should accept valid input with no parameters", () => {
const input = {};
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
it("should accept valid input with all parameters", () => {
const input = {
tags: ["story"],
page: 0,
hitsPerPage: 20,
};
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
it("should accept optional tags array", () => {
const input = { tags: ["story", "comment"] };
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
it("should reject negative page number", () => {
const input = { page: -1 };
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should accept page 0 (first page)", () => {
const input = { page: 0 };
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
it("should reject hitsPerPage less than 1", () => {
const input = { hitsPerPage: 0 };
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should reject hitsPerPage greater than 1000", () => {
const input = { hitsPerPage: 1001 };
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should accept hitsPerPage within valid range (1-1000)", () => {
const input = { hitsPerPage: 50 };
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
it("should apply default values for optional parameters", () => {
const input = {};
const result = GetLatestPostsInputSchema.parse(input);
expect(result.page).toBe(0);
expect(result.hitsPerPage).toBe(20);
});
it("should accept multiple tags", () => {
const input = { tags: ["story", "show_hn", "ask_hn"] };
const result = GetLatestPostsInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
});
describe("Tool Metadata", () => {
it("should have correct tool name", () => {
expect(getLatestPostsTool.name).toBe("get-latest-posts");
});
it("should have comprehensive description", () => {
expect(getLatestPostsTool.description).toBeTruthy();
expect(getLatestPostsTool.description.length).toBeGreaterThan(50);
});
it("should have properly defined input schema", () => {
expect(getLatestPostsTool.inputSchema).toBeDefined();
expect(getLatestPostsTool.inputSchema.type).toBe("object");
expect(getLatestPostsTool.inputSchema.properties).toHaveProperty("tags");
expect(getLatestPostsTool.inputSchema.properties).toHaveProperty("page");
expect(getLatestPostsTool.inputSchema.properties).toHaveProperty("hitsPerPage");
});
it("should not require any parameters", () => {
// All parameters should be optional with defaults
const result = GetLatestPostsInputSchema.safeParse({});
expect(result.success).toBe(true);
});
});
describe("Output Schema", () => {
it("should return error for invalid page number", async () => {
const result = await getLatestPostsHandler({ page: -1 });
expect(result.isError).toBe(true);
expect(result.content).toHaveLength(1);
expect(result.content[0].type).toBe("text");
});
it("should return error for invalid hitsPerPage", async () => {
const result = await getLatestPostsHandler({ hitsPerPage: 2000 });
expect(result.isError).toBe(true);
const content = result.content[0];
if (content.type === "text") {
expect(content.text).toBeTruthy();
}
});
});
});