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