|
| 1 | +import { addVersionMilestoneTool } from './addVersionMilestone.js'; |
| 2 | +import { jest, describe, it, expect } from '@jest/globals'; |
| 3 | +import type { Backlog } from 'backlog-js'; |
| 4 | +import { createTranslationHelper } from '../createTranslationHelper.js'; |
| 5 | + |
| 6 | +describe('addVersionMilestoneTool', () => { |
| 7 | + const mockBacklog: Partial<Backlog> = { |
| 8 | + postVersions: jest.fn<() => Promise<any>>().mockResolvedValue({ |
| 9 | + id: 1, |
| 10 | + projectId: 100, |
| 11 | + name: 'Version 1.0.0', |
| 12 | + description: 'Initial release version', |
| 13 | + startDate: '2023-01-01T00:00:00Z', |
| 14 | + releaseDueDate: '2023-03-31T00:00:00Z', |
| 15 | + archived: false, |
| 16 | + displayOrder: 1, |
| 17 | + }), |
| 18 | + }; |
| 19 | + |
| 20 | + const mockTranslationHelper = createTranslationHelper(); |
| 21 | + const tool = addVersionMilestoneTool( |
| 22 | + mockBacklog as Backlog, |
| 23 | + mockTranslationHelper |
| 24 | + ); |
| 25 | + |
| 26 | + it('returns created version milestone as formatted JSON text', async () => { |
| 27 | + const result = await tool.handler({ |
| 28 | + projectKey: 'TEST', |
| 29 | + name: 'Version 1.0.0', |
| 30 | + description: 'Initial release version', |
| 31 | + startDate: '2023-01-01T00:00:00Z', |
| 32 | + releaseDueDate: '2023-03-31T00:00:00Z', |
| 33 | + }); |
| 34 | + |
| 35 | + if (Array.isArray(result)) { |
| 36 | + throw new Error('Unexpected array result'); |
| 37 | + } |
| 38 | + expect(result.name).toEqual('Version 1.0.0'); |
| 39 | + expect(result.description).toEqual('Initial release version'); |
| 40 | + expect(result.startDate).toEqual('2023-01-01T00:00:00Z'); |
| 41 | + expect(result.releaseDueDate).toEqual('2023-03-31T00:00:00Z'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('calls backlog.postVersions with correct params when using projectKey', async () => { |
| 45 | + const params = { |
| 46 | + projectKey: 'TEST', |
| 47 | + name: 'Version 1.0.0', |
| 48 | + description: 'Initial release version', |
| 49 | + startDate: '2023-01-01T00:00:00Z', |
| 50 | + releaseDueDate: '2024-03-31T00:00:00Z', |
| 51 | + }; |
| 52 | + |
| 53 | + await tool.handler(params); |
| 54 | + |
| 55 | + expect(mockBacklog.postVersions).toHaveBeenCalledWith('TEST', { |
| 56 | + name: 'Version 1.0.0', |
| 57 | + description: 'Initial release version', |
| 58 | + startDate: '2023-01-01T00:00:00Z', |
| 59 | + releaseDueDate: '2024-03-31T00:00:00Z', |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('calls backlog.postVersions with correct params when using projectId', async () => { |
| 64 | + const params = { |
| 65 | + projectId: 100, |
| 66 | + name: 'Version 2.0.0', |
| 67 | + description: 'Major release', |
| 68 | + startDate: '2023-04-01T00:00:00Z', |
| 69 | + releaseDueDate: '2023-06-30T00:00:00Z', |
| 70 | + }; |
| 71 | + |
| 72 | + await tool.handler(params); |
| 73 | + |
| 74 | + expect(mockBacklog.postVersions).toHaveBeenCalledWith(100, { |
| 75 | + name: 'Version 2.0.0', |
| 76 | + description: 'Major release', |
| 77 | + startDate: '2023-04-01T00:00:00Z', |
| 78 | + releaseDueDate: '2023-06-30T00:00:00Z', |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('calls backlog.postVersions with minimal required params', async () => { |
| 83 | + const params = { |
| 84 | + projectKey: 'TEST', |
| 85 | + name: 'Quick Version', |
| 86 | + }; |
| 87 | + |
| 88 | + await tool.handler(params); |
| 89 | + |
| 90 | + expect(mockBacklog.postVersions).toHaveBeenCalledWith('TEST', { |
| 91 | + name: 'Quick Version', |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('throws an error if neither projectId nor projectKey is provided', async () => { |
| 96 | + const params = { |
| 97 | + // projectId and projectKey are missing |
| 98 | + name: 'Version without project', |
| 99 | + description: 'This should fail', |
| 100 | + }; |
| 101 | + |
| 102 | + await expect(tool.handler(params as any)).rejects.toThrow(Error); |
| 103 | + }); |
| 104 | +}); |
0 commit comments