UNPKG

@tiberriver256/mcp-server-azure-devops

Version:

Azure DevOps reference server for the Model Context Protocol (MCP)

135 lines 6.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const feature_1 = require("./feature"); const handle_request_error_1 = require("../../../shared/errors/handle-request-error"); // Mock the AzureDevOpsClient jest.mock('../../../shared/api/client'); // Mock the error handler jest.mock('../../../shared/errors/handle-request-error', () => ({ handleRequestError: jest.fn(), })); describe('createWikiPage Feature', () => { let client; const mockPut = jest.fn(); const mockHandleRequestError = handle_request_error_1.handleRequestError; const defaultParams = { wikiId: 'test-wiki', content: 'Hello world', pagePath: '/', }; beforeEach(() => { // Reset mocks for each test mockPut.mockReset(); mockHandleRequestError.mockReset(); client = { put: mockPut, defaults: { organizationId: 'defaultOrg', projectId: 'defaultProject', }, }; }); it('should call client.put with correct URL and data for default org and project', async () => { mockPut.mockResolvedValue({ data: { some: 'response' } }); await (0, feature_1.createWikiPage)(defaultParams, client); expect(mockPut).toHaveBeenCalledTimes(1); expect(mockPut).toHaveBeenCalledWith('defaultOrg/defaultProject/_apis/wiki/wikis/test-wiki/pages?path=%2F&api-version=7.1-preview.1', { content: 'Hello world' }); }); it('should call client.put with correct URL when projectId is explicitly provided', async () => { mockPut.mockResolvedValue({ data: { some: 'response' } }); const paramsWithProject = { ...defaultParams, projectId: 'customProject', }; await (0, feature_1.createWikiPage)(paramsWithProject, client); expect(mockPut).toHaveBeenCalledWith('defaultOrg/customProject/_apis/wiki/wikis/test-wiki/pages?path=%2F&api-version=7.1-preview.1', { content: 'Hello world' }); }); it('should call client.put with correct URL when organizationId is explicitly provided', async () => { mockPut.mockResolvedValue({ data: { some: 'response' } }); const paramsWithOrg = { ...defaultParams, organizationId: 'customOrg', }; await (0, feature_1.createWikiPage)(paramsWithOrg, client); expect(mockPut).toHaveBeenCalledWith('customOrg/defaultProject/_apis/wiki/wikis/test-wiki/pages?path=%2F&api-version=7.1-preview.1', { content: 'Hello world' }); }); it('should call client.put with correct URL when projectId is null (project-level wiki)', async () => { mockPut.mockResolvedValue({ data: { some: 'response' } }); const paramsWithNullProject = { ...defaultParams, projectId: null, // Explicitly null for project-level resources that don't need a project }; // Client default for projectId should also be null or undefined in this scenario const clientWithoutProject = { put: mockPut, defaults: { organizationId: 'defaultOrg', projectId: undefined, }, }; await (0, feature_1.createWikiPage)(paramsWithNullProject, clientWithoutProject); expect(mockPut).toHaveBeenCalledWith('defaultOrg/_apis/wiki/wikis/test-wiki/pages?path=%2F&api-version=7.1-preview.1', { content: 'Hello world' }); }); it('should correctly encode pagePath in the URL', async () => { mockPut.mockResolvedValue({ data: { some: 'response' } }); const paramsWithPath = { ...defaultParams, pagePath: '/My Test Page/Sub Page', }; await (0, feature_1.createWikiPage)(paramsWithPath, client); expect(mockPut).toHaveBeenCalledWith('defaultOrg/defaultProject/_apis/wiki/wikis/test-wiki/pages?path=%2FMy%20Test%20Page%2FSub%20Page&api-version=7.1-preview.1', { content: 'Hello world' }); }); it('should use default pagePath "/" if pagePath is null', async () => { mockPut.mockResolvedValue({ data: { some: 'response' } }); const paramsWithPath = { ...defaultParams, pagePath: null, // Explicitly null }; await (0, feature_1.createWikiPage)(paramsWithPath, client); expect(mockPut).toHaveBeenCalledWith('defaultOrg/defaultProject/_apis/wiki/wikis/test-wiki/pages?path=%2F&api-version=7.1-preview.1', { content: 'Hello world' }); }); it('should include comment in request body when provided', async () => { mockPut.mockResolvedValue({ data: { some: 'response' } }); const paramsWithComment = { ...defaultParams, comment: 'Initial page creation', }; await (0, feature_1.createWikiPage)(paramsWithComment, client); expect(mockPut).toHaveBeenCalledWith('defaultOrg/defaultProject/_apis/wiki/wikis/test-wiki/pages?path=%2F&api-version=7.1-preview.1', { content: 'Hello world', comment: 'Initial page creation' }); }); it('should return the data from the response on success', async () => { const expectedResponse = { id: '123', path: '/', content: 'Hello world' }; mockPut.mockResolvedValue({ data: expectedResponse }); const result = await (0, feature_1.createWikiPage)(defaultParams, client); expect(result).toEqual(expectedResponse); }); // Skip this test for now as it requires complex mocking of environment variables it.skip('should throw if organizationId is not provided and not set in defaults', async () => { const clientWithoutOrg = { put: mockPut, defaults: { projectId: 'defaultProject', organizationId: undefined, }, }; const paramsNoOrg = { ...defaultParams, organizationId: null, // Explicitly null and no default }; // This test is skipped because it requires complex mocking of environment variables // which is difficult to do in the current test setup await expect((0, feature_1.createWikiPage)(paramsNoOrg, clientWithoutOrg)).rejects.toThrow('Organization ID is not defined. Please provide it or set a default.'); expect(mockPut).not.toHaveBeenCalled(); }); it('should call handleRequestError if client.put throws an error', async () => { const error = new Error('API Error'); mockPut.mockRejectedValue(error); mockHandleRequestError.mockImplementation(() => { throw new Error('Handled Error'); }); await expect((0, feature_1.createWikiPage)(defaultParams, client)).rejects.toThrow('Handled Error'); expect(mockHandleRequestError).toHaveBeenCalledTimes(1); expect(mockHandleRequestError).toHaveBeenCalledWith(error, 'Failed to create or update wiki page'); }); }); //# sourceMappingURL=feature.spec.unit.js.map