/**
* Unit tests for utility functions
*/
import { describe, expect, it } from "vitest";
import {
buildApiUrl,
calculateAccountAge,
calculateKarmaPerYear,
dateToUnixTimestamp,
formatErrorResponse,
formatNumericFilters,
formatTags,
formatToolResponse,
safeJsonParse,
unixTimestampToDate,
} from "../../src/utils/index.js";
describe("Utility Functions", () => {
describe("dateToUnixTimestamp", () => {
it("should convert ISO date to Unix timestamp", () => {
const isoDate = "2024-01-01T00:00:00Z";
const timestamp = dateToUnixTimestamp(isoDate);
expect(timestamp).toBe(1704067200);
});
});
describe("unixTimestampToDate", () => {
it("should convert Unix timestamp to ISO date", () => {
const timestamp = 1704067200;
const isoDate = unixTimestampToDate(timestamp);
expect(isoDate).toBe("2024-01-01T00:00:00.000Z");
});
});
describe("buildApiUrl", () => {
it("should build URL without params", () => {
const url = buildApiUrl("search");
expect(url).toBe("https://hn.algolia.com/api/v1/search");
});
it("should build URL with params", () => {
const url = buildApiUrl("search", { query: "test", page: "0" });
expect(url).toContain("query=test");
expect(url).toContain("page=0");
});
});
describe("formatNumericFilters", () => {
it("should format numeric filters", () => {
const filters = ["points>=50", "num_comments>=10"];
const formatted = formatNumericFilters(filters);
expect(formatted).toBe("points>=50,num_comments>=10");
});
});
describe("formatTags", () => {
it("should format tags with parentheses", () => {
const tags = ["story", "comment"];
const formatted = formatTags(tags);
expect(formatted).toBe("(story),(comment)");
});
});
describe("calculateAccountAge", () => {
it("should calculate account age in years", () => {
const now = Date.now() / 1000;
const oneYearAgo = now - 365.25 * 24 * 60 * 60;
const age = calculateAccountAge(oneYearAgo);
expect(age).toBeCloseTo(1.0, 0);
});
});
describe("calculateKarmaPerYear", () => {
it("should calculate karma per year", () => {
const now = Date.now() / 1000;
const twoYearsAgo = now - 2 * 365.25 * 24 * 60 * 60;
const karmaPerYear = calculateKarmaPerYear(1000, twoYearsAgo);
expect(karmaPerYear).toBeCloseTo(500, 0);
});
});
describe("safeJsonParse", () => {
it("should parse valid JSON", () => {
const result = safeJsonParse('{"test": true}');
expect(result).toEqual({ test: true });
});
it("should return null for invalid JSON", () => {
const result = safeJsonParse("invalid");
expect(result).toBeNull();
});
});
describe("formatToolResponse", () => {
it("should format successful response", () => {
const response = formatToolResponse({ data: "test" });
expect(response).toHaveProperty("content");
expect(response.content[0].type).toBe("text");
expect(response.isError).toBeUndefined();
});
it("should format error response", () => {
const response = formatToolResponse({ error: "test" }, true);
expect(response).toHaveProperty("content");
expect(response.isError).toBe(true);
});
});
describe("formatErrorResponse", () => {
it("should format error object", () => {
const error = new Error("Test error");
const response = formatErrorResponse(error);
expect(response.isError).toBe(true);
expect(response.content[0].text).toContain("Test error");
});
});
});