get-user.test.ts•3.94 kB
/**
* Contract Tests: get-user tool
*
* Tests input validation and output schema compliance for the get-user tool.
* These tests verify the tool's contract without making real API calls.
*/
import { describe, expect, it } from "vitest";
import { GetUserInputSchema, getUserHandler, getUserTool } from "../../src/tools/get-user.js";
describe("get-user tool contract", () => {
describe("Input Validation", () => {
it("should accept valid username", () => {
const input = { username: "pg" };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
it("should accept alphanumeric username", () => {
const input = { username: "user123" };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
it("should accept username with underscores", () => {
const input = { username: "john_doe" };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(true);
});
it("should reject empty username", () => {
const input = { username: "" };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should reject missing username", () => {
const input = {};
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should reject username with spaces", () => {
const input = { username: "john doe" };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should reject username with special characters", () => {
const input = { username: "user@domain" };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should reject username with hyphens", () => {
const input = { username: "john-doe" };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should reject null username", () => {
const input = { username: null };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
it("should reject undefined username", () => {
const input = { username: undefined };
const result = GetUserInputSchema.safeParse(input);
expect(result.success).toBe(false);
});
});
describe("Tool Metadata", () => {
it("should have correct tool name", () => {
expect(getUserTool.name).toBe("get-user");
});
it("should have comprehensive description", () => {
expect(getUserTool.description).toBeTruthy();
expect(getUserTool.description.length).toBeGreaterThan(100);
expect(getUserTool.description).toContain("karma");
});
it("should have properly defined input schema", () => {
expect(getUserTool.inputSchema).toBeDefined();
expect(getUserTool.inputSchema.type).toBe("object");
expect(getUserTool.inputSchema.properties).toHaveProperty("username");
});
it("should require username parameter", () => {
expect(getUserTool.inputSchema.required).toContain("username");
});
});
describe("Output Schema", () => {
it("should return error for empty username", async () => {
const result = await getUserHandler({ username: "" });
expect(result.isError).toBe(true);
expect(result.content).toHaveLength(1);
expect(result.content[0].type).toBe("text");
});
it("should return error for missing username", async () => {
const result = await getUserHandler({});
expect(result.isError).toBe(true);
const content = result.content[0];
if (content.type === "text") {
expect(content.text).toContain("username");
}
});
it("should return error for invalid username format", async () => {
const result = await getUserHandler({ username: "user@domain" });
expect(result.isError).toBe(true);
const content = result.content[0];
if (content.type === "text") {
expect(content.text).toBeTruthy();
}
});
});
});