|
| 1 | +import { type UpdateCheckResult, checkForUpdate, printUpdateNotification } from '../update-notifier.js'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +const { mockReadFile, mockWriteFile, mockMkdir } = vi.hoisted(() => ({ |
| 5 | + mockReadFile: vi.fn(), |
| 6 | + mockWriteFile: vi.fn(), |
| 7 | + mockMkdir: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('fs/promises', () => ({ |
| 11 | + readFile: mockReadFile, |
| 12 | + writeFile: mockWriteFile, |
| 13 | + mkdir: mockMkdir, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../constants.js', () => ({ |
| 17 | + PACKAGE_VERSION: '1.0.0', |
| 18 | +})); |
| 19 | + |
| 20 | +const { mockFetchLatestVersion, mockCompareVersions } = vi.hoisted(() => ({ |
| 21 | + mockFetchLatestVersion: vi.fn(), |
| 22 | + mockCompareVersions: vi.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('../commands/update/action.js', () => ({ |
| 26 | + fetchLatestVersion: mockFetchLatestVersion, |
| 27 | + compareVersions: mockCompareVersions, |
| 28 | +})); |
| 29 | + |
| 30 | +describe('checkForUpdate', () => { |
| 31 | + beforeEach(() => { |
| 32 | + vi.spyOn(Date, 'now').mockReturnValue(1708646400000); |
| 33 | + mockWriteFile.mockResolvedValue(undefined); |
| 34 | + mockMkdir.mockResolvedValue(undefined); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + vi.restoreAllMocks(); |
| 39 | + mockReadFile.mockReset(); |
| 40 | + mockWriteFile.mockReset(); |
| 41 | + mockMkdir.mockReset(); |
| 42 | + mockFetchLatestVersion.mockReset(); |
| 43 | + mockCompareVersions.mockReset(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('fetches from registry when no cache exists', async () => { |
| 47 | + mockReadFile.mockRejectedValue(new Error('ENOENT')); |
| 48 | + mockFetchLatestVersion.mockResolvedValue('2.0.0'); |
| 49 | + mockCompareVersions.mockReturnValue(1); |
| 50 | + |
| 51 | + const result = await checkForUpdate(); |
| 52 | + |
| 53 | + expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' }); |
| 54 | + expect(mockFetchLatestVersion).toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('uses cache when last check was less than 24 hours ago', async () => { |
| 58 | + const cache = JSON.stringify({ |
| 59 | + lastCheck: 1708646400000 - 1000, // 1 second ago |
| 60 | + latestVersion: '2.0.0', |
| 61 | + }); |
| 62 | + mockReadFile.mockResolvedValue(cache); |
| 63 | + mockCompareVersions.mockReturnValue(1); |
| 64 | + |
| 65 | + const result = await checkForUpdate(); |
| 66 | + |
| 67 | + expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' }); |
| 68 | + expect(mockFetchLatestVersion).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('fetches from registry when cache is expired', async () => { |
| 72 | + const cache = JSON.stringify({ |
| 73 | + lastCheck: 1708646400000 - 25 * 60 * 60 * 1000, // 25 hours ago |
| 74 | + latestVersion: '1.5.0', |
| 75 | + }); |
| 76 | + mockReadFile.mockResolvedValue(cache); |
| 77 | + mockFetchLatestVersion.mockResolvedValue('2.0.0'); |
| 78 | + mockCompareVersions.mockReturnValue(1); |
| 79 | + |
| 80 | + const result = await checkForUpdate(); |
| 81 | + |
| 82 | + expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' }); |
| 83 | + expect(mockFetchLatestVersion).toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('writes cache after fetching', async () => { |
| 87 | + mockReadFile.mockRejectedValue(new Error('ENOENT')); |
| 88 | + mockFetchLatestVersion.mockResolvedValue('2.0.0'); |
| 89 | + mockCompareVersions.mockReturnValue(1); |
| 90 | + |
| 91 | + await checkForUpdate(); |
| 92 | + |
| 93 | + expect(mockMkdir).toHaveBeenCalled(); |
| 94 | + expect(mockWriteFile).toHaveBeenCalledWith( |
| 95 | + expect.stringContaining('update-check.json'), |
| 96 | + JSON.stringify({ lastCheck: 1708646400000, latestVersion: '2.0.0' }), |
| 97 | + 'utf-8' |
| 98 | + ); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns updateAvailable: false when versions match', async () => { |
| 102 | + mockReadFile.mockRejectedValue(new Error('ENOENT')); |
| 103 | + mockFetchLatestVersion.mockResolvedValue('1.0.0'); |
| 104 | + mockCompareVersions.mockReturnValue(0); |
| 105 | + |
| 106 | + const result = await checkForUpdate(); |
| 107 | + |
| 108 | + expect(result).toEqual({ updateAvailable: false, latestVersion: '1.0.0' }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns updateAvailable: false when current is newer', async () => { |
| 112 | + mockReadFile.mockRejectedValue(new Error('ENOENT')); |
| 113 | + mockFetchLatestVersion.mockResolvedValue('0.9.0'); |
| 114 | + mockCompareVersions.mockReturnValue(-1); |
| 115 | + |
| 116 | + const result = await checkForUpdate(); |
| 117 | + |
| 118 | + expect(result).toEqual({ updateAvailable: false, latestVersion: '0.9.0' }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('returns null on fetch error', async () => { |
| 122 | + mockReadFile.mockRejectedValue(new Error('ENOENT')); |
| 123 | + mockFetchLatestVersion.mockRejectedValue(new Error('network error')); |
| 124 | + |
| 125 | + const result = await checkForUpdate(); |
| 126 | + |
| 127 | + expect(result).toBeNull(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('returns null on cache parse error and fetch error', async () => { |
| 131 | + mockReadFile.mockResolvedValue('invalid json'); |
| 132 | + mockFetchLatestVersion.mockRejectedValue(new Error('network error')); |
| 133 | + |
| 134 | + const result = await checkForUpdate(); |
| 135 | + |
| 136 | + expect(result).toBeNull(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('succeeds even when cache write fails', async () => { |
| 140 | + mockReadFile.mockRejectedValue(new Error('ENOENT')); |
| 141 | + mockFetchLatestVersion.mockResolvedValue('2.0.0'); |
| 142 | + mockCompareVersions.mockReturnValue(1); |
| 143 | + mockWriteFile.mockRejectedValue(new Error('EACCES')); |
| 144 | + |
| 145 | + const result = await checkForUpdate(); |
| 146 | + |
| 147 | + expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' }); |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +describe('printUpdateNotification', () => { |
| 152 | + it('writes notification to stderr', () => { |
| 153 | + const stderrSpy = vi.spyOn(process.stderr, 'write').mockReturnValue(true); |
| 154 | + |
| 155 | + const result: UpdateCheckResult = { updateAvailable: true, latestVersion: '2.0.0' }; |
| 156 | + printUpdateNotification(result); |
| 157 | + |
| 158 | + const output = stderrSpy.mock.calls.map(c => c[0]).join(''); |
| 159 | + expect(output).toContain('Update available:'); |
| 160 | + expect(output).toContain('1.0.0'); |
| 161 | + expect(output).toContain('2.0.0'); |
| 162 | + expect(output).toContain('npm install -g @aws/agentcore@latest'); |
| 163 | + |
| 164 | + stderrSpy.mockRestore(); |
| 165 | + }); |
| 166 | +}); |
0 commit comments