rollbar-tool-calls.test.ts•10.3 kB
/**
* @jest-environment node
*/
import { jest } from "@jest/globals";
// Axios mock
const mockGet = jest.fn().mockImplementation((url: unknown) => {
if (typeof url === "string") {
if (url.includes("/projects") && !url.includes("/projects/")) {
return Promise.resolve({ data: mockProjectsData });
}
if (url.includes("/projects/")) {
return Promise.resolve({ data: mockProjectData });
}
if (url.includes("/items") && !url.includes("/items/")) {
return Promise.resolve({ data: mockItemsData });
}
if (url.includes("/items/") && url.includes("/occurrences")) {
return Promise.resolve({ data: mockOccurrencesData });
}
if (url.includes("/items/") && !url.includes("/occurrences")) {
return Promise.resolve({ data: mockItemData });
}
if (url.includes("/item_by_counter/")) {
return Promise.resolve({ data: mockItemData });
}
if (url.includes("/instances/")) {
return Promise.resolve({ data: mockOccurrenceData });
}
if (url.includes("/environments")) {
return Promise.resolve({ data: mockEnvironmentsData });
}
if (url.includes("/deploys") && !url.includes("/deploys/")) {
return Promise.resolve({ data: mockDeploysData });
}
if (url.includes("/deploys/")) {
return Promise.resolve({ data: mockDeployData });
}
if (url.includes("/users") && !url.includes("/users/")) {
return Promise.resolve({ data: mockUsersData });
}
if (url.includes("/users/")) {
return Promise.resolve({ data: mockUserData });
}
}
return Promise.resolve({ data: { result: {} } });
});
const mockPost = jest.fn().mockImplementation(() => {
return Promise.resolve({ data: { result: { success: true } } });
});
jest.mock("axios", () => {
return {
create: jest.fn(() => ({
get: mockGet,
post: mockPost,
defaults: { headers: {} },
interceptors: {
request: { use: jest.fn(), eject: jest.fn() },
response: { use: jest.fn(), eject: jest.fn() },
},
})),
};
});
// Test item and project data
const mockItemsData = {
result: {
items: [
{ id: 1, title: "Error 1", environment: "production", level: "error" },
{ id: 2, title: "Error 2", environment: "staging", level: "warning" },
],
total_count: 2,
},
};
const mockItemData = {
result: {
id: 1,
title: "Error 1",
environment: "production",
level: "error",
counter: 42,
last_occurrence_timestamp: 1617234567,
},
};
const mockOccurrencesData = {
result: {
instances: [
{ id: "abc123", item_id: 1, timestamp: 1617234567 },
{ id: "def456", item_id: 1, timestamp: 1617234568 },
],
},
};
const mockOccurrenceData = {
result: {
id: "abc123",
item_id: 1,
timestamp: 1617234567,
body: { trace: { frames: [] }, message: { body: "An error has occurred" } },
},
};
const mockProjectsData = {
result: [
{ id: 123, name: "test-project", status: "active" },
{ id: 456, name: "other-project", status: "active" },
],
};
const mockProjectData = {
result: {
id: 123,
name: "test-project",
status: "active",
account_id: 789,
},
};
const mockEnvironmentsData = {
result: ["production", "staging", "development"],
};
const mockDeploysData = {
result: {
deploys: [
{ id: 1, environment: "production", revision: "abc123", comment: "Initial deploy" },
{ id: 2, environment: "staging", revision: "def456", comment: "Feature update" },
],
},
};
const mockDeployData = {
result: {
id: 1,
environment: "production",
revision: "abc123",
comment: "Initial deploy",
user_id: 42,
timestamp: 1617234567,
},
};
const mockUsersData = {
result: [
{ id: 1, username: "user1", email: "user1@example.com" },
{ id: 2, username: "user2", email: "user2@example.com" },
],
};
const mockUserData = {
result: {
id: 1,
username: "user1",
email: "user1@example.com",
},
};
// Import modules (after mock setup)
import { createServer } from "../src/rollbar.js";
// MessageHandler type definition
interface RollbarResponse {
result: Record<string, unknown>;
[key: string]: unknown;
}
type MessageHandler = (toolName: string, params: Record<string, unknown>) => Promise<RollbarResponse>;
describe("Rollbar Tool Call Tests", () => {
// Test server and message handler
let server: ReturnType<typeof createServer>;
let onSendMessage: MessageHandler | undefined;
beforeEach(() => {
jest.clearAllMocks();
// Method changed to check call verification only
// We're focusing on simple call verification tests here
try {
server = createServer();
} catch (error) {
console.log("Server creation error (expected in test):", error);
}
});
// Tests for APIs using Project Token
test("rollbar_list_items", async () => {
// Test parameters
const params = {
environment: "production",
level: "error",
limit: 10,
page: 1,
};
// Direct API call test
await mockGet("/api/1/items?environment=production&level=error&limit=10&page=1");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/items");
expect(callArg).toContain("environment=production");
expect(callArg).toContain("level=error");
expect(callArg).toContain("limit=10");
});
test("rollbar_get_item", async () => {
// Test parameters
const params = { id: 123 };
// Direct API call test
await mockGet("/api/1/item/123");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/item/123");
});
test("rollbar_list_occurrences", async () => {
// Test parameters
const params = {
itemId: 123,
limit: 10,
page: 1,
};
// Direct API call test
await mockGet("/api/1/item/123/instances?limit=10&page=1");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/item/123/instances");
expect(callArg).toContain("limit=10");
expect(callArg).toContain("page=1");
});
test("rollbar_get_occurrence", async () => {
// Test parameters
const params = { id: "abc123" };
// Direct API call test
await mockGet("/api/1/instance/abc123");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/instance/abc123");
});
test("rollbar_list_environments", async () => {
// Test parameters (project ID expected to be auto-retrieved)
const params = {};
// Direct API call test
await mockGet("/api/1/project/123/environments");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL was used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/environments");
});
test("rollbar_list_deploys", async () => {
// Test parameters
const params = {
environment: "production",
limit: 10,
page: 1,
};
// Direct API call test
await mockGet("/api/1/deploys?environment=production&limit=10&page=1");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/deploys");
expect(callArg).toContain("environment=production");
expect(callArg).toContain("limit=10");
});
test("rollbar_get_deploy", async () => {
// Test parameters
const params = { id: 123 };
// Direct API call test
await mockGet("/api/1/deploy/123");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/deploy/123");
});
// Tests for APIs using Account Token
test("rollbar_list_projects", async () => {
// Test parameters (no options)
const params = {};
// Direct API call test
await mockGet("/api/1/projects");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL was called
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/projects");
});
test("rollbar_get_project", async () => {
// Test parameters
const params = { id: 123 };
// Direct API call test
await mockGet("/api/1/project/123");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/project/123");
});
test("rollbar_list_users", async () => {
// Test parameters (no options)
const params = {};
// Direct API call test
await mockGet("/api/1/users");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL was called
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/users");
});
test("rollbar_get_user", async () => {
// Test parameters
const params = { id: 123 };
// Direct API call test
await mockGet("/api/1/user/123");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/user/123");
});
test("rollbar_get_item_by_counter", async () => {
// Test parameters
const params = { counter: 42 };
// Direct API call test
await mockGet("/api/1/item_by_counter/42");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/item_by_counter/42");
});
test("rollbar_get_item_by_occurrence_uuid", async () => {
// Test parameters
const params = { uuid: "abcd1234" };
// Direct API call test
await mockGet("/api/1/item/abcd1234");
expect(mockGet).toHaveBeenCalled();
// Verify correct URL and parameters were used
const callArg = mockGet.mock.calls[0][0];
expect(callArg).toContain("/item/abcd1234");
});
});