/**
* Tests for callback_list tool
*/
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { callbackListTool } from '../../../src/tools/callback/callback-list.js';
import { createMockClientWrapper } from '../../mocks/client.js';
import {
mockCallbackListResponse,
mockCallbackEmptyListResponse,
} from '../../mocks/responses/callback.js';
import {
mockUnauthorizedError,
mockRateLimitError,
mockServerError,
mockNetworkTimeoutError,
mockInvalidAccountError,
} from '../../mocks/errors/freshbooks-errors.js';
// Mock the FreshBooks SDK query builders
vi.mock('@freshbooks/api/dist/models/builders/index.js', () => ({
PaginationQueryBuilder: class {
private _page: number = 1;
private _perPage: number = 30;
page(value: number) {
this._page = value;
return this;
}
perPage(value: number) {
this._perPage = value;
return this;
}
build() {
return { page: this._page, perPage: this._perPage };
}
},
}));
describe('callback_list tool', () => {
let mockClient: ReturnType<typeof createMockClientWrapper>;
beforeEach(() => {
mockClient = createMockClientWrapper();
vi.clearAllMocks();
});
describe('successful operations', () => {
it('should return callbacks with default pagination', async () => {
const mockResponse = mockCallbackListResponse(10);
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockResponse),
},
};
return apiCall(client);
});
const result = await callbackListTool.execute(
{ accountId: 'ABC123' },
mockClient as any
);
expect(result.callbacks).toHaveLength(10);
expect(result.pagination.page).toBe(1);
expect(result.pagination.perPage).toBe(30);
expect(result.pagination.total).toBe(10);
});
it('should return callbacks with custom pagination', async () => {
const mockResponse = mockCallbackListResponse(5, 2, 5);
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockResponse),
},
};
return apiCall(client);
});
const result = await callbackListTool.execute(
{ accountId: 'ABC123', page: 2, perPage: 5 },
mockClient as any
);
expect(result.callbacks).toHaveLength(5);
expect(result.pagination.page).toBe(2);
expect(result.pagination.perPage).toBe(5);
});
it('should return empty array when no callbacks exist', async () => {
const mockResponse = mockCallbackEmptyListResponse();
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockResponse),
},
};
return apiCall(client);
});
const result = await callbackListTool.execute(
{ accountId: 'ABC123' },
mockClient as any
);
expect(result.callbacks).toHaveLength(0);
expect(result.pagination.total).toBe(0);
});
it('should return callbacks with verification status', async () => {
const mockResponse = mockCallbackListResponse(3);
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockResponse),
},
};
return apiCall(client);
});
const result = await callbackListTool.execute(
{ accountId: 'ABC123' },
mockClient as any
);
expect(result.callbacks).toHaveLength(3);
expect(result.callbacks[0]).toHaveProperty('verified');
expect(result.callbacks[0]).toHaveProperty('event');
expect(result.callbacks[0]).toHaveProperty('uri');
});
it('should handle maximum perPage value', async () => {
const mockResponse = mockCallbackListResponse(100, 1, 100);
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockResponse),
},
};
return apiCall(client);
});
const result = await callbackListTool.execute(
{ accountId: 'ABC123', perPage: 100 },
mockClient as any
);
expect(result.callbacks).toHaveLength(100);
expect(result.pagination.perPage).toBe(100);
});
});
describe('error handling', () => {
it('should handle unauthorized error', async () => {
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockUnauthorizedError()),
},
};
return apiCall(client);
});
await expect(
callbackListTool.execute({ accountId: 'ABC123' }, mockClient as any)
).rejects.toThrow();
});
it('should handle rate limit error', async () => {
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockRateLimitError(60)),
},
};
return apiCall(client);
});
await expect(
callbackListTool.execute({ accountId: 'ABC123' }, mockClient as any)
).rejects.toThrow();
});
it('should handle server error', async () => {
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockServerError()),
},
};
return apiCall(client);
});
await expect(
callbackListTool.execute({ accountId: 'ABC123' }, mockClient as any)
).rejects.toThrow();
});
it('should handle network timeout', async () => {
mockClient.executeWithRetry.mockRejectedValueOnce(mockNetworkTimeoutError());
await expect(
callbackListTool.execute({ accountId: 'ABC123' }, mockClient as any)
).rejects.toThrow();
});
it('should handle invalid account error', async () => {
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockInvalidAccountError('ABC123')),
},
};
return apiCall(client);
});
await expect(
callbackListTool.execute({ accountId: 'ABC123' }, mockClient as any)
).rejects.toThrow();
});
});
describe('input validation', () => {
it('should require accountId', async () => {
await expect(
callbackListTool.execute({} as any, mockClient as any)
).rejects.toThrow();
});
it('should reject invalid page number', async () => {
await expect(
callbackListTool.execute(
{ accountId: 'ABC123', page: 0 },
mockClient as any
)
).rejects.toThrow();
});
it('should reject perPage exceeding maximum', async () => {
await expect(
callbackListTool.execute(
{ accountId: 'ABC123', perPage: 101 },
mockClient as any
)
).rejects.toThrow();
});
it('should reject negative page number', async () => {
await expect(
callbackListTool.execute(
{ accountId: 'ABC123', page: -1 },
mockClient as any
)
).rejects.toThrow();
});
it('should reject negative perPage', async () => {
await expect(
callbackListTool.execute(
{ accountId: 'ABC123', perPage: -5 },
mockClient as any
)
).rejects.toThrow();
});
});
describe('edge cases', () => {
it('should handle request beyond last page', async () => {
const mockResponse = mockCallbackListResponse(0, 999, 30);
mockResponse.data.callbacks = [];
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockResponse),
},
};
return apiCall(client);
});
const result = await callbackListTool.execute(
{ accountId: 'ABC123', page: 999 },
mockClient as any
);
expect(result.callbacks).toHaveLength(0);
});
it('should handle callbacks with various event types', async () => {
const mockResponse = mockCallbackListResponse(5);
// Mock response creates different event types
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockResponse),
},
};
return apiCall(client);
});
const result = await callbackListTool.execute(
{ accountId: 'ABC123' },
mockClient as any
);
expect(result.callbacks).toHaveLength(5);
const eventTypes = result.callbacks.map((cb: any) => cb.event);
expect(eventTypes.length).toBe(5);
});
it('should handle callbacks with long URIs', async () => {
const mockResponse = mockCallbackListResponse(1);
mockResponse.data.callbacks[0].uri = 'https://example.com/very/long/path/to/webhook/endpoint/that/handles/events';
mockClient.executeWithRetry.mockImplementation(async (operation, apiCall) => {
const client = {
callbacks: {
list: vi.fn().mockResolvedValue(mockResponse),
},
};
return apiCall(client);
});
const result = await callbackListTool.execute(
{ accountId: 'ABC123' },
mockClient as any
);
expect(result.callbacks[0].uri).toContain('very/long/path');
});
});
});