|
| 1 | +jest.mock('fs-extra', () => ({ |
| 2 | + __esModule: true, |
| 3 | + default: { |
| 4 | + pathExists: jest.fn(), |
| 5 | + }, |
| 6 | +})); |
| 7 | + |
| 8 | +jest.mock('execa', () => ({ |
| 9 | + __esModule: true, |
| 10 | + execa: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +import fs from 'fs-extra'; |
| 14 | +import { execa } from 'execa'; |
| 15 | +import { runLoad } from '../load.js'; |
| 16 | + |
| 17 | +const mockPathExists = fs.pathExists as jest.MockedFunction<typeof fs.pathExists>; |
| 18 | +const mockExeca = execa as jest.MockedFunction<typeof execa>; |
| 19 | + |
| 20 | +const cwd = '/tmp/my-repo'; |
| 21 | + |
| 22 | +describe('runLoad', () => { |
| 23 | + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 24 | + const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + afterAll(() => { |
| 31 | + consoleErrorSpy.mockRestore(); |
| 32 | + consoleLogSpy.mockRestore(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('throws and logs error when .git directory does not exist', async () => { |
| 36 | + mockPathExists.mockResolvedValue(false); |
| 37 | + |
| 38 | + await expect(runLoad(cwd)).rejects.toThrow('Not a git repository'); |
| 39 | + expect(mockPathExists).toHaveBeenCalledWith(`${cwd}/.git`); |
| 40 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 41 | + expect.stringContaining('This directory is not a git repository'), |
| 42 | + ); |
| 43 | + expect(mockExeca).not.toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('runs git pull and logs success when repo exists', async () => { |
| 47 | + mockPathExists.mockResolvedValue(true); |
| 48 | + mockExeca.mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>); |
| 49 | + |
| 50 | + await runLoad(cwd); |
| 51 | + |
| 52 | + expect(mockExeca).toHaveBeenCalledTimes(1); |
| 53 | + expect(mockExeca).toHaveBeenCalledWith('git', ['pull'], { |
| 54 | + cwd, |
| 55 | + stdio: 'inherit', |
| 56 | + }); |
| 57 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 58 | + expect.stringContaining('Pulling latest updates from GitHub'), |
| 59 | + ); |
| 60 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 61 | + expect.stringContaining('Latest updates loaded successfully'), |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('throws and logs pull-failure message when pull fails with exitCode 128', async () => { |
| 66 | + mockPathExists.mockResolvedValue(true); |
| 67 | + mockExeca.mockRejectedValueOnce(Object.assign(new Error('pull failed'), { exitCode: 128 })); |
| 68 | + |
| 69 | + await expect(runLoad(cwd)).rejects.toMatchObject({ message: 'pull failed' }); |
| 70 | + |
| 71 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 72 | + expect.stringContaining('Pull failed'), |
| 73 | + ); |
| 74 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 75 | + expect.stringContaining('remote'), |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('throws and logs generic failure when pull fails with other exitCode', async () => { |
| 80 | + mockPathExists.mockResolvedValue(true); |
| 81 | + mockExeca.mockRejectedValueOnce(Object.assign(new Error('pull failed'), { exitCode: 1 })); |
| 82 | + |
| 83 | + await expect(runLoad(cwd)).rejects.toMatchObject({ message: 'pull failed' }); |
| 84 | + |
| 85 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 86 | + expect.stringContaining('Pull failed'), |
| 87 | + ); |
| 88 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 89 | + expect.stringContaining('See the output above'), |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it('throws and logs error when execa throws without exitCode', async () => { |
| 94 | + mockPathExists.mockResolvedValue(true); |
| 95 | + mockExeca.mockRejectedValueOnce(new Error('network error')); |
| 96 | + |
| 97 | + await expect(runLoad(cwd)).rejects.toThrow('network error'); |
| 98 | + |
| 99 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 100 | + expect.stringContaining('An error occurred'), |
| 101 | + ); |
| 102 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 103 | + expect.stringContaining('network error'), |
| 104 | + ); |
| 105 | + }); |
| 106 | +}); |
0 commit comments