|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { UpdaterService } from '../../src/services'; |
| 3 | +import { Updater } from '../../src/interface'; |
| 4 | +import { PlatformDetectionError, VersionBumpError } from '../../src/errors'; |
| 5 | + |
| 6 | +const mockNodeUpdater: Updater = { |
| 7 | + platform: 'node', |
| 8 | + canHandle: vi.fn(), |
| 9 | + getCurrentVersion: vi.fn(), |
| 10 | + bumpVersion: vi.fn(), |
| 11 | +}; |
| 12 | + |
| 13 | +const mockPythonUpdater: Updater = { |
| 14 | + platform: 'python', |
| 15 | + canHandle: vi.fn(), |
| 16 | + getCurrentVersion: vi.fn(), |
| 17 | + bumpVersion: vi.fn(), |
| 18 | +}; |
| 19 | + |
| 20 | +const mockUpdaters: Updater[] = [mockNodeUpdater, mockPythonUpdater]; |
| 21 | + |
| 22 | +describe('UpdaterService', () => { |
| 23 | + let updaterService: UpdaterService; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + updaterService = new UpdaterService(mockUpdaters); |
| 27 | + vi.clearAllMocks(); |
| 28 | + mockNodeUpdater.canHandle.mockReset(); |
| 29 | + mockNodeUpdater.bumpVersion.mockReset(); |
| 30 | + mockPythonUpdater.canHandle.mockReset(); |
| 31 | + mockPythonUpdater.bumpVersion.mockReset(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('detectPlatform', () => { |
| 35 | + it('should return the platform of the first updater that can handle', () => { |
| 36 | + mockNodeUpdater.canHandle.mockReturnValue(false); |
| 37 | + mockPythonUpdater.canHandle.mockReturnValue(true); |
| 38 | + |
| 39 | + expect(updaterService.detectPlatform()).toBe('python'); |
| 40 | + expect(mockNodeUpdater.canHandle).toHaveBeenCalled(); |
| 41 | + expect(mockPythonUpdater.canHandle).toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should throw PlatformDetectionError if no updater can handle', () => { |
| 45 | + mockNodeUpdater.canHandle.mockReturnValue(false); |
| 46 | + mockPythonUpdater.canHandle.mockReturnValue(false); |
| 47 | + |
| 48 | + expect(() => updaterService.detectPlatform()).toThrow(PlatformDetectionError); |
| 49 | + expect(() => updaterService.detectPlatform()).toThrow('Could not detect platform.'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('updateVersion', () => { |
| 54 | + it('should call bumpVersion on the correct updater and return the new version', () => { |
| 55 | + const newVersion = '1.2.4'; |
| 56 | + mockNodeUpdater.bumpVersion.mockReturnValue(newVersion); |
| 57 | + |
| 58 | + const result = updaterService.updateVersion('node', 'patch'); |
| 59 | + |
| 60 | + expect(result).toBe(newVersion); |
| 61 | + expect(mockNodeUpdater.bumpVersion).toHaveBeenCalledWith('patch'); |
| 62 | + expect(mockPythonUpdater.bumpVersion).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should throw VersionBumpError if no updater is found for the platform', () => { |
| 66 | + expect(() => updaterService.updateVersion('nonexistent', 'patch')).toThrow(VersionBumpError); |
| 67 | + expect(() => updaterService.updateVersion('nonexistent', 'patch')).toThrow( |
| 68 | + 'No updater found for platform: nonexistent', |
| 69 | + ); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments