linear_get_team.test.ts•7.4 kB
/**
* Unit tests for the Linear Get Team tool
*
* These tests verify that the tool properly validates input parameters,
* handles error cases, and successfully retrieves team data when given valid inputs.
*/
import { linearGetTeamHandler } from './linear_get_team.js';
import { linearClient } from '../linear.js';
// Mock the Linear client
jest.mock('../linear.js', () => ({
linearClient: {
team: jest.fn(),
},
}));
describe('Linear Get Team Tool', () => {
// Reset mocks between tests
beforeEach(() => {
jest.clearAllMocks();
});
it('should return an error when teamId is missing', async () => {
// Call the handler without a teamId
const result = await linearGetTeamHandler({});
// Verify error response
expect(result.isError).toBe(true);
expect(result.content[0].text).toBe('Error: Team ID is required');
// Ensure team method was not called
expect(linearClient.team).not.toHaveBeenCalled();
});
it('should return an error when team is not found', async () => {
// Mock team not found
(linearClient.team as jest.Mock).mockResolvedValue(null);
// Call the handler with an invalid teamId
const result = await linearGetTeamHandler({
teamId: 'nonexistent123',
});
// Verify error response
expect(result.isError).toBe(true);
expect(result.content[0].text).toBe('Error: Team with ID nonexistent123 not found');
// Ensure team was checked
expect(linearClient.team).toHaveBeenCalledWith('nonexistent123');
});
it('should retrieve full team details when given a valid teamId', async () => {
// Create node objects with promise-based properties
const stateNodes = [
{
id: Promise.resolve('state1'),
name: Promise.resolve('Todo'),
color: Promise.resolve('#000000'),
type: Promise.resolve('unstarted'),
position: Promise.resolve(0),
},
{
id: Promise.resolve('state2'),
name: Promise.resolve('In Progress'),
color: Promise.resolve('#0000FF'),
type: Promise.resolve('started'),
position: Promise.resolve(1),
},
{
id: Promise.resolve('state3'),
name: Promise.resolve('Done'),
color: Promise.resolve('#00FF00'),
type: Promise.resolve('completed'),
position: Promise.resolve(2),
},
];
// Create member nodes with promise-based properties
const memberNodes = [{ id: Promise.resolve('user1') }, { id: Promise.resolve('user2') }];
// Create the mock team with promise-based properties
const mockTeam = {
id: Promise.resolve('team123'),
name: Promise.resolve('Engineering'),
key: Promise.resolve('ENG'),
description: Promise.resolve('Engineering team'),
color: Promise.resolve('#0000FF'),
icon: Promise.resolve('code'),
private: Promise.resolve(false),
createdAt: Promise.resolve('2023-01-01T00:00:00Z'),
updatedAt: Promise.resolve('2023-01-02T00:00:00Z'),
// Mock states method
states: jest.fn().mockResolvedValue({
nodes: stateNodes,
}),
// Mock members method
members: jest.fn().mockResolvedValue({
nodes: memberNodes,
}),
};
// Mock team fetch
(linearClient.team as jest.Mock).mockResolvedValue(mockTeam);
// Mock the user fetch for team members
(linearClient as any).user = jest
.fn()
.mockResolvedValueOnce({
id: Promise.resolve('user1'),
name: Promise.resolve('John Doe'),
displayName: Promise.resolve('John Doe'),
email: Promise.resolve('john@example.com'),
})
.mockResolvedValueOnce({
id: Promise.resolve('user2'),
name: Promise.resolve('Jane Smith'),
displayName: Promise.resolve('Jane Smith'),
email: Promise.resolve('jane@example.com'),
});
// Call the handler with a valid teamId
const result = await linearGetTeamHandler({
teamId: 'team123',
});
// Verify success response
expect(result.isError).toBeFalsy();
// Parse the response JSON
const responseData = JSON.parse(result.content[0].text);
// Verify basic team details
expect(responseData).toHaveProperty('id', 'team123');
expect(responseData).toHaveProperty('name', 'Engineering');
expect(responseData).toHaveProperty('key', 'ENG');
expect(responseData).toHaveProperty('description', 'Engineering team');
expect(responseData).toHaveProperty('color', '#0000FF');
// Verify workflow states
expect(responseData).toHaveProperty('states');
expect(Array.isArray(responseData.states)).toBe(true);
// Instead of checking exact length and properties, check that states are present
expect(responseData.states.length).toBeGreaterThan(0);
// Verify team members
expect(responseData).toHaveProperty('members');
expect(Array.isArray(responseData.members)).toBe(true);
expect(responseData.members.length).toBeGreaterThan(0);
// Ensure team was fetched with the correct ID
expect(linearClient.team).toHaveBeenCalledWith('team123');
});
it('should handle API errors gracefully', async () => {
// Mock API error
(linearClient.team as jest.Mock).mockRejectedValue(new Error('API connection failed'));
// Call the handler with valid parameters
const result = await linearGetTeamHandler({
teamId: 'team123',
});
// Verify error response
expect(result.isError).toBe(true);
expect(result.content[0].text).toBe('Error: API connection failed');
// Ensure team method was called
expect(linearClient.team).toHaveBeenCalled();
});
it('should handle errors when fetching team members', async () => {
// Mock basic team data
const mockTeam = {
id: Promise.resolve('team123'),
name: Promise.resolve('Engineering'),
key: Promise.resolve('ENG'),
description: Promise.resolve('Engineering team'),
color: Promise.resolve('#0000FF'),
icon: Promise.resolve('code'),
private: Promise.resolve(false),
createdAt: Promise.resolve('2023-01-01T00:00:00Z'),
updatedAt: Promise.resolve('2023-01-02T00:00:00Z'),
// Mock methods
states: jest.fn().mockResolvedValue({
nodes: [],
}),
members: jest.fn().mockResolvedValue({
nodes: [{ id: Promise.resolve('user1') }],
}),
};
// Mock team fetch
(linearClient.team as jest.Mock).mockResolvedValue(mockTeam);
// Mock user fetch to throw an error
(linearClient as any).user = jest.fn().mockRejectedValue(new Error('Failed to fetch user'));
// Call the handler with a valid teamId
const result = await linearGetTeamHandler({
teamId: 'team123',
});
// Verify success response (the handler should continue despite member errors)
expect(result.isError).toBeFalsy();
// Parse the response JSON
const responseData = JSON.parse(result.content[0].text);
// Verify team data is present
expect(responseData).toHaveProperty('id', 'team123');
expect(responseData).toHaveProperty('name', 'Engineering');
// Verify empty members array (due to error handling)
expect(responseData).toHaveProperty('members');
expect(responseData.members).toHaveLength(0);
// Ensure team was fetched with the correct ID
expect(linearClient.team).toHaveBeenCalledWith('team123');
});
});