/**
* Integration Tests: get-user tool
*
* Tests the get-user tool with real HackerNews API calls.
* Verifies correct behavior for retrieving user profiles.
*/
import { describe, expect, it } from "vitest";
import { getUserHandler } from "../../../src/tools/get-user.js";
describe("get-user integration", () => {
describe("Known User", () => {
it("should retrieve user profile by username", async () => {
// Using "pg" (Paul Graham), a well-known HN user
const result = await getUserHandler({ username: "pg" });
expect(result.isError).toBe(false);
expect(result.content).toHaveLength(1);
const content = result.content[0];
expect(content.type).toBe("text");
if (content.type === "text") {
const data = JSON.parse(content.text);
expect(data).toHaveProperty("username");
expect(data).toHaveProperty("karma");
expect(data.username).toBe("pg");
}
});
it("should include karma score", async () => {
const result = await getUserHandler({ username: "pg" });
expect(result.isError).toBe(false);
const content = result.content[0];
if (content.type === "text") {
const data = JSON.parse(content.text);
expect(typeof data.karma).toBe("number");
expect(data.karma).toBeGreaterThan(0);
}
});
it("should include about field", async () => {
const result = await getUserHandler({ username: "pg" });
expect(result.isError).toBe(false);
const content = result.content[0];
if (content.type === "text") {
const data = JSON.parse(content.text);
// About field may be present or null
expect(data).toHaveProperty("about");
}
});
it("should include creation date", async () => {
const result = await getUserHandler({ username: "pg" });
expect(result.isError).toBe(false);
const content = result.content[0];
if (content.type === "text") {
const data = JSON.parse(content.text);
// Created field should be a Unix timestamp
if (data.created) {
expect(typeof data.created).toBe("number");
expect(data.created).toBeGreaterThan(0);
}
}
});
it("should retrieve another known user (dang)", async () => {
// "dang" is a HN moderator
const result = await getUserHandler({ username: "dang" });
expect(result.isError).toBe(false);
const content = result.content[0];
if (content.type === "text") {
const data = JSON.parse(content.text);
expect(data.username).toBe("dang");
expect(typeof data.karma).toBe("number");
expect(data.karma).toBeGreaterThan(0);
}
});
});
describe("Error Handling", () => {
it("should return error for empty username", async () => {
const result = await getUserHandler({ username: "" });
expect(result.isError).toBe(true);
const content = result.content[0];
if (content.type === "text") {
expect(content.text).toContain("username");
}
});
it("should return error for missing username", async () => {
const result = await getUserHandler({});
expect(result.isError).toBe(true);
});
it("should return error for invalid username format (with @)", 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();
}
});
it("should return error for invalid username format (with spaces)", async () => {
const result = await getUserHandler({ username: "john doe" });
expect(result.isError).toBe(true);
});
it("should handle non-existent user", async () => {
// Using a very unlikely username
const result = await getUserHandler({
username: "thisuserdoesnotexist999999",
});
expect(result.isError).toBe(true);
const content = result.content[0];
if (content.type === "text") {
expect(content.text).toBeTruthy();
}
});
});
describe("Username Validation", () => {
it("should handle username with underscores", async () => {
// Try to find a valid user with underscores (if exists)
// For testing, we'll just verify the validation accepts it
const result = await getUserHandler({ username: "test_user" });
// Result may be error (user not found) but shouldn't be validation error
const content = result.content[0];
if (content.type === "text" && result.isError) {
// Should not be a validation error about format
expect(content.text).not.toContain("alphanumeric");
}
});
it("should handle username with numbers", async () => {
const result = await getUserHandler({ username: "user123" });
// Result may be error (user not found) but shouldn't be validation error
const content = result.content[0];
if (content.type === "text" && result.isError) {
// Should not be a validation error about format
expect(content.text).not.toContain("alphanumeric");
}
});
});
});