|
| 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 | +jest.mock('inquirer', () => ({ |
| 14 | + __esModule: true, |
| 15 | + default: { |
| 16 | + prompt: jest.fn(), |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +import fs from 'fs-extra'; |
| 21 | +import { execa } from 'execa'; |
| 22 | +import inquirer from 'inquirer'; |
| 23 | +import { runSave } from '../save.js'; |
| 24 | + |
| 25 | +const mockPathExists = fs.pathExists as jest.MockedFunction<typeof fs.pathExists>; |
| 26 | +const mockExeca = execa as jest.MockedFunction<typeof execa>; |
| 27 | +const mockPrompt = inquirer.prompt as jest.MockedFunction<typeof inquirer.prompt>; |
| 28 | + |
| 29 | +const cwd = '/tmp/my-repo'; |
| 30 | + |
| 31 | +describe('runSave', () => { |
| 32 | + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 33 | + const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + jest.clearAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterAll(() => { |
| 40 | + consoleErrorSpy.mockRestore(); |
| 41 | + consoleLogSpy.mockRestore(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('throws and logs error when .git directory does not exist', async () => { |
| 45 | + mockPathExists.mockResolvedValue(false); |
| 46 | + |
| 47 | + await expect(runSave(cwd)).rejects.toThrow('Not a git repository'); |
| 48 | + expect(mockPathExists).toHaveBeenCalledWith(`${cwd}/.git`); |
| 49 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 50 | + expect.stringContaining('This directory is not a git repository'), |
| 51 | + ); |
| 52 | + expect(mockExeca).not.toHaveBeenCalled(); |
| 53 | + expect(mockPrompt).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('logs "No changes to save" and returns when working tree is clean', async () => { |
| 57 | + mockPathExists.mockResolvedValue(true); |
| 58 | + mockExeca.mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 }); |
| 59 | + |
| 60 | + await runSave(cwd); |
| 61 | + |
| 62 | + expect(mockExeca).toHaveBeenCalledTimes(1); |
| 63 | + expect(mockExeca).toHaveBeenCalledWith('git', ['status', '--porcelain'], { |
| 64 | + cwd, |
| 65 | + encoding: 'utf8', |
| 66 | + }); |
| 67 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 68 | + expect.stringContaining('No changes to save'), |
| 69 | + ); |
| 70 | + expect(mockPrompt).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('logs "No changes to save" when status output is only whitespace', async () => { |
| 74 | + mockPathExists.mockResolvedValue(true); |
| 75 | + mockExeca.mockResolvedValue({ stdout: ' \n ', stderr: '', exitCode: 0 }); |
| 76 | + |
| 77 | + await runSave(cwd); |
| 78 | + |
| 79 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 80 | + expect.stringContaining('No changes to save'), |
| 81 | + ); |
| 82 | + expect(mockPrompt).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('prompts to save; when user says no, logs "Nothing has been saved"', async () => { |
| 86 | + mockPathExists.mockResolvedValue(true); |
| 87 | + mockExeca.mockResolvedValue({ stdout: ' M file.ts', stderr: '', exitCode: 0 }); |
| 88 | + mockPrompt.mockResolvedValueOnce({ saveChanges: false }); |
| 89 | + |
| 90 | + await runSave(cwd); |
| 91 | + |
| 92 | + expect(mockPrompt).toHaveBeenCalledTimes(1); |
| 93 | + expect(mockPrompt).toHaveBeenCalledWith([ |
| 94 | + expect.objectContaining({ |
| 95 | + type: 'confirm', |
| 96 | + name: 'saveChanges', |
| 97 | + message: expect.stringContaining('Would you like to save them?'), |
| 98 | + }), |
| 99 | + ]); |
| 100 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 101 | + expect.stringContaining('Nothing has been saved'), |
| 102 | + ); |
| 103 | + expect(mockExeca).toHaveBeenCalledTimes(1); // only status, no add/commit/push |
| 104 | + }); |
| 105 | + |
| 106 | + it('when user says yes but message is empty, logs "No message provided"', async () => { |
| 107 | + mockPathExists.mockResolvedValue(true); |
| 108 | + mockExeca.mockResolvedValue({ stdout: ' M file.ts', stderr: '', exitCode: 0 }); |
| 109 | + mockPrompt |
| 110 | + .mockResolvedValueOnce({ saveChanges: true }) |
| 111 | + .mockResolvedValueOnce({ message: ' ' }); |
| 112 | + |
| 113 | + await runSave(cwd); |
| 114 | + |
| 115 | + expect(mockPrompt).toHaveBeenCalledTimes(2); |
| 116 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 117 | + expect.stringContaining('No message provided'), |
| 118 | + ); |
| 119 | + expect(mockExeca).toHaveBeenCalledTimes(1); // only status |
| 120 | + }); |
| 121 | + |
| 122 | + it('when user says yes and provides message, runs add, commit, push and logs success', async () => { |
| 123 | + mockPathExists.mockResolvedValue(true); |
| 124 | + mockExeca |
| 125 | + .mockResolvedValueOnce({ stdout: ' M file.ts', stderr: '', exitCode: 0 }) |
| 126 | + .mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }) |
| 127 | + .mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }) |
| 128 | + .mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }); |
| 129 | + mockPrompt |
| 130 | + .mockResolvedValueOnce({ saveChanges: true }) |
| 131 | + .mockResolvedValueOnce({ message: 'Fix bug in save command' }); |
| 132 | + |
| 133 | + await runSave(cwd); |
| 134 | + |
| 135 | + expect(mockExeca).toHaveBeenCalledTimes(4); |
| 136 | + expect(mockExeca).toHaveBeenNthCalledWith(1, 'git', ['status', '--porcelain'], { |
| 137 | + cwd, |
| 138 | + encoding: 'utf8', |
| 139 | + }); |
| 140 | + expect(mockExeca).toHaveBeenNthCalledWith(2, 'git', ['add', '.'], { |
| 141 | + cwd, |
| 142 | + stdio: 'inherit', |
| 143 | + }); |
| 144 | + expect(mockExeca).toHaveBeenNthCalledWith(3, 'git', [ |
| 145 | + 'commit', |
| 146 | + '-m', |
| 147 | + 'Fix bug in save command', |
| 148 | + ], { cwd, stdio: 'inherit' }); |
| 149 | + expect(mockExeca).toHaveBeenNthCalledWith(4, 'git', ['push'], { |
| 150 | + cwd, |
| 151 | + stdio: 'inherit', |
| 152 | + }); |
| 153 | + expect(consoleLogSpy).toHaveBeenCalledWith( |
| 154 | + expect.stringContaining('Changes saved and pushed to GitHub successfully'), |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + it('throws and logs push-failure message when push fails with exitCode 128', async () => { |
| 159 | + mockPathExists.mockResolvedValue(true); |
| 160 | + mockExeca |
| 161 | + .mockResolvedValueOnce({ stdout: ' M file.ts', stderr: '', exitCode: 0 }) |
| 162 | + .mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }) |
| 163 | + .mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }) |
| 164 | + .mockRejectedValueOnce(Object.assign(new Error('push failed'), { exitCode: 128 })); |
| 165 | + |
| 166 | + mockPrompt |
| 167 | + .mockResolvedValueOnce({ saveChanges: true }) |
| 168 | + .mockResolvedValueOnce({ message: 'WIP' }); |
| 169 | + |
| 170 | + await expect(runSave(cwd)).rejects.toMatchObject({ message: 'push failed' }); |
| 171 | + |
| 172 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 173 | + expect.stringContaining('Push failed'), |
| 174 | + ); |
| 175 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 176 | + expect.stringContaining('remote'), |
| 177 | + ); |
| 178 | + }); |
| 179 | + |
| 180 | + it('throws and logs generic failure when add/commit/push fails with other exitCode', async () => { |
| 181 | + mockPathExists.mockResolvedValue(true); |
| 182 | + mockExeca |
| 183 | + .mockResolvedValueOnce({ stdout: ' M file.ts', stderr: '', exitCode: 0 }) |
| 184 | + .mockRejectedValueOnce(Object.assign(new Error('add failed'), { exitCode: 1 })); |
| 185 | + |
| 186 | + mockPrompt |
| 187 | + .mockResolvedValueOnce({ saveChanges: true }) |
| 188 | + .mockResolvedValueOnce({ message: 'WIP' }); |
| 189 | + |
| 190 | + await expect(runSave(cwd)).rejects.toMatchObject({ message: 'add failed' }); |
| 191 | + |
| 192 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 193 | + expect.stringContaining('Save or push failed'), |
| 194 | + ); |
| 195 | + }); |
| 196 | + |
| 197 | + it('throws and logs error when execa throws without exitCode', async () => { |
| 198 | + mockPathExists.mockResolvedValue(true); |
| 199 | + mockExeca |
| 200 | + .mockResolvedValueOnce({ stdout: ' M file.ts', stderr: '', exitCode: 0 }) |
| 201 | + .mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }) |
| 202 | + .mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 }) |
| 203 | + .mockRejectedValueOnce(new Error('network error')); |
| 204 | + |
| 205 | + mockPrompt |
| 206 | + .mockResolvedValueOnce({ saveChanges: true }) |
| 207 | + .mockResolvedValueOnce({ message: 'WIP' }); |
| 208 | + |
| 209 | + await expect(runSave(cwd)).rejects.toThrow('network error'); |
| 210 | + |
| 211 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 212 | + expect.stringContaining('An error occurred'), |
| 213 | + ); |
| 214 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 215 | + expect.stringContaining('network error'), |
| 216 | + ); |
| 217 | + }); |
| 218 | +}); |
0 commit comments