Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

feedback.test.ts 4.3 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...