|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { shouldReportCrash } from '../error'; |
| 4 | + |
| 5 | +describe('shouldReportCrash', () => { |
| 6 | + it('should return false when isInteractive is false', async () => { |
| 7 | + const result = await shouldReportCrash({ |
| 8 | + error: new Error('test error'), |
| 9 | + isInteractive: false, |
| 10 | + }); |
| 11 | + expect(result).toBe(false); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should return false when isInteractive is undefined', async () => { |
| 15 | + const result = await shouldReportCrash({ |
| 16 | + error: new Error('test error'), |
| 17 | + isInteractive: undefined, |
| 18 | + }); |
| 19 | + expect(result).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should not prompt when isInteractive is explicitly false', async () => { |
| 23 | + // Mock stdin/stdout to ensure we don't wait for user input |
| 24 | + const writeSpy = vi |
| 25 | + .spyOn(process.stdout, 'write') |
| 26 | + .mockImplementation(() => true); |
| 27 | + const setEncodingSpy = vi |
| 28 | + .spyOn(process.stdin, 'setEncoding') |
| 29 | + .mockImplementation(() => {}); |
| 30 | + const onceSpy = vi |
| 31 | + .spyOn(process.stdin, 'once') |
| 32 | + .mockImplementation(() => process.stdin); |
| 33 | + |
| 34 | + const result = await shouldReportCrash({ |
| 35 | + error: new Error('test error'), |
| 36 | + isInteractive: false, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(result).toBe(false); |
| 40 | + expect(writeSpy).not.toHaveBeenCalled(); |
| 41 | + expect(setEncodingSpy).not.toHaveBeenCalled(); |
| 42 | + expect(onceSpy).not.toHaveBeenCalled(); |
| 43 | + |
| 44 | + writeSpy.mockRestore(); |
| 45 | + setEncodingSpy.mockRestore(); |
| 46 | + onceSpy.mockRestore(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should prompt when isInteractive is true', async () => { |
| 50 | + // Mock stdin/stdout for interactive session |
| 51 | + const writeSpy = vi |
| 52 | + .spyOn(process.stdout, 'write') |
| 53 | + .mockImplementation(() => true); |
| 54 | + const setEncodingSpy = vi |
| 55 | + .spyOn(process.stdin, 'setEncoding') |
| 56 | + .mockImplementation(() => {}); |
| 57 | + const onceSpy = vi |
| 58 | + .spyOn(process.stdin, 'once') |
| 59 | + .mockImplementation((event, callback) => { |
| 60 | + // Simulate user typing 'n' |
| 61 | + setTimeout(() => { |
| 62 | + (callback as any)('n'); |
| 63 | + }, 0); |
| 64 | + return process.stdin; |
| 65 | + }); |
| 66 | + |
| 67 | + const result = await shouldReportCrash({ |
| 68 | + error: new Error('test error'), |
| 69 | + isInteractive: true, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(result).toBe(false); // User said 'n' |
| 73 | + expect(writeSpy).toHaveBeenCalledWith( |
| 74 | + expect.stringContaining('📢 Open a GitHub issue with crash details?'), |
| 75 | + ); |
| 76 | + |
| 77 | + writeSpy.mockRestore(); |
| 78 | + setEncodingSpy.mockRestore(); |
| 79 | + onceSpy.mockRestore(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle user saying yes to crash report', async () => { |
| 83 | + // Mock stdin/stdout for interactive session |
| 84 | + const writeSpy = vi |
| 85 | + .spyOn(process.stdout, 'write') |
| 86 | + .mockImplementation(() => true); |
| 87 | + const setEncodingSpy = vi |
| 88 | + .spyOn(process.stdin, 'setEncoding') |
| 89 | + .mockImplementation(() => {}); |
| 90 | + const onceSpy = vi |
| 91 | + .spyOn(process.stdin, 'once') |
| 92 | + .mockImplementation((event, callback) => { |
| 93 | + // Simulate user typing 'y' |
| 94 | + setTimeout(() => { |
| 95 | + (callback as any)('y'); |
| 96 | + }, 0); |
| 97 | + return process.stdin; |
| 98 | + }); |
| 99 | + |
| 100 | + const result = await shouldReportCrash({ |
| 101 | + error: new Error('test error'), |
| 102 | + isInteractive: true, |
| 103 | + }); |
| 104 | + |
| 105 | + expect(result).toBe(true); // User said 'y' |
| 106 | + |
| 107 | + writeSpy.mockRestore(); |
| 108 | + setEncodingSpy.mockRestore(); |
| 109 | + onceSpy.mockRestore(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments