1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
- import { gatherFeedback, sendFeedback } from '../src/feedback';
- import { fetchWithProxy } from '../src/fetch';
- import logger from '../src/logger';
- import * as readlineUtils from '../src/util/readline';
- const actualFeedback = jest.requireActual('../src/feedback');
- jest.mock('../src/fetch', () => ({
- fetchWithProxy: jest.fn(),
- }));
- jest.mock('../src/logger', () => ({
- info: jest.fn(),
- error: jest.fn(),
- }));
- jest.mock('../src/globalConfig/accounts', () => ({
- getUserEmail: jest.fn(),
- }));
- // Mock the readline utilities
- jest.mock('../src/util/readline', () => ({
- promptUser: jest.fn(),
- promptYesNo: jest.fn(),
- createReadlineInterface: jest.fn(),
- }));
- jest.mock('../src/feedback', () => {
- return {
- sendFeedback: jest.fn(),
- gatherFeedback: jest.fn(),
- };
- });
- const createMockResponse = (data: any): Response => {
- return {
- ok: data.ok,
- status: data.status || 200,
- statusText: data.statusText || '',
- headers: new Headers(),
- redirected: false,
- type: 'basic',
- url: '',
- json: async () => data,
- text: async () => '',
- arrayBuffer: async () => new ArrayBuffer(0),
- blob: async () => new Blob(),
- formData: async () => new FormData(),
- bodyUsed: false,
- body: null,
- clone: () => createMockResponse(data),
- } as Response;
- };
- describe('Feedback Module', () => {
- const originalConsoleLog = console.log;
- beforeEach(() => {
- jest.clearAllMocks();
- jest.spyOn(console, 'log').mockImplementation();
- });
- afterEach(() => {
- console.log = originalConsoleLog;
- });
- describe('sendFeedback', () => {
- beforeEach(() => {
- jest.mocked(sendFeedback).mockImplementation(actualFeedback.sendFeedback);
- });
- it('should send feedback successfully', async () => {
- const mockResponse = createMockResponse({ ok: true });
- jest.mocked(fetchWithProxy).mockResolvedValueOnce(mockResponse);
- await sendFeedback('Test feedback');
- // Verify fetch was called with correct parameters
- expect(fetchWithProxy).toHaveBeenCalledWith(
- 'https://api.promptfoo.dev/api/feedback',
- expect.objectContaining({
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ message: 'Test feedback' }),
- }),
- );
- expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Feedback sent'));
- });
- it('should handle API failure', async () => {
- const mockResponse = createMockResponse({ ok: false, status: 500 });
- jest.mocked(fetchWithProxy).mockResolvedValueOnce(mockResponse);
- await sendFeedback('Test feedback');
- expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Failed to send feedback'));
- });
- it('should handle network errors', async () => {
- jest.mocked(fetchWithProxy).mockRejectedValueOnce(new Error('Network error'));
- await sendFeedback('Test feedback');
- expect(logger.error).toHaveBeenCalledWith('Network error while sending feedback');
- });
- it('should not send empty feedback', async () => {
- await sendFeedback('');
- expect(fetchWithProxy).not.toHaveBeenCalled();
- });
- });
- describe('gatherFeedback', () => {
- it('should send feedback directly if a message is provided', async () => {
- jest.mocked(gatherFeedback).mockImplementation(async (message) => {
- if (message) {
- await sendFeedback(message);
- }
- });
- jest.mocked(sendFeedback).mockReset();
- await gatherFeedback('Direct feedback');
- expect(sendFeedback).toHaveBeenCalledWith('Direct feedback');
- });
- it('should handle empty feedback input', async () => {
- // Mock promptUser to return empty string
- jest.mocked(readlineUtils.promptUser).mockResolvedValueOnce(' ');
- jest.mocked(gatherFeedback).mockImplementation(actualFeedback.gatherFeedback);
- await gatherFeedback();
- expect(sendFeedback).not.toHaveBeenCalled();
- });
- it('should handle errors during feedback gathering', async () => {
- // Mock promptUser to throw an error
- jest.mocked(readlineUtils.promptUser).mockRejectedValueOnce(new Error('Test error'));
- jest.mocked(gatherFeedback).mockImplementation(actualFeedback.gatherFeedback);
- await gatherFeedback();
- expect(logger.error).toHaveBeenCalledWith(
- expect.stringContaining('Error gathering feedback'),
- );
- });
- });
- });
|